Class: Ea::Svg::EaEmitter::TextRenderer
- Inherits:
-
Object
- Object
- Ea::Svg::EaEmitter::TextRenderer
- Defined in:
- lib/ea/svg/ea_emitter/text_renderer.rb
Overview
Single source of truth for emitting <text> SVG elements.
Handles:
- Decimal
x/yformatting (%.2f): matches EA'sx="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
textLengthinteger (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
-
#content ⇒ Object
readonly
Returns the value of attribute content.
-
#family ⇒ Object
readonly
Returns the value of attribute family.
-
#fill ⇒ Object
readonly
Returns the value of attribute fill.
-
#rotation ⇒ Object
readonly
Returns the value of attribute rotation.
-
#size ⇒ Object
readonly
Returns the value of attribute size.
-
#size_unit ⇒ Object
readonly
Returns the value of attribute size_unit.
-
#stroke_in_text ⇒ Object
readonly
Returns the value of attribute stroke_in_text.
-
#style ⇒ Object
readonly
Returns the value of attribute style.
-
#text_length ⇒ Object
readonly
Returns the value of attribute text_length.
-
#weight ⇒ Object
readonly
Returns the value of attribute weight.
-
#width_factor ⇒ Object
readonly
Returns the value of attribute width_factor.
-
#x ⇒ Object
readonly
Returns the value of attribute x.
-
#y ⇒ Object
readonly
Returns the value of attribute y.
Class Method Summary collapse
Instance Method Summary collapse
-
#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
constructor
A new instance of TextRenderer.
- #to_svg ⇒ Object
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
#content ⇒ Object (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 |
#family ⇒ Object (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 |
#fill ⇒ Object (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 |
#rotation ⇒ Object (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 |
#size ⇒ Object (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_unit ⇒ Object (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_text ⇒ Object (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 |
#style ⇒ Object (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_length ⇒ Object (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 |
#weight ⇒ Object (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_factor ⇒ Object (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 |
#x ⇒ Object (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 |
#y ⇒ Object (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_svg ⇒ Object
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 |