Introduzione
Compact Font Format (CFF) è il formato binario di descrizione dei glifi utilizzato all’interno dei font OpenType con un motore di contorno compatibile con PostScript. Aspose.Font FOSS per Python espone un set dedicato di classi nello spazio dei nomi aspose_font.cff per leggere e ispezionare le strutture dati CFF: CffFont, CffCharset, CffEncoding, CffDict, CffIndex e Type2Interpreter. Questo post illustra come caricare i font CFF, navigare le loro tabelle interne e interpretare i programmi charstring usando la libreria.
Il pacchetto è rilasciato sotto licenza MIT. Installalo con pip install aspose-font>=1.0.0.
Caratteristiche principali
Caricamento di un font CFF
FontLoader.open() rileva automaticamente il formato CFF — fornisci un file .cff autonomo oppure un file OTF che incorpora una tabella di contorno CFF. L’oggetto restituito è un CffFont (accessibile tramite la proprietà cff_font su TtfFont per i file OTF).
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)
Charset CFF — Mappatura degli ID dei glifi ai nomi
CffCharset mappa gli ID glifo (GIDs) ai nomi glifo PostScript. Usa standard() per ottenere il set di caratteri ISO Adobe standard o from_reader() per analizzare un set di caratteri dai dati binari CFF. Il metodo name_for() restituisce il nome PostScript per un GID, e gid_for() esegue la ricerca inversa.
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"
Codifica CFF — Mappatura Unicode-to-GID
CffEncoding gestisce la tabella di codifica CFF, che mappa i codici carattere agli ID glifo. standard() restituisce la codifica standard, expert() restituisce la codifica esperta, e from_reader() analizza una codifica personalizzata dai dati binari CFF. Usa unicode_to_gid() per cercare un GID mediante punto di codice Unicode e get_all_codepoints() per enumerare tutti i punti di codice mappati.
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")
Analisi del Dizionario CFF
I font CFF memorizzano i metadati di livello superiore e privati nei dizionari. CffDict.from_bytes() analizza un blob DICT CFF grezzo e ti fornisce accesso get() e set() tramite chiave operatore. TopDict.from_dict() specializza questo per i dizionari CFF di livello superiore, e PrivateDict.from_dict() fa lo stesso per i DICT privati.
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)
Strutture di Indice CFF
CffIndex rappresenta una struttura dati CFF INDEX — un array prefissato in lunghezza di stringhe di byte di lunghezza variabile usate per charstrings, subrs e tabelle dei nomi. from_reader() analizza un INDEX da un lettore binario e il risultato è indicizzabile tramite indice intero.
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)}")
Interpretazione della Charstring di Tipo 2
Type2Interpreter esegue un programma charstring di Tipo 2 (il formato compatto simile a PostScript utilizzato nei font CFF) e produce un GlyphPath di comandi di contorno. Chiama interpret() con i byte della charstring e il dizionario privato associato. Questo ti consente di estrarre contorni grezzi dei glifi dalle charstring CFF senza un interprete completo di PostScript.
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)
Guida rapida
git clone https://github.com/aspose-font-foss/Aspose.Font-FOSS-for-Python.git
cd Aspose.Font-FOSS-for-Python
pip install -e .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)}")
Formati supportati
| Formato | Estensione | Leggi | Scrivi |
|---|---|---|---|
| CFF | .cff | ✓ | ✓ |
| OTF | .otf | ✓ | ✓ |
| TTF | .ttf | ✓ | ✓ |
| WOFF | .woff | ✓ | ✓ |
| WOFF2 | .woff2 | ✓ | ✓ |
| EOT | .eot | ✓ | ✓ |
| TYPE1 | .pfb / .pfa | ✓ | ✓ |
Open Source e Licenze
Aspose.Font FOSS per Python è con licenza MIT. Il codice sorgente è su GitHub a aspose-font-foss/Aspose.Font-FOSS-for-Python. Installa da PyPI come aspose-font.