Updated 2015-09-23 05:58:58 by APN

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. ([JoGusto] this is a very outmoded statement of "decent" and really applies as a synonym for "expensive" in the 21st century, as the first laser printers with PS capabilities contained more and faster 68K processors and RAM than the computers -- typically Macs -- which were driving them).

Tk's canvas widget can dump most of its contents in Postscript - obviously, with the postscript subcommand. By default, only the window area is dumped, which can be almost nothing if the canvas isn't visible yet. However, by explicitly setting -height and -width in the canvas postscript command, you can get everything dumped, even if the canvas isn't mapped to screen yet. (RS 2005-08-30)

Popular tools to visualize Postscript files are ghostscript/ghostview [1].

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]

kroc - This doesn't works if ghostscript isn't installed: you got the error "couldn't execute 'gs' no such file or directory." because Img uses gs to render postscript files. So you'll need ghostscript anyway.

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

Postscript 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 once created :-(. 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.

FORTH: Postscript's "language" design and "interpreter" architecture owe a great deal to the language FORTH developed in the '70's by Charles Moore and Elizabeth Rather at the National Radio Astronomy Observatory. Anyone who is facile with FORTH reading the PostScript standard and language tutorials immediately recognizes the striking similarities and obvious inspiration. [JoGusto]

Postscript also has a few other interesting datatypes (notably dictionaries, which are sort-of like arrays in Tcl, but often used more like namespaces) 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 for temporary storage the operand stack usually suffices. You can also 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. :^)

PostScript Fonts

The PostScript language supports three main types of fonts: vectorized (e.g. Type 1, TrueType/Type42, CFF/Type2), bitmap (Type 3), and programmatic (also Type 3) fonts. It also supports composite fonts, which are built up out of other fonts. These can be used to handle encodings with more than 256 characters (otherwise Postscript mainly takes the position that character=byte).

In vectorized fonts, each character is described by a path (general shape) which is then filled or stroked to display the character; this make them independent of printer resolution and thus arbitrarily scalable. Bitmapped fonts are not really scalable (you get horrible rendering artifacts) but are easy to create from all sorts of sources. In programmatic fonts, the characters are defined as a piece of Postscript code that draws the character. All fonts also come with various extra pieces of information like [kerning] rules, etc.

Generally, you'll want to stick to vectorized fonts for printing purposes where possible as they look good at all resolutions and sizes. In principle programmatic fonts can do that too, but they are rarely used, because you need a full Postscript interpreter to render them (and furthermore you probably need to be an experienced Postscript hacker to create them). The vectorized font formats are restricted to a much more limited set of operations, which makes it much easier to write a rendering engine for them. Current desktop environments usually have such engines built in or available as extensions (e.g. ATM) for free.

When it comes to rendering Type 1 fonts on screen, they often don't do quite as well as TrueType fonts as Type 1 fonts don't come as heavily annotated with hints for how to handle low-resolution display. This is only really a problem if you don't use antialiasing though.

DKF, 12-Jun-2003; Lars H, 3-Feb-2004.

Management welcomes the use of Postscript as development platform - because it looks good on paper RS :)

CL groans, and thinks that sounds like something MS would say.

TV When the origin is a (La)TeX file, one might prefer to use dvigif instead of going over postscript as it is part of the cygwin distribution, like so (to render a single formula in my case):
   dvigif.exe -x 4000 -T tight -v t.dvi

pi31415 Here is a TCL port of the Perl PostScript::Simple module. It requires TclOO. [3] (pentcl)

DKF: If you're having problems with getting fonts to come out right (especially if you're encountering missing fonts) then try using the -fontmap option to the canvas postscript method. That gives you a lot more control over these things.

WHD - 2009-09-28 15:54:13

Got any advice on how to set up the -fontmap? Figuring out the XLFD name that matches the font I'm using on the canvas is non-trivial.