﻿/*  
Author: Felix Harle
This script uses highlight data as input
(different people, same text) and outputs
a visualization of it in the context of the original
text in one InDesign document.
*/

//Input/////////////////////////////////////////////////////////////////////////////////////////////

var text_data = "I would begin the analysis by saying that a commodity has value and a gift does not. A gift has worth. I’m obviously using these terms in a particular sense. I mean worth to refer to those things we prize and yet say you can’t put a price on it. We derive value, on the other hand, from the comparison of one thing with another. I cannot express the value of linen in terms of linen, says Marx in the classic analysis of commodities which opens his Capital. Value needs a difference for its expression; when there is no difference we are left with tautology (a yard of linen is a yard of linen)."; // original the text

// highlight data - generated with our HTML to array tool
var highlight_data = [];
highlight_data[0] = "commodity has value";
highlight_data[1] = "A gift has worth";
highlight_data[2] = "worth to refer to those things we prize and yet say you can’t put a price on it.";
highlight_data[3] = "value";
highlight_data[4] = "comparison";
highlight_data[5] = "I cannot express the value of linen in terms of linen,";
highlight_data[6] = "Value needs a difference for its expression";
highlight_data[7] = "saying that a commodity has value and a gift does not";
highlight_data[8] = "a particular sense";
highlight_data[9] = "worth";
highlight_data[10] = "those things we prize";
highlight_data[11] = "I cannot express the value of linen in terms of linen,";
highlight_data[12] = "his Capital";
highlight_data[13] = "difference";
highlight_data[14] = "when there is no difference we are left with tautology";
highlight_data[15] = "a commodity has value and a gift does not";
highlight_data[16] = "worth";
highlight_data[17] = "things we prize";
highlight_data[18] = "can’t put a price on it";
highlight_data[19] = "I cannot express the value of linen in terms of linen, says Marx";
highlight_data[20] = "a difference";
highlight_data[21] = "mean worth";
highlight_data[22] = "things we prize";
highlight_data[23] = "can’t put a price on it";
highlight_data[24] = "I cannot express the value of linen in terms of linen, says Marx";
highlight_data[25] = "Value";
highlight_data[26] = "difference for its expression";

var people_count = 4; // used to calculate the opacity of the highlights below
					  // e.g. 100 % / 4 = 25 % 
					  // a word everyone highlighted is at 100 %

//Global declarations & assignments/////////////////////////////////////////////////////////////////

var page_width = 210; // canvas width in mm
var page_height = 300; // canvas height in mm

var font_size = 30; // reduce this if your text is long and doesn't fit in the text frame

var text_frame = []; // array that will hold the text frames w/ highlight data

//Document setup, spec. of highlight color swatch///////////////////////////////////////////////////

var doc = app.documents.add({
	documentPreferences:{
    	pageWidth : page_width,
    	pageHeight: page_height,
    	facingPages : false
    }
});

var highlight_color = doc.colors.add();
highlight_color.properties = {
	name:"highlight_color", 
	model:ColorModel.PROCESS,
	space:ColorSpace.RGB,
	colorValue:[244,239,122]
};

//Search setup//////////////////////////////////////////////////////////////////////////////////////

app.findTextPreferences = NothingEnum.nothing;
app.changeTextPreferences = NothingEnum.nothing;

app.findChangeTextOptions.includeFootnotes = true;
app.findChangeTextOptions.includeHiddenLayers = false;
app.findChangeTextOptions.includeLockedLayersForFind = false;
app.findChangeTextOptions.includeLockedStoriesForFind = true;
app.findChangeTextOptions.includeMasterPages = true;
app.findChangeTextOptions.wholeWord = 1;

//Creation of a text frame for each highlight, assignment of highlight data//////////////////////////

for (var i = 0; i < highlight_data.length; i++) {
	// add box
	text_frame[i] = doc.pages.item(0).textFrames.add({              
		geometricBounds:[12.7,12.7,297 - 12.7, 210 - 12.7],
		contents: text_data
	});
	// assign styles
	text_frame[i].paragraphs.item(0).properties = {
		pointSize: font_size,
		leading: 43,
		fillTint: 0,
		underlineColor: highlight_color,
		underlineWeight: 40,
		underlineOffset: -9,
		underlineTint: 0
	};
	// calculate and set opacity
	text_frame[i].transparencySettings.blendingSettings.opacity = 100 / people_count; 
	// search for highlight
	app.findTextPreferences.findWhat = highlight_data[i];
	// underline, set tint to 100%
	app.changeTextPreferences.underline = true;
	app.changeTextPreferences.underlineTint = 100;
	// save changes for current text frame (e.g. text_frame[0])
	text_frame[i].changeText();
	// clear "search field"
	app.findTextPreferences = NothingEnum.nothing;
	app.changeTextPreferences = NothingEnum.nothing;
}

//Setup of first layer in document; displays text, has no highlights assigned///////////////////////

var text_frame_stock = doc.pages.item(0).textFrames.add({                
	geometricBounds:[12.7,12.7,297 - 12.7, 210 - 12.7],
	contents: text_data
});
text_frame_stock.paragraphs.item(0).properties = {
	pointSize: font_size,
	leading: 43,
	underlineColor: highlight_color,
	underlineWeight: 40,
	underlineOffset: -9,
	underlineTint: 0
};