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

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

Overview

Draws the block of settings at the bottom left of a PDF, under a banner that names the gem.

Constant Summary collapse

META_KEYS =

The settings printed, in order, when they are set.

%i(units width height depth front_height slope thickness notch lid kerf stroke padding margin page_size page_layout).freeze
WIDTH =

The block, in points: its width, the height of the banner above the settings, the height of one line of them, and the space under the last.

140
55
LINE_HEIGHT =

See Also:

8.2
4

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

#document, #units

Constructor Details

#initialize(config = {}) ⇒ MetaRenderer

Returns a new instance of MetaRenderer.

Parameters:



23
24
25
26
27
# File 'lib/laser/cutter/renderer/meta_renderer.rb', line 23

def initialize(config = {})
  self.config = config
  height = BANNER_HEIGHT + (fields.size * LINE_HEIGHT) + FOOTER_HEIGHT
  self.enclosure = Laser::Cutter::Geometry::Rect.create(Laser::Cutter::Geometry::Point[1, 1], WIDTH, height)
end

Instance Method Details

#fields ⇒ Hash{Symbol => Object}

The settings the block prints, in order. The block is as tall as they need: one line too many would start a second page.

Returns:

  • (Hash{Symbol => Object})


33
34
35
36
37
38
# File 'lib/laser/cutter/renderer/meta_renderer.rb', line 33

def fields
   = config.to_hash
  [:page_size] ||= 'custom'
  .delete(:page_layout) if [:page_size].eql?('custom')
  META_KEYS.select { |key| [key] }.to_h { |key| [key, [key]] }
end

#render(pdf = nil) ⇒ void

This method returns an undefined value.

Parameters:

  • pdf (Prawn::Document) (defaults to: nil)


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
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
# File 'lib/laser/cutter/renderer/meta_renderer.rb', line 42

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 = BANNER_HEIGHT

  meta_fields = fields.keys.join(": \n") + ": \n"
  meta_values = fields.values.join("\n")

  rect = 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