Class: BoxPacker::SVGExporter::Face

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

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(packing) ⇒ Face

Returns a new instance of Face.



77
78
79
80
81
82
83
84
85
# File 'lib/box_packer/svg_exporter.rb', line 77

def initialize(packing)
  @i, @j, @k = @@coords_mapping.dup
  @front = @@front
  @axes = @@axes.dup
  @width = @@container_dimensions[i] * @@scale
  @height = @@container_dimensions[j] * @@scale
  iterate_class_variables
  @items = sorted_items(packing)
end

Instance Attribute Details

#axesObject (readonly)

Returns the value of attribute axes.



55
56
57
# File 'lib/box_packer/svg_exporter.rb', line 55

def axes
  @axes
end

#heightObject (readonly)

Returns the value of attribute height.



55
56
57
# File 'lib/box_packer/svg_exporter.rb', line 55

def height
  @height
end

#widthObject (readonly)

Returns the value of attribute width.



55
56
57
# File 'lib/box_packer/svg_exporter.rb', line 55

def width
  @width
end

Class Method Details

.reset(margin, scale, container_dimensions) ⇒ Object



57
58
59
60
61
62
63
64
# File 'lib/box_packer/svg_exporter.rb', line 57

def self.reset(margin, scale, container_dimensions)
  @@coords_mapping = [0, 1, 2]
  @@front = true
  @@margin = margin
  @@axes = [margin, margin]
  @@scale = scale
  @@container_dimensions = container_dimensions
end

Instance Method Details

#iterate_class_variablesObject



66
67
68
69
70
71
72
73
74
75
# File 'lib/box_packer/svg_exporter.rb', line 66

def iterate_class_variables
  if front
    @@axes[0] = width + @@margin * 2
  else
    @@coords_mapping.rotate!
    @@axes[0] = @@margin
    @@axes[1] += height + @@margin
  end
  @@front = !@@front
end

#outlineObject



87
88
89
# File 'lib/box_packer/svg_exporter.rb', line 87

def outline
  @axes + [width, height]
end

#rectangles_and_labelsObject



91
92
93
94
95
96
97
98
99
100
101
102
103
104
# File 'lib/box_packer/svg_exporter.rb', line 91

def rectangles_and_labels
  items.map do |item|
    x = axes[0] + item.position[i] * @@scale
    y = axes[1] + item.position[j] * @@scale
    width = item.dimensions[i] * @@scale
    height = item.dimensions[j] * @@scale
    label_x = x + width / 2 - item.label.length
    label_y = y + height / 2
    {
      rectangle: [x, y, width, height, fill: item.colour],
      label: [label_x, label_y, item.label]
    }
  end
end