MSRI Graphics School - Lecture 2 on PostScript
More basic commands
Colours
This shows you colors whose components are
indexed by integers 0, 1, ... , 5. In Java or PostScript
these values correspond to floating point numbers 0.0, 0.2, ..., 1.0.
Documents with several pages
The command showpage has an uncertain effect
on the graphics state, and consequently it is best to start
off in the default state on each page.
This can be done by defining procedures something like this:
/page-begin {
gsave
72 dup scale
1 72 div setlinewidth
} def
/page-end {
grestore
showpage
} def
and then making each page page-begin ... page-end.
Manipulating the stack
-
pop Removes the top item from the stack
-
exch Exchanges the two top items on the stack
-
4 1 roll Rolls up (and around) the top 4 items on the stack by one:
a b c d gets changed to d a b c
-
2 index Puts a copy of item 2 down in the stack
on top (counting from 0):
a b c d becomes a b c d b
Text
There are many different ways to
put text in figures, some quite sophisticated.
Here I'll explain the simplest way, and come back later to others.
-
/Helvetica-Bold findfont
10 scalefont
setfont
sets the font in your current graphics state
to be Helvetica-Bold. You have a limited choice in
general. The next line scales its height to be 10 of the current
units. Then finally the font at that scale is chosen to be the current font.
You must choose some font in order to get text displayed.
-
0 0 moveto
(This is a string) show
displays the string This is a string at the origin.
Strings in PostScript are enclosed between
parentheses. To get a parenthesis in the string,
use \( or .
-
0 0 moveto
x ( ) cvs show
places the current value of x into a string
(of at most 6 characters long) and then displays it
at the origin. The command cvs
is convert to string.
Displaying a string moves the current point to
the end of the string. So (A ) show (string) show
displays A string.
Loops
-
10 { ... } repeat
-
0 1 10 { /i exch def ... } for
-
{ ... n 10 ge { exit } if ... } loop
Arrays
-
/A [ 0 1 ] def Defines A to be the array [ 0 1 ] of two numbers.
-
A 0 get Puts A[0] on the stack
-
A 0 3 put Puts 3 in the 0th spot of A
-
A aload pop Places all the contents of A on the stack
Arrays are not just arrays of numbers, but arrays of
anything, maybe all different.
They are the only way to create complicated data
structures in PostScript. Arrays can also be used
as points in 2D or 3D, and for example can be used
as packaged arguments or return values for procedures.
A procedure that leaves something on the stack
is effectively a function.
Conditionals
There are tests ge, le, lt, gt, eq, ne for testing inequalities.
They return boolean values true/false.
-
n 10 ge { ... } if
-
n 10 ge { ... }{ ... } ifelse
Sample code
|