【问题标题】:How to get ascender/descender and x height for a given font如何获取给定字体的上升/下降和 x 高度
【发布时间】:2011-02-18 15:52:16
【问题描述】:
我需要得到ascender/descender 和x-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 ?
谢谢
【问题讨论】:
标签:
android
fonts
fontmetrics
【解决方案1】:
我认为上升高度和下降高度通常是相同的,但我不会对每种字体都依赖它。我真的没有看到直接到达 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;
【解决方案2】:
这对我有用:
Paint.FontMetrics fm = paint.getFontMetrics();
int totalHeight = (int)(fm.bottom - fm.top + .5f);