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

标签 python truetype

我有兴趣提取给定 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))

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

关于python - 在 python 中的 .ttf 字体文件中检索所有字形的边界框和贝塞尔曲线数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40437308/

相关文章:

linux - 检查字体是否等宽

python - pandas 按不区分大小写的两个列值过滤行

python - python比较程序

python - 渐近函数趋近于零的 float 问题 - Python

Python `type(x)==` 什么?

html - 为什么一种字体在 IE 中可以工作,而另一种却不能

python - 将一系列 .svg 文件作为字形导入 FontForge 并输出字体文件

truetype - OpenType的cmap中platformID和encodingID是什么意思?

php - 使用 PHP 获取 TrueType 中可用的字符

python - 确定第一组 CFG