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

commit 1385628548c5c69d9a5ac36595779783b54b4b18
parent 7ef82dd2a5346b228aa04a98344109cc68882c73
Author: Eamon Caddigan <eamon.caddigan@gmail.com>
Date:   Sun, 22 Oct 2023 05:53:18 -0700

Post about graph paper (and example PDF)

Acontent/posts/postscript-graph-paper/grid.pdf | 0
Acontent/posts/postscript-graph-paper/index.md | 107+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
2 files changed, 107 insertions(+), 0 deletions(-)

diff --git a/content/posts/postscript-graph-paper/grid.pdf b/content/posts/postscript-graph-paper/grid.pdf Binary files differ. diff --git a/content/posts/postscript-graph-paper/index.md b/content/posts/postscript-graph-paper/index.md @@ -0,0 +1,107 @@ +--- +title: "PostScript Graph Paper" +date: 2023-10-22T05:52:41-07:00 +draft: true +categories: +- Programming +--- + +I needed some graph paper, and for my needs it seemed easier and more +economical to print some at home than buy a whole pad of it. I turned to the +web and found plenty of sites offering PDF files with grids on them, but I +didn't like what I found[^pdfs] and decided to make my own. + +[Here's a PDF for a US Letter page with a 5 mm × 5 mm grid](grid.pdf). + +Drawing a grid is not difficult to do with a computer. The SVG format is +pretty straightforward, so I considered writing a simple program to create a +grid in SVG, and then using a second program to print it. But then I +remembered: PostScript is a bona fide programming language! I could +conceivably write the code to generate a graph paper grid in a file which +could be printed directly. So I spent an afternoon familiarizing myself with +PostScript and did that. + +I learned that PostScript is a stack based ("concatenative") language, like +Forth or [UXNTAL](https://wiki.xxiivv.com/site/uxntal.html). I was playing +with the latter last fall[^uxntal], so I have some familiarity with the +paradigm. Stack programming seems strange when you're used to other +paradigms, but I find it's not too hard to shift into the right mindset to +write code this way once I get started. + +The complete PostScript program, which produced the PDF linked above, +follows: + +```PostScript +%!PS +% Adjust these to suit your needs, units specified in "points" (1/72 inch) +/w 612 def /h 792 def % US Letter paper +/sp 5 72 mul 25.4 div def % Grid spacing (5 mm here) +/st 1 72 mul 300 div def % Stroke width (1 'dots' in a 300 dpi resolution) +/co 0.6 def % Gray-level of the line (0 = black, 1 = white) + +% Remainder function (`mod` doesn't take floating point values) +/r {dup 3 1 roll div dup floor sub mul} def +% Return the starting offset that centers the grid +/o {r 2 div} def + +% Page and line setup +<< /PageSize [w h] >> setpagedevice +co setgray +st setlinewidth + +% Create vertical lines +w sp o +sp w +{ + dup + 0 moveto + h lineto +} for + +% Create horizontal lines +h sp o +sp h +{ + dup + 0 exch moveto + w exch lineto +} for + +% Draw +stroke +``` + +This code is probably not idiomatic or efficient, and I welcome feedback on +writing better PostScript. But it worked for me and shouldn't be too hard to +adapt to other page or grid sizes. Some might want to add margins to the +page, but I decided that I would rather let my printer print as much grid as +it was able; I just made sure to disable "print scaling". + +## Programming with PostScript + +If you're interested in playing with PostScript programming, +[Ghostscript](https://www.ghostscript.com/) (which I used to convert the +PostScript file to PDF) has an interactive interpreter that allows you to +manipulate and view the stack (just type `stack`), and draw on a page. I +just [came +across](https://social.tchncs.de/@daveliepmann/111274696437077402) this +relevant [quote from Peter Norvig](http://www.norvig.com/21-days.html): + +> _Play_. Which way would you rather learn to play the piano: the normal, +> interactive way, in which you hear each note as soon as you hit a key, or +> "batch" mode, in which you only hear the notes after you finish a whole +> song? Clearly, interactive mode makes learning easier for the piano, and +> also for programming. Insist on a language with an interactive mode and +> use it. + +If you're like me and tend to open a calculator whenever you're faced with +trivial arithmetic, I invite you to launch `ghostscript` and perform your +calculations on the stack. + +[^pdfs]: For starters, these sites all branded their downloads—which may be + their right, but I didn't want an advertisement on my paper. + +[^uxntal]: I started learning it for a project I ought to pick back up. I + followed [compudanzas' _introduction to uxn + programming_](https://compudanzas.net/introduction_to_uxn_programming_book.html) + and recommend it!