Per-font character decoder (an opt-in extra) — code → Unicode text, and code → glyph width.
Module pdfFontDecoder (pdfFontDecoder) | Source packages/front/office/pdf/src/extra/font-decoder.js | Deps pdfFont, pdfFontEncoding, pdfFilterDispatch, cmapToUnicode, encodingLookup, encodingAgl, standard14Lookup, fontType1, fontCffProgram, fontSfnt | Worker-safe yes
Turns a PDF Font dict into a decoder honouring these decode paths in priority order: a /ToUnicode CMap (authoritative), then an /Encoding table + the AGL glyph-name hop for simple fonts, then — for composite (Type0) fonts only — the predefined CMap the /Encoding names, when that CMap's input codes are Unicode. The same decoder also gives each code's glyph width, which a page walk uses to place the end of every text piece. It was moved from @awacloud/oconv's pdf reader, which is now built on it (and on pdfPageText); oconv keeps no copy.
Resolve
import { ModuleRuntime } from '@awacloud/fw/core/runtime.js';
import { fw_require, pkg_require, modules, extras } from '@awacloud/pdf';
const runtime = new ModuleRuntime();
runtime.registerAll(fw_require);
runtime.registerAll(pkg_require);
runtime.registerAll(modules);
runtime.registerAll(extras);
const { buildDecoder, decodeShow } = runtime.resolve('pdfFontDecoder');
API
| Member | Signature | Returns | Throws |
|---|---|---|---|
buildDecoder |
(fontDict: object, resolve: (ref) => object) => {subtype, cidBytes, decode, split, width, vertical, widthApproximated, bold} |
a decoder bound to one resolved Font dict; decode(code: number) => string|null; split(bytes: Uint8Array) => [code, length][] (a show string's codes); width(code: number, length?: number) => number (glyph width w0, thousandths of text space); vertical (true for a …-V composite CMap); widthApproximated() => boolean |
— |
decodeShow |
(decoder: {cidBytes, decode, split?}, strBytes: Uint8Array) => {text, decoded, undecodable} |
splits strBytes by decoder.split (else into 1- or 2-byte codes per decoder.cidBytes) and decodes each |
— |
Examples
Decode a show-operator string through a ToUnicode CMap
const pdfParserObj = runtime.resolve('pdfParser').obj;
const cmapToUnicode = runtime.resolve('cmapToUnicode');
const enc = (s) => new TextEncoder().encode(s);
const toUniSrc = cmapToUnicode.buildToUnicode(new Map([[0x41, 'A'], [0x42, 'B']]));
const fontDict = pdfParserObj.dict({
Type: pdfParserObj.name('Font'), Subtype: pdfParserObj.name('Type1'),
BaseFont: pdfParserObj.name('Helvetica'),
ToUnicode: pdfParserObj.stream(pdfParserObj.dict({}), enc(toUniSrc))
});
const decoder = buildDecoder(fontDict, (ref) => ref);
decodeShow(decoder, new Uint8Array([0x41, 0x42]));
// { text: 'AB', decoded: 2, undecodable: 0 }
Notes
- Decode priority:
/ToUnicodefirst; when absent (or it has no entry for a code), the/Encodingtable +encodingAgl.glyphNameToUnicodehop is tried for simple (non-Type0) fonts only. - Symbol and Wingdings private-use text: a ToUnicode entry that is one character in 0xF020–0xF0FF, for a font whose family (base name less a subset tag and a
-or,suffix) isSymbolorSymbolMT, decodes as the code (less 0xF000) read throughSymbolEncodingand the AGL (U+F0B7 →•); forWingdings, the two pairs Word itself writes are read (U+F0A7 → U+25AA▪, U+F0FC → U+2713✓). Any other code, family or entry keeps its ToUnicode text. - A simple font's
/Encodingis resolved before it is read: the entry itself may be an indirect reference (pdfLaTeX writes/Encoding n 0 Ron every Type 1 font), and so may the/Differencesand/BaseEncodinginside an encoding dictionary. Such a font decodes like one with a direct/Encoding; it is not recorded astext/font-unresolved. - A simple font's implicit base encoding (no
/Encoding, or an encoding dictionary without/BaseEncoding) isStandardEncoding, except for a symbolicType1orMMType1font (/Flagsbit 3) that embeds its program:/FontFile(Type 1, read byfontType1with/Length1and/Length2),/FontFile3Type1C, or/FontFile3OpenTypewith aCFFtable (read byfontCffProgram). For such a font the base is the program's built-in encoding (ISO 32000-2 §9.6.6.1, Table 112), and/Differencesapply over it. This covers pdfLaTeX's Computer Modern fonts, which carry no/Encodingand no/ToUnicode. The program is read once per stream object. A program that does not read, a CID-keyed CFF, a nonsymbolic font, and a TrueType font keepStandardEncoding. - A
SymbolorZapfDingbatsType1font drawn without an embedded program (no/FontFile,/FontFile2or/FontFile3) and with no base in/Encodingtakes its built-in encoding as the base,SymbolEncodingorZapfDingbatsEncoding(ISO 32000-2 §9.6.2.2),/Differencesover it. - A font whose family (its base name less a subset tag and a
-or,suffix) names Dingbats resolves the glyph names of its encoding throughencodingAgl.dingbatsNameToUnicode: the ITC Zapf Dingbats Glyph List (a73→ U+25A0), then the AGL. Every other font reads names throughglyphNameToUnicodeonly, since other fonts useaNNnames for unrelated glyphs. - Standard CMap: a
Type0font whose/Encodingnames aUni{GB,CNS,JIS,KS}-{UCS2,UTF16}[-HW]-{H,V}predefined CMap (ISO 32000-2 §9.7.5.2) decodes a code without a ToUnicode entry as the UTF-16 code unit it is; a surrogate half is counted undecodable.Identity-H/Identity-V(codes are CIDs, not Unicode) and the legacy RKSJ/EUC/… CMaps (tables not bundled) decode nothing here — a composite font in that position with no ToUnicode stays an honesttext/undecodableloss (CID→GID→Unicode font-program walking is out of tier-1 scope). - A code neither path resolves is counted undecodable and omitted from the text — never silently kept as a wrong glyph, never dropped without a count (
decodeShow's own return shape). - Glyph widths (
width), by font kind:- simple fonts:
/Widthsindexed from/FirstChar. A code outside the array takes the descriptor's/MissingWidth(0 when absent, the spec default). A Type 3 font's widths are scaled by its/FontMatrix. - composite (
Type0) fonts: the descendant CIDFont's/Warray, in both forms (c [w1 w2 …]andcFirst cLast w), with/DW(default 1000) for CIDs it does not list, at each code's CID: the code itself underIdentity-H/Identity-V; under an embedded CMap stream, its mapping through@awacloud/fonts'cmapCid, its/UseCMapparent under it (a stream, or an Identity CMap by name), a code it maps to none showing CID 0 (ISO 32000-2 §9.7.6.3); the writing mode from the CMap's/WMode. Under a predefined CMap by another name (none is bundled) the code → CID map is unknown here, so/DWis used. - Standard 14 fonts with no
/Widths: the Adobe Core 14 AFM widths shipped by@awacloud/fonts(standard14Lookup), looked up by the glyph name the font's encoding gives the code.SymbolandZapfDingbatsare looked up by code.
- simple fonts:
boldis true when the font'sBaseFont(a Type 0 font's descendant's too), its subset tagABCDEF+removed, names a bold weight (Bold,Black,Heavy,Semibold,Demibold,Demi,Extrabold,Ultrabold, any case, or ends with URW's suffix-Medior-MediItal, its Nimbus Roman No9 L bold), or its font descriptor has/FontWeight600 or more or the ForceBold flag (/Flagsbit 19);oconvPdfToIr's opt-in heading inference reads it.- When a width cannot be read from the font, a declared fallback is used: 500 thousandths for a simple font, 1000 for a composite one.
widthApproximated()then turnstrue, and a page walk (pdfPageText) recordstext/width-approximated. The cases are:/Widthsabsent on a font that is not one of the Standard 14, a malformed width array or entry, a missing descendant font, a/Wtable under a predefined CMap other thanIdentity-H/Identity-V, and a named Standard 14 glyph missing from the AFM table. A malformed width structure never throws. cidBytesis2forType0fonts,1otherwise.splitreads a show string's codes: by a composite font's embedded CMap codespace when its codes are not all 2 bytes long (ISO 32000-2 §9.7.6.2: the shortest length at which every byte lies within a range's bytes; with no match, the shortest range's length), elsecidBytesat a time.decodeShowandpdfPageTextsplit by it;Twapplies to a 1-byte code 32.
See also
pdfPageText— the page walk built on this decoder- Extras index