Class: Archimate::Svg::Diagram

Inherits:
Object
  • Object
show all
Defined in:
lib/archimate/svg/diagram.rb

Constant Summary collapse

DEFAULT_SVG_OPTIONS =
{
  legend: false,
  format_xml: true
}.freeze

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(diagram, options = {}) ⇒ Diagram

Returns a new instance of Diagram.



18
19
20
21
22
# File 'lib/archimate/svg/diagram.rb', line 18

def initialize(diagram, options = {})
  @diagram = diagram
  @options = DEFAULT_SVG_OPTIONS.merge(options)
  @svg_template = Nokogiri::XML(SvgTemplate.new.to_s).freeze
end

Instance Attribute Details

#diagramObject (readonly)

Returns the value of attribute diagram.



9
10
11
# File 'lib/archimate/svg/diagram.rb', line 9

def diagram
  @diagram
end

#optionsObject (readonly)

Returns the value of attribute options.



11
12
13
# File 'lib/archimate/svg/diagram.rb', line 11

def options
  @options
end

#svg_docObject (readonly)

Returns the value of attribute svg_doc.



10
11
12
# File 'lib/archimate/svg/diagram.rb', line 10

def svg_doc
  @svg_doc
end

Instance Method Details

#archimate_diagram_groupObject



46
47
48
# File 'lib/archimate/svg/diagram.rb', line 46

def archimate_diagram_group
  @archimate_diagram_group ||= svg_doc.at_css("#archimate-diagram")
end

#calculate_max_extentsObject



88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
# File 'lib/archimate/svg/diagram.rb', line 88

def calculate_max_extents
  node_vals =
    svg_element
    .xpath("//*[@x or @y]")
    .map { |node| %w[x y width height].map { |attr| node.attr(attr).to_i } }
  svg_element.css(".archimate-relationship")
             .each do |path|
    path.attr("d").split(" ").each_slice(3) do |point|
      node_vals << [point[1].to_i, point[2].to_i, 0, 0]
    end
  end
  Extents.new(
    node_vals.map(&:first).min,
    node_vals.map { |v| v[0] + v[2] }.max,
    node_vals.map { |v| v[1] }.min,
    node_vals.map { |v| v[1] + v[3] }.max
  )
end

#format_node(node, depth = 4) ⇒ Object



80
81
82
83
84
85
86
# File 'lib/archimate/svg/diagram.rb', line 80

def format_node(node, depth = 4)
  node.children.each do |child|
    child.add_previous_sibling("\n#{' ' * depth}")
    format_node(child, depth + 2)
    child.add_next_sibling("\n#{' ' * (depth - 2)}") if node.children.last == child
  end
end

#format_xml?Boolean

Returns:

  • (Boolean)


58
59
60
# File 'lib/archimate/svg/diagram.rb', line 58

def format_xml?
  options[:format_xml]
end

#include_legend?Boolean

Returns:

  • (Boolean)


54
55
56
# File 'lib/archimate/svg/diagram.rb', line 54

def include_legend?
  options[:legend]
end

#indentationObject



62
63
64
# File 'lib/archimate/svg/diagram.rb', line 62

def indentation
  format_xml? ? 2 : 0
end

#legend_groupObject



50
51
52
# File 'lib/archimate/svg/diagram.rb', line 50

def legend_group
  @legend_group ||= svg_doc.at_css("#archimate-legend")
end

#render_connections(svg_node) ⇒ Object



113
114
115
116
117
# File 'lib/archimate/svg/diagram.rb', line 113

def render_connections(svg_node)
  diagram
    .connections
    .reduce(svg_node) { |svg, connection| Connection.new(connection).render(svg) }
end

#render_elements(svg_node) ⇒ Object



107
108
109
110
111
# File 'lib/archimate/svg/diagram.rb', line 107

def render_elements(svg_node)
  diagram
    .nodes
    .reduce(svg_node) { |svg, view_node| ViewNode.new(view_node).render_elements(svg) }
end

#set_titleObject



66
67
68
69
# File 'lib/archimate/svg/diagram.rb', line 66

def set_title
  svg_doc.at_css("title").content = diagram.name || "untitled"
  svg_doc.at_css("desc").content = diagram.documentation.to_s || ""
end

#svg_elementObject



42
43
44
# File 'lib/archimate/svg/diagram.rb', line 42

def svg_element
  @svg_element ||= svg_doc.at_css("svg")
end

#to_svgObject



24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
# File 'lib/archimate/svg/diagram.rb', line 24

def to_svg
  @svg_doc = @svg_template.clone
  set_title
  render_connections(
    render_elements(archimate_diagram_group)
  )
  legend = Legend.new(self)
  if include_legend?
    legend.insert
  else
    legend.remove
  end
  update_viewbox
  format_node(archimate_diagram_group) if format_xml?
  format_node(legend_group) if include_legend? && format_xml?
  svg_doc.to_xml(encoding: 'UTF-8', indent: indentation)
end

#update_viewboxObject

Scan the SVG and figure out min & max



72
73
74
75
76
77
78
# File 'lib/archimate/svg/diagram.rb', line 72

def update_viewbox
  extents = calculate_max_extents.expand(10)
  svg_element.set_attribute(:width, extents.width)
  svg_element.set_attribute(:height, extents.height)
  svg_element.set_attribute("viewBox", "#{extents.min_x} #{extents.min_y} #{extents.width} #{extents.height}")
  svg_element
end