How to get glyph widths by parsing a TTF font file?

蹲街弑〆低调 提交于 2019-12-02 13:32:13

问题


For capturing the glyph widths of a TrueType font, I convert the corresponding TTF file by fontforge into AFM, which is in text format (not binary). Then, parsing the text file to capture the glyph widths.

There should be easier way to directly parse the binary TTF file to capture the glyph widths.

I appreciate a strategy for shell script or C, but any programming language is OK, as the idea is the key, and codes can be adapted.


回答1:


Assuming you mean advance widths, what you're interested in is the hmtx table. It's fairly straightforward to parse, as you can see the structure is:

typedef struct  _longHorMetric {
    USHORT  advanceWidth;
    SHORT       lsb;
}  longHorMetric;

Of course, in order to parse the hmtx, you'll also need to know in general how to locate font tables, and you'll also need the numberOfHMetrics value from the hhea table and the font's numGlyphs from the maxp table. The structure of all of these is given in the links and should be fairly easy to parse in any language.



来源:https://stackoverflow.com/questions/18330464/how-to-get-glyph-widths-by-parsing-a-ttf-font-file

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