python-3.x - Python fonttools:检查字体是否支持多代码点表情符号
问题描述
我正在尝试检查字体是否具有多代码点表情符号的字形,例如 Python 3.x 中的“♂️”、“”或“”。
对于像 "" 或 "" 这样的单个代码点表情符号,我可以使用 Python 通过以下代码验证它们的支持fonttols
:
from fontTools.ttLib import TTFont
def __isEmojiSupportedByFont(emoji: str) -> bool:
font = TTFont(r"C:\Windows\Fonts\seguiemj.ttf")
emojiCodepoint = ord(str) # Only works for single codepoint emoji
for table in font['cmap'].tables:
for char_code, glyph_name in table.cmap.items():
if char_code == emojiCodepoint:
return True
return False
我该如何为多代码点表情符号执行此操作,因为其中cmp
只有单个代码点表情符号?
解决方案
要检查多代码点表情符号,我必须“查询”GSUB
查找列表。
检查python中的字体是否支持表情符号的更简单方法是使用HarfBuzz或更准确的harfpy。
我想出的解决方案是:
from uharfbuzz import Face, Font, Buffer, ot_font_set_funcs, shape
def __isEmojiSupportedByFont(self, emoji: str) -> bool:
# Load font:
with open(r"C:\Windows\Fonts\seguiemj.ttf", 'rb') as fontfile:
self.fontdata = fontfile.read()
# Load font (has to be done for call):
face = Face(self.fontdata)
font = Font(face)
upem = face.upem
font.scale = (upem, upem)
ot_font_set_funcs(font)
# Create text buffer:
buf = Buffer()
buf.add_str(emoji)
buf.guess_segment_properties()
# Shape text:
features = {"kern": True, "liga": True}
shape(font, buf, features)
# Remove all variant selectors:
while len(infos) > 0 and infos[-1].codepoint == 3:
infos = infos[:-1]
# Filter empty:
if len(infos) <= 0:
return False
# Remove uncombined, ending with skin tone like "":
lastCp = infos[-1].codepoint
if lastCp == 1076 or lastCp == 1079 or lastCp == 1082 or lastCp == 1085 or lastCp == 1088:
return False
# If there is a code point 0 or 3 => Emoji not fully supported by font:
return all(info.codepoint != 0 and info.codepoint != 3 for info in infos)
推荐阅读
- regex - Google Apps 脚本表单 textValidationBuilder 无法识别正则表达式模式
- azure - Azure API for FHIR 使用 Generate Id 创建新资源
- javascript - 浏览器前后按钮无法正常工作
- html - 显示 flex 没有正确对齐
- asp.net-mvc - 使用 windows 和 Form base 身份验证实现 MVC 5 App
- jenkins - 运行 jenkinsfile 时 docker.sock 权限被拒绝
- excel - 多个 IF 语句从第 3 条语句开始不起作用
- powerbi - 我可以在卡、量规或 kpi 以外的其他东西上设置 PBI 警报吗?
- shell - 如何列出配置单元中存在的所有数据库中的所有表
- typescript - 来自特定函数参数的通用类型推断