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

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

Overview

Canvas carries the diagram's computed pixel bounds plus the coordinate-translation helpers that align element (0,0) with EA's content origin (35, 40). Bound computation itself is delegated to BoundsCalculator (SRP).

Promoted from a Struct.new + do ... end block to a proper class so the public API is explicit (attr_reader vs implicit struct fields) and behavior is named rather than implicit. Struct definitions with instance methods conflate "data" and "behavior" — a class keeps them separable while the values stay immutable.

Constant Summary collapse

PX_PER_CM =

EA's diagram canvas places element (0,0) at SVG (35, 40) — the diagram's content origin inset from the canvas top-left corner. Reverse-engineered from reference SVG byte-diff against the maintenance diagram.

37.795275591
FRAME_INSET_LEFT =
35
FRAME_INSET_TOP =
40

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(min_x:, min_y:, width:, height:) ⇒ Canvas

Returns a new instance of Canvas.



28
29
30
31
32
33
# File 'lib/ea/svg/ea_emitter/canvas.rb', line 28

def initialize(min_x:, min_y:, width:, height:)
  @min_x = min_x
  @min_y = min_y
  @width = width
  @height = height
end

Instance Attribute Details

#heightObject (readonly)

Returns the value of attribute height.



26
27
28
# File 'lib/ea/svg/ea_emitter/canvas.rb', line 26

def height
  @height
end

#min_xObject (readonly)

Returns the value of attribute min_x.



26
27
28
# File 'lib/ea/svg/ea_emitter/canvas.rb', line 26

def min_x
  @min_x
end

#min_yObject (readonly)

Returns the value of attribute min_y.



26
27
28
# File 'lib/ea/svg/ea_emitter/canvas.rb', line 26

def min_y
  @min_y
end

#widthObject (readonly)

Returns the value of attribute width.



26
27
28
# File 'lib/ea/svg/ea_emitter/canvas.rb', line 26

def width
  @width
end

Class Method Details

.coord(value) ⇒ Object

Compact coordinate formatter. Integers lose the decimal point; floats keep up to two digits with trailing zeros stripped (matches EA's emitted numeric format).



71
72
73
74
75
# File 'lib/ea/svg/ea_emitter/canvas.rb', line 71

def self.coord(value)
  return value.to_i.to_s if value == value.to_i

  format("%.2f", value).sub(/\.?0+$/, "")
end

.from(diagram, model_index: nil) ⇒ Object

Build a Canvas for a diagram by running the bound calculator. Defaults model_index to nil (no canvas-aware element bounds needed) for callers that only need a frame-size estimate.



39
40
41
42
43
# File 'lib/ea/svg/ea_emitter/canvas.rb', line 39

def self.from(diagram, model_index: nil)
  min_x, min_y, width, height =
    BoundsCalculator.new(diagram, model_index: model_index).compute
  new(min_x: min_x, min_y: min_y, width: width, height: height)
end

Instance Method Details

#height_cmObject



53
54
55
# File 'lib/ea/svg/ea_emitter/canvas.rb', line 53

def height_cm
  format_cm(height)
end

#translate_x(x) ⇒ Object

Translate a content-space point into SVG-space. EA insets the content area from the canvas top-left by FRAME_INSET_LEFT/TOP.



60
61
62
# File 'lib/ea/svg/ea_emitter/canvas.rb', line 60

def translate_x(x)
  x - min_x + FRAME_INSET_LEFT
end

#translate_y(y) ⇒ Object



64
65
66
# File 'lib/ea/svg/ea_emitter/canvas.rb', line 64

def translate_y(y)
  y - min_y + FRAME_INSET_TOP
end

#view_boxObject



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

def view_box
  "0 0 #{width} #{height}"
end

#width_cmObject



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

def width_cm
  format_cm(width)
end