2010-12-16 195 views
39

如何在Linux上從TrueType或嵌入OpenType字體中提取受支持的Unicode字符的列表?查找字體支持哪些字符

是否有一個工具或庫可用於處理.ttf或.eot文件,並生成由字體提供的代碼點列表(如U + 0123,U + 1234等)?

回答

29

下面是使用FontTools模塊(你可以像安裝方法pip install fonttools):

#!/usr/bin/env python 
from itertools import chain 
import sys 

from fontTools.ttLib import TTFont 
from fontTools.unicode import Unicode 

ttf = TTFont(sys.argv[1], 0, verbose=0, allowVID=0, 
       ignoreDecompileErrors=True, 
       fontNumber=-1) 

chars = chain.from_iterable([y + (Unicode[y[0]],) for y in x.cmap.items()] for x in ttf["cmap"].tables) 
print(list(chars)) 

# Use this for just checking if the font contains the codepoint given as 
# second argument: 
#char = int(sys.argv[2], 0) 
#print(Unicode[char]) 
#print(char in (x[0] for x in chars)) 

ttf.close() 

腳本把參數作爲字體路徑:

python checkfont.py /path/to/font.ttf 
+0

'int(sys.argv [2],0)'在大多數情況下可能會失敗並顯示「無效文字」,因爲可能需要查找特殊字符。改用'ord(sys.argv [2] .decode('string_escape')。decode('utf-8'))'。 – 2017-02-07 12:44:12

+1

無論如何,這個基於'python-fontconfig'的腳本似乎要快得多:http://unix.stackexchange.com/a/268286/26952 – 2017-02-07 12:44:51

+0

@SkippyleGrandGourou那句話看起來不錯吧?它將'sys.argv [1]'傳遞給'TTFont()'? – Carpetsmoker 2017-02-07 15:28:31

6

ttf/otf字體的字符代碼點存儲在CMAP表中。

您可以使用TTX生成CMAP表的XML表示。看到http://www.letterror.com/code/ttx/index.html

一旦你運行TTX,你可以運行命令「ttx.exe -tcmap MyFont.ttf」,它應該輸出一個文件「MyFont.ttx」。在文本編輯器中打開它,它會顯示它在字體中找到的所有字符代碼。

+0

謝謝,這很有幫助。 – 2011-07-13 04:08:14

3

我剛剛遇到了同樣的問題,並且製作了一個HOWTO,它進一步烘烤了所有受支持的Unicode代碼點的正則表達式。

如果你只是想碼點的數組,你也可以在Chrome devtools您ttx XML偷看時,運行ttx -t cmap myfont.ttf後,很可能利用這一點,重命名myfont.ttxmyfont.xml調用瀏覽器的XML模式:

function codepoint(node) { return Number(node.nodeValue); } 
$x('//cmap/*[@platformID="0"]/*/@code').map(codepoint); 

(也依賴於fonttools從gilamesh的建議; sudo apt-get install fonttools如果你是一個Ubuntu系統上。)

18

Linux程序xfd可以做到這一點。它在我的發行版中被提供爲'xorg-xfd'。要查看字體的所有字符,可以在終端中運行此操作:

xfd -fa "DejaVu Sans Mono" 
+0

xfd也給出十六進制值,因爲你需要輸入它們爲unicode ala ctrl + shift + u – euxneks 2015-05-12 23:25:04

+12

打開一個GUI字符映射與列出支持的字符完全不一樣。 – rspeer 2015-08-04 03:15:31