如何找出哪些字体可以显示这些字符?
How to find out which font can display these characters?
⚫⚪
Unicode U+26AB
Unicode U+26AA
这两个字符可以在终端显示,我想用convert
(imagemagick命令)将这些文字转换成图片。
但是convert
只能使用一种特殊字体,不能使用后备字体。
convert -list font
那么我怎样才能找到可以显示这些字符的字体呢?
我修改了 this answer 中的代码,制作了一个脚本来查找包含特定字符的字体,如下所示:
#!/usr/bin/env python3
import os, sys
import unicodedata
from fontTools.ttLib import TTFont
from sys import platform
def char_in_font(unicode_char, font):
for cmap in font['cmap'].tables:
if cmap.isUnicode():
if ord(unicode_char) in cmap.cmap:
return True
return False
def test(char):
for fontpath in fonts:
font = TTFont(fontpath) # specify the path to the font in question
if char_in_font(char, font):
print(char + " "+ unicodedata.name(char) + " in " + fontpath)
if __name__ == '__main__':
if platform == "linux":
likelyPlaces = ['/usr/share/fonts', '/usr/local/share/fonts', '~/.fonts']
elif platform == "darwin":
likelyPlaces = ['/System/Library/Fonts', '/Library/Fonts', '~/Library/Fonts']
elif platform == "win32":
likelyPlaces = ['WHO KNOWS']
fonts = []
for place in likelyPlaces:
for root,dirs,files in os.walk(os.path.expanduser(place)):
for file in files:
if file.endswith(".ttf"): fonts.append(os.path.join(root,file))
# Check user has specified a character
if len(sys.argv) != 2:
sys.exit("Usage: findfont.py glyph")
test(sys.argv[1])
所以,我现在可以如下调用它来找到包含你的两个圆圈的字体:
⚫⚪
Unicode U+26AB
Unicode U+26AA
这两个字符可以在终端显示,我想用convert
(imagemagick命令)将这些文字转换成图片。
但是convert
只能使用一种特殊字体,不能使用后备字体。
convert -list font
那么我怎样才能找到可以显示这些字符的字体呢?
我修改了 this answer 中的代码,制作了一个脚本来查找包含特定字符的字体,如下所示:
#!/usr/bin/env python3
import os, sys
import unicodedata
from fontTools.ttLib import TTFont
from sys import platform
def char_in_font(unicode_char, font):
for cmap in font['cmap'].tables:
if cmap.isUnicode():
if ord(unicode_char) in cmap.cmap:
return True
return False
def test(char):
for fontpath in fonts:
font = TTFont(fontpath) # specify the path to the font in question
if char_in_font(char, font):
print(char + " "+ unicodedata.name(char) + " in " + fontpath)
if __name__ == '__main__':
if platform == "linux":
likelyPlaces = ['/usr/share/fonts', '/usr/local/share/fonts', '~/.fonts']
elif platform == "darwin":
likelyPlaces = ['/System/Library/Fonts', '/Library/Fonts', '~/Library/Fonts']
elif platform == "win32":
likelyPlaces = ['WHO KNOWS']
fonts = []
for place in likelyPlaces:
for root,dirs,files in os.walk(os.path.expanduser(place)):
for file in files:
if file.endswith(".ttf"): fonts.append(os.path.join(root,file))
# Check user has specified a character
if len(sys.argv) != 2:
sys.exit("Usage: findfont.py glyph")
test(sys.argv[1])
所以,我现在可以如下调用它来找到包含你的两个圆圈的字体: