<< Ļšåä. ńņš. ńņš. 19(īįłåå źīėč÷åńņāī: 45)ĪĆĖĄĀĖÅĶČÅ Ńėåä. ńņš. >>
version at the end of Chapter 3:

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

Hereā™s another example of cutoļ¬ , in which
the endpoints of ā™s ā˜Tā™ have been
ā—¦
cropped at 10 angles to the perpendicular of the
stroke direction:
pickup logo_pen; (Figure 16e will be inserted here;
too bad you canā™t see it now.)
top lft z1=(0,h); top rt z2=(w,h);
top z3=(.5w,h); z4=(.5w,0);
draw z1--z2;
cutoff(z1,170); cutoff(z2,-10);
draw z3--z4; cutoff(z4,-80).

The cutoļ¬ macro of Appendix B deals with several things that weā™ve been
studying recently, so it will be instructive to look at it now (slightly simpliļ¬ed):

def cutoļ¬ (expr z, theta ) =
cut pic := nullpicture;
addto cut pic doublepath z withpen currentpen ;
addto cut pic contour ((0, ā’1) - - (1, ā’1) - - (1, 1) - - (0, 1) - - cycle)
scaled 1.42(1 + max(ā’pen lft , pen rt , pen top , ā’pen bot ))
rotated theta shifted z ;
cull cut pic keeping (2, 2) withweight ā’1;
addto currentpicture also cut pic enddef .

The main work is done in a separate picture variable called cut pic , so that neighboring
strokes wonā™t be aļ¬ected. First cut pic is set to the full digitized pen image (by making
a doublepath from a single point). Then a rectangle that includes the cutoļ¬ region
is added in; pen lft , pen rt , pen top , and pen bot are the quantities used to compute
the functions lft , rt , top , and bot , so they bound the size of the pen. The culling
operation produces the intersection of pen and rectangle, which is ļ¬nally subtracted
from currentpicture .
152 Chapter 16: Calligraphic Eļ¬ects

We shall conclude this chapter by studying two examples of how ā™s tilde
pencircle
pen-and-curve-drawing facilities can combine in interesting ways. First, letā™s
pensquare
examine two ātildeā characters BernshteĖ˜ polynomial
Ä±n
serif
ļ¬lldraw
(Figure 16f&g will be inserted here; too bad you canā™t see it now.)
dishing
ļ¬‚ex
epsilon
which were both created by a single command of the form strange paths
controls
draw z1 . . controls z2 and z3 . . z4 .
The left example was done with a pencircle xscaled .8pt yscaled .2pt rotated 50, and
the right example was exactly the same but with pensquare. The control points z2
and z3 that made this work were deļ¬ned by
y2 ā’ y1 = y4 ā’ y3 = 3(y4 ā’ y1 );
z2 ā’ z1 = z4 ā’ z3 = whatever ā— dir 50.
The second pair of equations is an old calligrapherā™s trick, namely to start and ļ¬nish
a stroke in the direction of the pen youā™re holding. The ļ¬rst pair of equations is a
mathematicianā™s trick, based on the fact that the BernshteĖ˜ polynomial t[0, 3, ā’2, 1]
Ä±n
goes from 0 to 1 to 0 to 1 as t goes from 0 to .25 to .75 to 1.
Next, letā™s try to draw a fancy serif with the same two pens, holding them at
a 20ā—¦ angle instead of a 50ā—¦ angle. Here are two examples

(Figure 16h&i will be inserted here; too bad you canā™t see it now.)

