Class: Ea::Svg::EaEmitter::Canvas

Inherits:
Struct
  • Object
show all
Defined in:
lib/ea/svg/ea_emitter/canvas.rb

Overview

Computes the canvas dimensions for the root element. Union of all element image_bounds (the padded bounds EA uses for the visual box), with a 10 px outer margin. Emits cm dimensions (px / 28.346 at 72 DPI).

Constant Summary collapse

PX_PER_CM =
28.3464567

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#heightObject

Returns the value of attribute height

Returns:

  • (Object)

    the current value of height



10
11
12
# File 'lib/ea/svg/ea_emitter/canvas.rb', line 10

def height
  @height
end

#min_xObject

Returns the value of attribute min_x

Returns:

  • (Object)

    the current value of min_x



10
11
12
# File 'lib/ea/svg/ea_emitter/canvas.rb', line 10

def min_x
  @min_x
end

#min_yObject

Returns the value of attribute min_y

Returns:

  • (Object)

    the current value of min_y



10
11
12
# File 'lib/ea/svg/ea_emitter/canvas.rb', line 10

def min_y
  @min_y
end

#widthObject

Returns the value of attribute width

Returns:

  • (Object)

    the current value of width



10
11
12
# File 'lib/ea/svg/ea_emitter/canvas.rb', line 10

def width
  @width
end

Class Method Details

.from(diagram) ⇒ Object



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
# File 'lib/ea/svg/ea_emitter/canvas.rb', line 13

def self.from(diagram)
  points = []
  (diagram.elements || []).each do |e|
    b = e.image_bounds || e.bounds
    next unless b

    points << [b.x, b.y]
    points << [b.x + b.width, b.y + b.height]
  end
  (diagram.connectors || []).each do |c|
    (c.waypoints || []).each do |wp|
      next unless wp.position

      points << [wp.position.x, wp.position.y]
    end
  end
  return new(min_x: 0, min_y: 0, width: 1, height: 1) if points.empty?

  xs = points.map(&:first)
  ys = points.map(&:last)
  margin = 10
  new(
    min_x: (xs.min || 0) - margin,
    min_y: (ys.min || 0) - margin,
    width: (xs.max - xs.min) + (2 * margin),
    height: (ys.max - ys.min) + (2 * margin)
  )
end

Instance Method Details

#height_cmObject



50
51
52
# File 'lib/ea/svg/ea_emitter/canvas.rb', line 50

def height_cm
  format_cm(height)
end

#view_boxObject



42
43
44
# File 'lib/ea/svg/ea_emitter/canvas.rb', line 42

def view_box
  "#{min_x} #{min_y} #{width} #{height}"
end

#width_cmObject



46
47
48
# File 'lib/ea/svg/ea_emitter/canvas.rb', line 46

def width_cm
  format_cm(width)
end