Working with CFF Fonts
Working with CFF Fonts
This guide shows how to load and inspect CFF font internals — charsets, encoding tables, dictionaries, and index structures — using Aspose.Font FOSS for Python.
Compact Font Format (CFF) fonts store glyph outlines as Type 2 charstring programs.
Aspose.Font FOSS for Python provides dedicated classes in the aspose_font.cff namespace
for reading CFF charsets, encoding tables, dictionaries, and indexes.
CFF Charset
CffCharset maps glyph IDs to PostScript glyph names. Call CffCharset.standard() to obtain the built-in ISO Adobe charset, then use name_for() and gid_for() to translate between glyph IDs and names.
from aspose_font.cff.charset import CffCharset
charset = CffCharset.standard()
name = charset.name_for(1) # PostScript name for GID 1
gid = charset.gid_for("A") # GID for PostScript name "A"CFF Encoding
CffEncoding maps character codes to glyph IDs. The standard encoding covers the Adobe Latin character set; use unicode_to_gid() to look up individual code points and get_all_codepoints() to list every mapped value.
from aspose_font.cff.encoding import CffEncoding
enc = CffEncoding.standard()
gid = enc.unicode_to_gid(0x41)
codepoints = enc.get_all_codepoints()CFF Dictionary Parsing
CffDict.from_bytes() parses a raw CFF DICT blob into an operator-keyed mapping. Each operator key maps to a list of numeric values that configure the font, such as default width or subroutine offsets.
from aspose_font.cff.dict_ import CffDict
d = CffDict.from_bytes(raw_bytes)
vals = d.get(21) # retrieve values for operator 21CFF Index
CffIndex.from_reader() parses a CFF INDEX data structure from a binary stream. INDEX structures are arrays of variable-length data used for name tables, string tables, and charstring storage within CFF fonts.
from aspose_font.cff.index import CffIndex
from aspose_font._io import BinaryReader # internal API — may change between releases
import io
reader = BinaryReader(io.BytesIO(raw_index_bytes))
index = CffIndex.from_reader(reader)
print(f"INDEX entries: {len(index)}")Type 2 Charstring Interpretation
Type2Interpreter.interpret() executes a Type 2 charstring and returns a GlyphPath.
from aspose_font.cff.type2 import Type2Interpreter
interpreter = Type2Interpreter()
glyph_path = interpreter.interpret(charstring_bytes, private_dict)Tips and Best Practices
- Use
CffEncoding.standard()as a baseline; override withfrom_reader()for embedded encodings CffDict.to_bytes()lets you re-serialise modified dictionaries- Charstring interpretation is needed only for raw glyph outline extraction;
GlyphAccessorcovers most use cases at a higher level
API Reference Summary
| Class / Method | Description |
|---|---|
CffCharset.standard() | Standard ISO Adobe charset |
CffCharset.name_for(gid) | PostScript name for a glyph ID |
CffCharset.gid_for(name) | Glyph ID for a PostScript name |
CffEncoding.standard() | Standard CFF encoding table |
CffEncoding.unicode_to_gid(cp) | Map code point to GID |
CffDict.from_bytes(data) | Parse a raw CFF DICT |
CffDict.get(operator) | Get values for a DICT operator |
CffIndex.from_reader(reader) | Parse a CFF INDEX |
Type2Interpreter.interpret(cs, pd) | Execute a Type 2 charstring |