Jump to content

Having issue with TextMeasure and graphic


Recommended Posts

| Using 9.3.21 on Windows |

 

I'm admittedly not very proficient with JS so please bear with me...

 

I've got an oddball project where I've got a picture of a fish that needs to size to match some text which will always be the same size. Basically, the fish will be larger or smaller based on the size of a name field.

 

I've tried modifying existing JS from other projects to get something to work but it's not coming out right.

 

--my script --

 

 

var field = Field("First");.
var graphic = 'fish.jpg';
var graphicHeight = 1; 
var font = {
   face: "Proxima-Nova", 
   size: 18
}
var tags = '<span font="' + font.face + '" pointsize="' + font.size + '">';
field = tags + field + '</span>';
function getTextWidth(input){
   var tm = new FusionProTextMeasure;
   tm.useTags = true;
   tm.CalculateTextExtent(input);
   return tm.textWidth;
}
return field + '<br><fish.jpg="' + graphic + '" width="' + getTextWidth(field) + '" height="' + (graphicHeight*100) + '">';

 

Any helpful guidance as to where I'm messing this up?

 

Thanks

Link to comment
Share on other sites

Any helpful guidance as to where I'm messing this up?

var field = Field("First");[color="Red"].[/color]
var graphic = 'fish.jpg';
var graphicHeight = 1; 
var font = {
   face: "Proxima-Nova", 
   size: 18
}
var tags = '<span font="' + font.face + '" pointsize="' + font.size + '">';
field = tags + field + '</span>';
function getTextWidth(input){
   var tm = new FusionProTextMeasure;
   tm.useTags = true;
   tm.CalculateTextExtent(input);
   return tm.textWidth;
}
return field + '<br>[color="red"]<fish.jpg[/color]="' + graphic + '" width="' + getTextWidth(field) + '" height="' + [color="red"](graphicHeight*100)[/color] + '">';

I've highlighted in red the errors that I see.

I realize that it's probably a typo, but there shouldn't be a period after the semicolon on line one. That will definitely keep your rule from returning correctly.

 

Like the width of the text, the width and height of the graphic are measured in 100ths of a point. So setting the height to "1" and multiplying it by 100 will result in an image that is 1 point (1/72 inch) tall. As a matter of fact, if you want the height of the graphic to scale proportionally, just don't even specify a height.

 

The last issue is that you should be using a "graphic" tag to return the graphic. Try this:

 


var field = Field("First");
var graphic = 'fish.jpg';
var font = {
   face: "Proxima-Nova", 
   size: 18
};
var tags = '<span font="' + font.face + '" pointsize="' + font.size + '">';
field = tags + field + '</span>';
function getTextWidth(input){
   var tm = new FusionProTextMeasure;
   tm.useTags = true;
   tm.CalculateTextExtent(input);
   return tm.textWidth;
}
return field + '<br><graphic file="' + graphic + '" width="' + getTextWidth(field) + '">';

 

Keep in mind that a short name like "AJ" might result in the graphic being barely visible. I'd suggest setting a minimum width for the graphic so that it's visible:

var field = '<span font="Proxima-Nova" pointsize="18">' + Field("First") + '</span>';
var graphic = 'fish.jpg';
var minWidth = 7200; // 72 points, 1 inch 
var [width] = [minWidth, getTextWidth(field)].sort(function(s,p){return p-s});
return field + '<br><graphic file="' + graphic + '" width="' + width + '">';

function getTextWidth(input){
 var tm = new FusionProTextMeasure;
 tm.useTags = true;
 tm.CalculateTextExtent(input);
 return tm.textWidth;
}

 

The above will set a minimum width of 1 inch. If the text is wider than an inch, the graphic will be the same size as the text.

Link to comment
Share on other sites

I see three main problems with your rule:

  1. There's a stray period/dot at the end of the first line, which causes a syntax error.
  2. You're setting the height of the graphic to 100 (var graphicHeight = 1, then that gets multiplied by 100), which is one point, or 1/72 of an inch. That's like text in a point size of 1. Maybe start with something like graphicHeight=72, which then will get multiplied to 7200, or one inch.
  3. The tag on the last line is invalid. If you do an actual composition, instead of just a preview, and look at the status dialog or the log file, you'll see a message in there like "Unknown Tag fish.jpg="fish.jpg" ignored." The tag should be a <graphic> tag with a "file=" attribute.

 

I think this will do basically what you want:

var field = Field("First");
var graphic = 'fish.jpg';
var graphicHeight = 72; 
var font = {
   face: "Proxima-Nova", 
   size: 18
}
var tags = '<span font="' + font.face + '" pointsize="' + font.size + '">';
field = tags + field + '</span>';
function getTextWidth(input){
   var tm = new FusionProTextMeasure;
   tm.useTags = true;
   tm.CalculateTextExtent(input);
   return tm.textWidth;
}
return field + '<br><graphic file="' + graphic + '" width="' + getTextWidth(field) + '" height="' + (graphicHeight*100) + '">';

Link to comment
Share on other sites

Hah, Step just beat me to the punch. He has a couple of great ideas. It's probably better not to specify a height at all, and let the graphic scale proportionally. And yeah, a minimum width is a good idea too, although I think this is a simpler way to do that:

var minWidth = 7200; // 72 points, 1 inch 
var width = Math.max(minWidth, getTextWidth(field));

Edited by Dan Korn
Link to comment
Share on other sites

And yeah, a minimum width is a good idea too, although I think this is a simpler way to do that:

var minWidth = 7200; // 72 points, 1 inch 
var width = Math.max(minWidth, getTextWidth(field));

 

Agreed – that is a much simpler way to do it. Cool!

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