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 March 13th, 2015, 07:08 AM
-Lisa- -Lisa- is offline
Senior Contributor
 
Join Date: Sep 2008
Location: Long Island, NY
Posts: 189
Default Adjusting Leading when text wraps

Can anyone help me tie up the loose ends here?

I have a field with 9pt leading applied, however, if that field wraps to a second line, the second line (and any subsequent lines) need to be set to 7pts. I'm sure I need to somehow combine the leading tag with the the FusionProTextMeasure object but I'm still a JavaScript "newbie" (yes, even after all this time) so I don't even know how to begin this one.

Anyone up for giving me a head start?

Thanks!
__________________
Lisa B.
FusionPro VDP Creator 10.0.16
Adobe Acrobat Pro DC 2018
Mac OS X 10.13.6 (High Sierra)
Reply With Quote
  #2  
Old March 13th, 2015, 01:14 PM
step's Avatar
step step is offline
Senior Contributor
 
Join Date: Jan 2010
Location: Charlotte, NC
Posts: 955
Default Re: Adjusting Leading when text wraps

If I understand what you're asking, you're trying to have a list of items that have 9pt leading applied to them but if one of those items has to wrap to another line, you want to tighten up the leading on it so that it's not confused for a separate item?

If that's the case, I think you can do this by measuring the item, field, variable (or whatever it may be) using text measure to see if it is wider than the frame. If it's not, return it with 9pt leading, if it is, in order to keep the items equally spaced, you want to keep the leading of the first character of that item as 9pt and change everything else to 7pt in order to tighten up the rest of it.

I think it would look something like this:
Code:
/*========================================
//
// Edit these variables
//
//=======================================*/
    var field = Field("NAME"); // Your variable
    var frameWidth = GetSettableTextWidth(FindTextFrame("YOUR FRAME NAME")); // 100th's of a point 1in = 7200
    
    // Object of settings to be passed to the formatText function
    var settings = {
        font: "Helvetica",
        fontSize: 9, // in points
        defaultLeading: 12, // in points
        altLeading: 6 // in points
        }
    
    return formatText(field, frameWidth, settings);


/*========================================
//
// Function adds alternate leading after 
// the second character in the string if
// it determines it will wrap
//
//=======================================*/
    function formatText(input, width, config){
        // Assign to variables & set defaults
        var config = (config) ? config : new Object();
        var fontFace = config.font || "Helvetica";
        var pt = config.fontSize || 12;
        var lead = (config.defaultLeading*10) || (pt*12);
        var altLead = (config.altLeading*10) || (lead*0.75);
        
        // Tagged the text for TextMeasure
        var tags = '<span font="' + fontFace + '" pointsize=' + pt + '><leading newsize="'+lead+'">';
        var result = tags + input + '</leading></span>';
        
        // Initiate TextMeasure & Get Width
        var tm = new FusionProTextMeasure;
        tm.useTags = true;
        tm.CalculateTextExtent(result);
        
        // If Text is longer than the frame, adjust leading
        if (tm.textWidth > width){
            result = result.replace(new RegExp('(' + tags + '.)',""),
                      '$1</leading><leading newsize=' + altLead + '>');
        }
        
        return result;
    }
Just remember that if you're going to hard-code the width of the text frame, it needs to be in 100th's of a point in order to be accurate. I hope this helps!
__________________
Ste Pennell
FusionPro VDP Creator 9.3.15
Adobe Acrobat X 10.1.1
Mac OS X 10.12

LinkedIn

Last edited by step; March 13th, 2015 at 01:17 PM..
Reply With Quote
  #3  
Old March 13th, 2015, 02:12 PM
David Miller David Miller is offline
Senior Contributor
 
Join Date: Oct 2008
Location: Wallingford, CT
Posts: 263
Default Re: Adjusting Leading when text wraps

In the Variable Text Frame editor. Set the Absolute Leading to 7 pt and the Space Below to 2 pt.

This would give the appearance of 9 pt leading for single line items. Each multi-line item would have 7 pt leading and the space between them would have 9 pt leading.
__________________
David Miller
FusionPro VDP Creator 10.0.26
Adobe Acrobat Pro 2019.008.20074
Suitcase Fusion 20.0.0 (7653)
macOS High Sierra 10.13.6
Reply With Quote
  #4  
Old March 16th, 2015, 03:43 PM
-Lisa- -Lisa- is offline
Senior Contributor
 
Join Date: Sep 2008
Location: Long Island, NY
Posts: 189
Default Re: Adjusting Leading when text wraps

Thank you both!

David, unfortunately your method did not work. It actually returns the opposite result from what I'm trying to achieve. The first line leading needs to be set to 11pts. If the text from this field wraps to a second line, the second line's leading should be set to 8pts.

Ste, I appreciate the full commented code you provided! Unfortunately, I'm receiving an error that "result is not defined". I have a feeling it's got to be because I have not hard-coded my width. If I'm using the GetSettableTextWidth function then should the width already be calculated?

