Class: BoxPacker::SVGExporter

Inherits:
Object
  • Object
show all
Defined in:
lib/box_packer/svg_exporter.rb

Defined Under Namespace

Classes: Face

Instance Method Summary collapse

Constructor Details

#initialize(container, opts = {}) ⇒ SVGExporter

Returns a new instance of SVGExporter.



5
6
7
8
9
10
11
12
13
14
15
16
17
18
# File 'lib/box_packer/svg_exporter.rb', line 5

def initialize(container, opts = {})
  @container = container
  @images = []
  @margin  = opts[:margin] || 10

  dimensions = container.dimensions.to_a
  longest_side = dimensions.max
  sides_total = dimensions.reduce(&:+)
  scale_longest_side_to = opts[:scale_longest_side_to] || 400

  @scale = scale_longest_side_to / longest_side.to_f
  @image_width = (longest_side * scale * 2) + (margin * 3)
  @image_height = (sides_total * scale) + (margin * 4)
end

Instance Method Details

#drawObject



30
31
32
33
34
35
36
37
38
39
40
41
42
43
# File 'lib/box_packer/svg_exporter.rb', line 30

def draw
  container.packings.each do |packing|
    Face.reset(margin, scale, container.dimensions)
    new_image
    6.times do
      face = Face.new(packing)
      image.rectangle(*face.outline, stroke: 'black', stroke_width: 1, fill: 'white')
      face.rectangles_and_labels.each do |h|
        image.rectangle(*h[:rectangle])
        image.text(*h[:label])
      end
    end
  end
end

#save(filename) ⇒ Object



20
21
22
23
24
25
26
27
28
# File 'lib/box_packer/svg_exporter.rb', line 20

def save(filename)
  images.each_with_index do |image, i|
    image.close

    File.open("#{filename}#{i + 1}.svg", 'w') do |f|
      f << image.output
    end
  end
end