Docsity

A Beginner's Guide to PostScript Programming, Study notes of Programming Languages

Partial preview of the text

Download A Beginner's Guide to PostScript Programming and more Study notes Programming Languages in PDF only on Docsity!

A First Guide to PostScript

Peter J Weingartner

[email protected]

April 20, 1997

Contents

  • 1 Introduction
  • 2 What is PostScript?
  • 3 Graphics Concepts
  • 4 Language Concepts
  • 5 Programming in PostScript
  • 6 Drawing and Filling Shapes
  • 7 Putting Text on the Page
  • 8 Transformations
  • 9 Clipping for Effect
  • 10 Encapsulated PostScript
  • 11 Post Processing
  • A Examples
  • B PostScript Operators
  • C Errors You Might Encounter
  • D String Escape Codes
  • E PostScript Books

That noted, permission to copy freely all information within this document is granted free of charge, so long as the original author is acknowledged.

Chapter 2

What is PostScript?

PostScript is a programming language optimized for printing graphics and text (whether on paper, film, or CRT is im- material). In the jargon of the day, it is a page description language. It was introduced by Adobe in 1985 and first (to my knowledge) appeared in the Apple LaserWriter. The main purpose of PostScript was to provide a convenient language in which to describe images in a device independent manner. This device independence means that the image is described without reference to any specific device features ( e.g. printer resolution) so that the same description could be used on any PostScript printer (say, a LaserWriter or a Linotron) without modification. In practice, some PostScript files do make assumptions about the target device (such as its resolution or the number of paper trays it has), but this is bad practice and limits portability. The language itself, which is typically interpreted, is stack-based in the same manner as an RPN calculator. A program pushes arguments to an operator onto a stack and then invokes the operator. Typically, the operator will have some result which is left at the top of the stack. As an example, let us say we want to multiply 12 and 134. We would use the following PostScript code:

12 134 mul

The first two words ‘ 12 ’ and ‘ 134 ’ push the numbers 12 and 134 onto the stack. ‘mul’ invokes the multiply oper- ator which pops two values off the stack, multiplies them, and then pushes the result back onto the stack. The resulting value can be left there to be used by another operator later in the program. To follow the conventions used by Adobe in their manuals, I will synopsize operators using the following scheme:

arg-1 arg-2 ... operator result

This scheme means that, to use operator , you must push arguments arg-1 , arg-2 , and so on before invoking the operator. operator will return the result: result. Many operators return no result (they have some side-effect); these will be shown as returning ‘ - ’.

Chapter 4

Language Concepts

As a programming language, PostScript is particularly simple. There are really only a few concepts that need to be sketched out.

Comment A comment in PostScript is any text preceded by a ‘%’. The special comment ‘%!’ as the first two characters of a PostScript program is seen as a tag marking the file as PostScript code by many systems (including Unix’s lpr command). It is a good idea to start every PostScript document with a ‘%!’... doing so will ensure that every spooler and printer the document may encounter will recognize it as PostScript code.

Stack There are several stacks in a PostScript system, but only two are important for this guide: the operand stack, and the dictionary stack. The operand stack is where arguments to procedures (or operators, in PostScript jargon) are pushed prior to use. The dictionary stack is for dictionaries, and it provides storage for variables.

Dictionary A dictionary is a collection of name-value pairs. All named variables are stored in dictionaries. Also, all available operators are stored in dictionaries along with their code. The dictionary stack is a stack of all currently open dictionaries. When a program refers to some key, the interpreter wanders down the stack looking for the first instance of that key in a dictionary. In this manner, names may be associated with variables and a simple form of scoping is implemented. Conveniently, dictionaries may be given names and be stored in other dictionaries.

Name A name is any sequence of characters that can not be interpreted as a number. With the exception of spaces and certain reserved characters (the characters ‘(’, ‘)’, ‘[’, ‘]’, ‘<’, ‘>’, ‘{’, ‘}’, ‘/’, and ‘%’) any character may be part of a name. The name may even start with digits (1Z is a name, for example), but you can get into problems with them (1E10 is a real number). A name is seen as being a reference to some value in a dictionary on the dictionary stack. It should be noted that there are a couple of names that are legal in PostScript which do not follow the above definition. These are the ’[’ and the ’]’ operators. Yes, they are operators and are stored in the dictionary. Some other names that might surprise you are: ’=’, ’==’, ’<<’, and ’>>’. If a name is preceded by a slash, PostScript will place the name on the stack as an operand. If the name has no slash, the interpreter will look up its value in the dictionary stack. If the value is a procedure object, the procedure will be evaluated. If the value is not a procedure, the value will be pushed onto the operand stack.

Number PostScript supports integers and reals. You can express numbers in two forms: radix form, and scientific notation. Radix form is a number of the form radix # value where radix specifies the base for value. Scientific notation is the standard mantissa E exponent form used in most languages.

