. 1
( 13)



>>

page mcmlxxxv)




The book




The ¬ne print in the upper right-hand
corner of each page is a draft of intended
index entries; it won™t appear in the real book.
Some index entries will be in typewriter type
and/or enclosed in . . . , etc;
such typographic distinctions aren™t shown here.
An index entry often extends for several pages;
the actual scope will be determined later.
Please note things that should be indexed but aren™t.
Apology: The xeroxed illustrations are often hard to see;
they will be done professionally in the real book.
(page i)




The book Knuth, Donald Ervin
Bibby, Duane Robert




DONALD E. KNUTH Stanford University




Illustrations by
DUANE BIBBY




ADDISON “WESLEY
PUBLISHING COMPANY
Reading, Massachusetts
Menlo Park, California
New York
Don Mills, Ontario
Wokingham, England
Amsterdam · Bonn
Sydney · Singapore · Tokyo
Madrid · San Juan
(page ii)



Palais
Wilkins
Tobin
Knuth, Donald Ervin




This manual describes Version 2.0. Some of the advanced features mentioned here
are absent from earlier versions.
The joke on page 8 is due to Richard S. Palais.
The Wilkins quotation on page 283 was suggested by Georgia K. M. Tobin.
is a trademark of Addison “Wesley Publishing Company.
TEX is a trademark of the American Mathematical Society.


Library of Congress cataloging in publication data
Knuth, Donald Ervin, 1938-
The METAFONTbook.
(Computers & Typesetting ; C)
Includes index.
1. METAFONT (Computer system). 2. Type and type-
founding--Data processing. I. Title. II. Series:
Knuth, Donald Ervin, 1938- . Computers &
typesetting ; C.
Z250.8.M46K58 1986 686.2'24 85-28675
ISBN 0-201-13445-4
ISBN 0-201-13444-6 (soft)




Incorporates the ¬nal corrections made in 1995.
Internet page http://www-cs-faculty.stanford.edu/˜knuth/abcde.html contains current in-
formation about this book and related books.
Copyright c 1986 by the American Mathematical Society
This book is published jointly by the American Mathematical Society and Addison “Wesley
Publishing Company. All rights reserved. No part of this publication may be reproduced,
stored in a retrieval system, or transmitted, in any form or by any means, electronic, me-
chanical, photocopying, recording, or otherwise, without the prior written permission of the
publishers. Printed in the United States of America.
ISBN 0-201-13445-4
6 7 8 9 10 11 12“CRS“9998979695
(page iii)



Zapf, Hermann




To Hermann Zapf:
Whose strokes are the best
(page iv)
(page v)




Preface beauty




by mathematical means was ¬rst tried in
ENERATION OF LETTERFORMS
the ¬fteenth century; it became popular in the sixteenth and seventeenth
centuries; and it was abandoned (for good reasons) during the eighteenth century.
Perhaps the twentieth century will turn out to be the right time for this idea to
make a comeback, now that mathematics has advanced and computers are able
to do the calculations.
Modern printing equipment based on raster lines”in which metal “type”
has been replaced by purely combinatorial patterns of zeroes and ones that spec-
ify the desired position of ink in a discrete way”makes mathematics and com-
puter science increasingly relevant to printing. We now have the ability to give a
completely precise de¬nition of letter shapes that will produce essentially equiv-
alent results on all raster-based machines. Moreover, the shapes can be de¬ned
in terms of variable parameters; computers can “draw” new fonts of characters
in seconds, making it possible for designers to perform valuable experiments that
were previously unthinkable.
is a system for the design of alphabets suited to raster-based
devices that print or display text. The characters that you are reading were all
designed with , in a completely precise way; and they were developed
rather hastily by the author of the system, who is a rank amateur at such things.
It seems clear that further work with has the potential of producing
typefaces of real beauty. This manual has been written for people who would
like to help advance the art of mathematical type design.
A top-notch designer of typefaces needs to have an unusually good eye
and a highly developed sensitivity to the nuances of shapes. A top-notch user of
computer languages needs to have an unusual talent for abstract reasoning and
a highly developed ability to express intuitive ideas in formal terms. Very few
people have both of these unusual combinations of skills; hence the best products
of will probably be collaborative e¬orts between two people who
complement each other™s abilities. Indeed, this situation isn™t very di¬erent from
the way types have been created for many generations, except that the rˆle of o
“punch-cutter” is now being played by skilled computer specialists instead of by
skilled metalworkers.
A user writes a “program” for each letter or symbol of
a typeface. These programs are di¬erent from ordinary computer programs,
because they are essentially declarative rather than imperative. In the -
language you explain where the major components of a desired shape are
vi Preface




to be located, and how they relate to each other, but you don™t have to work
out the details of exactly where the lines cross, etc.; the computer takes over the
work of solving equations as it deduces the consequences of your speci¬cations.
One of the advantages of is that it provides a discipline according to
which the principles of a particular alphabet design can be stated precisely. The
underlying intelligence does not remain hidden in the mind of the designer; it
is spelled out in the programs. Thus consistency can readily be obtained where
consistency is desirable, and a font can readily be extended to new symbols that
are compatible with the existing ones.
It would be nice if a system like were to simplify the task
of type design to the point where beautiful new alphabets could be created in a
few hours. This, alas, is impossible; an enormous amount of subtlety lies behind
the seemingly simple letter shapes that we see every day, and the designers of
high-quality typefaces have done their work so well that we don™t notice the
underlying complexity. One of the disadvantages of is that a person
can easily use it to produce poor alphabets, cheaply and in great quantity. Let
us hope that such experiments will have educational value as they reveal why
the subtle tricks of the trade are important, but let us also hope that they will
not cause bad workmanship to proliferate. Anybody can now produce a book
in which all of the type is home-made, but a person or team of persons should
expect to spend a year or more on the project if the type is actually supposed
to look right. won™t put today™s type designers out of work; on
the contrary, it will tend to make them heroes and heroines, as more and more
people come to appreciate their skills.
Although there is no royal road to type design, there are some things
that can, in fact, be done well with in an afternoon. Geometric
designs are rather easy; and it doesn™t take long to make modi¬cations to letters
or symbols that have previously been expressed in form. Thus,
although comparatively few users of will have the courage to do
an entire alphabet from scratch, there will be many who will enjoy customizing
someone else™s design.
This book is not a text about mathematics or about computers. But
if you know the rudiments of those subjects (namely, contemporary high school
mathematics, together with the knowledge of how to use the text editing or
word processing facilities on your computing machine), you should be able to
use with little di¬culty after reading what follows. Some parts
Preface vii


dangerous bend




of the exposition in the text are more obscure than others, however, since the
author has tried to satisfy experienced ers as well as beginners and
casual users with a single manual. Therefore a special symbol has been used to
warn about esoterica: When you see the sign



at the beginning of a paragraph, watch out for a “dangerous bend” in the train of
thought”don™t read such a paragraph unless you need to. You will be able to use
reasonably well, even to design characters like the dangerous-bend
symbol itself, without reading the ¬ne print in such advanced sections.
Some of the paragraphs in this manual are so far out that they are rated
;
everything that was said about single dangerous-bend signs goes double for these.
You should probably have at least a month™s experience with before
you attempt to fathom such doubly dangerous depths of the system; in fact,
most people will never need to know in this much detail, even if
they use it every day. After all, it™s possible to fry an egg without knowing
anything about biochemistry. Yet the whole story is here in case you™re curious.
(About , not eggs.)
The reason for such di¬erent levels of complexity is that people change
as they grow accustomed to any powerful tool. When you ¬rst try to use -
, you™ll ¬nd that some parts of it are very easy, while other things will take
some getting used to. At ¬rst you™ll probably try to control the shapes too rigidly,
by overspecifying data that has been copied from some other medium. But later,
after you have begun to get a feeling for what the machine can do well, you™ll
be a di¬erent person, and you™ll be willing to let help contribute to
your designs as they are being developed. As you gain more and more experience
working with this unusual apprentice, your perspective will continue to change
and you will run into di¬erent sorts of challenges. That™s the way it is with any
powerful tool: There™s always more to learn, and there are always better ways
to do what you™ve done before. At every stage in the development you™ll want a
slightly di¬erent sort of manual. You may even want to write one yourself. By
paying attention to the dangerous bend signs in this book you™ll be better able
to focus on the level that interests you at a particular time.
viii Preface


