A Type 1 font is a PostScript program. Sent to a printer it is plain text, but its second half, the part with the glyph outlines, is encrypted and so is binary. PFB (“printer font binary”) stores that program in the form used on PCs: the text and the binary parts in separate segments, each with a small header saying how long it is.
Segments
A PFB file is a sequence of segments. Each starts with a header:
| Offset | Size | Field | Meaning |
|---|---|---|---|
0 | 1 | marker | Always 0x80. |
1 | 1 | type | 1 text (ASCII), 2 binary, 3 end of file. |
2 | 4 | length | Number of data bytes that follow, little-endian. Absent for type 3. |
The segment data follows the header directly, and the next header follows the data. A normal font has three data segments and the end marker:
080 01+ length the clear text part: the%!PS-AdobeFont-1.0line, comments, the font dictionary, ending withcurrentfile eexectext…80 02+ length the eexec-encrypted part: the Private dictionary, Subrs and CharStringsbinary…80 01+ length the trailer: 512 ASCII zeros in lines, thencleartomarktext…80 03end of file2 bytes
A file may split a part over several segments of the same type; readers join consecutive segments. Lines in the text segments may end in CR, LF or CR LF (the example below uses CR alone).
PFA and other wrappers
- PFA (“printer font ASCII”) is the same program with no segment
headers, the encrypted part written as lines of hexadecimal digits.
Converting PFB to PFA means dropping the headers and hex-encoding the type 2 data. A
reader can tell the two forms apart from the four bytes after
currentfile eexecand its line end: if they are all hex digits, the encrypted part is in hex. - On the classic Mac OS the same segments were kept as
POSTresources in a printer font file of typeLWFN: see the Macintosh form.
The Macintosh form: POST resources (LWFN)
A Macintosh printer font file (file type LWFN) keeps the font program in the
resource fork, as resources of type POST. Adobe Technical Note #5040
(Supporting Downloadable PostScript Language Fonts, section 3.2) describes them.
The resources are numbered from 501 and the first byte of each says what it holds:
| Type | Holds | What a reader does |
|---|---|---|
| 0 | A comment | Not sent to a printer: skipped. |
| 1 | ASCII text | The clear text parts; sent as they are. Becomes a PFB type 1 segment. |
| 2 | Binary data | Stored as binary and sent to a printer as hexadecimal. Becomes a PFB type 2 segment. |
| 3 | End-of-file marker | An end-of-file indication is sent to the interpreter: the font program ends. |
| 4 | “The rest of the font is in the data fork” | Reading stops here and the remaining program is read from the data fork of the file. Adobe did not use this type. |
| 5 | End of the font program | Downloading stops (no end-of-file indication is sent). Becomes the PFB end marker. |
A font program always begins with resource ID 501 and continues in sequence; the first type 5
resource, or the first ID that cannot be found, is its end. The note gives the type as the
first byte of the resource. Real files have a second byte, 00, before the data
(FreeType’s ftmac.c, which rebuilds a PFB from the resources, skips two bytes of
every POST resource), and so does the viewer. Consecutive resources of one type
make one segment, so the font on the Macintosh is the PFB file split at resource boundaries,
and a PFB is rebuilt by writing 80, the type and the length in front of each
run and 80 03 at the end.
The resource fork itself can reach a reader in four ways, and the viewer reads each:
- A bare resource fork, as
.rsrcfiles and the..namedfork/rsrcof a file on macOS hold it: a 16-byte header (offsets and lengths of the resource data and the map), the resource data (a 4-byte length and the bytes of each resource) and the map (a copy of the header, then the type list and the reference list that give each resource’s type, ID and data offset). See the resource fork page. - AppleSingle and AppleDouble files, which carry the fork as entry 2 (AppleSingle and AppleDouble).
- MacBinary: a 128-byte header, then the data fork padded with zeros to a
multiple of 128 bytes, then the resource fork. The header has a zero in byte 0, a name length
in byte 1 and the name after it, the file type (
LWFN) at byte 65, and the lengths of the two forks at bytes 83 and 87. A reader finds the resource fork at 128 + the data fork length rounded up to 128 (the checks FreeType makes inftobjs.c: a zero first byte, zero bytes 74 and 82, a name length of at least 1 (FreeType stops at 33, the longest Macintosh file name is 31; the viewer allows the header’s 63) and data and resource fork lengths below 231).
When a type 4 resource is met, the viewer joins what a printer would receive: the text
resources, the binary resources as hexadecimal, then the data fork, which is read as a PFA.
Sources read: Adobe TN #5040 (PDF) and FreeType’s src/base/ftmac.c and
ftobjs.c (raw files from the FreeType repository, read, not copied). The MacBinary
specification itself (versions I to III) was not reachable as a single raw file: the layout above is
only what FreeType relies on.
eexec encryption
The PostScript operator eexec decrypts the rest of the file and runs it.
The cipher is a simple stream cipher on bytes with a 16-bit key
r, started at 55665, and two constants
c1 = 52845 and c2 = 22719.
For each encrypted byte c:
plain = c XOR (r >> 8)
r = ((c + r) × 52845 + 22719) mod 65536
Encryption is the same with the roles swapped: c = plain XOR
(r >> 8), then r is updated from c. The first
four decrypted bytes are random padding and are thrown away; what follows is
PostScript text that defines the Private dictionary and the glyphs, using binary
strings for the charstrings (which are encrypted a second time; see
charstring encryption). The
decrypted text ends with mark currentfile closefile, which stops
eexec.
Trailer
After the encrypted part, the clear text resumes with 512 zeros (usually eight lines
of 64) and cleartomark. The zeros are there for printers that read past
the end of the encrypted data; cleartomark removes everything back to
the mark placed before closefile. Some files add
%%EndResource or %%EOF lines.
Synthetic and hybrid fonts
Because a Type 1 font is a PostScript program, some have been arranged in more complicated ways than the layout above. Chapter 9 of the book describes two forms used in Adobe’s fonts and says “Type 1 font parsers must be able to recognize these forms correctly”.
Synthetic fonts (9.1)
A synthetic font is a font program that is a modification of another, by a different transformation matrix:
obliqued, expanded and condensed fonts are the examples. It shares the large CharStrings dictionary of
a “basic” font already loaded in the PostScript interpreter, so the extra storage is only its own font dictionary. It
needs its own UniqueID, so it builds its own Private dictionary, which “must be a clone of the basic
font’s Private dictionary except for the UniqueID entry”. There is no guarantee that the basic font is loaded, so a
synthetic font must also carry a copy of the basic font program. Its PostScript checks whether the basic font is in the
FontDirectory dictionary (with known), whether its FontType is 1 and, if the
UniqueID is known, whether it is the same. If so it discards the copy it holds (by using
readstring on itself) and takes the CharStrings from the loaded font; if not it runs the
embedded basic font and builds itself from that, creating two font dictionaries. How to recognise one:
“A synthetic font program must use the token “FontDirectory” after the first occurrence of the token “/Private””
(the first occurrence is where the synthetic font defines its own Private dictionary, not where the embedded basic
font does).
Hybrid fonts (9.2)
A hybrid font contains two sets of outlines, one chosen by the resolution of the device: full fidelity at high
resolution, straighter edges at low resolution, for designs with subtle curves beyond what flex can do (Adobe’s
Optima is the example). In it “the Subrs entries and the CharStrings entries occur several times”: all the Subrs
entries for the low-resolution set come first in a group, as in a normal font, then all those for high resolution,
and the groups are separated by PostScript that checks the resolution and ignores one group with save and
restore; the CharStrings entries (which come last, as always) are grouped and selected the
same way. So the same glyph name appears twice in CharStrings, and that is correct. How to
recognise one: the PostScript sets a value in an identifier named hires, and “if the token
“hires” occurs in a Type 1 font program prior to the first occurrence of “/Subrs”, it must be a hybrid font program”
(chapter 10 adds the case of hires after the first /Private).
The viewer recognises both by those tokens (also when they are inside a procedure, which it does not run). For a synthetic font it says so in the Font tab and in a note. For a hybrid font it does not report the repeated glyph names as problems; the tree and the CharStrings table list every entry and mark each repeated name as the first (low resolution) or second (high resolution) outline set, each glyph is read from its own entry, the earlier Subrs array is shown in the tree, and the sample text, the glyph sheets and the widths use the later set, which is the one the font dictionary ends up with. It does not run the resolution test, so it cannot show which set a printer would use.
Adobe Type Manager parsing rules
Chapter 10 of the book: some Type 1 interpreters, such as the one in Adobe Type Manager, “do not incorporate a complete PostScript interpreter”. They separate the text into tokens by the PostScript rules (ignoring comments and the binary contents of charstrings), check the tokens at the top level (not inside procedure bodies) for certain keywords and act on them. Type 1 fonts “must strictly conform to these parsing rules in addition to being legal PostScript language programs”. The rules:
- Individual tokens and charstrings may not exceed 65,535 characters.
- The initial portion of the font program is assumed to contain names to be inserted in the font dictionary. If
eexecappears, the text following must be encrypted, and no assignment of a value to a name may occur in the plaintext that follows the encrypted portion. - All font dictionary assignments (except
CharStringsandPrivate) must take place before the first/Private; all assignments after it (except theCharStringscontents) must be in the Private dictionary; andeexec“must occur before the first occurrence of the token /Private”. - Simple values (integer, real, string, name, Boolean) must follow their name immediately as the next
token, written explicitly:
/FontType 1 defis right, but1 /FontType exch defand/FontType 2 1 sub defare not, though legal PostScript. Booleans are only the tokenstrueandfalse. - Arrays must immediately follow the name they are assigned to, begin with
[or{and end with the matching]or}; numeric contents must be single tokens. - Encoding: the tokens after
/Encodingmay beStandardEncoding def; for a special encoding the assignments are the repeated sequencedup index /name put(the parser skips to the firstdupafter/Encoding), followed bydeforreadonly, which may not occur inside the sequence. - The only binary data in a font program are in
SubrsandCharStringsentries. - Subrs: the integer after
/Subrsmust be exactly the number of entries; only Subrs entries may be defined until the array is complete; each isdup index nbytes RD bytes NPwith exactly one blank betweenRDand the bytes. - CharStrings: the integer after
/CharStringsmust be greater than or equal to the number of entries; each is/name nbytes RD bytes ND, again with exactly one blank afterRD; only CharStrings entries may be defined until the dictionary is complete; the entries are followed by the tokenend, which may not occur inside them; and no assignments may occur after the CharStrings dictionary is complete. - The token
FontDirectoryafter the first/Privatemeans a synthetic font; the tokenhiresafter the first/Privatemeans a hybrid font.
The viewer reports these as notes, not problems, since a PostScript interpreter reads such a font
either way and many files do not follow them to the letter: /Private before eexec, a token or a
charstring over 65,535 characters, RD not followed by exactly one blank, a Subrs count different
from the entries, and a CharStrings count below the entries. It also notes: a simple value that does
not follow its name as the next token (10.1); an array that does not follow its name or does not begin with
[ or { (10.2); an Encoding that is not StandardEncoding def or the repeated
dup index /name put ended by def or readonly; a font dictionary entry
(other than CharStrings and Private) assigned after the first /Private (the
/Private inside a multiple master font’s /Blend dictionary, TN 5015, does not count); text other
than dup index nbytes RD bytes NP between /Subrs and the end of its array; text other than
/name nbytes RD bytes ND between /CharStrings and its end; binary data
(RD) outside those two; an assignment after the CharStrings dictionary is complete; and an assignment in
the plain text after the encrypted part. These checks are skipped for synthetic and hybrid fonts, whose PostScript is
arranged differently. The viewer builds its dictionaries by reading
the PostScript as text and does not run it, so a simple value written by computation
(/FontType 2 1 sub def) does not give a value, and the problem shown is that the def found no
name for it. A numeric array that holds an operator between its numbers (so its numbers are not all single tokens) is noted with the arrays.
Example
TIR_____.PFB, Adobe’s Times Roman (version 001.007, created 20 March
1990) as shipped with Acrobat Reader for Windows, 32,976 bytes:
| File offset | Bytes | Meaning |
|---|---|---|
0x0000 | 80 01 B8 06 00 00 | Text segment of 0x6B8 = 1,720 bytes. |
0x0006 | 25 21 50 53 2D … | %!PS-AdobeFont-1.0: Times-Roman 001.007, then the font dictionary, ending currentfile eexec and a CR at 0x6BD. |
0x06BE | 80 02 F0 77 00 00 | Binary segment of 0x77F0 = 30,704 bytes. |
0x06C4 | 5A A4 F7 12 55 50 … | The eexec-encrypted Private dictionary and charstrings. |
0x7EB4 | 80 01 14 02 00 00 | Text segment of 0x214 = 532 bytes: 512 zeros, line ends and cleartomark. |
0x80CE | 80 03 | End of file, at 32,974 + 2 = 32,976 bytes. |