Jump to content

A better CopyfitLine


Dan Korn

Recommended Posts

Here's a function I wrote to basically do what CopyfitLine does, using the <magnify> tag and a more efficient algorithm:

 
 function CopyfitLineWithMagnifyTag(line, widthInPoints, AllowToExpand)
 {
   var tm = new FusionProTextMeasure;
   tm.CalculateTextExtent(line);
   if (tm.messages)
     ReportError("CopyfitMagnifyLine: " + tm.messages);

   if (tm.textWidth < widthInPoints*100 && !AllowToExpand)
     return line;

   var factor = Round(widthInPoints / tm.textWidth * 10000, 0) - 1;
   return "<magnify type=setwidth factor=" + factor + ">" +
           line + "</magnify>";
 }

The idea here is that we don't need to iterate until we find a size that fits; we can simply take the ratio of how big the line is already compared to the desired width and use that as the magnification factor.

 

Note that you have to specify all the font and size information with tags. So a call would look something like this:

 
 return CopyfitLineWithMagnifyTag('<f name="Arial">' +
           '<z newsize=12>' + Field("Address"), 360);

The caller can add the optional third parameter of "true" to allow the text to get bigger, like so:

 
 return CopyfitLineWithMagnifyTag('<f name="Arial">' +
           '<z newsize=12>' + Field("Address"), 360, true);

This function could also be modified to take parameters specifying the font name and bold/italic faces.

Link to comment
Share on other sites

  • 2 weeks later...

Dan,

 

I tried your copyfit function in a new job, and it works great. However, I ran into something odd when I went to print. If I output to PDF or PS, it prints correctly, but if I use VIPP output, the copyfitting does not occur in the printed output. In fact, the unreduced text actually prints past the boundary of the text frame.

 

I checked all of the other advanced settings in composition (like integral point size), and nothing is different between the compositions.

 

Has anyone else tested printed output using this function? Could there be something in the <magnify> tag that is not supported in VIPP? Or am I missing some other variable that caused the difference in my output?

Link to comment
Share on other sites

DaIf I output to PDF or PS, it prints correctly, but if I use VIPP output, the copyfitting does not occur in the printed output. In fact, the unreduced text actually prints past the boundary of the text frame.

 

I don't personally know a lot about the internals of the VIPP output module. I would recommend you try two things:

  1. Download the newest version of FusionPro and see if you can reproduce the problem there. If so;
  2. Contact Support and send them your job for analysis.

Link to comment
Share on other sites

  • 5 years later...
  • 5 months later...

Dan,

 

I'm just trying this for the first time. It seems to be magnifying just the width of the type, not the height. Is this intended, or am I missing a step?

 

Here's my code:

  function CopyfitLineWithMagnifyTag(line, widthInPoints, AllowToExpand)
 {
   var tm = new FusionProTextMeasure;
   tm.CalculateTextExtent(line);
   if (tm.messages)
     ReportError("CopyfitMagnifyLine: " + tm.messages);

   if (tm.textWidth < widthInPoints*87 && !AllowToExpand)
     return line;

   var factor = Round(87 / tm.textWidth * 10000, 0) - 1;
   return "<magnify type=setwidth factor=" + factor + ">" +
           line + "</magnify>";
 }

   return CopyfitLineWithMagnifyTag('<f name="Swis721 BT">' +
           '<z newsize=11>' + Field("Name") + '<z newsize=6>' + ", " + Field("Designation"), 87, false);

Link to comment
Share on other sites

Dan, I use this copyfit line rule often. It works great, except if the users enters an ampersand, such as "Teacher & Mentor", the rule does not work.

What can be done so that I can include amphersands in this copyfit line rule.

You're passing tagged markup to the function, so you need to encode the text properly as tagged, with the TaggedTextFromRaw function. Although if it's a field value, it's better to use TaggedDataField instead, like so:

  return CopyfitLineWithMagnifyTag('<f name="Arial">' +
           '<z newsize=12>' + TaggedDataField("Address"), 360);

Link to comment
Share on other sites

  • 10 months later...
Dan, I found this old post and it works great for using multiple font styles in one line, however I can't figure out how to modify the function so I can input a name of the text field I want to copyfit and have it automatically calculate the width. Is that possible? Thanks.
Link to comment
Share on other sites

Dan, I found this old post and it works great for using multiple font styles in one line, however I can't figure out how to modify the function so I can input a name of the text field I want to copyfit and have it automatically calculate the width. Is that possible? Thanks.

What do you mean by "text field?" Do you mean a data field? All the examples in this thread use the name of a data field to copyfit:

return CopyfitLineWithMagnifyTag('<f name="Arial">' +
           '<z newsize=12>' + [color="Green"]TaggedDataField("Address")[/color], 360);

Just put the name of your data field in there instead of "Address".

 

Or when you say "text field," do you mean that you want to use the name of a text frame? If so, you can use the FindTextFrame and GetSettableTextWidth() functions, like so:

