问题
I am trying to extract kerning information out of some .ttf fonts with freetype 2.6 library.
This is how I get kerning informations (looping through characters):
if( FT_HAS_KERNING(face->getFace()) && previous ){
FT_Vector delta;
FT_UInt glyph_index = FT_Get_Char_Index( face->getFace(), character );
FT_UInt prev_index = FT_Get_Char_Index( face->getFace(), previous );
FT_Get_Kerning( face->getFace(), prev_index, glyph_index,
FT_KERNING_DEFAULT, &delta );
kerning = delta.x >> 6;
}
I tried the program with some different fonts: "Times new roman.ttf", "Tymes.ttf", "minion.otf". For Times new Roman font only, the kerning information are correctly extracted, and I checked that by logging the info.
The problem is that I don't understand why the kerning is always 0 (i.e. FT_HAS_KERNING returns false, AND FT_GetKerning returns 0 anyway) for the other 2 fonts.
I checked with fontforge that kerning info are present for pairs "VA" and "To", and they are there! So they must be stored in the .ttf. Nevertheless, with the code above the kerning is always 0 for "VA" or "To", or FT_HAS_KERNING returns false.
Is there any freetype option or setting that I am missing here? Any kind of enlightenment is appreciated..
EDIT: I am setting the face size with
FT_Set_Pixel_Sizes( face->getFace(), 0, size);
EDIT: Kerning info for "tymes" font in fontforge:
回答1:
Freetype can only retrieve kerning values from a font's kern
table, not from the more modern implementation as an OpenType feature using GPOS
. From the documentation:
Note that OpenType fonts (OTF) provide two distinct mechanisms for kerning, using the ‘kern’ and ‘GPOS’ tables, respectively, which are part of the OTF files. Older fonts only contain the former, while recent fonts contain both tables or even ‘GPOS’ data only. FreeType only supports kerning via the (rather simple) ‘kern’ table. For the interpretation of kerning data in the (highly sophisticated) ‘GPOS’ table you need a higher-level library like ICU or HarfBuzz since it can be context dependent (this is, the kerning may vary depending on the position within a text string, for example).
Your FreeType code works with Times New Roman (mine is "Monotype:Times New Roman Regular:Version 5.11 (Microsoft)") because it contains both tables:
tag 'GPOS' checksum 5dfeb897 offset 778576 length 43506
tag 'kern' checksum a677acd1 offset 734088 length 5220
but the other fonts do not contain the kern
one.
GPOS
kerning is preferred over plain kern
because its tables can be linked to a particular script and language, and it offers finer control.
There are also good reasons to contain only one type of table – if both are present, it's up to the font renderer to select one. Microsoft's Recommendations for OpenType Fonts, for example, states the following:
The OFF specification allows CFF OT fonts to express their kerning in a kern table. Many OFF text layout engines support this. Windows GDI’s CFF OT driver, however, ignores the kern table in a CFF OT font when it prepares kerning pairs to report via its pair kerning API.
When a kern table and GPOS table are both present in a font, and an OFF layout engine is requested to apply kerning to a run of text of a particular script and language system: (a) If the number of kern feature lookups in the resolved language system in the GPOS table is zero, then the kern table should be applied, followed by any remaining GPOS features requested. (b) If the number of kern feature lookups in the resolved language system in the GPOS table is non-zero, then all GPOS lookups, including the kern lookups, should be applied in the usual way and the kern table data ignored.
来源:https://stackoverflow.com/questions/31782261/cannot-get-the-kerning-of-some-specific-ttf-fonts-with-freetype