Working with CFF Fonts
Working with CFF Fonts
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.
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.
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.
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 aspose_font.cff.index import CffIndex
from aspose_font._io import BinaryReader
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 |