The POSTSCRIPT language is not as highly structured as other
programming languages such as Pascal or Algol. There is no ex- plicit method for specifying the scope of variables. Instead one has to ‘‘simulate’’ the scoping mechanism through the careful use of dictionaries, the dictionary stack, and the dictionary operators.
First let’s review some of the basic concepts underlying the dic- tionary mechanism. The def operator associates a key with a value and that key-value pair is stored in the current dictionary. The current dictionary is always the topmost dictionary on the dictionary stack. A new dictionary can be created (using the dict operator) and it can be pushed onto the dictionary stack (using the begin operator), thereby making it the current dictionary.
When the POSTSCRIPTinterpreter encounters a name, it searches
for a definition of that name in the dictionary stack beginning with the topmost (current) dictionary and working its way down the dictionary stack until it finds the first instance of that name. Due to the nature of this name search process, dictionaries be- come the context for the scope of names.
Local variables are simulated by creating a new dictionary,
pushing it onto the dictionary stack, performing def operations, and then popping that new dictionary. As long as the new dic- tionary remains on the dictionary stack, we will find the ‘‘local’’ value for the variable when a name search is done. Once the new dictionary is popped from the dictionary stack, the values for names defined within the context of this dictionary will no longer be found (although if a variable by that same name were defined in another dictionary still on the dictionary stack, that value would be returned in the name search). Methodically push- ing and popping dictionaries is what gives variables their scope. The following example illustrates this mechanism:
Example:
/thestring (global) def % 1
thestring = % 2
/exampledict 1 dict def % 3
exampledict begin % 4
thestring = % 5
/thestring (local) def % 6
thestring = % 7
end % 8
thestring = % 9
The output produced by this program looks like:
global global local global
Description of the program: The first line defines the variable
‘‘thestring’ to have the value ‘‘(global).’’ Line 2 prints the value of ‘‘thestring’’ on the standard output. Line 3 creates a diction- ary called ‘‘exampledict’’ to be used for local storage of vari- ables. Line 4 pushes ‘‘exampledict’’ onto the dictionary stack, making it the current dictionary. Line 5 prints the value of ‘‘thestring’’ again. Since ‘‘thestring’’ has not yet been defined in the current dictionary, the value in the next-to-topmost diction- ary is printed. Line 6 defines ‘‘thestring’’ to have the value ‘‘(local)’’ within the context of ‘‘exampledict’’ and this value is the one found when ‘‘thestring’’ is printed in line 7. Line 8 pops ‘‘exampledict’’ from the dictionary stack. Line 9 prints the
original value of ‘‘thestring’’ since the value defined in ‘‘exampledict’’ is no longer found.
The dictionary mechanism can be used with POSTSCRIPT
procedures to simulate local variables in the following manner: create a new dictionary that is large enough to hold all the defini- tions made within the procedure. The first operation in the proce- dure should push this dictionary onto the dictionary stack and the last operation in the procedure should pop it from the dictionary stack. The following is a small example that can be used as a template:
/localdict 1 dict def /sampleproc
{ localdict begin /localvariable 6 def end
} def
In general it is not a good idea to create the dictionary within the procedure because each procedure call allocates new memory for the dictionary. This can use up a lot of virtual memory if the procedure is called repeatedly. The following example illustrates a procedure that creates a new dictionary each time the proce- dure is executed:
/sampleproc
{ 1 dict begin % this allocates new VM each time /localvariable 6 def
end } def
Although it uses more memory, the above method does have the advantage that each time the procedure is called, an entirely new context is created, whereas with the previous method, the old context is invoked each time the procedure is called.
There is another method for pushing a dictionary onto the dic- tionary stack as the first operation in a procedure without having to give the dictionary a name. This technique is advantageous for two reasons. The first reason is that it serves as a form of ‘‘information hiding’’ since the dictionary cannot be accessed by name; it can only be accessed within the procedure that contains
it. The second reason is that it saves key (name) space in the enclosing dictionary where the procedure definition is made since the dictionary itself has no name; the savings on name space become significant when many procedures requiring local variables are defined in a program.
/sampleproc % 1
{ 0 begin % 2
/localvariable 6 def % 3
end % 4
} def % 5
/sampleproc load 0 1 dict put % 6
Recall that procedures are actually executable arrays. The ‘‘0’’ in line 2 of the program merely serves as a placeholder for the reference to the local dictionary. Line 6 creates the dictionary and inserts it into the placeholder position. First the procedure is pushed onto the operand stack as an array object. Then the dic- tionary is created and inserted as the zeroth element of the proce- dure array. From now on a reference to the dictionary will exist in the zeroeth position of the procedure array. When the proce- dure is called, the first operation pushes the dictionary onto the dictionary stack. This technique is used in the programs ‘‘Creating an Analytic Font’’ and ‘‘Creating a Bitmap Font.’’
1 inch 72 points
135
Program 1 / Repeated Shapes
This program prints a rosette design by defining a section of that design and then printing that section repeatedly. This program illustrates the for and arc operators, and it shows how coordinate transformations can be nested to use the most convenient coordinate system for each part of a design.
/inch {72 mul} def
/wedge { newpath 0 0 moveto 1 0 translate 15 rotate 0 15 sin translate 0 0 15 sin -90 90 arc closepath } def
Define an ‘‘ice cream cone’’ shape with the arc operator. This shape will have a 30 degree angle topped off with a semicircle. Set the path’s first point at the current origin. Next, move the origin to the center of the semicircle by translating to the right 1 unit, rotating counter-clockwise by 15 degrees, and translating ‘‘up’’ in the rotated system by the radius of the semicircle. The
arc operator includes a straight line to the initial point
of the arc and a curved section to the end of the arc. Note that the semicircle goes from -90 degrees to 90 degrees in the rotated coordinate system.
gsave Remember the default coordinate system.
3.75 inch 7.25 inch translate Move into position for a sample of the wedge.
1 inch 1 inch scale Make the edge of the wedge 1 inch long.
wedge 0.02 setlinewidth stroke Draw the wedge with a 1/50 inch thick line.
grestore Get back to default coordinates.
gsave
4.25 inch 4.25 inch translate Move into position for the rosette.
1.75 inch 1.75 inch scale Make the edges of the rosette 1 3/4 inches long.
0.02 setlinewidth Use a 7/200 inch thick line.
1 1 12 Set up the for operator to iterate 12 times.
{ 12 div setgray Divide the loop index by 12 to set a gray value.
gsave wedge
Enclose the ‘‘wedge’’ operation in a gsave - grestore pair, as it will transform the coordinate system.
gsave fill grestore Save the wedge path for use after the fill.
0 setgray stroke Draw a black border around the wedge.
grestore Get out of the coordinate system left by wedge.
30 rotate Set up for the next section.
} for Close the procedure body and execute the for operator.
grestore
1 inch 72 points
137
Program 2 / Expanded and Constant Width Lines
This example demonstrates different effects achieved under the scaling transformation. Normally the line width used with the stroke operator is scaled according to the current user coordinate system. This is
demonstrated in the set of squares drawn on the left side of the page. It is possible to maintain a constant line width although the user coordinate system is being scaled arbitrarily. This is shown in the set of squares drawn on the right side of the page.
/inch {72 mul} def /centersquare { newpath .5 .5 moveto -.5 .5 lineto -.5 -.5 lineto .5 -.5 lineto closepath } def
‘‘centersquare’’ will draw a unit square centered on the current coordinate system origin. A square described in terms of its center, rather than in terms of one of its corners, is more convenient for this example since we will be drawing concentric squares.
gsave Remember the original coordinate system.
2.5 inch 6 inch translate Place the origin for the expanding line width squares.
1 16 div setlinewidth
1 1 5 Set up a ‘‘for’’ loop to execute five times.
{ gsave Remember the current coordinate system.
.5 mul inch dup scale Scale the current units by 1/2 inch times the loop index.
centersquare stroke
The stroked square has a line width proportional to the current scale.
grestore Return to the translated, unscaled coordinate system.
} for
grestore Return to the original untranslated coordinate system.
gsave
6 inch 6 inch translate Place the origin for the constant line width squares.
1 setlinewidth Set the line width to be 1 point.
/cmtx matrix currentmatrix def 1 1 5
Store the current transformation matrix, i.e., the current coordinate system, in the variable ‘‘cmtx’’.
{ gsave Remember the translated coordinate system.
.5 mul inch dup scale Scale the squares as before.
centersquare Create the square path, but don’t stroke it yet.
cmtx setmatrix stroke
Change the coordinate space back to the unscaled one, where the line width is truly 1/72nd of an inch thick. We explicitly reset only the coordinate space rather than use a grestore, since grestore resets the current path as well as the current coordinate system.
grestore } for
After stroking the path, return to the translated, unscaled coordinate system.
grestore Return to the original untranslated coordinate system.
1 inch 72 points
139
Program 3 / Elliptical Arcs
This program demonstrates how to build a procedure for drawing elliptical arcs from the basic POSTSCRIPT graphic primitives. It also demonstrates the use of dictionaries to implement local variables.
/ellipsedict 8 dict def Local storage for the procedure ‘‘ellipse.’’
ellipsedict /mtrx matrix put Allocate a matrix for the save matrix operation below; make it local to the procedure ‘‘ellipse.’’
/ellipse
{ ellipsedict begin /endangle exch def /startangle exch def /yrad exch def /xrad exch def /y exch def /x exch def
‘‘ellipse’’ adds a counter-clockwise segment of an elliptical arc to the current path. It takes six operands: the x and y coordinates of the center of the ellipse (the center is defined as the point of intersection of the major and minor axes), the ‘‘radius’’ of the ellipse in the x direction, the ‘‘radius’’ of the ellipse in the y direction, the starting angle of the elliptical arc and the ending angle of the elliptical arc. Since the first operation in this procedure pushes ‘‘ellipsedict’’ onto the dictionary stack and the last pops that dictionary from the dictionary stack, all def operations are local in scope. The basic strategy for defining the ellipse is to translate to the center of the ellipse, scale the user coordinate system by the x and y radius values, and then add a circular arc, centered at the origin with a 1 unit radius to the current path. We will be transforming the user coordinate system with the translate and rotate operators to add the elliptical arc segment but we don’t want these transformations to affect other parts of the program. In other words, we would like to isolate the effect of the transformations. Usually the gsave and
grestore operators would be ideal candidates for this
task. Unfortunately gsave and grestore are
inappropriate for this situation because they do not save the arc segment that has been added to the path. Instead we will isolate the effect of the transformations by saving the current transformation matrix and restoring it explicitly after adding the elliptical arc to the path.
/savematrix mtrx currentmatrix def Save the current transformation.
x y translate Translate to the center of the ellipse.
xrad yrad scale Scale by the x and y radius values.
0 0 1 startangle endangle arc Add the arc segment to the path.
savematrix setmatrix Restore the transformation.
end } def
1 inch 72 points
141
Program 3 / Elliptical Arcs
(continued)
newpath
144 400 72 144 0 360 ellipse stroke
Draw a full ellipse and outline it with a stroke. Note that the y-axis is longer than the x-axis.
newpath
400 400 144 36 0 360 ellipse fill
Draw a full ellipse and fill it with black. Note that the y-axis is shorter than the x-axis.
newpath
300 180 144 72 30 150 ellipse stroke
Draw a portion of an elliptical arc and outline it with a
stroke.
newpath
480 150 30 50 270 90 ellipse fill
Draw a portion of an elliptical arc and fill it with black. Note that although the path is not explicitly closed by the ‘‘ellipse’’ procedure, the fill operation implicitly closes the path for us.
1 inch 72 points
143
Program 4 / Drawing Arrows
This program demonstrates how to define a general procedure for drawing various kinds of straight arrows.
/arrowdict 14 dict def Local storage for the procedure ‘‘arrow.’’
arrowdict begin /mtrx matrix def end
Allocate a matrix for storing the current matrix below. Make it local to the procedure ‘‘arrow.’’
/arrow
{ arrowdict begin /headlength exch def
/halfheadthickness exch 2 div def /halfthickness exch 2 div def /tipy exch def /tipx exch def /taily exch def /tailx exch def
‘‘arrow’’ adds an arrow shape to the current path. It takes seven arguments: the x and y coordinates of the tail (imagine that a line has been drawn down the center of the arrow from the tip to the tail, then x and y lie on this line), the x and y coordinates of the tip of the arrow, the thickness of the arrow in the tail portion, the thickness of the arrow at the widest part of the arrowhead and the length of the arrowhead.
/dx tipx tailx sub def /dy tipy taily sub def
/arrowlength dx dx mul dy dy mul add sqrt def
/angle dy dx atan def
Compute the differences in x and y for the tip and tail. These will be used to compute the length of the arrow and to compute the angle of direction that the arrow is facing with respect to the current user coordinate system origin.
/base arrowlength headlength sub def Compute where the arrowhead joins the tail.
/savematrix mtrx currentmatrix def Save the current user coordinate system. We are using the same technique to isolate the effect of
transformations as was used in the program to draw elliptical arcs.
tailx taily translate Translate to the starting point of the tail.
angle rotate Rotate the x-axis to align with the center line of the arrow.
0 halfthickness neg moveto Add the arrow shape to the current path.
base halfthickness neg lineto base halfheadthickness neg lineto arrowlength 0 lineto
base halfheadthickness lineto base halfthickness lineto 0 halfthickness lineto closepath
savematrix setmatrix Restore the current user coordinate system.
end } def
1 inch 72 points
145
Program 4 / Drawing Arrows
(continued)
newpath
318 340 72 340 10 30 72 arrow fill
Draw a filled arrow with a thin tail and a long arrowhead.
newpath
382 400 542 560 72 232 116 arrow 3 setlinewidth stroke
Draw an outlined arrow with a 90 degree angle at the tip. To get a 90 degree angle, the ‘‘headthickness’’ should be twice the ‘‘headlength.’’
newpath
400 300 400 90 90 200 200 3 sqrt mul 2 div arrow .65 setgray fill
showpage
Draw a gray-filled arrow that has an equilateral triangle as its arrowhead. To get an equilateral triangle, the ‘‘headlength’’ should be the square root of 3 divided by 2 times the ‘‘headthickness.’’
1 inch 72 points
147
Program 5 / Centered Dash Patterns
This program demonstrates the use of the offset argument to the setdash operator to center any dash pattern on a continuous path. The algorithm presented will not give the expected results if the path is discontinuous or closed. Included in this example is a very useful procedure, ‘‘pathlength,’’ that computes the length of an arbitrary path.
/centerdash { /pattern exch def
The procedure ‘‘centerdash’’ will center a dash pattern on a path such that the dashes at the end points are identical. It takes an array describing the dash pattern as its argument.
/pathlen pathlength def In order to center the dash pattern on the path we need to determine the length of the path. (See the definition of ‘‘pathlength’’ below.)
/patternlength 0 def pattern
{ patternlength add /patternlength exch def } forall
First determine the total length of the repeating pattern by summing the elements of the dash array.
pattern length 2 mod 0 ne
{ /patternlength patternlength 2 mul def } if
If the pattern array is an odd number of elements, double the pattern length so that we can get identical end points.
/first pattern 0 get def Get the length of the first element in the pattern array for use later.
/last patternlength first sub def Calculate the length of the remaining part of the pattern.
/n pathlen last sub patternlength idiv def /endpart pathlen patternlength n mul sub last sub 2 div def
Now calculate the offset provided to the setdash operator so that the dashes at the end points are identical. Think of the path as being composed of 4 distinct parts: 2 identical end parts, 1 part which is composed of ‘‘n’’ repeating pattern pieces and 1 part which is the remaining piece of the pattern. We can compute the lengths of the remaining piece and the part composed of ‘‘n’’ repeating pattern pieces and from these determine the length of the end part.
/offset first endpart sub def The amount of offset is then given by the difference in length of the first part and the end part.
pattern offset setdash } def
Set up the dashing parameters using the offset computed above.
1 inch 72 points
149
Program 5 / Centered Dash Patterns
(continued)
/pathlength { flattenpath /dist 0 def
The procedure ‘‘pathlength’’ computes the length of any given path. It does so by first ‘‘flattening’’ the path with the flattenpath operator. flattenpath converts any
curveto and arc segments in a path to a series of lineto segments. Then the pathforall operator is used
to access each segment in the path, find its length and add the length to a total.
{ /yfirst exch def /xfirst exch def
/ymoveto yfirst def /xmoveto xfirst def }