python - 我们如何从Python中的glyph id获取unicode?

标签 python python-2.7 unicode glyph

如果我有像下面这样的字形 ID,我如何从中获取 unicode,我正在使用的语言是 python?另外,我理解第二个值是字形 id,但是我们如何称呼第一个值和第三个值?

 (582, 'uni0246', 'LATIN CAPITAL LETTER E WITH STROKE'), (583, 'uni0247', 'LATIN SMALL LETTER E WITH STROKE'), (584, 'uni0248', 'LATIN CAPITAL LETTER J WITHSTROKE'), (585, 'uni0249', 'LATIN SMALL LETTER J WITH STROKE')

请回复。

实际上,我正在尝试从 python 中给定的 ttf 文件获取 unicode。这是代码:

 from fontTools.ttLib import TTFont
 from fontTools.unicode import Unicode
 from ttfquery import ttfgroups
 from fontTools.ttLib.tables import _c_m_a_p
 from itertools import chain

 ttfgroups.buildTable() 
 ttf = TTFont(sys.argv[1], 0, verbose=0, allowVID=0,
            ignoreDecompileErrors=True,
            fontNumber=-1)

 chars = chain.from_iterable([y + (Unicode[y[0]],) for y in x.cmap.items()] for x in ttf["cmap"].tables)
 print(list(chars))`

这段代码是我从 stackoverflow 获得的,但这给出了上面的输出,而不是我所需要的。那么有人可以告诉我如何从 ttf 文件中获取 unicode 吗?或者可以将 glyphid 转换为 unicode,它会产生实际的 unicode 吗?

最佳答案

您可以使用第一个字段:unichr(x[0]),或等效的第二个字段。然后删除“uni”部分([3:])并将其转换为十六进制值'Ɇ'e,然后转换为字符。当然,第一种方法更快更简单。

unichr(int(x[1][3:], 16)) #对于您显示的第一个项目,返回“Ɇ”,对于第二个“ɇ”

如果您使用 python3,请使用 chr 而不是 unichr。

关于python - 我们如何从Python中的glyph id获取unicode?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29138935/

相关文章:

Python/Regex - 括号和斜杠的扩展

python - 为什么我不能在函数内定义(和保存)Tkinter 字体?

python-2.7 - Python pptx : how to find and delete the empty or unpopulated placeholders

mysql - 使用 'regexp' 的相同查询在 mysql 中给出不同的结果

python - 在 Python 中发送非 ASCII POST 请求?

python - 访问下一个同级的文本

python - Keras输入形状错误

python - Pygame fill 直到将其移出屏幕后才会更新

python - 使用 sqlalchemy 和 psycopg2 将数据帧推送到 postgres

powershell - 在 PowerShell 中显示 Unicode 表情符号