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

时间:2016-11-05 10:47:00

标签: python true-type-fonts

我感兴趣的是提取给定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值间接计算)?

1 个答案:

答案 0 :(得分:3)

字形不一定以字符命名。虽然将字符映射到字形cmap,但TTF文件中有一个结构。 ttyquery有一个访问该地图的API:

stage('Deploy') {
withCredentials([[$class: 'FileBinding', credentialsId: 'bitbucket-key-file', variable: 'SSHKEY']]) {
    bat '''
        E:\\Jenkins\\tools\\Git_2.10.1\\usr\\bin\\scp.exe -i "${SSHKEY}" dsub.tar.gz tprmbbuild@192.168.220.57:dsubdeploy
        E:\\Jenkins\\tools\\Git_2.10.1\\usr\\bin\\scp.exe -i "${SSHKEY}" deployDsubUi.sh tprmbbuild@192.168.220.57:dsubdeploy
        E:\\Jenkins\\tools\\Git_2.10.1\\usr\\bin\\scp.exe -i "${SSHKEY}" -o StrictHostKeyChecking=no 192.168.220.57 < server_script.sh
}

即,替换

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

g = glyph.Glyph(char)

并且您的代码应该可以使用。