Postscript - A Hardware-Independent Page Description Language

Modern desktop publishing software is the result of the confluence of three major technologies: the WYSIWYG display technology exemplified by the Macintosh, Microsoft Windows and OS/2 Presentation Manager; laser printers and other imaging systems; and page description languages. The most important of these three is the development of page description languages.

Although several such languages have been released over the years, such as DDL and Interpress, it is clear that the market is dominated by PostScript and that this language has become a de facto standard in the desktop publishing and desktop graphics industries.

Postscript was developed primarily by John Warnock of Adobe Systems Inc., with assistance along the way from John Gaffney, Martin Newell, Chuck Geschke, Doug Brotz, Bill Paxton and Ed Taft. It is based upon earlier work performed at Evans and Sutherland and and Xerox PARC, where it shared common antecedents with Interpress.

Postscript is a computer programming language which has a large number of graphics operators which are intended to convey a description of almost any desired page to a printer, through the placement of three different types of graphics objects:


One of the major strengths of PostScript over competing languages is that it performs all calculations using real (or floating point) numbers. This means that it is hardware independent; some VDI's (virtual device interfaces) perform their calculations with integers which correspond to pixel locations on the host machine, but that then limits them to the resolution of that machine. PostScript is capable of producing output to the maximum resolution of any output device, even though the PostScript file may have been generated on some other machine.

This makes possible the use of varying devices for drafting and final output. For example, it is common practice to use a laser printer for draft output and a phototypesetter for final copy. One can be confident that the laser-printed draft is a reasonable approximation of the final, and also that the final will look better.

The default PostScript coordinate system works in units of 1/72 of an inch, i.e. 1 point, relative to the lower left hand corner of the page. However, this is only one possible user coordinate system or user space. PostScript can work with other coordinate systems such as inches or cm, and can also translate, rotate and scale user spaces arbitrarily. Finally, the PostScript interpreter automatically transforms user space into the device space of the target printer - say, 300 dpi.

It is important to realize that PostScript is basically a general-purpose computer language, of which approximately one-third is devoted to graphics. This is what gives it its flexibility: new functions can be defined and built up to higher levels, just as in other languages, and programs can be built in a modular fashion. Although PostScript may look strange at first, it is most closely related to an earlier language called FORTH.

Like FORTH (and Hewlett-Packard calculators), PostScript operates by pushing data onto a stack, combining it in various ways and popping the results off. It also uses postfix notation; that is, data are first placed onto the stack and then an operator is called to place the desired result in place of the data on the stack. So, for example, to add 3 plus 4, one would feed PostScript the line

3 4 add

The stack would look like this:

---  3  4  7
---  3 ---
---

Of course, the result of the addition remains on the stack, as it may be used in further calculations. If you wanted to see the results of the calculation, you would have to give the == command, which removes the top item off the stack and echoes it over the communications channel (typically a serial interface to a PostScript printer, but it may be a network link - typically Apple LocalTalk).

The items on the stack need not be numbers, but can also include strings, arrays and dictionaries. The == operator will print them as best it can. Alternatively, pstack can be used to print the entire stack.

Postscript printers typically operate in two modes: batch, in which a Postscript program is simply dumped to the printer which then runs it and produces output, or in interactive mode, in which the printer produces a prompt on the terminal attached to its serial port and initiates an arbitrarily long dialog with the user. For example, an Apple Laserwriter can be placed into this mode using a terminal emulation program on a PC (set to the appropriate baud rate [usually 9600]), by typing executive and pressing return. Since the printer is currently in batch mode, what you type will not be echoed.

Following this, a prompt should appear, and you can enter Postscript commands and programs, including the examples given in this article. To exit interactive mode, either type quit, or press Ctrl-D, the Postscript (and UNIX) end-of-file character.

Obtaining Graphics Output

PostScript builds up a graphics image using painting operators to place marks on the current page; each mark obliterates whatever it overwrites. The current path is a set of connected and disconnected points, lines and curves set by various path operators, and which may (or may not) be stroked onto the current page.

Here's a simple example:

newpath
216 72 moveto
216 504 lineto
stroke
showpage

The newpath operator clears the current path and initiates a new path. Next, 216 72 moveto locates us at coordinates 216/72 (3 inches) to the right and 72/72 (1 inch) up from the the lower left of the page.

