Skip to main content

Module encoding

Module encoding 

Source
Expand description

PDF simple-font encoding resolver — /Encoding dictionary + /Differences array → 256-entry byte → Unicode map.

ISO 32000-1:2008 §9.6.6.1 “Type 1 Encodings” defines a simple font’s /Encoding as either a single name (WinAnsiEncoding / MacRomanEncoding / MacExpertEncoding / StandardEncoding) or a dictionary with a /BaseEncoding name and an optional /Differences array. The array is a flat sequence of (code, glyph-name, glyph-name, …, code, glyph-name, …): every numeric starts a new run of code points, and every following name token is the glyph at the next consecutive code. Each glyph name is mapped to a Unicode scalar value via the Adobe Glyph List (docs/document/pdf/agl/subset.txt).

Round 28 wires this resolver into the text-extraction path so a simple font whose /Encoding carries /Differences decodes to the correct Unicode payload (matching what pdftotext produces).

§Provenance

ISO 32000-1:2008 §9.6.6.1 (Type 1 Encodings) + §D.2 (Latin character set) for the encoding tables; Adobe Glyph List v2.0 (public document, 5 Sep 2002) for the glyph-name → Unicode mapping. No third-party PDF library SOURCE was consulted.

Structs§

EncodingDifferences
Parsed /Differences array — flat list of (code, name) overrides.
EncodingMap
256-entry byte → Unicode (UTF-8 string) map. Most entries hold a single char but the AGL also defines ligature glyphs whose expansion is multi-character (/fi → “fi”), so the slot has to accommodate a short String. Slots for unassigned bytes hold the empty string — the decoder emits U+FFFD when it sees one.
EncodingOverride
One /Differences array override: at code point code, the rendering glyph is glyph_name.

Enums§

BaseEncoding
The named base encodings ISO 32000-1 §9.6.6.1 + §D.2 define for simple Type 1 / TrueType fonts.

Functions§

apply_encoding_differences
Overlay differences on top of base, returning a fresh map. The base map is left untouched (cheap clone — String allocations are per-entry).
glyph_name_to_unicode
Adobe Glyph List lookup. Returns the UTF-8 expansion of a PostScript glyph name (multi-char for ligatures like /fi, single-char for the common case). Returns None for unknown names — the caller emits U+FFFD as a marker.
parse_encoding_differences
Parse a /Differences array — flat [N name1 name2 … M nameK …].