Introduction

Compact Font Format (CFF) is the binary glyph description format used inside OpenType fonts with a PostScript-compatible outline engine. Aspose.Font FOSS for Python exposes a dedicated set of classes under the aspose_font.cff namespace for reading and inspecting CFF data structures: CffFont, CffCharset, CffEncoding, CffDict, CffIndex, and Type2Interpreter. This post walks through how to load CFF fonts, navigate their internal tables, and interpret charstring programs using the library.

The package is MIT licensed. Install it with pip install aspose-font>=1.0.0.


Key Features

Loading a CFF Font

FontLoader.open() detects CFF format automatically — pass either a standalone .cff file or an OTF file that embeds a CFF outline table. The returned object is a CffFont (accessible via the cff_font property on TtfFont for OTF files).

from aspose_font.loader import FontLoader

# Load a standalone CFF file
font = FontLoader.open("OpenSans.cff")

# Load a standalone CFF file (auto-detected from format)
font = FontLoader.open("OpenSans.cff")
print(font.font_name, font.num_glyphs)

CFF Charset — Mapping Glyph IDs to Names

CffCharset maps glyph IDs (GIDs) to PostScript glyph names. Use standard() to get the standard ISO Adobe charset or from_reader() to parse a charset from binary CFF data. The name_for() method returns the PostScript name for a GID, and gid_for() performs the reverse lookup.

from aspose_font.cff.charset import CffCharset

charset = CffCharset.standard()
print(charset.name_for(1))    # ".notdef" or similar
print(charset.gid_for("A"))   # GID for the PostScript name "A"

CFF Encoding — Unicode-to-GID Mapping

CffEncoding handles the CFF encoding table, which maps character codes to glyph IDs. standard() returns the standard encoding, expert() returns the expert encoding, and from_reader() parses a custom encoding from CFF binary data. Use unicode_to_gid() to look up a GID by Unicode code point and get_all_codepoints() to enumerate all mapped code points.

from aspose_font.cff.encoding import CffEncoding

encoding = CffEncoding.standard()
gid = encoding.unicode_to_gid(0x41)  # Unicode for 'A'
all_codepoints = encoding.get_all_codepoints()
print(f"Standard encoding covers {len(all_codepoints)} code points")

CFF Dictionary Parsing

CFF fonts store top-level and private metadata in dictionaries. CffDict.from_bytes() parses a raw CFF DICT blob and gives you get() and set() access by operator key. TopDict.from_dict() specialises this for top-level CFF dictionaries, and PrivateDict.from_dict() does the same for private DICTs.

from aspose_font.cff.dict_ import CffDict

# Parse a raw CFF DICT from bytes
raw_dict_bytes = b"..."  # CFF-encoded DICT bytes
d = CffDict.from_bytes(raw_dict_bytes)
vals = d.get(21)  # retrieve values for operator 21 (PostScript CharstringType)

CFF Index Structures

CffIndex represents a CFF INDEX data structure — a length-prefixed array of variable- length byte strings used for charstrings, subrs, and name tables. from_reader() parses an INDEX from a binary reader and the result is subscriptable by integer index.

from aspose_font.cff.index import CffIndex
from aspose_font._io import BinaryReader  # internal API — may change between releases
import io

raw_index = b"..."  # raw CFF INDEX bytes
reader = BinaryReader(io.BytesIO(raw_index))
index = CffIndex.from_reader(reader)
print(f"INDEX entries: {len(index)}")

Type 2 Charstring Interpretation

Type2Interpreter executes a Type 2 charstring program (the compact PostScript-like format used in CFF fonts) and produces a GlyphPath of outline commands. Call interpret() with the charstring bytes and the associated private dictionary. This enables you to extract raw glyph outlines from CFF charstrings without a full PostScript interpreter.

from aspose_font.cff.type2 import Type2Interpreter

interpreter = Type2Interpreter()
glyph_path = interpreter.interpret(charstring_bytes, private_dict)
for cmd in glyph_path.commands:
    print(type(cmd).__name__, cmd)

Quick Start

pip install aspose-font>=1.0.0
from aspose_font.loader import FontLoader

# Load an OTF with embedded CFF outlines
font = FontLoader.open("OpenSans-Regular.otf")

glyph_id = font.encoding.unicode_to_gid(0x41)
glyph = font.glyph_accessor.get_glyph_by_id(glyph_id)
print(f"Path commands for 'A': {len(glyph.path.commands)}")

Supported Formats

FormatExtensionReadWrite
CFF.cff
OTF.otf
TTF.ttf
WOFF.woff
WOFF2.woff2
EOT.eot
TYPE1.pfb / .pfa

Open Source & Licensing

Aspose.Font FOSS for Python is MIT licensed. Source code is on GitHub at aspose-font-foss/Aspose.Font-FOSS-for-Python. Install from PyPI as aspose-font.


Getting Started