Working with TrueType Fonts

Working with TrueType Fonts

Working with TrueType Fonts

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.

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 accessing ttf_tables or get_table_bytes()
  • Use TtfTableSet.kern to check kerning availability before reading kern pairs
  • Variable font instancing produces a new font object; the original is unchanged

API Reference Summary

Class / MethodDescription
TtfFont.ttf_tablesAccess 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.kernKerning table (None if absent)