简体   繁体   English

python unicode rendering:如何知道字体中是否缺少unicode字符

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

In Python when I render a unicode character, eg 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. 在Python中,当我使用选定的字体渲染unicode字符(例如中文字符)时,有时字体对于常见的unicode字符是不完整的,并且无法呈现有问题的unicode字符。 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. 在这些情况下,如果我调用“print”函数,输出通常看起来像一个方框,无论底层unicode字符应该是什么样子。

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. 当然,一旦我打印出unicode字符,我就可以查看输出,然后确定所选字体是否错过了特定的unicode字符。 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. 我的问题很简单,对于任何给定的字体,我如何判断字体中是否缺少unicode字符,而不使用依赖于人类对输出的判断的任何手动过程。

See https://unix.stackexchange.com/questions/247108/how-to-find-out-which-unicode-codepoints-are-defined-in-a-ttf-file 请参阅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. 简而言之,可以安装fonttools包,为其提供任何感兴趣的.ttf字体文件的路径,并检查字体文件的unicode映射表中是否包含长形式的感兴趣的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

Then just call the char_in_font function to check if the unicode character is included in the font. 然后只需调用char_in_font函数来检查字体中是否包含unicode字符。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM