How to get the ascending or descending height of a font that uses 'Text figures'

百般思念 提交于 2019-12-13 14:40:57

问题


I'm having a problem with Text figures (see Wikipedia) in a PDF document created with itextsharp.

The distances between the baseline and the lowest point of a number (e.g. 9) is NOT the same as the normal descender height of a font.

With the following code i can calculate the height of the descender of a font:

var fontSize = 60;
var fontPath = @"CorpusGothic-Condensed.otf";
var font = BaseFont.CreateFont(fontPath, BaseFont.CP1252, BaseFont.EMBEDDED);
var descentHeight = font.GetFontDescriptor(BaseFont.DESCENT, fontSize)

My question now:
Is it possible to get the descender- or ascender-heights of the numbers in a font with text-figures as shown on the Wikipedia page?


回答1:


A font contains a general value of the ascender and the descender. This is the value that you are getting in your own code sample.

However, every glyph also has its own dimensions. The glyph for letter h has a higher ascender and a lower descender than the glyph for the letter g.

Only four hours ago, I answered the question How to calculate the height of an element? (making your question a duplicate).

This is what I wrote:

If bf is a BaseFont instance, then you can use:

float ascent = bf.getAscentPoint("Some String", 12);
float descent = bf.getDescentPoint("Some String", 12);

This will return the height above the baseline and the height below the baseline, when we use a font size of 12. As you probably know, the font size is an indication of the average height. It's not the actual height. It's just a number we work with.

The total height will be:

float height = ascent - descent;

Note that providing a String (in your case for instance "9") isn't sufficient. You also need to pass a font size (in my case 12pt) to get the value of the ascender and descender in points.

Remark: if you're using iTextSharp, replace get with Get in the methods I mentioned.



来源:https://stackoverflow.com/questions/24556000/how-to-get-the-ascending-or-descending-height-of-a-font-that-uses-text-figures

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!