Introduction
Aspose.Font FOSS for Python is a new open-source library that gives Python developers a complete toolkit for working with font files programmatically. Whether you need to load a TTF for glyph inspection, convert an OTF to WOFF2 for web delivery, subset a font to reduce payload size, or generate animated previews of variable font axes, the library exposes a unified, dependency-free API for all these tasks.
The library is published on PyPI as aspose-font under the MIT license. It requires
Python 3.10 or later and has no dependency on OS-level font rendering libraries such as
FreeType or HarfBuzz. This makes it well-suited for headless server environments,
CI pipelines, and containerized workloads where installing system fonts is impractical.
Aspose.Font FOSS v1.0.0 supports loading fonts in TTF, OTF, CFF, Type 1, WOFF, WOFF2,
and EOT formats, with export capability to the same set of formats via FontConverter.
What’s Included
Font Loading and Metadata Access
The FontLoader class is the single entry point for loading fonts. Call FontLoader.open()
with a file path or raw bytes and the library will detect the format automatically and
return the appropriate font object. Once loaded, the Font base class gives you access
to font_name, font_family, font_style, num_glyphs, and the encoding map.
from aspose_font.loader import FontLoader
font = FontLoader.open("Roboto-Regular.ttf")
print(font.font_name) # "Roboto"
print(font.font_family) # "Roboto"
print(font.num_glyphs) # number of glyphs in the font
Glyph Inspection and Path Access
The GlyphAccessor class lets you look up glyphs by glyph ID or Unicode code point via
get_glyph_by_id() and get_glyph_by_unicode(). To resolve all glyphs for a string of
text at once, use get_glyphs_for_text(). Each Glyph object exposes its outline path
as a sequence of typed path commands — MoveTo, LineTo, QuadraticTo, CurveTo, and
ClosePath — giving you access to raw Bézier data without a rendering engine.
from aspose_font.loader import FontLoader
font = FontLoader.open("OpenSans-Regular.otf")
glyph_id = font.encoding.unicode_to_gid(0x41) # Unicode code point for 'A'
glyph = font.glyph_accessor.get_glyph_by_id(glyph_id)
for cmd in glyph.path.commands:
print(type(cmd).__name__, cmd)
Font Subsetting
FontSubsetter provides multiple subsetting strategies. Use subset_by_text() to
keep only the glyphs needed for a specific string, subset_by_gids() to specify glyph
IDs directly, or subset_for_web() to apply a web-optimized preset that drops rarely
used glyphs and tables. The analyze_coverage() method lets you inspect what percentage
of the original font a subset covers before committing to it.
from aspose_font.loader import FontLoader
from aspose_font.subsetter import FontSubsetter
font = FontLoader.open("NotoSans-Regular.ttf")
# Subset to the characters needed for a Latin-script page
result = FontSubsetter.subset_by_text(font, "Hello, World!")
Web Font Optimization
FontCleaner.clean_for_web() strips DSIG, FFTM, and meta tables and removes Mac
platform name records from the font’s name table. The result is a smaller binary
that loads faster as a web font without changing the glyph outlines or metrics.
from aspose_font.loader import FontLoader
from aspose_font.cleaner import FontCleaner
font = FontLoader.open("Roboto-Regular.ttf")
cleaned = FontCleaner.clean_for_web(font)
# stripped of legacy tables; ready for WOFF2 packaging
Variable Font Animation Previews
For variable fonts, AnimationPreviewBuilder can generate an animated APNG that sweeps
one axis from a start value to an end value. The build_axis_sweep() method accepts
the axis tag (e.g., "wdth" or "wght"), a frame count, frames per second, and an
optional bounce flag that reverses the sweep to create a looping animation. The result
is an AnimationAsset whose write_to() method saves the APNG to disk.
from aspose_font.loader import FontLoader
from aspose_font.animation import AnimationPreviewBuilder
font = FontLoader.open("Roboto-VariableFont_wdth,wght.ttf")
asset = AnimationPreviewBuilder.build_axis_sweep(
font,
axis_tag="wdth",
start_val=75.0,
end_val=100.0,
frames=3,
fps=10,
text="A",
size=10.0,
bounce=True,
)
asset.write_to("roboto-sweep-wdth.png")
Quick Start
Install the library from PyPI:
pip install aspose-font>=1.0.0
Then load a font and inspect its metadata in a few lines:
from aspose_font.loader import FontLoader
font = FontLoader.open("MyFont.ttf")
print(f"Family: {font.font_family}")
print(f"Style: {font.font_style}")
print(f"Glyphs: {font.num_glyphs}")
# Look up a glyph by Unicode code point
glyph_id = font.encoding.unicode_to_gid(ord("A"))
glyph = font.glyph_accessor.get_glyph_by_id(glyph_id)
print(f"Path commands for 'A': {len(glyph.path.commands)}")
Supported Formats
| Format | Extension | Read | Write |
|---|---|---|---|
| TTF | .ttf | ✓ | ✓ |
| OTF | .otf | ✓ | ✓ |
| CFF | .cff | ✓ | ✓ |
| TYPE1 | .pfb / .pfa | ✓ | ✓ |
| WOFF | .woff | ✓ | ✓ |
| WOFF2 | .woff2 | ✓ | ✓ |
| EOT | .eot | ✓ | ✓ |
Open Source & Licensing
Aspose.Font FOSS for Python is published under the MIT license. You can use it in
commercial projects, modify it, and distribute it freely. The source code is available
on GitHub at aspose-font-foss/Aspose.Font-FOSS-for-Python
and the package is available on PyPI as aspose-font.