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 (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.