\ example of how to do graphics by outputting Postscript (to Ghostscript) \ This program is in the public domain. No Warranty. \ Author: Anton Ertl \ this example just draws a sine wave. You could also do this \ directly in Postscript. \ The interesting parts here are the uses of the words OPEN-PIPE, \ OUTFILE-EXECUTE and FLUSH-FILE (see the Gforth manual for \ explanations). \ For learning Postscript, look at the appropriate books or websites; \ this program only uses very little and the comments don't explain \ that (except where necessary to explain the placement of \ FLUSH-FILE). : init-ps ." 0.3 setlinewidth 0 setgray " cr ; variable point-set? point-set? off : data2coord { f: rx f: ry -- rx1 ry1 } \ transform from our data to Postscript coordinates rx 20e f* 250e f+ ry 100e f* 150e f+ ; : next-point { f: rx f: ry -- } rx f. ry f. point-set? @ if ." lineto" cr else ." moveto" cr point-set? on then ; : flush-out ( -- ) outfile-id flush-file throw ; : finish-line ( -- ) \ only stroke actually draws the line, so this is a good time to \ flush the output: earlier there is little effect, later delays \ displaying the line ." stroke" cr flush-out point-set? off ; : nextpage ( -- ) \ also clears the canvas ." showpage" cr cr flush-out ; : sin-lines ( -- ) 100 -100 do i s>d d>f 10e f/ fdup fsin data2coord next-point loop ; : sin-graph ( -- ) init-ps sin-lines finish-line ; \ We could now do the following to get postscript output (e.g., for \ redirecting it to a file or a printer): 0 [if] sin-graph nextpage [then] \ Alternatively, we can call gs directly from Forth: outfile-id value ps-fid : start-ps s" gs -q -dNOPROMPT" w/o open-pipe throw to ps-fid ; : >ghostscript ( xt -- ) ps-fid outfile-execute ; start-ps ' sin-graph >ghostscript \ and when you are done looking at the result, exit Gforth or do: \ ps-fid close-file throw