Introduction
WOFF2 (Web Open Font Format 2) uses Brotli as its compression algorithm. Unlike WOFF 1 which uses zlib, WOFF2 applies Brotli to a pre-transformed glyph table representation, typically achieving 20-30% better compression than WOFF 1. This makes WOFF2 the preferred delivery format for web typography.
Aspose.Font FOSS for Python handles all Brotli operations internally through its
bundled BrotliEncoder and BrotliDecoder classes. No external brotli,
brotlicffi, or similar package is needed. The standard font conversion API
applies Brotli compression transparently when producing WOFF2 output.
The library is published on PyPI as aspose-font under the MIT License.
Key Features
Converting Fonts to WOFF2
The primary use of Brotli in Aspose.Font is automatic WOFF2 compression. Use
FontConverter.convert with FontType.WOFF2 to produce a Brotli-compressed
web font:
from aspose_font.loader import FontLoader
from aspose_font.converter import FontConverter
from aspose_font import FontType
from pathlib import Path
# Load any supported format
font = FontLoader.open("MyFont.ttf") # TTF, OTF, WOFF, EOT, CFF, TYPE1
# Convert to WOFF2 — Brotli applied internally
woff2 = FontConverter.convert(font, FontType.WOFF2)
output = woff2.to_bytes(FontType.WOFF2)
Path("MyFont.woff2").write_bytes(output)
print(f"WOFF2 output: {len(output)} bytes")
You can also convert from an existing WOFF 1 file to WOFF2 to upgrade compression:
from aspose_font.loader import FontLoader
from aspose_font.converter import FontConverter
from aspose_font import FontType
from pathlib import Path
woff = FontLoader.open("MyFont.woff")
woff2 = FontConverter.convert(woff, FontType.WOFF2)
Path("MyFont.woff2").write_bytes(woff2.to_bytes(FontType.WOFF2))
print("Upgraded WOFF 1 (zlib) -> WOFF2 (Brotli)")
Loading WOFF2 Fonts
FontLoader.open decompresses the Brotli-compressed glyph data automatically:
from aspose_font.loader import FontLoader
woff2 = FontLoader.open("MyFont.woff2")
print(f"Name : {woff2.font_name}")
print(f"Family : {woff2.font_family}")
print(f"Glyphs : {woff2.num_glyphs}")
print(f"Type : {woff2.font_type}")
No decompression step is required. The loader applies BrotliDecoder to the
glyph stream before constructing the Woff2Font instance.
Low-Level Brotli Encoding
Internal API notice:
BrotliEncoderandBrotliDecoderare internal utilities (aspose_font._brotli). They are not part of the public API surface and may change between releases. For WOFF2 font compression, preferFontLoader.open()combined withWoff2Font, which handles Brotli transparently.
For compressing arbitrary binary data independently of font operations,
the internal BrotliEncoder exposes a quality parameter (0-11):
from aspose_font._brotli import BrotliEncoder # internal API
data = b"Sample data to compress" * 200
# quality=11 = maximum compression; quality=0 = fastest
encoder = BrotliEncoder(quality=11)
compressed = encoder.encode(data)
ratio = len(compressed) / len(data)
print(f"Original : {len(data)} bytes")
print(f"Compressed : {len(compressed)} bytes")
print(f"Ratio : {ratio:.2%}")
BrotliEncoder.encode(data) accepts bytes and returns bytes. The encoder
instance is not reusable across calls with different quality settings — create
a new instance for each quality level.
Low-Level Brotli Decoding
Internal API notice: See the notice above —
BrotliDecoderis also an internal utility.
BrotliDecoder.decode is a class-level method that decompresses any Brotli
payload:
from aspose_font._brotli import BrotliEncoder, BrotliDecoder # internal API
original = b"Round-trip compression test" * 100
# Compress
encoder = BrotliEncoder(quality=6)
compressed = encoder.encode(original)
print(f"Compressed to {len(compressed)} bytes")
# Decompress
recovered = BrotliDecoder.decode(compressed)
assert recovered == original
print(f"Decompressed: {len(recovered)} bytes — data intact: {recovered == original}")
BrotliDecoder.decode takes a bytes payload and returns bytes. It processes
the entire payload in one call — there is no streaming interface.
Compression Level Guide
The quality parameter in BrotliEncoder.__init__ maps to the standard Brotli
quality levels:
| Quality | Speed | Compression Ratio | Typical Use |
|---|---|---|---|
| 0 | Fastest | Lowest | Real-time / latency-sensitive |
| 1-3 | Fast | Low | Streaming data |
| 4-6 | Balanced | Medium | General-purpose (recommended) |
| 7-9 | Slow | High | Static assets |
| 10-11 | Slowest | Highest | Font files, archival |
For font production (WOFF2), quality 11 gives the best file size and is used
in the standard FontConverter.convert path.
Quick Start
Install the library:
pip install "aspose-font>=1.0.0"
Convert a TTF to WOFF2 and verify the compression:
from aspose_font.loader import FontLoader
from aspose_font.converter import FontConverter
from aspose_font import FontType
from pathlib import Path
# Convert to WOFF2 (Brotli applied automatically)
ttf = FontLoader.open("MyFont.ttf")
woff2 = FontConverter.convert(ttf, FontType.WOFF2)
woff2_bytes = woff2.to_bytes(FontType.WOFF2)
# Compare sizes
ttf_bytes = ttf.to_bytes(FontType.TTF)
print(f"TTF : {len(ttf_bytes)} bytes")
print(f"WOFF2 : {len(woff2_bytes)} bytes ({len(woff2_bytes)/len(ttf_bytes):.1%} of TTF)")
Path("MyFont.woff2").write_bytes(woff2_bytes)
print("Done — WOFF2 saved")
Open Source and Licensing
Aspose.Font FOSS for Python is released under the MIT License. The bundled Brotli implementation is included in the package under its own open-source license. You can use the library in personal, commercial, and open-source projects at no cost.