IS 1014 Introduction to Computer Graphics -- Paul Munro A Postscript Tutorial Book available at: 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 1014 Introduction to Computer Graphics -- Paul Munro The Stack A piece of memory set aside for immediate processing Store (push), retrieve (pop) LIFO “Last In -- First Out” Example: Postscript stack
IS 1014 Introduction to Computer Graphics -- Paul Munro Stacks are not just for numbers a line of postscript code -- all objects that are not operators go onto the stack: mark /Font [1 2] (PS) (PS) [1 2] /Font mark
IS 1014 Introduction to Computer Graphics -- Paul Munro Stack Arithmetic add add mul mul div 3 5 add div add
IS 1014 Introduction to Computer Graphics -- Paul Munro 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 =
IS 1014 Introduction to Computer Graphics -- Paul Munro Stack Operators CBACBA clear CCBACCBA CBACBA dup BCABCA CBACBA exch BABA CBACBA pop
IS 1014 Introduction to Computer Graphics -- Paul Munro 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 1014 Introduction to Computer Graphics -- Paul Munro An example (0,0) newpath moveto rlineto moveto rlineto stroke showpage
IS 1014 Introduction to Computer Graphics -- Paul Munro Another example (a box) newpath moveto 0 72 rlineto 72 0 rlineto rlineto rlineto 4 setlinewidth stroke showpage closepath
IS 1014 Introduction to Computer Graphics -- Paul Munro Filled Shapes (a filled box) newpath moveto 0 72 rlineto 72 0 rlineto rlineto closepath fill showpage
IS 1014 Introduction to Computer Graphics -- Paul Munro A shaded box newpath moveto 0 72 rlineto 72 0 rlineto rlineto closepath.5 setgray fill showpage
IS 1014 Introduction to Computer Graphics -- Paul Munro A buncha boxes newpath %Begin black box moveto 0 72 rlineto 72 0 rlineto rlineto closepath fill newpath %Begin gray box moveto 0 72 rlineto 72 0 rlineto rlineto closepath.4 setgray fill newpath %Begin lighter box moveto 0 72 rlineto 72 0 rlineto rlineto closepath.8 setgray fill showpage %Send to printer
IS 1014 Introduction to Computer Graphics -- Paul Munro Path Construction Operators closepath Closes the current path with a straight line to the last moveto point x y lineto Continue the path with line to (x,y) x y moveto Set the current point to (x,y) newpath Clear the current path x y rlineto Relative lineto (currentpoint + (x,y)) x y rmoveto Relative moveto
IS 1014 Introduction to Computer Graphics -- Paul Munro 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
IS 1014 Introduction to Computer Graphics -- Paul Munro Variables /ppi 72 def 5555 ppi 72 5 mul ppi mul example:
IS 1014 Introduction to Computer Graphics -- Paul Munro 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” 6 inch => 6 72 mul => 432 example:
IS 1014 Introduction to Computer Graphics -- Paul Munro Three Boxes Again % Begin Program newpath % First box moveto box 0 setgray fill newpath % Second box moveto box.4 setgray fill newpath % Third box moveto box.8 setgray fill showpage %Black box newpath moveto 0 72 rlineto 72 0 rlineto rlineto closepath fill %Gray box newpath moveto 0 72 rlineto 72 0 rlineto rlineto closepath.4 setgray fill %Begin lighter box newpath moveto 0 72 rlineto 72 0 rlineto rlineto closepath.8 setgray fill % Define box procedure --- /box { 72 0 rlineto 0 72 rlineto rlineto closepath } def
IS 1014 Introduction to Computer Graphics -- Paul Munro 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 inch 4.5 inch box 0 fillbox 3.75 inch 5 inch box.4 fillbox 4 inch 5.5 inch box.8 fillbox showpage
IS 1014 Introduction to Computer Graphics -- Paul Munro Translating Space translate
IS 1014 Introduction to Computer Graphics -- Paul Munro Translation example /square %procedure to draw a { newpath % filled square 0 0 moveto 90 0 lineto %define a square path lineto 0 90 lineto closepath fill %fill it } def square %do a square translate %move coord. sys. square %do another square translate %and move again square %do a third square showpage
IS 1014 Introduction to Computer Graphics -- Paul Munro Rotation 45 rotate this statement will rotate the user coordinate system 45 degrees:
IS 1014 Introduction to Computer Graphics -- Paul Munro Rotation Example /square %procedure from { newpath % previous program 0 0 moveto 90 0 lineto lineto 0 90 lineto closepath fill 6 92 moveto %Label the box } def square %do a square translate %move coord. sys. 60 rotate %and rotate it square %do it again translate 60 rotate square %do a third square showpage
IS 1014 Introduction to Computer Graphics -- Paul Munro Scaling The scale operator rescales the two coordinate axes scale
IS 1014 Introduction to Computer Graphics -- Paul Munro Scaling example /square %procedure to draw a { newpath % filled square 0 0 moveto 90 0 lineto lineto 0 90 lineto closepath fill 6 92 moveto %Label the box } def square %do a square translate scale square translate scale %non-uniform scaling square showpage
IS 1014 Introduction to Computer Graphics -- Paul Munro Conditionals and loops eq ne lt gt le ge if ifelse loop exit for
IS 1014 Introduction to Computer Graphics -- Paul Munro if bool {commands} if /chkforendofline { currentpoint pop %get x-position 612 gt %greater than 612? {0 -12 translate 0 0 moveto} if } def
IS 1014 Introduction to Computer Graphics -- Paul Munro 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 rlineto 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 translate 5 {IncreaseCounter doATrap DecreaseScale 0 20 translate } repeat showpage bool {cmds A} {cmds B} ifelse
IS 1014 Introduction to Computer Graphics -- Paul Munro loop { cmds } loop /pagewidth mul def /doCircle { xpos ypos radius 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 lineofcircles lineofcircles lineofcircles showpage
IS 1014 Introduction to Computer Graphics -- Paul Munro for a inc b {cmds} for % Define box procedure --- /box { 72 0 rlineto 0 72 rlineto rlineto closepath } def moveto {rmoveto box} showpage