Python unicode rendering: how to find out if a Unicode character is missing in a font

In Python, when rendering a Unicode character, for example. Chinese character, with the selected font, sometimes the font is incomplete with respect to common Unicode characters and cannot display the Unicode character. In cases where I call the print function, the output usually looks like a square square, regardless of how the main Unicode character should look.

Of course, as soon as I type in the Unicode character, I can look at the result and then determine that the selected font skips the specific Unicode character. But is there a way to say before I type automatically without resorting to a personโ€™s own eyes to determine if the character is included in the font?

I will also clarify that I know fonts that are more complete than others. My question is NOT which font I can use, so that if I call "print" I usually have a reasonable conclusion. Also ignore the question of how I print the character or really want to print the character. My question is simply for any given font, how can I determine if a Unicode character is missing in a font without using any manual process based on a personโ€™s judgment of the exit.

+4
source share
1 answer

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

, fonttools, .ttf, , unicode .

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

char_in_font, , .

+6

Source: https://habr.com/ru/post/1676605/


All Articles