无法使用 freetype 获得某些特定 .ttf 字体的字距调整

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

我正在尝试使用 freetype 2.6 库从某些 .ttf 字体中提取字距调整信息。

这就是我获取字距调整信息(遍历字符)的方式:

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;
}

我用一些不同的字体尝试了该程序:"Times new roman.ttf"、"Tymes.ttf"、"minion.otf"。 仅对于 Times new Roman 字体,字距调整信息已正确提取,我通过记录信息进行了检查。

问题是我不明白为什么字距调整始终为 0(即 FT_HAS_KERNING returns false,并且无论如何 FT_GetKerning returns 0)其他2种字体。

我用 fontforge 检查了 "VA" 和 "To" 对的字距调整信息,它们就在那里!所以它们必须存储在 .ttf 中。然而,对于上面的代码,"VA" 或 "To" 或 FT_HAS_KERNING returns false.

的字距调整始终为 0

这里是否缺少任何自由类型选项或设置? 任何形式的启蒙都将受到赞赏..

编辑: 我正在用

设置面部大小
FT_Set_Pixel_Sizes( face->getFace(), 0, size);

编辑: fontforge 中 "tymes" 字体的字距调整信息:

Freetype 只能从字体的 kern table 中检索字距调整值,而不能从使用 GPOS 作为 OpenType 功能的更现代的实现中检索。来自 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).

您的 FreeType 代码适用于 Times New Roman(我的是 "Monotype:Times New Roman Regular:Version 5.11 (Microsoft)"),因为它同时包含 tables:

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

但其他字体不包含kern

GPOS 字距调整优于普通 kern,因为它的 table 可以链接到特定的脚本和语言,并且它提供更好的控制。

也有充分的理由只包含一种类型的 table——如果两者都存在,则由字体渲染器决定是否包含 select 一种。 Microsoft's Recommendations for OpenType Fonts,例如,声明如下:

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.