Class: Ea::Diagram::ElementRenderers::ClassRenderer

Inherits:
BaseRenderer
  • Object
show all
Defined in:
lib/ea/diagram/element_renderers/class_renderer.rb

Overview

Renderer for UML class elements

Instance Attribute Summary

Attributes inherited from BaseRenderer

#element, #style_resolver

Instance Method Summary collapse

Methods inherited from BaseRenderer

#escape_text, #initialize, #render

Methods included from Util

#parse_ea_geometry, #parse_geometry_offsets, #style_to_css

Constructor Details

This class inherits a constructor from Ea::Diagram::ElementRenderers::BaseRenderer

Instance Method Details

#calculate_attributes_heightObject



169
170
171
172
173
# File 'lib/ea/diagram/element_renderers/class_renderer.rb', line 169

def calculate_attributes_height
  return 0 unless element[:attributes]&.any?

  (element[:attributes].size * 15) + 10
end

#calculate_operations_heightObject



175
176
177
178
179
# File 'lib/ea/diagram/element_renderers/class_renderer.rb', line 175

def calculate_operations_height
  return 0 unless element[:operations]&.any?

  (element[:operations].size * 15) + 10
end

#calculate_text_length(text, font_size) ⇒ Object

Calculate approximate text length in pixels (EA uses this for precise positioning)



311
312
313
314
315
316
317
318
319
# File 'lib/ea/diagram/element_renderers/class_renderer.rb', line 311

def calculate_text_length(text, font_size)
  return 0 unless text

  # Simple approximation: average character width * text length
  # This is a rough approximation
  # - in a real implementation, we'd use actual font metrics
  font_size_num = parse_font_size(font_size)
  text.to_s.length * (font_size_num * 0.6).to_i
end

#format_attribute(attribute) ⇒ Object



251
252
253
254
255
256
257
258
259
# File 'lib/ea/diagram/element_renderers/class_renderer.rb', line 251

def format_attribute(attribute)
  return attribute unless attribute.is_a?(Hash)

  visibility = visibility_symbol(attribute[:visibility])
  name = attribute[:name] || ""
  type = attribute[:type] ? ": #{attribute[:type]}" : ""

  "#{visibility}#{name}#{type}"
end

#format_operation(operation) ⇒ Object



261
262
263
264
265
266
267
268
269
270
271
272
273
274
# File 'lib/ea/diagram/element_renderers/class_renderer.rb', line 261

def format_operation(operation)
  return operation unless operation.is_a?(Hash)

  visibility = visibility_symbol(operation[:visibility])
  name = operation[:name] || ""
  params = format_parameters(operation[:parameters] || [])
  return_type = if operation[:return_type]
                  ": #{operation[:return_type]}"
                else
                  ""
                end

  "#{visibility}#{name}(#{params})#{return_type}"
end

#format_parameters(parameters) ⇒ Object



276
277
278
279
280
281
282
283
284
# File 'lib/ea/diagram/element_renderers/class_renderer.rb', line 276

def format_parameters(parameters)
  parameters.map do |param|
    if param.is_a?(Hash)
      "#{param[:name]}: #{param[:type]}"
    else
      param.to_s
    end
  end.join(", ")
end

#parse_font_size(font_size) ⇒ Object

Parse font size string to get numeric value



297
298
299
300
301
302
303
304
305
306
307
# File 'lib/ea/diagram/element_renderers/class_renderer.rb', line 297

def parse_font_size(font_size)
  return 7 if font_size.nil? # default size

  # Extract numeric part from font size string (e.g., "7pt" -> 7)
  if font_size.is_a?(String)
    match = font_size.match(/(\d+(?:\.\d+)?)/)
    match ? match[1].to_f : 7
  else
    font_size.to_f
  end
end

#render_attributes_compartment_separator(x, y, width, name_height, attributes_height, style) ⇒ Object

rubocop:disable Metrics/ParameterLists



181
182
183
184
185
186
187
188
189
190
191
# File 'lib/ea/diagram/element_renderers/class_renderer.rb', line 181

def render_attributes_compartment_separator( # rubocop:disable Metrics/ParameterLists
  x, y, width, name_height, # rubocop:disable Naming/MethodParameterName
  attributes_height, style
)
  return "" if attributes_height.zero?

  separator_y = y + name_height + attributes_height
  <<~SVG
    <path d="M #{x} #{separator_y} L #{x + width} #{separator_y}" shape-rendering="#{style[:shape_rendering] || 'auto'}"/>
  SVG
end

#render_label(style) ⇒ Object

rubocop:disable Metrics/AbcSize,Metrics/CyclomaticComplexity,Metrics/MethodLength,Metrics/PerceivedComplexity



73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
# File 'lib/ea/diagram/element_renderers/class_renderer.rb', line 73