var widthInPoints = FindTextFrame("YourFrameName").GetSettableTextWidth() / 100; 
return CopyfitLineWithMagnifyTag('<f name="Arial">' +
           '<z newsize=12>' + TaggedDataField("Address"), widthInPoints);

Or, if you check the "Re-evaluate this rule for every text flow" box, you can have it use the current frame's width automatically, like so:

var widthInPoints = FindTextFrame(FusionPro.Composition.CurrentFlow.name).GetSettableTextWidth() / 100; 
return CopyfitLineWithMagnifyTag('<f name="Arial">' +
           '<z newsize=12>' + TaggedDataField("Address"), widthInPoints);

Although if you're doing that, it's probably easier just to turn on full-flow Copyfitting for the flow and check the "Do not break on Copyfit" box in the Paragraph Formatting dialog.

Link to comment
Share on other sites

  • 9 months later...
They would like the field "Credentials" to be able to fill the remaining space in the top line, roll to the second line and have the ability to fill the entire second line. They want to be able to copyfit the text and spacing Width the field "Credentials" for the first two lines to a given %. Else return something like "Credential Do Not Fit".

Well, first of all, a new thread should be used for a new question. And this isn't really supposed to be just the "Ask Dan" forum.

 

Anyway, I'm not sure I understand the requirements. Under what specific criteria do the Credentials not fit? If First Name, Last Name, and Credentials/Certifications would fill more than two lines after shrinking a certain amount? Maybe some examples of what you expect to see with different data would be helpful. (Also, do you mean Credentials or Certifications?)

Link to comment
Share on other sites

  • 4 months later...

I love and use this function all the time! However it has issues with specific characters being used like &, <, >. Here’s a simple fix for it that. It escapes the codes to HTML runs the copy fit calculations and then un-escapes the codes back. I am unable to fully test if the magnify factor is affected due to the HTML characters being more characters. Either way its worked well for me!

 

function CopyfitLineWithMagnifyTag(line, widthInPoints, AllowToExpand)
{
   var escapeCode = [
       [/\&/g, "&", "&"],
       [/\</g, "<", "<"],
       [/\>/g, ">", ">"],
       [/\¢/g, "¢", "¢"],
       [/\£/g, "£", "£"],
       [/\¥/g, "¥", "¥"],
       [/\€/g, "?", "€"],
       [/\©/g, "©", "©"],
       [/\®/g, "®", "®"]
   ];

   //Escape to HTML code for copyFit
   for(i=0;i<escapeCode.length;i++){
       line = line.replace(escapeCode[i][0], escapeCode[i][2]);
   }

   var tm = new FusionProTextMeasure;
   tm.CalculateTextExtent(line);
   if (tm.messages){
     ReportError("CopyfitMagnifyLine: " + tm.messages);
   }

   //Unescape HTML codes
   for(i=0;i<escapeCode.length;i++){
       line = line.replace(escapeCode[i][2], escapeCode[i][1]);
   }

   if (tm.textWidth < widthInPoints*100 && !AllowToExpand){
     return line;
   }

   var factor = Round(widthInPoints / tm.textWidth * 10000, 0) - 1;
   return "<magnify type=setwidth factor=" + factor + ">" +
          line + "</magnify>";
}

Link to comment
Share on other sites

I love and use this function all the time! However it has issues with specific characters being used like &, <, >. Here’s a simple fix for it that. It escapes the codes to HTML runs the copy fit calculations and then un-escapes the codes back. I am unable to fully test if the magnify factor is affected due to the HTML characters being more characters. Either way its worked well for me!

You shouldn't need to do any of that as long as you pass tagged markup to the function in the first place, either by calling TaggedTextFromRaw or TaggedDataField, and set the rule to return tagged markup, as explained in previous posts in this thread.

Link to comment
Share on other sites

  • 4 years later...

Dan,

I've just starting using this function in a rule for a address block. Works so much faster than the other method. The rule works splendidly. However I'm getting copy fit errors when I compose my pdf even tho the output is correct. Is this the normal behavior?

 

var Var1 = Field("FirstName") + " " +Field("LastName");
var Var2 = Field("Company");
var Var3 = Field("Address");
var Var4 = Field("Address2");
var Var5 = Field("City") + ", " + Field("State") + " " + Field("Zip");
var Var6;

function CopyfitLineWithMagnifyTag(line, widthInPoints, AllowToExpand)
 {
   var tm = new FusionProTextMeasure;
   tm.CalculateTextExtent(line);
   if (tm.messages)
     ReportError("CopyfitMagnifyLine: " + tm.messages);

   if (tm.textWidth < widthInPoints*100 && !AllowToExpand)
     return line;

   var factor = Round(widthInPoints / tm.textWidth * 10000, 0) - 1;
   return "<magnify type=setwidth factor=" + factor + ">" +
           line + "</magnify>";
 }


 Var6 = CopyfitLineWithMagnifyTag('<f name="Percanthen">' + '<z newsize=30>' + Var1, 234, false) + "<p>" +
           CopyfitLineWithMagnifyTag('<f name="Percanthen">' + '<z newsize=30>' + Var2, 234, false) + "<p>" +
           CopyfitLineWithMagnifyTag('<f name="Percanthen">' + '<z newsize=30>' + Var3, 234, false) + "<p>" + 
           CopyfitLineWithMagnifyTag('<f name="Percanthen">' + '<z newsize=30>' + Var4, 234, false) + "<p>" + 
           CopyfitLineWithMagnifyTag('<f name="Percanthen">' + '<z newsize=30>' + Var5, 234, false);

 return Var6;

 

 

