如何从Linux上的TrueType或嵌入式OpenType字体中提取受支持的Unicode字符列表?
是否有工具或库可用于处理.ttf或.eot文件并构建字体提供的代码点列表(如U + 0123,U + 1234等)?
这是一个使用FontTools模块的方法(可以使用pip install fonttools
之类的东西安装):
#!/usr/bin/env python
from itertools import chain
import sys
from fontTools.ttLib import TTFont
from fontTools.unicode import Unicode
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))
# Use this for just checking if the font contains the codepoint given as
# second argument:
#char = int(sys.argv[2], 0)
#print(Unicode[char])
#print(char in (x[0] for x in chars))
ttf.close()
该脚本将字体路径作为参数:
python checkfont.py /path/to/font.ttf
Linux程序xfd可以做到这一点。它在我的发行版中提供为'xorg-xfd'。要查看字体的所有字符,可以在终端中运行:
xfd -fa "DejaVu Sans Mono"
fc-query my-font.ttf
将根据fontconfig为您提供支持的字形和字体适合的所有语言环境的映射
由于几乎所有现代Linux应用程序都基于fontconfig,因此这比原始unicode列表更有用
这里讨论实际的输出格式http://lists.freedesktop.org/archives/fontconfig/2013-September/004915.html
ttf / otf字体的字符代码点存储在CMAP
表中。
您可以使用ttx
生成CMAP
表的XML表示。见here。
你可以运行命令ttx.exe -t cmap MyFont.ttf
,它应该输出一个文件MyFont.ttx
。在文本编辑器中打开它,它应该显示它在字体中找到的所有字符代码。
fontconfig
命令可以输出字形列表作为范围的紧凑列表,例如:
$ fc-match --format='%{charset}\n' OpenSans
20-7e a0-17f 192 1a0-1a1 1af-1b0 1f0 1fa-1ff 218-21b 237 2bc 2c6-2c7 2c9
2d8-2dd 2f3 300-301 303 309 30f 323 384-38a 38c 38e-3a1 3a3-3ce 3d1-3d2 3d6
400-486 488-513 1e00-1e01 1e3e-1e3f 1e80-1e85 1ea0-1ef9 1f4d 2000-200b
2013-2015 2017-201e 2020-2022 2026 2030 2032-2033 2039-203a 203c 2044 2070
2074-2079 207f 20a3-20a4 20a7 20ab-20ac 2105 2113 2116 2120 2122 2126 212e
215b-215e 2202 2206 220f 2211-2212 221a 221e 222b 2248 2260 2264-2265 25ca
fb00-fb04 feff fffc-fffd
使用fc-query
作为.ttf
文件,使用fc-match
作为已安装的字体名称。
这可能不涉及安装任何额外的包,也不涉及翻译位图。
使用fc-match --format='%{file}\n'
检查是否匹配了正确的字体。
我只是遇到了同样的问题,并使HOWTO更进了一步,烘焙了所有支持的Unicode代码点的正则表达式。
如果你只是想要一系列代码点,你可以在运行ttx
后,在Chrome devtools中查看你的ttx -t cmap myfont.ttf
xml时使用它,并且可能将myfont.ttx
重命名为myfont.xml
以调用Chrome的xml模式:
function codepoint(node) { return Number(node.nodeValue); }
$x('//cmap/*[@platformID="0"]/*/@code').map(codepoint);
(还依赖于gilamesh的建议中的fonttools
;如果你使用的是ubuntu系统,则需要sudo apt-get install fonttools
。)
如果您只想“查看”字体,以下内容可能会有所帮助(如果您的终端支持相关字体):
#!/usr/bin/env python
import sys
from fontTools.ttLib import TTFont
with TTFont(sys.argv[1], 0, ignoreDecompileErrors=True) as ttf:
for x in ttf["cmap"].tables:
for (_, code) in x.cmap.items():
point = code.replace('uni', '\\u').lower()
print("echo -e '" + point + "'")
查看不安全但容易的方法:
python font.py my-font.ttf | sh
感谢Janus(https://stackoverflow.com/a/19438403/431528)获得上述答案。
您可以使用Font::TTF模块在Perl上的Linux上执行此操作。