Cannot get the kerning of some specific .ttf fonts with freetype
Asked Answered
A

1

9

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: enter image description here

Aimo answered 3/8, 2015 at 8:2 Comment(6)
Does FontForge tell you if this is TrueType kern kerning (aka. "ye olde style"), or using a modern OpenType GPOS table? "FreeType only supports kerning via the (rather simple) ‘kern’ table." (freetype.org/freetype2/docs/glyphs/glyphs-4.html)Letishaletitia
Thank you for your help!^^ I checked the kern info in fontforge and it looks to me like "kern subtable" containing simple pairs of characters and one value is present.. (see edit), looks like fontforge is also using freetype, which makes me wonders what am I doing wrongAimo
The terminology "lookup x subtable" is actually OpenType GPOS talk. If you can add an online download location for one of the fonts on which this is failing, I can take a look inside. I wrote myself a tool for this purpose.Letishaletitia
I can't thank you enough for your help, this is the link where I downloaded one of the fonts that doesn't work: trac.openscenegraph.org/projects/osg/browser/…Aimo
That font indeed does not contain a kern table, only GPOS. So you need a true OpenType enabled glyph renderer. (Unrelated: the glyphs for ¼½¾ are really a mess...)Letishaletitia
Understood, I guess I have to use harfbuzz library then^^ If you make that into an answer I'll accept it^^Aimo
L
8

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.

Letishaletitia answered 4/8, 2015 at 11:42 Comment(1)
I wonder why FreeType still doesn't support GPOS and OpenType features. The fonts without kerning looks really not so good.Crotch

© 2022 - 2024 — McMap. All rights reserved.