Jump to content

Add a character conditially if line does not break


MeeshKB

Recommended Posts

This post is a further wrinkle to the issue I posted here.

 

I am working on a business card form where we require the individual's name, professional designations and title in one area. Ideally, the card looks like this:

 

http://i141.photobucket.com/albums/r42/Turtlegirl203/Public/Professional/01StandardCardLayout_zpsef83cea3.jpg

 

But in some cases, if the employee's name is particulary long and they have a lot of designations, that line must be split to accommodate both. Like so:

 

http://i141.photobucket.com/albums/r42/Turtlegirl203/Public/Professional/02LongNameLayout_zps224e48ce.jpg

 

I have managed to do so by applying "do not break" rules to the name field and the designation field. But now the client has thrown a wrench in the works. They don't want the comma following the employee's name if the designations fall on the next line.

 

The comma itself is already applied with a rule. It is only inserted if there is something in the designation field. That rule looks like this:

if (Field("Designation") > "0")

{

return "<span>" + RawText(", ") + "</span>";

}

return "";

 

Now I need to add another condition to that rule (I think) where the comma is only added if there is data in the designation field AND the designation does not break to another line.

 

Any thoughts on how to do this?

Link to comment
Share on other sites

Thanks, Dan.

 

I investigated TextMeasure when I began this project, but a search of these boards failed to turn up any info that I understood well enough to apply to my project (I am working hard to get up to speed on JS, but am still a noob).

 

Could you please point me in the direction of some info on applying TextMeasure in an instance like this?

 

Thanks so much.

Link to comment
Share on other sites

Okay, so I've read and done my best to comprehend the sample you directed me to, Dan. And I am closer to understanding, but not quite there.

 

I am getting that I need to define the variable, thusly:

 

var tm = new FusionProTextMeasure;

var frameWidth = 4; // width of your text frame in inches

tm.pointSize = "10 pt"; // your point size here

tm.font = "Helvetica"; // your font here

tm.useTags = true;

 

But I am not sure how to apply that result to give me the conditional text I require. Also, is this solution still viable considering that within the frame I have text with two different sizes? I guess the only one I need to measure it the Employee Name field, so maybe that's moot.

Edited by MeeshKB
Link to comment
Share on other sites

Basically, you would want to create a variable which contains a record's name and designations (as they would appear if all on one line). You then test that against the tm object to see if its length would exceed the frame width using the pre-defined font and size. If tm.textWidth is less than the frame width, the name and designations are left on one line. If equal to or greater, a break tag can be inserted to separate on two lines.

 

So your version of the rule might look something like:

var name = Field(First") + " " + Field("Last);
var test = name + ", " + Field("Designation");
var result = "";

var tm = new FusionProTextMeasure;
var frameWidth = 4; // width of your text frame in inches
tm.pointSize = "10 pt"; // your point size here
tm.font = "Helvetica"; // your font here
tm.useTags = true;

if (name != "") {
   tm.CalculateTextExtent(test);
   if (tm.textWidth < frameWidth * 7200) result = test;
   else result = name + "<br />" + Field("Designation");
}
return result;

Of course, you'll need to correct the details regarding your frame width, font and point size as well as the variables "name" and "test" depending on how your data is provided. And this code only checks to see if the name and designation together fit on one line. If either name or designations don't fit on a line by themselves (i.e. a REALLY LONG name), then you will still have to deal with line breaks on those records. :)

Link to comment
Share on other sites

Alright, so I tried out the code you supplied, Eric. Thank you again for that.

 

My code now looks like this:

(Please excuse my awkward rule naming. "Add Reg to LEED" is the rule I am using for the designation field.)

var name = Rule("Case selection for a name field Rule");

var test = name + ", " + Rule("Add Reg to LEED");

var result = "";

 

var tm = new FusionProTextMeasure;

var frameWidth = 1.45; // width of your text frame in inches

tm.pointSize = "8"; // your point size here

tm.font = "Myriad Pro Semibold"; // your font here

tm.useTags = true;

 

if (name != "") {

tm.CalculateTextExtent(test);

if (tm.textWidth < frameWidth * 7200) result = test;

else result = Rule("Case selection for a name field Rule") + '<br>' + Rule("Add Reg to LEED");

}

return result;

 

It works well except for a couple of wrenches in the works...

 

1) I need to somehow add another condition which will not apply the comma if there is no data in the Designation field.

 

2) The text for the designation is a smaller point size than the Employee name. Since these are both applied inside the same rule, is there any way to indicate the different sizes?

Link to comment
Share on other sites

There's probably a cleaner way to do this, but here's my solution:

var name = Rule("Case selection for a name field Rule");
var designation = Rule("Add Reg to LEED");
var result = "";

var frameWidth = 1.45; // width of your text frame in inches

var tmName = new FusionProTextMeasure;
tmName.pointSize = "8"; // point size of name
tmName.font = "Myriad Pro Semibold"; // font for name
tmName.useTags = true;

var tmDesig = new FusionProTextMeasure;
tmDesig.pointSize = "6"; // point size for designations
tmDesig.font = "Myriad Pro Semibold"; // font for designations
tmDesig.useTags = true;

if (designation != "") {
  tmName.CalculateTextExtent(name + ", ");
  tmDesig.CalculateTextExtent(designation);
  if ((tmName.textWidth + tmDesig.textWidth) < frameWidth * 7200) {
     result = name + ", " + designation;
  }
  else result = name + '<br>' + designation;
}
else result = name;

return result;

Link to comment
Share on other sites

We are so very close, Eric. Thank you so much for all the help.

 

The sole remaining issue is that this code isn't actually returning the designation text at the reduced size (6). Is there any way to roll that last detail into this rule?

Link to comment
Share on other sites

What does Rule("Add Reg to LEED") actually return? I had assumed you were already tagging your designations with that rule. If not, you will need to add font tagging to the result by revising this section of my previous code:

      result = name + ", " + designation;
  }
  else result = name + '<br>' + designation;

 

to look like this:

      result = name + ', <z newsize="6">' + designation;
  }
  else result = name + '<br><z newsize="6">' + designation;

 

And don't forget to check the box for returning result as tagged text. :)

Link to comment
Share on other sites

Eric, you are my new hero. :D That worked like a charm.

 

Rule("Add Reg to LEED") returns the designation, with a replaceSubstring to add a Registered Trademark character after any occurence of the acronym LEED. I hadn't tagged the rule because I was adding it as a separate element, and had applied the type size in the text editor.

 

Thank you so very much for your help. Every one of these challenges I overcome, I learn a bit more. I sincerely appreciate that you and the others here are willing to share your knowledge.

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