String Strings are, of course, just strings of characters. There are two ways of expressing strings in Level 1 PostScript. The most common way is to wrap your text in parentheses. For example the string “This is a string” would be written as (This is a string). You can also express a string as hexidecimal codes in angle brackets. For example, the string “ABC” would be expressed as <414243>. There are several escape codes that may be used in the parenthesis format of strings.

Array Arrays in PostScript are like arrays in any other language. Arrays may contain objects of different type, and they are written as a list of objects surrounded by brackets. For instance, [12 /Foo 5] is a three element array containing the number 12, the name Foo, and the number 5.

Procedure A procedure is your way of defining new operators. A procedure is an array that is executable and is written with braces rather than brackets. For example, a procedure to square the top element on the stack might be written as:

dup mul . We can define this procedure to be the square operator with: /square

dup mul def.

Chapter 6

Drawing and Filling Shapes

Principles

The main purpose of PostScript is to draw graphics on the page. One of the elegant aspects of PostScript is that even text is a kind of graphic. The main task that must be mastered, then, is constructing paths which may be used to create the image. To draw and fill shapes, the basic sequence is: Start the path with the newpath operator. Construct the path out of line segments and curves (the path need not be contiguous). Draw the path with the stroke operator or fill it in with the fill operator.

This basic sequence can be modified to do more complicated things as we will see later.

Drawing a Box

In this first example, we will draw a square inch box toward the lower left corner of the page. We start off by defining a function to convert inches into PostScript’s main unit, the point (a point is defined in PostScript as 1/72th of an inch). The conversion is simple, we just multiply the number of inches by 72. This gives us the function /inch

72 mul

def. To actually draw the square, we start a new path and move the current point to a point an inch in from both margins. This is accomplished with the code:

newpath 1 inch 1 inch moveto At this point, the path contains only the point (72, 72). We add in line segments leading away from this point with the lineto operator. This operator adds a line segment from the current point to the point specified to lineto and makes that point the new current point. We can build three sides of the box as follows:

2 inch 1 inch lineto 2 inch 2 inch lineto 1 inch 2 inch lineto We can add the last line by telling PostScript to close up the path with the smallest possible line segment. The closepath operator does this. This operator is especially useful if you need a closed figure for filling. Once we have closed the path, we can draw it with the stroke operator. We finish off the example by ejecting the page (if you are using a printer). PostScript ejects a page with the showpage operator:

closepath stroke showpage

Refinements

The lineto operator works in absolute coordinates within user space. That is, 72 72 lineto adds a line segment from the current point to the point (72, 72) in user space. In drawing the box, however, it is more convenient to ignore the absolute coordinates of the box’s vertices and think instead of the lengths and directions of its sides. Fortunately, PostScript provides a version of lineto which takes relative coordinates instead. This is the rlineto operator. rlineto adds the coordinates given as operands to the coordinates of the current point in the path to find the desti- nation point. That is, 10 20 rlineto will draw a line from the current point to a point 10 points to the right and 20 points toward the top of the page. This is in contrast to 10 20 lineto which adds a line segment which always ends at (10, 20). To see how we can use rlineto, let’s replace the lineto lines in the last example with the following code:

1 inch 0 inch rlineto 0 inch 1 inch rlineto -1 inch 0 inch rlineto

This new example will draw the same figure, but it draws the lines using relative coordinates instead of absolute. This makes it a little easier to visualize and has the added benefit that the same code can draw the three lines at a different location. Note that a negative relative x coordinate moves the point in the left direction while a negative relative y coordinate moves the point down the page.

Filling Shapes

Filling a shape is just as easy as drawing it. You create the path using the standard path creation operators, but instead of calling stroke at the end, you invoke the fill operator. The fill operator will fill the path with the current ink settings. If you want to fill a shape with a pattern, you will need to do some special tricks which we will cover later. We will use the box from above as an example, but we replace the original invocation of stroke with fill. fill uses a simple winding rule (which is described in the Programming Language Reference Manual ) to deter- mine what parts of the page are inside or outside the path. The regions that are inside are painted. Note that arbitrarily complex shapes can be filled with this operator so long as you have enough memory on your PostScript interpreter. You can easily fill in different shades and even some patterns, but to fill an area with a complex image takes some special effects which we will cover later.

Shading and Width

