Version 16 of Postscript

Updated 2003-02-26 21:06:46

Postscript is a programming language (in reverse polish notation, see RPN in Tcl) that is used mostly for representing printable documents. More decent printers usually understand (interpret) Postscript right away.

Popular tools to visualize Postscript files are ghostscript/ghostview [L1 ]. - RS: Recent versions of the Img library allow Tcl to render Postscript too. Here's a tiny renderer that can do big pages (notice that Postscript is nowhere mentioned - image create just can do it, thanks to Img):


 package require Img
 canvas .c -xscrollc ".x set" -yscrollc ".y set"
 scrollbar .x -ori hori -command ".c xview"
 scrollbar .y -ori vert -command ".c yview"
 grid .c .y -sticky news
 grid .x    -sticky news
 grid rowconfig . 0 -weight 1
 grid columnconfig . 0 -weight 1   
 set im [image create photo -file [lindex $argv 0]]
 .c create image 0 0 -image $im -anchor nw
 .c configure -scrollregion [.c bbox all]

Also, Tk's canvas widget can dump most of its contents in Postscript - obviously, with the postscript subcommand.

The Postscript Language Reference Manual and some related specifications can be found at [L2 ].

Syntax in brief: Words are separated by whitespace, but the ten special characters %/(){}[]<> also end the previous word (with some exceptions). % starts a comment, that continues to the end of the line. / before a word makes that word literal (prevents that it is executed). Parentheses delimit strings; \ is escape character in strings. Braces delimit procedures (executable arrays); they work sort of like in Tcl. Brackets construct normal arrays, which are similar to lists in Tcl, except that they have fixed length :-(. The PS code

 a [b /c /d e] f

is sort of equivalent to Tcl

 $a [list $b c d $e] $f

or (if f is more of a command than a variable)

 f $a [list $b c d $e]

A typical Postscript file starts with a %! comment, and it usually contains a couple of %% comments that contain markup for programs that manipulate Postscript files.

 %!
 %%Creator: Gif2PS
 /#copies 1 def

The last line is an assignment: "assign to the variable #copies the value 1", or set #copies 1 as we would say in Tcl.

See also Manipulating Postscript.


Postscript also has a few other interesting datatypes (notably dictionaries, which are sort-of like arrays in Tcl) and it is possible to write postscript code that manipulates functions written in postscript. In fact, functions are really executable arrays (arrays of PS primitives) that are bound to a name in a dictionary that's on the dictionary stack (which effectively forms a search path for command name resolution.)

Note that you don't have any local variables. But you can insert a private dictionary into an executable array in a way that functions virtually the same. Which is either cute or disgusting, depending on your point-of-view. :^)


Category Language