From 04:00 PM CDT – 08:00 PM CDT (09:00 PM UTC – 01:00 AM UTC) Tuesday, April 16, ni.com will undergo system upgrades that may result in temporary service interruption.

We appreciate your patience as we improve our online experience.

LabVIEW

cancel
Showing results for 
Search instead for 
Did you mean: 

Draw text as an image to fit a box?

Solved!
Go to solution

How can I draw text to fit a specified box?

Lets say I have a string, "Hello World" and I want to draw it as a 2d image. There are two LabVIEW functions that I can use: Draw Text in Rect.vi and Draw Text at Point.vi. The problem here is that none of those vis give me the opportunity to specify the overall width of the text. Lets say I want the text to always fit in a box 200x200 by adjusting its font size. 
Some people suggest using the zoom factor property node, however, that's not really what I am after.

So yeah, to summarize, I want a Vi which inputs are Box Size in pixels and string and the output is a 2D image containing that string with the appropriate font size to fit inside the specified box.

0 Kudos
Message 1 of 23
(3,721 Views)

Another possible solution might be if there is a possibility to get the 2d text image as vector art?

0 Kudos
Message 2 of 23
(3,718 Views)

Text size is governed by Font Size (in "points"), while box sizes are specified as pixels.  A Text Box has two dimensions, height and width, yet Font Size only governs height -- width will be a function of Font Size, number of characters, and "specifics" of the characters (and the Font -- some Fonts are fixed-width, but most are proportional, so that an "M" is much wider than a "l" (that's a lower-case "L", not a capital "I" or the numeral "1").

 

It is an interesting exercise to size a Text Box to "fit" a particular String (and Font, and Bold/Italic, and Font Size specification).  One way is to simply draw it and start "squeezing" down on the Text Box until it doesn't fit any more.  It seems to me much more difficult to "go the other way", as you propose -- to take a fixed size Text Box and determine the Font that fits.  Height shouldn't be too difficult -- simply determine (for a particular Font) how Font Size and Pixel Size are related.  Width, however, will be much harder, as it is critically dependent on the text you are displaying (which is why allowing the Box Size to "vary to fit the Font" is an easier problem).

 

Good luck.  Try things, post what seems to work, and ask for specific help.

 

Bob Schor

0 Kudos
Message 3 of 23
(3,696 Views)

Hi Bob,

just to clarify if I understood you correctly. What you are suggesting is:

1)start by drawing the text at fixed font size- 20 lets say

2)check the total width of the text

3)step size the font up or down

4)repeat step 2 and 3 until text width = desired box size

This sounds like a PID control system 😄 but its a viable solution 

0 Kudos
Message 4 of 23
(3,693 Views)
Solution
Accepted by topic author datatechNDT

That is more-or-less what I was saying.  I re-read your original post, and see that you specified a 200 x 200 box, hence probably are talking about multi-line text which is an even more complicated problem.

 

Several years ago, I wanted to "solve" the simpler problem, "fit display box to Text, regardless of Font, Font Size, or Font Style", found a method NI suggested that basically was a "crude guesstimate" that basically didn't work, and came to the same conclusion you just drew (it's an "optimization" problem).  I'm acquainted with Type Design -- for anything other than monospaced fonts, it resists "formal rules" and relies on more empiric methods (such as "Trial and Error", or "Beauty in the Eye of the Beholder").

 

Bob Schor

 

P.S. -- I write documents using LaTeX, not Word.

0 Kudos
Message 5 of 23
(3,687 Views)

Thanks a lot for your help. I will try that. Probably I can make it faster if I keep track of error and make like an error function that I am minimizing, rather than hard-coded step size.

0 Kudos
Message 6 of 23
(3,684 Views)
Solution
Accepted by topic author datatechNDT

Hint -- try "Binary Search".  Start with Font Size of, say, 16.  If it fits, double the size and try again.  Once it doesn't fit (say at 64), you know "32" fits, so start by subtracting half-the-last increment (i.e. half 32 = 16).  If 48 works, add half the last increment, else subtract half the last increment.  When you get down to an increment of 1, choose the "last one that works".  You'll have not-that-many trials.

 

Second hint -- to further speed things up, try to not display the text as you test.

 

Bob Schor

Message 7 of 23
(3,676 Views)

not see it mentioned in this thread but the function "Get Text Rectangle" will accept text as well as Font type and size and will return the size of the rectangle required to render the text.

 

That returned rectangle size can be used to do the checks.

 

Ben

Retired Senior Automation Systems Architect with Data Science Automation LabVIEW Champion Knight of NI and Prepper LinkedIn Profile YouTube Channel
Message 8 of 23
(3,669 Views)

works like a charm with "binary search".. down to 1-2 pixels error. Thank you

Message 9 of 23
(3,663 Views)

@datatechNDT wrote:

works like a charm with "binary search".. down to 1-2 pixels error. Thank you


Mind sharing some VIs for this so others can use them?  I remember having to do something like this years ago and my code wasn't very smart, it just incremented the font by one, then check the size until it didn't fit anymore and then went back one.  It took too long to find the size for really large or really small fonts.

0 Kudos
Message 10 of 23
(3,658 Views)