如何获得给定字体的ascender / descender和x height

时间:2011-02-18 15:52:16

标签: android fonts fontmetrics

我需要获得ascender / descenderx-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 ?

由于

2 个答案:

答案 0 :(得分:6)

我认为上升器和下降器的高度通常是相同的,但我不会依赖于每种字体。我真的没有看到直接进入x高度的方法,但是你可以使用的技巧就像下面这样。另外,对于总高度,您是在谈论从最高上升者到最低下降者的绝对距离吗?我还在下面提供了一些内容。我自己没有对它们进行过测试,但它应该有效(但如果我误解了你所说的话,请告诉我):

// 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;

答案 1 :(得分:1)

这对我有用:

Paint.FontMetrics fm = paint.getFontMetrics();
int totalHeight = (int)(fm.bottom - fm.top + .5f);