python unicode 渲染:如何知道字体中是否缺少 unicode 字符 [英] python unicode rendering: how to know if a unicode character is missing from the font

查看:20
本文介绍了python unicode 渲染:如何知道字体中是否缺少 unicode 字符的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在 Python 中,当我呈现 unicode 字符时,例如一个汉字,在选定的字体下,对于常用的unicode字符,有时字体不完整,无法呈现有问题的unicode字符.在这些情况下,如果我调用打印"函数,输出通常看起来就像一个方框,而不管底层的 unicode 字符应该是什么样子.

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.

当然,一旦我打印了 unicode 字符,我就可以查看输出,然后确定所选字体是否遗漏了特定的 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. 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?

我还要澄清一下,我知道比其他字体更完整的字体.我的问题不是我可以使用哪种字体,所以如果我调用打印",我通常会有一个合理的输出.也请忽略我如何打印字符或我是否真的想打印字符的问题.我的问题很简单,对于任何给定的字体,我如何判断字体中是否缺少 unicode 字符,而不使用任何依赖于人工判断输出的手动过程.

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.

推荐答案

参见 https://unix.stackexchange.com/questions/247108/how-to-find-out-which-unicode-codepoints-are-defined-in-a-ttf 文件

简而言之,可以安装 fonttools 包,为它提供任何感兴趣的 .ttf 字体文件的路径,并检查感兴趣的 unicode 字符的长格式是否包含在字体文件的 unicode 映射表中.

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

然后只需调用char_in_font函数来检查字体中是否包含unicode字符.

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

这篇关于python unicode 渲染:如何知道字体中是否缺少 unicode 字符的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