Search code examples
linuxshellfontsimagemagick

How to find out which font can display these characters?


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

enter image description here

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?


Solution

  • 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:

    enter image description here