ńņš. 13 


ā“
Baseline
Reference pointā’ ā¢
ā’ ā‘
depth
ā“
ā width ā’
ā’ ā’
The example characters in previous chapters have all had zero depth, but we
will soon be seeing examples in which both height and depth are relevant.
A character shape need not ļ¬t inside the boundaries of its box. Indeed,
italic and slanted letters are put into ordinary boxes just as if they were not
slanted, so they frequently stick out at the right. For example, the letter ā˜gā™
in the font you are now reading (cmr10) can be compared with the ā˜gā™ in the
corresponding slanted font (cmsl10):
(A figure will be inserted here; too bad you canā™t see it now.
It shows two gā™s, as claimed. In fact, the same figure ap
peared on page 63 of The TeXbook.)
The slanted ā˜gā™ has been drawn as if its box were skewed right at the top and
left at the bottom, keeping the baseline ļ¬xed; but TEX is told in both cases that
the box is 5 pt wide, 4.3055 pt high, and 1.9444 pt deep. Slanted letters will be
102 Chapter 12: Boxes
spaced properly in spite of the fact that their boxes have been straightened up, italic correction
beginchar
because the letters will match correctly at the baseline. sharped
w
Boxes also have a fourth dimension called the italic correction, which gives h
d
TEX additional information about whether or not a letter protrudes at the
graph paper
right. For example, the italic correction for an unslanted ā˜gā™ in cmr10 is 0.1389 pt, drift
while the corresponding slanted letter in cmsl10 has an italic correction of 0.8565 pt. endchar
The italic correction is added to a boxā™s width when math formulas like g2 or g 2 are
being typeset, and also in other cases as explained in The TEXbook.
Plain ā™s beginchar command establishes the width, height,
and depth of a box. These dimensions should be given in terms of āsharpedā
quantities that do not vary with the resolution or magniļ¬cation, because the size
of a characterā™s type box should not depend in any way on the device that will
be used to output that character. It is important to be able to deļ¬ne documents
that will not change even though the technology for printing those documents is
continually evolving. can be used to produce fonts for new devices
by introducing new āmodes,ā as we have seen in Chapter 11, but the new fonts
should still give the same box dimensions to each character. Then the device
independent ļ¬les output by TEX will not have to be changed in any way when
they are printed or displayed with the help of new equipment.
The three dimensions in a beginchar command are given in reverse
alphabetical order: First comes the width, then the height, then the depth. The
beginchar routine converts these quantities into pixel units and assigns them
to the three variables w , h , and d . In fact, beginchar rounds these dimensions
to the nearest whole number of pixels; hence w, h, and d will always be integers.
ā™s pixels are like squares on graph paper, with pixel bound
aries at points with integer coordinates. The left edge of the type box lies on
the line x = 0, and the right edge lies on the line x = w; we have y = h on the
top edge and y = ā’d on the bottom edge. There are w pixels in each row and
h + d in each column, so there are exactly wh + wd pixels inside the type box.
Since w, h, and d are integers, they probably do not exactly match
the box dimensions that are assumed by deviceindependent typesetting systems
like TEX. Some characters will be a fraction of a pixel too wide; others will be a
fraction of a pixel too narrow. However, itā™s still possible to obtain satisfactory
results if the pixel boxes are stacked together based on their w values and if the
accumulated error is removed in the spaces between words, provided that the box
positions do not drift too far away from their true deviceindependent locations.
A designer should strive to obtain letterforms that work well together when they
are placed together in boxes that are an integer number of pixels wide.
You might not like the value of w that beginchar computes by rounding the
deviceindependent width to the nearest pixel boundary. For example, you
might want to make the letter ā˜mā™ one pixel wider, at certain resolutions, so that its
three stems are equally spaced or so that it will go better with your ā˜nā™. In such a case
you can assign a new value to w, at any time between beginchar and endchar. This
Chapter 12: Boxes 103
new value will not aļ¬ect the deviceindependent box width assumed by TEX, but it dvi
parenthesis
should be respected by the software that typesets dvi ļ¬les using your font.
Computer Modern
metadesign
Hereā™s an example of a character that has nonzero width, height, and axis
depth; itā™s the left parenthesis in Computer Modern fonts like cmr10. Computer ļ¬lldraw
soft
Modern typefaces are generated by programs that involve lots of pa crisp
rameters, so this example also illustrates the principles of āmetadesignā: Many
diļ¬erent varieties of left parentheses can be drawn by this one program. But
letā™s focus our attention ļ¬rst on the comparatively simple way in which the box
dimensions are established and used, before looking into the details of how a
metaparenthesis has actually been speciļ¬ed. (0,h) (w,h)
"Left parenthesis";
numeric ht #, dp #;
ht # = body height #; .5[ht #, ā’dp #] = axis #;
beginchar ("(", 7u#, ht #, dp #);
italcorr ht # ā— slant ā’ .5u#;
pickup ļ¬ne.nib ;
penpos1 (hair ā’ ļ¬ne , 0); (Figure 12a will be
penpos2 (.75[thin , thick ] ā’ ļ¬ne , 0); inserted here; too bad
you canā™t see it now.)
penpos3 (hair ā’ ļ¬ne , 0);
rt x1r = rt x3r = w ā’ u; lft x2l = x1 ā’ 4u;
top y1 = h; y2 = .5[y1 , y3 ] = axis ;
ļ¬lldraw z1l {(z2l ā’ z1l ) xscaled 3} . . . z2l
. . . {(z3l ā’ z2l ) xscaled 3}z3l
  z3r {(z2r ā’ z3r ) xscaled 3} . . . z2r
. . . {(z1r ā’ z2r ) xscaled 3}z1r   cycle;
(0,ā’d) (w,ā’d)
penlabels(1, 2, 3); endchar;
The width of this left parenthesis is 7u#, where u# is an ad hoc pa
rameter that ļ¬gures in all the widths of the Computer Modern characters. The
height and depth have been calculated in such a way that the top and bot
tom of the bounding box are equally distant from an imaginary line called the
axis, which is important in mathematical typesetting. (For example, TEX puts
the bar line at the axis in fractions like 1 ; many symbols like ā˜+ā™ and ā˜=ā™,
2
as well as parentheses, are centered on the axis line.) Our example program
puts the axis midway between the top and bottom of the type by saying that
ā˜.5[ht #, ā’dp #] = axis #ā™. We also place the top at position ā˜ht # = body height #ā™ ;
here body height # is the height of the tallest characters in the entire typeface.
It turns out that body height # is exactly 7.5pt # in cmr10, and axis # = 2.5pt #;
hence dp # = 2.5pt #, and the parenthesis is exactly 10 pt tall.
The program for ā˜(ā™ uses a ļ¬lldraw command, which we havenā™t seen
before in this book; itā™s basically a combination of ļ¬ll and draw, where the ļ¬lling
is done with the currentlypickedup pen. Some of the Computer Modern fonts
have characters with āsoftā edges while others have ācrispā edges; the diļ¬erence
is due to the pen that is used to ļ¬lldraw the shapes. This pen is a circle whose
104 Chapter 12: Boxes
diameter is called ļ¬ne ; when ļ¬ne is fairly large, ļ¬lldraw will produce rounded ļ¬ne
hairline
corners, but when ļ¬ne = 0 (as it is in cmr10) the corners will be sharp. proof
The statement ā˜penpos1 (hair ā’ļ¬ne , 0)ā™ makes the breadth of a simulated
broadedge pen equal to hair ā’ ļ¬ne at position 1; i.e., the distance between z1l
and z1r will be hair ā’ ļ¬ne . We will be ļ¬lling a region between z1l and z1r with
a circleshaped pen nib whose diameter is ļ¬ne ; the center of that nib will pass
through z1l and z1r , hence the pen will eļ¬ectively add 1 ļ¬ne to the breadth of
2
the stroke at either side. The overall breadth at position 1 will therefore be
2 ļ¬ne + (hair ā’ ļ¬ne ) + 2 ļ¬ne = hair . (Computer Modernā™s āhairline thicknessā
1 1
parameter, which governs the breadth of the thinnest strokes, is called hair .)
Similarly, the statement ā˜penpos2 (.75[thin , thick ] ā’ ļ¬ne , 0)ā™ makes the overall
breadth of the pen at position 2 equal to .75[thin , thick ], which is 3 of the way
4
between two other parameters that govern stroke breadths in Computer Modern
routines. If ļ¬ne is increased while hair , thin , and thick stay the same, the eļ¬ect
will simply be to produce more rounded corners at positions 1 and 3, with little
or no eļ¬ect on the rest of the shape, provided that ļ¬ne doesnā™t get so large that
it exceeds hair .
Here, for example, are ļ¬ve diļ¬erent left parentheses, drawn by our ex
ample program with various settings of the parameters:
cmr10 cmbx10 cmvtt10 cmssdc10 cmti10
(Figure 12c will be in
(Figure 12d will be
serted here; too bad (Figure 12e will be
(Figure 12a will be (Figure 12b will be in inserted here; too
you canā™t see it now.) inserted here; too
inserted here; too bad serted here; too bad you bad you canā™t see it bad you canā™t see it
you canā™t see it now.) canā™t see it now.) now.) now.)
u= 20 u= 23 u= 21 u= 19 u = 18.4
ht = 270 ht = 270 ht = 250 ht = 270 ht = 270
axis = 90 axis = 90 axis = 110 axis = 95 axis = 90
ļ¬ne = 0 ļ¬ne = 0 ļ¬ne = 22 ļ¬ne = 8 ļ¬ne = 7
hair = 8 hair = 13 hair = 22 hair = 23 hair = 8
thin = 9 thin = 17 thin = 25 thin = 40 thin = 11
thick = 25 thick = 41 thick = 25 thick = 40 thick = 23
Parameter values are shown here in proof mode pixel units, 36 to the point.
Chapter 12: Boxes 105
(Thus, for example, the value of u# in cmr10 is 20 pt #.) Since cmbx10 is a ābold slanted
36
italic correction
extendedā font, its unit width u is slightly larger than the unit width of cmr10, italcorr
and its pen widths (especially thick ) are signiļ¬cantly larger. The āvariable slant
width typewriterā font cmvtt10 has soft edges and strokes of almost uniform
thickness, because ļ¬ne and hair are almost as large as thin and thick . This font
also has a raised axis and a smaller height. An intermediate situation occurs
in cmssdc10, a āsans serif demibold condensedā font that is similar to the type
used in the chapter titles of this book; thick = thin in this font, but hairlines are
noticeably thinner, and ļ¬ne provides slightly rounded corners. The ātext italicā
font cmti10 has rounded ends, and the character shape has been slanted by .25;
this means that each point (x, y) has been moved to position (x + .25y, y), in the
path that is ļ¬lled by ļ¬lldraw.
The vertical line just to the right of the italic left parenthesis shows the italic
correction of that character, i.e., the fourth box dimension mentioned earlier.
This quantity was deļ¬ned by the statement ā˜italcorr ht # ā—slant ā’.5u#ā™ in our program;
here slant is a parameter of Computer Modern that is zero in all the unslanted fonts,
but slant = .25 in the case of cmti10. The expression following italcorr should always
be given in sharped units. If the value is negative, the italic correction will be zero;
otherwise the italic correction will be the stated amount.
The author has obtained satisfactory results by making the italic correction
roughly equal to .5u plus the maximum amount by which the character sticks
out to the right of its box. For example, the top right end of the left parenthesis will be
nearly at position (w ā’ u, ht ) before slanting, so its x coordinate after slanting will be
w ā’ u + ht ā— slant ; this will be the rightmost point of the character, if we assume that
slant ā„ 0. Adding .5u, subtracting w, and rewriting in terms of sharped units gives the
stated formula. Notice that when slant = 0 the statement reduces to ā˜italcorr ā’.5u#ā™;
this means that unslanted left parentheses will have an italic correction of zero.
EXERCISE 12.1
Write a program for right parentheses, to go with these left parentheses.
The reader should bear in mind that the conventions of plain
and of Computer Modern are not hardwired into the language; they
are merely examples of how a person might use the system, and other typefaces
may well be better served by quite diļ¬erent approaches. Our program for left
parentheses makes use of beginchar, endchar, italcorr, penlabels, pickup,
penpos , lft , rt , top , z , and ļ¬lldraw, all of which are deļ¬ned somewhat arbitrarily
in Appendix B as part of the plain base; it also uses the quantities u , body height ,
axis , ļ¬ne , hair , thin , thick , and slant , all of which are arbitrary parameters that
the author decided to introduce in his programs for Computer Modern. Once
you understand how to use arbitrary conventions like these, you will be able to
modify them to suit your own purposes.
EXERCISE 12.2
(For people who know TEX.) Itā™s fairly clear that the width of a type box is
important for typesetting, but what use does TEX make of the height and depth?
106 Chapter 12: Boxes
The primitive commands by which actually learns the dimensions charwd
charht
of each box are rarely used directly, since they are intended to be embedded
chardp
in higherlevel commands like beginchar and italcorr. But if you must know how charic
things are done at the low level, here is the secret: There are four internal quantities shipout
charcode
called charwd , charht , chardp , and charic , whose values at the time of every shipout location
command are assumed to be the box dimensions for the character being shipped out, in c code
charext
units of printerā™s points. (See the deļ¬nitions of beginchar and italcorr in Appendix B
oriental
for examples of how these quantities can be manipulated.) chardx
dvi
Besides charwd and its cousins, also has four other internal vari drift
ables whose values are recorded at the time of every shipout: w
charexists
charcode is rounded to the nearest integer and then converted to a number dangerous bend
between 0 and 255, by adding or subtracting multiples of 256 if necessary; this āc codeā
is the location of the character within its font.
charext is rounded to the nearest integer; the resulting number is a secondary
code that can be used to distinguish between two or more characters with equal c codes.
(TEX ignores charext and assumes that each font contains at most 256 characters; but
extensions to TEX for oriental languages can use charext to handle much larger fonts.)
chardx and chardy represent horizontal and vertical escapement in units of
pixels. (Some typesetting systems use both of these devicedependent amounts to
change their current position on a page, just after typesetting each character. Other sys
tems, like the dvi software associated with TEX, assume that chardy = 0 but use chardx
as the horizontal escapement whenever a horizontal movement by chardx does not cause
the subsequent position to drift too far from the deviceindependent position deļ¬ned by
accumulated charwd values. Plain ā™s endchar routine keeps chardy = 0,
but sets chardx := w just before shipping a character to the output. This explains why
a change to w will aļ¬ect the spacing between adjacent letters, as discussed earlier.)
Two characters with the same c code should have the same box dimensions and
escapements; otherwise the second character will override the speciļ¬cations of
the ļ¬rst. The boolean expression ā˜charexists cā™ can be used to determine whether or
not a character with a particular c code has already been shipped out.
Letā™s conclude this chapter by contemplating a program that gen
erates the ādangerous bendā symbol, since that symbol appears so often in
this book. Itā™s a custommade character intended to be used only at the very beginnings
of paragraphs in which the baselines of the text are exactly 11 pt apart. Therefore it
extends below its baseline by 11 pt; but it is put into a box of depth zero, because TEX
would otherwise think that the ļ¬rst line of the paragraph contains an extremely deep
character, and such depth would cause the second line to be moved down.
baselinedistance # := 11pt #; deļ¬ne pixels(baselinedistance );
heavyline # := 50/36pt #; deļ¬ne blacker pixels(heavyline );
beginchar (127, 25u#, h height # + border #, 0); "Dangerous bend symbol";
pickup pencircle scaled rulethickness ; top y1 = 25 h; lft x4 = 0;
27
x1 +x1 = x1a +x1b = x4b +x2a = x4 +x2 = x4a +x2b = x3b +x3a = x3 +x3 = w;
x4a = x4b = x4 + u; x3b = x1a = x1 ā’ 2u;
y4 + y4 = y4a + y4b = y3b + y1a = y3 + y1 = y3a + y1b = y2b + y2a = y2 + y2 = 0;
2 4
y1a = y1b = y1 ā’ 27 h; y4b = y2a = y4 + 27 h;
Chapter 12: Boxes 107
draw z1a . . z1 . . z1b    z2a . . z2 . . z2b    beginchar
location
z3a . . z3 . . z3b    z4a . . z4 . . z4b    cycle; % the signboard
ā”
x10 = x11 = x12 = x13 = .5w ā’ u; x14 = x15 = x16 = x17 = w ā’ x10 ; intersectionpoint
y10 = y14 = 28 h; bot y13 = ā’baselinedistance ; HAGGARD
27
COLBURN
z11 = (z10 . . z13 ) intersectionpoint (z1a {z1a ā’ z4b } . . z1 {right });
y15 = y11 ; y16 = y12 = ā’y11 ; y17 = y20 = y21 = y13 ;
draw z11   z10   z14   z15 ; draw z12   z13 ; draw z16   z17 ; % the signpost
x20 = w ā’ x21 ; x21 ā’ x20 = 16u; draw z20   z21 ; % ground level
x36 = w ā’ x31 ; x36 ā’ x31 = 8u; x32 = x33 = x36 ; x31 = x34 = x35 ;
y31 = ā’y36 = 12 h; y32 = ā’y35 = 27 h; y33 = ā’y34 = 27 h;
9 3
27
pickup pencircle scaled heavyline ;
draw z32 {z32 ā’ z31 } . . z33    z34 . . z35 {z36 ā’ z35 }; % the dangerous bend
pickup penrazor xscaled heavyline rotated (angle(z32 ā’ z31 ) + 90);
draw z31   z32 ; draw z35   z36 ; % upper and lower bars
labels(1a, 1b, 2a, 2b, 3a, 3b, 4a, 4b, range 1 thru 36); endchar;
This program has sev
eral noteworthy points
of interest: (1) The ļ¬rst
parameter to beginchar
here is 127, not a string;
this puts the character
into font location 127.
(2) A sequence of equa
tions like ā˜a = w ā’ b; a =
w ā’ b ā™ can conveniently
be shortened to ā˜a + b =
a + b = wā™. (3) Three
hyphens ā˜  ā™ is an ab
breviation for a line with
(Figure 12f will be inserted here; too bad you canā™t see it now.)
āinļ¬niteā tension, i.e.,
an almost straight line
that connects smoothly
to its curved neighbors.
(4) An ā˜intersectionpointā™
operation ļ¬nds out where
two paths cross; weā™ll
learn more about this in
Chapter 14.
Well, we are in the same box.
ā” RIDER HAGGARD, Dawn (1884)
A story, too,
may be boxed.
ā” DOROTHY COLBURN, Newspaper Nomenclature (1927)
(page 108)
13
Drawing, Filling,
and Erasing
Chapter 13: Drawing, Filling, and Erasing 109
The pictures that produces are made up of tiny pixels that are either graph paper
ļ¬ll
āonā or āoļ¬ā; therefore you might imagine that the computer works behind the unļ¬ll
scenes with some sort of graph paper, and that it darkens some of the squares coordinates
whenever you tell it to draw a line or to ļ¬ll a region.
ā™s internal graph paper is actually more sophisticated than
this. Pixels arenā™t simply āonā or āoļ¬ā when is working on a picture;
they can be ādoubly onā or ātriply oļ¬.ā Each pixel contains a small integer value,
and when a character is ļ¬nally shipped out to a font the black pixels are those
whose value is greater than zero. For example, the two commands
ļ¬ll (0, 3)   (9, 3)   (9, 6)   (0, 6)   cycle;
ļ¬ll (3, 0)   (3, 9)   (6, 9)   (6, 0)   cycle
yield the following 9 Ć— 9 pattern of pixel values:
000111000
000111000
000111000
111222111
111222111
111222111
000111000
000111000
000111000
Pixels that have been ļ¬lled twice now have a value of 2.
When a simple region is āļ¬lled,ā its pixel values are all increased by 1;
when it is āunļ¬lled,ā they are all decreased by 1. The command
unļ¬ll (1, 4)   (8, 4)   (8, 5)   (1, 5)   cycle
will therefore change the pattern above to
000111000
000111000
000111000
111222111
100111001
111222111
000111000
000111000
000111000
The pixels in the center have not been erased (i.e., they will still be black if this
picture is output to a font), because they still have a positive value.
Incidentally, this example illustrates the fact that the edges between
ā™s pixels are lines that have integer coordinates, just as the squares
on graph paper do. For example, the lower left ā˜0ā™ in the 9 Ć— 9 array above
corresponds to the pixel whose boundary is ā˜(0, 0)   (1, 0)   (1, 1)   (0, 1)  
cycleā™. The (x, y) coordinates of the points inside this pixel lie between 0 and 1.
EXERCISE 13.1
What are the (x, y) coordinates of the four corners of the middle pixel in the
9 Ć— 9 array?
EXERCISE 13.2
What picture would have been obtained if the unļ¬ll command had been given
before the two ļ¬ll commands in the examples above?
ńņš. 13 