Class: Dieses::Application::Canvas

Inherits:
Geometry::Rect show all
Defined in:
lib/dieses/application/canvas.rb

Constant Summary collapse

TEMPLATE =
<<~XML
  <?xml version="1.0" standalone="no"?>
  <!DOCTYPE svg PUBLIC "-//W3C//DTD SVG 1.1//EN" "http://www.w3.org/Graphics/SVG/1.1/DTD/svg11.dtd">
  <svg xmlns="http://www.w3.org/2000/svg" width="%{width}mm" height="%{height}mm" viewBox="0 0 %{width} %{height}" shape-rendering="geometricPrecision" %{header}>
    <style>
      svg         { stroke:           %8{color}; }
      svg         { stroke-width:     %8{medium}; }
      .altcolor   { stroke:           %8{altcolor}; }
      .extrafine  { stroke-width:     %8{extrafine}; }
      .fine       { stroke-width:     %8{fine}; }
      .medium     { stroke-width:     %8{medium}; }
      .broad      { stroke-width:     %8{broad}; }
      .extrabroad { stroke-width:     %8{extrabroad}; }
      .dashed     { stroke-dasharray: %8{dashed}; }
    </style>
    <g id="sheet">
  %{content}
    </g>
  </svg>
XML

Constants inherited from Geometry::Rect

Geometry::Rect::Orientation

Instance Attribute Summary collapse

Attributes inherited from Geometry::Rect

#height, #position, #width

Attributes inherited from Geometry::Element

#attributes, #hash

Instance Method Summary collapse

Methods inherited from Geometry::Rect

#align, #angle, #bbox, #intersect, #orient, #orientation, #shrink, #to_h, #to_s, #to_svgf, #translate

Methods included from Geometry::Rect::Predicate

#cover?, #inside?, #onto?, #outside?

Methods inherited from Geometry::Element

#attr, #classify, #eql?, #to_svg

Constructor Details

#initialize(paper = Paper.a4) ⇒ Canvas

Returns a new instance of Canvas.



31
32
33
34
35
36
# File 'lib/dieses/application/canvas.rb', line 31

def initialize(paper = Paper.a4)
  super(paper.inner)

  @paper    = paper
  @elements = Set.new
end

Instance Attribute Details

#elementsObject (readonly)

Returns the value of attribute elements.



29
30
31
# File 'lib/dieses/application/canvas.rb', line 29

def elements
  @elements
end

#paperObject (readonly)

Returns the value of attribute paper.



29
30
31
# File 'lib/dieses/application/canvas.rb', line 29

def paper
  @paper
end

Instance Method Details

#<<(items) ⇒ Object



38
39
40
41
42
43
44
45
46
# 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) ⇒ Object



48
49
50
51
52
53
# 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