Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Java: how do you find out the cap height and x-height of a font?

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.

like image 886
Jason S Avatar asked Jun 01 '11 14:06

Jason S


People also ask

Is cap height the same as x height?

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.

How to find the font height in Java?

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:

What is cap height in typography?

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.

What is x height in font size?

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.


1 Answers

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.

like image 99
warren Avatar answered Sep 28 '22 14:09

warren