 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) ->
+    draw_page (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 = (chord_name, abbr, intervals, outervals, pdf) ->
-  outervals ||= []
-
+NoteNames = "E F F# G G# A A# B C C# D D#".split(/\s/)
+
+chord_page = (chord) ->
   diagram_gutter = 20
   header_height = 40
   diagram_title_height = 30

    ctx.addPage()
  fs.writeFile build_directory + filename, canvas.toBuffer()

chord_book = ->
  chords = [
    {name: 'Major', abbr: '', offsets: [0, 4, 7], relative: [0, 3, 7]},
    {name: 'Minor', abbr: 'm', offsets: [0, 3, 7], relative: [0, 4, 7]},
    {name: 'Aug', abbr: '+', offsets: [0, 4, 8], relative: [0, 4, 7]},
    {name: 'Dim', abbr: '°', offsets: [0, 3, 6], relative: [0, 3, 7]},
    {name: 'Sus2', abbr: 'sus2', offsets: [0, 2, 7], relative: [0, 4, 7]},
    {name: 'Sus4', abbr: 'sus4', offsets: [0, 5, 7], relative: [0, 4, 7]},
    {name: 'Dom 7th', abbr: '7', offsets: [0, 4, 7, 10]},
    {name: 'Major 7th', abbr: 'maj7', offsets: [0, 4, 7, 11]},
    {name: 'Minor 7th', abbr: 'min7', offsets: [0, 3, 7, 10], relative: [0, 4, 7, 10]},
    {name: 'Dim 7th', abbr: '°7', offsets: [0, 3, 6, 9]},
    {name: 'Dom 7 b5', abbr: '7b5', offsets: [0, 4, 6, 10]},
    {name: 'Min 7th b5', abbr: 'm7b5', offsets: [0, 3, 6, 10]},
    # {name: 'Aug 7th', abbr: '7aug', offsets: [0, 4, 8, 10]},
    {name: 'Dim Maj 7th', abbr: '°maj7', offsets: [0, 3, 6, 11]},
    {name: 'Min Maj 7th', abbr: 'min-maj7', offsets: [0, 3, 7, 11]},
    # {name: '6th', abbr: '6', offsets: [0, 4, 7, 9]},
  ]
  book "Combined Fretboard Chords.pdf", (page) ->
    for {name, abbr, offsets, relative} in chords
      page -> chord_page name, abbr, offsets, relative

chord_book()

      filename = "#{string}-#{fret}-#{interval_long_name}.png"
      save_canvas_to_png canvas, filename

intervals_from_position_page = (fingering) ->
  canvas_gutter = 20
  header_height = 20
  grid 3, 4, padded_fretboard_width + canvas_gutter, padded_fretboard_height + header_height, 0, (cell) ->
    for interval_name, semitones in intervals
      cell ->
        ctx.translate 0, header_height
        draw_fingerboard()
        draw_intervals_from fingering, semitones
        canvas_label = interval_name
        ctx.font = '10px Times'
        ctx.fillStyle = 'rgb(10,20,30)'
        ctx.fillText canvas_label, h_gutter, -3

  # filename = "#{string}-#{fret}-study-sheet.png"
  # save_canvas_to_png canvas, filename unless pdf

intervals_from_note_sheets = ->
  finger_positions_each (string, fret) ->
    intervals_from_note_sheet string, fret

intervals_page = (semitones) ->
  canvas_gutter = 5
  header_height = 40
  cols = fret_count + 1
  rows = string_count

  grid cols, rows, padded_fretboard_width + canvas_gutter, padded_fretboard_height + canvas_gutter, header_height, (cell) ->
    title = long_intervals_names[semitones] + " Intervals"
    ctx.font = '25px Impact'
    ctx.fillStyle = 'rgb(128, 128, 128)'
    ctx.fillText title, canvas_gutter / 2, 30

    finger_positions_each (fingering) ->
      cell ->
        draw_fingerboard()
        draw_intervals_from fingering, semitones

  # unless pdf
  #   long_interval_name = intervals[semitones].replace(/^m/, 'min').replace(/^M/, 'Maj')
  #   filename = "interval-#{long_interval_name}-study-sheet.pdf"
  #   fs.writeFile build_directory + filename, canvas.toBuffer()

intervals_book = ({by_root}) ->
  if by_root
    book "Fretboard Intervals by Root.pdf", (page) ->
      finger_positions_each (fingering) ->
        page -> intervals_from_position_page fingering
  else
    book "Fretboard Intervals.pdf", (page) ->
      for _, semitones in intervals
        page -> intervals_page semitones

mode = null
pdf = false
