About macenable

computer programmer, group organizer

gsave … grestore

To keep that string from affecting the path of the line, save the graphics state with a gsave command before showing the string. Then, restore it with grestore before the next lineto:

0 0 moveto 306 396 lineto
/Helvetica 10 selectfont
(the effect of showing this string is isolated) show
612 792 lineto
showpage % END OF PROGRAM


Get it? The current point (within the graphics state) at the end of the first line is saved to be used by the start of the second line. The effect of changing the current point by showing the string is thus negated.


show Characters

To paint a string of characters, use the show command, which requires a string argument, and expects the font face and size to have been selected:

/Helvetica 10 selectfont
(any string of text characters) show

The show command creates a path of its own. Look what happens when you combine lines with text:

0 0 moveto 306 396 lineto
/Helvetica 10 selectfont
(this adds to the path and moves the current point) show
612 792 lineto
showpage % END OF PROGRAM

show Characters


Now’s a good time to introduce a little documentation to your programming. Words appearing after a % are comments. (It’s just me, but I like to make them all caps.)

Run the following program in Distiller or Preview to make a PDF file, or in Ghostscript to make an image. You’ll see that all the lines end up running through the exact center of the page:

0 396 moveto 612 396 lineto % HORIZ LINE HALFWAY UP THE PAGE
306 0 moveto 306 792 lineto % VERTICAL LINE HALFWAY OVER
0 0 moveto 612 792 lineto % DIAGONAL LINE UP
0 792 moveto 612 0 lineto % DIAGONAL LINE DOWN
showpage % SEND TO OUTPUT


We need stroke to paint the current path. It expects nothing on the stack and doesn’t push anything onto the stack. The showpage command doesn’t expect any arguments either. It finally creates the PDF file or image.

Line Graphics

Lines are drawn from point to point on the page. The positions of each point are given by two coordinates, the x and y coordinates. They are the number of the point across the page horizontally from the left, and the number of the point vertically from the bottom, respectively. So, the bottom left point is 0 0, and on a letter size page, the top right point is 612 792.

The lineto command is an operator requiring two operands, or arguments. Use it to make a line from the current point (set by the “moveto” command) to the point specified by the lineto arguments. Thus, to create a diagonal from the bottom left to the top right of the page:

0 0 moveto
612 792 lineto

Named Variables

Beside the integer and string types of operands, there is also a “name”
operand. You can recognize a literal name because it is preceded by a
slash. It can be any word you make up. Since all the built-in
commands in Postscript are lower case, you can’t go wrong by throwing
in an upper-case letter or two. Here are some of the names I made up
for a program we’ll be talking about later:


You get the idea.

Literal names are almost always first used with the def operator,
which defines the name as a variable which contains an operand or a
procedure. The Adobe reference manual says

key value def

which means the def operator expects on the stack a key (the name) and
a value (an operand or procedure) that you want the name to be
associated with. The def operator doesn’t leave anything on the stack,
but the result of using it is that your program will know the value of that variable when you use it.

For example, define three variables:

/firstNum 2 def
/secondNum 3 def
/MyProcedure { add } def

Notice that braces go around the add operator to defer it from
executing immediately.

In the program, use the names without the slash prefix in order to
execute them:

firstNum secondNum MyProcedure

is equivalent to saying

2 3 add

The number 5 will be left on the stack to do with as you will, such as
storing it in another variable:

/theResult firstNum secondNum MyProcedure def

You want to increment theResult by two? Do this:

/theResult theResult 2 add def

or even

/bump { 2 add } def
/theResult theResult bump def

By this time the value held by the variable theResult should be
something like 9. How do you find out for sure? In our last lesson we
introduced the “print” operator which is only good for printing
strings. Another built-in procedure is the equal sign,=, which first converts
anything into a string and then prints. Now is a good time to create a
test file, save it as test.ps (or whatever), and run it thru your

/firstNum 2 def
/secondNum 3 def
/MyProcedure { add } def
/theResult firstNum secondNum MyProcedure def
(theResult is ) print theResult =

Reverse Polish Notation

Like most programming languages, Postscript code consists of operators and operands.

Consider the arithmatic operation 2 + 3 = 5. The operands are 2 and 3, and the operator is plus (+). In Postscript this same operation would be coded as

2 3 add

The Postscript operation appears to be the reverse of “add 2 and 3.” The reason for this is that Postscipt is a “stack” based language. The numbers 2 and 3 are “pushed” onto the stack and then subsequently “popped” off the stack by the add operator which expected to find its two operands, the integers, on the stack. The result, 5, is then pushed onto the stack waiting invisibly for the next operation.

Another “type” of operand is the string of characters. Where some programming languages might say

PRINT “Hello world!”

Postscript says:

(Hello world!) print

The term Polish in “reverse-polish notation” refers to the fact that operators and operands are kept to one side or another. Postscript finds the operands to the left.

For instance, the common notation (2 + 3) * (4 – 1) is stated in Postscript as:

2 3 add 4 1 sub mul

Notice how the results of add and sub are left waiting invisibly on the stack for the mul operator. And the final result, 15, is also left waiting on the stack.

Another operator, exch, is very useful in Postscript because it exchanges the top two positions on the stack.

4 1 exch sub

results in the number minus three being left on the stack.

Hello World!

There is an application, comes with Mac OS X, called Preview that
interprets postscript code. With it, you can open a text file containing code like this:

/fontSize 12 def
/charRow 16 string def % BUFFER
/Symbol fontSize selectfont
20 770 translate
0 0 moveto
0 1 15 {
    /y exch 16 mul def
    0 1 15 {
        charRow exch dup y add put
        } for
    gsave charRow show grestore
    0 fontSize neg rmoveto
    } for

and Preview will automatically interpret the code and give you the image.

The above code generates a page showing all the Symbol font characters:

Symbol character set

The Preview application is an interpreter, albeit not a full-featured interpreter like Distiller, but it’s very useful for running simple postscript programs.