How Aspose.Font Uses Brotli Compression for WOFF2 in Python
WOFF2 (Web Open Font Format 2) compresses its glyph data using Brotli, a general-purpose
lossless compression algorithm developed by Google. Aspose.Font FOSS for Python handles
all Brotli operations internally — you do not need to install any external compression
library. The bundled BrotliEncoder and BrotliDecoder classes are available for
low-level use when you need to compress or decompress arbitrary data outside of the
standard WOFF2 conversion path.
Step-by-Step Guide
Step 1: Install the Package
pip install "aspose-font>=1.0.0"Step 2: Import Required Classes
For standard WOFF2 operations:
from aspose_font.loader import FontLoader
from aspose_font.converter import FontConverter
from aspose_font import FontTypeFor low-level Brotli operations:
from aspose_font._brotli import BrotliEncoder, BrotliDecoderStep 3: Create a WOFF2 Font (Brotli-Compressed)
The standard path: convert any font to WOFF2. Brotli compression is applied automatically.
from aspose_font.loader import FontLoader
from aspose_font.converter import FontConverter
from aspose_font import FontType
from pathlib import Path
# Load a TTF or WOFF source
source = FontLoader.open("MyFont.ttf")
# Convert to WOFF2 — Brotli compression is applied internally
woff2 = FontConverter.convert(source, FontType.WOFF2)
output_bytes = woff2.to_bytes(FontType.WOFF2)
Path("MyFont.woff2").write_bytes(output_bytes)
print(f"WOFF2 size: {len(output_bytes)} bytes")No additional steps are needed for compression. The library calls BrotliEncoder
internally when producing WOFF2 output.
Step 4: Load and Verify a WOFF2 Font
When loading a .woff2 file, the library decompresses the Brotli-compressed glyph
data automatically before exposing the font properties:
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}")FontLoader.open detects the WOFF2 magic bytes and invokes BrotliDecoder to
decompress the glyph stream before constructing the Woff2Font instance.
Step 5: Low-Level Brotli Encoding
Use BrotliEncoder directly when you need to compress arbitrary binary data with
Brotli. The quality parameter (0–11) controls the compression level:
from aspose_font._brotli import BrotliEncoder
data = b"Hello, Brotli!" * 100 # Sample data to compress
encoder = BrotliEncoder(quality=11) # quality 11 = maximum compression
compressed = encoder.encode(data)
print(f"Original : {len(data)} bytes")
print(f"Compressed: {len(compressed)} bytes")
print(f"Ratio : {len(compressed)/len(data):.2%}")Quality 11 gives the best compression ratio at the cost of encoding speed. Quality 0 is fastest. For font data in WOFF2 output, the library uses the default quality level.
Step 6: Low-Level Brotli Decoding
Use BrotliDecoder.decode to decompress Brotli-compressed data:
from aspose_font._brotli import BrotliEncoder, BrotliDecoder
original = b"Hello, Brotli!" * 100
# Compress
encoder = BrotliEncoder(quality=6)
compressed = encoder.encode(original)
# Decompress
decoded = BrotliDecoder.decode(compressed)
assert decoded == original
print(f"Decoded {len(decoded)} bytes — matches original: {decoded == original}")BrotliDecoder.decode is a class method that takes the compressed bytes and
returns the decompressed bytes. There is no streaming API — the full compressed
payload must be provided in one call.
When to Use Low-Level Brotli APIs
The BrotliEncoder and BrotliDecoder classes are useful when:
- You need to compress or decompress arbitrary binary payloads independently of font processing (for example, compressing font metadata or custom data blocks).
- You need to pre-validate that a Brotli-compressed payload is well-formed before passing it to a WOFF2 parser.
- You are building a test fixture that requires known-compression-ratio data.
For all standard font operations (loading, converting, saving WOFF2), you do not
need the low-level API — FontLoader.open and FontConverter.convert handle
Brotli transparently.
Frequently Asked Questions
Does the library require an external brotli package?
No. Aspose.Font FOSS for Python bundles its own Brotli implementation. No
brotli, brotlicffi, or similar package is required.
What compression quality does the library use for WOFF2?
The library uses the default quality level for WOFF2 output. To control the
quality level for custom compression tasks, use BrotliEncoder(quality=N) where
N is between 0 (fastest) and 11 (best compression).
Can I decompress a WOFF2 glyph stream directly?
WOFF2 glyph data uses a combination of Brotli decompression and a font-specific
transform (the “transformed” table format). Decompressing the raw Brotli stream
alone is not sufficient to recover a usable font — you must load the WOFF2 file
through FontLoader.open, which applies both the Brotli decompression and the
inverse table transform.