Class: Laser::Cutter::Renderer::LayoutRenderer

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

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

#units

Constructor Details

#initialize(config) ⇒ LayoutRenderer

Returns a new instance of LayoutRenderer.



7
8
9
10
# File 'lib/laser-cutter/renderer/layout_renderer.rb', line 7

def initialize(config)
  self.config = config
  super(config)
end

Instance Method Details

#calculate_image_boundary(box_renderer, margin) ⇒ Object



50
51
52
53
54
# File 'lib/laser-cutter/renderer/layout_renderer.rb', line 50

def calculate_image_boundary(box_renderer, margin)
  box_renderer.enclosure.to_a[1].map do |c|
    c.send(config.units.to_sym) + 2 * margin
  end
end

#renderObject



12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
# File 'lib/laser-cutter/renderer/layout_renderer.rb', line 12

def render
  renderer = self
  renderers = []

  box_renderer = BoxRenderer.new(config)
  renderers << box_renderer

  if config.
    meta_renderer = MetaRenderer.new(config)
    renderers << meta_renderer
    box_renderer.ensure_space_for(meta_renderer.enclosure)
  end

  if config.debug
    unkerfed_config = Laser::Cutter::Configuration.new(config.to_hash)
    unkerfed_config.merge!(kerf: 0.0, color: 'DD2211')
    unkerfed_box_renderer = BoxRenderer.new(unkerfed_config)
    unkerfed_box_renderer.ensure_space_for(meta_renderer.enclosure) if meta_renderer
    renderers << unkerfed_box_renderer
  end

  margin = config.margin.to_f.send(config.units.to_sym)
  page_size = config.page_size || calculate_image_boundary(box_renderer, margin)

  pdf = Prawn::Document.new(:margin => margin,
                            :page_size => page_size,
                            :page_layout => self.config.page_layout.to_sym)

  pdf.instance_eval do
    renderers.each {|r|  r.render(self) }
    render_file(renderer.config.file)
  end

  if config.verbose
    puts "PDF saved to #{config.file}."
  end
end