Class FontSubsetter
Writes fonts that hold only the glyphs a document uses, as PDF embedding and SVG export want: TrueType, OpenType CFF or bare CFF, with composite glyphs and seac accents closed over and unused CFF subroutines dropped.
public static class FontSubsetter
- Inheritance
-
FontSubsetter
- Inherited Members
Examples
// Keep only the glyphs a page uses, as a PDF writer embeds them: renumbered from 0, with the map to write the text with.
GlyphRun run = new Font(face, 12).Shape("Invoice 2026-10");
FontSubset subset = FontSubsetter.Subset(face, run.Glyphs.ToArray(), new SubsetOptions
{
FontNamePrefix = "ABCDEF+",
KeepCmap = false,
});
byte[] fontFile2 = subset.Data.ToArray();
ushort first = subset.GlyphMap[run.Glyphs[0]];
// The face's embedding permissions are reported, never enforced: the writer decides.
bool restricted = face.Embedding.HasFlag(FontEmbedding.Restricted);
Remarks
Glyph 0 (.notdef) is always kept. The subsetter reads Embedding but never enforces it: the caller decides whether a face may be subsetted and embedded.
Methods
Subset(Typeface, IEnumerable<ushort>, SubsetOptions?)
Writes a subset of a face.
public static FontSubset Subset(Typeface face, IEnumerable<ushort> glyphs, SubsetOptions? options = null)
Parameters
faceTypefaceThe face.
glyphsIEnumerable<ushort>The glyphs to keep; duplicates are fine.
optionsSubsetOptions
Returns
- FontSubset
The subset with its glyph map.
Examples
// Keep only the glyphs a page uses, as a PDF writer embeds them: renumbered from 0, with the map to write the text with.
GlyphRun run = new Font(face, 12).Shape("Invoice 2026-10");
FontSubset subset = FontSubsetter.Subset(face, run.Glyphs.ToArray(), new SubsetOptions
{
FontNamePrefix = "ABCDEF+",
KeepCmap = false,
});
byte[] fontFile2 = subset.Data.ToArray();
ushort first = subset.GlyphMap[run.Glyphs[0]];
// The face's embedding permissions are reported, never enforced: the writer decides.
bool restricted = face.Embedding.HasFlag(FontEmbedding.Restricted);
Exceptions
- ArgumentOutOfRangeException
A glyph id is past the face's glyph count.
- ArgumentException
The format needs outlines of the other kind, such as CFF output from a TrueType face; or LayoutClosure is set without RetainGlyphIds or with bare CFF output.
- CorruptDataException
A kept glyph's data cannot be read.