python unicode rendering: how to know if a unicode character is missing from the font

后端 未结 1 887
你的背包
你的背包 2020-12-15 11:01

In Python when I render a unicode character, e.g. a Chinese character, with a selected font, sometimes the font is incomplete regarding the common unicode characters, and ca

相关标签:
1条回答
  • 2020-12-15 11:28

    See https://unix.stackexchange.com/questions/247108/how-to-find-out-which-unicode-codepoints-are-defined-in-a-ttf-file

    In short, one can install the fonttools package, supply it with the path to any .ttf font file of interest, and check if the long form of the unicode character of interest is included in the font file's unicode map table.

    from fontTools.ttLib import TTFont
    font = TTFont(fontpath)   # specify the path to the font in question
    
    
    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
    

    Then just call the char_in_font function to check if the unicode character is included in the font.

    0 讨论(0)
提交回复
热议问题