ńņš. 1 |

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 |