Introduction to PostScript

5.2 Introduction to PostScript

PostScript is an interpreted page description programming language. Although it is most commonly used in printers, the language is powerful enough that it can be applied in other ways, such as what you will be doing in your course.

Since the language is interpreted, you will need an interpreter before you can do any real PostScript programming. Fortunately, there exists a public domain implementation called ghostscript. This is installed in the undergraduate lab, and you can access it by typing gs at a shell command prompt. You can also get copies of ghostscript from the ghostscript home page.

Programming in PostScript is very much like programming an HP calculator, so those who are familiar with HP's will have no difficulties getting used to it. At the heart of the issue is an object called a stack. Data can be stored in the stack, operators take their arguments from the stack and they return thier output to the stack. This storing of information doesn't occur randomly - rather data is retrieved from the stack in the reverse order from how it was put on the stack. The model you should have in mind is a stack of plates: you can add plates to the top or take plates from the top. The plate you take from the top is the the last plate that was put on the stack.

The following example shows what the stack looks like during a simple ghostscript session. For it, you will need to know that add takes the last two objects on the stack and places their sum on the stack, mul works similarly, and sqrt takes the last argument from the stack and places its square root on the stack. Also, = removes the last object from the stack and prints it. Next to each command, you will see what the stack looks like after the command is executed. This is not standard ghostscript -- it has been added here to help you see what is happening. The top of the stack (where objects are added to and removed from) is on the left of the list.

Ghostscript Sesssion               Stack Contents

GS>3                               3
GS<1>3                             3 3
GS<2>mul                           9
GS<1>4                             4 9
GS<2>4                             4 4 9
GS<3>mul                           16 9
GS<2>add                           25
GS<1>sqrt                          5
GS<1>=
5.0
GS>

You should notice how operators take objects from the stack. The name for this kind of syntax (arguments before operation) is called Reverse Polish Notation, or RPN. Also, at each prompt you can see how deep the stack is on the prompt: GS<3> means that there are three objects on the stack. You can see the entire stack at once with the stack command. This does not remove anything from the stack, unlike the = command. If you just want to see the last object on the stack, and you don't want to loose it, be sure to duplicate it first with the dup command. This creates a copy so that when you give the = command, the copy is removed from the stack, but the original remains. The following example shows how these commands work.

GS>5
GS<1>4
GS<2>3
GS<3>8
GS<4>stack
8
3
4
5

GS<4>=
8

GS<3>stack
3
4
5

GS<3>dup   
GS<4>=
3

GS<3>stack
3
4
5


David Maxwell, who is still writing this, would like to hear your comments and suggestions. And remember, parts of this manual are based on P.J. Weingartner's work: A First Guide to PostScript.