that can be created by ā˜ļ¬lldrawā™ commands:
ļ¬lldraw z1 . . controls z2 . . z3
- - (ļ¬‚ex (z3 , .5[z3 , z4 ] + dishing , z4 )) shifted (0, ā’epsilon )
- - z4 . . controls z5 . . z6 - - cycle.
The dishing parameter causes a slight rise between z3 and z4 ; the ļ¬‚ex has been lowered
by epsilon in order to avoid the danger of āstrange paths,ā which might otherwise be
caused by tiny loops at z3 or z4 . But the most interesting thing about this example
is the use of double control points, z2 and z5 , in two of the path segments. (Recall
that ā˜controls z2 ā™ means the same thing as ā˜controls z2 and z2 ā™.) These points were
determined by the equations
x2 = x1 ; z2 = z3 + whatever ā— dir 20;
x5 = x6 ; z5 = z4 + whatever ā— dir ā’20;
thus, they make the strokes vertical at z1 and z6 , parallel to the pen angle at z3 , and
parallel to the complementary angle at z4 .
Chapter 16: Calligraphic Eļ¬ects 153

EVETTS
REYNOLDS

The pen, probably more than any other tool,
has had the strongest inļ¬‚uence upon lettering
in respect of serif design . . .
It is probable that the letters [of the Trajan column]
were painted before they were incised,
and though their main structure is attributed to the pen
and their ultimate design to the technique of the chisel,
they undoubtedly owe much of their freedom
to the inļ¬‚uence of the brush.
ā” L. C. EVETTS, Roman Lettering (1938)

Remember that it takes time, patience, critical practice
and knowledge to learn any art or craft.
No āart experienceā is going to result from any busy work
for a few hours experimenting with the edged pen.
. . . Take as much time as you require,
and do not become impatient.
If it takes a month to get it,
then be happy that it takes only a month.
ā” LLOYD REYNOLDS, Italic Calligraphy & Handwriting (1969)
(page 154)

17
Grouping
Chapter 17: Grouping 155

We have now covered all the visual, graphic aspects of ā”its points, end
program
paths, pens, and pictures; but we still donā™t know everything about ā™s end
organizational, administrative aspectsā”its programs. The next few chapters of statement list
;
this book therefore concentrate on how to put programs together eļ¬ectively. statements
A program is a sequence of statements separated by semi- empty statement
compound statement
colons and followed by ā˜endā™. More precisely, the syntax rules group
statement
program ā’ā’ statement list end begingroup
endgroup
statement list ā’ā’ empty | statement ; statement list save command
save
deļ¬ne a program in terms of a statement . symbolic token list
,
But what are statements? Well, they are of various kinds. An āequationā interim command
states that two expressions are supposed to be equal. An āassignmentā assigns interim
:=
the value of an expression to a variable. A ādeclarationā states that certain
variables will have a certain type. A ādeļ¬nitionā deļ¬nes a macro. A ātitleā
gives a descriptive name to the character that is to follow. A ācommandā orders
to do some speciļ¬c operation, immediately. The āempty statementā
tells to do absolutely nothing. And a ācompound statementā is a
list of other statements treated as a group.
statement ā’ā’ equation | assignment | declaration
| deļ¬nition | title | command | empty
| begingroup statement list statement endgroup
Weā™ve given the syntax for equation and assignment in Chapter 10; the syntax
for declaration appeared in Chapter 7; deļ¬nition and title and command
will appear in later chapters. Our main concern just now is with the ļ¬nal type
of statement , where begingroup and endgroup bind other statements into a
unit, just as parentheses add structure to the elements of an algebraic expression.
The main purpose of grouping is to protect the values of variables in one
part of the program from being clobbered in another. A symbolic token can be
given a new meaning inside a group, without changing the meaning it had outside
that group. (Recall that deals with three basic kinds of tokens, as
discussed in Chapter 6; it is impossible to change the meaning of a numeric token
or a string token, but symbolic tokens can change meanings freely.)
There are two ways to protect the values of variables in a group. One is
called a save command , and the other is called an interim command :
save command ā’ā’ save symbolic token list
symbolic token list ā’ā’ symbolic token
| symbolic token list , symbolic token
interim command ā’ā’ interim internal quantity := right-hand side
The symbolic tokens in a save command all lose their current meanings, but
those old meanings are put into a safe place and restored at the end of the current
group. Each token becomes undeļ¬ned, as if it had never appeared before. For
156 Chapter 17: Grouping

