laser-cutter 2.0.1 → 3.0.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (104) hide show
  1. checksums.yaml +4 -4
  2. data/.github/workflows/pages.yml +44 -0
  3. data/.gitignore +2 -0
  4. data/.openlanes/config.yml +106 -0
  5. data/.plans/004.00/342/224/201/342/224/201/342/232/252/342/224/201/342/224/201/342/235/257can-we-create-an-angled-box-where-angle-is-configurable-like-in-spec-wishes-image-shows/spec.md +40 -0
  6. data/.plans/004.00/342/224/201/342/224/201/342/232/252/342/224/201/342/224/201/342/235/257can-we-create-an-angled-box-where-angle-is-configurable-like-in-spec-wishes-image-shows/state.json +337 -0
  7. data/.plans/004.01/342/224/201/342/224/201/342/232/252/342/224/201/342/224/201/342/235/257sloped-box-panels/plan.md +91 -0
  8. data/.plans/decisions.md +11 -0
  9. data/CHANGELOG.md +134 -0
  10. data/CLAUDE.md +18 -6
  11. data/Gemfile +2 -1
  12. data/README.md +223 -50
  13. data/Rakefile +11 -7
  14. data/docs/badges/coverage_badge.svg +2 -2
  15. data/docs/decisions/decisions-on-angled-box.md +93 -0
  16. data/docs/decisions/stack.md +6 -0
  17. data/docs/images/box-3d.avif +0 -0
  18. data/docs/images/box-sloped-3d.avif +0 -0
  19. data/docs/images/box-sloped.avif +0 -0
  20. data/justfile +24 -8
  21. data/laser-cutter.gemspec +1 -1
  22. data/lib/laser/cutter/aggregator.rb +28 -0
  23. data/lib/laser/cutter/box.rb +95 -5
  24. data/lib/laser/cutter/box_factory.rb +15 -0
  25. data/lib/laser/cutter/cli/command.rb +27 -1
  26. data/lib/laser/cutter/cli/config_file.rb +2 -0
  27. data/lib/laser/cutter/cli/examples.rb +3 -0
  28. data/lib/laser/cutter/cli/generate.rb +37 -6
  29. data/lib/laser/cutter/cli/help.rb +4 -0
  30. data/lib/laser/cutter/cli/page_sizes.rb +3 -0
  31. data/lib/laser/cutter/cli/version.rb +2 -0
  32. data/lib/laser/cutter/cli.rb +23 -4
  33. data/lib/laser/cutter/configuration.rb +38 -4
  34. data/lib/laser/cutter/geometry/dimensions.rb +7 -0
  35. data/lib/laser/cutter/geometry/line.rb +42 -0
  36. data/lib/laser/cutter/geometry/loop.rb +138 -0
  37. data/lib/laser/cutter/geometry/point.rb +3 -0
  38. data/lib/laser/cutter/geometry/rect.rb +25 -0
  39. data/lib/laser/cutter/geometry/shape.rb +19 -1
  40. data/lib/laser/cutter/geometry/transform.rb +50 -0
  41. data/lib/laser/cutter/geometry/tuple.rb +47 -11
  42. data/lib/laser/cutter/launcher.rb +14 -1
  43. data/lib/laser/cutter/missing_option.rb +2 -0
  44. data/lib/laser/cutter/notching/angled_edge.rb +67 -0
  45. data/lib/laser/cutter/notching/base.rb +8 -0
  46. data/lib/laser/cutter/notching/edge.rb +44 -4
  47. data/lib/laser/cutter/notching/infinite_iterator.rb +11 -1
  48. data/lib/laser/cutter/notching/path_generator.rb +40 -6
  49. data/lib/laser/cutter/notching/shift.rb +4 -0
  50. data/lib/laser/cutter/options.rb +25 -4
  51. data/lib/laser/cutter/page_manager.rb +12 -2
  52. data/lib/laser/cutter/renderer/base.rb +19 -3
  53. data/lib/laser/cutter/renderer/box_renderer.rb +14 -1
  54. data/lib/laser/cutter/renderer/layout_renderer.rb +10 -0
  55. data/lib/laser/cutter/renderer/line_renderer.rb +6 -0
  56. data/lib/laser/cutter/renderer/meta_renderer.rb +33 -9
  57. data/lib/laser/cutter/renderer/rect_renderer.rb +5 -0
  58. data/lib/laser/cutter/renderer/svg_renderer.rb +9 -1
  59. data/lib/laser/cutter/renderer.rb +2 -0
  60. data/lib/laser/cutter/sloped/box.rb +90 -0
  61. data/lib/laser/cutter/sloped/corners.rb +58 -0
  62. data/lib/laser/cutter/sloped/layout.rb +97 -0
  63. data/lib/laser/cutter/sloped/notcher.rb +53 -0
  64. data/lib/laser/cutter/sloped/panels.rb +121 -0
  65. data/lib/laser/cutter/sloped/placement.rb +36 -0
  66. data/lib/laser/cutter/sloped/profile.rb +176 -0
  67. data/lib/laser/cutter/sloped/request.rb +31 -0
  68. data/lib/laser/cutter/sloped/ring_panel.rb +120 -0
  69. data/lib/laser/cutter/sloped/side_panel.rb +89 -0
  70. data/lib/laser/cutter/types.rb +3 -0
  71. data/lib/laser/cutter/units_converter.rb +5 -0
  72. data/lib/laser/cutter/version.rb +2 -1
  73. data/lib/laser/cutter/zero_value_not_allowed.rb +2 -0
  74. data/lib/laser/cutter.rb +2 -0
  75. data/spec/laser/cutter/angled_edge_spec.rb +103 -0
  76. data/spec/laser/cutter/box_factory_spec.rb +19 -0
  77. data/spec/laser/cutter/cli_spec.rb +87 -7
  78. data/spec/laser/cutter/configuration_spec.rb +37 -0
  79. data/spec/laser/cutter/loop_spec.rb +107 -0
  80. data/spec/laser/cutter/options_spec.rb +51 -9
  81. data/spec/laser/cutter/renderer/layout_renderer_spec.rb +18 -0
  82. data/spec/laser/cutter/renderer/meta_renderer_spec.rb +82 -0
  83. data/spec/laser/cutter/renderer/svg_renderer_spec.rb +34 -0
  84. data/spec/laser/cutter/sloped/assembly_spec.rb +78 -0
  85. data/spec/laser/cutter/sloped/box_spec.rb +151 -0
  86. data/spec/laser/cutter/sloped/corners_spec.rb +45 -0
  87. data/spec/laser/cutter/sloped/layout_spec.rb +77 -0
  88. data/spec/laser/cutter/sloped/notcher_spec.rb +57 -0
  89. data/spec/laser/cutter/sloped/panels_spec.rb +175 -0
  90. data/spec/laser/cutter/sloped/placement_spec.rb +50 -0
  91. data/spec/laser/cutter/sloped/request_spec.rb +48 -0
  92. data/spec/laser/cutter/sloped/ring_panel_spec.rb +102 -0
  93. data/spec/laser/cutter/sloped/side_panel_spec.rb +98 -0
  94. data/spec/laser/cutter/sloped_profile_spec.rb +156 -0
  95. data/spec/laser/cutter/transform_spec.rb +53 -0
  96. data/spec/spec_helper.rb +4 -0
  97. data/spec/support/loops.rb +106 -0
  98. data/spec/support/sloped.rb +26 -0
  99. data/spec/support/solid.rb +95 -0
  100. data/spec/wishes/wish-tilted-box.png +0 -0
  101. metadata +55 -9
  102. /data/.plans/{001.00-dry-cli-migration/plan.md → 001.00/342/224/201/342/224/201/342/232/252/342/224/201/342/224/201/342/235/257dry-cli-migration/plan.md"} +0 -0
  103. /data/.plans/{002.00-lid-options/plan.md → 002.00/342/224/201/342/224/201/342/232/252/342/224/201/342/224/201/342/235/257lid-options/plan.md"} +0 -0
  104. /data/.plans/{003.00-ruby-api/plan.md → 003.00/342/224/201/342/224/201/342/232/252/342/224/201/342/224/201/342/235/257ruby-api/plan.md"} +0 -0
@@ -3,6 +3,8 @@
3
3
  module Laser
4
4
  module Cutter
5
5
  module Notching
6
+ # Draws the notched path of one {Edge}: a zigzag between its inside and
7
+ # outside lines, plus the corner boxes at the ends the edge names.
6
8
  class PathGenerator
7
9
  extend ::Forwardable
8
10
 
@@ -10,11 +12,11 @@ module Laser
10
12
  def_delegator :@edge, method_name, method_name
11
13
  end
12
14
 
15
+ # @!attribute [rw] edge
16
+ # @return [Edge] the side to draw
13
17
  attr_accessor :edge
14
18
 
15
- # This class generates lines that zigzag between two lines: the outside line, and the
16
- # inside line of a single edge. Edge class encapsulates both of them with additional
17
- # properties.
19
+ # @param edge [Edge] the side to draw, with both its lines
18
20
  def initialize(edge)
19
21
  @edge = edge
20
22
  end
@@ -25,6 +27,8 @@ module Laser
25
27
  #
26
28
  # We always want to create a symmetric path that has a notch in the middle
27
29
  # (for center_out = true) or dip in the middle (center_out = false)
30
+ #
31
+ # @return [Array<Geometry::Line>] the corner boxes, then the zigzag
28
32
  def generate
29
33
  lines = []
30
34
  lines << corner_box_sides if corners
@@ -47,6 +51,11 @@ module Laser
47
51
  create_lines(vertices)
48
52
  end
49
53
 
54
+ # Moves the last vertex along the edge by the kerf.
55
+ #
56
+ # @param vertices [Array<Geometry::Point>] the path so far, changed in place
57
+ # @param direction [Integer] -1 backwards, 1 forwards
58
+ # @return [void]
50
59
  def adjust_for_kerf(vertices, direction)
51
60
  return unless kerf?
52
61
 
@@ -55,6 +64,8 @@ module Laser
55
64
  vertices << point
56
65
  end
57
66
 
67
+ # @return [Array<Geometry::Line>] the sides of the corner boxes, with
68
+ # their kerf strips when the corners are adjusted
58
69
  def corner_box_sides
59
70
  ends = Edge::ENDS.select { |end_index| edge.corner_at?(end_index) }
60
71
 
@@ -85,6 +96,11 @@ module Laser
85
96
  Geometry::Rect[moved, Geometry::Point[*coords]].sides << Geometry::Line[moved, inside.clone]
86
97
  end
87
98
 
99
+ # Half the kerf, along one axis, outwards at one end of the edge.
100
+ #
101
+ # @param index [Integer] 1 for p1, 2 for p2
102
+ # @param dim_shift [Integer] 0 for along the edge, 1 for across it
103
+ # @return [Vector]
88
104
  def shift_vector(index, dim_shift = 0)
89
105
  shift = []
90
106
  shift[(d_index_across + dim_shift) % 2] = 0
@@ -92,45 +108,58 @@ module Laser
92
108
  Vector.[](*shift)
93
109
  end
94
110
 
111
+ # @return [Geometry::Point] where the zigzag starts: p1 of the inside line
95
112
  def starting_point
96
113
  edge.inside.p1.clone # start
97
114
  end
98
115
 
99
- # 0 = X, 1 = Y
116
+ # @return [Integer] the axis the edge runs along: 0 for x, 1 for y
100
117
  def d_index_along
101
118
  edge.inside.p1.x == edge.inside.p2.x ? 1 : 0
102
119
  end
103
120
 
121
+ # @return [Integer] the axis across the edge: 0 for x, 1 for y
104
122
  def d_index_across
105
123
  (d_index_along + 1) % 2
106
124
  end
107
125
 
126
+ # @return [Integer] 1 when the edge runs towards greater coordinates, -1 otherwise
108
127
  def direction_along
109
128
  edge.inside.p1.coords.[](d_index_along) < edge.inside.p2.coords.[](d_index_along) ? 1 : -1
110
129
  end
111
130
 
131
+ # @return [Integer] 1 when the outside line lies at greater coordinates, -1 otherwise
112
132
  def direction_across
113
133
  edge.inside.p1.coords.[](d_index_across) < edge.outside.p1.coords.[](d_index_across) ? 1 : -1
114
134
  end
115
135
 
116
136
  private
117
137
 
138
+ # @param end_index [Integer] 1 for p1, 2 for p2
139
+ # @return [Geometry::Point] that end of the inside line
118
140
  def inside_point(end_index)
119
141
  edge.inside.public_send(:"p#{end_index}")
120
142
  end
121
143
 
144
+ # @param end_index [Integer] 1 for p1, 2 for p2
145
+ # @return [Geometry::Point] that end of the outside line
122
146
  def outside_point(end_index)
123
147
  edge.outside.public_send(:"p#{end_index}")
124
148
  end
125
149
 
126
150
  # A corner box next to a hole is widened by the kerf, so the hole starts that much later.
151
+ #
152
+ # @param end_index [Integer] 1 for p1, 2 for p2
153
+ # @return [Boolean] whether to move that end of the zigzag
127
154
  def adjust_end?(end_index)
128
155
  adjust_corners && !first_notch_out? && edge.corner_at?(end_index)
129
156
  end
130
157
 
131
158
  # This method has the bulk of the logic: we create the list of path deltas
132
- # to be applied when we walk the edge next.
133
- # @param [Object] shift
159
+ # to be applied when we walk the edge next. Tabs grow by the kerf and
160
+ # holes shrink by it, except the first and last.
161
+ #
162
+ # @return [Array<Shift>]
134
163
  def define_shifts
135
164
  along_iter = create_iterator_along
136
165
  across_iter = create_iterator_across
@@ -163,15 +192,20 @@ module Laser
163
192
  # As we draw notches, shifts define the 'delta' – movement from one point
164
193
  # to the next. This method defines three types of movements we'll be doing:
165
194
  # one alongside the edge, and two across (towards the box and outward from the box)
195
+ #
196
+ # @return [InfiniteIterator] of the one step along the edge
166
197
  def create_iterator_along
167
198
  InfiniteIterator.new([Shift.new(notch_width, direction_along, d_index_along)])
168
199
  end
169
200
 
201
+ # @return [InfiniteIterator] of the steps out and back across the edge
170
202
  def create_iterator_across
171
203
  InfiniteIterator.new([Shift.new(thickness, direction_across, d_index_across),
172
204
  Shift.new(thickness, -direction_across, d_index_across)])
173
205
  end
174
206
 
207
+ # @param vertices [Array<Geometry::Point>]
208
+ # @return [Array<Geometry::Line>] a line between each vertex and the next
175
209
  def create_lines(vertices)
176
210
  lines = []
177
211
  vertices.each_with_index do |v, i|
@@ -3,7 +3,11 @@
3
3
  module Laser
4
4
  module Cutter
5
5
  module Notching
6
+ # One step of a notched path: delta along one axis, x (0) or y (1),
7
+ # forwards (1) or backwards (-1).
6
8
  Shift = Struct.new(:delta, :direction, :dim_index) do
9
+ # @param point [Geometry::Point]
10
+ # @return [Geometry::Point] the point one step on
7
11
  def next_point_after(point)
8
12
  p = point.clone
9
13
  shift = []
@@ -21,8 +21,11 @@ module Laser
21
21
  # options.lid # => "plain"
22
22
  # Laser::Cutter.render(options) # => "<svg ..."
23
23
  #
24
- # @raise [MissingOption] when width, height, depth or thickness is left out
25
- # @raise [InvalidOption] when a value cannot be used, or a key is not an option
24
+ # @example A box whose front slopes back at 45 degrees, from 1 inch up
25
+ # Laser::Cutter::Options.new(width: 4, height: 3, depth: 5, thickness: 0.125, slope: 45, front_height: 1)
26
+ #
27
+ # @raise [MissingOption] when width, height, depth or thickness is left out, or a slope has no front height
28
+ # @raise [InvalidOption] when a value cannot be used, a key is not an option, or the numbers do not make a sloped box
26
29
  class Options < Dry::Struct
27
30
  # The dimensions a box cannot be drawn without.
28
31
  REQUIRED = %i[width height depth thickness].freeze
@@ -49,6 +52,16 @@ module Laser
49
52
  # @return [Float] thickness of the material
50
53
  attribute :thickness, Types::Length
51
54
 
55
+ # @!attribute [r] slope
56
+ # @return [Float, nil] angle of a sloped front above horizontal, in degrees, above 0 and below 90;
57
+ # nil for the box with six panels
58
+ attribute? :slope, Types.optional(Types::Slope)
59
+
60
+ # @!attribute [r] front_height
61
+ # @return [Float, nil] internal height at the front of a sloped box, where the slope begins;
62
+ # required with a slope, and 0 starts the slope at the bottom
63
+ attribute? :front_height, Types.optional(Types::Allowance)
64
+
52
65
  # @!attribute [r] notch
53
66
  # @return [Float, nil] notch length, a guide only; nil for three times the thickness
54
67
  attribute? :notch, Types.optional(Types::Length)
@@ -62,7 +75,8 @@ module Laser
62
75
  attribute :units, Types::Units
63
76
 
64
77
  # @!attribute [r] lid
65
- # @return [String] "full", "back" or "plain": notched on every side, into the back wall only, or on no side
78
+ # @return [String] "full", "back" or "plain": notched on every side, into the back wall only, or on no side.
79
+ # On a sloped box it is the tilted panel: "full" notches it into the side walls, "plain" lays it on them.
66
80
  attribute :lid, Types::Lid
67
81
 
68
82
  # @!attribute [r] format
@@ -109,13 +123,16 @@ module Laser
109
123
  return attributes if attributes.is_a?(self)
110
124
 
111
125
  require_dimensions!(attributes) if attributes.respond_to?(:to_h)
112
- super
126
+ super.tap { |options| Sloped::Request.validate!(options.to_h) }
113
127
  rescue Dry::Struct::Error => e
114
128
  raise InvalidOption, explain(e)
115
129
  end
116
130
 
117
131
  private
118
132
 
133
+ # @param attributes [#to_h] the options, by name
134
+ # @return [void]
135
+ # @raise [MissingOption] when a dimension in {REQUIRED} is blank
119
136
  def require_dimensions!(attributes)
120
137
  given = attributes.to_h.transform_keys(&:to_sym)
121
138
  missing = REQUIRED.select { |name| Types.blank?(given[name]) }
@@ -125,6 +142,9 @@ module Laser
125
142
  end
126
143
 
127
144
  # Says which option was refused, and what it may be when that is a short list.
145
+ #
146
+ # @param error [Dry::Struct::Error]
147
+ # @return [String] the message for an {InvalidOption}
128
148
  def explain(error)
129
149
  cause = error.cause
130
150
  return error.message.sub(/\A\[.*?\] /, '') unless cause.respond_to?(:key) && cause.respond_to?(:value)
@@ -136,6 +156,7 @@ module Laser
136
156
 
137
157
  # The words an attribute may be, found under whatever wraps its type.
138
158
  #
159
+ # @param type [Dry::Types::Type]
139
160
  # @return [Array<String>, nil] nil when the attribute is not one of a list
140
161
  def choices_of(type)
141
162
  return type.values if type.respond_to?(:values)
@@ -2,9 +2,12 @@
2
2
 
3
3
  module Laser
4
4
  module Cutter
5
+ # Lists the page sizes a PDF can take, in inches or millimetres.
5
6
  class PageManager
7
+ # The page sizes Prawn knows, in PDF points, by name.
6
8
  SIZES = PDF::Core::PageGeometry::SIZES.clone.freeze
7
9
 
10
+ # @return [String] "in" or "mm"
8
11
  attr_reader :units
9
12
 
10
13
  # @param units [String, Symbol] 'in' or 'mm'; the configuration's default is a Symbol
@@ -12,6 +15,7 @@ module Laser
12
15
  @units = units.to_s
13
16
  end
14
17
 
18
+ # @return [String] one line for each page size, with its width and height
15
19
  def all_page_sizes
16
20
  output = +""
17
21
  page_size_values.each do |k|
@@ -20,8 +24,12 @@ module Laser
20
24
  output
21
25
  end
22
26
 
23
- # if from_units is nil, we expect it to be in dots per inch (default
24
- # measurements for Prawn
27
+ # Converts a length into the units of this page manager.
28
+ #
29
+ # @param value [Numeric] the length
30
+ # @param from_units [String, nil] "in" or "mm"; nil for PDF points,
31
+ # 72 to the inch, which is what Prawn measures in
32
+ # @return [Float]
25
33
  def value_from_units(value, from_units = nil)
26
34
  multiplier = if from_units.nil?
27
35
  if units.eql?('in')
@@ -39,6 +47,8 @@ module Laser
39
47
  value.to_f * multiplier
40
48
  end
41
49
 
50
+ # @return [Array<Array(String, Float, Float)>] each page size, sorted by
51
+ # name, with its width and height in {#units}
42
52
  def page_size_values
43
53
  h = Hashie::Mash.new(SIZES)
44
54
  h.keys.sort.map do |k|
@@ -3,15 +3,26 @@
3
3
  module Laser
4
4
  module Cutter
5
5
  module Renderer
6
- # subject is what we are rendering
7
- # enclosure is the rectangle enclosing our subject's rendered image
8
- # page_manager contains access to units and page sizes
6
+ # The base of every renderer: it holds the settings and the thing it
7
+ # draws, its subject.
9
8
  class Base
9
+ # The color of the lines to cut.
10
10
  BLACK = "000000"
11
+ # The color of the settings block.
11
12
  BLUE = "0070E0"
12
13
 
14
+ # @!attribute [rw] config
15
+ # @return [Configuration] the settings
16
+ # @!attribute [rw] subject
17
+ # @return [Object, nil] what is drawn: a box, a line or a rectangle
18
+ # @!attribute [rw] enclosure
19
+ # @return [Geometry::Rect, nil] the rectangle around what is drawn
20
+ # @!attribute [rw] page_manager
21
+ # @return [PageManager] converts PDF points into the units of the settings
13
22
  attr_accessor :config, :subject, :enclosure, :page_manager
14
23
 
24
+ # @param config [Configuration]
25
+ # @param subject [Object, nil] what to draw
15
26
  def initialize(config, subject = nil)
16
27
  self.config = config
17
28
  self.subject = subject
@@ -19,15 +30,20 @@ module Laser
19
30
  end
20
31
 
21
32
  # Writes the document to config.file, yielding after each line drawn.
33
+ #
34
+ # @yieldparam line [Geometry::Line]
35
+ # @return [Integer] the number of bytes written
22
36
  def render(&)
23
37
  File.binwrite(config.file, document(&))
24
38
  end
25
39
 
26
40
  # @return [String] the document, yielding after each line drawn
41
+ # @raise [RuntimeError] when the renderer does not implement it
27
42
  def document
28
43
  raise 'Abstract method'
29
44
  end
30
45
 
46
+ # @return [Symbol] :in or :mm
31
47
  def units
32
48
  config.units.to_sym
33
49
  end
@@ -3,19 +3,28 @@
3
3
  module Laser
4
4
  module Cutter
5
5
  module Renderer
6
+ # Draws the lines of a box onto a Prawn document.
6
7
  class BoxRenderer < Base
8
+ # @return [Box, Sloped::Box] the box to draw
7
9
  alias_method :box, :subject
8
10
 
11
+ # @param config [Configuration] the box is built from it by {BoxFactory}
9
12
  def initialize(config)
10
13
  super
11
- self.subject = Laser::Cutter::Box.new(config)
14
+ self.subject = Laser::Cutter::BoxFactory.for(config)
12
15
  end
13
16
 
17
+ # Keeps room on the page for a rectangle at the bottom left, such as
18
+ # the settings block.
19
+ #
20
+ # @param rect [Geometry::Rect] in PDF points
21
+ # @return [Geometry::Point] its top right corner, in the units of the box
14
22
  def ensure_space_for(rect)
15
23
  coords = [rect.p2.x, rect.p2.y].map{ |a| page_manager.value_from_units(a) }
16
24
  box.metadata = Geometry::Point.new(coords)
17
25
  end
18
26
 
27
+ # @return [Geometry::Rect] the rectangle around every line of the box
19
28
  def enclosure
20
29
  box.enclosure
21
30
  end
@@ -26,6 +35,10 @@ module Laser
26
35
  end
27
36
 
28
37
  # Draws the box, yielding after each line so a caller can count them.
38
+ #
39
+ # @param pdf [Prawn::Document]
40
+ # @yieldparam line [Geometry::Line]
41
+ # @return [Array<Geometry::Line>] the lines
29
42
  def render(pdf = nil)
30
43
  pdf.line_width = config.stroke.send(units)
31
44
  pdf.stroke_color config[:color] || BLACK
@@ -6,8 +6,10 @@ module Laser
6
6
  # Writes the PDF: the box, its metadata block, and the unkerfed outline
7
7
  # when asked for.
8
8
  class LayoutRenderer < Base
9
+ # The color of the box drawn without kerf, under config.debug.
9
10
  UNKERFED_COLOR = 'DD2211'
10
11
 
12
+ # @param config [Configuration]
11
13
  def initialize(config)
12
14
  self.config = config
13
15
  super
@@ -31,6 +33,11 @@ module Laser
31
33
  pdf.render
32
34
  end
33
35
 
36
+ # The page size that fits the box.
37
+ #
38
+ # @param box_renderer [BoxRenderer]
39
+ # @param margin [Float] in PDF points
40
+ # @return [Array(Float, Float)] the width and height, in PDF points
34
41
  def calculate_image_boundary(box_renderer, margin)
35
42
  box_renderer.enclosure.to_a[1].map do |c|
36
43
  c.send(units) + (2 * margin)
@@ -39,11 +46,14 @@ module Laser
39
46
 
40
47
  private
41
48
 
49
+ # @return [MetaRenderer, nil] nil unless config.metadata is set
42
50
  def meta_renderer
43
51
  @meta_renderer ||= MetaRenderer.new(config) if config.metadata
44
52
  end
45
53
 
46
54
  # The box, then the same box without kerf when config.debug is set.
55
+ #
56
+ # @return [Array<BoxRenderer>]
47
57
  def box_renderers
48
58
  @box_renderers ||= begin
49
59
  configs = [config]
@@ -3,8 +3,13 @@
3
3
  module Laser
4
4
  module Cutter
5
5
  module Renderer
6
+ # Strokes one line onto a Prawn document.
6
7
  class LineRenderer < Base
8
+ # @return [Geometry::Line] the line to draw
7
9
  alias_method :line, :subject
10
+
11
+ # @param pdf [Prawn::Document]
12
+ # @return [void]
8
13
  def render(pdf = nil)
9
14
  pdf.stroke {
10
15
  pdf.line([line.p1.x, line.p1.y].map{ |p| p.send(units) },
@@ -12,6 +17,7 @@ module Laser
12
17
  }
13
18
  end
14
19
 
20
+ # @return [Geometry::Line] the line itself
15
21
  def enclosure
16
22
  line
17
23
  end
@@ -3,14 +3,42 @@
3
3
  module Laser
4
4
  module Cutter
5
5
  module Renderer
6
+ # Draws the block of settings at the bottom left of a PDF, under a
7
+ # banner that names the gem.
6
8
  class MetaRenderer < Laser::Cutter::Renderer::Base
7
- META_KEYS = %i(units width height depth thickness notch lid kerf stroke padding margin page_size page_layout).freeze
9
+ # The settings printed, in order, when they are set.
10
+ META_KEYS = %i(units width height depth front_height slope thickness notch lid kerf stroke padding margin page_size page_layout).freeze
8
11
 
12
+ # The block, in points: its width, the height of the banner above the
13
+ # settings, the height of one line of them, and the space under the last.
14
+ WIDTH = 140
15
+ # @see WIDTH
16
+ BANNER_HEIGHT = 55
17
+ # @see WIDTH
18
+ LINE_HEIGHT = 8.2
19
+ # @see WIDTH
20
+ FOOTER_HEIGHT = 4
21
+
22
+ # @param config [Configuration]
9
23
  def initialize(config = {})
10
24
  self.config = config
11
- self.enclosure = Laser::Cutter::Geometry::Rect.create(Laser::Cutter::Geometry::Point[1, 1], 140, 150)
25
+ height = BANNER_HEIGHT + (fields.size * LINE_HEIGHT) + FOOTER_HEIGHT
26
+ self.enclosure = Laser::Cutter::Geometry::Rect.create(Laser::Cutter::Geometry::Point[1, 1], WIDTH, height)
27
+ end
28
+
29
+ # The settings the block prints, in order. The block is as tall as
30
+ # they need: one line too many would start a second page.
31
+ #
32
+ # @return [Hash{Symbol => Object}]
33
+ def fields
34
+ metadata = config.to_hash
35
+ metadata[:page_size] ||= 'custom'
36
+ metadata.delete(:page_layout) if metadata[:page_size].eql?('custom')
37
+ META_KEYS.select { |key| metadata[key] }.to_h { |key| [key, metadata[key]] }
12
38
  end
13
39
 
40
+ # @param pdf [Prawn::Document]
41
+ # @return [void]
14
42
  def render(pdf = nil)
15
43
  banner = <<-EOF
16
44
 
@@ -23,14 +51,10 @@ module Laser
23
51
  EOF
24
52
 
25
53
  meta_color = BLUE
26
- meta_top_height = 55
27
-
28
- metadata = config.to_hash
29
- metadata[:page_size] ||= 'custom'
30
- metadata.delete(:page_layout) if metadata[:page_size].eql?('custom')
54
+ meta_top_height = BANNER_HEIGHT
31
55
 
32
- meta_fields = META_KEYS.find_all{ |k| metadata[k] }.join(": \n") + ": \n"
33
- meta_values = META_KEYS.find_all{ |k| metadata[k] }.map{ |k| metadata[k].to_s }.join("\n")
56
+ meta_fields = fields.keys.join(": \n") + ": \n"
57
+ meta_values = fields.values.join("\n")
34
58
 
35
59
  rect = enclosure
36
60
 
@@ -3,8 +3,13 @@
3
3
  module Laser
4
4
  module Cutter
5
5
  module Renderer
6
+ # Strokes the four sides of a rectangle onto a Prawn document.
6
7
  class RectRenderer < Base
8
+ # @return [Geometry::Rect] the rectangle to draw
7
9
  alias_method :rect, :subject
10
+
11
+ # @param pdf [Prawn::Document]
12
+ # @return [Array<Geometry::Line>] the sides
8
13
  def render(pdf)
9
14
  rect.sides.each do |side|
10
15
  LineRenderer.new(config, side).render(pdf)
@@ -9,9 +9,10 @@ module Laser
9
9
  # fits the page to the box, and carries the metadata as a <desc>
10
10
  # instead of drawing it.
11
11
  class SvgRenderer < Base
12
+ # @param config [Configuration] the box is built from it by {BoxFactory}
12
13
  def initialize(config)
13
14
  super
14
- self.subject = Laser::Cutter::Box.new(config)
15
+ self.subject = Laser::Cutter::BoxFactory.for(config)
15
16
  end
16
17
 
17
18
  # @return [Array<Geometry::Line>] every line to cut, computed once
@@ -37,24 +38,31 @@ module Laser
37
38
 
38
39
  private
39
40
 
41
+ # @return [Float] the margin, in the units of the settings
40
42
  def margin
41
43
  config.margin.to_f
42
44
  end
43
45
 
46
+ # @return [Float] the width of the page: the box and both margins
44
47
  def width
45
48
  (subject.enclosure.p2.x + (2 * margin)).round(5)
46
49
  end
47
50
 
51
+ # @return [Float] the height of the page: the box and both margins
48
52
  def height
49
53
  (subject.enclosure.p2.y + (2 * margin)).round(5)
50
54
  end
51
55
 
52
56
  # SVG counts y down from the top, the box geometry up from the bottom.
57
+ #
58
+ # @param line [Geometry::Line]
59
+ # @return [Hash{Symbol => Float}] x1, y1, x2 and y2 on the page
53
60
  def coordinates(line)
54
61
  { x1: (line.p1.x + margin).round(5), y1: (height - margin - line.p1.y).round(5),
55
62
  x2: (line.p2.x + margin).round(5), y2: (height - margin - line.p2.y).round(5) }
56
63
  end
57
64
 
65
+ # @return [String] the gem's version and the settings, for the <desc>
58
66
  def description
59
67
  fields = MetaRenderer::META_KEYS.filter_map { |key| "#{key}: #{config[key]}" if config[key] }
60
68
  "Made with laser-cutter v#{VERSION}. #{fields.join(', ')}"
@@ -2,6 +2,8 @@
2
2
 
3
3
  module Laser
4
4
  module Cutter
5
+ # Draws a box into a document: a PDF through Prawn, or an SVG through
6
+ # Victor. {Renderer.for} picks the renderer for a format.
5
7
  module Renderer
6
8
  # The renderer that writes each output format.
7
9
  FORMATS = {
@@ -0,0 +1,90 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Laser
4
+ module Cutter
5
+ # The box whose front slopes back, with seven panels: its side view,
6
+ # its panels, their notches and their layout on the page.
7
+ module Sloped
8
+ # A box whose front slopes back, drawn as seven panels. It answers what
9
+ # the renderers ask of a Box: the lines to cut, the rectangle that
10
+ # encloses them, and room for the metadata.
11
+ #
12
+ # The panels come from Panels, each in its own frame and without kerf.
13
+ # Here each one is grown by half the kerf, so it is cut to size, and
14
+ # moved to its place on the sheet.
15
+ class Box
16
+ # How the tilted panel joins the side walls: notched into both, or lying on them.
17
+ LIDS = %i[full plain].freeze
18
+
19
+ # @return [Profile] the side view of the box
20
+ attr_reader :profile
21
+
22
+ # @return [Geometry::Point] the size of the metadata block the layout keeps clear
23
+ attr_reader :metadata
24
+
25
+ # @param config [Configuration, Hash] width, height, depth, front_height, slope, thickness, and
26
+ # optionally notch, kerf, padding and lid
27
+ # @raise [InvalidOption] when the numbers do not make this shape
28
+ def initialize(config)
29
+ @config = config
30
+ @profile = Profile.new(depth: config[:depth], height: config[:height], front_height: config[:front_height],
31
+ slope: config[:slope], thickness: config[:thickness])
32
+ @metadata = Geometry::Point[0, 0]
33
+ end
34
+
35
+ # Moves every panel clear of a metadata block of the given size.
36
+ #
37
+ # @param size [Geometry::Point]
38
+ def metadata=(size)
39
+ @metadata = size
40
+ @outlines = @enclosure = nil
41
+ end
42
+
43
+ # @return [Hash{String => Array<Geometry::Line>}] the lines to cut for each panel, by its name
44
+ def outlines
45
+ @outlines ||= begin
46
+ placements = layout.placements
47
+ panels.to_h.to_h do |name, loop|
48
+ [name, loop.map { |point| placements[name].apply(point) }.grow(kerf / 2.0).lines]
49
+ end
50
+ end
51
+ end
52
+
53
+ # @return [Array<Geometry::Line>] every line to cut
54
+ def generate_notches
55
+ outlines.values.flatten
56
+ end
57
+ alias notches generate_notches
58
+
59
+ # @return [Geometry::Rect] the smallest rectangle around every line
60
+ def enclosure
61
+ @enclosure ||= begin
62
+ points = generate_notches.flat_map { |line| [line.p1, line.p2] }
63
+ xs = points.map(&:x)
64
+ ys = points.map(&:y)
65
+ Geometry::Rect[Geometry::Point[xs.min, ys.min], Geometry::Point[xs.max, ys.max]]
66
+ end
67
+ end
68
+
69
+ private
70
+
71
+ # @return [Float] the kerf, 0 when none is set
72
+ def kerf = (@config[:kerf] || 0.0).to_f
73
+
74
+ # @return [Symbol] :full or :plain
75
+ def lid = (@config[:lid] || LIDS.first).to_sym
76
+
77
+ # @return [Panels] the seven panels, notched, each in its own frame
78
+ def panels
79
+ notcher = Notcher.new(notch_width: @config[:notch] || (3 * profile.thickness), thickness: profile.thickness)
80
+ Panels.new(profile: profile, width: @config[:width], notcher: notcher, lid: lid)
81
+ end
82
+
83
+ # @return [Layout] where each panel goes on the page
84
+ def layout
85
+ Layout.new(sizes: panels.sizes, thickness: profile.thickness, padding: @config[:padding] || 0.0, metadata: metadata)
86
+ end
87
+ end
88
+ end
89
+ end
90
+ end