fonttools正在读取cmap d

2024-04-25 22:54:06 发布

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

背景:

使用fonttools,我希望将“ل”(U+0644)等字符更改为其初始形式“ﻟ”(U+FEDF)

  1. 使用fonttools,将字体数据保存为xml,然后通过它进行解析

    font = TTFont(fontPath) font.saveXML("tempfont.xml")

  2. 在cmap表中找到与U+0644关联的名称(假设名称为“isolam”)

  3. 在GSUB表中找到“init”的表,找到“in”属性为“isolam”的条目,然后读取它的“out”属性(假设它是“initlam”)

  4. 最后在cmap表中搜索名称“initlam”,得到代码点

这个过程非常缓慢,我认为这是因为xml文件是在硬文件上写的,然后从那里读取的,还有大量的xml文件迭代。在

问题:

我现在尝试直接使用TTFont对象,而不是保存xml文件。但是我在从cmap读取代码点时遇到问题。在

font = TTFont(fontPath)
cmap = font['cmap'].tables

# there are 3 cmap tables for different platform in the font i am using, but
# for now i'm using cmap[2] which has platformId = 3 and is for windows.
print(cmap[2].data)

但结果似乎如此胡言乱语很长,所以我只展示一些:

b'\x00`\x00@\x00\x05\x00\x00!\x00+\x00/\x009\x00:\x00>\x00[\x00]\x00{\x00}\x00\xab\x00\xbb\

现在我希望它返回一个字典,其中代码点作为键,名称作为值,或者可能是元组列表。在

那么,如何以可理解的格式访问cmap数据呢?在

或者,如果给定了相关的代码点,如何获得glyph的名称,反之亦然?在


Tags: 文件数据代码in名称for属性xml
1条回答
网友
1楼 · 发布于 2024-04-25 22:54:06

要获得实际字符到cmap table中名称的映射,可以执行以下操作:

font = TTFont(fontPath)
ch_to_name = {} # key will be the codepoint in hex, value will be name

cmap = font["cmap"]
for ch, name in cmap.getBestCmap().items():
    ch_to_name["{:04X}".format(ch)] = name

相关问题 更多 >

    热门问题