gpt4 book ai didi

python - 如何判断一个字形是否可以显示?

转载 作者:行者123 更新时间:2023-12-05 04:44:48 24 4
gpt4 key购买 nike

我有一大堆要显示的 Unicode 图标。但是,我想隐藏/跳过任何我无法显示的图标(因为我没有安装正确的字体)。是否有程序化的方法来确定这一点?

最佳答案

Python 中没有为此内置任何内容。但是,您可以应用 fonttools 模块,例如如下(在Windows 10中使用):

# ToDo: find fallback font
# ToDo: reverse algorithm (font => characters) instead of (character => fonts)
# ToDo: check/print merely basic font (omit variants like Bold, Light, Condensed, …)

import unicodedata
import sys
import os
from fontTools.ttLib import TTFont, TTCollection

fontsPaths = []
fontcPaths = []
fontsdirs = [ os.path.join( os.getenv('SystemRoot'), 'Fonts') # r"c:\Windows\Fonts"
, r"D:\Downloads\MathJax-TeX-fonts-otf"
# , os.path.join( os.getenv('LOCALAPPDATA'), r'Microsoft\Windows\Fonts')
]

print(fontsdirs, file=sys.stderr)
for fontsdir in fontsdirs:
for root,dirs,files in os.walk( fontsdir ):
for file in files:
if file.endswith(".ttf") or file.endswith(".otf") or file.endswith(".ttc"):
tfile = os.path.join(root,file)
if file.endswith(".ttc"):
fontcPaths.append(tfile)
else:
fontsPaths.append(tfile)
# print( len(fonts), "fonts", fontsdir)

def char_in_font(unicode_char, font):
for cmap in font['cmap'].tables:
if cmap.isUnicode() or cmap.getEncoding() == 'utf_16_be':
if ord(unicode_char) in cmap.cmap:
# print(type(cmap))
auxcn = cmap.cmap[ord(unicode_char)]
# print(auxcn, type(auxcn))
return auxcn if auxcn != '' else '<nil>'
return ''

def checkfont(char,font,fontdict,fontpath):
nameID_index = 1 # works generally (not always)
for i,f in enumerate(font['name'].names):
# An Introduction to TrueType Fonts: A look inside the TTF format
# https://scripts.sil.org/cms/scripts/page.php?site_id=nrsi&id=IWS-Chapter08
# 1 = Font Family name, 2 = Font SubFamily name, 4 = Full font name
if f.nameID == 1:
nameID_index = i
break
fontname = font['name'].names[nameID_index].toStr()
if fontname not in fontdict.keys():
aux = char_in_font(char, font)
if aux != '':
fontdict[fontname] = "{} ({}) [{}] '{}' \t {} {}".format(
char,
'0x{:04x}'.format(ord(char)),
aux,
fontname, # string.decode('unicode-escape'),
# '', ''
'in', fontpath.split('\\')[-1]
)

def testfont(char):
fontdict = {}
for fontpath in fontsPaths:
font = TTFont(fontpath) # specify the path to the font
checkfont(char,font,fontdict,fontpath)
for fontpath in fontcPaths: # specify the path to the font collection
fonts = TTCollection(fontpath)
for ii in range(len(fonts)):
font = TTFont(fontpath, fontNumber=ii) # fontfile and index
checkfont(char,font,fontdict,fontpath)
return fontdict.values()

def testprint(char):
print('') # empty line for better readability
print(char, ' 0x{:04x}'.format(ord(char)), unicodedata.name(char, '???'))
fontarray = testfont(char)
for x in fontarray:
print(x)

if len(sys.argv) == 1:
# sample output
testprint(u"अ") # 0x0905 Devanagari Letter A
else:
for i in range( 1, len(sys.argv) ):
if len(sys.argv[i]) >=2:
try:
chars = chr(int(sys.argv[i])) # 0x042F or 1071
except:
try:
chars = chr(int(sys.argv[i],16)) # 042F
except:
chars = (sys.argv[i].
encode('raw_unicode_escape').
decode('unicode_escape')) # ➕🐈\U00010A30\u042F\xFE
else:
chars = sys.argv[i] # Я (Cyrillic Capital Letter Ya)
for char in chars:
testprint(char);

示例输出(如果不带参数调用):.\FontGlyphs.py

['C:\\WINDOWS\\Fonts', 'D:\\Downloads\\MathJax-TeX-fonts-otf']

अ 0x0905 DEVANAGARI LETTER A
अ (0x0905) [uni0905] 'Nirmala UI' in Nirmala.ttf
अ (0x0905) [uni0905] 'Nirmala UI Semilight' in NirmalaS.ttf
अ (0x0905) [uni0905] 'Unifont' in unifont-8.0.01.ttf
अ (0x0905) [uni0905] 'Unifont CSUR' in unifont_csur-8.0.01.ttf

另一个例子:.\FontGlyphs.py 🐈

['C:\\WINDOWS\\Fonts', 'D:\\Downloads\\MathJax-TeX-fonts-otf']

🐈 0x1f408 CAT
🐈 (0x1f408) [u1F408] 'EmojiOne Color' in EmojiOneColor-SVGinOT.ttf
🐈 (0x1f408) [u1F408] 'Segoe UI Emoji' in seguiemj.ttf
🐈 (0x1f408) [u1F408] 'Segoe UI Symbol' in seguisym.ttf

仅供引用,我编写了类似的脚本来显示使用适当字体呈现的输出(字形)(使用默认浏览器...

限制 脚本无法识别Emoji Sequence , 例如

.\FontGlyphs.py 👍🏽

['C:\\WINDOWS\\Fonts', 'D:\\Downloads\\MathJax-TeX-fonts-otf']

👍 0x1f44d THUMBS UP SIGN
👍 (0x1f44d) [u1F44D] 'EmojiOne Color' in EmojiOneColor-SVGinOT.ttf
👍 (0x1f44d) [u1F44D] 'Segoe UI Emoji' in seguiemj.ttf
👍 (0x1f44d) [u1F44D] 'Segoe UI Symbol' in seguisym.ttf

🏽 0x1f3fd EMOJI MODIFIER FITZPATRICK TYPE-4
🏽 (0x1f3fd) [u1F3FD] 'EmojiOne Color' in EmojiOneColor-SVGinOT.ttf
🏽 (0x1f3fd) [u1F3FD] 'Segoe UI Emoji' in seguiemj.ttf
🏽 (0x1f3fd) [u1F3FD] 'Segoe UI Symbol' in seguisym.ttf

关于python - 如何判断一个字形是否可以显示?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/69261202/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com