OptimizeEmbeddedFonts
Fonts
Description
Rewrites embedded composite (Type0) and simple TrueType font programs so that they contain only the glyphs the document's text uses. Glyph IDs are kept, so the page content, glyph widths, CIDToGIDMap and ToUnicode data stay valid and the pages look and extract the same
This is useful after pages are deleted or when a font was embedded in full or with a large subset
Syntax
Delphi
Function TPDFlib.OptimizeEmbeddedFonts(Options: Integer): Integer;
ActiveX
Function PDFlib::OptimizeEmbeddedFonts(Options As Long) As Long
Available on IPDFlibrary5 from type library version 3.6
DLL
int DLOptimizeEmbeddedFonts(int InstanceID, int Options);
Parameters
| Options | 0 optimizes fonts that are not already subsets; 1 also re-subsets fonts whose name carries a subset tag |
|---|
Return values
Returns the number of fonts rewritten, or 0 with LastErrorCode 123 for an invalid Options value. GetLastFontOptimizationJSON lists every candidate font with its outcome
Remarks
Eligible fonts are Type0 fonts whose descendant font has a TrueType (FontFile2), OpenType (FontFile3 /OpenType) or bare CFF (FontFile3 /CIDFontType0C) program. The encoding can be Identity-H, Identity-V, a predefined CMap such as UniJIS-UCS2-H or an embedded CMap stream; content strings are decoded through it to find the CIDs in use. For CIDFontType0 fonts glyphs are selected through the CFF charset, and every CID in use is checked to select the same glyph after subsetting. Simple TrueType fonts with an embedded FontFile2 program keep every glyph a viewer could select for the codes in use through the cmap subtables, the encoding glyph names and the post table; only the outlines of other glyphs are removed and every other table stays unchanged. Other simple fonts are not changed
A bare CFF program stays a bare CFF program. When the font descriptor has a /CIDSet, it is rebuilt to list the CIDs the new program still contains
Used glyphs are collected from every page and every form XObject, including annotation appearances. A resource name that refers to different fonts in different places counts for each of them, so a font may keep a few extra glyphs but never loses one in use
The parts of a page /Contents array are read as one stream, so a font selected in one part applies to text in the next. A font program shared by several font dictionaries is reported as shared-program and left unchanged
A font is left unchanged when new text could later be drawn with it or its use cannot be proven: fonts in the interactive form default resources (/DR), fonts named by a default appearance (/DA) string, fonts selected through a graphics state, and fonts named by content that does not parse or that shows text before selecting a font
A rewritten font gets a new six-letter subset tag. Composite glyphs keep the component glyphs they reference. Subsetting uses the OpenType support library (PDFlibOpenType32.dll or PDFlibOpenType64.dll); without it the fonts report subset-failed
Example
PDF.LoadFromFile('report.pdf', '');
PDF.DeletePages(2, 10);
if PDF.OptimizeEmbeddedFonts(1) > 0 then
PDF.SaveToFile('report-small.pdf');
Writeln(PDF.GetLastFontOptimizationJSON);