How to Work with Core Font Management in Python

How to Work with Core Font Management in Python

Aspose.Font FOSS for Python provides FontLoader as the main entry point for loading fonts from file paths, byte arrays, or streams. The returned Font object exposes metadata such as name, family, style, glyph count, and encoding map.

Step-by-Step Guide

Step 1: Install the Package

pip install "aspose-font>=1.0.0"

Step 2: Import Required Classes

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

Step 3: Load a Font from a File Path

Call FontLoader.open() with the path to your font file. The method detects the format automatically and returns the appropriate Font subclass (TtfFont, CffFont, etc.):

font = FontLoader.open("Roboto-Regular.ttf")
print(font.font_name)
print(font.num_glyphs)

Step 4: Access Font Metrics

The Font.metrics property returns a FontMetrics object containing ascender, descender, line gap, cap height, and other typographic measurements for the font:

from aspose_font.loader import FontLoader

font = FontLoader.open("Roboto-Regular.ttf")
metrics = font.metrics
print(metrics)

Step 5: Convert to Another Format

Pass the loaded font to FontConverter.convert() with the desired FontType target. The method returns a new font object in the requested format without modifying the original:

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

FontType not found — Import FontType from the top-level aspose_font package: from aspose_font import FontType.

Font has no attribute font_name — Ensure you are using a Font instance returned by FontLoader.open(), not a raw bytes object.

Encoding errors on Windows paths — Pass paths as raw strings: r"C:\Fonts\MyFont.ttf".

Frequently Asked Questions

Which font formats does FontLoader support?

FontLoader.open() supports TTF, OTF, CFF, Type 1, WOFF, WOFF2, and EOT.

Can I load a font from bytes?

Yes. FontLoader.open() accepts a file path string, bytes, or a stream object via the source argument along with an explicit font_type parameter.

Is Font.to_bytes() available?

No. Font.to_bytes() is not implemented in the base class. Use FontConverter.convert() to change format, then use the subclass save methods.

See Also