Skip to content

Repository files navigation

Font Optimizer

Automate your web typography workflow with this Composer dev-tool.

Converts TTF fonts to optimized WOFF2 subsets using Python FontTools. Works with Laravel, Symfony, and vanilla PHP projects.

Screenshot

Prerequisites

Requires Python 3 and FontTools with Brotli support on your system.

# Ubuntu / Debian
sudo apt update && sudo apt install -y python3 python3-fonttools python3-brotli
# macOS
brew install python3 && pip3 install fonttools brotli

Installation

composer require --dev uxcode-fr/font-optimizer

After installation, Composer automatically checks that Python 3 and fontTools are available:

✓ font-optimizer: Python3 + fontTools detected.

If the check fails, you will see:

⚠ font-optimizer requires Python3 + fontTools to work.
Run: sudo apt install -y python3 python3-fonttools python3-brotli

The binary is available at vendor/bin/font-optimizer.


Configuration

Configuration is loaded from config/font-optimizer.php if present, otherwise from the extra section of composer.json.

Option A — config/font-optimizer.php (Laravel, Symfony, vanilla PHP)

Laravel — publish the default config:

php artisan vendor:publish --tag=font-optimizer-config

Other projects — create the file manually at the root of your project:

<?phpreturn [
'source' => 'resources/fonts', // directory containing .ttf source files'destination' => 'public/fonts', // directory where output files will be saved'unicodes' => 'U+0020-007F,U+00A0-00FF,U+0152-0153,U+20AC,U+2019,U+201C,U+201D', // Unicode ranges to subset'features' => 'kern,liga', // OpenType features to preserve'flavor' => 'woff2', // output format: woff2 or woff'name_ids' => '*', // name table IDs to keep (* = all)'hinting' => true, // keep hinting instructions
];

Option B — composer.jsonextra section (all projects)

{
"extra": {
"font-optimizer": {
"source": "resources/fonts",
"destination": "public/fonts",
"unicodes": "U+0020-007F,U+00A0-00FF,U+0152-0153,U+20AC,U+2019,U+201C,U+201D",
"features": "kern,liga",
"flavor": "woff2",
"name_ids": "*",
"hinting": true
}
}
}

Configuration options

KeyDescriptionAllowed valuesDefault
sourceDirectory containing your source .ttf filesAny path relative to the project rootresources/fonts
destinationDirectory where output files will be savedAny path relative to the project rootpublic/fonts
unicodesUnicode ranges to include in the subset — use * to keep all glyphs (no subsetting)Hex codepoints or ranges (U+0020-007F), comma-separated, or *Basic Latin + Latin-1 + Œœ
featuresOpenType layout features to preserveSee OpenType feature tags, comma-separated, or *kern,liga
flavorOutput font formatwoff2, woffwoff2
name_idsName table record IDs to keepComma-separated IDs (0–19), or **
hintingKeep hinting instructionstrue, falsetrue

OpenType feature tags

TagDescription
kernKerning adjustments between glyph pairs
ligaStandard ligatures (fi, fl, ff…)
cligContextual ligatures
caltContextual alternates
ccmpGlyph composition / decomposition
loclLocalized forms (language-specific glyph variants)
markMark positioning (diacritic placement)
mkmkMark-to-mark positioning
caseCase-sensitive forms (punctuation for all-caps)
fracDiagonal fractions (1/2, 3/4)
supsSuperscript
subsSubscript
smcpSmall capitals
onumOldstyle figures
tnumTabular figures
zeroSlashed zero
ss01ss20Stylistic sets

Unicode range examples

RangeCharacters
U+0020-007FBasic Latin (ASCII printable)
U+00A0-00FFLatin-1 Supplement (accented, ©, €…)
U+0100-017FLatin Extended-A (Œœ, Šš, Žž…)
U+0400-04FFCyrillic
U+0370-03FFGreek and Coptic
U+2000-206FGeneral Punctuation (—, …, ", "…)
U+20A0-20CFCurrency Symbols (€, £, ¥…)

Usage

vendor/bin/font-optimizer

Output example:

🗜️ Optimizing Inter-Regular.ttf (312 KB)...
✓ inter-regular.woff2 (28 KB, -91%)
🗜️ Optimizing Inter-Bold.ttf (318 KB)...
✓ inter-bold.woff2 (30 KB, -91%)

Convert without subsetting

To convert TTF fonts to WOFF/WOFF2 without any subsetting (keep all glyphs), set unicodes to * and features to *:

return [
'source' => 'resources/fonts',
'destination' => 'public/fonts',
'unicodes' => '*', // keep all glyphs — no subsetting'features' => '*', // keep all OpenType features'flavor' => 'woff2',
];

Composer shortcut (optional)

Add to your composer.json:

{
"scripts": {
"fonts": "vendor/bin/font-optimizer"
}
}

Then run:

composer fonts

Why use this?

  • Performance — reduces font file size by up to 70–90% via WOFF2 and Unicode subsetting.
  • Automation — no more manual CLI commands or online converters.
  • Consistency — every developer on your team uses the same optimization settings.

Articles


Authors

Contributing

Feel free to fork this repo and submit pull requests. For major changes, please open an issue first.

License: MIT

About

Automate your web typography workflow with this Composer dev-tool. 🚀

Topics

Resources

Stars

9 stars

Watchers

0 watching

Forks

Contributors

Languages