example, the command tag
spark
internal quantity
save x, y
beginchar
endchar
eļ¬ectively causes all previously known variables like x1 and y5r to become in- tracingrestores
accessible; the variable x1 could now appear in a new equation, where it would
have no connection with its out-of-group value. You could also give the silly
command
save save;
this would make the token ā˜saveā™ itself into a tag instead of a spark , so you
couldnā™t use it to save anything else until the group ended.
An interim command is more restrictive than a save, since it applies only to
an internal quantity . (Recall that internal quantities are special variables
like tracingequations that take numeric values only; a complete list of all the standard
internal quantities can be found in Chapter 25, but that list isnā™t exhaustive because
you can deļ¬ne new ones for your own use.) treats an interim command just
like an ordinary assignment, except that it undoes the assignment when the group ends.
If you save something two or more times in the same group, the ļ¬rst saved
value takes precedence. For example, in the construction
begingroup
...
interim autorounding := 0; save x;
...
interim autorounding := 1; save x;
...
endgroup
the values of autorounding and x after the end of the group will be their previous values
just before the statement ā˜interim autorounding := 0ā™. (Incidentally, these might not
be the values they had upon entry to the group).
Tokens and internal quantities regain their old meanings and values at the end
of a group only if they were explicitly saved in a save or interim command.
All other changes in meaning and/or value will survive outside the group.
The beginchar operation of plain includes a begingroup, and
endchar includes endgroup. Thus, for example, interim assignments can be
made in a program for one character without any eļ¬ect on other characters.
A save command thatā™s not in a group simply clears the meanings of the
symbolic tokens speciļ¬ed; their old meanings are not actually saved, because
they never will have to be restored. An interim command outside a group acts just
like a normal assignment.
If you set the internal quantity tracingrestores to a positive value,
will make a note in your transcript ļ¬le whenever it is restoring the former value
of a symbolic token or internal quantity. This can be useful when youā™re debugging a
program that doesnā™t seem to make sense.
Chapter 17: Grouping 157

Groups can also be used within algebraic expressions. This is the other group expression
whatever
important reason for grouping; it allows to do arbitrarily compli- FULTON
cated things while in the middle of other calculations, thereby greatly increasing WILLIS
the power of macro deļ¬nitions (which we shall study in the next chapter). A
group expression has the general form
begingroup statement list expression endgroup
and it ļ¬ts into the syntax of expressions at the primary level. The meaning
of a group expression is: āPerform the list of statements, then evaluate the
expression, then restore anything that was saved in this group.ā
Group expressions belong in the syntax rules for each type of expression,
but they were not mentioned in previous chapters because it would have been
unnecessarily distracting. Thus, for example, the syntax for numeric primary actually
includes the additional alternative
begingroup statement list numeric expression endgroup.
The same goes for pair primary , picture primary , etc.; Chapter 25 has the complete
rules of syntax for all types of expressions.
EXERCISE 17.1
What is the value of the expression
begingroup x:=x+1; x endgroup + begingroup x:=2x; x endgroup
if x initially has the value a? What would the value have been if the two group
expressions had appeared in the opposite order? Verify your answers using the expr
routine of Chapter 8.
EXERCISE 17.2
Appendix B deļ¬nes whatever to be an abbreviation for the group expression
ā˜begingroup save ?; ? endgroupā™. Why does this work?
EXERCISE 17.3
What is the value of ā˜begingroup save ?; (?, ?) endgroupā™ ?
EXERCISE 17.4
According to exercise 10.2, the assignment ā˜x3 := whatever ā™ will make the
numeric variable x3 behave like new, without aļ¬ecting other variables like x2 . Devise
a similar stratagem that works for arrays of picture variables.

