Font­Tools 安裝與使用簡明指南

Posted in :

Max 最近在協助其他的好心人開發免費的字型,順便學一下關於字型的相關技術。

Font­Tools 是一套以 ttx 為核心的工具集,用於處理與字體編輯有關的各種問題,程序用 Python 編寫完成,代碼開源,具有良好的跨平台性。

FontForeg正式版和開發中的版本下載:
https://fontforge.org/en-US/downloads/

官網網站:
https://github.com/fonttools/fonttools

Font­Tools 由以下 4 個程序組成:

  • ttx 可將字體文件與 xml 文件進行雙向轉換
  • pyft­merge 可將數個字體文件合併成為一個字體文件
  • pyft­sub­set 可產生一個由字體的指定字符組成的子集
  • pyftin­spect 可顯示字體文件的二進制組成信息

安裝 Font­Tools

詳細介紹:
https://pypi.org/project/fonttools/

指令:

pip install fonttools

字體基本知識

一個字體由數個表(ta­ble)構成,字體的信息儲存在表中。一個最基本的字體文件一定會包含以下的表:

  • cmap: Char­ac­ter to glyph map­ping
  • head: Font header
  • hhea: Hor­i­zon­tal header
  • hmtx: Hor­i­zon­tal met­rics
  • maxp: Max­i­mum pro­file
  • name: Nam­ing ta­ble
  • OS/2: OS/2 and Win­dows spe­cific met­rics
  • post: Post­Script in­for­ma­tion:

以上都不知道,也沒差,有個印象就好了。


使用 True­Type 曲線繪制的字體會包含如下的表:

  • cvt: Con­trol Value Table
  • fpgm: Font pro­gram
  • glyf: Glyph data
  • loca: In­dex to lo­ca­tion
  • prep: CVT Pro­gram
  • gasp: Grid-fit­ting/Scan-con­ver­sion (op­tional ta­ble)

以上都不知道,也沒差,有個印象就好了。


使用 Post­Script 曲線繪制的字體會包含如下的表:

  • CFF: Post­Script font pro­gram (com­pact font for­mat)
  • VORG: Ver­ti­cal Ori­gin (op­tional ta­ble)

使用 SVG 曲線繪制的字體會包含如下的表:

  • SVG: The SVG (Scal­able Vec­tor Graph­ics) ta­ble

使用 Bitmap 圖形構成的字體會包含如下的表:

  • EBDT: Em­bed­ded bitmap data
  • EBLC: Em­bed­ded bitmap lo­ca­tion data
  • EBSC: Em­bed­ded bitmap scal­ing data
  • CBDT: Color bitmap data
  • CBLC: Color bitmap lo­ca­tion data

包含高級書法特性的字體會包含如下的表:

  • BASE: Base­line data
  • GDEF: Glyph de­f­i­n­i­tion data
  • GPOS: Glyph po­si­tion­ing data
  • GSUB: Glyph sub­sti­tu­tion data
  • JSTF: Jus­ti­fi­ca­tion data
  • MATH: Math lay­out data

包含其他特性的字體會包含如下的表:

  • DSIG: Dig­i­tal sig­na­ture
  • hdmx: Hor­i­zon­tal de­vice met­rics
  • kern: Kern­ing
  • LTSH: Lin­ear thresh­old data
  • PCLT: PCL 5 data
  • VDMX: Ver­ti­cal de­vice met­rics
  • vhea: Ver­ti­cal Met­rics header
  • vmtx: Ver­ti­cal Met­rics
  • COLR: Color ta­ble
  • CPAL: Color palette ta­ble

對字體的修改基本上是圍繞著最上方的基本表進行的,如果要修改字符形態等才需要用到之後的表。

以上都不知道,也沒差,有個印象就好了。


ttx 指令使用說明

ttx 是 Font­Tools 的核心工具,用於將字體轉換為 xml 文件,或者將 xml 文件轉換回字體。ttx 所產生的 xml 文件的後綴名是 ttx,可以用各種文檔編輯器打開進行編輯。

將字體全部的表轉換為 ttx 文件:

$ ttx font.ttf

將 ttx 文件轉換回字體文件。

$ ttx font.ttx

pyft­merge 使用說明

合併字體:

$ pyft­merge font1.otf font2.otf

實測結果:

合併失敗

➜  ttf pyftmerge temp.ttf temp2.ttf       
 WARNING: Dropped cmap subtable from font [0]: format  0, platformID  1, platEncID  0
 WARNING: Dropped cmap subtable from font [1]: format  0, platformID  1, platEncID  0
 Traceback (most recent call last):
   File "/usr/local/bin/pyftmerge", line 8, in <module>
     sys.exit(main())
   File "/usr/local/lib/python2.7/site-packages/fontTools/misc/loggingTools.py", line 375, in wrapper
     return func(*args, **kwds)
   File "/usr/local/lib/python2.7/site-packages/fontTools/merge.py", line 1157, in main
     font = merger.merge(args)
   File "/usr/local/lib/python2.7/site-packages/fontTools/merge.py", line 997, in merge
     table = clazz(tag).merge(self, tables)
   File "/usr/local/lib/python2.7/site-packages/fontTools/merge.py", line 160, in merge
     return m.mergeObjects(self, self.mergeMap, tables)
   File "/usr/local/lib/python2.7/site-packages/fontTools/merge.py", line 1043, in mergeObjects
     value = mergeLogic(getattr(table, key, NotImplemented) for table in tables)
   File "/usr/local/lib/python2.7/site-packages/fontTools/merge.py", line 49, in equal
     assert all(item == first for item in t), "Expected all items to be equal: %s" % lst
 AssertionError: Expected all items to be equal: [1024, 1000]

pyft­sub­set 使用說明

pyft­sub­set 可提取一個字體的部分字符,產生一個只由它們組成的新字體。通過這一子集化技術,可有效縮小字體文件的體積,便於網絡傳輸。

用法:

$ pyft­sub­set font.otf --text="漢字"
  • –text 選項用於指定需要保留的字符
  • –text-file 選項用於指定一個包含需要保留的字符的 txt 文檔
  • –out­put-file 選項用於指定輸出文件的保存位置

發佈留言

發佈留言必須填寫的電子郵件地址不會公開。 必填欄位標示為 *