Introduction

Aspose.Font FOSS for Python provides a coherent set of classes for every common font management task: loading font files in multiple formats, reading metadata and glyph data, converting between formats, subsetting for web delivery, and cleaning binary tables for optimal file size. This guide walks through each core capability with working code examples drawn from the library’s verified API.

The package (aspose-font) is MIT licensed, requires Python 3.10+, and has no OS-level font library dependencies. Install it with pip install aspose-font>=1.0.0.


Key Features

Loading Fonts with FontLoader

FontLoader is the single entry point for all font loading operations. The static open() method accepts a file path (str or Path) and auto-detects the format. The load() method accepts raw bytes for in-memory workflows. Both return a Font-typed object you can inspect immediately.

from aspose_font.loader import FontLoader

# Load from file path — format detected automatically
font = FontLoader.open("Roboto-Regular.ttf")

# Load from raw bytes (e.g., from a network request or database blob)
with open("OpenSans-Regular.otf", "rb") as f:
    font_bytes = f.read()
font = FontLoader.load(font_bytes)

Reading Font Metadata via the Font Base Class

Once loaded, the Font base class exposes standard metadata properties without requiring you to parse binary font tables. The properties font_name, font_family, font_style, and num_glyphs cover the most common metadata needs.

from aspose_font.loader import FontLoader

font = FontLoader.open("NotoSans-Regular.ttf")
print(f"Name:   {font.font_name}")
print(f"Family: {font.font_family}")
print(f"Style:  {font.font_style}")
print(f"Glyphs: {font.num_glyphs}")

Glyph Encoding and Access

FontEncoding maps Unicode code points to glyph IDs. Use unicode_to_gid() for single code points or get_all_codepoints() to enumerate the full coverage of the font. GlyphAccessor then retrieves glyph objects by ID via get_glyph_by_id(), by Unicode via get_glyph_by_unicode(), or for a text string via get_glyphs_for_text().

from aspose_font.loader import FontLoader

font = FontLoader.open("OpenSans-Regular.otf")

# Map Unicode code point to glyph ID
glyph_id = font.encoding.unicode_to_gid(0x41)  # 'A'
print(f"Glyph ID for 'A': {glyph_id}")

# Retrieve glyph object
glyph = font.glyph_accessor.get_glyph_by_id(glyph_id)

# Inspect path commands
for cmd in glyph.path.commands:
    print(type(cmd).__name__, cmd)

Font Conversion with FontConverter

FontConverter.convert() converts a loaded font between supported formats. Pass the source font and the target format identifier to get a new font object in the target format. The method handles CFF, TTF, WOFF, WOFF2, EOT, and Type 1 as both source and target.

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

source = FontLoader.open("MyFont.ttf")
converted = FontConverter.convert(source, target_format="woff2")

Web Optimization with FontCleaner

FontCleaner.clean_for_web() removes binary tables that are unnecessary for web font delivery — specifically DSIG, FFTM, and meta — and strips Mac platform (platform_id=1) name records from the name table. The result is a leaner binary that reduces network payload without touching outlines or metrics. Optional keyword arguments let you preserve specific table types if needed.

from aspose_font.loader import FontLoader
from aspose_font.cleaner import FontCleaner

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

# Strip all legacy tables and Mac name records (default)
cleaned = FontCleaner.clean_for_web(font)

# Preserve specific metadata tables if required
cleaned_selective = FontCleaner.clean_for_web(
    font,
    drop_metadata_tables=False,
    drop_legacy_tables=True,
    drop_mac_names=True,
)

Glyph Path Inspection

Every Glyph object exposes its outline as a GlyphPath whose commands sequence contains typed instances of MoveTo, LineTo, QuadraticTo, CurveTo, and ClosePath. Each command carries the relevant coordinate attributes — for example, CurveTo has x1, y1, x2, y2, x, y — allowing you to extract raw Bézier data for rendering, SVG generation, or collision geometry.

from aspose_font.loader import FontLoader
from aspose_font import CurveTo, LineTo

font = FontLoader.open("OpenSans-Regular.otf")
glyph_id = font.encoding.unicode_to_gid(ord("A"))
glyph = font.glyph_accessor.get_glyph_by_id(glyph_id)

curves = [cmd for cmd in glyph.path.commands if isinstance(cmd, CurveTo)]
print(f"Cubic Bezier segments in 'A': {len(curves)}")

Quick Start

pip install aspose-font>=1.0.0
from aspose_font.loader import FontLoader
from aspose_font.cleaner import FontCleaner

# Load, inspect, and clean a font in five lines
font = FontLoader.open("MyFont.ttf")
print(font.font_family, font.num_glyphs, "glyphs")
cleaned = FontCleaner.clean_for_web(font)
print("Font cleaned for web delivery")

Supported Formats

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

Open Source & Licensing

Aspose.Font FOSS for Python is MIT licensed. The source is on GitHub at aspose-font-foss/Aspose.Font-FOSS-for-Python and the package is on PyPI as aspose-font.


Getting Started