 XMLSerializer = xmldom.XMLSerializer
 
 SVGNS = 'http://www.w3.org/2000/svg'
+XLINKNS = 'http://www.w3.org/1999/xlink'
 
 splitIntoLines = (data) ->
   data.replace('\r\n', '\n').replace('\r', '\n').split('\n')

class CoffeeMapping extends Mapping

class Drawing extends Input
  constructor: (@data) ->
  writeSVG: (mappings, filename) ->
    ## Default filename is the input filename with extension replaced by .svg

  renderSVG: (mappings) ->
    doc = domImplementation.createDocument SVGNS, 'svg'
    doc.documentElement.appendChild defs = doc.createElementNS SVGNS, 'defs'
    symbols = {}  ## dictionary of used symbols
    for row, y in @data
      for cell, x in row
        symbol = mappings.lookup cell
        continue unless symbol?
        symbols[symbol.key] = symbol
        doc.documentElement.appendChild use = doc.createElementNS SVGNS, 'use'
        use.setAttributeNS XLINKNS, 'href', '#' + symbol.key
        use.setAttributeNS null, 'x', x * @tileWidth
        use.setAttributeNS null, 'y', y * @tileHeight
        use.setAttributeNS null, 'width', @tileWidth
        use.setAttributeNS null, 'height', @tileHeight
        #console.log i, j, cell, symbol
    for key, symbol of symbols
      defs.appendChild node = doc.createElementNS SVGNS, 'symbol'