Thanks for the help!
__________________
Lisa B.
FusionPro VDP Creator 10.0.16
Adobe Acrobat Pro DC 2018
Mac OS X 10.13.6 (High Sierra)
Reply With Quote
  #5  
Old March 17th, 2015, 06:41 AM
step's Avatar
step step is offline
Senior Contributor
 
Join Date: Jan 2010
Location: Charlotte, NC
Posts: 955
Default Re: Adjusting Leading when text wraps

Well, I don't think that not hard-coding the width should case an issue since the code I posted doesn't use a hard-coded width.

The error that "result" is not defined, leads me to believe something weird happened when you copied that "formatText" function into your template (since that's where the 'result' variable is used). Could you post your rule? Or even more helpful, collect your template and upload it to this forum so I could have a better idea of what you're working with?
__________________
Ste Pennell
FusionPro VDP Creator 9.3.15
Adobe Acrobat X 10.1.1
Mac OS X 10.12

LinkedIn
Reply With Quote
  #6  
Old March 17th, 2015, 07:04 AM
-Lisa- -Lisa- is offline
Senior Contributor
 
Join Date: Sep 2008
Location: Long Island, NY
Posts: 189
Default Re: Adjusting Leading when text wraps

Thanks Ste. Once again you prove to be a valuable asset to these forums!!

I reviewed the code last night and was able to locate the error when I copied the rule over so I no longer receive the "result" error. However, when placing the rule into my template, it is now only returning the first letter of the field.

I am unable to post the template to the boards but I created a "test" template showing you the size of the text frame I'm working with and with the rule inserted.

Another related question:
Will this rule still function properly if this becomes a multi-line text field and the user forces a line break?

Thank you!!!
Attached Files
File Type: zip Test_Template.zip (7.37 MB, 9 views)
__________________
Lisa B.
FusionPro VDP Creator 10.0.16
Adobe Acrobat Pro DC 2018
Mac OS X 10.13.6 (High Sierra)
Reply With Quote
  #7  
Old March 17th, 2015, 08:35 AM
step's Avatar
step step is offline
Senior Contributor
 
Join Date: Jan 2010
Location: Charlotte, NC
Posts: 955
Default Re: Adjusting Leading when text wraps

It looks like the closing '>' on line 34 got converted to a '.' in your rule. If you check to make sure the if statement in the function looks like this (edit in red):
Code:
//If Text is longer than the frame, adjust leading 
if (tm.textWidth > width){
    result = result.replace(new RegExp('(' + tags + '.)',""),
        '$1</leading><leading newsize=' + altLead + '>');

}
you should see more than just the first character of the title.

Quote:
Will this rule still function properly if this becomes a multi-line text field and the user forces a line break?
In order to ensure that the leading is adjusted for forced-line breaks ("<br>") you could alter the function to adjust the leading when the text is wider than the width of the text frame OR if the field contains a break tag (in red):
Code:
function formatText(input, width, config){
    //Assign to variables and set defaults
    var config = (config) ? config : new Object();
    var fontFace = config.font || "Times New Roman Italic";
    var pt = config.fontSize || 7;
    var lead = (config.defaultLeading*10) || (pt*12);
    var altLead = (config.altLeading*10) || (lead*0.75);

    //Tag the text for TextMeasure
    var tags = '<span font="' + fontFace + '" pointsize=' + pt + '><leading newsize="'+lead+'">';
    var result = tags + input + '</leading></span>';

    //Initate TextMeasure & GetWidth
    var tm = new FusionProTextMeasure;
    tm.useTags = true;
    tm.CalculateTextExtent(result);

    var useAltLeading = ((tm.textWidth > width) || (input.search(/<br>/g) > -1));

    //If Text is longer than the frame, adjust leading 
    if (useAltLeading){
        result = result.replace(new RegExp('(' + tags + '.)',""),
            '$1</leading><leading newsize=' + altLead + '>');

    }

    return result;
}
__________________
Ste Pennell
FusionPro VDP Creator 9.3.15
Adobe Acrobat X 10.1.1
Mac OS X 10.12

LinkedIn
Reply With Quote
  #8  
Old March 17th, 2015, 09:06 AM
-Lisa- -Lisa- is offline
Senior Contributor
 
Join Date: Sep 2008
Location: Long Island, NY
Posts: 189
Default Re: Adjusting Leading when text wraps

Thanks Ste! Much appreciated! I fixed the code which corrected the one-character issue and works when the line wraps...but forcing a line break just reverts everything back to the auto-leading. I'll keep playing around with this. Thank you SO much for the feedback and the assistance!!
__________________
Lisa B.
FusionPro VDP Creator 10.0.16
Adobe Acrobat Pro DC 2018
Mac OS X 10.13.6 (High Sierra)
Reply With Quote
Reply

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 08:57 AM.


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