Determine Pixel Length of Text String
49 views (last 30 days)
Show older comments
Michael
on 1 Jul 2013
Answered: Jasper van Casteren
on 10 May 2017
Hi Guys,
I was wondering if there was a way to determine the pixel length for a given text? I know there is a way to do this once a text object has been created on a graph etc., but I am particular looking for a way to do this without having to even create an object. That way I would know what the bounding box size would have to be.
Thanks
2 Comments
Matt Kindig
on 1 Jul 2013
This question doesn't make sense to me. Fonts are usually defined in terms of Bezier curves, which by definition are vectorized and thus can be rendered at any scale. So the pixel representation of the text string isn't defined until it is a rendered, and the number of pixels will of course depend on the font size. I'm not exactly sure what you are asking to do.
Accepted Answer
John Barber
on 2 Jul 2013
Michael,
Especially with a proportional font, it would be very difficult to determine the pixel size for an arbitrary string without doing all of the work needed to actually render the string. You'd have to account for the variable width of each glyph, the kerning used to space the glyphs, plus any possible adjustments applied to align the glyphs to the pixel/subpixel grid. Monospaced fonts are simpler, as each character has the same width and there is no kerning.
If you want to get a decent estimate of the pixel size, you'll need to determine the scale factor between the font size (usually in points) and the displayed size in pixels. In theory, you could get this from the monitor's pixel resolution (usually in dots per inch), which you can get in MATLAB using get(0,'ScreenPixelsPerInch'). In practice, this value is often incorrect.
The approach I have used (and I think I've seen similar code buried in some of the MATLAB graphics routines), is to draw a test string, get its size in pixels, then delete it. The value you get will be specific to the font, font size, graphics adapter and monitor you are using, but it shouldn't change as long as these remain invariant. It is inelegant, but you then have a decent estimate to start with. For fixed width fonts, you can just draw a single character, then multiply by your string's length. For proportional fonts, you can draw your entire string if you know it beforehand. If not, you can follow the method used in my File Exchange program calcticks at http://www.mathworks.com/matlabcentral/fileexchange/30671 (see the getTextSize() subfunction), which is to draw a character, then apply a scale factor to account for kerning (I used 0.8 times the width of the character '2', but I don't think I spent much time trying to calculate it, as I just needed a rough estimate for that application. Also, note that the 'Extent' text property only works for axes using a 2D view, at least in R2010a. For a 3D axes, you have to use the undocumented 'PixelBounds' property and subtract 4 pixels.
I hope that this info helps. However, I think that you might be out of luck if you are looking for an exact method which is valid for arbitrary text.
0 Comments
More Answers (1)
Jasper van Casteren
on 10 May 2017
The answers seem to miss the point. Often, to make a nice app dialog, you want to set the width of text boxes to the largest width. So you would have to find the size of a text in pixels if you cannot design your whole dialog in character units (which is often the case). So, the question is not whether you need to render before you know it, but whether there is a "quite" renderer that does the rendering but only returns the size of the box, without actually displaying something.
0 Comments
See Also
Categories
Find more on Characters and Strings in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!