Debian Codegraph @

tests/graphviz_edge.cpp

raw
1 #include <cassert>
2 #include <string>
3 #include <unordered_set>
4
5 #include "graphviz_edge.h"
6 #include "graphviz_node.h"
7 #include "svg_element.h"
8
9 GraphvizEdge::GraphvizEdge(SVG::SVGElement &svg_g_element)
10 : m_edgeop(svg_g_element.graphviz_id), m_svg_g_element(svg_g_element) {}
11
12 void GraphvizEdge::add_bbox() { m_svg_g_element.add_bbox(); }
13
14 void GraphvizEdge::add_outline_bbox() { m_svg_g_element.add_outline_bbox(); }
15
16 void GraphvizEdge::add_outline_overlap_bbox(const GraphvizNode &node,
17 const double tolerance) {
18 m_svg_g_element.add_outline_overlap_bbox(node.svg_g_element(), tolerance);
19 }
20
21 static const std::unordered_set<std::string_view>
22 path_based_primitive_arrow_shapes = {
23 "curve", //
24 "icurve", //
25 };
26
27 static const std::unordered_set<std::string_view>
28 polygon_based_primitive_arrow_shapes = {
29 "box", //
30 "crow", //
31 "diamond", //
32 "inv", //
33 "normal", //
34 "tee", //
35 "vee", //
36 };
37
38 static const std::unordered_set<std::string_view>
39 ellipse_based_primitive_arrow_shapes = {
40 "dot", //
41 };
42
43 static const std::unordered_set<std::string_view>
44 primitive_arrow_shapes_containing_polyline = {
45 "box", //
46 "curve", //
47 "icurve", //
48 "tee", //
49 };
50
51 static SVG::SVGElementType
52 edge_arrowhead_main_svg_element_type(std::string_view primitive_arrow_shape) {
53 if (polygon_based_primitive_arrow_shapes.contains(primitive_arrow_shape)) {
54 return SVG::SVGElementType::Polygon;
55 } else if (ellipse_based_primitive_arrow_shapes.contains(
56 primitive_arrow_shape)) {
57 return SVG::SVGElementType::Ellipse;
58 } else if (path_based_primitive_arrow_shapes.contains(
59 primitive_arrow_shape)) {
60 return SVG::SVGElementType::Path;
61 } else {
62 assert(false && "unsupported primitive arrow shape");
63 }
64 }
65
66 SVG::SVGRect GraphvizEdge::arrowhead_outline_bbox(
67 std::string_view dir, std::string_view primitive_arrow_shape) const {
68 assert((dir == "forward" || dir == "both") &&
69 "no arrowhead for this edge direction");
70
71 const auto index = dir == "forward" ? 0 : 1;
72
73 const auto main_svg_element_type =
74 edge_arrowhead_main_svg_element_type(primitive_arrow_shape);
75 const auto main_index =
76 index + (main_svg_element_type == SVG::SVGElementType::Path ? 1 : 0);
77 auto edge_arrowhead =
78 m_svg_g_element.find_child(main_svg_element_type, main_index);
79 auto edge_arrowhead_bbox = edge_arrowhead.outline_bbox();
80 if (primitive_arrow_shapes_containing_polyline.contains(
81 primitive_arrow_shape)) {
82 auto edge_arrowhead_stem =
83 m_svg_g_element.find_child(SVG::SVGElementType::Polyline, index);
84 auto edge_arrowhead_stem_bbox = edge_arrowhead_stem.outline_bbox();
85 edge_arrowhead_bbox.extend(edge_arrowhead_stem_bbox);
86 }
87
88 return edge_arrowhead_bbox;
89 }
90
91 SVG::SVGRect GraphvizEdge::arrowtail_outline_bbox(
92 std::string_view dir, std::string_view primitive_arrow_shape) const {
93 assert((dir == "back" || dir == "both") &&
94 "no arrowhead for this edge direction");
95 const auto index = 0;
96 const auto main_svg_element_type =
97 edge_arrowhead_main_svg_element_type(primitive_arrow_shape);
98 const auto main_index =
99 index + (main_svg_element_type == SVG::SVGElementType::Path ? 1 : 0);
100 auto edge_arrowtail =
101 m_svg_g_element.find_child(main_svg_element_type, main_index);
102 auto edge_arrowtail_bbox = edge_arrowtail.outline_bbox();
103 if (primitive_arrow_shapes_containing_polyline.contains(
104 primitive_arrow_shape)) {
105 auto edge_arrowtail_stem =
106 m_svg_g_element.find_child(SVG::SVGElementType::Polyline, index);
107 auto edge_arrowtail_stem_bbox = edge_arrowtail_stem.outline_bbox();
108 edge_arrowtail_bbox.extend(edge_arrowtail_stem_bbox);
109 }
110
111 return edge_arrowtail_bbox;
112 }
113
114 std::string_view GraphvizEdge::edgeop() const { return m_edgeop; }
115
116 std::string GraphvizEdge::fillcolor() const {
117 const auto fill = m_svg_g_element.attribute_from_subtree<std::string>(
118 &SVG::SVGAttributes::fill, &SVG::SVGElement::is_closed_shape_element, "");
119 const auto fill_opacity = m_svg_g_element.attribute_from_subtree<double>(
120 &SVG::SVGAttributes::fill_opacity,
121 &SVG::SVGElement::is_closed_shape_element, 1);
122 if (fill.empty() && fill_opacity == 1) {
123 return "";
124 }
125 return SVG::to_dot_color(fill, fill_opacity);
126 }
127
128 SVG::SVGElement &GraphvizEdge::stem() const {
129 return m_svg_g_element.find_child(SVG::SVGElementType::Path);
130 }
131
132 const SVG::SVGElement &GraphvizEdge::svg_g_element() const {
133 return m_svg_g_element;
134 }
135
136 SVG::SVGRect GraphvizEdge::bbox() const { return m_svg_g_element.bbox(); }
137
138 SVG::SVGPoint GraphvizEdge::center() const { return bbox().center(); }
139
140 std::string GraphvizEdge::color() const {
141 const auto stroke = m_svg_g_element.attribute_from_subtree<std::string>(
142 &SVG::SVGAttributes::stroke, &SVG::SVGElement::is_shape_element, "");
143 const auto stroke_opacity = m_svg_g_element.attribute_from_subtree<double>(
144 &SVG::SVGAttributes::stroke_opacity, &SVG::SVGElement::is_shape_element,
145 1);
146 return SVG::to_dot_color(stroke, stroke_opacity);
147 }
148
149 double GraphvizEdge::penwidth() const {
150 return m_svg_g_element.attribute_from_subtree<double>(
151 &SVG::SVGAttributes::stroke_width, &SVG::SVGElement::is_shape_element, 1);
152 }
153
154 SVG::SVGRect GraphvizEdge::outline_bbox() const {
155 return m_svg_g_element.outline_bbox();
156 }