在Python中如何使用字体表?
本指南显示如何读取命名的 TTF 表,检索原始二进制表字节,并使用编写修改后的表数据. TtfFont 在Aspose.Font FOSS for Python中.TrueType字体存储所有数据在由4个符号标签识别的二进制表中, Aspose.Font 曝光了FOS S TtfTableSet 对于命名表访问,以及 TtfFont.get_table_bytes() / TtfFont.set_table_bytes() 对于原始二元访问.
步骤指南
步骤1:安装包装
使用下面的 pip 命令从 PyPI 中安装 Aspose.Font FOSS 包:
pip install "aspose-font>=1.0.0"步骤2:进口所需的类别
Import FontLoader 及其他 TtfFont 载入字体文件并访问其表组:
from aspose_font.loader import FontLoader
from aspose_font.ttf.font import TtfFont步骤3:访问命名表
载入一个TF文件. FontLoader.open() 通过该表的名称访问,并使用 ttf_tables 财产:
ttf_font: TtfFont = FontLoader.open("Roboto-Regular.ttf")
tables = ttf_font.ttf_tables
print("kern present:", tables.kern is not None)
print("cmap present:", tables.cmap is not None)步骤4:阅读原始表字节
通过传递其4字符标签给一个特定表的原始二进制数据阅读 get_table_bytes():
ttf_font: TtfFont = FontLoader.open("Roboto-Regular.ttf")
raw = ttf_font.get_table_bytes("name")
print(f"name table: {len(raw)} bytes")步骤5:写改动表字节
使用编写修改的二进制数据回到命名表中. set_table_bytes() 标签和字节有效载荷:
ttf_font: TtfFont = FontLoader.open("Roboto-Regular.ttf")
modified = b"..." # your modified binary data
ttf_font.set_table_bytes("GSUB", modified)常见问题和解决方案
AttributeError: Font has no attribute ttf_tables — ttf_tables 在线的 TtfFont, 不是基层. Font 类. 使用 ttf_font: TtfFont = FontLoader.open(...) 获得一个 打字参考.
get_table_bytes 返回空字节 标签必须有4个字符. (如果需要,请填上空格的贴纸. "kern" 没有 "KRN").
常见的提问
在 TtfTableSet 上显示了哪些命名表?
头部,hhea,maxp,OS2,名字,邮件,cmap,局域,hmtx和内核.
我可以在字体中添加一个新的表吗?
是的. 传递任何有效的4字节标签和字符数据到 TtfFont.set_table_bytes(tag, data).