Jump to content

Abbreviating text


Recommended Posts

Hi

I am trying to create a business card in which I need to Provide a drop-down menu for selection of Rank/Title. It is preferred that each officer selects their rank from a list as opposed to manual entry. As a further requirement the Rank may be abbreviated if the Officer’s name is over a set length. I can create the drop down menu but I am having trouble abbreviating Title when the name is too long.

Is this at all achievable?

Regards.

.

Link to comment
Share on other sites

I'm imagining a rule something like this:

var abbrs = {
   "Captain": "Capt",
   "Lieutenant": "Lt",
   "Major": "Maj",
   // etc.
};

var rank = Field("Rank");
if (Field("Name").length > 20)
   rank = abbrs[rank] || rank;

return rank;

Although, instead of simply examining the number of characters in the name, you might want to use Text Measurement instead.

Link to comment
Share on other sites

Dan posted a most-excellent way of handling the abbreviations. Let's hope he keeps imaging these really cool things. Okay, so, try combining it with his other posts regarding FusionProTextMeasure.

 

[color="red"]var maxWidth = [color="Magenta"].25[/color] * 7200; [color="SeaGreen"]// Set the maximum width of your field in hundredths of points; 7200 = one inch[/color]

[color="SeaGreen"]// Use FusionProTextMeasure to measure the length of your field[/color]
var tm = new FusionProTextMeasure;
tm.pointSize = "10 pt"; [color="SeaGreen"]// Set the type size used for your field[/color]
tm.font = "Helvetica"; [color="SeaGreen"]// Set typeface used for your field[/color]
tm.CalculateTextExtent(Field("[color="Magenta"]MyFieldName[/color]")); [color="SeaGreen"]// Replace with your field name[/color]
tm.useTags = false;
var tmWidth = tm.textWidth;[/color]

[color="red"][color="SeaGreen"]// Edit long names and abbreviations in the same format as below[/color][/color]
var abbrs = {
   "Captain": "Capt",
   "Lieutenant": "Lt",
   "Major": "Maj",
   [color="SeaGreen"]// etc.[/color]
};

var rank = Field("[color="Magenta"]Rank[/color]");[color="SeaGreen"]// The variable "rank" is being set to the field "rank" here. Edit accordingly.[/color]
if [color="red"](tmWidth > maxWidth)[/color]
   rank = abbrs[rank] || rank;

return rank;

 

My additions and changes are in red, comments are in green and template specifics are in magenta. You will need to edit your JavaScript rule according to the comments in green.

Edited by David Miller
Link to comment
Share on other sites

Thanks David, that's good. Still, this can be improved upon a bit more. For one thing, you can have the rule automatically calculate the width of the current frame, so that you don't need to hard-code it. Also, you probably want to measure the whole line, with the rank and the name, and adjust only if that doesn't fit. And, you probably want to check if it fits after using the abbreviation, and apply some copyfitting to shrink the text as necessary.

 

Try this, with both the "Re-evaluate this rule for every text box" and "Treat returned strings as tagged text" boxes checked, and a name applied to the text frame:

// Edit long names and abbreviations in the same format as below
var abbrs = {
   "Captain": "Capt",
   "Lieutenant": "Lt",
   "Major": "Maj",
   // etc.
};

var rank = Field("Rank"); // The variable "rank" is being set to the field "rank" here. Edit accordingly.
var name = Field("Name"); // Replace with your field name

var pointsize = 10; // Set the type size used for your field
var font = "Arial"; // Set typeface used for your field

var tags = '<f name="' + font + '"><z newsize="' + pointsize + '">';

var maxWidth = 0.25 * 7200; // For validation in the Rule Editor, in hundredths of points; 7200 = one inch

if (!FusionPro.inValidation) // At composition time, use the actual frame width
   maxWidth = FindTextFrame(FusionPro.Composition.CurrentFlow.name).GetSettableTextWidth();

var text = tags + rank + " " + name;

