Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get ascender/descender and x height for a given font

I need to get a ascender/descender and x-height..

By using following code I can find the descender and the total height:

descender_height = paint.descent();
total_height = descender_height - paint.ascent();
//ascender = ?; is this always equal to descender height?
//x_height = ?; total_height - 2*descender_height ?

Thanks

like image 259
sinek Avatar asked Feb 18 '11 15:02

sinek


People also ask

How do you find the x-height of a font?

In typography, the x-height, or corpus size, is the distance between the baseline and the mean line of lowercase letters in a typeface. Typically, this is the height of the letter x in the font (the source of the term), as well as the letters v, w, and z.

How do you find the height of a letter?

Traditionally, certain letter heights are measured from the top of the face to the bottom of the face (C). Other letters are measured from the center of the top of the face to the center of the bottom of the face (E).

What is the height of a letter that does not include descenders or ascenders?

"the height of the letters that does not include descenders and ascender is basically about same as A-4 size" .

How do you measure cap height in typography?

Cap height is the height of a typeface's uppercase letters, measured from the baseline to the top of flat-topped glyphs. This is usually slightly lower than the ascender height, and cap height can vary between typefaces.


2 Answers

I would think the ascender and descender height would typically be the same, but I wouldn't depend on it for every font. I don't really see a direct way to get to the x-height, but a trick you could use would be something like the below. Also, for the total height, are you talking about the absolute distance from the highest ascender to the lowest descender? I've also included something for that below. I haven't tested these myself, but it should work (but let me know if I'm misinterpreting something you've said):

// Assuming TextPaint/Paint tp;
Rect bounds;

// this will just retrieve the bounding rect for 'x'
tp.getTextBounds("x", 0, 1, bounds);
int xHeight = bounds.height();

Paint.FontMetrics metrics = tp.getFontMetrics();
int totalHeight = metrics.top - metrics.bottom;
like image 78
Kevin Coppock Avatar answered Sep 28 '22 15:09

Kevin Coppock


This is what worked for me:

Paint.FontMetrics fm = paint.getFontMetrics();
int totalHeight = (int)(fm.bottom - fm.top + .5f);
like image 37
Sileria Avatar answered Sep 28 '22 15:09

Sileria