I’m looking for PostScript code that estimates a PostScript font’s height (room for ascender) and depth (room for descender). Could a font’s bounding box (FontBBox) be used for this?`
Here is some background for my question: glyphs of a font are sitting on a baseline. Obviously glyphs with descenders will reach below the baseline and I want to know how far below the baseline descenders can reach such that I can provide sufficient space in a layout.
I have seen PostScript code that renders a given string to check its dimension. I’m rather interested in a general answer for a given font.
Well, you’ve already discovered the two “shortcuts”. The FontBBox gives the bounding box of all the glyphs in the font superposed together.
false charpath flattenpath pathbboxgives the box for a specified string.For a Type 3 (user-defined) font this is all you can reaonably expect to do; but for the more-popular Type 1 fonts, there is a wealth of metric information both in the font and in a “metrics” file (for other applications).
But this is the part I don’t know very well. So I’m going to read-up (in the Adobe Type 1 manual) and expand this answer later.
Edit: Actually, it looks like these may be the best ways in general.
There is an array of vertical alignment values in a Type 1 font, in the /Private dictionary, under the name /BlueValues; but there is no guarantee that the font honors them. The first number in the array is the baseline overshoot; this is the bottom edge of letters like ‘O’ which dip below the baseline. And the largest value in the array will be either the ascender-height overshoot or the cap-height overshoot (whichever is higher). But any individual character may be plotted without regard to these values (hence, no guarantee).
On the other hand, the FontBBox itself may reflect the bounding box of any “special” characters present in the font, whether or not they’re accessible through the encoding vector (ie. you can’t
showthem, but mustglyphshowthem).So the best bet would probably be to take the
pathbboxof a string of all the characters you intend to use. This would ignore the contribution of any other characters that may be present but are irrelevant to your purpose. And don’t forget toflattenpathto remove the control points from the curves (which may be far outside the “real” bounding box).