JOKES
truth
EXERCISES
MF79
Hobby




Computer system manuals usually make dull reading, but take heart:
This one contains JOKES every once in a while. You might actually enjoy read-
ing it. (However, most of the jokes can only be appreciated properly if you
understand a technical point that is being made”so read carefully.)
Another noteworthy characteristic of this book is that it doesn™t always
tell the truth. When certain concepts of are introduced informally,
general rules will be stated; afterwards you will ¬nd that the rules aren™t strictly
true. In general, the later chapters contain more reliable information than the
earlier ones do. The author feels that this technique of deliberate lying will
actually make it easier for you to learn the ideas. Once you understand a simple
but false rule, it will not be hard to supplement that rule with its exceptions.
In order to help you internalize what you™re reading, EXERCISES are
sprinkled through this manual. It is generally intended that every reader should
try every exercise, except for questions that appear in the “dangerous bend”
areas. If you can™t solve a problem, you can always look up the answer. But
please, try ¬rst to solve it by yourself; then you™ll learn more and you™ll learn
faster. Furthermore, if you think you do know the solution, you should turn to
Appendix A and check it out, just to make sure.


WARNING: Type design can be hazardous to your other interests.
Once you get hooked, you will develop intense feelings about letter-
forms; the medium will intrude on the messages that you read. And
you will perpetually be thinking of improvements to the fonts that
you see everywhere, especially those of your own design.


The language described here has very little in common with
the author™s previous attempt at a language for alphabet design, because ¬ve
years of experience with the old system has made it clear that a completely
di¬erent approach is preferable. Both languages have been called ;
but henceforth the old language should be called 79, and its use
should rapidly fade away. Let™s keep the name for the language
described here, since it is so much better, and since it will never change again.
I wish to thank the hundreds of people who have helped me to formulate
this “de¬nitive edition” of , based on their experiences with prelim-
inary versions of the system. In particular, John Hobby discovered many of
Preface ix


National Science Foundation
O¬ce of Naval Research
IBM Corporation
System Development Foundation
American Mathematical Society
TUGboat
Knuth, Jill
Knuth, Don
BIERCE
MORISON


the algorithms that have made the new language possible. My work at Stan-
ford has been generously supported by the National Science Foundation, the
O¬ce of Naval Research, the IBM Corporation, and the System Development
Foundation. I also wish to thank the American Mathematical Society for its
encouragement and for publishing the TUGboat newsletter (see Appendix J).
Above all, I deeply thank my wife, Jill, for the inspiration, understanding, com-
fort, and support she has given me for more than 25 years, especially during the
eight years that I have been working intensively on mathematical typography.
Stanford, California ” D. E. K.
September 1985




It is hoped that Divine Justice may ¬nd
some suitable a¬„iction for the malefactors
who invent variations upon the alphabet of our fathers. . . .
The type-founder, worthy mechanic, has asserted himself
with an overshadowing individuality,
defacing with his monstrous creations and revivals
every publication in the land.
” AMBROSE BIERCE, The Opinionator. Alphabˆtes (1911)
e

Can the new process yield a result that, say,
a Club of Bibliophiles would recognise as a work of art
comparable to the choice books they have in their cabinets?
” STANLEY MORISON, Typographic Design in Relation to
Photographic Composition (1958)
(page x)




Contents Contents of this manual, table




1 The Name of the Game . . . . . . . . . 1
2 Coordinates . . . . . . . . . . . 5
3 Curves . . . . . . . . . . . . . 13
4 Pens . . . . . . . . . . . . . 21
5 Running . . . . . . . . . . 31
6 How Reads What You Type . . . . . 49
7 Variables . . . . . . . . . . . . 53
8 Algebraic Expressions . . . . . . . . . 59
9 Equations . . . . . . . . . . . . 75
10 Assignments . . . . . . . . . . . 87
11 Magni¬cation and Resolution . . . . . . . . 91
12 Boxes . . . . . . . . . . . . 101
13 Drawing, Filling, and Erasing . . . . . . . . 109
14 Paths . . . . . . . . . . . . 123
15 Transformations . . . . . . . . . . . 141
16 Calligraphic E¬ects . . . . . . . . . 147
17 Grouping . . . . . . . . . . . . 155
18 De¬nitions (also called Macros) . . . . . . . 159
19 Conditions and Loops . . . . . . . . . 169
20 More about Macros . . . . . . . . . 175
21 Random Numbers . . . . . . . . . . 183
22 Strings . . . . . . . . . . . . 187
23 Online Displays . . . . . . . . . . . 191
Contents xi




24 Discreteness and Discretion . . . . . . . . 195
25 Summary of Expressions . . . . . . . . . 209
26 Summary of the Language . . . . . . . . 217
27 Recovering from Errors . . . . . . . . . 223


Appendices
A Answers to All the Exercises . . . . . . . 233
B Basic Operations . . . . . . . . . . 257
C Character Codes . . . . . . . . . . 281
D Dirty Tricks . . . . . . . . . . . . 285
E Examples . . . . . . . . . . . . 301
F Font Metric Information . . . . . . . . . 315
G Generic Font Files . . . . . . . . . . 323
H Hardcopy Proofs . . . . . . . . . . . 327
I Index . . . . . . . . . . . . 345
J Joining the TEX Community . . . . . . . . 361
(page xii)




1
The Name of
the Game
Chapter 1: The Name of the Game 1


This is a book about a computer system called , just as The TEXbook TeX
METAFONT, the name
is about TEX. and TEX are good friends who intend to live together meta-font
for a long time. Between them they take care of the two most fundamental tasks
of typesetting: TEX puts characters into the proper positions on a page, while
determines the shapes of the characters themselves.
Why is the system called ? The ˜- ™ part is easy to
understand, because sets of related characters that are used in typesetting are
traditionally known as fonts of type. The ˜ -™ part is more interesting: It
indicates that we are interested in making high-level descriptions that transcend
any of the individual fonts being described.
Newly coined words beginning with ˜meta-™ generally re¬‚ect our con-
temporary inclination to view things from outside or above, at a more abstract
level than before, with what we feel is a more mature understanding. We now
have metapsychology (the study of how the mind relates to its containing body),
metahistory (the study of principles that control the course of events), meta-
mathematics (the study of mathematical reasoning), meta¬ction (literary works
that explicitly acknowledge their own forms), and so on. A metamathemati-
cian proves metatheorems (theorems about theorems); a computer scientist of-
ten works with metalanguages (languages for describing languages). Similarly,
a meta-font is a schematic description of the shapes in a family of related fonts;
the letterforms change appropriately as their underlying parameters change.
Meta-design is much more di¬cult than design; it™s easier to draw some-
thing than to explain how to draw it. One of the problems is that di¬erent sets
of potential speci¬cations can™t easily be envisioned all at once. Another is that
a computer has to be told absolutely everything. However, once we have suc-
cessfully explained how to draw something in a su¬ciently general manner, the
same explanation will work for related shapes, in di¬erent circumstances; so the
time spent in formulating a precise explanation turns out to be worth it.
Typefaces intended for text are normally seen small, and our eyes can
read them best when the letters have been designed speci¬cally for the size at
which they are actually used. Although it is tempting to get 7-point fonts by
simply making a 70% reduction from the 10-point size, this shortcut leads to a
serious degradation of quality. Much better results can be obtained by incorpo-
rating parametric variations into a meta-design. In fact, there are advantages to
built-in variability even when you want to produce only one font of type in a sin-
gle size, because it allows you to postpone making decisions about many aspects
of your design. If you leave certain things unde¬ned, treating them as parame-
ters instead of “freezing” the speci¬cations at an early stage, the computer will
be able to draw lots of examples with di¬erent settings of the parameters, and
you will be able to see the results of all those experiments at the ¬nal size. This
will greatly increase your ability to edit and ¬ne-tune the font.
If meta-fonts are so much better than plain old ordinary fonts, why
weren™t they developed long ago? The main reason is that computers did not
exist until recently. People ¬nd it di¬cult and dull to carry out calculations
2 Chapter 1: The Name of the Game


