<commit_before>lineEndCapShapes = require './lineEndCapShapes.coffee'
renderers = {}


# shapeToSVG(shape) -> string
defineSVGRenderer = (shapeName, shapeToSVGFunc) ->
	renderers[shapeName] = shapeToSVGFunc


renderShapeToSVG = (shape, opts={}) ->
  opts.shouldIgnoreUnsupportedShapes ?= false

  if renderers[shape.className]
    return renderers[shape.className](shape)
  else if opts.shouldIgnoreUnsupportedShapes
    console.warn "Can't render shape of type #{shape.className} to SVG"
    return ""
  else
    throw "Can't render shape of type #{shape.className} to SVG"


defineSVGRenderer 'Rectangle', (shape) ->
  x = shape.x
  y = shape.y
  if shape.strokeWidth % 2 != 0
    x += 0.5
    y += 0.5

  "
    <rect x='#{x}' y='#{y}'
      width='#{shape.width}' height='#{shape.height}'
      stroke='#{shape.strokeColor}' fill='#{shape.fillColor}'
      stroke-width='#{shape.strokeWidth}' />
  "


defineSVGRenderer 'SelectionBox', (shape) -> return ""


defineSVGRenderer 'Ellipse', (shape) ->
  halfWidth = Math.floor(shape.width / 2)
  halfHeight = Math.floor(shape.height / 2)
  centerX = shape.x + halfWidth
  centerY = shape.y + halfHeight
  "
    <ellipse cx='#{centerX}' cy='#{centerY}' rx='#{halfWidth}'
      ry='#{halfHeight}'
      stroke='#{shape.strokeColor}' fill='#{shape.fillColor}'
      stroke-width='#{shape.strokeWidth}' />
  "


defineSVGRenderer 'Image', (shape) ->
  # This will only work when embedded in a web page.
  "
    <image x='#{shape.x}' y='#{shape.y}'
      width='#{shape.image.naturalWidth * shape.scale}'
      height='#{shape.image.naturalHeight * shape.scale}'
      xlink:href='#{shape.image.src}' />
  "


defineSVGRenderer 'Line', (shape) ->
  dashString =
    if shape.dash then "stroke-dasharray='#{shape.dash.join(', ')}'" else ''
  capString = ''
  arrowWidth = Math.max(shape.strokeWidth * 2.2, 5)

  x1 = shape.x1
  x2 = shape.x2
  y1 = shape.y1
  y2 = shape.y2
  if shape.strokeWidth % 2 != 0
    x1 += 0.5
    x2 += 0.5
    y1 += 0.5
    y2 += 0.5

  if shape.endCapShapes[0]
    capString += lineEndCapShapes[shape.endCapShapes[0]].svg(
      x1, y1, Math.atan2(y1 - y2, x1 - x2), arrowWidth, shape.color)
  if shape.endCapShapes[1]
    capString += lineEndCapShapes[shape.endCapShapes[1]].svg(
      x2, y2, Math.atan2(y2 - y1, x2 - x1), arrowWidth, shape.color)
  "
    <g>
      <line x1='#{x1}' y1='#{y1}' x2='#{x2}' y2='#{y2}'
        #{dashString}
        stroke-linecap='#{shape.capStyle}'
        stroke='#{shape.color}'stroke-width='#{shape.strokeWidth}' />
      #{capString}
    <g>
  "


defineSVGRenderer 'LinePath', (shape) ->
  "
    <polyline
      fill='none'<commit_msg>Fix SVG renderer typos<commit_after>lineEndCapShapes = require './lineEndCapShapes.coffee'
renderers = {}


# shapeToSVG(shape) -> string
defineSVGRenderer = (shapeName, shapeToSVGFunc) ->
	renderers[shapeName] = shapeToSVGFunc


renderShapeToSVG = (shape, opts={}) ->
  opts.shouldIgnoreUnsupportedShapes ?= false

  if renderers[shape.className]
    return renderers[shape.className](shape)
  else if opts.shouldIgnoreUnsupportedShapes
    console.warn "Can't render shape of type #{shape.className} to SVG"
    return ""
  else
    throw "Can't render shape of type #{shape.className} to SVG"


defineSVGRenderer 'Rectangle', (shape) ->
  x = shape.x
  y = shape.y
  if shape.strokeWidth % 2 != 0
    x += 0.5
    y += 0.5

  "
    <rect x='#{x}' y='#{y}'
      width='#{shape.width}' height='#{shape.height}'
      stroke='#{shape.strokeColor}' fill='#{shape.fillColor}'
      stroke-width='#{shape.strokeWidth}' />
  "


defineSVGRenderer 'SelectionBox', (shape) -> return ""


defineSVGRenderer 'Ellipse', (shape) ->
  halfWidth = Math.floor(shape.width / 2)
  halfHeight = Math.floor(shape.height / 2)
  centerX = shape.x + halfWidth
  centerY = shape.y + halfHeight
  "
    <ellipse cx='#{centerX}' cy='#{centerY}' rx='#{halfWidth}'
      ry='#{halfHeight}'
      stroke='#{shape.strokeColor}' fill='#{shape.fillColor}'
      stroke-width='#{shape.strokeWidth}' />
  "


defineSVGRenderer 'Image', (shape) ->
  # This will only work when embedded in a web page.
  "
    <image x='#{shape.x}' y='#{shape.y}'
      width='#{shape.image.naturalWidth * shape.scale}'
      height='#{shape.image.naturalHeight * shape.scale}'
      xlink:href='#{shape.image.src}' />
  "


defineSVGRenderer 'Line', (shape) ->
  dashString =
    if shape.dash then "stroke-dasharray='#{shape.dash.join(', ')}'" else ''
  capString = ''
  arrowWidth = Math.max(shape.strokeWidth * 2.2, 5)

  x1 = shape.x1
  x2 = shape.x2
  y1 = shape.y1
  y2 = shape.y2
  if shape.strokeWidth % 2 != 0
    x1 += 0.5
    x2 += 0.5
    y1 += 0.5
    y2 += 0.5

  if shape.endCapShapes[0]
    capString += lineEndCapShapes[shape.endCapShapes[0]].svg(
      x1, y1, Math.atan2(y1 - y2, x1 - x2), arrowWidth, shape.color)
  if shape.endCapShapes[1]
    capString += lineEndCapShapes[shape.endCapShapes[1]].svg(
      x2, y2, Math.atan2(y2 - y1, x2 - x1), arrowWidth, shape.color)
  "
    <g>
      <line x1='#{x1}' y1='#{y1}' x2='#{x2}' y2='#{y2}'
        #{dashString}
        stroke-linecap='#{shape.capStyle}'
        stroke='#{shape.color} 'stroke-width='#{shape.strokeWidth}' />
      #{capString}
    </g>
  "


defineSVGRenderer 'LinePath', (shape) ->
  "
    <polyline
      fill='none'
{'fraction_non_alphanumeric': 0.17511781338360038, 'fraction_numerical': 0.022243166823751177, 'mean_word_length': 3.2721417069243155, 'pattern_counts': {'":': 0, '<': 17, '<?xml version=': 0, '>': 33, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4483513', 'n_tokens_mistral': 1980, 'n_tokens_neox': 1985, 'n_words': 379}