Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
292 views
in Technique[技术] by (71.8m points)

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

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 can't render the unicode character in question. In those cases, if I call the "print" function, the output usually just look like a square box, regardless what the underlying unicode character should look like.

Of course, once I print the unicode character, I can look at the output and then determine that the chosen font misses the particular unicode character. But is there a way to tell before I print, automatically, without having to resort to my own human eyes to determine if a character is included in the font?

I'd also clarify that I know of fonts that are more complete than others. My question is NOT which font I can use so that if I call "print" I'd generally have a reasonable output. Please also ignore the question of how I print the character or if I actually want to print a character. My question is simply, for any given font, how do I tell if a unicode character is missing from the font, without using any manual process relying on human judgement of the output.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...