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

2024-09-28 01:28:11 发布

您现在位置:Python中文网/ 问答频道 /正文

我的最终目标是创建从glyph_idunicode_chars的映射。这种映射在某种程度上是有序的glyph_id --> uni_1, uni_2, uni_3 ...,因为单个glyph可以映射到许多有序的unicode_characters

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

另外,我正在寻找一些可靠的源代码,通过这些源代码我可以理解将多个Unicode映射到glyph的过程

我知道harfbuzz之类的工具会在给定的Unicode字符串上生成(glyph,position)对。但我不确定它是否会反过来

谢谢你的帮助


Tags: 工具信息id源代码过程unicode字体harfbuzz
1条回答
网友
1楼 · 发布于 2024-09-28 01:28:11

您可能应该查看fontToolsPython库,其中包含处理字体所需的组件

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

fontTools实际上有一个很好的特性,可以自动选择“最佳”cmap子表(它有一个首选cmap子表类型的有序列表,并返回您打开的特定字体中的第一个可用字体)。下面是使用该函数的示例:

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现在保存glyph(glyph name)到整数代码点列表的映射:

>>> 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]})

您可以看到有两个glyph,“.null”和“nonmarkingreturn”映射到多个Unicode

如果需要将glyph名称解析为glyph索引,可以使用font.getGlyphID()方法(传入glyph名称;它将返回相应的整数ID)

相关问题 更多 >

    热门问题