In PostScript, you can view lines as being drawn by pens that have a given width and ink as having particular shades. You are not restricted to completely black ink and one-point wide lines. PostScript provides two handy operators to change these characteristics. The setgray operator sets the intensity of the ink used in drawing lines and filling shapes (actually, setgray affects all subsequent markings made on the page). setgray takes a single numerical argument between 0 and 1. 0 signifies black, and 1 signifies white. Numbers between these two values signify various shades of gray. The setlinewidth operator does just what its name suggests: it sets the width of lines to be drawn. It takes a single numerical argument which is the width of the line in points. setlinewidth affects all lines stroked after the operator is invoked. Both of these operators affect the markings placed on the page after they are called... they do not effect the path until it is stroked or filled. In particular, you can not set the width or gray level for one part of the path and then change it for another... they are the same for all parts of the path, since it is stroked or filled only once. Also, both of these operators affect part of the graphics state and can be saved with gsave and restored with grestore.

Since the font “Times-Roman” is stored in a dictionary, we search for it using its PostScript name. Your printer will usually come with a set of built in fonts and will almost always allow you to add more. The names of the fonts available will vary from printer to printer, but Times is almost always present. Fonts typically come in families. “Times” is the name of the family we used here, and it has four member fonts: Times-Roman, Times-Italic, Time-Bold, and Times- BoldItalic.

Showing Text

The show operator is used to typeset text on the page. It takes a single argument: a string containing the text to be typeset. Text can be considered to be part of the path, so you must also have set the current point with call to moveto or an equivalent operator. A typical call to show might look like this:

newpath % Start a new path 72 72 moveto % Lower left corner of text at (72, 72) (Hello, world!) show % Typeset ‘‘Hello, world!’’

If we ran this code right after the font selection code above, we would get the string “Hello, world!” printed an inch in from the lower left corner, and it would be printed in 20 point Times-Roman.

Chapter 8

Transformations

The PostScript interpreter keeps track of a matrix called the current transformation matrix. When constructing an image, the interpreter uses this matrix to convert the world coordinates used by the program into device coordinates used by the printer itself. Generally, the actual contents of the matrix are of little interest to a well-written PostScript program; the reason for this is that the specific contents are device-dependent. A program that uses them might not work properly. PostScript does provide a number of operators, however, that transform the matrix in a device-independent way. These operators allow you to transform the way user space maps onto device space, and they modify the current transformation matrix with a simple matrix transformation. The basic transformation operators are:

rotate

translate

scale

It is useful to realize that the current transformation matrix (and, hence the effect of all these operators) is part of the current graphics state and can be saved and restored using the gsave, and grestore operators. In addition, the transformations on the matrix affect path components constructed after the transformation. Even if a path is only partially constructed when a transformation is invoked, the parts of the path that were in place before the transformation will be unaffected.

Rotate

The rotate operator takes a single, numerical operand. This operand specifies how many degrees to rotate the user space around its origin (positive values specify counter clockwise rotations). This transform allows you to do some pretty neat tricks. For example, let’s say you have written a routine to draw some complex shape; and you have found that you need to draw it several times at different angles. In a more primitive graphics system, you might need to re- write to routine to take an angle as an argument, but in PostScript you only need to rotate the coordinates with the rotate operator. As a concrete example, let’s say you want to draw lines in a circular pattern so that each line is ten degrees from its neighbors. Rather than figure out the coordinates for each of the 36 lines, we can just draw a horizontal line and rotate it repeatedly to different angles. To do the repeated looping, we can use the for operator. The for operator takes four arguments: an initial index value, a step size, a final index value, and a procedure. The operator increments an index from the initial value to the final value, incrementing it by the step size. For each index value, for will push the index on the stack and execute the procedure. This gives you a simple means of looping. We start by setting up the for loop. At the beginning of the loop’s procedure, we start a new path and save the graphics state.

