   ctx.fillStyle = 'white'
   ctx.fillRect 0, 0, canvas.width, canvas.height
 
+draw_title = (text, {font, x, y, gravity}) ->
+  gravity ||= ''
+  ctx.font = font if font
+  m = ctx.measureText(text)
+  # x -= m.width / 2
+  y -= m.emHeightDescent if gravity.match(/^bottom$/)
+  y += m.emHeightAscent if gravity.match(/^top|topLeft|topRight$/)
+  ctx.fillText text, x, y
+
+with_context = (ctx, fn) ->
+  ctx.save()
+  fn()
+  ctx.restore()
+
 filename = (name) -> DefaultFilename = name
 
 save_canvas_to_png = (canvas, fname) ->

chord_page = (chord, options) ->
  {best_fingering} = options || {}

  header_height = 50
  diagram_title_height = 30
  if draw_diagrams
    diagram_title_height = 35
    diagram_gutter = 10

  pitch_fingers = []
  finger_positions_each (finger_position) ->
    pitch = pitch_number_for_position(finger_position) % 12

  ctx = canvas.getContext('2d')
  ctx.textDrawingMode = 'glyph' if pdf
  erase_background()

  ctx.save()
  ctx.translate page_margin, page_margin
  draw_page ctx
  ctx.restore()

  ctx.fillStyle = 'black'
  license = "©2013 by Oliver Steele. "
  license += "This work is licensed under a Creative Commons Attribution 3.0 United States License."
  draw_title license
  , font: "4pt Times", x: page_margin, y: canvas.height
  , gravity: 'bottom'

  unless pdf
    filename = "#{DefaultFilename or 'test'}.png"
    fs.writeFile BuildDirectory + filename, canvas.toBuffer()
