Retained-ID TrueType font subsetting
FPdfFontSubset provides a portable concrete backend for SubsetExistingPdfFont and calls for no native font-subsetting dependency
Public API
TPdfTrueTypeSubsetBackend = class(TInterfacedObject,
IPdfFontSubsetBackend, IPdfFontSubsetClosureBackend)
function Subset(const FontProgram: TBytes; const Glyphs: TPdfGlyphInfos;
out SubsetProgram: TBytes): Boolean;
function SubsetWithClosure(const FontProgram: TBytes;
const Glyphs: TPdfGlyphInfos; out SubsetProgram: TBytes;
out GlyphClosure: TPdfGlyphInfos; out ErrorMessage: string): Boolean;
end;
The backend takes standalone static TrueType SFNT programs carrying glyf, loca, head, maxp, hhea and hmtx tables, within 64 MiB and 65536 requested glyph records
Glyph zero and composite components referenced recursively are kept, glyph IDs and the original glyph count stay stable, and outlines nobody asked for become empty slots
Retained glyph bytes, metrics, character mappings, font names and hint tables survive; the backend rebuilds long-format loca, table offsets, table checksums and head.checksumAdjustment, and drops the obsolete DSIG table
Composite graphs are capped at depth 128 and 1048576 visits; cycles, invalid components, overlapping tables and invalid ranges are refused
TrueType Collections, CFF/CFF2, variable fonts, colour/bitmap outlines and embedding policies that forbid outline subsetting come back with empty backend output and an error message
Since v3.206.0, InstantiatePdfVariableFont can first pin a variable TrueType font or selected collection face to a static instance through optional HarfBuzz; this backend can then subset that standalone static result whilst retaining its glyph IDs
For an optional native alternative with qualified CBDT/CBLC font-program support and native dependency closure, use TPdfHarfBuzzSubsetBackend; bitmap-program output does not establish native PDFium bitmap embedding support
SubsetWithClosure reports a sorted, de-duplicated dependency closure and keeps the first supplied metadata for duplicate requested IDs; dependencies it adds carry zero UnicodeValue and Width metadata
Use SubsetExistingPdfFont to apply byte/glyph budgets and minimum-saving thresholds whilst preserving the original program, or SubsetPdfDocumentFonts to replace proven document resources
Backend := TPdfTrueTypeSubsetBackend.Create; Options := TPdfFontSubsetOptions.Default; Applied := SubsetExistingPdfFont(Original, UsedGlyphs, Backend, Options, SubsetProgram, Closure, Report);
The backend keeps to the requested glyph repertoire and its composite dependencies; drawing on new characters outside that repertoire calls for a fresh font program
Table layout follows the Microsoft OpenType glyf, loca and head specifications