encoding

package
v0.2.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Aug 8, 2026 License: MIT Imports: 3 Imported by: 0

Documentation

Overview

Package encoding maps simple-font character codes to glyph names and Unicode.

A simple font addresses glyphs with single bytes, and the meaning of a byte comes from a base encoding (ISO 32000-2 Annex D) optionally modified by a /Differences array (§9.6.5.1). Resolving a code therefore takes two steps: code to glyph name, then glyph name to Unicode via the Adobe Glyph List.

Both steps are needed because a large share of real fonts ship no /ToUnicode CMap: 55 of the 134 distinct simple fonts across this repo's corpus, every one of them WinAnsiEncoding. An extractor that relies on /ToUnicode alone returns nothing for those, which is one of the ways existing tools lose text. The count is measured rather than estimated, by TestCorpusFontsWithoutToUnicode- HaveAnEncoding in cmd/pdfspec, which also asserts every one of them is recoverable through this package.

This package holds no PDF object knowledge: it takes glyph names and byte codes, not dictionaries. Reading /Encoding and /Differences out of a font dictionary belongs to the font package, which keeps the tables here testable against the Unicode data they claim to implement.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func GlyphRune

func GlyphRune(name string) (rune, bool)

GlyphRune returns the single Unicode value of a glyph name, or false when the name is unknown or stands for more than one character.

GlyphText is the resolver; this is the narrow form for callers that genuinely need one rune.

func GlyphText

func GlyphText(name string) (string, bool)

GlyphText returns the characters a glyph name stands for.

Four rules apply in order, per the Adobe Glyph List specification:

  1. A name in the glyph list maps to its listed value.
  2. A uniXXXX or uXXXX[XX] name maps to those code points directly. Producers emit these for glyphs with no conventional name, and they are the reason a table alone is not enough. The uni form may carry several code points, one per four digits.
  3. A name with a suffix after a period — "a.sc", "one.oldstyle" — maps as its base name does. Subsetting tools add these freely, and dropping the suffix recovers text that would otherwise be lost entirely.
  4. A name joined by underscores — "f_f", "f_t" — is a ligature, and maps to its components' characters in order. Both of those appear in this repo's corpus, and "f_t" has no precomposed code point at all, so a resolver that cannot return multiple characters cannot represent it.

A name of the form "gXX" or "cidXX" deliberately does not resolve: those identify a glyph by index in the font program, carry no character meaning, and guessing would emit plausible wrong text rather than nothing.

Types

type Encoding

type Encoding struct {
	// contains filtered or unexported fields
}

Encoding is a code-to-glyph mapping: the 256 glyph names of a base encoding, with any /Differences already applied.

The zero Encoding maps nothing, which is the correct starting point for a symbolic font that supplies its own /Differences for every code it uses.

func Base

func Base(name string) (*Encoding, bool)

Base returns the named base encoding, or false if the name is not one of the four defined by Annex D.

The returned Encoding is a copy, so a caller may apply /Differences to it without disturbing the shared table.

func Empty

func Empty() *Encoding

Empty returns an Encoding that maps no code, for a symbolic font whose codes mean only what its /Differences say.

func Standard

func Standard() *Encoding

Standard returns StandardEncoding, the fallback when a font names no encoding and is not symbolic (§9.6.5.1).

func (*Encoding) Clone

func (e *Encoding) Clone() *Encoding

Clone returns a copy, so /Differences can be applied without mutating a shared table.

func (*Encoding) Glyph

func (e *Encoding) Glyph(code byte) string

Glyph returns the glyph name for a code, or "" when the code is unmapped.

func (*Encoding) Rune

func (e *Encoding) Rune(code byte) rune

Rune returns the single character a code stands for, or 0 when the code is unmapped or stands for more than one character.

Convenience for the common case and for tests. Anything extracting text should use Text, or it silently loses every ligature.

func (*Encoding) Set

func (e *Encoding) Set(code byte, glyph string)

Set assigns a glyph name to a code and resolves its text. This is how one entry of a /Differences array is applied.

func (*Encoding) Text

func (e *Encoding) Text(code byte) string

Text returns the characters a code stands for, or "" when the code is unmapped or its glyph name has no known Unicode value.

A string rather than a rune because a glyph is not always one character: an "ffi" ligature is one glyph and three characters, and the AGL names such glyphs by joining their components with underscores. Returning a rune would force this package to drop them, which is how "efficient" becomes "ecient".

The empty string rather than an error, and rather than U+FFFD: an unmapped code is routine in a symbolic font, and the caller decides whether to drop the glyph or fall back to a /ToUnicode entry.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL