有'字体工具 https://github.com/behdad/fonttools' 包,其中包含一个用于读取和查询 TrueType 字体的 python 类。像这样的事情是可能的
from fontTools.ttLib import TTFont
f = TTFont('/path/to/font/arial.ttf')
print(f.getGlyphOrder()) # a list of the glyphs in the order
# they appear
print(f.getReversedGlyphMap() # mapping from glyph names to Id
id = f.getGlyphID('Euro') # The internal code for the Euro character,
# Raises attribute error if the character
# isn't present.
从字符到字形的映射通常很复杂,并且在字体的 cmap 表中定义。这是一个二进制部分,但可以通过以下命令进行检查
f.getTableData('cmap')
一种字体可以有多个 cmap 表。这自由类型接口 https://github.com/rougier/freetype-py还可以读取ttf文件。可以使用 freetype 尝试渲染字符,并查看结果:这会很慢。
import freetype as ft
face = ft.Face('arial.ttf')
face.set_size(25*32)
face.load_char(‽)
bitmap = face.glyph.bitmap.buffer
if bitmap == [255, 255, 255, 255, 0, 255, 255, 0, 255, 255, 0, 255, 255, 0, 255, 255, 0, 255, 255, 255, 255]:
print("No interobang in the font")