我有兴趣提取给定 ttf 文件中所有字形的二次贝塞尔曲线信息。目前,使用 python 中的 ttfquery 库,我能够提取给定字形的轮廓(例如a
)按以下方式:
from ttfquery import describe
from ttfquery import glyphquery
import ttfquery.glyph as glyph
char = "a"
font_url = "/usr/share/fonts/truetype/liberation/LiberationSerif-Regular.ttf"
font = describe.openFont(font_url)
g = glyph.Glyph(char)
contours = g.calculateContours(font)
for contour in contours:
for point, flag in contour:
print point, flag
这对于字母字符效果很好,但对于数字、标点符号、空格等会出现以下关键错误:
Traceback (most recent call last):
File "demo.py", line 9, in <module>
contours = g.calculateContours(font)
File "/usr/local/lib/python2.7/dist-packages/ttfquery/glyph.py", line 33, in calculateContours
charglyf = glyf[self.glyphName]
File "/usr/local/lib/python2.7/dist-packages/FontTools/fontTools/ttLib/tables/_g_l_y_f.py", line 185, in __getitem__
glyph = self.glyphs[glyphName]
KeyError: '!'
获取贝塞尔曲线点以及每个字形的边界框(我目前正在使用从轮廓检索的最小和最大 x 和 y 值间接计算)的可靠方法是什么?
字形不一定以字符命名。尽管 TTF 文件中有一个结构将字符映射到字形,cmap https://developer.apple.com/fonts/TrueType-Reference-Manual/RM06/Chap6cmap.html。 ttyquery 有一个 API 可以访问该映射:
>>> ttfquery.glyphquery.glyphName(font, "!")
"exclam"
也就是说,替换
g = glyph.Glyph(char)
with
g = glyph.Glyph(ttfquery.glyphquery.glyphName(f, char))
并且您的代码应该可以工作。
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)