Core Font Management
Core Font Management
This guide shows how to load fonts from file paths or byte arrays, read metadata properties, map Unicode code points to glyph IDs, retrieve glyphs, and work with variable font axes and named instances.
FontLoader is the primary entry point for all font loading operations. Once a font is
loaded, the Font base class provides access to metadata, encoding, glyph access, and
metrics without requiring binary table parsing.
Loading Fonts
FontLoader.open(source, font_type, collection_index) loads a font from a file path with automatic format detection. FontLoader.load(source, font_type, collection_index) accepts raw bytes for in-memory or network workflows.
from aspose_font.loader import FontLoader
# From a file path — format auto-detected
font = FontLoader.open("Roboto-Regular.ttf")
# From raw bytes — useful for in-memory or network workflows
with open("OpenSans-Regular.otf", "rb") as f:
font = FontLoader.load(f.read())Reading Font Metadata
After loading, the Font object exposes font_name, font_family, font_style, and num_glyphs properties for quick inspection without manual table parsing.
from aspose_font.loader import FontLoader
font = FontLoader.open("MyFont.ttf")
print(font.font_name) # full PostScript name
print(font.font_family) # family name
print(font.font_style) # style name e.g. "Regular", "Bold"
print(font.num_glyphs) # total glyph countGlyph Encoding
FontEncoding maps Unicode code points to glyph IDs. Use unicode_to_gid(cp) to resolve a single code point, or get_all_codepoints() to enumerate every mapped code point in the font.
from aspose_font.loader import FontLoader
font = FontLoader.open("OpenSans-Regular.otf")
# Map a single code point
gid = font.encoding.unicode_to_gid(0x41) # GID for 'A'
# Enumerate all mapped code points
all_cps = font.encoding.get_all_codepoints()
print(f"Font covers {len(all_cps)} Unicode code points")Glyph Access
GlyphAccessor retrieves glyph objects by ID, Unicode, or text string.
from aspose_font.loader import FontLoader
font = FontLoader.open("OpenSans-Regular.otf")
glyph = font.glyph_accessor.get_glyph_by_id(gid)
glyph_a = font.glyph_accessor.get_glyph_by_unicode(0x41)
glyphs = font.glyph_accessor.get_glyphs_for_text("Hello")
all_ids = font.glyph_accessor.get_all_glyph_ids()Variable Fonts
For variable fonts (those containing an fvar table), TtfFont exposes axis
definitions and named instances directly.
from aspose_font.loader import FontLoader
from aspose_font.ttf.font import TtfFont
loaded = FontLoader.load("Roboto-VariableFont_wdth,wght.ttf")
ttf: TtfFont = loaded.font
# Check if the font is variable
if ttf.is_variable:
# List all variation axes
for axis in ttf.variable_axes:
print(f"{axis.tag} range: {axis.min_value}..{axis.max_value} default: {axis.default_value}")
# Look up a single axis by tag
wght = ttf.get_axis("wght")
if wght:
print(f"Weight axis: {wght.min_value}..{wght.max_value}")
# Enumerate named instances (e.g. Bold, Condensed)
for inst in ttf.named_instances:
print(f"Instance: name_id={inst.name_id} coordinates={inst.coordinates}")For higher-level instancing workflows, SmartInstancer wraps axis enumeration and
named instance resolution in a single object:
from aspose_font.loader import FontLoader
from aspose_font.instancing import SmartInstancer
loaded = FontLoader.load("Roboto-VariableFont_wdth,wght.ttf")
instancer = SmartInstancer(loaded.font)
print(f"Axes: {[a.tag for a in instancer.axes]}")
print(f"Named instances: {len(instancer.named_instances)}")Tips and Best Practices
- Cache loaded
Fontobjects when processing the same font file multiple times - Use
FontLoader.load(bytes)for network or database workflows to avoid temp files - Check
num_glyphsbefore subsetting to estimate post-subset coverage get_all_glyph_ids()is useful for iterating the full glyph set
Common Issues
| Issue | Cause | Fix |
|---|---|---|
UnsupportedFontFormatException | File format not recognized | Verify the file is a valid TTF/OTF/CFF/WOFF/WOFF2/EOT/Type1 |
FontParseException | Malformed binary data | Validate the font file with a linter before loading |
GlyphNotFoundException | GID out of range | Check num_glyphs before accessing by ID |
FAQ
Does FontLoader support WOFF2?
Yes. FontLoader.open() and FontLoader.load() both accept WOFF2 files and auto-detect
the format.
Can I load a font from a URL?
Not directly. Download the font bytes with urllib or requests and pass them to
FontLoader.load(bytes).
Is loading thread-safe?
Each FontLoader.open() call returns an independent Font object. Concurrent calls
on separate objects are safe.
API Reference Summary
| Class / Method | Description |
|---|---|
FontLoader.open(path) | Load a font from a file path |
FontLoader.load(data) | Load a font from raw bytes |
Font.font_name | Full PostScript font name |
Font.font_family | Font family name |
Font.font_style | Font style name |
Font.num_glyphs | Total glyph count |
FontEncoding.unicode_to_gid(cp) | Map Unicode code point to glyph ID |
FontEncoding.get_all_codepoints() | All Unicode code points with glyph mappings |
GlyphAccessor.get_glyph_by_id(gid) | Retrieve glyph by ID |
GlyphAccessor.get_glyph_by_unicode(cp) | Retrieve glyph by Unicode code point |
GlyphAccessor.get_glyphs_for_text(text) | Retrieve glyphs for a text string |
GlyphAccessor.get_all_glyph_ids() | List all glyph IDs in the font |
TtfFont.is_variable | Whether the font contains variable font data |
TtfFont.variable_axes | List of VariableAxis objects for each variation axis |
TtfFont.get_axis(tag) | Look up a single axis by its four-character tag |
TtfFont.named_instances | Named instances defined in the font’s fvar table |
SmartInstancer(font) | High-level helper wrapping axis and instance queries |