index.md (5130B)
1 --- 2 title: "PostScript Graph Paper" 3 date: 2023-10-22T05:52:41-07:00 4 lastmod: 2025-04-21T13:42:55-07:00 5 draft: false 6 categories: 7 - Programming 8 --- 9 10 I needed a few pages of graph paper, and it seemed easier and more 11 economical[^scrap] to print some at home than to buy a whole pad of it. 12 There are plenty of websites offering PDF files with grids on them, but I 13 didn't like what I found[^pdfs] and decided to make my own. I thought that 14 the process was fun enough that I have since made a few different kinds: 15 16 * [5 mm × 5 mm grid (pdf)](grid.pdf) ([postscript](grid.ps)) 17 * [5 mm × 5 mm dot grid (pdf)](dotgrid.pdf) ([postscript](dotgrid.ps)) 18 * [1 inch width hex grid (pdf)](hexgrid.pdf) ([postscript](hexgrid.ps)) 19 * [Three hole template with registration marks](threehole.pdf) ([postscript](threehole.ps)) 20 21 These files are meant for US Letter paper but could easily be adapted to 22 other dimensions. They are also "full bleed" because I decided to allow my 23 printer to impose its own margins; just be sure that you turn off any 24 "autoscale" or "fit to page" option before printing these. 25 26 ## Why PostScript? 27 28 Drawing a grid is not difficult to do with a computer. I considered writing 29 code to programmatically generate an SVG file, but then I recalled learning 30 that PostScript, the precursor to the PDF format, is itself a bona fide 31 programming language. It only took an afternoon of familiarizing myself with 32 the language to make the grid, and I kept going from there. 33 34 It turns out that PostScript is a stack-based ("concatenative") language, 35 like Forth or [UXNTAL](https://wiki.xxiivv.com/site/uxntal.html). I was 36 playing with the latter last fall[^uxntal], so I have some familiarity with 37 this type of programming. Stack languages seem strange when you're used to 38 other paradigms, but once you get started you may find it easy to shift into 39 the right mindset for structuring programs this way. 40 41 A complete PostScript program, which produced the 5 mm × 5 mm grid linked 42 above, follows: 43 44 ```PostScript 45 %!PS 46 % Adjust these to suit your needs, units specified in "points" (1/72 inch) 47 /w 612 def /h 792 def % US Letter paper 48 /sp 5 72 mul 25.4 div def % Grid spacing (5 mm here) 49 /st 1 72 mul 300 div def % Stroke width (1 'dots' in a 300 dpi resolution) 50 /co 0.6 def % Gray-level of the line (0 = black, 1 = white) 51 52 % Remainder function (`mod` doesn't take floating point values) 53 /r {dup 3 1 roll div dup floor sub mul} def 54 % Return the starting offset that centers the grid 55 /o {r 2 div} def 56 57 % Page and line setup 58 << /PageSize [w h] >> setpagedevice 59 co setgray 60 st setlinewidth 61 62 % Create vertical lines 63 w sp o 64 sp w 65 { 66 dup 67 0 moveto 68 h lineto 69 } for 70 71 % Create horizontal lines 72 h sp o 73 sp h 74 { 75 dup 76 0 exch moveto 77 w exch lineto 78 } for 79 80 % Draw 81 stroke 82 ``` 83 84 This code is probably not idiomatic or efficient, and I welcome feedback on 85 writing better PostScript. But it worked for me and won't be hard to adapt 86 to your own needs. 87 88 ## Programming with PostScript 89 90 If you're interested in playing with PostScript programming, 91 [Ghostscript](https://www.ghostscript.com/)[^gs] has an interactive 92 interpreter that allows you to manipulate and view the stack (just type 93 `stack`), and draw on a page. I just [came 94 across](https://social.tchncs.de/@daveliepmann/111274696437077402) this 95 relevant [quote from Peter Norvig](http://www.norvig.com/21-days.html): 96 97 > _Play_. Which way would you rather learn to play the piano: the normal, 98 > interactive way, in which you hear each note as soon as you hit a key, or 99 > "batch" mode, in which you only hear the notes after you finish a whole 100 > song? Clearly, interactive mode makes learning easier for the piano, and 101 > also for programming. Insist on a language with an interactive mode and 102 > use it. 103 104 Interest in PostScript peaked during an era when books were easily the best 105 way to learn about a computing technology[^books]; I've seen the following 106 recommended and found them fairly easily online: 107 108 * _Thinking in PostScript_ by Glenn Reid 109 * _PostScript Language Tutorial & Cookbook_ ("the Blue Book") by Adobe 110 Systems Incorporated 111 * _PostScript Language Program Design_ ("the Green Book") by Adobe Systems 112 Incorporated 113 * _PostScript Language Reference_ ("the Red Book") by Adobe Systems 114 Incorporated 115 116 [^scrap]: I actually printed on the back of scrap paper because I'm that 117 ~~cheap~~ eco-conscious. 118 119 [^pdfs]: For starters, these sites all branded their downloads—which is 120 certainly their right, but I didn't want an advertisement on my paper. 121 122 [^uxntal]: I started learning it for a project I ought to pick back up. I 123 followed [compudanzas' _introduction to uxn 124 programming_](https://compudanzas.net/introduction_to_uxn_programming_book.html) 125 and recommend it! 126 127 [^gs]: I used Ghostscript to convert these PS files to PDF, and have been 128 using it to tweak PDFs from the command line for years without realizing 129 it was a whole interactive programming language interpreter. 130 131 [^books]: This may well still be true today, but speaking for myself, I 132 rarely begin learning new tools with a trip to the library, as I once 133 had for C and Perl.