Class: Laser::Cutter::Renderer::MetaRenderer

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

Constant Summary collapse

META_KEYS =
%w(units width height depth thickness notch kerf stroke padding margin page_size page_layout)

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 = {}) ⇒ MetaRenderer

Returns a new instance of MetaRenderer.



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

def initialize(config = {})
  self.config = config
  self.enclosure = Laser::Cutter::Geometry::Rect.create(Laser::Cutter::Geometry::Point[1, 1], 140, 150)
end

Instance Method Details

#render(pdf = nil) ⇒ Object



11
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
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
# File 'lib/laser-cutter/renderer/meta_renderer.rb', line 11

def render pdf = nil
  banner = <<-EOF

  Made with Laser Cutter Ruby Gem (v#{Laser::Cutter::VERSION})
  Credits to Prawn for ruby PDF generation,
  and BoxMaker for inspiration.

  Online: http://makeabox.io/
  Source: https://github.com/kigster/laser-cutter
  EOF

  meta_color = BLUE
  meta_top_height = 55

   = config.to_hash
  ['page_size'] ||= 'custom'
  .delete('page_layout') if ['page_size'].eql?('custom')

  meta_fields = META_KEYS.find_all{|k| [k]}.join(": \n") + ": \n"
  meta_values = META_KEYS.find_all{|k| [k]}.map{|k| [k] }.join("\n")

  rect = self.enclosure

  pdf.instance_eval do
    self.line_width = 0.2.mm
    float do
      bounding_box([rect.p1.x, rect.h + rect.p1.y], :width => rect.w, :height => rect.h) do
        stroke_color meta_color
        stroke_bounds

        # Print banner
        indent 10 do
          font('Helvetica', :size => 6) do
            text banner, :color => meta_color
          end
        end

        # print values of the config, in two parts – keys right aligned first, values left aligned second.
        float do
          bounding_box([0, rect.h - meta_top_height],
                       :width => rect.w,
                       :height => rect.h - meta_top_height) do
            float do
              bounding_box([0, rect.h - meta_top_height], :width => 70, :height => rect.h - meta_top_height) do
                indent 10 do
                  font('Helvetica', :size => 7) do
                    text meta_fields, :color => meta_color, align: :right
                  end
                end
              end
            end
            float do
              bounding_box([60, rect.h - meta_top_height], :width => 70, :height => rect.h - meta_top_height) do
                indent 10 do
                  font('Helvetica', :size => 7) do
                    text meta_values, :color => meta_color
                  end
                end
              end
            end
          end
        end
      end
    end
  end
end