Coordinates specified in a PostScript program refer to locations within a coordinate system that always bears the same relationship to the current page, regardless of the output device on which printing or displaying will be done. This coordinate system is called user space.
For example, to clear a square at an abstract position indicated by two coordinates on the operand stack, you can define:/cs { gsave translate 1 setgray 0 0 1 1 rectfill grestore } bind def
and use it like:5 7 cs
in the code that emits animation steps.
/sl { gsave translate 0.5 0.28 translate dup stringwidth pop -2 div 0 moveto
0 setgray /Palatino-Roman 0.8 selectfont show grestore } bind def
(x) 5 3 sl
User-defined procedures allow for very concise and simple instructions to show individual steps of the animation.50 dup scale
at the beginning of your PostScript definitions. We have already seen applications of translate in the previous snippets. Notice also the use of gsave and grestore to save and restore the graphics state, so that the user space is only changed temporarily to conveniently perform a particular drawing operation.run executes the contents of the specified file. In other words, it interprets the characters in that file as a PostScript program. When run encounters end-of-file or terminates for some other reason (for example, execution of the stop operator), it closes the file.
For example, you can run load the definitions contained in preface.ps with:(preface.ps) run
Note that this requires the use of the -dNOSAFER option of GhostScript.gs -dNOSAFER -dNOPROMPT -q
GhostScript then reads PostScript instructions from standard input, updating the display continuously. Using this feature, you can show a real-time animation of your program states by piping the PostScript instructions your program emits to GhostScript:./your_program | gs -dNOSAFER -dNOPROMPT -q
The first instruction your program should generate is:(preface.ps) run
as explained above. After that, your program simply emits instructions of your mini-language for showing individual steps of the animation. For example, the stream of output may look like:
(preface.ps) run
0 0 cs
(a) 7 8 sl
(b) 3 7 sl
3 7 cs
(c) 3 7 sl
-gnumber1xnumber2
This lets you specify the width and height of the GhostScript
window. Example: -g500x200.
-rnumber
This lets you set a resolution to scale the graphical output
according to the device resolution.
Example: -r100.
./your_program | gs -dNOSAFER -dNOPROMPT -q preface.ps
However, the way outlined above (i.e., including "(preface.ps) run" in the emitted instructions) makes the PostScript output self-contained: You can you can then also write it to a file and view it later, again using the -dNOSAFER option../your_program | gs -dNOSAFER -sDEVICE=bbox -q -
This shows you the smallest BoundingBox containing all graphical elements.Acknowledgments: I first saw the idea of using PostScript for animations in the excellent Prolog teaching environment GUPU, created by Ulrich Neumerkel. Thank you!