Class: Laser::Cutter::Renderer::SvgRenderer

Inherits:
Base
  • Object
show all
Defined in:
lib/laser/cutter/renderer/svg_renderer.rb

Overview

Writes the box as an SVG sized in the configuration's own units. It fits the page to the box, and carries the metadata as a instead of drawing it.

Constant Summary

Constants inherited from Base

Base::BLACK, Base::BLUE

Instance Attribute Summary

Attributes inherited from Base

#config, #enclosure, #page_manager, #subject

Instance Method Summary collapse

Methods inherited from Base

#render, #units

Constructor Details

#initialize(config) ⇒ SvgRenderer

Returns a new instance of SvgRenderer.

Parameters:



13
14
15
16
# File 'lib/laser/cutter/renderer/svg_renderer.rb', line 13

def initialize(config)
  super
  self.subject = Laser::Cutter::BoxFactory.for(config)
end

Instance Method Details

#document ⇒ String

Returns the SVG, yielding after each line drawn.

Returns:

  • (String) —

    the SVG, yielding after each line drawn



29
30
31
32
33
34
35
36
37
# File 'lib/laser/cutter/renderer/svg_renderer.rb', line 29

def document
  svg = Victor::SVG.new(width: "#{width}#{units}", height: "#{height}#{units}", viewBox: "0 0 #{width} #{height}")
  svg.element(:desc, description) if config.
  lines.each do |line|
    svg.line(**coordinates(line), stroke: "##{config[:color] || BLACK}", stroke_width: config.stroke)
    yield line if block_given?
  end
  svg.render
end

#lines ⇒ Array<Geometry::Line>

Returns every line to cut, computed once.

Returns:



19
20
21
# File 'lib/laser/cutter/renderer/svg_renderer.rb', line 19

def lines
  @lines ||= subject.generate_notches
end

#total ⇒ Integer

Returns how many lines Base#render draws, and yields.

Returns:

  • (Integer) —

    how many lines Base#render draws, and yields



24
25
26
# File 'lib/laser/cutter/renderer/svg_renderer.rb', line 24

def total
  lines.size
end