with a multiplicity of parameters, while today™s machines do such tasks with interpolate
recipe
ease. The introduction of parameters is a natural outgrowth of automation.
OK, let™s grant that meta-fonts sound good, at least in theory. There™s
still the practical problem about how to achieve them. How can we actually
specify shapes that depend on unspeci¬ed parameters?
If only one parameter is varying, it™s fairly easy to solve the problem
in a visual way, by overlaying a series of drawings that show graphically how
the shape changes. For example, if the parameter varies from 0 to 1, we might
prepare ¬ve sketches, corresponding to the parameter values 0, 1 , 1 , 3 , and 1.
424
If these sketches follow a consistent pattern, we can readily interpolate to ¬nd
the shape for a value like 2 that lies between two of the given ones. We might
3
even try extrapolating to parameter values like 1 1 .4
But if there are two or more independent parameters, a purely visual
solution becomes too cumbersome. We must go to a verbal approach, using some
sort of language to describe the desired drawings. Let™s imagine, for example,
that we want to explain the shape of a certain letter ˜a™ to a friend in a distant
country, using only a telephone for communication; our friend is supposed to be
able to reconstruct exactly the shape we have in mind. Once we ¬gure out a
su¬ciently natural way to do that, for a particular ¬xed shape, it isn™t much of
a trick to go further and make our verbal description more general, by including
variable parameters instead of restricting ourselves to constants.
An analogy to cooking might make this point clearer. Suppose you have
just baked a delicious berry pie, and your friends ask you to tell them the recipe
so that they can bake one too. If you have developed your cooking skills entirely
by intuition, you might ¬nd it di¬cult to record exactly what you did. But there
is a traditional language of recipes in which you could communicate the steps
you followed; and if you take careful measurements, you might ¬nd that you
used, say, 1 1 cups of sugar. The next step, if you were instructing a computer-
4
controlled cooking machine, would be to go to a meta-recipe in which you use,
say, .25x cups of sugar for x cups of berries; or .3x + .2y cups for x cups of
boysenberries and y cups of blackberries.
In other words, going from design to meta-design is essentially like going
from arithmetic to elementary algebra. Numbers are replaced by simple formulas
that involve unknown quantities. We will see many examples of this.
A de¬nition of a complete typeface generally consists of
three main parts. First there is a rather mundane set of subroutines that take
care of necessary administrative details, such as assigning code numbers to indi-
vidual characters; each character must also be positioned properly inside an in-
visible “box,” so that typesetting systems will produce the correct spacing. Next
comes a more interesting collection of subroutines, designed to draw the basic
strokes characteristic of the typeface (e.g., the serifs, bowls, arms, arches, and so
on). These subroutines will typically be described in terms of their own special
parameters, so that they can produce a variety of related strokes; a serif subrou-
tine will, for example, be able to draw serifs of di¬erent lengths, although all of
Chapter 1: The Name of the Game 3


the serifs it draws should have the same “feeling.” Finally, there are routines for Hobby
Gu
each of the characters. If the subroutines in the ¬rst and second parts have been Chinese characters
chosen well, the routines of the third part will be fairly high-level descriptions Kandinsky
Herbin
that don™t concern themselves unnecessarily with details; for example, it may ´
HULTEN
be possible to substitute a di¬erent serif-drawing subroutine without changing Tinguely
KNUTH
any of the programs that use that subroutine, thereby obtaining a typeface of
quite a di¬erent ¬‚avor. [A particularly striking example of this approach has
been worked out by John D. Hobby and Gu Guoan in “A Chinese Meta-Font,”
TUGboat 5 (1984), 119“136. By changing a set of 13 basic stroke subroutines,
they were able to draw 128 sample Chinese characters in three di¬erent styles
(Song, Long Song, and Bold), using the same programs for the characters.]
A well-written program will express the designer™s intentions
more clearly than mere drawings ever can, because the language of algebra has
simple “idioms” that make it possible to elucidate many visual relationships.
Thus, programs can be used to communicate knowledge about type
design, just as recipes convey the expertise of a chef. But algebraic formulas are
not easy to understand in isolation; descriptions are meant to be
read with an accompanying illustration, just as the constructions in geometry
textbooks are accompanied by diagrams. Nobody is ever expected to read the
text of a program and say, “Ah, what a beautiful letter!” But with
one or more enlarged pictures of the letter, based on one or more settings of the
parameters, a reader of the program should be able to say, “Ah,
I understand how this beautiful letter was drawn!” We shall see that the -
system makes it fairly easy to obtain annotated proof drawings that you
can hold in your hand as you are working with a program.
Although is intended to provide a relatively painless way to
describe meta-fonts, you can, of course, use it also to describe unvarying shapes
that have no “meta-ness” at all. Indeed, you need not even use it to produce
fonts; the system will happily draw geometric designs that have no relation to
the characters or glyphs of any alphabet or script. The author occasionally uses
simply as a pocket calculator, to do elementary arithmetic in an
interactive way. A computer doesn™t mind if its programs are put to purposes
that don™t match their names.

[Tinguely] made some large, brightly coloured open reliefs,
juxtaposing stationary and mobile shapes.
He later gave them names like Meta-Kandinsky and Meta-Herbin,
to clarify the ideas and attitudes that lay at the root of their conception.
´
” K. G. PONTUS HULTEN, Jean Tinguely: M´ta (1972) e

The idea of a meta-font should now be clear. But what good is it?
The ability to manipulate lots of parameters may be interesting and fun,
but does anybody really need a 6 -point font
that is one fourth of the way between Baskerville and Helvetica?
” DONALD E. KNUTH, The Concept of a Meta-Font (1982)
(page 4)




2
Coordinates
Chapter 2: Coordinates 5


If we want to tell a computer how to draw a particular shape, we need a way to Cartesian
coordinates
explain where the key points of that shape are supposed to be. uses x coordinate
standard Cartesian coordinates for this purpose: The location of a point is de- y coordinate
graph paper
¬ned by specifying its x coordinate, which is the number of units to the right of raster
some reference point, and its y coordinate, which is the number of units upward grid
pixels
from the reference point. First we determine the horizontal (left/right) compo- pel, see pixel
nent of a point™s position, then we determine the vertical (up/down) component.
™s world is two-dimensional, so two coordinates are enough.
For example, let™s consider the following six points:



(Figure 2a will be inserted here; too bad you can™t see it now.)




