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);
Freetype can only retrieve kerning values from a font's
kern
table, not from the more modern implementation as an OpenType feature usingGPOS
. From the documentation:Your FreeType code works with Times New Roman (mine is "Monotype:Times New Roman Regular:Version 5.11 (Microsoft)") because it contains both tables:
but the other fonts do not contain the
kern
one.GPOS
kerning is preferred over plainkern
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: