字体表检查

字体表检查

本指南显示如何读取命名表属性,检索原始二进制表数据并将修改的表字节写回TrueType字体.

实体字体和开放式字符组成的二进制表,每个标识由4位标签. TtfTableSet 暴露了Python名字的常用表属性,并 TtfFont 提供: 所有表的原始字节访问.


阅读名字表

TtfTableSet 曝光头,hhea,maxp,OS2,名字,邮件,cmap,loca,hmtx和内核表.

from aspose_font.loader import FontLoader
from aspose_font.ttf.font import TtfFont

ttf_font: TtfFont = FontLoader.open("Roboto-Regular.ttf")
tables = ttf_font.ttf_tables

print("head table:", tables.head is not None)
print("kern table:", tables.kern is not None)

阅读原始表位

TtfFont.get_table_bytes(tag) 返回任何表的完整二进制内容,由其四个字符标签识别,允许低级检查或自定义解析.

ttf_font: TtfFont = FontLoader.open(“Roboto-Regular.ttf”) raw_name = ttf_font.get_table_bytes(“name”) print(f"name table: {len(raw_name)} bytes")


---

### 编写修改表字节

`TtfFont.set_table_bytes(tag, data)` 取代表的二进制内容,允许定制表修改,例如补丁GSUB规则或更新名称记录.

ttf_font: TtfFont = FontLoader.open("Roboto-Regular.ttf")
modified_bytes = b"..."  # your modified table
ttf_font.set_table_bytes("GSUB", modified_bytes)

提示和最佳实践

  • Check tables.kern is not None 在访问Kerning数据之前.
  • TtfTableSet.get_raw(tag) 及其他 set_raw(tag, data) 是一个称的 get_table_bytes / set_table_bytes
  • 使用情况 FontCleaner.clean_for_web() 而不是手动删除表来避免一致性问题.

API 参考概述

类/方法Description
TtfFont.ttf_tables访问命名表属性
TtfFont.get_table_bytes(tag)阅读原始二进制表数据
TtfFont.set_table_bytes(tag, data)编写已修改的表数据
TtfTableSet.get_raw(tag)获得_表_字节的别名
TtfTableSet.set_raw(tag, data)设置_表_字节的别名

See Also

 中文