™s names for the positions of these points are
(x1 , y1 ) = (0, 100); (x2 , y2 ) = (100, 100); (x3 , y3 ) = (200, 100);
(x4 , y4 ) = (0, 0); (x5 , y5 ) = (100, 0); (x6 , y6 ) = (200, 0).
Point 4 is the same as the reference point, since both of its coordinates are zero;
to get to point 3 = (200, 100), you start at the reference point and go 200 steps
right and 100 up; and so on.
EXERCISE 2.1
Which of the six example points is closest to the point (60, 30)?
EXERCISE 2.2
True or false: All points that lie on a given horizontal straight line have the same
x coordinate.
EXERCISE 2.3
Explain where the point (’5, 15) is located.
EXERCISE 2.4
What are the coordinates of a point that lies exactly 60 units below point 6 in
the diagram above? (“Below” means “down the page,” not “under the page.”)
In a typical application of , you prepare a rough sketch of
the shape you plan to de¬ne, on a piece of graph paper, and you label important
points on that sketch with any convenient numbers. Then you write a -
program that explains (i) the coordinates of those key points, and (ii) the
lines or curves that are supposed to go between them.
has its own internal graph paper, which forms a so-called
raster or grid consisting of square “pixels.” The output of will specify
that certain of the pixels are “black” and that the others are “white”; thus,
the computer essentially converts shapes into binary patterns like the designs
a person can make when doing needlepoint with two colors of yarn.
6 Chapter 2: Coordinates


Coordinates are lengths, but we haven™t discussed yet what the units of resolution
Cartesian
length actually are. It™s important to choose convenient units, and ™s Descartes
coordinates are given in units of pixels. The little squares illustrated on the
previous page, which correspond to di¬erences of 10 units in an x coordinate or
a y coordinate, therefore represent 10 — 10 arrays of pixels, and the rectangle
enclosed by our six example points contains 20,000 pixels altogether.*
Coordinates don™t have to be whole numbers. You can refer, for example,
to point (31.5, 42.5), which lies smack in the middle of the pixel whose corners
are at (31, 42), (31, 43), (32, 42), and (32, 43). The computer works internally
with coordinates that are integer multiples of 65536 ≈ 0.00002 of the width of a
1

pixel, so it is capable of making very ¬ne distinctions. But will never
make a pixel half black; it™s all or nothing, as far as the output is concerned.
The ¬neness of a grid is usually called its resolution, and resolution is
usually expressed in pixel units per inch (in America) or pixel units per millimeter
(elsewhere). For example, the type you are now reading was prepared by -
with a resolution of slightly more than 700 pixels to the inch, but with
slightly fewer than 30 pixels per mm. For the time being we shall assume that the
pixels are so tiny that the operation of rounding to whole pixels is unimportant;
later we will consider the important questions that arise when is
producing low-resolution output.
It™s usually desirable to write programs that can manu-
facture fonts at many di¬erent resolutions, so that a variety of low-resolution
printing devices will be able to make proofs that are compatible with a variety
of high-resolution devices. Therefore the key points in programs are
rarely speci¬ed in terms of pure numbers like ˜100™ ; we generally make the co-
ordinates relative to some other resolution-dependent quantity, so that changes
will be easy to make. For example, it would have been better to use a de¬nition
something like the following, for the six points considered earlier:
(x1 , y1 ) = (0, b); (x2 , y2 ) = (a, b); (x3 , y3 ) = (2a, b);
(x4 , y4 ) = (0, 0); (x5 , y5 ) = (a, 0); (x6 , y6 ) = (2a, 0);
then the quantities a and b can be de¬ned in some way appropriate to the desired
resolution. We had a = b = 100 in our previous example, but such constant
values leave us with little or no ¬‚exibility.
Notice the quantity ˜2a™ in the de¬nitions of x3 and x6 ; un-
derstands enough algebra to know that this means twice the value of a, whatever
a is. We observed in Chapter 1 that simple uses of algebra give its
meta-ness. Indeed, it is interesting to note from a historical standpoint that
Cartesian coordinates are named after Ren´ Descartes, not because he invented
e
the idea of coordinates, but because he showed how to get much more out of

* We sometimes use the term “pixel” to mean a square picture element, but sometimes
we use it to signify a one-dimensional unit of length. A square pixel is one pixel-unit
wide and one pixel-unit tall.
Chapter 2: Coordinates 7


that idea by applying algebraic methods. People had long since been using co- ..
z convention
ordinates for such things as latitudes and longitudes, but Descartes observed draw
that by putting unknown quantities into the coordinates it became possible to Union Jack
hex symbol
describe in¬nite sets of related points, and to deduce properties of curves that
were extremely di¬cult to work out using geometrical methods alone.
So far we have speci¬ed some points, but we haven™t actually done any-
thing with them. Let™s suppose that we want to draw a straight line from point 1
to point 6, obtaining



(Figure 2b will be inserted here; too bad you can™t see it now.)




One way to do this with is to say

draw (x1 , y1 ) . . (x6 , y6 ).

The ˜. .™ here tells the computer to connect two points.
It turns out that we often want to write formulas like ˜(x1 , y1 )™, so it
will be possible to save lots of time if we have a special abbreviation for such
things. Henceforth we shall use the notation z1 to stand for (x1 , y1 ); and in
general, zk with an arbitrary subscript will stand for the point (xk , yk ). The
˜draw™ command above can therefore be written more simply as

draw z1 . . z6 .

Adding two more straight lines by saying, ˜draw z2 . . z5 ™ and ˜draw z3 . . z4 ™,
we obtain a design that is slightly reminiscent of the Union Jack:



(Figure 2c will be inserted here; too bad you can™t see it now.)




We shall call this a hex symbol, because it has six endpoints. Notice that the
straight lines here have some thickness, and they are rounded at the ends as
if they had been drawn with a felt-tip pen having a circular nib.
provides many ways to control the thicknesses of lines and to vary the terminal
shapes, but we shall discuss such things in later chapters because our main
concern right now is to learn about coordinates.
If the hex symbol is scaled down so that its height parameter b is exactly
equal to the height of the letters in this paragraph, it looks like this: ˜ ™. Just
8 Chapter 2: Coordinates


for fun, let™s try to typeset ten of them in a row:



How easy it is to do this!*
Let™s look a bit more closely at this new character. The is a bit too
tall, because it extends above points 1, 2, and 3 when the thickness of the lines
is taken into account; similarly, it sinks a bit too much below the baseline (i.e.,
below the line y = 0 that contains points 4, 5, and 6). In order to correct this,
we want to move the key points slightly. For example, point z1 should not be
exactly at (0, b); we ought to arrange things so that the top of the pen is at (0, b)
when the center of the pen is at z1 . We can express this condition for the top
three points as follows:

top z1 = (0, b); top z2 = (a, b); top z3 = (2a, b);

similarly, the remedy for points 4, 5, and 6 is to specify the equations

bot z4 = (0, 0); bot z5 = (a, 0); bot z6 = (2a, 0).

The resulting squashed-in character is



(Figure 2d will be inserted here; too bad you can™t see it now.)




(shown here with the original weight ˜ ™ and also in a bolder version ˜ ™).

EXERCISE 2.5
Ten of these bold hexes produce ˜ ™; notice that
adjacent symbols overlap each other. The reason is that each character has width
2a, hence point 3 of one character coincides with point 1 of the next. Suppose
that we actually want the characters to be completely con¬ned to a rectan-
gular box of width 2a, so that adjacent characters come just shy of touching
( ). Try to guess how the point-de¬ning equa-
tions above could be modi¬ed to make this happen, assuming that
has operations ˜lft ™ and ˜rt ™ analogous to ˜top ™ and ˜bot ™.

* Now that authors have for the ¬rst time the power to invent new symbols with great
ease, and to have those characters printed in their manuscripts on a wide variety
of typesetting devices, we must face the question of how much experimentation is
desirable. Will font freaks abuse this toy by overdoing it? Is it wise to introduce new
symbols by the thousands? Such questions are beyond the scope of this book; but
it is easy to imagine an epidemic of fontomania occurring, once people realize how
much fun it is to design their own characters, hence it may be necessary to perform
fontal lobotomies.
Chapter 2: Coordinates 9


