How to Work with TTF Fonts in Python

How to Work with TTF Fonts in Python

This guide shows how to load TTF fonts, inspect TtfFont properties, read tables, instantiate variable fonts, and convert to other formats using Aspose.Font FOSS for Python. TrueType fonts (.ttf) are the most common font format. Aspose.Font FOSS loads them as TtfFont instances, giving access to tables, variable font axes, glyph data, and conversion utilities.

Step-by-Step Guide

Step 1: Install the Package

Install the Aspose.Font FOSS package from PyPI using the pip command below:

pip install "aspose-font>=1.0.0"

Step 2: Import Required Classes

Import FontLoader and TtfFont to load a TTF font and access its typed properties:

from aspose_font.loader import FontLoader
from aspose_font.ttf.font import TtfFont

Step 3: Load and Inspect a TTF Font

Load a TTF file using FontLoader.open() and read basic properties such as name, family, and glyph count:

ttf_font: TtfFont = FontLoader.open("Roboto-Regular.ttf")
print(ttf_font.font_name)
print(ttf_font.font_family)
print(ttf_font.num_glyphs)

Step 4: Read the kern Table

Access the ttf_tables property and check for the presence of the kern table by name:

ttf_font: TtfFont = FontLoader.open("Roboto-Regular.ttf")
kern = ttf_font.ttf_tables.kern
print("kern present:", kern is not None)

Step 5: Instantiate a Variable Font

Load a variable font file, check is_variable, and call instantiate() with an axis dictionary to produce a static instance:

from aspose_font.loader import FontLoader
from aspose_font.ttf.font import TtfFont

ttf_font: TtfFont = FontLoader.open("Roboto-VariableFont_wdth,wght.ttf")
if ttf_font.is_variable:
    static = ttf_font.instantiate({"wght": 700.0})
    print("Instantiated:", static.font_name)

Step 6: Convert to WOFF2

Convert a loaded TTF font to WOFF2 format by passing FontType.WOFF2 to FontConverter.convert():

from aspose_font.loader import FontLoader
from aspose_font.converter import FontConverter
from aspose_font import FontType

font = FontLoader.open("Roboto-Regular.ttf")
woff2 = FontConverter.convert(font, FontType.WOFF2)

Common Issues and Fixes

AttributeError: Font has no attribute ttf_tables — Use ttf_font: TtfFont = FontLoader.open(...). The ttf_tables property is on TtfFont, not the base Font.

is_variable is False on a variable font — Some variable fonts use a non-standard fvar table format. Check ttf_font.fvar is not None as an alternative.

instantiate() raises KeyError — The axis tag must be a registered 4-char tag (e.g. "wght", "wdth"). Check ttf_font.axes for available tags.

Frequently Asked Questions

What is the difference between TTF and OTF?

OTF and TTF share the same binary container (SFNT). OTF fonts typically use CFF outlines while TTF fonts use TrueType quadratic curves. Both are loaded by FontLoader.open().

Can I get kern pairs from a TTF font?

Yes. Use TtfFont.get_kern_pairs() to get a list of KernPair objects.

Does the library support font collections (.ttc)?

Yes. Use FontLoader.open(source, collection_index=N) to select a specific font from a TTC collection file.

See Also