為什麼要學這個?
與使用「人工」手動去處理相比,用程式去刪除特定文字有效率。
這個程式與另外2個程式,有一點點相似,如果是使用 copy_lost_out.py 或使用 copy_selected_out.py 是無法處理「複雜的」字型檔,如果使用了 .vert 檔案 或是設定了 kerning 之類的設定值,暫時還沒有寫程式去使判斷。
延伸的問題是,要避免直接複製.glyph 到 font forage 專案的目錄裡,遇到複雜的字型檔時,會造成 kerning 之類的設定值錯亂,因為共用的外部檔案的編號會相同,所以要合併複雜的字型時,請使用 Font Forge 的 Merge Font… 來合併。
重要:刪除檔案前,請先一定要做要備份,因為會救不回來!有提供參數 –check_only , 就不會刪除任何檔案.
下面提供二種解法來處理:
- 解法1: 透過 python 處理 fontforge 匯出的專案目錄.
- 解法2: 透過 fontforge 開啟字型檔, 從字串或文字檔移除目標
- 解法3: 透過 fontforge 開啟2個字型檔, 字型檔移除字型檔包含項目
前置作業
使用前,需要先安裝FontForge,把要處理的字體先存成.sfdir 的資料夾格式。
FontForge執行檔下載:
https://fontforge.org/en-US/
解法1: 透過 python 處理 fontforge 匯出的專案目錄.
Max腳本檔案下載:
https://github.com/max32002/MaxFontScripts/blob/master/delete_selected_glyph.py
使用方法:
delete_selected_glyph.py --input INPUT --string STRING
參數說明:
--input
要處理的字體專案目錄。--string
要取出的字串。--check_only
不實際刪除檔案。--check_alt
檢查 alt 欄位。
操作範例:
delete_selected_glyph.py --input source.sfdir --string 每一天
使用範例:從 source.sfdir 刪除 「每一天」3個字的 .glyph 檔案。
說明: 上面的範例, 只能針對 .sfdir 的資料夾做處理.
解法2: 透過 fontforge 開啟字型檔, 從字串或文字檔移除目標
Max腳本檔案下載:
https://github.com/max32002/MaxFontScripts/blob/master/clear_glyph.py
使用方法:
fontforge clear_glyph.py --input INPUT --output new-font.ttf --string STRING
參數說明:
--input
輸入的來源字體專案目錄。--output
要處理的字體專案目錄或字型檔。--string
要取出的字串。預設值是空字串.--file
使用文字檔內的子串.--skip_alt
遇到有alt (lookup) 的字, 不刪除. 以避免錯誤: Lookup subtable contains unused glyph uni???? making the whole subtable invalid
執行結果:

說明:
- 開啟 alpha-1.ttf
- 刪除字元 g
- 另存為 alpha-2.ttf
- 來源字型字元數: 8
- 被清除掉的字元數: 1, (代表: 另存完的字元數: 7)
- 這個範例可以直接對2個字型檔操作, 針對大型的字型檔, 效率非常顯著的提升很多.
解法3: 透過 fontforge 開啟2個字型檔, 字型檔移除字型檔包含項目
Max腳本檔案下載:
https://github.com/max32002/MaxFontScripts/blob/master/font_delete_font.py
使用方法:
fontforge font_delete_font.py --input input_font --remove selecte_font --output new-font.ttf
參數說明:
--input
輸入的來源字體專案目錄。--remove
要移除文字清單的字體專案目錄。--output
要處理的字體專案目錄或字型檔。
執行結果:

說明:
- 開啟 1.sfdir, 當作來源字型.
- 開啟 2.sfdir, 列出所有的文字清單.
- 另存為 3.sfdir
- 被清除掉的字元數: 2, 在個案之中, 這2個字其實只有一個檔案, 使用了 alt 對映.
- 被清除掉的字元數: 1, 在個案之中, 來源字型 1.sfdir 只有2個字, 2.sfdir 是1個字, 但有使用 alt 對映.
- 這個範例可以直接對2個字型檔 .ttf 操作, 針對大型的字型檔, 效率非常顯著的提升很多.
- 也可以使用這個 script 來解決刪除不常用字, 讓字型減肥的效果, 參考:
從文字檔移除特定文字集合
https://codereview.max-everyday.com/remove-selected-char/
相關文章:
透過指令「字體」與「FontForge專案」互轉
https://codereview.max-everyday.com/font-to-fontforge/