Go Back   MarcomCentral (PTI) and FusionPro User Communities > Software-Related Talk > The JavaScript Library > Formatting Text

Notices

Reply
 
Thread Tools Search this Thread Display Modes
  #1  
Old August 2nd, 2013, 09:01 AM
Meir Meir is offline
Regular Contributor
 
Join Date: Mar 2011
Location: Lawrenceville, NJ
Posts: 50
Question Add padding to the left based on text length

I have a letterhead I need the address info to be on the top right corner, but aligned left, where the end of the furthest right letter of the Address and/or Address2 lines up with the 8" mark.

To compensate for this, I have set a rule that calculates how many spaces it needs to use as padding to before each line. (Feel free to point out a better way.)

The below code keeps erroring out on line 52: Error: execution limit exceeded.

I'm not sure why it wouldn't work. Any ideas?


Code:
var frameWidth = 2; // set width of the text box (in inches)

// Use TextMeasure to get the length of each company name
var space = new FusionProTextMeasure;
space.pointSize = "8 pt"; // set the type size
space.font = "HelveticaNeue Condensed"; // set your typeface
space.CalculateTextExtent(" ");
space.useTags = false;
var spaceWidth = space.textWidth;

// Use TextMeasure to get the length of each company name
var address1 = new FusionProTextMeasure;
address1.pointSize = "8 pt"; // set the type size
address1.font = "HelveticaNeue Condensed"; // set your typeface
address1.CalculateTextExtent(Field("Address"));
address1.useTags = false;
var addressWidth = address1.textWidth;

// Use TextMeasure to get the length of each company name
var address2 = new FusionProTextMeasure;
address2.pointSize = "8 pt"; // set the type size
address2.font = "HelveticaNeue Condensed"; // set your typeface
address2.CalculateTextExtent(Field("Address2"));
address2.useTags = false;
var address2Width = address2.textWidth;

// Use TextMeasure to get the length of each company name
var gap = new FusionProTextMeasure;
gap.pointSize = "8 pt"; // set the type size
gap.font = "HelveticaNeue Condensed"; // set your typeface
gap.CalculateTextExtent(" • ");
gap.useTags = false;
var gapWidth = gap.textWidth;

var i=1

var success = false
while (success ==  false)
    {
    if (Field("Address2") != "") //if there is an address2
        {
        if((spaceWidth*i) + addressWidth + gapWidth + address2Width >= frameWidth*7200) //if there is an address 2 and the width is greater or equal to frameWidth
            {
                success= true; //then stop looking
            }
        else i= i+1; // if there is an address 2 and the width is less than frameWidth
        };
    if ((spaceWidth*i) + addressWidth >= frameWidth*7200) //if there is no address 2 and the width is greater than frameWidth
        {
        success= true; //then stop looking
        }
    else i= i+1; // if there is no address 2 and the width is less than frameWidth  #### ERRORS OUT ON THIS LINE  ####
    };
    


var result = Array(i).join('space');

return result;
__________________
Meir Galimidi
Allegra
FP 7.2P1k, MAC OSX 10.5.8, Acrobat 9.4.0

Last edited by Meir; August 2nd, 2013 at 09:05 AM.. Reason: The end code tag did not display.
Reply With Quote
  #2  
Old August 2nd, 2013, 02:26 PM
esmith's Avatar
esmith esmith is offline
Senior Contributor
 
Join Date: Oct 2008
Location: Charlotte, NC
Posts: 870
Default Re: Add padding to the left based on text length

I'm not sure what you are trying to return in your result. What is "Array(i)" intended to return?

I would build your result first and then measure the entire line at the same time. Then I would offset the line by the difference of the frame width and the text width like so:
Code:
// Set variables
var frame = 2; // width of text box in inches
var address = Trim(Field("Address"));
var address2 = Trim(Field("Address2"));

var result = '';
// Add Address to result if it contains a value
if (address != '') result += address;
// add bullet and Address2 if applicable
if (address2 != '' && result != '') result += ' • ' + address2;
// or add Address2 alone if result was previously empty
else if (address2 != '') result += address2;

if (result != '') {
	// measure length of result
	var txt = new FusionProTextMeasure;
	txt.pointSize = "8"; // set the type size
	txt.font = "Futura Book"; // set your typeface
	txt.CalculateTextExtent(result);

	// calculate indent amount
	var offset = (frame * 7200) - txt.textWidth;

	// add indent to result
    result = '<p findent="' + offset + '">' + result;
}

return result;
__________________
Eric Smith
Content Developer
Classic
FP 9.3.6, MAC OSX 10.9.4, Acrobat 11.0.09
Reply With Quote
  #3  
Old August 2nd, 2013, 02:31 PM
esmith's Avatar
esmith esmith is offline
Senior Contributor
 
Join Date: Oct 2008
Location: Charlotte, NC
Posts: 870
Default Re: Add padding to the left based on text length

For cleaner code, you could replace all of this:
Code:
var address = Trim(Field("Address"));
var address2 = Trim(Field("Address2"));

var result = '';
// Add Address to result if it contains a value
if (address != '') result += address;
// add bullet and Address2 if applicable
if (address2 != '' && result != '') result += ' • ' + address2;
// or add Address2 alone if result was previously empty
else if (address2 != '') result += address2;
with this:
Code:
var addresses = [Trim(Field("Address"),Trim(Field("Address2")];
var result = addresses.filter(Boolean).join(" • ");
__________________
Eric Smith
Content Developer
Classic
FP 9.3.6, MAC OSX 10.9.4, Acrobat 11.0.09
Reply With Quote
Reply

Tags
calculatetextextent, font, pointsize, textmeasure, textwidth

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT -7. The time now is 07:13 PM.


Powered by vBulletin® Version 3.8.4
Copyright ©2000 - 2019, Jelsoft Enterprises Ltd.
(c) 2011, PTI Marketing Technologies™, Inc.