FP 11.2.1

 

Acrobat

Architecture: x86_64

Build: 20.13.20074.411690

AGM: 4.30.104

CoolType: 5.14.5

JP2K: 1.2.2.46820

Link to comment
Share on other sites

Every record has these errors.

 

Job started 13:09:40 - 1613066980.

Creator: FusionPro® VDP Creator 11.2.1

Computer Name:

Current working folder: /Applications/PTI/FusionPro

Temporary files folder: /var/folders/x4/mjdwd7nj7hv8phtj8_ljm3mr0000gn/T/

Template File: /Volumes/Mac_Jobs/140911_GSG/VarableWork/140911_GSG-EnvFPExport Test.dif

Input File: /Volumes/Mac_Jobs/140911_GSG/VarableWork/Links/140911_ListTest.csv

Job Config File: /private/var/folders/x4/mjdwd7nj7hv8phtj8_ljm3mr0000gn/T/140911_GSG-Env-Test.cfg

The amount of text inserted into a flow exceeds the depth

of all frames in the flow <>. Text is truncated.

Text does not fit in the last frame on page 0 at (0 in, 0 in).

The amount of text inserted into a flow exceeds the depth

of all frames in the flow <>. Text is truncated.

Text does not fit in the last frame on page 0 at (0 in, 0 in).

The amount of text inserted into a flow exceeds the depth

of all frames in the flow <>. Text is truncated.

Text does not fit in the last frame on page 0 at (0 in, 0 in).

The amount of text inserted into a flow exceeds the depth

of all frames in the flow <>. Text is truncated.

Text does not fit in the last frame on page 0 at (0 in, 0 in).

Composing record #1, input record 1

The amount of text inserted into a flow exceeds the depth

of all frames in the flow <>. Text is truncated.

Text does not fit in the last frame on page 0 at (0 in, 0 in).

The amount of text inserted into a flow exceeds the depth

of all frames in the flow <>. Text is truncated.

Text does not fit in the last frame on page 0 at (0 in, 0 in).

The amount of text inserted into a flow exceeds the depth

of all frames in the flow <>. Text is truncated.

Text does not fit in the last frame on page 0 at (0 in, 0 in).

The amount of text inserted into a flow exceeds the depth

of all frames in the flow <>. Text is truncated.

Text does not fit in the last frame on page 0 at (0 in, 0 in).

Composing record #2, input record 2

Link to comment
Share on other sites

Just the text won't fit errors even tho it does.

Job started 13:09:40 - 1613066980.

Creator: FusionPro® VDP Creator 11.2.1

Computer Name:

Current working folder: /Applications/PTI/FusionPro

Temporary files folder: /var/folders/x4/mjdwd7nj7hv8phtj8_ljm3mr0000gn/T/

Template File: /Volumes/Mac_Jobs/140911_GSG/VarableWork/140911_GSG-EnvFPExport Test.dif

Input File: /Volumes/Mac_Jobs/140911_GSG/VarableWork/Links/140911_ListTest.csv

Job Config File: /private/var/folders/x4/mjdwd7nj7hv8phtj8_ljm3mr0000gn/T/140911_GSG-Env-Test.cfg

The amount of text inserted into a flow exceeds the depth

of all frames in the flow <>. Text is truncated.

Text does not fit in the last frame on page 0 at (0 in, 0 in).

The amount of text inserted into a flow exceeds the depth

of all frames in the flow <>. Text is truncated.

Text does not fit in the last frame on page 0 at (0 in, 0 in).

The amount of text inserted into a flow exceeds the depth

of all frames in the flow <>. Text is truncated.

Text does not fit in the last frame on page 0 at (0 in, 0 in).

The amount of text inserted into a flow exceeds the depth

of all frames in the flow <>. Text is truncated.

Text does not fit in the last frame on page 0 at (0 in, 0 in).

Composing record #1, input record 1

The amount of text inserted into a flow exceeds the depth

of all frames in the flow <>. Text is truncated.

Text does not fit in the last frame on page 0 at (0 in, 0 in).

The amount of text inserted into a flow exceeds the depth

of all frames in the flow <>. Text is truncated.

Text does not fit in the last frame on page 0 at (0 in, 0 in).

The amount of text inserted into a flow exceeds the depth

of all frames in the flow <>. Text is truncated.

Text does not fit in the last frame on page 0 at (0 in, 0 in).

The amount of text inserted into a flow exceeds the depth

of all frames in the flow <>. Text is truncated.

Text does not fit in the last frame on page 0 at (0 in, 0 in).

Composing record #2, input record 2

Link to comment
Share on other sites

  • 1 month later...

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...