Pairs of coordinates can be thought of as “vectors” or “displacements” vectors
addition of vectors
as well as points. For example, (15, 8) can be regarded as a command to go multiplication of vector by scalar
right 15 and up 8; then point (15, 8) is the position we get to after starting at negation of vectors
vector subtraction principle
the reference point and obeying the command (15, 8). This interpretation works subtraction of vectors
out nicely when we consider addition of vectors: If we move according to the bracket notation
vector (15, 8) and then move according to (7, ’3), the result is the same as if mediation
of-the-way function
we move (15, 8) + (7, ’3) = (15 + 7, 8 ’ 3) = (22, 5). The sum of two vectors
z1 = (x1 , y1 ) and z2 = (x2 , y2 ) is the vector z1 + z2 = (x1 + x2 , y1 + y2 ) obtained
by adding x and y components separately. This vector represents the result
of moving by vector z1 and then moving by vector z2 ; alternatively, z1 + z2
represents the point you get to by starting at point z1 and moving by vector z2 .
EXERCISE 2.6
Consider the four fundamental vectors (0, 1), (1, 0), (0, ’1), and (’1, 0). Which
of them corresponds to moving one pixel unit (a) to the right? (b) to the left?
(c) down? (d) up?
Vectors can be subtracted as well as added; the value of z1 ’ z2 is simply
(x1 ’ x2 , y1 ’ y2 ). Furthermore it is natural to multiply a vector by a single
number c: The quantity c times (x, y), which is written c(x, y), equals (cx, cy).
Thus, for example, 2z = 2(x, y) = (2x, 2y) turns out to be equal to z + z. In the
special case c = ’1, we write ’(x, y) = (’x, ’y).
Now we come to an important notion, based on the fact that subtraction
is the opposite of addition. If z1 and z2 are any two points, then z2 ’ z1 is the
vector that corresponds to moving from z1 to z2 . The reason is simply that z2 ’z1
is what we must add to z1 in order to get z2 : i.e., z1 + (z2 ’ z1 ) = z2 . We shall
call this the vector subtraction principle. It is used frequently in
programs when the designer wants to specify the direction and/or distance of
one point from another.
programs often use another idea to express relations be-
tween points. Suppose we start at point z1 and travel in a straight line from
there in the direction of point z2 , but we don™t go all the way. There™s a special
notation for this, using square brackets:
1
3 [z1 , z2 ] is the point one-third of the way from z1 to z2 ,
1
2 [z1 , z2 ] is the point midway between z1 and z2 ,
.8[z1 , z2 ] is the point eight-tenths of the way from z1 to z2 ,
and, in general, t[z1 , z2 ] stands for the point that lies a fraction t of the way from
z1 to z2 . We call this the operation of mediation between points, or (informally)
the “of-the-way function.” If the fraction t increases from 0 to 1, the expression
t[z1 , z2 ] traces out a straight line from z1 to z2 . According to the vector subtrac-
tion principle, we must move z2 ’ z1 in order to go all the way from z1 to z2 ,
hence the point t of the way between them is
t[z1 , z2 ] = z1 + t(z2 ’ z1 ).
10 Chapter 2: Coordinates


This is a general formula by which we can calculate t[z1 , z2 ] for any given values sidebar
of t, z1 , and z2 . But has this formula built in, so we can use the
bracket notation explicitly.
For example, let™s go back to our ¬rst six example points, and suppose
that we want to refer to the point that™s 2/5 of the way from z2 = (100, 100) to
z6 = (200, 0). In we can write this simply as .4[z2 , z6 ]. And if we
need to compute the exact coordinates for some reason, we can always work them
out from the general formula, getting z2 + .4(z6 ’ z2 ) = (100, 100) + .4 (200, 0) ’
(100, 100) = (100, 100) + .4(100, ’100) = (100, 100) + (40, ’40) = (140, 60).
EXERCISE 2.7
True or false: The direction vector from (5, ’2) to (2, 3) is (’3, 5).
EXERCISE 2.8
Explain what the notation ˜0[z1 , z2 ]™ means, if anything. What about ˜1[z1 , z2 ]™ ?
And ˜2[z1 , z2 ]™ ? And ˜(’.5)[z1 , z2 ]™ ?
EXERCISE 2.9
True or false, for mathematicians: (a) 1 [z1 , z2 ] = 1 1
2 (z1 + z2 ); (b) 3 [z1 , z2 ] =
2
z1 + 2 z2 ; (c) t[z1 , z2 ] = (1 ’ t)[z2 , z1 ].
1
3 3

Let™s conclude this chapter by using ¦
¦
¦
mediation to help specify the ¬ve points in the ¦
¦
¦
stick-¬gure ˜ ™ shown enlarged at the right. The ¦
¦
¦
distance between points 1 and 5 should be a, ¦
¦
and point 3 should be b pixels above the base- (Figure 2e will be in-
line; these values a and b have been predeter- b
¦
serted here; too bad
¦ you can™t see it now.)
¦
mined by some method that doesn™t concern ¦
¦
¦
us here, and so has a “sidebar” parameter s ¦
¦
¦
that speci¬es the horizontal distance of points ¦
¦
1 and 5 from the edges of the type. We shall
assume that we don™t know for sure what the
s ← ’ ’a’ ’ ’ s
’’ ’’
height of the bar line should be; point 2 should
be somewhere on the straight line from point 1
to point 3, and point 4 should be in the corresponding place between 5 and 3,
but we want to try several possibilities before we make a decision.
The width of the character will be s + a + s, and we can specify points
z1 and z5 by the equations
bot z1 = (s, 0); z5 = z1 + (a, 0).
There are other ways to do the job, but these formulas clearly express our in-
tention to have the bottom of the pen at the baseline, s pixels to the right of
the reference point, when the pen is at z1 , and to have z5 exactly a pixels to the
right of z1 . Next, we can say
1
z3 = 2 [x1 , x5 ], b ;
Chapter 2: Coordinates 11


this means that the x coordinate of point 3 should be halfway between the golden ratio
dangerous bend
x coordinates of points 1 and 5, and that y3 = b. Finally, let™s say ELLIS
Descartes
z2 = alpha [z1 , z3 ]; z4 = alpha [z5 , z3 ]; BELL

the parameter alpha is a number between 0 and 1 that governs the position of
the bar line, and it will be supplied later. When alpha has indeed received a
value, we can say
draw z1 . . z3 ; draw z3 . . z5 ; draw z2 . . z4 .
will draw the characters ˜ ™ when alpha varies from 0.2 to
0.5 in steps of 0.05 and when a = 150, b = 250, s = 30. The illustration on the

previous page has alpha = (3 ’ 5 )/2 ≈ 0.38197; this value makes the ratio of

the area above the bar to the area below it equal to ( 5 ’ 1)/2 ≈ 0.61803, the
so-called “golden ratio” of classical Greek mathematics.
(Are you sure you should be reading this paragraph? The “dangerous bend”
sign here is meant to warn you about material that ought to be skipped on ¬rst
reading. And maybe also on second reading. The reader-beware paragraphs sometimes
refer to concepts that aren™t explained until later chapters.)
EXERCISE 2.10
Why is it better to de¬ne z3 as ( 1 [x1 , x5 ], b), rather than to work out the
2
explicit coordinates z3 = (s + 1 a, b) that are implied by the other equations?
2

EXERCISE 2.11
Given z1 , z3 , and z5 as above, explain how to de¬ne z2 and z4 so that all of
the following conditions hold simultaneously:
the line from z2 to z4 slopes upward at a 20—¦ angle;
the y coordinate of that line™s midpoint is 2/3 of the way from y3 to y1 ;
z2 and z4 are on the respective lines z1 . . z3 and z3 . . z5 .
(If you solve this exercise, you deserve an ˜ ™.)