// Use FusionProTextMeasure to measure the length of the text and see if it fits.
var tm = new FusionProTextMeasure;
tm.useTags = true;
tm.CalculateTextExtent(text);
if (tm.textWidth <= maxWidth)
   return text; // it fits without modification
//else

// Abbreviate the rank
rank = abbrs[rank] || rank;
text = tags + rank + " " + name;

// See if it fits with the abbreviated rank.
tm.CalculateTextExtent(text);
if (tm.textWidth <= maxWidth)
   return text; // it fits with the abbreviated rank
//else

// Shrink (copyfit) the line to fit.
var factor = Round(maxWidth / tm.textWidth * 100, 0) - 1;
return "<magnify type=setwidth factor=" + factor + ">" + text + "</magnify>";

The bit at the end is borrowed from the CopyfitLineWithMagnifyTag function.

Link to comment
Share on other sites

I don't see your template repeating the name and the abbreviations are working.

 

There is 1 sample record with a name that has 11 or less characters. That person's rank is "PC". The abbreviation for "PC" is "PC".

 

So, it might appear that it is not working, but it is.

Link to comment
Share on other sites

Dan's rule works perfectly. But adjustments are needed to work with your template.

 

// Edit long names and abbreviations in the same format as below
var abbrs = {
   "PCSO": "PCSO",
   "PC": "PC",
   "Detective Constable": "Det Con",
   "Sergeant": "Sgt",
   "Inspector": "Insp",
   "Detective Inspector": "Det Insp",
   "Chief Inspector": "Ch Insp",
   "Detective Chief Inspector": "Det Ch Insp",
   "Superintendent": "Supt",
   "Detective Superintendent": "Det Supt",
   "Chief Superintendent": "Ch Supt",
   "Detective Chief Superintendent": "Det Ch Supt",
   "Assistant Chief Constable": "ACC",
   "Detective Chief Constable": "DCC",
   // etc.
};

var rank = Field("Rank"); // The variable "rank" is being set to the field "rank" here. Edit accordingly.
var name = Field("Name"); // Replace with your field name
[color="Red"]var collar = Field("Collar Number");[/color]

var pointsize = 12; // Set the type size used for your field
var font = "[color="red"]Helvetica[/color]"; // Set typeface used for your field

var tags = '<f name="' + font + '"><z newsize="' + pointsize + '">';

var maxWidth = [color="red"]3.19[/color] * 7200; // For validation in the Rule Editor, in hundredths of points; 7200 = one inch

if (!FusionPro.inValidation) // At composition time, use the actual frame width
   maxWidth = FindTextFrame(FusionPro.Composition.CurrentFlow.name).GetSettableTextWidth();

[color="Red"]var text = tags + rank + " " + name + " " + collar;[/color]

// Use FusionProTextMeasure to measure the length of the text and see if it fits.
var tm = new FusionProTextMeasure;
tm.useTags = true;
tm.CalculateTextExtent(text);
if (tm.textWidth <= maxWidth)
   return text; // it fits without modification
//else

// Abbreviate the rank
rank = abbrs[rank] || rank;
text = tags + rank + " " + name;

// See if it fits with the abbreviated rank.
tm.CalculateTextExtent(text);
if (tm.textWidth <= maxWidth)
   return text; // it fits with the abbreviated rank
//else

// Shrink (copyfit) the line to fit.
var factor = Round(maxWidth / tm.textWidth * 100, 0) - 1;
return "<magnify type=setwidth factor=" + factor + ">" + text + "</magnify>";

 

The rule should evaluate the entire line of text and return the entire line of text accordingly. Changes are in red.

 

Remove the fields "name" and "collar number" from the variable text frame using the variable text editor. (Because they are returned in the rule above.)

 

Add var "collar", fix the font name, change the maxWidth of your frame and update the var "text" line.

Edited by David Miller
typos and technical stuff
Link to comment
Share on other sites

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...