How to Work with CFF Fonts in Python

How to Work with CFF Fonts in Python

CFF (Compact Font Format) is the outline format used inside OpenType fonts and standalone .cff files. Aspose.Font FOSS represents CFF fonts as CffFont instances, which extend the base Font class and expose CFF-specific data.

Step-by-Step Guide

Step 1: Install the Package

pip install "aspose-font>=1.0.0"

Step 2: Import Required Classes

from aspose_font.loader import FontLoader
from aspose_font.cff.font import CffFont
from aspose_font import FontType

Step 3: Load a CFF Font

font = FontLoader.open("MyFont.cff")
print(font.font_name)
print(font.num_glyphs)

Step 4: Access Kern Pairs

from aspose_font.loader import FontLoader
from aspose_font.cff.font import CffFont

cff_font: CffFont = FontLoader.open("MyFont.cff")
kern_pairs = cff_font.get_kern_pairs()
print(f"kern pairs: {len(kern_pairs)}")

Step 5: Convert CFF to TTF

from aspose_font.loader import FontLoader
from aspose_font.converter import FontConverter
from aspose_font import FontType

font = FontLoader.open("MyFont.cff")
ttf = FontConverter.convert(font, FontType.TTF)

Common Issues and Fixes

CffFont has no attribute cff_fontcff_font is a property on TtfFont that returns embedded CFF data, not on the base Font class. For standalone CFF files use CffFont directly.

Empty kern pairs — Not all CFF fonts contain kern pair data. Check with len(cff_font.get_kern_pairs()) > 0 before iterating.

Frequently Asked Questions

What is the difference between CffFont and TtfFont?

CffFont represents standalone CFF files. TtfFont represents TTF/OTF files; an OTF may embed a CFF outline accessible via ttf_font.cff_font.

Can I convert a CFF font to WOFF2?

Yes. FontConverter.convert(font, FontType.WOFF2) works with any Font instance including CffFont.

See Also