It is often diļ¬cult
to account for some beginners grouping right away
and others proving almost hopeless.
ā” A. G. FULTON, Notes on Riļ¬‚e Shooting (1913)

Rock bands prefer San Francisco groupies to New York groupies.
ā” ELLEN WILLIS, But Now Iā™m Gonna Move (1971)
(page 158)

18
Deļ¬nitions
(also called Macros)
Chapter 18: Deļ¬nitions (also called Macros) 159

You can often save time writing programs by letting single tokens ā”
deļ¬nitions
stand for sequences of other tokens that are used repeatedly. For example, replacement text
Appendix B deļ¬nes ā˜- - -ā™ to be an abbreviation for ā˜. . tension inļ¬nity . .ā™, and parameters
arguments
this deļ¬nition is preloaded as part of the plain base. Programs that rotatedaround
use such deļ¬nitions are not only easier to write, theyā™re also easier to read. But capsules
:=
Appendix B doesnā™t contain every deļ¬nition that every programmer might want; assignment
the present chapter therefore explains how you can make deļ¬nitions of your own.
In the simplest case, you just say
def symbolic token = replacement text enddef
and the symbolic token will henceforth expand into the tokens of the replacement
text. For example, Appendix B says
def --- = ..tension infinity.. enddef.
The replacement text can be any sequence of tokens not including ā˜enddef ā™; or
it can include entire subdeļ¬nitions like ā˜def . . . enddef ā™, according to certain
rules that we shall explain later.
Deļ¬nitions get more interesting when they include parameters, which
are replaced by arguments when the deļ¬nition is expanded. For example, Ap-
pendix B also says
def rotatedaround(expr z,theta) =
shifted -z rotated theta shifted z enddef;
this means that an expression like ā˜z1 rotatedaround (z2 , 30)ā™ will expand into
ā˜z1 shifted ā’z2 rotated 30 shifted z2 ā™.
The parameters ā˜zā™ and ā˜thetaā™ in this deļ¬nition could have been any
symbolic tokens whatever; thereā™s no connection between them and appearances
of ā˜zā™ and ā˜thetaā™ outside the deļ¬nition. (For example, ā˜zā™ would ordinarily
stand for ā˜(x,y)ā™, but itā™s just a simple token here.) The deļ¬nition could even
have been written with āprimitiveā tokens as parameters, like
def rotatedaround(expr;,+) =
shifted-; rotated+shifted; enddef;
the eļ¬ect would be exactly the same. (Of course, thereā™s no point in doing such
a thing unless you are purposely trying to make your deļ¬nition inscrutable.)
When ā˜rotatedaroundā™ is used, the arguments that are substituted for
z and theta are ļ¬rst evaluated and put into ācapsules,ā so that they will behave
like primary expressions. Thus, for example, ā˜z1 rotatedaround (z2 + z3 , 30)ā™ will
not expand into ā˜z1 shifted ā’z2 + z3 rotated 30 shifted z2 + z3 ā™ā”which means
something entirely diļ¬erentā”but rather into ā˜z1 shifted ā’Ī± rotated 30 shifted Ī±ā™,
where Ī± is a nameless internal variable that contains the value of z2 + z3 .
A capsule value cannot be changed, so an expr parameter should not appear
at the left of the assignment operator ā˜:=ā™.
160 Chapter 18: Deļ¬nitions (also called Macros)

