Unable to get kerning of some specific .ttf fonts with freetype

I am trying to extract kerning information from some .ttf fonts with freetype 2.6 library.

This is how I get kerning information (by scrolling 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 a program with several different fonts: "Times new roman.ttf", "Tymes.ttf", "minion.otf". For Times New Roman fonts only, kerning information is correctly extracted, and I checked this by registering the information.

The problem is that I don’t understand why 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 information is present for the "VA" and "To" pairs, and they are! Therefore, they must be stored in .ttf. However, with the code above, kerning is always 0 for "VA" or "To", or FT_HAS_KERNING returns false.

Is there any freetype parameter or parameter that I am missing here? Any enlightenment is welcome.

EDIT: I set face size using

 FT_Set_Pixel_Sizes( face->getFace(), 0, size); 

EDIT: Kerning for the font "tymes" in fontforge: enter image description here

+6
source share
1 answer

Freetype can extract kerning values ​​from the kern font kern , rather than from a more modern implementation, as an OpenType function using GPOS . From the doc:

Note that OpenType (OTF) fonts provide two different kerning mechanisms, using the kern and GPOS tables, respectively, which are part of OTF files. Old fonts contain only the first, and the latest fonts contain both tables or even β€œGPOS data”. FreeType only supports kerning through a (fairly simple) kernel table. To interpret kerning data in a (very complex) GPOS table, you need a higher-level library, such as ICU or HarfBuzz , as it may be context sensitive (for example, kerning may vary depending on the position in the text string, for example).

Your FreeType code works with Times New Roman (mine is "Monotype: Times New Roman Regular: Version 5.11 (Microsoft)", as it contains both tables:

 tag 'GPOS' checksum 5dfeb897 offset 778576 length 43506 tag 'kern' checksum a677acd1 offset 734088 length 5220 

but other fonts do not contain kern .

GPOS Kerning is preferable to simple kern because its tables can be associated with a specific script and language, and it offers more subtle control.

There are also good reasons to contain only one type of table - if both are present, then select one from the font renderer. Microsoft recommendations for OpenType fonts , for example, indicate the following:

The OFF specification allows CFF OT fonts to express kerning in the kernel table. Many OFF text output engines support this. The Windows GDIs CFF OT driver, however, ignores the kernel table in the CFF OT font when it prepares kerning pairs for a report through its kerning API.
When the kernel table and the GPOS table are present in the font, and the OFF linking mechanism asks for kerning to run the text of a specific script system and language: (a) If the number of kernel function searches in the allowed language system in the GPOS table is zero, then the kernel table should be applied. followed by any remaining GPOS features. (b) If the number of kernel search functions in the allowed language system in the GPOS table is nonzero, then all GPOS search queries, including search in the kernel, must be applied in the usual way, and the data in the kernel table is ignored.

+4
source

All Articles