Each glyph in the CharStrings
dictionary, and each subroutine in Subrs, is a charstring: a compact
program of numbers and operators that draws the outline with straight lines and cubic
Bézier curves and marks the stems for hinting. It works like a tiny stack machine:
numbers are pushed, each operator takes its arguments from the stack, and the
drawing and hint operators then clear it. This is the Type 1 format; the CFF table of
OpenType fonts uses the related
Type 2 format.
Charstring encryption
Every charstring is encrypted on its own, with the same cipher as eexec but a starting key of 4330:
r = 4330
for each byte c:
plain = c XOR (r >> 8)
r = ((c + r) × 52845 + 22719) mod 65536
The first lenIV decrypted bytes (4 unless the Private dictionary says
otherwise) are random and are dropped (Type 1 specification, sections 7.1 and 7.3).
So a glyph is decrypted twice: once as part of the eexec section, and again as a
charstring.
Numbers
| First byte v | Bytes | Value |
|---|---|---|
| 0 to 31 | 1 or 2 | An operator, not a number (12 starts a two-byte operator). |
| 32 to 246 | 1 | v − 139: −107 to 107. |
| 247 to 250 | 2 | (v − 247) × 256 + w + 108: 108 to 1131. |
| 251 to 254 | 2 | −(v − 251) × 256 − w − 108: −108 to −1131. |
| 255 | 5 | The next four bytes as a signed 32-bit big-endian integer. |
There are only integers; a fraction is made with the div operator. A number
with an absolute value over 32,000 must be followed by a number and div, so that the
quotient is under 32,000 (section 6.2: “Numbers with absolute values greater than 32,000 must be
followed by a div operator such that the result of the div is less than 32,000”), for example
40000 2 div.
Operators
Arguments are listed in the order they are pushed. Coordinates are relative to the current point (“d” values) unless stated.
| Code | Operator | Arguments | Effect |
|---|---|---|---|
13 | hsbw | sbx wx | Starts the glyph: left side bearing sbx (the current point becomes (sbx, 0)) and advance width wx. It does not put the point in the path: the first point comes from an rmoveto. Either hsbw or sbw is used once, as the first command; in a non-marking glyph such as the space the left side bearing point should be (0, 0). |
12 7 | sbw | sbx sby wx wy | Like hsbw with vertical components; hsbw should be used when both y values are 0. |
1 | hstem | y dy | Horizontal stem hint from y to y + dy, relative to the side bearing’s y. Zones of one direction within one set of hints may not overlap (use hint replacement to avoid it); an hstem may not have its top at or in a top zone and its bottom at or in a bottom zone (section 6.5). |
3 | vstem | x dx | Vertical stem hint, relative to the side bearing’s x; vertical zones may not overlap either. |
12 2 | hstem3 | y0 dy0 y1 dy1 y2 dy2 | Three evenly spaced horizontal stems (as in “≡”) to be kept evenly spaced. Sorted by y they are the lowest (ymin), middle and highest zones, and dymin = dymax, and the distance from the centre of the bottom stem to the centre of the middle one equals the distance from there to the centre of the top one. A charstring that uses hstem3 must not use hstem, and must use the same hstem3 in every hint set if it replaces hints. |
12 1 | vstem3 | x0 dx0 x1 dx1 x2 dx2 | Three evenly spaced vertical stems (as in “m”), with the same two restrictions (dxmin = dxmax, equal centre-to-centre distances), not mixed with vstem, and the same one in every hint set. |
12 0 | dotsection | — | Brackets a small dot or similar feature, so it is not hinted away; used in pairs: the first right after the first rmoveto that begins the dot, the second right after the closepath that finishes it (section 8.2). |
21 | rmoveto | dx dy | Moves to a new point, starting a new contour. |
22 | hmoveto | dx | Horizontal move. |
4 | vmoveto | dy | Vertical move. |
5 | rlineto | dx dy | Line. |
6 | hlineto | dx | Horizontal line. |
7 | vlineto | dy | Vertical line. |
8 | rrcurveto | dx1 dy1 dx2 dy2 dx3 dy3 | Cubic curve; each point relative to the previous one. |
30 | vhcurveto | dy1 dx2 dy2 dx3 | Curve that starts vertical and ends horizontal (0 dy1 dx2 dy2 dx3 0 rrcurveto). |
31 | hvcurveto | dx1 dx2 dy2 dy3 | Curve that starts horizontal and ends vertical (dx1 0 dx2 dy2 0 dy3 rrcurveto). |
9 | closepath | — | Closes the contour with a line back to its start. Unlike the PostScript closepath it does not move the current point, so the next rmoveto is relative to the point before the closepath. A subpath meant to close with a zero-length line must really be zero length, or it makes a “spike”; the book recommends ending every subpath with closepath (stroked outlines join wrongly otherwise). |
14 | endchar | — | Ends the glyph. |
12 6 | seac | asb adx ady bchar achar | Standard encoding accented character: draws the glyphs whose StandardEncoding codes are bchar (base) and achar (accent), the accent shifted by (adx, ady) allowing for its side bearing asb. Ends the glyph, and is the last command. Rules (section 6.4): asb must equal the sidebearing in the accent’s own hsbw; the hsbw or sbw that begins the accented glyph must equal the base’s; the base and the accent must sit at their StandardEncoding codes in the font’s own Encoding; and FontBBox must be large enough for both parts (and not all zeros). |
12 12 | div | num1 num2 | Pushes num1 ÷ num2; the only arithmetic. |
10 | callsubr | … subr# | Runs Subrs[subr#] and continues; the stack is shared. |
11 | return | — | Returns from a subroutine. |
12 16 | callothersubr | arg1 … argn n othersubr# | Calls PostScript procedure OtherSubrs[othersubr#] with n arguments; its results are fetched with pop. |
12 17 | pop | — | Moves one result from the PostScript stack to the charstring stack. The pop commands “must immediately follow the callothersubr command with no intervening commands”, and there may be no more of them than the n arguments (section 8.1). |
12 33 | setcurrentpoint | x y | Sets the current point to absolute coordinates; used only after a flex. |
Every other code from 0 to 31, and every other second byte after 12, is undefined.
A glyph must begin with hsbw or sbw, and must end with
endchar or seac.
Hint replacement and flex
- Hint replacement. Stem hints may overlap in a complex glyph, so a
charstring can change them part-way with
subr# 1 3 callothersubr pop callsubr, where Subrs[subr#] holds the newhstemandvstemset.OtherSubrs3 hands subr# back, so the new hints are run; an interpreter that cannot replace hints hands back 3 instead, and Subrs 3 is justreturn(section 8.1). The hint Subr may be any entry except 0 to 3 and holds only stem hints. - Flex. A pair of shallow curves (such as a cupped serif) can be
marked so that at small sizes it is drawn as a straight line. The charstring calls
Subrs 1, then gives seven points each followed by a call to Subrs 2, then pushes
the flex height in hundredths of a pixel and the end point and calls Subrs 0, which
uses
OtherSubrs0 andsetcurrentpoint. Each of the seven points is anrmoveto: the reference point first, then the six points of the two curves. A flex height of 50 (half a pixel) is the usual value for cupped serifs (section 8.3). A curve pair can be a flex only if: it is exactly two Bézier segments; the outer end points are at the same x (or the same y), precisely vertical or horizontal; the joining point and the control points next to it are all at the horizontal (or vertical) extreme of the double curve; and the flex height, the difference in x (or y) between an outer end point and the joining point, is 20 units or less. The joining point should lie exactly on the flat edge of an alignment zone when the flex is in one.BlueShiftmust be larger than the highest flex of the font, so with the default 7 it must be set explicitly when a flex is more than 6 units high (a flex 8 high needsBlueShift9; the book’s example with a height of 10 needs 11).
Rules the viewer checks
The viewer applies the book’s rules for charstrings and shows what breaks them. A rule is a
problem when a font that breaks it cannot be read or run correctly; it is a
note (in the Notes section, quoting the book, with the first glyph and the count of glyphs)
when the book states the rule but it concerns only how well the hints work and real fonts break it.
The Computer Modern fonts, for example, begin some charstrings with another command before hsbw,
and have overlapping stem zones in some glyphs, and they work.
| Rule | Source | Reported as |
|---|---|---|
A number over 32,000 not followed by a divisor and div that bring it under 32,000 | 6.2 | problem |
pop not immediately after callothersubr (or another pop); more pops than the call’s n | 8.1 | problem |
The charstring has no hsbw or sbw; no endchar; the last subpath not closed; a command that is not defined; the stack deeper than 24; subroutine calls deeper than 10 | 6.1, 6.4, 8 | problem |
hsbw or sbw not the first command, or used twice | 6.4 | note |
| Overlapping stem zones of one direction in one hint set | 6.4 | note |
hstem3 or vstem3 with unequal outer zones or unequal centre distances; mixed with hstem or vstem; changed by hint replacement | 6.4 | note |
| An hstem whose bottom is in a bottom zone and whose top is in a top zone | 6.5 | note |
seac: asb different from the accent’s sidebearing; metrics different from the base’s; a command after seac; base or accent not at its StandardEncoding position in the font’s Encoding; FontBBox all zeros | 6.4 | note |
A flex whose outer end points are at neither one x nor one y, whose joining point and control points are not at the extreme, or that is over 20 units high; BlueShift not above the highest flex | 8.3, 5.7 | note |
| A hint replacement subroutine at an index from 0 to 3, or holding more than stem hints | 8.1 | note |
dotsection not in a pair, not right after the first rmoveto of the dot, or the second not right after closepath | 8.2 | note |
Counter control (OtherSubrs 12 and 13) not right after hsbw or sbw, called twice for the 13, or with more than 22 arguments | TN 5015 2.2 | note |
The rules for the Private dictionary are on the dictionaries page.
Hinting at a pixel size
The hints do nothing to the outline in character space. They tell a rasteriser, at a given size, where to put the edges of stems and the tops and bottoms of letters on the pixel grid. The viewer applies them when “Apply the hints” is on, at the pixel size chosen for the sample text and for the glyph sheets, following Adobe Type 1 Font Format chapters 5, 6 and 8 and Technical Note #5015 section 2. With the option off the outlines are drawn as designed.
- Stem widths. A stem of width w units is w × scale
pixels wide. When a standard width (
StdHWandStemSnapHfor horizontal stems,StdVWandStemSnapVfor vertical ones) is within one pixel of it, the standard width is used instead (book, 5.9: “when the difference … is small, the standard width is used”; “small” is read here as under one pixel). The width then rounds to a whole number of pixels, never fewer than one. WithForceBold true(book, 5.10: where stems would normally be one pixel thick, an interpreter “may thicken the stem”) a stem that comes out one pixel thick is made two pixels thick; withfalseor no entry nothing is thickened. - Alignment zones (5.3 to 5.5). The first
BlueValuespair is the baseline zone, whose flat edge is its upper value; the other pairs are top zones with the lower value as the flat edge; eachOtherBluespair is a bottom zone with the upper value as the flat edge. A horizontal stem whose bottom edge lies in a bottom zone, or whose top edge lies in a top zone (zones widened byBlueFuzz, default 1, on each side), is put on the flat edge, rounded to a pixel. A zone ofFamilyBluesorFamilyOtherBluesreplaces the font’s own flat edge when they are less than a pixel apart. The stem’s other edge follows at the width of step 1. Stems in no zone are centred on the pixel grid by their middle. - Overshoot (5.6 and 5.7). Below the size where one character unit is
BlueScalepixels (default 0.039625, about 39.6 pixels per em) every feature in a zone is drawn at the flat edge. At and above it, a feature beyond the flat edge byBlueShiftunits (default 7) or more overshoots by its size in pixels, at least one pixel; one closer to the flat edge overshoots only if its device distance is at least half a pixel. - Ghost stems (6.5). A letter such as a sans serif capital I has no horizontal stem at the
baseline or the cap height, so the book gives it “ghost” stems for those places, “created with a stem height of
20 or 21—either is acceptable”, describing a y range inside the glyph (not above a top or below a bottom). A ghost
has no width: it only puts its edge in its zone. The book gives no way to tell a ghost from a real stem 20
units high, so the viewer takes a horizontal stem of height 20 or 21 as a ghost when its bottom edge is in a
bottom zone or its top edge in a top zone (zones widened by
BlueFuzz), and as a real stem otherwise. Other rasterisers write a ghost as height −20 (bottom edge at y) or −21 (top edge at y); those are not in the book but are read as ghosts too. - Hint replacement. Each
OtherSubrs3 call starts a new set of stems for the rest of the path; a point is placed by the set in force where it is drawn, and a curve or line starts where the segment before it ended. - Other points. A coordinate that is not on a stem edge moves by linear interpolation between the edges around it, and by the nearest edge’s shift outside them. The book does not say how other points move; this is the usual way a hinting rasteriser keeps the outline whole.
- Flex (8.3). The two curves of a flex are drawn as one straight line when the flex height
(the distance from the joining point to the line between the outer end points, after hinting) is under the
height parameter of
OtherSubrs0, in hundredths of a pixel; otherwise as the two curves. - Counters. The three stems of an
hstem3orvstem3, and the stems of each counter control group (OtherSubrs12 and 13), are placed so that counters of equal size get the same number of pixels; unequal ones share the pixels in proportion (TN 5015 2.1 and 2.4). A group between two stems that sit in zones fills the room between them. The order of the groups in the data is their priority (TN 5015 2.4: it “determines the order in which the groups are allocated white space pixels”): the viewer places the first group’s stems and counters first, and a later group that shares a stem with an earlier one keeps that stem where it is and spreads only its own other counters, so the earlier group’s counters are the last to collapse. In a font of language group 1,ExpansionFactor(default 0.06) limits how far the counter adjustment may change the size of the glyph’s bounding box in each direction that has counter control; when the hinted extent would differ from the scaled design by more than that fraction, the viewer moves the hinted positions back towards the scaled ones until it does not, accepting irregular counters, as TN 5015 2.5 allows. (TN 5015 adds that the limit in y may be constrained further byBlueValues; the zones are applied as described in step 2 and nothing more is done for it.) The three stems of anhstem3/vstem3come after the counter control groups in priority. - Advance width rounds to whole pixels, and the glyph is placed at a whole pixel.
The book and the Technical Note give no algorithm for how much a stem is thickened by ForceBold (“may
thicken”), for how the groups share the pixels beyond their order, or for how a rasteriser should bring a box back
inside ExpansionFactor; the choices above (two pixels, earlier groups first, a straight pull back) are the
viewer’s own, and they are labelled so. Not done, because the book gives no rule or because a particular rasteriser
chose it: anything ATM, FreeType or Ghostscript do that the book does not say, such as
moving dotsection parts or their exact rounding of stem positions, so a pixel here can differ from
one of theirs; and the case where overshoot is kept by BlueShift below the BlueScale
size, which the book’s wording (“for characters larger than the BlueScale size”) does not cover.
Anti-aliasing is the site’s ordinary coverage fill on the hinted outline, not a rasteriser’s.
Example
A square 500 units wide, written out as charstring bytes before encryption (an encrypted copy would start with four random bytes), with a left side bearing of 50 and an advance width of 600:
BD F8 EC 0D 50 600 hsbw 189 − 139 = 50; (248 − 247) × 256 + 236 + 108 = 600
8B 8B 15 0 0 rmoveto 139 − 139 = 0; the contour starts at (50, 0)
F8 88 06 500 hlineto (248 − 247) × 256 + 136 + 108 = 500
F8 88 07 500 vlineto
FC 88 06 -500 hlineto −(252 − 251) × 256 − 136 − 108 = −500
09 closepath back down to (50, 0)
0E endchar