使用 CFF 字体

使用 CFF 字体

本指南展示了如何使用 Aspose.Font FOSS for Python 加载和检查 CFF 字体内部——字符集、编码表、字典和索引结构——。

紧凑字体格式(CFF)字体将字形轮廓存储为 Type 2 charstring 程序。Aspose.Font FOSS for Python 在 aspose_font.cff 命名空间中提供了专用类,用于读取 CFF 字符集、编码表、字典和索引。


CFF 字符集

CffCharset 将字形 ID 映射到 PostScript 字形名称。调用 CffCharset.standard() 获取内置的 ISO Adobe 字符集,然后使用 name_for() 和 gid_for() 在字形 ID 与名称之间进行转换。

from aspose_font.cff import CffCharset

charset = CffCharset.standard()
name = charset.name_for(1)   # PostScript name for GID 1
gid  = charset.gid_for("A")  # GID for PostScript name "A"

CFF 编码

CffEncoding 将字符码映射到字形 ID。标准编码覆盖 Adobe Latin 字符集;使用 unicode_to_gid() 查找单个代码点,使用 get_all_codepoints() 列出所有映射值。

from aspose_font.cff import CffEncoding

enc = CffEncoding.standard()
gid = enc.unicode_to_gid(0x41)
codepoints = enc.get_all_codepoints()

CFF 字典解析

CffDict.from_bytes() 将原始 CFF DICT blob 解析为以运算符为键的映射。每个运算符键映射到一个数值列表,用于配置字体,例如默认宽度或子例程偏移量。

from aspose_font.cff import CffDict

d = CffDict.from_bytes(raw_bytes)
vals = d.get(21)   # retrieve values for operator 21

CFF 索引

CffIndex.from_reader() 从二进制流中解析 CFF INDEX 数据结构。INDEX 结构是可变长度数据的数组,用于 CFF 字体中的名称表、字符串表和字符串存储。

from aspose_font.cff import CffIndex
from aspose_font._io import BinaryReader  # internal API — may change between releases
import io

reader = BinaryReader(io.BytesIO(raw_index_bytes))
index = CffIndex.from_reader(reader)
print(f"INDEX entries: {len(index)}")

Type 2 Charstring 解释

Type2Interpreter.interpret() 执行一个 Type 2 charstring 并返回一个 GlyphPath。

from aspose_font.cff import Type2Interpreter

interpreter = Type2Interpreter()
glyph_path = interpreter.interpret(charstring_bytes, private_dict)

提示与最佳实践

  • 将 CffEncoding.standard() 用作基准;对于嵌入式编码,请使用 from_reader() 覆盖。
  • CffDict.to_bytes() 让您能够重新序列化已修改的字典
  • 字符串解释仅在提取原始字形轮廓时才需要;GlyphAccessor 在更高层面上覆盖了大多数使用场景

API Reference 摘要

类 / 方法描述
CffCharset.standard()标准 ISO Adobe 字符集
CffCharset.name_for(gid)PostScript 名称对应字形 ID
CffCharset.gid_for(name)用于 PostScript 名称的字形 ID
CffEncoding.standard()标准 CFF 编码表
CffEncoding.unicode_to_gid(cp)将代码点映射到 GID
CffDict.from_bytes(data)解析原始 CFF DICT
CffDict.get(operator)获取 DICT 操作符的值
CffIndex.from_reader(reader)解析 CFF INDEX
Type2Interpreter.interpret(cs, pd)执行 Type 2 charstring

另请参阅

 中文