def render_label(style) # rubocop:disable Metrics/AbcSize,Metrics/CyclomaticComplexity,Metrics/MethodLength,Metrics/PerceivedComplexity
  x = element[:x] || 0
  y = element[:y] || 0
  width = element[:width] || 120

  # Calculate compartment heights
  name_height = 25
  attributes_height = calculate_attributes_height
  calculate_operations_height

  svg_content = +""

  # Class name (centered in name compartment)
  name_y = y + (name_height / 2) + 5
  svg_content << render_text_element(
    element[:name],
    x + (width / 2),
    name_y,
    style,
    "lutaml-diagram-class-name",
    font_weight: style[:font_weight] || "700",
    font_style: style[:font_style] || "italic",
    font_family: style[:font_family] || "Cambria",
    font_size: style[:font_size] || "7pt",
  )

  # Stereotype (above name if present)
  if element[:stereotype]
    stereotype_y = y + 15
    svg_content << render_text_element(
      "«#{element[:stereotype]}»",
      x + (width / 2),
      stereotype_y,
      style,
      "lutaml-diagram-class-stereotype",
      font_weight: style[:stereotype_font_weight] || "0",
      font_style: style[:stereotype_font_style] || "normal",
      font_family: style[:stereotype_font_family] || "Cambria",
      font_size: style[:stereotype_font_size] || "7pt",
    )
  end

  # Attributes
  if element[:attributes]&.any?
    attr_y = y + name_height + 15
    element[:attributes].each do |attr|
      svg_content << render_text_element(
        format_attribute(attr),
        x + 5,
        attr_y,
        style,
        "lutaml-diagram-class-attribute",
        font_weight: style[:attribute_font_weight] || "0",
        font_style: style[:attribute_font_style] || "normal",
        font_family: style[:attribute_font_family] || "Cambria",
        font_size: style[:attribute_font_size] || "7pt",
        text_anchor: "start",
      )
      attr_y += 15
    end
  end

  # Operations
  if element[:operations]&.any?
    op_y = y + name_height + attributes_height + 15
    element[:operations].each do |op|
      svg_content << render_text_element(
        format_operation(op),
        x + 5,
        op_y,
        style,
        "lutaml-diagram-class-operation",
        font_weight: style[:attribute_font_weight] || "0",
        font_style: style[:attribute_font_style] || "normal",
        font_family: style[:attribute_font_family] || "Cambria",
        font_size: style[:attribute_font_size] || "7pt",
        text_anchor: "start",
      )
      op_y += 15
    end
  end

  # Build text style string
  text_style_attrs = []
  text_style_attrs << "stroke-width:1"
  text_style_attrs << "stroke-linecap:round"
  text_style_attrs << "stroke-linejoin:bevel"
  text_style_attrs << "fill:#000000"
  text_style_attrs << "fill-opacity:1.00"
  text_style_attrs << "stroke:#000000"
  text_style_attrs << "stroke-opacity:0.00"

  "<g style=\"#{text_style_attrs.join(';')}\">\n#{svg_content}\n</g>"
end

#render_operations_compartment_separator(x, y, width, name_height, attributes_height, operations_height, style) ⇒ Object

rubocop:disable Metrics/ParameterLists



193
194
195
196
197
198
199
200
201
202
203
204
# File 'lib/ea/diagram/element_renderers/class_renderer.rb', line 193

def render_operations_compartment_separator( # rubocop:disable Metrics/ParameterLists
  x, y, width, name_height, # rubocop:disable Naming/MethodParameterName
  attributes_height, operations_height, style
)
  return "" if operations_height.zero?

  separator_y = y + name_height +
    attributes_height + operations_height
  <<~SVG
    <path d="M #{x} #{separator_y} L #{x + width} #{separator_y}" shape-rendering="#{style[:shape_rendering] || 'auto'}"/>
  SVG
end

#render_shape(style) ⇒ Object

rubocop:disable Metrics/AbcSize,Metrics/CyclomaticComplexity,Metrics/MethodLength,Metrics/PerceivedComplexity



8
9
10
11
12
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
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
# File 'lib/ea/diagram/element_renderers/class_renderer.rb', line 8

