A professional bash script for processing TTF/OTF font files to create self-hosted font packages with comprehensive metadata extraction, WOFF2 conversion, and CSS generation.
The README documentation, LICENSE file, and test suite were generated with AI assistance but based on Leonardo's specifications and requirements.
This tool was created for educational purposes to demonstrate:
- Font metadata extraction from TTF/OTF files
- Web font optimization techniques
- Self-hosting best practices
- Content Security Policy (CSP) integration
- Font licensing and attribution handling
- Comprehensive Metadata Extraction: Extracts font family, subfamily, designer, version, copyright, license, and more
- WOFF2 Conversion: Converts fonts to WOFF2 format for optimal web delivery
- Fallback Support: Includes original TTF/OTF files as fallbacks in CSS declarations
- Integrity Verification: Generates SHA-256 checksums for all files
- CSP Compatibility: Provides base64-encoded hashes for Content Security Policy headers
- Professional Logging: Color-coded output with detailed logging
- Error Handling: Robust error handling and validation
- Command Line Interface: Full CLI with help, version, and configuration options
ttx(fonttools) - For font metadata extractionxmllint(libxml2-utils) - For XML parsingwoff2_compress(woff2) - For WOFF2 conversionopenssl- For cryptographic operations
brew install fonttools woff2 libxml2sudo apt-get update
sudo apt-get install fonttools woff2-tools libxml2-utils opensslsudo yum install fonttools woff2-tools libxml2 openssl# Process fonts from ./fonts directory
./build.sh
# Process fonts from custom directory
./build.sh --dir ./my-fonts
# Specify custom output directory
./build.sh --dir ./fonts --output ./dist# Show help
./build.sh --help
# Show version
./build.sh --version
# Force overwrite existing output
./build.sh --force
# Skip WOFF2 conversion
./build.sh --no-woff2
# Enable verbose logging
./build.sh --verbose| Option | Description |
|---|---|
-h, --help |
Show help message |
-v, --version |
Show version information |
-d, --dir DIR |
Font directory (default: ./fonts) |
-o, --output DIR |
Output directory (default: ./output) |
-f, --force |
Force overwrite existing output |
--no-woff2 |
Skip WOFF2 conversion |
--verbose |
Enable verbose logging |
project/
βββ fonts/ # Input directory (place your TTF/OTF files here)
β βββ font1.ttf
β βββ font2.otf
βββ output/ # Generated output directory
β βββ README.md # Detailed font information and usage
β βββ font-face.css # CSS @font-face declarations
β βββ font1.woff2 # Optimized WOFF2 files
β βββ font1.ttf # Original font files (fallbacks)
β βββ build.log # Build log file
βββ build.sh # The script itself
Contains detailed information about each processed font:
- Font metadata (family, designer, version, etc.)
- Licensing information
- SHA-256 checksums
- CSP-compatible hashes
- Usage instructions
Generated CSS file with @font-face declarations:
@font-face {
font-family: 'Font Name';
src: url('./font.woff2') format('woff2'),
url('./font.ttf') format('truetype');
font-weight: 700;
font-style: normal;
font-display: swap;
}- WOFF2 files: Optimized for web delivery
- Original files: TTF/OTF files as fallbacks
The script generates CSP-compatible hashes for secure font loading:
<!-- HTML -->
<link rel="stylesheet" href="./font-face.css"
integrity="sha256-..." crossorigin="anonymous">
<!-- HTTP Headers -->
Content-Security-Policy: font-src 'self' 'sha256-...'All files include SHA-256 checksums for integrity verification.
This script demonstrates several important concepts:
- Uses TTX (fonttools) to extract font metadata
- Parses XML to extract name table information
- Handles various font formats and metadata structures
- Converts fonts to WOFF2 format for better compression
- Implements proper fallback strategies
- Uses
font-display: swapfor better performance
- Generates CSP-compatible hashes
- Provides integrity verification
- Demonstrates secure font loading techniques
- Extracts and displays licensing information
- Ensures proper attribution
- Promotes respect for font licenses
Based on the OpenType specification name table:
| NameID | Field | Description |
|---|---|---|
| 0 | Copyright | Copyright notice |
| 1 | Font Family | Font family name |
| 2 | Font Subfamily | Font subfamily name |
| 4 | Full Name | Full font name |
| 5 | Version | Font version |
| 6 | PostScript Name | PostScript name |
| 9 | Designer | Font designer |
| 13 | License | License description |
| 14 | License URL | License URL |
The script intelligently maps font subfamily names to CSS font-weight values:
- Thin/Hairline β 100
- ExtraLight/UltraLight β 200
- Light β 300
- Regular/Normal β 400
- Medium β 500
- SemiBold/DemiBold β 600
- Bold β 700
- ExtraBold/UltraBold β 800
- Black/Heavy β 900
This tool is created for educational purposes. Always:
- Respect font licenses and attribution requirements
- Verify licensing before using fonts commercially
- Follow best practices for font self-hosting
- Always check and respect font licenses
- Provide proper attribution when required
- Some fonts may have restrictions on self-hosting
- WOFF2 files are significantly smaller than TTF/OTF
- Use
font-display: swapfor better perceived performance - Consider preloading critical fonts
This is an educational project. Contributions that improve the educational value or add new learning opportunities are welcome.
This script is released under the MIT License. See the script header for details.
Remember: This tool is for educational purposes. Always respect font licenses and attribution requirements when using fonts in your projects.