-->

How to get glyph widths by parsing a TTF font file

2020-04-30 02:53发布

问题:

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.