python - 在 python 中的 .ttf 字体文件中检索所有字形的边界框和贝塞尔曲线数据

我有兴趣提取给定 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 . ttyquery 有一个 API 来访问该 map :

>>> ttfquery.glyphquery.glyphName(font, "!")
"exclam"

即替换

g = glyph.Glyph(char)

g = glyph.Glyph(ttfquery.glyphquery.glyphName(f, char))

并且您的代码应该可以工作。

https://stackoverflow.com/questions/40437308/

相关文章:

reactjs - 如何淡入淡出文本

performance - AWS 资源共享

spring-boot - 我如何在 Spring Boot-1.4.1-RELEASE 中制作 i

asp.net - 时间的 DataFormatString (ASP.Net)

angular - 如何检测下拉列表元素的变化?

math - 围绕一个点旋转 Vector2

php - 如何使用 laravel 5.2 获取 Blade 文件(HTML)中的最后一部分 ur

git - 如何直接从 Google Cloud Source Repository 部署到 Goo

python - django 模板中的多个加载标签

php - 使用干预将图像的 DPI 从 72 更改为 300