Optional HarfBuzz font subsetting
Since v3.207.0, FPdfHarfBuzzSubset supplies an optional native backend for SubsetExistingPdfFont, preserving selected original glyph IDs and exposing the native dependency closure
Public API
TPdfHarfBuzzSubsetBackend = class(TInterfacedObject,
IPdfFontSubsetBackend, IPdfFontSubsetClosureBackend)
constructor Create(const LibraryName: string = ''; FaceIndex: Word = 0);
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;
Repertoire and closure
Glyphs supplies original GIDs, not a Unicode selection request; the native Unicode set is cleared and only the explicit glyph set plus glyph zero starts the subset operation
The backend retains glyph IDs and the .notdef outline, follows HarfBuzz's dependency rules and checks every native old-to-new mapping before publishing output
GlyphClosure is sorted and unique, including native dependencies; duplicate requested IDs retain their first supplied UnicodeValue and Width metadata, while added dependencies have zero metadata
Unselected outlines or bitmap payloads are removed according to native table policies; the output glyph count may shrink to the highest retained slot even though retained IDs do not change
Subset provides the basic Boolean/bytes interface; SubsetWithClosure also supplies the closure and an English diagnostic, with empty bytes and closure on expected failure
Construction and dependencies
LibraryName selects a matching HarfBuzz subset/core pair for each call, using the same platform names and directory rules as FPdfFontVariation
FaceIndex selects a zero-based SFNT/TrueType Collection face and defaults to zero; unavailable exports, an invalid face index or out-of-range GIDs reject without native font fallback
Native objects are released before owned module references; construction does not load libraries, and no process-wide library configuration changes
Native FPC builds also need the matching Lazarus type units imported by the production interfaces; console subsetting itself does not initialize a widget set
Policies and limits
Source and published output are each limited to 64 MiB, with one to 65,535 source glyphs and one to 65,536 requested records; the closure is bounded by the source glyph count
The backend checks OS/2.fsType, respecting legacy table-version interpretation, and rejects restricted modification, no-subsetting or invalid modern permission combinations; a bitmap-only policy is accepted only for a bitmap program without nonempty outline tables
Embedding-policy fields are not relaxed by subsetting; see the OpenType fsType definition
Native processing can allocate beyond the accepted byte sizes and has no timeout here; ordinary allocation or operating-system exceptions can propagate
The backend does not enforce a saving threshold itself; SubsetExistingPdfFont applies MinimumSavingPercent and preserves the original program when output does not qualify
Qualified profiles and PDFium limits
Focused evidence covers CBDT/CBLC Noto Color Emoji, static Arial, variable Segoe UI Variable and selected generated collection faces; bitmap glyph payloads and metrics are compared exactly, and outline profiles cover retained cmap/GIDs, composite closure and PDF text/pixels
Windows Delphi Win64 and native WSL FPC Linux x64 execute the backend; the existing axis-instance helper has focused regression coverage after sharing the native loader
For the reviewed CBDT writer profile, use CreateHarfBuzzBitmapFontPdf to create a Type3 font page and import it through the existing native Form XObject API
Successful raw bitmap-program subsetting does not imply the two native font-loading APIs can embed or render that program: with default library initialization, the recorded DLL rejects both the original and subset CBDT sample through FPDFText_LoadFont and FPDFText_LoadCidType2Font
This backend does not replace the dependency-free TPdfTrueTypeSubsetBackend, pin variable axes or convert bitmap glyphs to outlines; raw CFF/Type1 streams, other color/bitmap formats, other fonts, Win32 HarfBuzz and Darwin runtime behavior remain outside the qualified profiles
Backend := TPdfHarfBuzzSubsetBackend.Create(SubsetLibraryPath); Options := TPdfFontSubsetOptions.Default; Applied := SubsetExistingPdfFont(FontProgram, UsedGlyphs, Backend, Options, SubsetProgram, Closure, Report);