Font Subsetting
Font subsetting reduces a font to only the glyphs needed for a specific text. This is essential for PDF embedding, web fonts, and any context where font file size matters. A typical font contains thousands of glyphs, but a document may use only a few dozen.
The subset package lives in github.com/boxesandglue/textshape/subset.
import (
"github.com/boxesandglue/textshape/ot"
"github.com/boxesandglue/textshape/subset"
)Quick Start
For the common case — subset a font to the characters in a string:
fontData, _ := os.ReadFile("NotoSans-Regular.ttf")
font, _ := ot.ParseFont(fontData, 0)
subsetData, err := subset.SubsetString(font, "Hello, World!")
if err != nil {
log.Fatal(err)
}
os.WriteFile("NotoSans-Subset.ttf", subsetData, 0644)Or with explicit codepoints:
codepoints := []rune{'H', 'e', 'l', 'o', ',', ' ', 'W', 'r', 'd', '!'}
subsetData, err := subset.Subset(font, codepoints)Detailed Workflow
For more control, use the three-step process: Input → Plan → Execute.
1. Create an Input
The Input specifies which glyphs to keep and how the subsetting should behave.
input := subset.NewInput()
// Add characters by Unicode codepoint
input.AddUnicode('A')
input.AddUnicodes('B', 'C', 'D')
input.AddUnicodeRange('a', 'z') // Inclusive range
input.AddString("Hello, World!") // All unique characters in the string
// Or add glyphs by their glyph ID directly
input.AddGlyph(42)
input.AddGlyphs(10, 11, 12, 13)The .notdef glyph (GID 0) is always included automatically.
2. Create a Plan
The plan computes the full set of glyphs needed and builds the old→new glyph ID mapping. It adds the glyphs that the kept GSUB features can produce from the requested ones (ligatures, alternates, contextual substitutions) and the components of composite glyphs. GPOS adds no glyphs.
plan, err := subset.CreatePlan(font, input)
if err != nil {
log.Fatal(err)
}
fmt.Printf("Output will have %d glyphs\n", plan.NumOutputGlyphs())You can inspect the glyph mapping:
// Old glyph ID → new glyph ID
newGID, ok := plan.MapGlyph(oldGID)
// New glyph ID → old glyph ID
oldGID, ok := plan.OldGlyph(newGID)
// Set of all old glyph IDs that will be retained
glyphSet := plan.GlyphSet()3. Execute
Execute the plan to produce the subsetted font binary:
subsetData, err := plan.Execute()
if err != nil {
log.Fatal(err)
}
// Write to file
os.WriteFile("subset.ttf", subsetData, 0644)Flags
Flags control subsetting behavior. Set them on the Input before creating a plan:
input := subset.NewInput()
input.AddString("Hello")
input.Flags = subset.FlagNoHinting | subset.FlagDropLayoutTables| Flag | Description |
|---|---|
FlagNoHinting |
Remove TrueType hinting instructions (cvt, fpgm, prep, VDMX) |
FlagRetainGIDs |
Keep original glyph IDs — unused slots are padded with empty glyphs |
FlagGlyphNames |
Retain PostScript glyph names in the post table (otherwise post is written as version 3.0) |
FlagNotdefOutline |
Retain the .notdef glyph outline (otherwise replaced with empty) |
FlagNoLayoutClosure |
Skip the GSUB glyph closure, only include explicitly requested glyphs |
FlagPassUnrecognized |
Copy unrecognized tables to the output unchanged |
FlagDropLayoutTables |
Exclude GSUB, GPOS, and GDEF tables entirely |
FlagNameLegacy |
Keep name records in non-Unicode encodings, such as Macintosh Roman |
Common Flag Combinations
PDF embedding (minimal size, no hinting needed):
input.Flags = subset.FlagNoHinting | subset.FlagDropLayoutTables
input.DropTable(ot.TagName)
input.DropTable(ot.TagPost)
input.DropTable(ot.TagGasp)Web font (keep layout tables for browser shaping):
input.Flags = subset.FlagNoHintingRetain glyph IDs (when external references depend on stable GIDs):
input.Flags = subset.FlagRetainGIDsTable Management
Dropping Tables
Exclude specific tables from the output:
input.DropTable(ot.MakeTag('G', 'S', 'U', 'B')) // Drop GSUB
input.DropTable(ot.MakeTag('n', 'a', 'm', 'e')) // Drop name table
Passing Tables Through
Copy tables to the output without modification:
input.PassThroughTable(ot.MakeTag('D', 'S', 'I', 'G')) // Keep DSIG as-is
Check Table Handling
input.ShouldDropTable(tag) // Will this table be excluded?
input.ShouldPassThrough(tag) // Will this table be copied unchanged?
Feature Filtering
By default the subset keeps the OpenType features that the shapers use: ccmp, liga, locl, mark, mkmk, calt, kern, curs, the vertical and fraction features, and the Arabic, Indic, Tibetan and Hangul shaping features. Optional typographic features such as ss01, dlig, smcp or tnum are dropped. The list is the one hb-subset uses.
Change the feature set on the Input:
input.KeepFeature(ot.MakeTag('d', 'l', 'i', 'g')) // Add dlig to the default list
input.DropFeature(ot.MakeTag('k', 'e', 'r', 'n')) // Remove kern
input.KeepAllFeatures() // Keep every feature of the font
input.SetLayoutFeatures( // Keep exactly these features
ot.MakeTag('k', 'e', 'r', 'n'),
ot.MakeTag('l', 'i', 'g', 'a'),
)All scripts are kept by default. SetLayoutScripts restricts them:
input.SetLayoutScripts(ot.MakeTag('l', 'a', 't', 'n'))ShouldKeepFeature(tag) and ShouldKeepScript(tag) report the current choice.
Layout Tables
GSUB, GPOS and GDEF are subset the way hb-subset does it. The subsetter keeps the lookups that the kept features and scripts reach, including lookups called from contextual and chained contextual rules, and drops the rules that refer to removed glyphs. Lookup, feature and mark glyph set indices are renumbered, lookups that become empty stay in place so that the indices remain valid. GPOS device tables and the GDEF item variation store are kept for the retained glyphs.
Feature variations (the FeatureVariations part of GSUB and GPOS, used by variable fonts to swap glyphs at certain axis positions) are not written yet. The tables are written as version 1.0 without them.
The subsetter is tested against the hb-subset test suite of HarfBuzz: the layout, name and post tests produce the same tables as hb-subset. Two differences remain. When a GSUB or GPOS table is too large for 16-bit offsets, textshape turns all lookups into extension lookups, hb-subset only the ones that need it. And glyphs reached only through Unicode variation sequences (cmap format 14) are not added to the subset.
Name Table
The subset keeps the name records with name IDs 0 to 6 (copyright, family, subfamily, unique ID, full name, version, PostScript name) in English (language ID 0x0409) and in Unicode encodings. It adds the name IDs that the kept tables refer to: the UI names of ssXX and cvXX features and the size feature, the STAT axis and value names, and the fvar axis and instance names. This is what hb-subset does.
input.KeepNameID(256, 257) // Add name IDs to the default list
input.SetNameIDs(1, 2) // Keep exactly these (plus referenced ones)
input.KeepAllNameIDs() // Keep every name ID
input.SetNameLanguages(0x0409, 0x0407) // English and German (Windows language IDs)
input.KeepAllNameLanguages() // Keep every language
input.Flags |= subset.FlagNameLegacy also keeps the records in Macintosh and other non-Unicode encodings.
Variable Font Instancing
For variable fonts, you can pin variation axes to specific values. This creates a static (non-variable) font from a variable source — useful for reducing file size when you only need one weight/width/style.
Pin Individual Axes
// Create a bold instance
input.PinAxisLocation(ot.MakeTag('w', 'g', 'h', 't'), 700)
// Create a condensed bold instance
input.PinAxisLocation(ot.MakeTag('w', 'g', 'h', 't'), 700)
input.PinAxisLocation(ot.MakeTag('w', 'd', 't', 'h'), 75)Pin to Defaults
// Pin one axis to its default value
input.PinAxisToDefault(font, ot.MakeTag('w', 'g', 'h', 't'))
// Pin ALL axes to their default values (produce a static font)
input.PinAllAxesToDefault(font)Check Instancing State
input.HasPinnedAxes() // Any axes pinned?
input.PinnedAxes() // map[Tag]float32 of pinned values
input.IsFullyInstanced(font) // All axes pinned? (will produce static font)
Example: Instancing a Variable Font
fontData, _ := os.ReadFile("NotoSans-VF.ttf")
font, _ := ot.ParseFont(fontData, 0)
input := subset.NewInput()
input.AddString("The quick brown fox")
// Pin to Bold weight, producing a static Bold font
input.PinAxisLocation(ot.MakeTag('w', 'g', 'h', 't'), 700)
plan, _ := subset.CreatePlan(font, input)
staticBold, _ := plan.Execute()
os.WriteFile("NotoSans-Bold-Subset.ttf", staticBold, 0644)When axes are pinned, the subsetter:
- Applies gvar deltas to glyph outlines (for TrueType) or CFF subroutines (for CFF)
- Applies HVAR deltas to advance widths
- Removes the fvar, avar, gvar, HVAR, and other variation tables from the output
Building Fonts Manually
The FontBuilder can assemble a font from raw table data:
builder := subset.NewFontBuilder()
builder.AddTable(ot.MakeTag('h', 'e', 'a', 'd'), headData)
builder.AddTable(ot.MakeTag('h', 'h', 'e', 'a'), hheaData)
// ... add more tables
fontBytes, err := builder.Build()This is used internally by Plan.Execute() but is also available for custom font construction.
Tables Handled During Subsetting
The subsetter handles the following OpenType tables:
| Category | Tables |
|---|---|
| Required | head, maxp, hhea, hmtx |
| Outlines | glyf + loca (TrueType), CFF (PostScript) |
| Character mapping | cmap (rebuilds format 4 / format 12) |
| Layout | GSUB, GPOS, GDEF (subset, see Layout Tables) |
| Variation | fvar, avar, gvar, HVAR, VVAR, STAT, MVAR, cvar (removed when instancing; otherwise copied only with PassThroughTable or FlagPassUnrecognized) |
| Hinting | cvt, fpgm, prep, VDMX (copied, removed with FlagNoHinting) |
| Naming | name (records filtered, see Name Table), post (version 3.0, or glyph names with FlagGlyphNames) |
| Other | OS/2, gasp (copied) |
Error Handling
The subset package defines three error values:
| Error | Description |
|---|---|
ErrNoTables |
No tables to build (empty font) |
ErrMissingTable |
A required table is missing from the source font |
ErrInvalidGlyph |
An invalid glyph reference was encountered |
Complete Example
package main
import (
"log"
"os"
"github.com/boxesandglue/textshape/ot"
"github.com/boxesandglue/textshape/subset"
)
func main() {
// Load font
data, err := os.ReadFile("fonts/NotoSans-Regular.ttf")
if err != nil {
log.Fatal(err)
}
font, err := ot.ParseFont(data, 0)
if err != nil {
log.Fatal(err)
}
// Configure subset
input := subset.NewInput()
input.AddString("The quick brown fox jumps over the lazy dog. 0123456789")
input.Flags = subset.FlagNoHinting
// Only keep kerning and ligatures
input.SetLayoutFeatures(
ot.MakeTag('k', 'e', 'r', 'n'),
ot.MakeTag('l', 'i', 'g', 'a'),
)
// Create plan and execute
plan, err := subset.CreatePlan(font, input)
if err != nil {
log.Fatal(err)
}
log.Printf("Subsetting: %d glyphs in source → %d in output",
font.NumGlyphs(), plan.NumOutputGlyphs())
result, err := plan.Execute()
if err != nil {
log.Fatal(err)
}
if err := os.WriteFile("NotoSans-Subset.ttf", result, 0644); err != nil {
log.Fatal(err)
}
log.Printf("Original: %d bytes, Subset: %d bytes (%.0f%% reduction)",
len(data), len(result),
(1-float64(len(result))/float64(len(data)))*100)
}