Macros are great when they work, but complicated macros sometimes surprise tracingmacros
EXPR0
their creators. provides ātracingā facilities so that you can see
group expressions
what the computer thinks itā™s doing, when youā™re trying to diagnose the reasons for reļ¬‚ectedabout
unexpected behavior. If you say ā˜tracingmacros := 1ā™, the transcript ļ¬le of your run save
transform
will record every macro that is subsequently expanded, followed by the values of its xxpart
arguments as soon as they have been computed. For example, ā˜rotatedaround (up , 30)ā™ yypart
xypart
might produce the following lines of diagnostic information:
yxpart
special-purpose macros
rotatedaround(EXPR0)(EXPR1)->shifted-(EXPR0)rotated(EXPR1)sh
beginlogochar
ifted(EXPR0) expr
(EXPR0)<-(0,1)
(EXPR1)<-30
Hereā™s another example from Appendix B. It illustrates the usefulness of group
expressions in macro deļ¬nitions:
def reļ¬‚ectedabout (expr p, q) =
transformed begingroup
save T ; transform T ;
p transformed T = p;
q transformed T = q;
xxpart T = ā’yypart T ;
xypart T = yxpart T ;
T endgroup enddef ;
thus a new transform, T , is computed in the midst of another expression, and the
macro ā˜reļ¬‚ectedabout(p, q)ā™ essentially expands into ā˜transformed T ā™.
Some macros, like ā˜rotatedaroundā™, are meant for general-purpose use.
But itā™s also convenient to write special-purpose macros that simplify the devel-
opment of particular typefaces. For example, letā™s consider the logo
from this standpoint. The program for ā˜ ā™ in Chapter 11 starts with
beginchar("E",14u#+2s#,ht#,0); pickup logo_pen;
and the programs for ā˜ ā™, ā˜ ā™, etc., all have almost the same beginning. Therefore
we might as well put the following deļ¬nition near the top of the ļ¬le logo.mf:
def beginlogochar(expr code, unit_width) =
beginchar(code,unit_width*u#+2s#,ht#,0);
pickup logo_pen enddef;
Then we can start the ā˜ ā™ by saying simply
beginlogochar("E",14);
similar simpliļ¬cations apply to all seven letters. Notice from this example that
macros can be used inside macros (since ā˜begincharā™ and ā˜pickupā™ are them-
selves macros, deļ¬ned in Appendix B); once you have deļ¬ned a macro, you have
essentially extended the language. Notice also that expr parame-
ters can be expressions of any type; for example, "E" is a string, and the ļ¬rst
parameter of ā˜rotatedaroundā™ is a pair.
Chapter 18: Deļ¬nitions (also called Macros) 161

Chapter 11 didnā™t give the programs for superellipse
suļ¬x
ā˜ ā™ or ā˜ ā™. It turns out that those programs can suļ¬x
be simpliļ¬ed if we write them in terms of an aux- text
iliary subroutine called ā˜super_halfā™. For exam-
ple, here is how the ā˜ ā™ is made: (Figure 18a will be inserted here; too
bad you canā™t see it now.)
beginlogochar("O",15);
x1=x4=.5w; top y1=h+o; bot y4=-o;
x2=w-x3=1.5u+s; y2=y3=barheight;
super_half(2,1,3);
super_half(2,4,3);
labels(1,2,3,4); endchar;
The super_half routine is supposed to draw half of a superellipse, through three
points whose subscripts are speciļ¬ed.
We could deļ¬ne super_half as a macro with three expr parameters,
referring to the ļ¬rst point as ā˜z[i]ā™, say; but thereā™s a better way. Parameters
to macros can be classiļ¬ed as suļ¬xes, by saying suļ¬x instead of expr. In this
case the actual arguments may be any suļ¬x , i.e., any sequence of subscripts
and tags that complete the name of a variable as explained in Chapter 7. Hereā™s
what super_half looks like, using this idea:
def super_half(suffix i,j,k) =
draw z.i{0,y.j-y.i}
... (.8[x.j,x.i],.8[y.i,y.j]){z.j-z.i}
... z.j{x.k-x.i,0}
... (.8[x.j,x.k],.8[y.k,y.j]){z.k-z.j}
... z.k{0,y.k-y.j} enddef;
EXERCISE 18.1
 << Ļšåä. ńņš. ńņš. 19(īįłåå źīėč÷åńņāī: 45)ĪĆĖĄĀĖÅĶČÅ Ńėåä. ńņš. >>