0 10 360 { % Go from 0 to 360 degrees in 10 degree steps newpath % Start a new path gsave % Keep rotations temporary

Combining Transformations

Each of these transformations merely modifies the current transformation matrix. This means that these operators can be combined for some interesting effects. For example, you can take a normal document and print two of its pages on a single page (reduced and placed side-by-side) simply by translating the first page to one side, rotating the page by ninety degrees and then reducing the page so that it fits. The second page is handled in the same manner, but is translated to the other side of the page. This can be easily done by PostScript postprocessors so long as they know where one page ends and the next begins (this is often accomplished using special comments). A somewhat simpler example is to draw a simple box and some text translated, rotated, and scaled in various ways. An important thing to remember when viewing this example is that translations are always relative to the current user space. This means that

0.5 0.5 scale 72 72 translate

will have a different effect on the image than does

72 72 translate 0.5 0.5 scale

In the first case, the origin will be half an inch in from the bottom and left margins. In the second case, the origin will be an inch in from the two margins.

Chapter 9

Clipping for Effect

Within the graphics state of a PostScript system is a special path called the clipping path. Every bit of ink to be placed on the page is checked against this path. If PostScript determines that the ink would go outside the current clipping path, that portion of ink is ignored. If the ink would be within the clipping path, it is actually placed on the page. For the mathematically inclined, the clipping process is intersection: the set of pixels to be painted is intersected with the set of pixels within the current clipping path to get the set of pixels to paint. For objects that are partly inside and partly outside the clipping path, the natural implication is that only the part that is within the clipping path is drawn. By default, the clipping path is defined to be a rectangle just within the boundary of the page (usually it is set to about a quarter of an inch). You can set your own clip path by constructing the path with the normal path construction operators and invoking the clip operator. There is only one difficulty: once you reduce the size of the current clipping path, there is no way to expand the size of the clipping path with clip. The only way to go back to a larger clipping path is to save the one you would like to restore with gsave and restore it later with grestore. In fact, it is always good policy to only set a clipping path withing a bracketing gsave/grestore pair. You will always be safe if you do this.

Clipping a Simple Path

As a simple example of clipping, let us say that we want to draw a box and fill it with text in such a way that some text is cut off. The effect we wanting is that of a hole in a piece of paper over some newsprint. This can be done quite simply. First, we set up the box to act as our window. We can set up the path, stroke it if we want to see it, and then clip to it:

gsave % Save the old clip path 72 72 box % Set up our box gsave % Don’t allow box to be lost after stroke stroke grestore % Restore the box path clip % Clip to the box

The clip path is now established, and we can now go on to draw the text that should be clipped (note that there is a leading gsave...this is to keep us from loosing our old clip path which covered the whole page).

60 60 moveto (This is Times-Roman clipped to a box) show 70 90 moveto (This is Times-Roman clipped to a box) show 50 120 moveto (This is Times-Roman clipped to a box) show

Chapter 10

Encapsulated PostScript

What is Encapsulated PostScript?

At some point, you may want to include some nice PostScript image into a document. There are a number of problems associated with this, but the main one is that your page layout program needs to know how big the image is, and how to move it to the correct place on the page. Encapsulated PostScript (EPS) is that part of Adobe’s Document Structuring Convention that provides this information.

What Is the Document Structuring Convention?

The DSC is a special file format for PostScript documents. The full details for the DSC can (and should) be gotten from Adobe. If you are writing a PostScript printer driver or other utility which will be used by a large number of people to create or manipulate PostScript documents, do not even think about writing it without making it DSC-compliant. You will save yourself and your users a lot of headaches. Although the full DSC is beyond the scope of this guide, the most basic rules can be explained. A DSC-compliant document is an ordinary PostScript document with a number of comments added. These comments provide informa- tion to any postprocessors which work with the files. Some comments strictly provide information, others are used to structure the document into sections, which may be shuffled or processed in other ways by the postprocessor. Every DSC-compliant document is indicated by having the comment %!PS-Adobe 3.0 as the first line. This comment is a flag to indicate that the document is compliant. You should never use this comment unless your docu- ment really is DSC compliant. There are many other parts to proper DSC. A document which follows the DSC can be manipulated in many ways. In particular, postprocessors can shuffle the pages, print two or more pages on a side, and so on. The printer drivers from some notable companies do not follow the DSC, and their PostScript documents are, therefore, impossible to work with once they’ve been generated.

Now, What About EPS?

An EPS file is a PostScript file which follows the DSC and which follows a couple of other rules. These rules can be summarized as follows:

The first line must be %!PS-Adobe EPSF-3.

The file must make use of the BoundingBox comment

The file should be a single page image (in DSC terms, the %%Pages comment must have a value of 0 or 1).

The file should not use any operators which affect the global state.  Forbidden operators include: banddevice, clear, cleardictstack, copypage, erasepage, exitserver, framedevice, grestoreall, initclip, initgraph- ics, initmatrix, quit, renderbands, setglobal, setpagedevice, setshared, and startjob. Operators to avoid include: nulldevice, setgstate, sethalftone, setmatrix, setscreen, settransfer, ande undefinedfont.

Finally, the EPS file should not use showpage. Actually, Adobe says that it is fine to use showpage in your EPS files. Officially, it is the responsibility of the importing application to redefine showpage so that the EPS file does not actually eject the page. Still, in creating EPS files, it would be wise not to use this operator.

In general, if you are creating a PostScript image by hand, all you need to do is calculate the bounding box, add the first comment and the %%BoundingBox comment, and you have an EPS file. Once you have the file in this form, page layout programs can incorporate the image and will allow you to move and resize the image at will.

BoundingBox

The BoundingBox comment is used in DSC to indicate where the actual image will be on a page. The comment de- scribes a rectangle which completely encloses the image. The form of the comment is: %%BoundingBox: llx lly urx ury. For instance, suppose I have an image which extends from x=72 to x=144 and from y=150 to y=170. The BoundingBox comment in the document should then be: %%BoundingBox: 72 150 144 170.