01-03-2019 04:52 AM
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.
Solved! Go to Solution.
01-03-2019 05:10 AM
Another possible solution might be if there is a possibility to get the 2d text image as vector art?
01-03-2019 07:38 AM
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
01-03-2019 07:44 AM
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
01-03-2019 08:15 AM
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.
01-03-2019 08:19 AM
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.
01-03-2019 08:56 AM
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
01-03-2019 09:49 AM
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
01-03-2019 10:11 AM
works like a charm with "binary search".. down to 1-2 pixels error. Thank you
01-03-2019 10:23 AM
@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.
Unofficial Forum Rules and Guidelines
Get going with G! - LabVIEW Wiki.
17 Part Blog on Automotive CAN bus. - Hooovahh - LabVIEW Overlord