Class: Archimate::Svg::Diagram
- Inherits:
-
Object
- Object
- Archimate::Svg::Diagram
- Defined in:
- lib/archimate/svg/diagram.rb
Constant Summary collapse
- DEFAULT_SVG_OPTIONS =
{ legend: false, format_xml: true }.freeze
Instance Attribute Summary collapse
-
#diagram ⇒ Object
readonly
Returns the value of attribute diagram.
-
#options ⇒ Object
readonly
Returns the value of attribute options.
-
#svg_doc ⇒ Object
readonly
Returns the value of attribute svg_doc.
Instance Method Summary collapse
- #archimate_diagram_group ⇒ Object
- #calculate_max_extents ⇒ Object
- #format_node(node, depth = 4) ⇒ Object
- #format_xml? ⇒ Boolean
- #include_legend? ⇒ Boolean
- #indentation ⇒ Object
-
#initialize(diagram, options = {}) ⇒ Diagram
constructor
A new instance of Diagram.
- #legend_group ⇒ Object
- #render_connections(svg_node) ⇒ Object
- #render_elements(svg_node) ⇒ Object
- #set_title ⇒ Object
- #svg_element ⇒ Object
- #to_svg ⇒ Object
-
#update_viewbox ⇒ Object
Scan the SVG and figure out min & max.
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, = {}) @diagram = diagram @options = DEFAULT_SVG_OPTIONS.merge() @svg_template = Nokogiri::XML(SvgTemplate.new.to_s).freeze end |
Instance Attribute Details
#diagram ⇒ Object (readonly)
Returns the value of attribute diagram.
9 10 11 |
# File 'lib/archimate/svg/diagram.rb', line 9 def diagram @diagram end |
#options ⇒ Object (readonly)
Returns the value of attribute options.
11 12 13 |
# File 'lib/archimate/svg/diagram.rb', line 11 def @options end |
#svg_doc ⇒ Object (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_group ⇒ Object
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_extents ⇒ Object
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
58 59 60 |
# File 'lib/archimate/svg/diagram.rb', line 58 def format_xml? [:format_xml] end |
#include_legend? ⇒ Boolean
54 55 56 |
# File 'lib/archimate/svg/diagram.rb', line 54 def include_legend? [:legend] end |
#indentation ⇒ Object
62 63 64 |
# File 'lib/archimate/svg/diagram.rb', line 62 def indentation format_xml? ? 2 : 0 end |
#legend_group ⇒ Object
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_title ⇒ Object
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_element ⇒ Object
42 43 44 |
# File 'lib/archimate/svg/diagram.rb', line 42 def svg_element @svg_element ||= svg_doc.at_css("svg") end |
#to_svg ⇒ Object
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_viewbox ⇒ Object
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.(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 |