Module: Ea::Svg::EaEmitter::Compartment::InstanceUnderline

Defined in:
lib/ea/svg/ea_emitter/compartment/instance_underline.rb

Overview

Instance-name underline for object diagrams. EA draws a short horizontal line just below each instance name to denote that it is an instance (UML convention). Rendered only when the diagram's diagram_type is "Object".

Constant Summary collapse

Y_OFFSET =
2
WIDTH_FACTOR =
0.65

Class Method Summary collapse

Class Method Details

.build_path_tag(x_start, y_pos, text_width) ⇒ Object



44
45
46
47
48
49
50
51
52
# File 'lib/ea/svg/ea_emitter/compartment/instance_underline.rb', line 44

def build_path_tag(x_start, y_pos, text_width)
  x_end = x_start + text_width
  coords = format_coords(x_start, y_pos, x_end)
  d = "M #{coords[:x1]} #{coords[:y1]} L #{coords[:x2]} #{coords[:y1]}"
  style = "stroke-width:1;stroke-linecap:square;" \
          "stroke-linejoin:miter; fill:none; stroke:#000000;" \
          " stroke-opacity:1.00"
  %(<g style="#{style}">\n  <path d="#{d}" shape-rendering="auto"/>\n</g>)
end

.format_coords(x_start, y_pos, x_end) ⇒ Object



55
56
57
58
59
60
61
# File 'lib/ea/svg/ea_emitter/compartment/instance_underline.rb', line 55

def format_coords(x_start, y_pos, x_end)
  {
    x1: Ea::Svg::EaEmitter::Canvas.coord(x_start),
    x2: Ea::Svg::EaEmitter::Canvas.coord(x_end),
    y1: Ea::Svg::EaEmitter::Canvas.coord(y_pos)
  }
end

.object_diagram?(context) ⇒ Object



29
30
31
# File 'lib/ea/svg/ea_emitter/compartment/instance_underline.rb', line 29

def object_diagram?(context)
  context.diagram && context.diagram.diagram_type == "Object"
end

.render(context) ⇒ Object



17
18
19
20
21
22
23
24
25
26
27
# File 'lib/ea/svg/ea_emitter/compartment/instance_underline.rb', line 17

def render(context)
  return nil unless context.diagram
  return nil unless object_diagram?(context)
  return nil if context.header_lines.empty?

  name_line = context.header_lines.last
  text = name_line.first.to_s
  return nil if text.empty?

  underline_path(context, text)
end

.underline_path(context, text) ⇒ Object



34
35
36
37
38
39
40
41
# File 'lib/ea/svg/ea_emitter/compartment/instance_underline.rb', line 34

def underline_path(context, text)
  bounds = context.bounds
  text_width = TextRenderer.estimate_width(text, context.size,
                                           WIDTH_FACTOR)
  x_start = bounds.x + (bounds.width - text_width) / 2.0
  y_pos = context.geometry.header_first_y + Y_OFFSET
  build_path_tag(x_start, y_pos, text_width)
end