Introduction

TrueType fonts (.ttf) and their OpenType superset (.otf) are the dominant font formats on modern operating systems. Both formats share the same binary table structure rooted in the OpenType specification. Aspose.Font FOSS for Python exposes this structure through TtfFont, giving direct access to individual OpenType tables, kern pair data, variable font axes, and glyph outlines.

The TtfFont class extends the Font base class with TTF-specific methods including get_blob, get_table_bytes, set_table_bytes, and instantiate for variable font axis locking. The TtfTableSet property (ttf_tables) provides typed access to the most common OpenType tables: head, hhea, maxp, os2, name, post.

The library is published on PyPI as aspose-font and licensed under the MIT License. It has no mandatory commercial dependencies.


Key Features

Loading TTF and OTF Fonts

Use FontLoader.open to load any TrueType or OpenType font file:

from aspose_font import FontLoader

loaded = FontLoader.open("fonts/Roboto-Regular.ttf")

print(loaded.font_name)
print(loaded.font_family)
print(loaded.font_style)
print(loaded.num_glyphs)

FontLoader.open detects the font format from the file header. Both .ttf (TrueType) and .otf (OpenType CFF) files are supported and both return a TtfFont instance.


Accessing OpenType Tables via TtfTableSet

The ttf_tables property returns a TtfTableSet with typed accessors for common OpenType tables. Each property returns the table object or None if absent:

The ttf_tables property on TtfFont returns a TtfTableSet instance. Available table accessors:

  • headHeadTable | None
  • hheaHheaTable | None
  • maxpMaxpTable | None
  • os2Os2Table | None
  • nameNameTable | None
  • postPostTable | None

Each property returns the table object or None if that table is not present in the font file. Access TtfTableSet properties to check for specific tables before reading them. For tables not covered by named accessors, use TtfTableSet.get_raw(tag) to retrieve raw bytes by four-character OpenType tag.


Reading Raw Table Bytes

get_table_bytes(tag) returns the raw bytes for any OpenType table. Use get_blob(tag) for a None-safe variant that returns None when the table is absent:

TtfFont provides two methods for raw table access:

  • get_table_bytes(tag) -> bytes — returns the raw bytes for a named table (raises if absent)
  • get_blob(tag) -> bytes | None — returns None if the table is absent (safe variant)

Both methods accept a four-character OpenType table tag such as "cmap", "GDEF", or "GPOS". Use get_blob when a table is optional so you can check for None before processing.


Kern Pairs

get_kern_pairs() returns all kern pairs encoded in the font. Each KernPair has left (GlyphId), right (GlyphId), and value (int in font units):

from aspose_font import FontLoader

loaded = FontLoader.open("fonts/Roboto-Regular.ttf")

pairs = loaded.get_kern_pairs()
print(f"Kern pairs: {len(pairs)}")

for pair in pairs[:5]:
    print(f"  Left: {pair.left}  Right: {pair.right}  Value: {pair.value}")

Font Metrics

Standard metrics are accessible through the metrics property:

from aspose_font import FontLoader

loaded = FontLoader.open("fonts/Roboto-Regular.ttf")

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

Variable Font Support

For variable fonts, TtfFont exposes is_variable, variable_axes, and named_instances. Use get_axis(tag) to retrieve a specific axis by its four-letter tag:

from aspose_font import FontLoader

loaded = FontLoader.open("fonts/RobotoFlex.ttf")

print(f"Is variable: {loaded.is_variable}")
print(f"Axes       : {len(loaded.variable_axes)}")

The get_axis(tag) method on TtfFont retrieves a VariableAxis by its four-letter tag (e.g. "wght" for weight, "wdth" for width). It returns None if the axis is not present in the font’s fvar table.


Converting Fonts

to_bytes(font_type) converts the font to a different format. Use the FontType enum to specify the target:

from aspose_font import FontLoader, FontType

loaded = FontLoader.open("fonts/Roboto-Regular.ttf")

ttf_bytes = loaded.to_bytes(FontType.TTF)
with open("output/Roboto-copy.ttf", "wb") as fh:
    fh.write(ttf_bytes)

print(f"Serialized: {len(ttf_bytes)} bytes")

Quick Start

Install the library:

.venv/bin/pip install aspose-font

Load a TTF font and inspect it:

from aspose_font import FontLoader, FontType

loaded = FontLoader.open("Roboto-Regular.ttf")

print(f"Font name : {loaded.font_name}")
print(f"Family    : {loaded.font_family}")
print(f"Glyphs    : {loaded.num_glyphs}")
print(f"Variable  : {loaded.is_variable}")

metrics = loaded.metrics
print(f"UPM       : {metrics.units_per_em}")

ttf_bytes = loaded.to_bytes(FontType.TTF)
print(f"Serialized: {len(ttf_bytes)} bytes")

Supported Formats

FormatExtensionReadWrite
TTF.ttf
OTF.otf
EOT.eot
CFF.cff
TYPE1.pfb/.pfa
WOFF.woff
WOFF2.woff2

All formats load via FontLoader.open. TTF/OTF fonts return a TtfFont instance.


Open Source & Licensing

Aspose.Font FOSS for Python is released under the MIT License. You can use it in personal, commercial, and open-source projects at no cost. The source code is available on GitHub and the package is distributed via PyPI as aspose-font.


Getting Started