TCM
UoC crest

(Encapsulated) PostScript Explained

PostScript

PostScript is a language invented and trademarked by Adobe in 1985. It is primarily designed for graphics, with many commands dedicated to drawing and filling text and geometric shapes. It does also have the standard language features of procedures, loops, conditional statements, maths functions, etc.

The coordinate system used is floating point and based on points (72 points = 1 inch, 2.8 points = 1 millimetric). The output device does its best when converting these to the 100 dpi (typical X11 screen) or 600 dpi (typical printer) device it is using. Thus one can scale PostScript in a fairly arbitrary fashion, as there is never a special correspondance between the size of an object and the (unknown) resolution of the (unknown) output device.

A PostScript file can, and should, consist of lines of 7 bit ASCII characters, with no line exceeding about 80 characters. The first two characters of the file must be `%!'.

DSC and Comments

In Postscript comments run from a `%' character to the end of a line.

Comments starting with `%%' at the beginning of a line include extra information about the structure of a documents: Document Structure Comments. Interesting ones include `%%Pages: n', the total number of pages, `%%Page: n n', the start of each page, and `%%BoundingBox: llx lly urx ury', the lower left and upper right corners of the area used by the graphics.

A DSC conformant file will start `%!PS-Adobe-n.n' and will be structured in such a fashion that another program can remove and reorder pages simply by reading the comments rather than the whole of the PostScript.

Encapsulated PostScript

An EPS file is a PostScript file designed to be included in another PostScript file. It need not itself do anything if sent to a PostScript device such as a printer, and it is written in a subset of PostScript.

An EPS file must not contain multiple pages, and must contain a `%%BoundingBox:' comment. It must start `%!PS-Adobe-n.n EPSF-n.n'. There are also many more subtle restrictions about altering the state of the PostScript interpreter (it must not).

All EPS files can be readily converted to PostScript. The converse is not true.

One should never attempt to include a PostScript file in another document (e.g. LaTeX), but always use EPS for figures and diagrams. It is possible for a file to be both EPS and printable when sent to a PostScript device, and thus one has the best of both worlds.

An EPS file must not draw outside of its bounding box, and therefore cropping an EPS file by changing its bounding box is wrong. Indeed, it does not work with dvips, but does work with eps2gif.

PostScript Levels

The first version of PostScript, level 1, was described in 1985. Level 2 followed in 1990, and level 3 in 1999. Currently (2003) it is very rare to find a device which supports level 1 only, but many devices do not yet support level 3.

Support for colour was greatly improved in level 2, and most colour level 1 devices have some of the level 2 enhancements anyway. Support for included bitmap data has also improved dramatically.

PostScript does support boring old bitmaps, for often (ray tracing, photographs) they are the most reasonable way of describing an image. In level 1 one was reduced to greyscale (or monochrome) bitmaps encoded as hex, that is, two characters per byte of real data. Colour or Run Length Encoded bitmaps needed a decoding procedure to be written in PostScript and supplied with them, so were interpreted rather slowly.

Level 2 supports colour fully, as well as RLE compression (8 bit only), LZW compression (similar to, but not identical to, GIF, and patented), DCT compression (identical to baseline JPEG), and ASCII85 encoding of data, which requires five characters for four bytes of data.

Level 3 expands its LZW filter so that it can support GIFs directly, as well as adding Flate (zlib, or PNG) compression, which is unpatented and gernerally better than LZW.

A 24 bit colour image stored uncompressed and hex encoded will need just over 6 bytes per pixel. With compression and ASCII85 it could be as little as 6 pixels per byte.

EPS Previews

In order to support applications which cannot provide a full PostScript interpreter, EPS files can also include a low-res bitmap representation of their image. In the MS DOS world this is often done using WMF or TIFF inclusions, and the result is a binary file which many UNIX programs will not accept. In the world of portability, EPSI files include a simple bitmap hex encoded in the comment section of an EPS file.

Xdvi and xfig ignore previews and send the PostScript to ghostscript.

Fonts

A standard set of 35 scalable fonts is usually assumed to be known by any PostScript interpreter: Avant Garde Gothic, Bookman, Courier, Helvetica, Helvetica Narrow, New Century Schoolbook, Palatino and Times Roman in each of normal, bold, italic and bold-italic, and Zapf Chancery, Zapf Dingbats and Symbol once each. No standard requires that every interpreter knows these fonts, but, in practice they do. The Level 3 revision of PostScript does require these fonts, and many more besides. For the naming of the free URW versions of the 35 fonts, see a separate page on PostScript35 font names.

Other fonts must be included embedded in the document, preferably as Type1 (scalable) fonts, and not Type3 (bitmap).

Non PostScripts

PostScript is a trademark of Adobe, and to call a product `PostScript' one must pay Adobe a license fee and have Adobe certify the product. Ghostscript is a free program which re-impliments the PostScript language: it is not Adobe certified, it does not call itself PostScript, and it does not pay a license fee to Adobe. Several printer manufacturers, including HP, use PostScript `clones' in their printers to avoid the license fee too. Sometimes the clones have bugs. (On the other hand, sometimes Adobe certified interpreters have bugs.)

Adobe does not require a fee from those who write programs which output PostScript. If the PostScript includes LZW compressed data, Unisys will expect a fee though. (Possibly not after Summer 2003)

Under Windoze, choosing `print to file' with an HP printer selected nearly produces PostScript, but it is wrapped up in a PJL header and trailer which should be removed.

Bad Postscript

Most PostScript generated is, unfortunately, wrong. Acroread is a good example: it always claims '%%DocumentData: Clean7Bit', and then has a pair of high-ASCII quotes in an error message, with character codes 170 and 186. However, almost nothing cares about this.

Acroread with 'fit to page' checked generates PostScript which detects the page size as it prints, and rescales itself accordingly. This makes an utter nonsense of the supplied bounding box.

Many, many other errors are possible, including using non-standard fonts and not including a copy of such fonts, and producing excessively verbose output.

Hand-editing PostScript

A few hints for making minor corrections to PostScript files.

Postscript and Software

The following software in TCM does the following with Postscript.

xv Converts bitmaps to level 1 EPS. Full colour bitmaps are stored hex encoded, reduced colour (8 bit) and greyscale bitmaps offer `compression' as an option, which uses RLE compression, and the decoder is in PostScript. Xv's EPS output is also printable PostScript.

pnmtops Converts PPM and PNM bitmaps to level 1 EPS. Unlike xv, offers RLE for full colour (24 bit) output.

convert Converts most things to EPS level 1 or 2. Its use of level 2 includes LZW, DCT and ASCII85 filters.

bmp2eps Converts PNM/GIF/JPEG files to level 2 EPS. For JPEG the resulting EPS file is only about a third bigger than the original and unlike convert, it does not expand then recompress the JPEG. Can also produce printable Postscript and single-page PDF.

eps2ps Converts EPS to Postscript so that our printers will print them.

eps2gif Converts EPS to GIF, PNG, PPM or JPEG, with antialiasing.

gv View PostScript and EPS files, and extract pages from same.

psselect Extract pages from DSC-conformant PS files.

giftoppm, djpeg Convert GIF and JPEG to PPM (for use with pnmtops).

xfig Drawing package which can import and write EPS.

gnuplot, xmgr, grace Graphing packages which can write EPS. Some versions of grace produce buggy EPS though (wrong BoundingBox).

ps2pdf PostScript to PDF converter.

Three of the above, bmp2eps, eps2gif and eps2ps, were written by the author of this page, so might have bugs and misfeatures fixed rapidly when the same are reported by those in TCM.



Please send comments/corrections to mjr19.