FontSubsetter Class

Subsets the outlines of a font to the glyphs a document uses, keeping the glyph identifiers: the glyphs that are not kept are empty, so that what refers to a glyph by its identifier (a PDF content stream, a shaped text) is unchanged.

public static class FontSubsetter

Remarks

  • TrueType outlines (FontSubsetter.SubsetTrueType): an sfnt with the glyf and loca tables rebuilt from the kept glyphs and the components of the composite glyphs among them, the metrics tables as they are, the instructions on request, and a post table without glyph names.
  • CFF outlines (FontSubsetter.SubsetCff): a CID-keyed CFF table whose CIDs are the glyph identifiers (OpenType specification, "CFF"; Adobe Technical Note #5176): the charstrings of the glyphs that are not kept are endchar, the subroutines are kept whole.

Methods

NameDescription
GetClosure(FontFace, IEnumerable<int>)Gets the glyphs a subset keeps: the requested glyphs, the glyph 0 (.notdef) and the components of the composite glyphs.
SubsetCff(FontFace, IEnumerable<int>, string)Subsets the CFF table of a font into a CID-keyed CFF table whose CIDs are the glyph identifiers.
SubsetTrueType(FontFace, IEnumerable<int>, bool)Subsets a font with TrueType outlines.

GetClosure(FontFace, IEnumerable<int>)

FontSubsetter.cs

Gets the glyphs a subset keeps: the requested glyphs, the glyph 0 (.notdef) and the components of the composite glyphs.

public static SortedSet<int> GetClosure(FontFace face, IEnumerable<int> glyphs)

Parameters

face FontFace
The face.
glyphs IEnumerable<int>
The glyphs a document uses; the identifiers that are not glyphs of the face are ignored.

Returns

SortedSet<int> The glyphs, sorted.

SubsetCff(FontFace, IEnumerable<int>, string)

FontSubsetter.cs

Subsets the CFF table of a font into a CID-keyed CFF table whose CIDs are the glyph identifiers.

public static byte[] SubsetCff(FontFace face, IEnumerable<int> glyphs, string fontName)

Parameters

face FontFace
The face: its outlines are CFF outlines.
glyphs IEnumerable<int>
The glyphs to keep; the glyph 0 is always kept.
fontName string
The name of the font in the table.

Returns

byte[] The CFF table.

Exceptions

ArgumentException
The face does not have CFF outlines.
InvalidFontException
The CFF table is malformed.

SubsetTrueType(FontFace, IEnumerable<int>, bool)

FontSubsetter.cs

Subsets a font with TrueType outlines.

public static byte[] SubsetTrueType(FontFace face, IEnumerable<int> glyphs, bool keepHinting = true)

Parameters

face FontFace
The face: its outlines are TrueType outlines.
glyphs IEnumerable<int>
The glyphs to keep; the closure (FontSubsetter.GetClosure) is computed.
keepHinting bool
Whether to keep the instructions (cvt, fpgm, prep and the instructions of the glyphs).

Returns

byte[] The font file.

Exceptions

ArgumentException
The face does not have TrueType outlines.
InvalidFontException
A table the subset needs is missing or malformed.