介绍

嵌入式 OpenType(EOT)是一种紧凑的字体格式,最初用于在网页中嵌入 TrueType 字体。EOT 文件将 TTF 或 OTF 字体包装在一个固定大小的头部中,头部包含描述字体尺寸、版本、字符集和嵌入权限的元数据字段。虽然现代网页开发已基本转向 WOFF/WOFF2,但 EOT 文件仍在旧版文档和企业应用中流通。

Aspose.Font FOSS 为 Python 提供通过 EotFont 类及其伴随的 EotHeader 直接访问 EOT 字体内部的功能。您可以使用 FontLoader.open 加载 EOT 文件,读取头部字段,遍历内部的 TtfFont,并使用 Font.save 将结果序列化回字节或保存为文件。该库已在 PyPI 上以 aspose-font 的名称发布,并在 MIT 许可证下授权。 它没有强制性的商业依赖,并且可在标准 Python 3 运行时上运行。


关键特性

加载 EOT 字体

使用 FontLoader.open 从路径或字节对象加载 EOT 文件。当检测到 EOT 签名时,该方法返回一个 EotFont 实例:

from aspose_font import FontLoader, EotFont

font = FontLoader.open("fonts/sample.eot")
assert isinstance(font, EotFont)

print(font.font_name)     # e.g. "Roboto"
print(font.font_family)   # e.g. "Roboto"
print(font.num_glyphs)    # number of glyphs in the inner font

FontLoader.open 会读取文件扩展名和头部魔数以自动检测格式。对于常见的字体文件,无需提供 font_type 参数。


读取 EotHeader 元数据

EotFont 将头部字段直接公开为类的属性。这些属性对应于 EOT 二进制规范中定义的字段:

from aspose_font import FontLoader, EotFont, EotSerializer

# Load EOT — FontLoader returns an EotFont instance for .eot files
loaded = FontLoader.open("fonts/sample.eot")

# Serialize back to bytes to confirm the font loaded correctly
eot_bytes = EotSerializer.serialize(loaded)
print(f"EOT data loaded: {len(eot_bytes)} bytes")

# Base Font properties work on any loaded font
print("Font name:", loaded.font_name)
print("Glyphs   :", loaded.num_glyphs)

EotFont 类将以下头部元数据公开为属性:

  • eot_version(int)— EOT 格式版本(例如 0x00020002)
  • flags(int)— 嵌入权限标志
  • charset(int)— 字符集标识符(0 = ANSI,1 = DEFAULT)
  • italic(int)— 斜体标志(0 或 1)
  • weight(int)— 字体粗细(例如,Regular 为 400,Bold 为 700)
  • fs_type(int)— OS/2 fsType 嵌入位

使用 flags 和 fs_type 来确定字体是否允许子集化或嵌入,这在处理用于再分发流水线的字体时很重要。

访问内部 TtfFont

每个 EotFont 包装一个标准的 TrueType 字体。inner_font 属性返回 TtfFont 实例,允许您访问完整的 TrueType 表集合:

from aspose_font import FontLoader

loaded = FontLoader.open("fonts/sample.eot")

# Metrics are accessible directly from the loaded font
metrics = loaded.metrics
print("Units per em :", metrics.units_per_em)
print("Ascender     :", metrics.ascender)
print("Descender    :", metrics.descender)
print("Line gap     :", metrics.line_gap)

EotFont 上的 inner_font 属性返回包装的 TtfFont 实例,提供对完整的 TrueType 表集合、字距对和编码表的访问。


将 EOT 序列化为字节

EotSerializer.serialize 将任意 Font 对象转换为原始的 EOT 字节串:

from aspose_font import FontLoader, EotFont, EotSerializer

font = FontLoader.open("fonts/sample.eot")
assert isinstance(font, EotFont)

eot_bytes = EotSerializer.serialize(font)
with open("output/sample_copy.eot", "wb") as fh:
    fh.write(eot_bytes)

print(f"Serialized {len(eot_bytes)} bytes")

将 TTF 转换为 EOT

EotSerializer.serialize 接受任何 Font 实例,因此您可以加载 TTF 并直接将其序列化为 EOT 字节:

from aspose_font import FontLoader, EotFont, EotSerializer

ttf = FontLoader.open("fonts/Roboto-Regular.ttf")
eot_bytes = EotSerializer.serialize(ttf)

with open("output/Roboto-Regular.eot", "wb") as fh:
    fh.write(eot_bytes)

reloaded = FontLoader.open("output/Roboto-Regular.eot")
assert isinstance(reloaded, EotFont)
print("Converted and verified:", reloaded.font_name)

字体度量和字距对

EotFont 继承了完整的 Font 接口,因此可以直接使用度量和编码,而无需通过 inner_font:

from aspose_font import FontLoader

loaded = FontLoader.open("fonts/sample.eot")

metrics = loaded.metrics
print(f"Ascender : {metrics.ascender}")
print(f"Descender: {metrics.descender}")
print(f"Line gap : {metrics.line_gap}")
print(f"Underline: {metrics.underline_position}")

for pair in loaded.get_kern_pairs():
    print(f"  Kern: {pair}")

快速入门

安装库:

.venv/bin/pip install aspose-font

加载并检查 EOT 字体:

from aspose_font import FontLoader, EotFont, EotSerializer

loaded = FontLoader.open("sample.eot")

# Base font info
print(f"Font name : {loaded.font_name}")
print(f"Glyphs    : {loaded.num_glyphs}")

# Metrics
metrics = loaded.metrics
print(f"UPM       : {metrics.units_per_em}")
print(f"Ascender  : {metrics.ascender}")

# Serialize the loaded font back to EOT bytes
eot_bytes = EotSerializer.serialize(loaded)
print(f"Serialized: {len(eot_bytes)} bytes")

支持的格式

格式扩展名读取写入
TTF.ttf✓—
OTF.otf✓—
EOT.eot✓—
CFF.cff✓—
TYPE1.pfb/.pfa✓—
WOFF.woff✓—
WOFF2.woff2✓—

上述所有列出的格式均可通过 FontLoader.open 加载。将数据写入特定格式时,EOT 使用 Font.save 或 EotSerializer.serialize。


开源与许可

Aspose.Font FOSS for Python 在 MIT 许可证下发布。您可以在个人、商业和开源项目中免费使用它。源代码可在 GitHub 获取,软件包通过 PyPI 以 aspose-font 形式分发。


入门指南

相关资源