Here, where we reach the sphere of mathematics,
we are among processes which seem to some
the most inhuman of all human activities
and the most remote from poetry.
Yet it is here that the artist has the fullest scope for his imagination.
” HAVELOCK ELLIS, The Dance of Life (1923)

To anyone who has lived in a modern American city (except Boston)
at least one of the underlying ideas of Descartes™ analytic geometry
will seem ridiculously evident. Yet, as remarked,
it took mathematicians all of two thousand years
to arrive at this simple thing.
” ERIC TEMPLE BELL, Mathematics: Queen and Servant of Science (1951)
(page 12)




3
Curves
Chapter 3: Curves 13


Albrecht D¨rer and other Renaissance men attempted to establish mathematical
u D¨ rer
u
curves
principles of type design, but the letters they came up with were not especially four-point method for curves
beautiful. Their methods failed because they restricted themselves to “ruler midpoints
and compass” constructions, which cannot adequately express the nuances of
good calligraphy. gets around this problem by using more powerful
mathematical techniques, which provide the necessary ¬‚exibility without really
being too complicated. The purpose of the present chapter is to explain the
simple principles by which a computer is able to draw “pleasing” curves.
The basic idea is to start with four points (z1 , z2 , z3 , z4 ) and to construct
the three midpoints z12 = 1 [z1 , z2 ], z23 = 1 [z2 , z3 ], z34 = 1 [z3 , z4 ]:
2 2 2




(Figure 3a will be inserted here; too bad you can™t see it now.)




Then take those three midpoints (z12 , z23 , z34 ) and construct two second-order
midpoints z123 = 1 [z12 , z23 ] and z234 = 1 [z23 , z34 ]; ¬nally, construct the third-
2 2
order midpoint z1234 = 1 [z123 , z234 ]:
2




(Figure 3b will be inserted here; too bad you can™t see it now.)




This point z1234 is one of the points of the curve determined by (z1 , z2 , z3 , z4 ).
To get the remaining points of that curve, repeat the same construction on
(z1 , z12 , z123 , z1234 ) and on (z1234 , z234 , z34 , z4 ), ad in¬nitum:



(Figure 3c will be inserted here; too bad you can™t see it now.)




The process converges quickly, and the preliminary sca¬olding (which appears
above the limiting curve in our example) is ultimately discarded. The limiting
curve has the following important properties:
It begins at z1 , heading in the direction from z1 to z2 .
It ends at z4 , heading in the direction from z3 to z4 .
It stays entirely within the so-called convex hull of z1 , z2 , z3 , and z4 ;
i.e., all points of the curve lie “between” the de¬ning points.
14 Chapter 3: Curves


The recursive midpoint rule for curve-drawing was discovered in 1959 by Paul de Casteljau
Bernshte˜ polynomial
±n
de Casteljau, who showed that the curve could be described algebraically by
Bernshte˜
±n
the remarkably simple formula B´zier
e
mediation
z(t) = (1 ’ t)3 z1 + 3(1 ’ t)2 t z2 + 3(1 ’ t)t2 z3 + t3 z4 ,
as the parameter t varies from 0 to 1. This polynomial of degree 3 in t is called
a Bernshte˜ polynomial, because Serge˜ N. Bernshte˜ introduced such functions in
±n ± ±n
1912 as part of his pioneering work on approximation theory. Curves traced out by
Bernshte˜ polynomials of degree 3 are often called B´zier cubics, after Pierre B´zier
±n e e
who realized their importance for computer-aided design during the 1960s.
It is interesting to observe that the Bernshte˜ polynomial of degree 1, i.e., the
±n
function z(t) = (1’t) z1 +t z2 , is precisely the mediation operator t[z1 , z2 ] that
we discussed in the previous chapter. Indeed, if the geometric construction we have just
seen is changed to use t-of-the-way points instead of midpoints (i.e., if z12 = t[z1 , z2 ]
and z23 = t[z2 , z3 ], etc.), then z1234 turns out to be precisely z(t) in the formula above.
No matter what four points (z1 , z2 , z3 , z4 ) are given, the construction on
the previous page de¬nes a curved line that runs from z1 to z4 . This curve is not
always interesting or beautiful; for example, if all four of the given points lie on
a straight line, the entire “curve” that they de¬ne will also be contained in that
same line. We obtain rather di¬erent curves from the same four starting points
if we number the points di¬erently:




(Figure 3d will be inserted here; too bad you can™t see it now.)




Some discretion is evidently advisable when the z™s are chosen. But the four-
point method is good enough to obtain satisfactory approximations to any curve
we want, provided that we break the desired curve into short enough segments
and give four suitable control points for each segment. It turns out, in fact, that
we can usually get by with only a few segments. For example, the four-point
method can produce an approximate quarter-circle with less than 0.06% error; it
never yields an exact circle, but the di¬erences between four such quarter-circles
and a true circle are imperceptible.
All of the curves that draws are based on four points, as just
described. But it isn™t necessary for a user to specify all of those points, because
the computer is usually able to ¬gure out good values of z2 and z3 by itself. Only
the endpoints z1 and z4 , through which the curve is actually supposed to pass,
are usually mentioned explicitly in a program.
For example, let™s return to the six points that were used to introduce
the ideas of coordinates in Chapter 2. We said ˜draw z1 . . z6 ™ in that chapter,
Chapter 3: Curves 15


in order to draw a straight line from point z1 to point z6 . In general, if three ..
control points
or more points are listed instead of two, will draw a smooth curve cycle
through all the points. For example, the commands ˜draw z4 . . z1 . . z2 . . z6 ™ bean-like shape
tension
and ˜draw z5 . . z4 . . z1 . . z3 . . z6 . . z5 ™ will produce the respective results




(Figure 3e will be inserted here; too bad you can™t see it now.)




(Unlabeled points in these diagrams are control points that has
supplied automatically so that it can use the four-point scheme to draw curves
between each pair of adjacent points on the speci¬ed paths.)
Notice that the curve is not smooth at z5 in the right-hand example,
because z5 appears at both ends of that particular path. In order to get a
completely smooth curve that returns to its starting point, you can say ˜draw
z5 . . z4 . . z1 . . z3 . . z6 . . cycle™ instead:




(Figure 3f will be inserted here; too bad you can™t see it now.)




The word ˜cycle™ at the end of a path refers to the starting point of that path.
believes that this bean-like shape is the nicest way to connect the
given points in the given cyclic order; but of course there are many decent curves
that satisfy the speci¬cations, and you may have another one in mind. You can
obtain ¬ner control by giving hints to the machine in various ways. For example,
the bean curve can be “pulled tighter” between z1 and z3 if you say
draw z5 . . z4 . . z1 . . tension 1.2 . . z3 . . z6 . . cycle;
the so-called tension between points is normally 1, and an increase to 1.2 yields



(Figure 3g will be inserted here; too bad you can™t see it now.)
16 Chapter 3: Curves


An asymmetric e¬ect can be obtained by increasing the tension only at point 1
but not at points 3 or 4; the shape




(Figure 3h will be inserted here; too bad you can™t see it now.)




comes from ˜draw z5 . . z4 . . tension 1 and 1.5 . . z1 . . tension 1.5 and 1 . . z3 . . z6 . .
cycle™. The e¬ect of tension has been achieved in this example by moving two of the
anonymous control points closer to point 1.

It™s possible to control a curve in another way, by telling
what direction to travel at some or all of the points. Such directions are given
inside curly braces; for example,
draw z5 . . z4 {left } . . z1 . . z3 . . z6 {left } . . cycle
says that the curve should be traveling leftward at points 4 and 6. The resulting
curve is perfectly straight from z6 to z5 to z4 :



(Figure 3i will be inserted here; too bad you can™t see it now.)




