A PostScript file is a program in Adobe’s PostScript language, defined in the PostScript Language Reference (third edition, 1999, for LanguageLevel 3). The interpreter reads it as a stream of tokens, each of which becomes an object; most are pushed onto the operand stack, and executable names run the operators they stand for. This page covers the text form; the binary encoding is on its own page.
How a file starts
| First bytes | Meaning |
|---|---|
%!PS-Adobe-3.0 | A file that follows the Document Structuring Conventions, version 3.0 (2.0 and 2.1 also seen). Words may follow, such as EPSF-3.0. |
%! | Any PostScript program; the minimum that printers and spoolers look for. |
04 (Ctrl-D) | Control-D, which the serial communication protocol of many printers uses as an end-of-file indication. It is part of the channel, not of the language (reference §3.2), but drivers often leave it at the start or end of saved jobs. |
1B 25 2D 31 32 33 34 35 58 | A PJL job header (Esc%-12345X) around the PostScript, from print-to-file: see PCL. |
C5 D0 D3 C6 | A DOS EPS binary header with a preview: see EPS. |
Characters
Programs are written in ASCII; string contents may hold any bytes.
- White space: NUL (0x00), tab (0x09), line feed (0x0A), form feed (0x0C), carriage return (0x0D) and space (0x20). Any run counts as one separator. CR, LF and CR LF all end a line.
- Delimiters:
( ) < > [ ] { } / %. They end the token before them, so/a/bis two names. - Regular characters: everything else. A token made of regular characters is a number if it can be read as one, otherwise a name.
Tokens
| Token | Examples | Rules |
|---|---|---|
| Comment | % to end of line | Runs to the next newline or form feed and counts as one white-space character. Ignored, except that comments starting %! or %% carry the DSC. |
| Integer | 123, -98, +17 | Optional sign and digits. A value too large for an integer becomes a real. |
| Real | -.002, 34.5, 1.0E-5, 1e10 | A decimal point, an exponent, or both. |
| Radix number | 8#1777, 16#FFFE, 2#1000 | base#digits, base 2 to 36, digits 0–9 then letters. Read as an unsigned number and stored with the same two’s-complement bits, so 16#FFFFFFFF is −1; a larger value is a limitcheck error. |
| Literal string | (Hello (world)) | Balanced parentheses need no escape. Escapes: \n LF, \r CR, \t tab, \b backspace, \f form feed, \\, \(, \), \ddd one to three octal digits; a backslash before a line end joins the lines; a backslash before any other character is ignored. A bare line end inside the string becomes LF. |
| Hexadecimal string | <48 65 6C6C6F> | Pairs of hex digits, either case; white space ignored; an odd final digit is followed by 0. |
| ASCII85 string | <~ … ~> | LanguageLevel 2: base-85 encoding, 5 characters !–u per 4 bytes, z for four zero bytes, ended by ~>. |
| Literal name | /moveto | Pushed as a name, not executed. / alone is the empty name. |
| Executable name | moveto | Looked up in the dictionary stack and executed. |
| Immediately evaluated name | //moveto | LanguageLevel 2: replaced by its value when the token is read, even inside a procedure. |
| Array | [1 (two) /three] | [ pushes a mark; ] collects everything down to the mark into an array. Contents are executed as they are read. |
| Procedure | {1 add} | An executable array, built without executing its contents. |
| Dictionary | << /Key 1 /Other (x) >> | LanguageLevel 2: << pushes a mark; >> pairs up keys and values into a dictionary. |
Object types
Every object has a type, an attribute (literal or executable) and an access
(unlimited, read-only, execute-only or none). type returns the type’s name.
| Type name | Kind | Meaning |
|---|---|---|
booleantype | simple | true or false. |
integertype | simple | A 32-bit signed integer. |
realtype | simple | A real number, typically IEEE single precision. |
nametype | simple | An atom, compared by identity. |
nulltype | simple | The null object. |
marktype | simple | A marker on the stack (mark, [, <<). |
operatortype | simple | A built-in operator. |
savetype | simple | A snapshot of virtual memory from save. |
fonttype | simple | A font identifier (FID). |
stringtype | composite | An array of bytes, up to 65,535 long. |
arraytype | composite | An array of any objects, up to 65,535 long. |
packedarraytype | composite | A read-only, compact array (LanguageLevel 2). |
dicttype | composite | A dictionary of key and value pairs. |
filetype | composite | A file or filter. |
gstatetype | composite | A saved graphics state (LanguageLevel 2). |
conditiontype, locktype | composite | Synchronisation objects of Display PostScript. |
conditiontype and locktype belong to Display PostScript,
which the third edition of the reference no longer documents. Architectural limits
(appendix B, table B.1): integers −2,147,483,648 to 2,147,483,647; reals about
±1038 with about 8 significant digits; arrays, strings and dictionaries
up to 65,535 elements; names up to 127 characters; file names up to 100;
save nesting 15; gsave and clipsave nesting
31; DeviceN up to 250 components. Typical LanguageLevel 1 memory limits (table B.2)
include an operand stack of 500, a dictionary stack of 20 and an execution stack of
250; later levels allocate memory more flexibly.
LanguageLevels
| Level | Added (appendix A) |
|---|---|
| 1 | The original language, introduced in 1985. |
| 2 | << >> dictionaries, <~ ~> ASCII base-85 strings, // immediately evaluated names, binary encodings, packed arrays, and many operators: filters, resources, colour spaces and patterns, forms, user paths, setpagedevice, global VM. Several began as LanguageLevel 1 extensions (CMYK colour, composite fonts, file system). |
| 3 | Functions, smooth shading (shfill), the FlateDecode, FlateEncode and ReusableStreamDecode filters, idiom recognition, CIEBasedDEF, CIEBasedDEFG and DeviceN colour spaces, CIDFonts and CMaps, and the resource categories listed on the operators page. |
A DSC file states its level with %%LanguageLevel:; a program can test
languagelevel when it exists.
Sources
- Adobe, PostScript Language Reference, third edition (1999) – §3.2 syntax and table 3.1, table 3.2 object types, appendix A LanguageLevels, appendix B limits
- Adobe Technical Note #5001, Document Structuring Conventions 3.0 – the
%!PS-Adobe-3.0first line