Jump to content

Rule to measure formatted text resource (text length)


EricC

Recommended Posts

I need help with a Text Measure Rule.

I am trying to measure the width of a Formatted Text Resource.

 

1. I created a Formatted Text Resource that looks like this:

<p style="(no style)" br="false" override="true" quad="L" leading="180" findent="0" lindent="0" rindent="0" leadbefore="0" leadafter="0" widows="1" kerning="false" hyphenate="false" skipifempty="true" skipifemptyvar="false" noparabreakoncopyfit="false"><tracking newsize="3.000000"><f Name="Yanone Kaffeesatz Thin"><z newsize="14.0"><color Name="PANTONE Orange 021 U">Hello<f Name="Yanone Kaffeesatz Regular"><z newsize="12.0"> <color Name="White"><variable Name="Name">

 

2. Next, I created a Rule that simply returns the content of the text resource:

// Rule Name:  Rule_Resource_NameAndTitle
return Resource("NameAndTitle").content;

 

3. Then I created a Rule that (attempts to) measure the width of the text:

// Rule Name:  Rule_TextWidth
var string3 = Rule("Rule_Resource_NameAndTitle");
var TMO3 = new FusionProTextMeasure;
TMO3.CalculateTextExtent(string3);
return TMO3.textWidth;

 

4. Finally, I placed this Rule into a FusionPro text box in my template.

 

I must be doing something wrong, because as I browse through the records in my data file, the rule always returns "5610"

 

If I use the same code (above) to measure a Field (instead of a Formatted Text Resource), the value returned changes, indicating that it is properly working … at least when measuring individual fields.

 

I want to measure the width of the Formatted Text Resource (which includes the font name, font color, font style, tracking, some static text and some variable text).

Link to comment
Share on other sites

This is a known issue, case FP-10000. (Nice even number!) Note that the text measurement should work at composition time, in the output. However, the context of composition-time variables, for tags such as <variable name="Name">, is not set at rule validation time in the Rule Editor.

 

What I would do, as a workaround, is simply use the Field function directly in the rule instead of accessing the resource. You could have Rule_Resource_NameAndTitle return just the tags, with calls to the Field function as needed.

 

Alternately, you could add this line to the rule using FusionProTextMeasure, right after the first line which gets the tags:

string3 = string3.replace(/(\<variable name=\")([^\"]+)(\"\>)/gi, function(all, s1, s2, s3){ return RuleOrField(s2); });

This should allow you to still edit the resource in the Text Editor.

Link to comment
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...