FontMetrics doesn't have getters for cap height and x-height of a font.
How can I obtain these values?
As far as cap height goes, there's no guarantee for a particular capital letter that the letter's ascent is the same as the cap height. (e.g. a capital H isn't guaranteed to be flat on the top)
As far as x height goes, I assume it's probably the same as the height of an "x", but again, there's no guarantee.
edit: Grr! I just tried FontMetrics.getBounds()
and FontMetrics.getLineMetrics()
for specific character sequences, and I always get the same answer for heights (getBounds() does differ for widths, obviously). There's a note in the hasUniformLineMetrics()
method about a fontmetrics having several fonts to cover the character set, but that covers character groups, not individual characters.
And it is. So is cap height. In typography, the x-height is the height of lowercase letters that do not have an ascender or descender, represented by the lower case letter x. It is the distance between the baseline and the mid line of a font.
I know this is a very old question, but someone might still find it: The font height in Java (and many other places) is given in "typographic points", which are defined as roughly 1/72nd of an inch. To calculate the points needed for a certain pixel height, you should be able to use the following:
In typography, cap height or cap line refers to the height of the flat capital letters measured from the baseline, the bottom of the cap height, to the top of the flat characters. The distance from the baseline to the top of the capital letter is what determines the point size of a letter.
The x-height is the distance between the baseline and the median. This is the size of most small letters in a specific font or script, that don’t ascend or descent, such as the letter w, z or of course x. Curved letters however like s, r, u, c, a etc.
What you are looking for is the screen render box that tells you the exact size of text.
This means that you will need to supply information at some point about the surface you are drawing on and the string you are drawing. The reason is that the system simply does not know the visual result until late in rendering. I used:
Graphics2D g;
g.getFont().createGlyphVector(g.getFontRenderContext(),"abc").getVisualBounds();
You might also try:
Graphics2D g;
g.getFont().getMaxCharBounds(g.getFontRenderContext());
I too have trouble keeping all those font methods straight.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With