 
 # intervals_from_root_book()
 
+mode = null
+pdf = false
+page = (width, height, draw_page) ->
+  return [width, height] if mode == 'measure'
+  canvas ||= new Canvas(width, height, pdf and 'pdf')
+  ctx = canvas.getContext('2d')
+  ctx.textDrawingMode = 'glyph' if pdf
+  erase_background()
+  draw_page ctx
+  unless pdf
+    filename = "test.pdf"
+    fs.writeFile build_directory + filename, canvas.toBuffer()
+
+grid = (cols, rows, cell_width, cell_height, header_height, draw_page) ->
+  page cols * cell_width, header_height + rows * cell_height, (ctx) ->
+    i = 0
+    draw_page ctx, (draw_cell) ->
+      ctx.save()
+      ctx.translate (i % cols) * cell_width, header_height + Math.floor(i / cols) * cell_height
+      draw_cell()
+      ctx.restore()
+      i += 1
+
 chord_page = (title, intervals, outervals, pdf) ->
   outervals ||= []
 
-  canvas_gutter = 20
+  diagram_gutter = 20
   header_height = 40
   diagram_title_height = 30
   cols = 3
   rows = 4
   if draw_diagrams
-    diagram_title_height = 15
-    canvas_gutter = 2
-  canvas_width = fretboard_width_with_gutter * cols + canvas_gutter * cols
-  canvas_height = header_height + (fretboard_height_with_gutter + diagram_title_height) * rows + canvas_gutter * rows
-
-  roots = []
-  positions_each (string, fret) ->
-    number = note_number_at(string, fret) % 12
-    roots[number] = [string, fret]
-
-  canvas = new Canvas(canvas_width, canvas_height, pdf and 'pdf') unless pdf and canvas
-  ctx = canvas.getContext('2d')
-  ctx.textDrawingMode = 'glyph' if pdf
-  erase_background()
-
-  ctx.font = '20px Impact'
-  ctx.fillStyle = 'rgb(128, 128, 128)'
-  ctx.fillText "#{title} Chords", canvas_gutter / 2, header_height
+    diagram_title_height = 35
+    diagram_gutter = 10
+
+  pitch_fingers = []
+  fingerings_each (string, fret) ->
+    pitch_number = note_number_at(string, fret) % 12
+    pitch_fingers[pitch_number] = [string, fret]
 
   note_names = "E F F# G G# A A# B C C# D D#".split(/\s/)
   colors = ['red', 'blue', 'green', 'orange']
   other_colors = ['rgba(255,0,0 ,.1)', 'rgba(0,0,255, 0.1)', 'rgba(0,255,0, 0.1)', 'rgba(255,0,255, 0.1)']
 
-  roots.map (note, ix) ->
-    note_number = (ix * 5 + 7) % 12
-    note = roots[note_number]
-    string = note[0]
-    fret = note[1]
-    col = ix % cols
-    row = Math.floor(ix / cols)
-    dx = col * (fretboard_width_with_gutter + canvas_gutter) + canvas_gutter / 2
-    dy = header_height + row * (fretboard_height_with_gutter + canvas_gutter + diagram_title_height) + canvas_gutter / 2
-    ctx.save()
-    ctx.translate dx, dy + diagram_title_height
+  grid cols, rows, padded_fretboard_width + diagram_gutter, padded_fretboard_height + diagram_gutter, diagram_title_height, (ctx, draw_cell) ->
     ctx.font = '20px Impact'
-    ctx.font = '5pt Times' if draw_diagrams
-    ctx.fillStyle = 'rgb(10,20,30)'
-    ctx.fillText note_names[note_number] + ' ' + title, h_gutter, -3
-    draw_fingerboard()
-    for semitones, si in outervals
-      continue if semitones in intervals
-      draw_intervals_from semitones, string, fret, other_colors[si]
-    for semitones, si in intervals
-      draw_intervals_from semitones, string, fret, colors[si]
-    ctx.restore()
-
-  unless pdf
-    filename = "test.pdf"
-    fs.writeFile build_directory + filename, canvas.toBuffer()
+    ctx.fillStyle = 'rgb(128, 128, 128)'
+    ctx.fillText "#{title} Chords", diagram_gutter / 2, header_height / 2
+
+    for ix in [0...12]
+      pitch_number = (ix * 5 + 7) % 12
+      fingering = pitch_fingers[pitch_number]
+      string = fingering[0]
+      fret = fingering[1]
+      draw_cell ->
+        ctx.font = '20px Impact'
+        ctx.font = '5pt Times' if draw_diagrams
+        ctx.fillStyle = 'rgb(10,20,30)'
+        ctx.fillText note_names[pitch_number] + ' ' + title, h_gutter, -3
+        draw_fingerboard()
+        for semitones, si in outervals
+          continue if semitones in intervals
+          draw_intervals_from semitones, string, fret, other_colors[si]
+        for semitones, si in intervals
+          draw_intervals_from semitones, string, fret, colors[si]
 
 # chord_page 'Major', [0, 4, 7]
+
+book = (filename, draw_book) ->
+  pdf = true
+  mode = 'draw'
+  draw_book (draw_page) ->
+    draw_page()
+    ctx.addPage()
+  fs.writeFile build_directory + filename, canvas.toBuffer()
 
 chord_book = ->
   chords = [

string_spacing = 20
fret_width = 45
fret_overhang = .3 * fret_width
fretboard_width_with_gutter = 2 * v_gutter + fret_width * fret_count + fret_overhang
fretboard_height_with_gutter = 2 * h_gutter + (string_count - 1) * string_spacing

draw_diagrams = true
if draw_diagrams

    ['Dim7', [0, 3, 6, 9]],
    ['6th', [0, 4, 7, 9]],
  ]
  book "Combined Fretboard Chords.pdf", (pager) ->
    for chord in chords
      pager -> chord_page chord[0], chord[1], chord[2], true

chord_book()
