CommitsOverCoffee

How to Subset Fonts

Font subsetting reference.

What is Font Subsetting?

Modern font files (like .ttf or .otf) are essentially massive databases. They contain vector data (the actual shapes of the letters), spacing metrics, kerning pairs, and layout rules for thousands of characters across multiple languages and symbol sets.

Font subsetting is the process of opening that database, stripping out all the glyphs, languages, and metadata you will never use, and repackaging the remaining essential characters into a highly compressed format meant specifically for the web.

The Benefits:

  • Significant File Size Reduction: A typical .ttf font can be relatively large, while a subsetted .woff2 version with only the required character set is much smaller.
  • Faster Rendering (Improved Web Vitals): Browsers delay text rendering until fonts are available, or temporarily show fallback fonts, which can cause layout shifts. Smaller font files load quickly, reducing or eliminating FOUT or FOIT.
  • Lower Bandwidth Usage: Smaller assets reduce data transfer, improving performance for users on slow or metered connections, lowering overall bandwidth costs.

How to Subset Fonts

Below, we’re using a Bash script. Alternatively, you can use Font Subset to visually subset fonts and download the resulting WOFF2 files for self-hosting.

1. Install the Tools:

These packages (on arch linux) provide utilities for working with fonts and compression, which are often needed for font optimization workflows.

sudo pacman -S python-fonttools python-brotli

If you are using a different linux distribution or operating system, install the equivalent packages using your system’s package manager.

2. Download Your Source Font:

  • Choose the font you want to use.
  • Download it from a reliable source.
  • Extract the .zip to access the font files.

3. Run the Subsetting Script:

When a foundry ships a font, each weight (Light, Regular, Bold) and style (Italic) is often provided as a separate .ttf file, these are known as static fonts. In this case, you subset each file individually and then reconnect them as a single family in CSS.

With variable font, the approach is different. Instead of multiple files, you work with a single font that contains all weights and styles along adjustable axes. You subset that one file. In many cases, this is the more practical approach, fewer files, more flexibility, and better control over typography across different contexts.

This script is designed to handle both cases. It scans the current folder for font files, whether they represent individual static weights or a variable font, subsets them accordingly, and outputs optimized .woff2 versions alongside the originals.

#!/bin/bash

shopt -s nullglob
ttf_files=(*.ttf *.otf)

if [ ${#ttf_files[@]} -eq 0 ]; then
    echo "No .ttf or .otf files found in this directory."
    exit 1
fi

echo "Found ${#ttf_files[@]} font files. Starting optimization..."
echo "--------------------------------------------------------"

for font in "${ttf_files[@]}"; do
    basename="${font%.*}"
    outfile="${basename}-subset.woff2"

    echo "Processing: $font"
    pyftsubset "$font" 
      --flavor=woff2 
      --layout-features='kern,liga,clig,calt' 
      --unicodes="U+0000-00FF,U+0131,U+0152-0153,U+02BB-02BC,U+02C6,U+02DA,U+02DC,U+2000-206F,U+2074,U+20AC,U+2122,U+2191,U+2193,U+2212,U+2215,U+FEFF,U+FFFD" 
      --name-IDs='*' 
      --no-hinting 
      --desubroutinize 
      --glyph-names 
      --symbol-cmap 
      --output-file="$outfile" 2>/dev/null
done

echo "--------------------------------------------------------"
echo "All done! Your WOFF2 files are ready."

Results

Impact of subsetting across a few commonly used fonts:

FontOriginal SizeSubset Size
Inter1.7 MiB132 KiB
Open Sans1.1 MiB144 KiB
Bricolage Grotesque400 KiB116 KiB
Geist340 KiB56 KiB
Libre Baskerville336 KiB60 KiB
Satoshi256 KiB48 KiB
Manrope164 KiB24 KiB

Using the Subsetted Font (in Svelte)

1. Add Font Files to the Project Assets

Start by placing the subsetted font files inside your project’s font assets directory so they can be easily referenced throughout the app. In a typical Svelte setup, this means adding the .woff2 files to src/lib/assets/fonts/.

your-svelte-project/
└── src/
    └── lib/
        └── assets/
            └── fonts/
                ├── geist-regular.woff2
                ├── geist-italic.woff2

2. Define and Apply the Custom Font Globally

Define the font in layout.svelte so it is applied globally across the app. Using @font-face, register both the regular and italic variants of the font and point them to the files in your assets directory. Finally, apply the font to the body using :global, so all components inherit it by default.

<style>
	@font-face {
		font-family: "Geist Variable";
		font-style: normal;
		font-display: swap;
		font-weight: 100 900;
		src: url("$lib/assets/fonts/geist-regular.woff2") format("woff2");
	}

	@font-face {
		font-family: "Geist Variable";
		font-style: italic;
		font-display: swap;
		font-weight: 100 900;
		src: url("$lib/assets/fonts/geist-italic.woff2") format("woff2");
	}

	:global(body) {
		font-family: "Geist Variable", sans-serif;
	}
</style>

Thats All Folks

Font subsetting is a trade-off between control and flexibility. When you know exactly what text your site will render, it becomes one of the most effective ways to cut down asset size and improve loading performance.

Step outside your predefined character set, and things break quickly, whether through missing glyphs in other languages, unexpected user input, or dynamic content. For that reason, subsetting works best for static, tightly scoped projects where you control both the content and its boundaries.

> cd ..

© 2026 commitsovercoffee