We will see later that ˜left ™ is an abbreviation for the vector (’1, 0), which
stands for one unit of travel in a leftward direction. Any desired direction can
be speci¬ed by enclosing a vector in {. . .}™s; for example, the command ˜draw
z4 . . z2 {z3 ’ z4 } . . z3 ™ will draw a curve from z4 to z2 to z3 such that the tangent
direction at z2 is parallel to the line z4 . . z3 , because z3 ’ z4 is the vector that
represents travel from z4 to z3 :



(Figure 3j will be inserted here; too bad you can™t see it now.)




The same result would have been obtained from a command such as ˜draw
z4 . . z2 {10(z3 ’ z4 )} . . z3 ™, because the vector 10(z3 ’ z4 ) has the same direction
as z3 ’ z4 . ignores the magnitudes of vectors when they are simply
being used to specify directions.
Chapter 3: Curves 17


EXERCISE 3.1 curl
What do you think will be the result of ˜draw z4 . . z2 {z4 ’ z3 } . . z3 ™, when
points z2 , z3 , z4 are the same as they have been in the last several examples?
EXERCISE 3.2
Explain how to get to draw the wiggly shape



(Figure 3k will be inserted here; too bad you can™t see it now.)




in which the curve aims directly at point 2 when it™s at point 6, but directly
away from point 2 when it™s at point 4. [Hint: No tension changes are needed;
it™s merely necessary to specify directions at z4 and z6 .]
allows you to change the shape of a curve at its endpoints
by specifying di¬erent amounts of “curl.” For example, the two commands
draw z4 {curl 0} . . z2 {z3 ’ z4} . . {curl 0} z3 ;
draw z4 {curl 2} . . z2 {z3 ’ z4} . . {curl 2} z3
give the respective curves



(Figure 3l will be inserted here; too bad you can™t see it now.)




which can be compared with the one shown earlier when no special curl was
requested. (The speci¬cation ˜curl 1™ is assumed at an endpoint if no explicit
curl or direction has been mentioned, just as ˜tension 1™ is implied between points
when no tension has been explicitly given.) Chapter 14 explains more about this.
It™s possible to get curved lines instead of straight lines even when only
two points are named, if a direction has been prescribed at one or both of the
points. For example,
draw z4 {z2 ’ z4 } . . {down } z6
asks for a curve that starts traveling towards z2 but ¬nishes in a
downward direction:


(Figure 3m will be inserted here; too bad you can™t see it now.)
18 Chapter 3: Curves


Here are some of the curves that draws between two points, when for
it is asked to move outward from the left-hand point at an angle of 60—¦ , and step
until
to approach the right-hand point at various angles: cm
dir
in¬‚ection point
...


(Figure 3aa will be inserted here; too bad you can™t see it now.)




This diagram was produced by the program
for d = 0 step 10 until 120:
draw (0, 0){dir 60} . . {dir ’d}(6cm , 0); endfor;
the ˜dir™ function speci¬es a direction measured in degrees counterclockwise from a
horizontal rightward line, hence ˜dir ’d™ gives a direction that is d—¦ below the horizon.
The lowest curves in the illustration correspond to small values of d, and the highest
curves correspond to values near 120—¦ .
A car that drives along the upper paths in the diagram above is always turning
to the right, but in the lower paths it comes to a point where it needs to turn to
the left in order to reach its destination from the speci¬ed direction. The place where a
path changes its curvature from right to left or vice versa is called an “in¬‚ection point.”
introduces in¬‚ection points when it seems better to change the curvature
than to make a sharp turn; indeed, when d is negative there is no way to avoid points
of in¬‚ection, and the curves for small positive d ought to be similar to those obtained
when d has small negative values. The program
for d = 0 step ’10 until ’90:
draw (0, 0){dir 60} . . {dir ’d}(6cm , 0); endfor
shows what does when d is negative:




(Figure 3bb will be inserted here; too bad you can™t see it now.)




It is sometimes desirable to avoid points of in¬‚ection, when d is positive, and
to require the curve to remain inside the triangle determined by its initial and
¬nal directions. This can be achieved by using three dots instead of two when you
specify a curve: The program
for d = 0 step 10 until 120:
draw (0, 0){dir 60} . . . {dir ’d}(6cm , 0); endfor
Chapter 3: Curves 19


generates the curves bounded
controls
DESCARTES
D¨ rer
u
Tory
Da Vinci
Serlio
(Figure 3cc will be inserted here; too bad you can™t see it now.)
GOUDY




which are the same as before except that in¬‚ection points do not occur for the small
values of d. The ˜. . .™ speci¬cation keeps the curve “bounded” inside the triangle that
is de¬ned by the endpoints and directions; but it has no e¬ect when there is no such
triangle. More precisely, suppose that the curve goes from z0 to z1 ; if there™s a point z
such that the initial direction is from z0 to z and the ¬nal direction is from z to z1 ,
then the curve speci¬ed by ˜. . .™ will stay entirely within the triangle whose corners are
z0 , z1 , and z. But if there™s no such triangle (e.g., if d < 0 or d > 120 in our example
program), both ˜. . .™ and ˜. .™ will produce the same curves.

In this chapter we have seen lots of di¬erent ways to get
to draw curves. And there™s one more way, which subsumes all of the others.
If changes to tensions, curls, directions, and/or boundedness aren™t enough to
produce the sort of curve that a person wants, it™s always possible as a last
resort to specify all four of the points in the four-point method. For example,
the command
draw z4 . . controls z1 and z2 . . z6
will draw the following curve from z4 to z6 :



(Figure 3n will be inserted here; too bad you can™t see it now.)




And so I think I have omitted nothing
that is necessary to an understanding of curved lines.
´
” RENE DESCARTES, La G´om´trie (1637)
e e

Rules or substitutes for the artist™s hand must necessarily be inadequate,
although, when set down by such men as
D¨rer, Tory, Da Vinci, Serlio, and others,
u
they probably do establish canons of proportion and construction
which a¬ord a sound basis upon which to present new expressions.
” FREDERIC W. GOUDY, Typologia (1940)
(page 20)




4
Pens
Chapter 4: Pens 21


Our examples so far have involved straight lines or curved lines that look as pen
nib
if they were drawn by a felt-tip pen, where the nib of that pen was perfectly bean
round. A mathematical “line” has no thickness, so it™s invisible; but when we scaled
pickup
plot circular dots at each point of an in¬nitely thin line, we get a visible line draw
that has constant thickness. pencircle
pt
Lines of constant thickness have their uses, but also provides xscaled
several other kinds of scrivener™s tools, and we shall take a look at some of them yscaled
rotated
in this chapter. We™ll see not only that the sizes and shapes of pen nibs can be
varied, but also that characters can be built up in such a way that the outlines
of each stroke are precisely controlled.
First let™s consider the simplest extensions of what we have seen before.
The letter ˜ ™ of Chapter 2 and the kidney-bean ˜ ™ of Chapter 3 were drawn
with circular pen nibs of diameter 0.4 pt, where ˜pt™ stands for a printer™s point;*
0.4 pt is the standard thickness of a ruled line ˜ ™ drawn by TEX. Such a
penpoint can be speci¬ed by telling to

pickup pencircle scaled 0.4pt ;

will use the pen it has most recently picked up whenever it is asked
to ˜draw™ anything. A pencircle is a circular pen whose diameter is the width
of one pixel. Scaling it by 0.4pt will change it to the size that corresponds
to 0.4 pt in the output, because pt is the number of pixels in 1 pt. If the key
points (z1 , z2 , z3 , z4 , z5 , z6 ) of Chapters 2 and 3 have already been de¬ned, the
commands

pickup pencircle scaled 0.8pt ;
draw z5 . . z4 . . z1 . . z3 . . z6 . . cycle

