Stylesheet Development with PTC ALD > Adding PTC APP Code to Stylesheet Source > Samples > Testing > Test Line Number on Page and Apply Formatting Properties
  
Test Line Number on Page and Apply Formatting Properties
Samples directory: Arbortext-path/samples/APP/Testing/LineOnPage
Sample file: testLineOnPage.xml
Main stylesheet: testLineOnPage.style
Stylesheet modules: baseStylesheet.style and testLineOnPagePropertySets.style
The sample is based on the standard axdocbook document type. It demonstrates how to test the line on a page on which a paragraph starts. The value of the line number can then be used to decide the text color to be applied to the text content of the paragraph.
Refer to the para element in the sample file Arbortext-path/samples/APP/Testing/LineOnPage/LineOnPage.xml. The para in chapter context in the associated stylesheet references the line on page test property set. The property set includes PTC APP source code edits to test the line number and drive text color accordingly. Access the source code for the property set by selecting it in the Property Sets list, and using the Edit > Edit Property Set Source > APP menu option.
The relevant code from the property set is given below:
var lineOnPage = formatting.evaluateShowString("$^01612") + 1;


//Use the lineOnPage value to set the text colour
switch (lineOnPage % 3) {
case 0:
style.color="red";
break;
case 1:
style.color="green";
break;
case 2:
style.color="blue";
break;
}
Extract the current line number
var lineOnPage = formatting.evaluateShowString("$^01612") + 1;
Create the variable lineOnPage to hold the current line number.
The phrase = formatting.evaluateShowString("$^01612") + 1 calls the evaluateShowString() method of the fFormatting object to query the current value of the PTC APP getvar (variable) numbered 01612, using a show string. This getvar contains the number of the current line on the page (note that the first line is numbered 0). Since the property set that holds the code is referenced by the para in chapter context, the line number will be extracted when that context begins. The phrase will also increment the current getvar value by 1 whenever the context is encountered (+ 1). The current line number will be stored as the current value of the lineOnPage variable.
You reference this variable later, when setting text color for the paragraph.
* 
An PTC APP showstring is a sequence of characters that identifies a particular piece of system or document information. A showstring contains a question or instruction defined by its syntax and will count, set, test, pass, call in and/or output information based on the answers to these questions. The syntax $^01612 is the established method of outputting the value of a showstring.
Set the text color for the text content of the current element, based on the line number
switch (lineOnPage % 3) {
case 0:
style.color="red";
break;
case 1:
style.color="green";
break;
case 2:
style.color="blue";
break;
}
Use a switch statement to create a list of possible options (cases) for processing the value of the lineOnPage variable.
Note the % 3 entry for the value of lineOnPage. The JavaScript operator % interprets the current value in the form of the remainder left when the value is divided by the supplied number. With % 3 in operation, the current value of lineOnPage will be provided as the remainder when the value is divided by 3. For example, if the actual value of lineOnPage is 31, it will be given as 1. Including this operator ensures that the current line number is always given as a value between 0 and 2, regardless of the actual number of lines in a document.
The switch statement works in three stages:
1. The value of the variable lineOnPage is evaluated.
2. The value of lineOnPage is compared with the values for each case entry.
3. If the value of lineOnPage matches a case entry in the switch statement, the block of code associated with that case is executed.
An entry of break prevents one case from running into the next
So if a paragraph starts on line 31 of the document, the case case 1 will match. The code phrase style.color="green"; will be executed. The first part of the phrase style.color specifies that you want to provide a value for the color property for the style of the context, which is based on an fStyle object. The second half ="green" confirms that the value of the color property should be green. The content of the paragraph will be output in green text.
* 
If you want to verify the line number on which a paragraph starts, include the phrase formatting.write(lineOnPage) in the source for the para in chapter context. This will output the current value of the lineOnPage variable that you have set up, which contains the current line number.