Working with TrueType Fonts
Working with TrueType Fonts
This guide shows how to load TrueType fonts, inspect their binary table data, read raw table bytes, and create static instances from variable fonts using TtfInstancer.
TrueType (TTF) fonts are the most common desktop and web font format. Aspose.Font FOSS
for Python exposes TtfFont for accessing TTF-specific table data, glyph blob retrieval,
and variable font axis control.
Loading a TTF Font
FontLoader.open() auto-detects TTF format and returns a TtfFont-typed object. The returned instance provides access to TTF-specific features such as ttf_tables, get_table_bytes(), and variable font axis data.
from aspose_font.loader import FontLoader
font = FontLoader.open("Roboto-Regular.ttf")
print(font.font_name, font.num_glyphs, "glyphs")Accessing TTF Tables
TtfFont.ttf_tables exposes a TtfTableSet with named properties for common tables.
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("kern table present:", tables.kern is not None)Raw Table Bytes
Use TtfFont.get_table_bytes(tag) to retrieve raw binary table data and
TtfFont.set_table_bytes(tag, data) to write modified table bytes.
from aspose_font.loader import FontLoader
from aspose_font.ttf.font import TtfFont
ttf_font: TtfFont = FontLoader.open("Roboto-Regular.ttf")
raw_cmap = ttf_font.get_table_bytes("cmap")Variable Font Instances
TtfFont.get_axis() returns axis information. TtfInstancer.instantiate() produces a
static instance from a variable font at specified axis coordinates.
from aspose_font.loader import FontLoader
from aspose_font.ttf.font import TtfFont
from aspose_font.ttf.instancer import TtfInstancer
ttf_font: TtfFont = FontLoader.open("Roboto-VariableFont_wdth,wght.ttf")
# Get a static instance at wght=700, wdth=75
instance = TtfInstancer.instantiate(ttf_font, {"wght": 700.0, "wdth": 75.0})Tips and Best Practices
- Always use
isinstance(font, TtfFont)before accessingttf_tablesorget_table_bytes() - Use
TtfTableSet.kernto check kerning availability before reading kern pairs - Variable font instancing produces a new font object; the original is unchanged
API Reference Summary
| Class / Method | Description |
|---|---|
TtfFont.ttf_tables | Access to TTF table set |
TtfFont.get_table_bytes(tag) | Raw bytes for a named table |
TtfFont.set_table_bytes(tag, data) | Write modified table bytes |
TtfFont.get_axis() | Variable font axis information |
TtfInstancer.instantiate(font, coords) | Create a static instance |
TtfTableSet.kern | Kerning table (None if absent) |