300 likes | 458 Views
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.
E N D
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 A Postscript TutorialBook available at: http://www-cdf.fnal.gov/offline/PostScript/BLUEBOOK.PDF
The Stack 12 12 6.3 6.3 12 -99 -99 6.3 12 Postscript stack • A piece of memory set aside for immediate processing • Store (push), retrieve (pop) • LIFO “Last In -- First Out” • Example: 12 6.3 -99
Stacks are not just for numbers mark /Font [1 2] (PS) a line of postscript code -- all objects that are not operators go onto the stack: (PS) [1 2] /Font mark
Stack Arithmetic 3.1 3.1 4 4 2 2 6 5 6 6 5 5 5 2 2 3.1 div 3 5 5 5 4 mul 6.2 add 9 add 8 4 5 add 3.1 2 mul 5 6 2 div add
Arithmetic Operators • div • idiv • mod • mul • neg • add • sub 5 2 div = 2.5 5 2 idiv = 2 5 2 mod = 1 5 2 mul = 10 5 neg = -5 5 2 add = 7 5 2 sub = 3
Stack Operators clear dup C B A C B A C C B A exch pop C B A B C A C B A B A
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
An example (0,0) newpath 72 360 moveto 144 72 rlineto 144 432 moveto 0 -216 rlineto stroke showpage
Another example (a box) closepath newpath 270 360 moveto 0 72 rlineto 72 0 rlineto 0 -72 rlineto -72 0 rlineto 4 setlinewidth stroke showpage
Filled Shapes (a filled box) newpath 270 360 moveto 0 72 rlineto 72 0 rlineto 0 -72 rlineto closepath fill showpage
A shaded box newpath 270 360 moveto 0 72 rlineto 72 0 rlineto 0 -72 rlineto closepath .5 setgray fill showpage
A buncha boxes showpage %Send to printer newpath %Begin black box 252 324 moveto 0 72 rlineto 72 0 rlineto 0 -72 rlineto closepath fill newpath %Begin lighter box 288 396 moveto 0 72 rlineto 72 0 rlineto 0 -72 rlineto closepath .8 setgray fill newpath %Begin gray box 270 360 moveto 0 72 rlineto 72 0 rlineto 0 -72 rlineto closepath .4 setgray fill
Path Construction Operators closepath Closes the current path with a straight line to the last movetopoint x ylineto Continue the path with line to (x,y) x ymoveto Set the current point to (x,y) newpath Clear the current path x yrlineto Relative lineto(currentpoint + (x,y)) x yrmoveto Relative moveto
Painting Operators fill Fill current path with the current color n setgray Set the current color n setlinewidth Set the current line width stroke Paint the current path with the current color and line width An Output Operator showpage Transfer the current page to the output device
Variables 5 5 ppi 72 5 mul 360 /ppi 72 def example: 5 ppi mul
Procedures In Postscript, procedure definitions use the same syntax as variable definitions: /inch {72 mul} def whenever “inch” appears, it is replaced with “72 mul” example: 6 inch => 6 72 mul => 432
Three Boxes Again %Black box newpath 252 324 moveto 0 72 rlineto 72 0 rlineto 0 -72 rlineto closepath fill %Begin lighter box newpath 288 396 moveto 0 72 rlineto 72 0 rlineto 0 -72 rlineto closepath .8 setgray fill % ----- Define box procedure --- /box { 72 0 rlineto 0 72 rlineto -72 0 rlineto closepath } def % --------- Begin Program ----------- newpath % First box 252 324 moveto box 0 setgray fill newpath % Second box 270 360 moveto box .4 setgray fill newpath % Third box 288 396 moveto box .8 setgray fill showpage %Gray box newpath 270 360 moveto 0 72 rlineto 72 0 rlineto 0 -72 rlineto closepath .4 setgray fill
Three Boxes Yet Again % ------- Define procedures---- /inch {72 mul} def /box % stack: x y => --- { newpath moveto 1 inch 0 rlineto 0 1 inch rlineto -1 inch 0 rlineto closepath } def /fillbox % stack: grayvalue => --- { setgray fill } def % ----------- Main Program ----------- 3.5 inch 4.5 inch box 0 fillbox 3.75 inch 5 inch box .4 fillbox 4 inch 5.5 inch box .8 fillbox showpage
Translating Space 100 200 translate
Translation example /square %procedure to draw a { newpath % filled square 0 0 moveto 90 0 lineto %define a square path 90 90 lineto 0 90 lineto closepath fill %fill it } def square %do a square 200 250 translate %move coord. sys. square %do another square 200 250 translate %and move again square %do a third square showpage
Rotation this statement will rotate the user coordinate system 45 degrees: 45 rotate
Rotation Example /square %procedure from { newpath % previous program 0 0 moveto 90 0 lineto 90 90 lineto 0 90 lineto closepath fill 6 92 moveto %Label the box } def square %do a square 300 150 translate %move coord. sys. 60 rotate %and rotate it square %do it again... 300 150 translate 60 rotate square %do a third square showpage
Scaling 1.5 2 scale The scale operator rescales the two coordinate axes
Scaling example /square %procedure to draw a { newpath % filled square 0 0 moveto 90 0 lineto 90 90 lineto 0 90 lineto closepath fill 6 92 moveto %Label the box } def square %do a square 100 100 translate 1.5 1.5 scale square 100 100 translate .75 1.25 scale %non-uniform scaling square showpage
Conditionals and loops • eq ne lt gt le ge • if • ifelse • loop • exit • for
if bool {commands} if /chkforendofline { currentpoint pop %get x-position 612 gt %greater than 612? {0 -12 translate 0 0 moveto} if } def
if else bool {cmds A} {cmds B} ifelse % ------- 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 rlineto closepath } def /doATrap { gsave 1 scalefactor scale %scale vert. axis trappath %construct path counter 2 mod %is counter even? 0 eq {.5} {0} ifelse %choose grey or black setgray fill grestore } def %restore scale, etc. % ------------ Begin Program ---------- 250 350 translate 5 {IncreaseCounter doATrap DecreaseScale 0 20 translate } repeat showpage
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 } def 7.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 lineofcircles 30 400 lineofcircles 90 400 lineofcircles showpage
for a inc b {cmds} for % ----- Define box procedure --- /box { 72 0 rlineto 0 72 rlineto -72 0 rlineto closepath } def 50 50 moveto 1 1 10 {rmoveto 30 60 box} showpage