Skip to main content

Subsetting and embedding

A program that writes a document with its own fonts inside, such as a PDF, does not want to copy a whole font file of a megabyte to show five letters. A subset is a font file that holds only the glyphs the document uses. libstb makes one from a TrueType font, and tells you what the font's license says about it first.

import os

import libstb as stb

os.makedirs("out", exist_ok=True)
font = stb.Font.open("Vazirmatn-Regular.ttf")

# What the license says about putting the font inside a document, such as a PDF.
print("may be embedded without limits:", "yes" if font.embedding.permission == "installable" else "no")
print("may be subset:", "yes" if font.embedding.subsetting_allowed else "no")
print("libstb can subset it:", "yes" if font.can_subset else "no")

# Shape the text first: the glyphs it needs are the ones the shaper chose.
glyphs = sorted({g.glyph for g in font.shape("Hello")})
sub = font.subset(glyphs)
print("glyphs kept:", len(sub.glyphs), "of", font.glyph_count)
print("bytes:", len(sub.data))

# The glyphs are numbered again: new_index() says where a glyph of the original went.
h = font.glyph_index("H")
print("H was glyph", h, "and is now glyph", sub.new_index(h))

# The subset is a font file of its own, with the same outlines.
small = stb.Font.open(sub.data)
print("outline of H has the same commands:", "yes" if font.outline(h) == small.outline(sub.new_index(h)) else "no")

with open("out/subset.ttf", "wb") as f:
f.write(sub.data)
Output
may be embedded without limits: yes
may be subset: yes
libstb can subset it: yes
glyphs kept: 5 of 1333
bytes: 640
H was glyph 112 and is now glyph 1
outline of H has the same commands: yes

The license​

The font's OS/2 table carries a number, fsType, that says what the license allows when the font is put inside a document. libstb reports it and takes no decision: the program that writes the document decides what to do with it.

  • installable: no restriction;
  • restricted: the license forbids embedding;
  • preview and print: the font may be embedded so that the document can be viewed and printed, and then it is read-only;
  • editable: it may be embedded and the document may be edited.

Two more flags say whether the font may be embedded only whole (no subsetting) and whether only its bitmaps may be embedded, not its outlines. A subset of a font whose license forbids it is for the caller to avoid.

The subset​

  1. Shape the text with the original font, because the glyphs a text needs are the ones the shaper picks, not one per character (joining forms and ligatures are different glyphs).
  2. Subset with the list of those glyph indices. Glyph 0, the font's "no glyph" shape, and the parts of composite glyphs are added for you.
  3. Renumber. The glyphs are numbered again from zero, in their old order, so write the text into the document with new_index, the new number of each glyph.

The result is a complete .ttf with one face, the glyph, metric and hinting tables, and an empty character map. It has no layout tables (no GSUB, GPOS or kern) and no name, which is why you shape first. A font with CFF outlines, or a variable font with a weight or axis applied, cannot be subset yet; a variable font at its default settings can.

API at a glance​

Licensefont.embedding, .permission, .subsetting_allowed, .bitmap_only
Can it be subsetfont.can_subset
Make the subsetfont.subset(glyph_indices)
The filesub.data
New glyph numbersub.new_index(old)