Einleitung
Aspose.Font FOSS für Python bietet ein konsistentes Set an Klassen für jede gängige Schriftartenverwaltungsaufgabe: Laden von Schriftdateien in mehreren Formaten, Auslesen von Metadaten und Glyphendaten, Konvertieren zwischen Formaten, Subsetting für die Webauslieferung und Bereinigen binärer Tabellen für optimale Dateigröße. Dieser Leitfaden führt durch jede Kernfunktionalität mit funktionierenden Code-Beispielen aus den verifizierten API der Bibliothek.
Das Paket (aspose-font) ist unter der MIT-Lizenz veröffentlicht, benötigt Python3.10+ und hat keine Betriebssystem-abhängigen Schriftbibliotheksabhängigkeiten. Installieren Sie es mit pip install aspose-font>=1.0.0.
Hauptmerkmale
Schriften laden mit FontLoader
FontLoader ist der einzige Einstiegspunkt für alle Schrift-Ladeoperationen. Die statische Methode open() akzeptiert einen Dateipfad (str oder Path) und erkennt das Format automatisch. Die Methode load() akzeptiert rohe Bytes für In-Memory-Workflows. Beide geben ein Objekt vom Typ Font zurück, das Sie sofort inspizieren können.
from aspose_font.loader import FontLoader
# Load from file path — format detected automatically
font = FontLoader.open("Roboto-Regular.ttf")
# Load from raw bytes (e.g., from a network request or database blob)
with open("OpenSans-Regular.otf", "rb") as f:
font_bytes = f.read()
font = FontLoader.load(font_bytes)
Lesen von Schrift-Metadaten über die Font-Basis-Klasse
Nach dem Laden stellt die Basisklasse Font Standard-Metadaten-Eigenschaften bereit, ohne dass Sie Binär-Font-Tabellen parsen müssen. Die Eigenschaften font_name, font_family, font_style und num_glyphs decken die häufigsten Metadaten-Bedürfnisse ab.
from aspose_font.loader import FontLoader
font = FontLoader.open("NotoSans-Regular.ttf")
print(f"Name: {font.font_name}")
print(f"Family: {font.font_family}")
print(f"Style: {font.font_style}")
print(f"Glyphs: {font.num_glyphs}")
Glyph-Kodierung und Zugriff
FontEncoding ordnet Unicode-Codepoints Glyph-IDs zu. Verwenden Sie unicode_to_gid() für einzelne Codepoints oder get_all_codepoints(), um die gesamte Abdeckung der Schrift zu enumerieren. GlyphAccessor ruft dann Glyph-Objekte nach ID über get_glyph_by_id(), nach Unicode über get_glyph_by_unicode() oder für einen Textstring über get_glyphs_for_text() ab.
from aspose_font.loader import FontLoader
font = FontLoader.open("OpenSans-Regular.otf")
# Map Unicode code point to glyph ID
glyph_id = font.encoding.unicode_to_gid(0x41) # 'A'
print(f"Glyph ID for 'A': {glyph_id}")
# Retrieve glyph object
glyph = font.glyph_accessor.get_glyph_by_id(glyph_id)
# Inspect path commands
for cmd in glyph.path.commands:
print(type(cmd).__name__, cmd)
Font-Konvertierung mit FontConverter
FontConverter.convert() konvertiert eine geladene Schrift zwischen unterstützten Formaten. Übergeben Sie die Quellschrift und den Ziel-Format-Identifikator, um ein neues Schrift-Objekt im Zielformat zu erhalten. Die Methode unterstützt CFF, TTF, WOFF, WOFF2, EOT und Type1 sowohl als Quelle als auch als Ziel.
from aspose_font.loader import FontLoader
from aspose_font.converter import FontConverter
source = FontLoader.open("MyFont.ttf")
converted = FontConverter.convert(source, target_format="woff2")
Web-Optimierung mit FontCleaner
FontCleaner.clean_for_web() entfernt binäre Tabellen, die für die Web-Font-Auslieferung nicht nötig sind — insbesondere DSIG, FFTM und meta — und streicht Mac-Plattform (platform_id=1) Namens-Einträge aus der Namens-Tabelle. Das Ergebnis ist ein schlankeres Binär-File, das die Netzwerk-Last reduziert, ohne Konturen oder Metriken zu verändern. Optionale Schlüsselwort-Argumente ermöglichen es, bei Bedarf bestimmte Tabellentypen zu erhalten.
from aspose_font.loader import FontLoader
from aspose_font.cleaner import FontCleaner
font = FontLoader.open("Roboto-Regular.ttf")
# Strip all legacy tables and Mac name records (default)
cleaned = FontCleaner.clean_for_web(font)
# Preserve specific metadata tables if required
cleaned_selective = FontCleaner.clean_for_web(
font,
drop_metadata_tables=False,
drop_legacy_tables=True,
drop_mac_names=True,
)
Glyph-Pfad-Inspektion
Jedes Glyph-Objekt stellt seine Kontur als GlyphPath bereit, deren commands-Sequenz typisierte Instanzen von MoveTo, LineTo, QuadraticTo, CurveTo und ClosePath enthält. Jeder Befehl enthält die entsprechenden Koordinatenattribute— zum Beispiel hat CurveTo x1, y1, x2, y2, x und y— und ermöglicht das Extrahieren roher Bézier-Daten für Rendering, SVG-Generierung oder Kollisionsgeometrie.
from aspose_font.loader import FontLoader
from aspose_font import CurveTo, LineTo
font = FontLoader.open("OpenSans-Regular.otf")
glyph_id = font.encoding.unicode_to_gid(ord("A"))
glyph = font.glyph_accessor.get_glyph_by_id(glyph_id)
curves = [cmd for cmd in glyph.path.commands if isinstance(cmd, CurveTo)]
print(f"Cubic Bezier segments in 'A': {len(curves)}")
Schnellstart
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
from aspose_font.cleaner import FontCleaner
# Load, inspect, and clean a font in five lines
font = FontLoader.open("MyFont.ttf")
print(font.font_family, font.num_glyphs, "glyphs")
cleaned = FontCleaner.clean_for_web(font)
print("Font cleaned for web delivery")
Unterstützte Formate
| Format | Erweiterung | Lesen | Schreiben |
|---|---|---|---|
| TTF | .ttf | ✓ | ✓ |
| OTF | .otf | ✓ | ✓ |
| CFF | .cff | ✓ | ✓ |
| TYPE1 | .pfb / .pfa | ✓ | ✓ |
| WOFF | .woff | ✓ | ✓ |
| WOFF2 | .woff2 | ✓ | ✓ |
| EOT | .eot | ✓ | ✓ |
Open Source & Lizenzierung
Aspose.Font FOSS für Python ist unter der MIT-Lizenz verfügbar. Der Quellcode befindet sich auf GitHub unter aspose-font-foss/Aspose.Font-FOSS-for-Python und das Paket ist auf PyPI als aspose-font.