The line 216 504 lineto adds a line segment to the path from the current point to one 7 inches up the page (i.e. a 6 inch long line). This new point now becomes the current point. The stroke command causes the current path to be painted onto the page, making it 'visible'. Finally, the showpage operator prints the page, transferring it to paper.

Filled shapes are possible: Here is a program which will draw a gray box:

newpath
216 360 moveto
0 72 rlineto
72 0 rlineto
0 -72 rlineto
closepath
.30 setgray
fill
showpage

The rlineto operator provides a line relative to the current position, as you might expect, while the closepath operator connects the current point to the last point addressed by a moveto operator, closing the path with a mitred join. Without the closepath operator, the closing corners of boxes will have notches in them.

The setgray operator specifies the shade of gray for subsequent painting, with zero being white and one being black. Finally, the fill operator fills in the current path, providing a solid gray square.

Notice that the coordinates in the graybox program are specified in Postscript's default of 1/72 of an inch, which makes visualizing the output a bit more difficult. We can get around this problem by defining a Postscript procedure which will convert dimensions or coordinates in inches into 1/72 inch units.

A procedure is defined by placing the procedure name, preceded by a slash, onto the stack, followed by the list of operations which make up the procedure. The def operator is then used to store the name and operations into the current dictionary. The program then becomes:

/inch {72 mul} def
newpath
3 inch 5 inch moveto
0 1 inch rlineto
1 inch 0 rlineto
0 -1 inch rlineto
closepath
.30 setgray
fill
showpage

- only now, it is easier to visualize the size and position of the box.

Curves

One of the trickiest problems faced by graphics languages is the definition of curves. Postscript solves the problem with the use of Bezier spline curves.

The curveto operator takes three points (in addition to the current position) as arguments, and these are used as Bezier cubic control points. Its usage therefore is:

x1 y1 x2 y2 x3 y3 curveto

The current position can be taken as x0, y0, and the curve leaves that point in the direction of x1, y1. Along the way, however, it starts to curve towards x2, y2, and then towards x3, y3, which it reaches from the direction of x2, y2. The mathematical formula which controls all of this is actually derived from a pair of parametric cubic equations, and if it all sounds terribly complex, rest assured that it is not.

One can get a good 'feel' for Bezier curves by playing around with an illustration package such as Adobe Illustrator, which displays the cubic control points as 'handles' on the curve - by dragging the handles around one can observe the behaviour of the curve and derive and intuitive 'feel' for it.

Fonts

The ability to define complex curves comes in very handy in the creation of fonts. Most printers define their fonts as dot matrixes, but this leads to problems when one wants to scale the font up or rotate text through some arbitrary angle.

Postscript defines its fonts as geometrical descriptions of the outlines of the characters, and this allows a wide variety of mathematical operations to be performed, including scaling, rotating and various other 'distortions' which can be used to create effects such as perspective or wrapping text around objects.

Take a look at this program:

/inch {72 mul} def
/Helvetica-Bold findfont 48 scalefont setfont
/ShowOutline {true charpath stroke} def
/WordCircle {15 15 345
{gsave rotate 0 0 moveto (Postscript) ShowOutline grestore} for} def
9 inch 2 div 11 inch 2 div translate
newpath
.5 setlinewidth
WordCircle 0 0 moveto (Postscript) true charpath
gsave .5 setgray fill grestore
stroke
showpage

The program defines two procedures, one of which (ShowOutline) prints its text argument as outline text, and the other, more complex, procedure (WordCircle) uses a Postscript for operator to repeatedly rotate the text around the origin. The translate statement, later in the program, translates the origin to be near the center of the page, and the rest of the program is fairly obvious.

Summary

In this article, I have dealt only with some of the basic features of Postscript; it has many other advanced features such as support for colour, half-tones (i.e. photographs), bit-mapped images, and all the facilities of a full programming language. However, the key points which I wanted to emphasise are:


With the advent of Interactive Display Postscript, which is intended for use in screen windowing environments, Postscript is likely to become increasingly significant in the PC and workstation world.
Page last updated: 10/Dec/2004 Back to Home Copyright © 1987-2010 Les Bell and Associates Pty Ltd. All rights reserved. webmaster@lesbell.com.au
...........................