Core Font Management

Core Font Management

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

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

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 count

Glyph Encoding

FontEncoding maps Unicode code points to glyph IDs.

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()

Tips and Best Practices

  • Cache loaded Font objects when processing the same font file multiple times
  • Use FontLoader.load(bytes) for network or database workflows to avoid temp files
  • Check num_glyphs before subsetting to estimate post-subset coverage
  • get_all_glyph_ids() is useful for iterating the full glyph set

Common Issues

IssueCauseFix
UnsupportedFontFormatExceptionFile format not recognizedVerify the file is a valid TTF/OTF/CFF/WOFF/WOFF2/EOT/Type1
FontParseExceptionMalformed binary dataValidate the font file with a linter before loading
GlyphNotFoundExceptionGID out of rangeCheck 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 / MethodDescription
FontLoader.open(path)Load a font from a file path
FontLoader.load(data)Load a font from raw bytes
Font.font_nameFull PostScript font name
Font.font_familyFont family name
Font.font_styleFont style name
Font.num_glyphsTotal 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