Bengal includes a built-in font management system that automatically downloads and hosts Google Fonts locally, improving performance and privacy.
Overview
The fonts system provides:
- Automatic font downloading from Google Fonts (no external dependencies)
- Self-hosting for better performance and GDPR compliance
- CSS generation with @font-face rules and CSS custom properties
- Zero configuration - just specify fonts in bengal.toml
Benefits:
- No external requests to Google Fonts CDN at runtime
- Faster page loads (fonts served from same domain)
- GDPR/privacy compliance (no third-party tracking)
- Offline builds
- Cache control
Font Downloader (bengal/fonts/downloader.py)
Purpose
Downloads font files from Google Fonts using the public CSS API.
Key Classes
| Class | Description |
|---|---|
GoogleFontsDownloader |
Downloads fonts from Google Fonts API |
FontVariant |
Represents a specific font variant (family + weight + style) |
Features
- No API key required
- Uses stdlib only (urllib)
- Automatic WOFF2 format selection (modern, smaller files)
- Supports multiple weights and styles
- SSL/TLS support
- Robust error handling
Usage
1 2 3 4 5 6 7 8 9 10 11 | |
Font CSS Generator (bengal/fonts/generator.py)
Purpose
Generates @font-face CSS rules for self-hosted fonts.
Key Classes
| Class | Description |
|---|---|
FontCSSGenerator |
Generates CSS with @font-face rules and custom properties |
Generated CSS includes
- @font-face declarations for each variant
- font-display: swap for performance
- CSS custom properties for easy reference
Example output
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 | |
Font Helper (bengal/fonts/__init__.py)
Purpose
Main interface that orchestrates downloading and CSS generation.
Key Classes
| Class | Description |
|---|---|
FontHelper |
Coordinates font downloading and CSS generation |
Usage
1 2 3 4 | |
Configuration
Configure fonts inbengal.toml:
Simple format (family:weights)
1 2 3 4 | |
Detailed format (with styles)
1 2 3 4 5 6 7 8 9 | |
Build Integration
Fonts are automatically processed during builds:
- Font Helper runs early in build process
- Downloads fonts to
public/assets/fonts/ - Generates fonts.css in
public/assets/ - Theme can reference fonts via CSS custom properties
In theme CSS
1 2 3 4 5 6 7 8 9 10 11 | |
Health Validation
The FontValidator checks:
- Font files downloaded successfully
- fonts.css generated correctly
- File sizes reasonable (< 500KB per variant)
- All configured fonts present
1 2 3 4 5 6 7 8 | |
Performance Characteristics
File sizes: WOFF2 format typically 15-100KB per variant
Build overhead: Minimal - fonts cached locally after first download
Runtime performance: Eliminates external CDN requests
Comparison to Google Fonts CDN:
| Aspect | CDN | Self-hosted |
|---|---|---|
| Initial page load | External request | Same domain |
| Caching | Shared cache | Local cache |
| Privacy | Third-party | No tracking |
| Offline | Requires connection | Works offline |