是否有一些 python 工具或软件工具,通过它我可以访问字体内的所有组件和表格?

Is there some python tool or software tool through which i can access all the components and tables inside of a fonts?

我的最终目标是创建一个从 glyph_idunicode_chars 的映射。该映射将有点顺序 glyph_id --> uni_1, uni_2, uni_3 ... 因为单个字形可以映射到许多有序的 unicode_characters.

我正在寻找一些工具或库,最好是在 python 中,通过它我可以访问所有元信息,例如字体内部的 table。

此外,我正在寻找一些可靠的资源,通过它我可以理解将多个 Unicode 映射到字形的过程。

我知道像 harfbuzz 这样的工具会在给定的 Unicode 字符串上生成 (glyph, position) 对。但是不知道是不是反了。

我们将不胜感激。

您应该查看 fontTools Python 库,其中包含使用字体所需的组件。

你感兴趣的字体table是'cmap'table,你想要的基本上是Unicode映射子table的反向映射(有几种可以映射 Unicode 的子 table;如果您不熟悉这个概念,我建议您查看 OpenType specification 以获取更多信息)。基本上你得到了 Unicode 到字形的映射,然后反转它。

fontTools 实际上有一个很好的功能,它会自动 select "best" cmap subtable(它有一个首选 cmap subtable 种类的有序列表,并且return 是您打开的特定字体中的第一个)。下面是使用该函数的示例:

from fontTools.ttLib import TTFont
from collections import defaultdict

font = TTFont('path/to/fontfile.ttf')
unicode_map = font.getBestCmap()
reverse_unicode_map = defaultdict(list)

for k, v in unicode_map.items():
    reverse_unicode_map[v].append(k)

reverse_unicode_map 现在保存字形(字形名称)到整数代码点列表的映射:

>>> reverse_unicode_map
defaultdict(<class 'list'>, {'.null': [0, 8, 29], 'nonmarkingreturn': [9, 13], 'space': [32], 'exclam': [33], 'quotedbl': [34], 'numbersign': [35], 'dollar': [36], 'percent': [37], 'quotesingle': [39], 'parenleft': [40], 'parenright': [41], 'asterisk': [42], 'plus': [43], 'comma': [44], 'hyphen': [45], 'period': [46], 'slash': [47], 'zero': [48], 'one': [49], 'two': [50], 'three': [51], 'four': [52], 'five': [53]})

您可以看到有 2 个字形,“.null”和 "nonmarkingreturn" 映射到多个 Unicode。

如果您需要将字形名称解析为字形 indices,您可以使用 font.getGlyphID() 方法(传入字形名称;它将 return对应的整数ID)。