Class: Ea::Svg::EaEmitter::TextRenderer

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

Overview

Single source of truth for emitting <text> SVG elements. Handles:

  • Decimal x/y formatting (%.2f): matches EA's x="11.00" y="19.00" encoding.
  • Always-present rotation transform: even when rotation is 0, EA emits transform="rotate(-0.00 X Y)".
  • Proper XML escaping of content.
  • Optional textLength integer (computed by caller).

Replaces the duplicated build_text helpers across HeaderRenderer, AttributeRenderer, OperationRenderer, EnumerationLiteralRenderer, Labels, and DiagramFrame.

Constant Summary collapse

DEFAULT_FILL =
"#000000"
DEFAULT_ROTATION =
-0.00
DEFAULT_STROKE_IN_TEXT =
"#000000"
DEFAULT_WIDTH_FACTOR =
0.65

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(content:, x:, y:, family:, size:, weight: 400, style: "normal", fill: DEFAULT_FILL, text_length: nil, rotation: DEFAULT_ROTATION, size_unit: "px", stroke_in_text: DEFAULT_STROKE_IN_TEXT, width_factor: DEFAULT_WIDTH_FACTOR) ⇒ TextRenderer

Returns a new instance of TextRenderer.



29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
# File 'lib/ea/svg/ea_emitter/text_renderer.rb', line 29

def initialize(content:, x:, y:, family:, size:, weight: 400,
               style: "normal", fill: DEFAULT_FILL,
               text_length: nil, rotation: DEFAULT_ROTATION,
               size_unit: "px",
               stroke_in_text: DEFAULT_STROKE_IN_TEXT,
               width_factor: DEFAULT_WIDTH_FACTOR)
  @content = content.to_s
  @x = x
  @y = y
  @family = family
  @size = size
  @weight = weight
  @style = style
  @fill = fill
  @text_length = text_length
  @rotation = rotation
  @size_unit = size_unit
  @stroke_in_text = stroke_in_text
  @width_factor = width_factor
end

Instance Attribute Details

#contentObject (readonly)

Returns the value of attribute content.



25
26
27
# File 'lib/ea/svg/ea_emitter/text_renderer.rb', line 25

def content
  @content
end

#familyObject (readonly)

Returns the value of attribute family.



25
26
27
# File 'lib/ea/svg/ea_emitter/text_renderer.rb', line 25

def family
  @family
end

#fillObject (readonly)

Returns the value of attribute fill.



25
26
27
# File 'lib/ea/svg/ea_emitter/text_renderer.rb', line 25

def fill
  @fill
end

#rotationObject (readonly)

Returns the value of attribute rotation.



25
26
27
# File 'lib/ea/svg/ea_emitter/text_renderer.rb', line 25

def rotation
  @rotation
end

#sizeObject (readonly)

Returns the value of attribute size.



25
26
27
# File 'lib/ea/svg/ea_emitter/text_renderer.rb', line 25

def size
  @size
end

#size_unitObject (readonly)

Returns the value of attribute size_unit.



25
26
27
# File 'lib/ea/svg/ea_emitter/text_renderer.rb', line 25

def size_unit
  @size_unit
end

#stroke_in_textObject (readonly)

Returns the value of attribute stroke_in_text.



25
26
27
# File 'lib/ea/svg/ea_emitter/text_renderer.rb', line 25

def stroke_in_text
  @stroke_in_text
end

#styleObject (readonly)

Returns the value of attribute style.



25
26
27
# File 'lib/ea/svg/ea_emitter/text_renderer.rb', line 25

def style
  @style
end

#text_lengthObject (readonly)

Returns the value of attribute text_length.



25
26
27
# File 'lib/ea/svg/ea_emitter/text_renderer.rb', line 25

def text_length
  @text_length
end

#weightObject (readonly)

Returns the value of attribute weight.



25
26
27
# File 'lib/ea/svg/ea_emitter/text_renderer.rb', line 25

def weight
  @weight
end

#width_factorObject (readonly)

Returns the value of attribute width_factor.



25
26
27
# File 'lib/ea/svg/ea_emitter/text_renderer.rb', line 25

def width_factor
  @width_factor
end

#xObject (readonly)

Returns the value of attribute x.



25
26
27
# File 'lib/ea/svg/ea_emitter/text_renderer.rb', line 25

def x
  @x
end

#yObject (readonly)

Returns the value of attribute y.



25
26
27
# File 'lib/ea/svg/ea_emitter/text_renderer.rb', line 25

def y
  @y
end

Class Method Details

.estimate_width(text, size, width_factor = 0.65) ⇒ Object



66
67
68
69
70
71
# File 'lib/ea/svg/ea_emitter/text_renderer.rb', line 66

def self.estimate_width(text, size, width_factor = 0.65)
  content = text.to_s
  space_count = content.count(" ")
  letter_count = content.length - space_count
  letter_count * size * width_factor + space_count * size * 0.3
end

Instance Method Details

#to_svgObject



50
51
52
53
54
# File 'lib/ea/svg/ea_emitter/text_renderer.rb', line 50

def to_svg
  attrs = format_attrs
  style = format_style
  %(<text #{attrs} textLength="#{formatted_text_length}" style="#{style}" xml:space="preserve" transform="#{formatted_transform}">#{escaped_content}</text>)
end