www.eamoncaddigan.net

Content and configuration for https://www.eamoncaddigan.net
git clone https://git.eamoncaddigan.net/www.eamoncaddigan.net.git
Log | Files | Refs | Submodules | README

index.md (5038B)


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