will produce a bean shape twice as thick as before: ˜ ™ instead of ˜ ™.
More interesting e¬ects arise when we use non-circular pen nibs. For
example, the command

pickup pencircle xscaled 0.8pt yscaled 0.2pt

picks up a pen whose tip has the shape of an ellipse, 0.8 pt wide and 0.2 pt tall;
magni¬ed 10 times, it looks like this: ˜ ™. (The operation of “xscaling” mul-
tiplies x coordinates by a speci¬ed amount but leaves y coordinates unchanged,
and the operation of “yscaling” is similar.) Using such a pen, the ˜ ™ becomes
˜ ™, and ˜ ™ becomes ˜ ™. Furthermore,

pickup pencircle xscaled 0.8pt yscaled 0.2pt rotated 30

takes that ellipse and rotates it 30—¦ counterclockwise, obtaining the nib ˜ ™; this
changes ˜ ™ into ˜ ™ and ˜ ™ into ˜ ™. An enlarged view of the bean shape shows

* 1 in = 2.54 cm = 72.27 pt exactly, as explained in The TEXbook.
22 Chapter 4: Pens


more clearly what is going on: draw
T
METAFONT logo
bounding box
box
h
w
(Figure 4a will be inserted here; too bad you can™t see it now.)




The right-hand example was obtained by eliminating the clause ˜yscaled 0.2pt ™;
this makes the pen almost razor thin, only one pixel tall before rotation.
EXERCISE 4.1
Describe the pen shapes de¬ned by (a) pencircle xscaled 0.2pt yscaled 0.8pt ;
(b) pencircle scaled 0.8pt rotated 30; (c) pencircle xscaled .25 scaled 0.8pt .
EXERCISE 4.2
We™ve seen many examples of ˜draw™ used with two or more points. What do
you think will do if you ask it to perform the following commands?
draw z1 ; draw z2 ; draw z3 ; draw z4 ; draw z5 ; draw z6 .
Let™s turn now to the design of a real letter that has already appeared
many times in this manual, namely the ˜ ™ of ˜ ™. All seven of the
distinct letters in ˜ ™ will be used to illustrate various ideas as we get
into the details of the language; we might as well start with ˜ ™, because it
occurs twice, and (especially) because it™s the sim- (0,h) (w,h)
plest. An enlarged version of this letter is shown at
the right of this paragraph, including the locations
of its four key points (z1 , z2 , z3 , z4 ) and its bounding
box. Typesetting systems like TEX are based on the
assumption that each character ¬ts in a rectangular (Figure 4b will be inserted here;
too bad you can™t see it now.)
box; we shall discuss boxes in detail later, but for
now we will be content simply to know that such
boundaries do exist.* Numbers h and w will have
been computed so that the corners of the box are at
positions (0, 0), (0, h), (w, 0), and (w, h) as shown. (0,0) (w,0)
Each of the letters in ˜ ™ is drawn
with a pen whose nib is an unrotated ellipse, 90% as tall as it is wide. In the
10-point size, which is used for the main text of this book, the pen is 2/3 pt wide,

* Strictly speaking, the bounding box doesn™t actually have to “bound” the black pixels
of a character; for example, the ˜ ™ protrudes slightly below the baseline at point 4,
and italic letters frequently extend rather far to the right of their boxes. However,
TEX positions all characters by lumping boxes together as if they were pieces of metal
type that contain all of the ink.
Chapter 4: Pens 23


so it has been speci¬ed by the command o
overshoot
pickup pencircle scaled 2 pt yscaled 9 top
3 10 lft
rt
or something equivalent to this. bot
We shall assume that a special value ˜o™ has been computed so that the coordinates
M
bottom of the vertical stroke in ˜ ™ should descend exactly o pixels below the METAFONT logo
baseline; this is called the amount of “overshoot.” Given h, w, and o, it is a
simple matter to de¬ne the four key points and to draw the ˜ ™:
top lft z1 = (0, h); top rt z2 = (w, h);
top z3 = (.5w, h); bot z4 = (.5w, ’o);
draw z1 . . z2 ; draw z3 . . z4 .
Sometimes it is easier and/or clearer to de¬ne the x and y coordinates sepa-
rately. For example, the key points of the ˜ ™ could also be speci¬ed thus:
w ’ x2 = x1 ;
lft x1 = 0; x3 = x4 = .5w;
bot y4 = ’o;
top y1 = h; y1 = y2 = y3 .
The equation w ’ x2 = x1 expresses the fact that x2 is just as far from the right edge
of the bounding box as x1 is from the left edge.
What exactly does ˜top™ mean in a equation? If the currently-
picked-up pen extends l pixels to the left of its center, r pixels to the right,
t pixels upward and b downward, then
bot z = z ’ (0, b), lft z = z ’ (l, 0),
top z = z + (0, t), rt z = z + (r, 0),
when z is a pair of coordinates. But”as the previous paragraph shows, if you study it
carefully”we also have
bot y = y ’ b, lft x = x ’ l,
top y = y + t, rt x = x + r,
when x and y are single values instead of coordinate pairs. You shouldn™t apply ˜top™
or ˜bot ™ to x coordinates, nor ˜lft ™ or ˜rt ™ to y coordinates.
EXERCISE 4.3
True or false: top bot z = z, whenever z is a pair of coordinates.
EXERCISE 4.4
(0,h) (w,h)
An enlarged picture of ™s
˜ ™ shows that it has ¬ve key points. Assum-
ing that special values ss and ygap have been
precomputed and that the equations
x1 = ss = w ’ x5 ; y3 ’ y1 = ygap (Figure 4c will be inserted here; too bad you
can™t see it now.)
have already been given, what further equa-
tions and ˜draw™ commands will complete the
speci¬cation of this letter? (The value of w
will be greater for ˜ ™ than it was for ˜ ™; it
stands for the pixel width of whatever char- (0,0) (w,0)
acter is currently being drawn.)
24 Chapter 4: Pens


™s ability to ˜draw™ allows it to produce character shapes that ¬ll
bean
are satisfactory for many applications, but the shapes are inherently limited by knife
the fact that the simulated pen nib must stay the same through an entire stroke. cycle
triangle
Human penpushers are able to get richer e¬ects by using di¬erent amounts of “
pressure and/or by rotating the pen as they draw. ..
polygonal path
We can obtain ¬ner control over the characters we produce if we specify
their outlines, instead of working only with key points that lie somewhere in the
middle. In fact, works internally with outlines, and the computer
¬nds it much easier to ¬ll a region with solid black than to ¬gure out what pixels
are blackened by a moving pen. There™s a ˜¬ll™ command that does region ¬lling;
for example, the solid bean shape




(Figure 4d will be inserted here; too bad you can™t see it now.)




can be obtained from our six famous example points by giving the command
¬ll z5 . . z4 . . z1 . . z3 . . z6 . . cycle.
The ¬lled region is essentially what would be cut out by an in¬nitely sharp knife
blade if it traced over the given curve while cutting a piece of thin ¬lm. A draw
command needs to add thickness to its curve, because the result would otherwise
be invisible; but a ¬ll command adds no thickness.
The curve in a ¬ll command must end with ˜cycle™, because an entire
region must be ¬lled. It wouldn™t make sense to say, e.g., ˜¬ll z1 . . z2 ™. The cycle
being ¬lled shouldn™t cross itself, either; would have lots of trouble
trying to ¬gure out how to obey a command like ˜¬ll z1 . . z6 . . z3 . . z4 . . cycle™.
EXERCISE 4.5
Chapter 3 discusses the curve z5 . . z4 . . z1 . . z3 . . z6 . . z5 , which isn™t smooth
at z5 . Since this curve doesn™t end with ˜cycle™, you can™t use it in a ¬ll command. But

. 1
( 13)



>>