How to find out which font can display these characters?

烈酒焚心 提交于 2021-02-05 08:32:04

问题


⚫⚪
Unicode U+26AB
Unicode U+26AA

This two characters can be display in terminal, I want use convert(imagemagick command) to convert those text into picture.

But convert only can use one special font, not fallback font could be used.

convert -list font

So how can I find which font could display those characters?


回答1:


I adapted the code in this answer, to make a script to find which font contains a particular character as follows:

#!/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])

So, I can now call it as follows to find the font containing your two circles:



来源:https://stackoverflow.com/questions/60027198/how-to-find-out-which-font-can-display-these-characters

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!