我们如何从python中的字形id中获取unicode? [英] How could we get unicode from glyph id in python?
问题描述
如果我有如下所示的字形id,如何从它们中获取unicode,那么我正在使用的语言是python?另外,我理解的第二个值是字形ID,但是我们称第一个值和第三个值是什么?
If I have glyph ids like below how can I get the unicode from them, the language is python that I am working on ? Also what I understand the second value is the glyph id but what do we call the first value and the third value?
(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.这是代码:
Actually I am trying to get the unicode from a given ttf file in python.Here is the code :
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吗?
This code I got from stackoverflow only but this gives the above output and not what I require. So could anybody please tell me how to fetch the unicodes from the ttf file or is it fine to convert the glyphid to unicode, will it yield to actual unicode ?
推荐答案
您可以使用第一个字段:unichr(x [0]),或等效地使用第二个字段.然后,删除"uni"部分([3:]),然后将其转换为十六进制值'Ɇ'e,然后转换为字符.当然,第一种方法更快,更简单.
You can use the first field: unichr(x[0]), or equivalently the second field. Then you remove the "uni" part ([3:]) and you convert it to a hexadecimal valu'Ɇ'e, then to a character. Of course, the first method is faster and simpler.
unichr(int(x [1] [3:],16))#对于显示的第一个项目,返回Ɇ",对于第二个显示"returns"
unichr(int(x[1][3:], 16)) #for the first item you've showed, returns 'Ɇ', for the second 'ɇ'
如果您使用python3,请使用chr而不是unichr.
If you use python3, chr instead of unichr.
这篇关于我们如何从python中的字形id中获取unicode?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!