def render_shape(style) # rubocop:disable Metrics/AbcSize,Metrics/CyclomaticComplexity,Metrics/MethodLength,Metrics/PerceivedComplexity
  x = element[:x] || 0
  y = element[:y] || 0
  width = element[:width] || 120
  height = element[:height] || 80

  # Calculate compartment heights
  name_height = 25
  attributes_height = calculate_attributes_height
  operations_height = calculate_operations_height

  total_height = name_height + attributes_height + operations_height

  # Adjust element height if needed
  height = [height, total_height].max

  # Build style string for the group
  group_style_attrs = []
  group_style_attrs << "stroke-width:#{style[:stroke_width] || 1}"
  group_style_attrs << "stroke-linecap:#{style[:stroke_linecap] ||
    'round'}"
  group_style_attrs << "stroke-linejoin:#{style[:stroke_linejoin] ||
    'bevel'}"
  group_style_attrs << "fill:#{style[:fill]}"
  group_style_attrs << "fill-opacity:#{style[:fill_opacity] ||
    '1.00'}"
  group_style_attrs << "stroke:#{style[:stroke]}"
  group_style_attrs << "stroke-opacity:#{style[:stroke_opacity] ||
    '1.00'}"

  # Build style string for compartment lines
  line_style_attrs = []
  line_style_attrs << "stroke-width:#{style[:compartment_width] || 1}"
  line_style_attrs << "stroke-linecap:#{style[:stroke_linecap] ||
    'round'}"
  line_style_attrs << "stroke-linejoin:#{style[:stroke_linejoin] ||
    'bevel'}"
  line_style_attrs << "fill:#000000"
  line_style_attrs << "fill-opacity:0.00"
  line_style_attrs << "stroke:#{style[:stroke] || '#000000'}"
  line_style_attrs << "stroke-opacity:#{style[:stroke_opacity] ||
    '1.00'}"

  <<~SVG
    <g style="#{group_style_attrs.join(';')}">
     <rect x="#{x}"
          y="#{y}"
          width="#{width}"
          height="#{height}"
          rx="#{style[:corner_radius] || 0.00}"
          shape-rendering="#{style[:shape_rendering] || 'auto'}"  />
    </g>
    <g style="#{line_style_attrs.join(';')}">
    <!-- Name compartment -->
    <path d="M #{x} #{y + name_height} L #{x + width} #{y + name_height}" shape-rendering="#{style[:shape_rendering] || 'auto'}"/>

    <!-- Attributes compartment (if any) -->
    #{render_attributes_compartment_separator(x, y, width, name_height, attributes_height, style)}

    <!-- Operations compartment (if any) -->
    #{render_operations_compartment_separator(x, y, width, name_height, attributes_height, operations_height, style)}
    </g>
  SVG
end

#render_text_element(text, x, y, style, css_class, options = {}) ⇒ Object

rubocop:disable Metrics/AbcSize,Metrics/CyclomaticComplexity,Metrics/MethodLength,Metrics/PerceivedComplexity,Metrics/ParameterLists,Naming/MethodParameterName



206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
# File 'lib/ea/diagram/element_renderers/class_renderer.rb', line 206

def render_text_element(text, x, y, style, css_class, options = {}) # rubocop:disable Metrics/AbcSize,Metrics/CyclomaticComplexity,Metrics/MethodLength,Metrics/PerceivedComplexity,Metrics/ParameterLists,Naming/MethodParameterName
  return "" unless text

  font_size = options[:font_size] || style[:font_size] || "7pt"
  font_weight = options[:font_weight] || style[:font_weight] || "0"
  font_style = options[:font_style] || style[:font_style] || "normal"
  font_family = options[:font_family] || style[:font_family] ||
    "Cambria"
  fill_color = options[:fill] || style[:text_color] || "#000000"
  text_anchor = options[:text_anchor] || "middle"

  # Calculate text length approximation
  # (EA uses this for precise positioning)
  text_length = calculate_text_length(text, font_size)

  # Build transform attribute if needed
  transform_attr = ""
  if options[:rotate] && options[:rotate] != 0
    transform_attr = "transform=\"rotate(#{options[:rotate]} " \
                     "#{x} #{y})\""
  end

  # Text stroke styling (EA uses this for text elements)
  text_stroke = options[:text_stroke] ||
    style[:text_stroke] ||
    "#000000"
  text_stroke_opacity = options[:text_stroke_opacity] ||
    style[:text_stroke_opacity] || "0.00"
  text_stroke_width = options[:text_stroke_width] ||
    style[:text_stroke_width] || "0"

  <<~SVG
    <text x="#{x}.00"
          y="#{y}.00"
          text-anchor="#{text_anchor}"
          class="#{css_class}"
          #{"textLength=\"#{text_length}\"" if text_length.positive?}
          style="font-family:#{font_family}; font-weight:#{font_weight}; font-style:#{font_style}; font-size:#{font_size}; fill:#{fill_color};fill-opacity:1.00; stroke:#{text_stroke}; stroke-opacity:#{text_stroke_opacity} stroke-width:#{text_stroke_width}; white-space: pre;"
          xml:space="preserve"
          #{transform_attr}>
      #{escape_text(text)}
    </text>
  SVG
end

#visibility_symbol(visibility) ⇒ Object



286
287
288
289
290
291
292
293
294
# File 'lib/ea/diagram/element_renderers/class_renderer.rb', line 286

def visibility_symbol(visibility)
  case visibility&.to_s
  when "public" then "+"
  when "private" then "-"
  when "protected" then "#"
  when "package" then "~"
  else ""
  end
end