通过fontforge脚本获取字形宽度 [英] Get glyph widths by fontforge script

查看:407
本文介绍了通过fontforge脚本获取字形宽度的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

要获取字形宽度,我将TTF字体转换为AFM,然后解析AFM文件的内容以获取每个字形的宽度.

For getting glyph widths, I convert a TTF font to AFM, and then parse the content of AFM file to get the width of each glyph.

从技术上讲,fontforge从二进制TTF文件捕获字形宽度,然后基于AFM标准模板创建AFM字体文件.我想知道是否可以通过fontforge命令将TTF文件直接转换为字形宽度列表?!?

Since technically, fontforge is capturing the glyph widths from the binary TTF file, and then create a AFM font file based on the AFM standard template. I wonder if it is possible to directly convert a TTF file to a list of glyph widths by a fontforge command?!?

推荐答案

FontForge包含两个解释器,因此您可以编写脚本来修改字体.这些解释器之一是Python(首选),一种是传统语言. Fontforge嵌入了Python,但也可以将Fontforge构建为Python扩展.

FontForge includes two interpreters so you can write scripts to modify fonts. One of these interpreters is Python (preferred), one is a legacy language. Fontforge embeds Python but it is also possible to build Fontforge as a Python extension.

那么您将使用什么: Python 传统语言? 什么界面:命令行 GUI Python扩展?

So what will you use: Python or Legacy language? What interface: Command line or GUI or Python extension?

该脚本可以位于文件中,也可以只是作为参数出现的字符串.您可能需要使用-lang参数指定要使用的解释器.请参见命令行参数.

The script can be in a file, or just a string presented as an argument. You may need to specify which interpreter to use with the -lang argument. See Command Line Arguments.

$ fontforge -script scriptfile.pe {arguments}
$ fontforge -c "script-string" {arguments}
$ fontforge -lang={ff|py} -c "script-string"

扫描文档之后,我编写了scriptfile.pe:

After scanning the documentation I wrote my scriptfile.pe:

Open($1, 1)
Select($2)
Print( GlyphInfo('Width') )

比:

$ fontforge -script scriptfile.pe YourFont.ttf A
... # Some output truncated.
1298

从GUI执行脚本

打开一种字体.比选择:文件">执行脚本...".输入:

Execute Scripts from GUI

Open a font. Than choose: 'File' > 'Execute script...'. Enter:

Select('A')
Error(ToString(GlyphInfo('Width')))

点击确定".

首先是单个字形的宽度(文档):

First the width of a single glyph (docs):

>>> import fontforge
>>> f = fontforge.open("YourFont.ttf")
>>> f['A'].width
1298

这是您问题的答案.对于每个字形,编码索引,名称和宽度:

Here the answer to your question. For each glyph the encoding index, name and width:

>>> for i in f.selection.all():
...    try:
...       name, width = f[i].glyphname, f[i].width
...       print i, name, width
...    except:
...       pass
... 
0 uni0009 0
2 uni0002 0
13 nonmarkingreturn 510
# ... Truncated ...
65707 germandbls.smcp 2266
>>>

注意:我使用try/except是因为f.selection.all()确实也选择了非字形.访问不存在的字形会引发错误.

Note: I used try/except because somehow f.selection.all() does also select non-glyphs. Accessing a glyph that doesn't exist will raise an error.

这篇关于通过fontforge脚本获取字形宽度的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