a postscript tutorial book available at:
DESCRIPTION
Postscript as a page description language like HTML text position, orientation font, style figures position, orientation scaling coordinates pen control (move, line). PS as a programming language like C++ stack commands arithmetic operators loops and conditionals lines and shapes. - PowerPoint PPT PresentationTRANSCRIPT
IS 1
014
In
trod
uct
ion
to C
ompu
ter
Gra
phic
s --
Pau
l Mu
nro
A Postscript Tutorial
Book available at:
http://www-cdf.fnal.gov/offline/PostScript/BLUEBOOK.PDF
• Postscript as a page description language– like HTML
– text • position, orientation
• font, style
– figures• position, orientation
• scaling
• coordinates
• pen control (move, line)
• PS as a programming language– like C++
– stack• commands
• arithmetic operators
– loops and conditionals
– lines and shapes
IS 1
014
In
trod
uct
ion
to C
ompu
ter
Gra
phic
s --
Pau
l Mu
nro
The Stack
• A piece of memory set aside for immediate processing
• Store (push), retrieve (pop)
• LIFO “Last In -- First Out”
• Example: 12 6.3 -99 12
126.3
6.3
12
-99
-99
6.3
12Postscript stack
IS 1
014
In
trod
uct
ion
to C
ompu
ter
Gra
phic
s --
Pau
l Mu
nro
Stacks are not just for numbers
a line of postscript code -- all objects that are not operatorsgo onto the stack:
mark
/Font
[1 2]
(PS)
(PS) [1 2] /Font mark
IS 1
014
In
trod
uct
ion
to C
ompu
ter
Gra
phic
s --
Pau
l Mu
nro
Stack Arithmetic
4
45
5
4
add
9
4 5 add3.1
3.12
2
3.1
mul
6.2
3.1 2 mul
2
2
6
5
div
3
5
add
8
5 6 2 div add6
6
5
5
5
IS 1
014
In
trod
uct
ion
to C
ompu
ter
Gra
phic
s --
Pau
l Mu
nro
Arithmetic Operators
• div
• idiv
• mod
• mul
• neg
• add
• sub
5 2 div =
5 2 idiv =
5 2 mod =
5 2 mul =
5 neg =
5 2 add =
5 2 sub =
2.5
2
1
10
-5
7
3
IS 1
014
In
trod
uct
ion
to C
ompu
ter
Gra
phic
s --
Pau
l Mu
nro
Stack Operators
C
B
A
clear
C
C
B
A
C
B
A
dup
B
C
A
C
B
A
exch
B
A
C
B
A
pop
IS 1
014
In
trod
uct
ion
to C
ompu
ter
Gra
phic
s --
Pau
l Mu
nro
Graphics in Postscript
• construct a path on the current page• coordinates are in points (72 pt = 1 inch)• (0,0) is the lower left corner of the page• newpath begins a path description• moveto, rmoveto move the pen (up)• lineto, rlineto draw with the pen (down)• stroke places the marks on the virtual paper• showpage renders the marks
IS 1
014
In
trod
uct
ion
to C
ompu
ter
Gra
phic
s --
Pau
l Mu
nro
An example
(0,0)
newpath72 360 moveto144 72 rlineto144 432 moveto0 -216 rlinetostrokeshowpage
IS 1
014
In
trod
uct
ion
to C
ompu
ter
Gra
phic
s --
Pau
l Mu
nro
Another example (a box)
newpath270 360 moveto0 72 rlineto72 0 rlineto0 -72 rlineto-72 0 rlineto
4 setlinewidthstroke showpage
closepath
IS 1
014
In
trod
uct
ion
to C
ompu
ter
Gra
phic
s --
Pau
l Mu
nro
Filled Shapes (a filled box)
newpath270 360 moveto0 72 rlineto72 0 rlineto0 -72 rlinetoclosepathfillshowpage
IS 1
014
In
trod
uct
ion
to C
ompu
ter
Gra
phic
s --
Pau
l Mu
nro
A shaded box
newpath270 360 moveto0 72 rlineto72 0 rlineto0 -72 rlinetoclosepath.5 setgrayfillshowpage
IS 1
014
In
trod
uct
ion
to C
ompu
ter
Gra
phic
s --
Pau
l Mu
nro
A buncha boxes
newpath %Begin black box252 324 moveto0 72 rlineto72 0 rlineto0 -72 rlinetoclosepathfill
newpath %Begin gray box270 360 moveto0 72 rlineto72 0 rlineto0 -72 rlinetoclosepath.4 setgrayfill
newpath %Begin lighter box288 396 moveto0 72 rlineto72 0 rlineto0 -72 rlinetoclosepath.8 setgrayfillshowpage %Send to printer
IS 1
014
In
trod
uct
ion
to C
ompu
ter
Gra
phic
s --
Pau
l Mu
nro
Path Construction OperatorsclosepathCloses the current path with a straight line to the last moveto point
x y linetoContinue the path with line to (x,y)
x y movetoSet the current point to (x,y)
newpathClear the current path
x y rlinetoRelative lineto (currentpoint + (x,y))
x y rmovetoRelative moveto
IS 1
014
In
trod
uct
ion
to C
ompu
ter
Gra
phic
s --
Pau
l Mu
nro
Painting OperatorsfillFill current path with the current color
n setgraySet the current color
n setlinewidthSet the current line width
strokePaint the current path with the current color and line width
An Output OperatorshowpageTransfer the current page to the output device
IS 1
014
In
trod
uct
ion
to C
ompu
ter
Gra
phic
s --
Pau
l Mu
nro
Variables
/ppi 72 def
5
5ppi
72
5
mul
360
5 ppi mulexample:
IS 1
014
In
trod
uct
ion
to C
ompu
ter
Gra
phic
s --
Pau
l Mu
nro
Procedures
In Postscript, procedure definitions use the same syntax asvariable definitions:
/inch {72 mul} def
whenever “inch” appears, it is replaced with “72 mul”
6 inch => 6 72 mul => 432example:
IS 1
014
In
trod
uct
ion
to C
ompu
ter
Gra
phic
s --
Pau
l Mu
nro
Three Boxes Again
% --------- Begin Program -----------newpath % First box252 324 moveto box0 setgray fillnewpath % Second box270 360 moveto box.4 setgray fillnewpath % Third box288 396 moveto box.8 setgray fillshowpage
%Black box newpath252 324 moveto0 72 rlineto72 0 rlineto0 -72 rlinetoclosepathfill
%Gray box newpath270 360 moveto0 72 rlineto72 0 rlineto0 -72 rlinetoclosepath.4 setgrayfill
%Begin lighter box newpath288 396 moveto0 72 rlineto72 0 rlineto0 -72 rlinetoclosepath.8 setgrayfill
% ----- Define box procedure ---/box{ 72 0 rlineto0 72 rlineto-72 0 rlinetoclosepath } def
IS 1
014
In
trod
uct
ion
to C
ompu
ter
Gra
phic
s --
Pau
l Mu
nro
Three Boxes Yet Again
% ------- Define procedures----/inch {72 mul} def
/box % stack: x y => ---{ newpath moveto1 inch 0 rlineto0 1 inch rlineto-1 inch 0 rlinetoclosepath } def
/fillbox % stack: grayvalue => ---{ setgray fill } def
% ----------- Main Program -----------3.5 inch 4.5 inch box0 fillbox3.75 inch 5 inch box.4 fillbox4 inch 5.5 inch box.8 fillboxshowpage
IS 1
014
In
trod
uct
ion
to C
ompu
ter
Gra
phic
s --
Pau
l Mu
nro
Translating Space100 200 translate
IS 1
014
In
trod
uct
ion
to C
ompu
ter
Gra
phic
s --
Pau
l Mu
nro
Translation example/square %procedure to draw a{ newpath % filled square0 0 moveto90 0 lineto %define a square path90 90 lineto0 90 linetoclosepath fill %fill it} def
square %do a square
200 250 translate %move coord. sys.square %do another square
200 250 translate %and move againsquare %do a third square
showpage
IS 1
014
In
trod
uct
ion
to C
ompu
ter
Gra
phic
s --
Pau
l Mu
nro
Rotation
45 rotate
this statement will rotate the user coordinate system 45 degrees:
IS 1
014
In
trod
uct
ion
to C
ompu
ter
Gra
phic
s --
Pau
l Mu
nro
Rotation Example/square %procedure from{ newpath % previous program0 0 moveto90 0 lineto90 90 lineto0 90 linetoclosepath fill6 92 moveto %Label the box} def
square %do a square300 150 translate %move coord. sys.60 rotate %and rotate itsquare %do it again...300 150 translate60 rotatesquare %do a third squareshowpage
IS 1
014
In
trod
uct
ion
to C
ompu
ter
Gra
phic
s --
Pau
l Mu
nro
Scaling
The scale operator rescales the two coordinate axes
1.5 2 scale
IS 1
014
In
trod
uct
ion
to C
ompu
ter
Gra
phic
s --
Pau
l Mu
nro
Scaling example/square %procedure to draw a{ newpath % filled square0 0 moveto90 0 lineto90 90 lineto0 90 linetoclosepath fill6 92 moveto %Label the box} defsquare %do a square100 100 translate1.5 1.5 scalesquare100 100 translate.75 1.25 scale %non-uniform scalingsquareshowpage
IS 1
014
In
trod
uct
ion
to C
ompu
ter
Gra
phic
s --
Pau
l Mu
nro
Conditionals and loops
• eq ne lt gt le ge
• if
• ifelse
• loop
• exit
• for
IS 1
014
In
trod
uct
ion
to C
ompu
ter
Gra
phic
s --
Pau
l Mu
nro
if
bool {commands} if
/chkforendofline{ currentpoint pop %get x-position612 gt %greater than 612?{0 -12 translate 0 0 moveto} if} def
IS 1
014
In
trod
uct
ion
to C
ompu
ter
Gra
phic
s --
Pau
l Mu
nro
if else
% ------- Variables & Procedures ---------/scalefactor 1 def
/counter 0 def
/DecreaseScale{ scalefactor .2 sub/scalefactor exch def } def
/IncreaseCounter{ /counter counter 1 add def } def
/trappath %construct a trapezoid{ 0 0 moveto 90 0 rlineto-20 45 rlineto -50 0 rlinetoclosepath } def
/doATrap{ gsave1 scalefactor scale %scale vert. axistrappath %construct pathcounter 2 mod %is counter even?0 eq {.5} {0} ifelse %choose grey or blacksetgray fillgrestore } def %restore scale, etc.
% ------------ Begin Program ----------250 350 translate5{IncreaseCounterdoATrapDecreaseScale0 20 translate } repeatshowpage
bool {cmds A} {cmds B} ifelse
IS 1
014
In
trod
uct
ion
to C
ompu
ter
Gra
phic
s --
Pau
l Mu
nro
loop{ cmds } loop
/pagewidth 8.5 72 mul def/doCircle{ xpos ypos radius 0 360 arc stroke} def/increase-x{ xpos radius add/xpos exch def } def7.2 LOOPS 69/lineofcircles %stack: radius y{ /ypos exch def %define ypos/radius exch def % ...& radius/xpos 0 def % ...& xpos{xpos pagewidth le %begin loop{doCircle increase-x}{exit} ifelse}loop %end loop} def %end definition
% --------------- Begin Program -----------10 400 lineofcircles30 400 lineofcircles90 400 lineofcirclesshowpage
IS 1
014
In
trod
uct
ion
to C
ompu
ter
Gra
phic
s --
Pau
l Mu
nro
fora inc b {cmds} for
% ----- Define box procedure ---/box{ 72 0 rlineto0 72 rlineto-72 0 rlinetoclosepath } def
50 50 moveto1 1 10 {rmoveto 30 60 box}showpage