Stylesheet Development with PTC ALD > Adding PTC ALD Code to Stylesheet Source > Samples > Testing > Test Existing Text Properties and Apply Formatting Properties > Set Text Color Based on Current Text Height
  
Set Text Color Based on Current Text Height
The sample listed here describes how to set text height for a piece of inline text based on the current value of an attribute set for the paragraph in which the inline text appears.
Refer to the sample file Arbortext-path/samples/APP/Testing/TextProperties/testTextProperties.xml. The paragraphs in the chapter Emphasis testing the text height each have a role attribute. The value of that attribute can be small, medium, large, or xlarge. Conditions in the testTextProperties.style that are based on these attribute values set the font size property for the para in chapter context:
para role="small"para in chapter context in stylesheet, condition IF attribute “role”=”small” — Font size 7
para role="medium"para in chapter context in stylesheet, condition ELSE IF attribute “role”=”medium” — Font size 10
para role="large"para in chapter context in stylesheet, condition ELSE IF attribute “role”=”large” — Font size 18
para role="xlarge"para in chapter context in stylesheet, condition ELSE IF attribute “role”=”xlarge” — Font size 30
Inline text in a paragraph is tagged as emphasis, with the attribute role=”testHeight” set. The condition If attribute "role" = "testTextHeight" for the emphasis everywhere context in the stylesheet references the property set testTextHeight. The property set tests the current text height in operation in the paragraph, set by the role attribute on the paragraph, and applies a certain text color to the piece of inline text, based on that height.
The relevant code in the property set is given below:
var currentHeight = new fLength(formatting.currentStyle.height);

switch (currentHeight.toUnit("pt")) {
case "7pt":
style.color="red";
break;
case "10pt":
style.color="blue";
break;
case "18pt":
style.color="green";
break;
case "30pt":
style.color="magenta";
break;
}
Extract the current text height for the paragraph
var currentHeight = new fLength(formatting.currentStyle.height);
Create the variable currentHeight to hold the current text height. You reference this variable later, when setting text color for the current element.
Create a new fLength datatype object via the fLength() constructor of the fLength object, and its keyword new. The value that will be returned for the new fLength object is the value of the height property of the current style. The current style is defined in the currentStyle property of the fFormatting object.
Pass the new fLength object, with its associated value, into the currentHeight variable.
Set the text color for the current element based on the current text height
switch (currentHeight.toUnit("pt")) {
case "7pt":
style.color="red";
break;
case "10pt":
style.color="blue";
break;
case "18pt":
style.color="green";
break;
case "30pt":
style.color="magenta";
break;
}
Use a switch statement to create a list of possible options (cases) for processing the value of the currentHeight variable.
Note the toUnit("pt") entry for currentHeight. The toUnit() method for an fLength object returns the length as a string, represented in the specified unit of measurement, in this case pt. For example, if the actual value of currentHeight is 7, it will be returned as 7pt.
toUnit() can also be used to convert other units into the target unit.
As the content of currentHeight is an fLength object, you can use this associated method to process its value.
The switch statement works in three stages:
1. The value of the variable currentHeight is evaluated.
2. The value of currentHeight is compared with the values for each case entry.
3. If the value of currentHeight 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 the text height for a paragraph is 7pt, case "7pt" will match. The code phrase style.color="red"; 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 ="red" confirms that the value of the color property should be red. The content of the current element will be output in red text.
In this sample, the current element is the emphasis element, with the condition If attribute "role" = "testHeight". This context wraps a piece of inline text in a paragraph, so inline text will be output in red if the current text height for the paragraph in which it appears is 7pt. Refer to the para in chapter context in the stylesheet and the condition IF attribute "role"="small". You can see that the font size for this context is set to 7.