class Dieses::Application::Canvas

Constants

DEFAULT_ALTCOLOR
DEFAULT_COLOR
DEFAULT_DASHES
DEFAULT_LINEWIDTH
TEMPLATE

Attributes

elements[R]
paper[R]

Public Class Methods

new(paper = Paper.a4) click to toggle source
Calls superclass method
# File lib/dieses/application/canvas.rb, line 31
def initialize(paper = Paper.a4)
  super(paper.inner)

  @paper    = paper
  @elements = Set.new
end

Public Instance Methods

<<(items) click to toggle source
# File lib/dieses/application/canvas.rb, line 38
def <<(items)
  [*items].each do |item|
    case item
    when Array             then item.each { |element| elements << element }
    when Geometry::Element then elements << item
    else                        raise Error, 'Item must be an Array or Element'
    end
  end
end
render(header: EMPTY_STRING, variables: EMPTY_HASH) click to toggle source
# File lib/dieses/application/canvas.rb, line 48
def render(header: EMPTY_STRING, variables: EMPTY_HASH)
  # We avoid prettifying XML through REXML which is pretty slow, at the cost of a somewhat hacky code.
  format(TEMPLATE, **variables(**variables),
         content: Geometry.to_svg(elements, paper, prefix: ' ' * 4),
         header:  header)
end

Private Instance Methods

variables(**kwargs) click to toggle source
# File lib/dieses/application/canvas.rb, line 62
def variables(**kwargs) # rubocop:disable Metrics/MethodLength, Metrics/AbcSize, Metrics/PerceivedComplexity
  paper.to_h.merge(kwargs).tap do |variables|
    linewidth = (variables[:linewidth] || variables[:medium] || DEFAULT_LINEWIDTH).to_f

    variables[:color]      ||= DEFAULT_COLOR
    variables[:altcolor]   ||= DEFAULT_ALTCOLOR
    variables[:medium]     ||= linewidth.to_s
    variables[:extrafine]  ||= (linewidth / 4.0).to_s
    variables[:fine]       ||= (linewidth / 2.0).to_s
    variables[:broad]      ||= (linewidth * 2.0).to_s
    variables[:extrabroad] ||= (linewidth * 4.0).to_s
    variables[:dashed]     ||= DEFAULT_DASHES.map(&:to_s).join(' ')
  end
end