| 1 |
#include <format> |
| 2 |
#include <memory> |
| 3 |
#include <stdexcept> |
| 4 |
|
| 5 |
#include <boost/algorithm/string.hpp> |
| 6 |
#include <catch2/catch_all.hpp> |
| 7 |
|
| 8 |
#include "svg_analyzer.h" |
| 9 |
#include "svgpp_context.h" |
| 10 |
#include "svgpp_document_traverser.h" |
| 11 |
#include <cgraph++/AGraph.h> |
| 12 |
#include <gvc++/GVContext.h> |
| 13 |
#include <gvc++/GVLayout.h> |
| 14 |
#include <gvc++/GVRenderData.h> |
| 15 |
|
| 16 |
SVGAnalyzer::SVGAnalyzer(char *text) |
| 17 |
: m_original_svg(text), m_svg(SVG::SVGElement(SVG::SVGElementType::Svg)) { |
| 18 |
m_elements_in_process.push_back(&m_svg); |
| 19 |
SvgppContext context{this}; |
| 20 |
traverseDocumentWithSvgpp(context, text); |
| 21 |
if (m_elements_in_process.size() != 1) { |
| 22 |
throw std::runtime_error{ |
| 23 |
"Wrong number of elements in process after traversing SVG document"}; |
| 24 |
} |
| 25 |
m_elements_in_process.pop_back(); |
| 26 |
retrieve_graphviz_components(); |
| 27 |
} |
| 28 |
|
| 29 |
const std::vector<GraphvizGraph> &SVGAnalyzer::graphs() const { |
| 30 |
return m_graphs; |
| 31 |
} |
| 32 |
|
| 33 |
void SVGAnalyzer::on_enter_element_svg() { m_num_svgs++; } |
| 34 |
|
| 35 |
void SVGAnalyzer::on_enter_element_g() { |
| 36 |
enter_element(SVG::SVGElementType::Group); |
| 37 |
m_num_groups++; |
| 38 |
} |
| 39 |
|
| 40 |
void SVGAnalyzer::on_enter_element_circle() { |
| 41 |
enter_element(SVG::SVGElementType::Circle); |
| 42 |
m_num_circles++; |
| 43 |
} |
| 44 |
|
| 45 |
void SVGAnalyzer::on_enter_element_ellipse() { |
| 46 |
enter_element(SVG::SVGElementType::Ellipse); |
| 47 |
m_num_ellipses++; |
| 48 |
} |
| 49 |
|
| 50 |
void SVGAnalyzer::on_enter_element_line() { |
| 51 |
enter_element(SVG::SVGElementType::Line); |
| 52 |
m_num_lines++; |
| 53 |
} |
| 54 |
|
| 55 |
void SVGAnalyzer::on_enter_element_path() { |
| 56 |
enter_element(SVG::SVGElementType::Path); |
| 57 |
m_num_paths++; |
| 58 |
} |
| 59 |
|
| 60 |
void SVGAnalyzer::on_enter_element_polygon() { |
| 61 |
enter_element(SVG::SVGElementType::Polygon); |
| 62 |
m_num_polygons++; |
| 63 |
} |
| 64 |
|
| 65 |
void SVGAnalyzer::on_enter_element_polyline() { |
| 66 |
enter_element(SVG::SVGElementType::Polyline); |
| 67 |
m_num_polylines++; |
| 68 |
} |
| 69 |
|
| 70 |
void SVGAnalyzer::on_enter_element_rect() { |
| 71 |
enter_element(SVG::SVGElementType::Rect); |
| 72 |
m_num_rects++; |
| 73 |
} |
| 74 |
|
| 75 |
void SVGAnalyzer::on_enter_element_text() { |
| 76 |
enter_element(SVG::SVGElementType::Text); |
| 77 |
m_num_texts++; |
| 78 |
} |
| 79 |
|
| 80 |
void SVGAnalyzer::on_enter_element_title() { |
| 81 |
enter_element(SVG::SVGElementType::Title); |
| 82 |
m_num_titles++; |
| 83 |
} |
| 84 |
|
| 85 |
void SVGAnalyzer::on_exit_element() { m_elements_in_process.pop_back(); } |
| 86 |
|
| 87 |
void SVGAnalyzer::path_cubic_bezier_to(double x1, double y1, double x2, |
| 88 |
double y2, double x, double y) { |
| 89 |
auto &path_points = current_element().path_points; |
| 90 |
path_points.emplace_back(x1, y1); |
| 91 |
path_points.emplace_back(x2, y2); |
| 92 |
path_points.emplace_back(x, y); |
| 93 |
} |
| 94 |
|
| 95 |
void SVGAnalyzer::path_move_to(double x, double y) { |
| 96 |
auto &paths = current_element().path_points; |
| 97 |
paths.emplace_back(x, y); |
| 98 |
} |
| 99 |
|
| 100 |
void SVGAnalyzer::path_exit() {} |
| 101 |
|
| 102 |
SVG::SVGElement &SVGAnalyzer::grandparent_element() { |
| 103 |
if (m_elements_in_process.empty()) { |
| 104 |
throw std::runtime_error{"No current element to get grandparent of"}; |
| 105 |
} |
| 106 |
if (m_elements_in_process.size() == 1) { |
| 107 |
throw std::runtime_error{"No parent element"}; |
| 108 |
} |
| 109 |
if (m_elements_in_process.size() == 2) { |
| 110 |
throw std::runtime_error{"No grandparent element"}; |
| 111 |
} |
| 112 |
return *m_elements_in_process.end()[-3]; |
| 113 |
} |
| 114 |
|
| 115 |
SVG::SVGElement &SVGAnalyzer::parent_element() { |
| 116 |
if (m_elements_in_process.empty()) { |
| 117 |
throw std::runtime_error{"No current element to get parent of"}; |
| 118 |
} |
| 119 |
if (m_elements_in_process.size() == 1) { |
| 120 |
throw std::runtime_error{"No parent element"}; |
| 121 |
} |
| 122 |
return *m_elements_in_process.end()[-2]; |
| 123 |
} |
| 124 |
|
| 125 |
SVG::SVGElement &SVGAnalyzer::current_element() { |
| 126 |
if (m_elements_in_process.empty()) { |
| 127 |
throw std::runtime_error{"No current element"}; |
| 128 |
} |
| 129 |
return *m_elements_in_process.back(); |
| 130 |
} |
| 131 |
|
| 132 |
void SVGAnalyzer::enter_element(SVG::SVGElementType elem_type) { |
| 133 |
if (m_elements_in_process.empty()) { |
| 134 |
throw std::runtime_error{ |
| 135 |
"No element is currently being processed by the SVG++ document " |
| 136 |
"traverser when entering a new element. Expecting at least the top " |
| 137 |
"level 'svg' to be in process"}; |
| 138 |
} |
| 139 |
auto &element = current_element(); |
| 140 |
element.children.emplace_back(elem_type); |
| 141 |
m_elements_in_process.push_back(&element.children.back()); |
| 142 |
} |
| 143 |
|
| 144 |
void SVGAnalyzer::set_class(std::string_view class_) { |
| 145 |
current_element().attributes.class_ = class_; |
| 146 |
} |
| 147 |
|
| 148 |
void SVGAnalyzer::retrieve_graphviz_components() { |
| 149 |
retrieve_graphviz_components_impl(m_svg); |
| 150 |
} |
| 151 |
|
| 152 |
void SVGAnalyzer::retrieve_graphviz_components_impl( |
| 153 |
SVG::SVGElement &svg_element) { |
| 154 |
if (svg_element.elem_type == SVG::SVGElementType::Group) { |
| 155 |
// The SVG 'class' attribute determines which type of Graphviz element a 'g' |
| 156 |
// element corresponds to |
| 157 |
const auto &class_ = svg_element.attributes.class_; |
| 158 |
if (class_ == "graph") { |
| 159 |
m_graphs.emplace_back(svg_element); |
| 160 |
} else if (class_ == "node") { |
| 161 |
if (m_graphs.empty()) { |
| 162 |
throw std::runtime_error{ |
| 163 |
std::format("No graph to add node {} to", svg_element.graphviz_id)}; |
| 164 |
} |
| 165 |
m_graphs.back().add_node(svg_element); |
| 166 |
} else if (class_ == "edge") { |
| 167 |
if (m_graphs.empty()) { |
| 168 |
throw std::runtime_error{ |
| 169 |
std::format("No graph to add edge {} to", svg_element.graphviz_id)}; |
| 170 |
} |
| 171 |
m_graphs.back().add_edge(svg_element); |
| 172 |
} else if (class_ == "cluster") { |
| 173 |
// ignore for now |
| 174 |
} else { |
| 175 |
throw std::runtime_error{std::format("Unknown class {}", class_)}; |
| 176 |
} |
| 177 |
} |
| 178 |
|
| 179 |
for (auto &child : svg_element.children) { |
| 180 |
retrieve_graphviz_components_impl(child); |
| 181 |
} |
| 182 |
} |
| 183 |
|
| 184 |
void SVGAnalyzer::re_create_and_verify_svg() { |
| 185 |
|
| 186 |
const auto indent_size = 0; |
| 187 |
auto recreated_svg = svg_string(indent_size); |
| 188 |
|
| 189 |
// compare the recreated SVG with the original SVG |
| 190 |
if (recreated_svg != m_original_svg) { |
| 191 |
std::vector<std::string> original_svg_lines; |
| 192 |
boost::split(original_svg_lines, m_original_svg, boost::is_any_of("\n")); |
| 193 |
|
| 194 |
std::vector<std::string> recreated_svg_lines; |
| 195 |
boost::split(recreated_svg_lines, recreated_svg, boost::is_any_of("\n")); |
| 196 |
|
| 197 |
for (std::size_t i = 0; i < original_svg_lines.size(); i++) { |
| 198 |
REQUIRE(i < recreated_svg_lines.size()); |
| 199 |
REQUIRE(recreated_svg_lines[i] == original_svg_lines[i]); |
| 200 |
} |
| 201 |
|
| 202 |
REQUIRE(recreated_svg_lines.size() == original_svg_lines.size()); |
| 203 |
} |
| 204 |
} |
| 205 |
|
| 206 |
void SVGAnalyzer::set_cx(double cx) { current_element().attributes.cx = cx; } |
| 207 |
|
| 208 |
void SVGAnalyzer::set_cy(double cy) { current_element().attributes.cy = cy; } |
| 209 |
|
| 210 |
void SVGAnalyzer::set_font_family(std::string_view font_family) { |
| 211 |
current_element().attributes.font_family = font_family; |
| 212 |
} |
| 213 |
|
| 214 |
void SVGAnalyzer::set_font_size(double font_size) { |
| 215 |
current_element().attributes.font_size = font_size; |
| 216 |
} |
| 217 |
|
| 218 |
void SVGAnalyzer::set_fill(std::string_view fill) { |
| 219 |
current_element().attributes.fill = fill; |
| 220 |
} |
| 221 |
|
| 222 |
void SVGAnalyzer::set_fill_opacity(double fill_opacity) { |
| 223 |
current_element().attributes.fill_opacity = fill_opacity; |
| 224 |
} |
| 225 |
|
| 226 |
void SVGAnalyzer::set_height(double height) { |
| 227 |
current_element().attributes.height = height; |
| 228 |
} |
| 229 |
|
| 230 |
void SVGAnalyzer::set_stroke(std::string_view stroke) { |
| 231 |
current_element().attributes.stroke = stroke; |
| 232 |
} |
| 233 |
|
| 234 |
void SVGAnalyzer::set_stroke_opacity(double stroke_opacity) { |
| 235 |
current_element().attributes.stroke_opacity = stroke_opacity; |
| 236 |
} |
| 237 |
|
| 238 |
void SVGAnalyzer::set_stroke_width(double stroke_width) { |
| 239 |
current_element().attributes.stroke_width = stroke_width; |
| 240 |
} |
| 241 |
|
| 242 |
void SVGAnalyzer::set_id(std::string_view id) { |
| 243 |
current_element().attributes.id = id; |
| 244 |
} |
| 245 |
|
| 246 |
void SVGAnalyzer::set_rx(double rx) { current_element().attributes.rx = rx; } |
| 247 |
|
| 248 |
void SVGAnalyzer::set_ry(double ry) { current_element().attributes.ry = ry; } |
| 249 |
|
| 250 |
void SVGAnalyzer::set_point(std::pair<double, double> pt) { |
| 251 |
current_element().attributes.points.emplace_back(pt.first, pt.second); |
| 252 |
} |
| 253 |
|
| 254 |
void SVGAnalyzer::set_text(std::string_view text) { |
| 255 |
auto &element = current_element(); |
| 256 |
element.text = text; |
| 257 |
|
| 258 |
if (element.elem_type == SVG::SVGElementType::Title) { |
| 259 |
// The title text is normally the 'graph_id', 'node_id' or 'edgeop' |
| 260 |
// according to the DOT language specification. Save it on the parent 'g' |
| 261 |
// element to avoid having to look it up later. |
| 262 |
if (parent_element().elem_type != SVG::SVGElementType::Group) { |
| 263 |
throw std::runtime_error{"Unexpected parent element of 'title' element"}; |
| 264 |
} |
| 265 |
parent_element().graphviz_id = text; |
| 266 |
// If the 'g' element corresponds to the graph, set the Graphviz ID also on |
| 267 |
// the top level 'svg' element. |
| 268 |
if (grandparent_element().elem_type == SVG::SVGElementType::Svg) { |
| 269 |
grandparent_element().graphviz_id = text; |
| 270 |
} |
| 271 |
} |
| 272 |
} |
| 273 |
|
| 274 |
void SVGAnalyzer::set_text_anchor(std::string_view text_anchor) { |
| 275 |
current_element().attributes.text_anchor = text_anchor; |
| 276 |
} |
| 277 |
|
| 278 |
void SVGAnalyzer::set_width(double width) { |
| 279 |
current_element().attributes.width = width; |
| 280 |
} |
| 281 |
|
| 282 |
void SVGAnalyzer::set_x(double x) { current_element().attributes.x = x; } |
| 283 |
|
| 284 |
void SVGAnalyzer::set_y(double y) { current_element().attributes.y = y; } |
| 285 |
|
| 286 |
void SVGAnalyzer::add_bboxes() { |
| 287 |
for (auto &graph : m_graphs) { |
| 288 |
graph.add_bboxes(); |
| 289 |
} |
| 290 |
} |
| 291 |
|
| 292 |
void SVGAnalyzer::add_node_edge_outline_bbox_overlaps(double tolerance) { |
| 293 |
for (auto &graph : m_graphs) { |
| 294 |
graph.add_node_edge_outline_bbox_overlaps(tolerance); |
| 295 |
} |
| 296 |
} |
| 297 |
|
| 298 |
void SVGAnalyzer::add_outline_bboxes() { |
| 299 |
for (auto &graph : m_graphs) { |
| 300 |
graph.add_outline_bboxes(); |
| 301 |
} |
| 302 |
} |
| 303 |
|
| 304 |
SVGAnalyzer SVGAnalyzer::make_from_dot(const std::string &dot_source, |
| 305 |
const std::string &engine) { |
| 306 |
auto g = CGraph::AGraph{dot_source}; |
| 307 |
|
| 308 |
const auto demand_loading = false; |
| 309 |
auto gvc = |
| 310 |
std::make_shared<GVC::GVContext>(lt_preloaded_symbols, demand_loading); |
| 311 |
|
| 312 |
const auto layout = GVC::GVLayout(gvc, std::move(g), engine); |
| 313 |
|
| 314 |
const auto result = layout.render("svg"); |
| 315 |
|
| 316 |
auto svg_analyzer = SVGAnalyzer{result.c_str()}; |
| 317 |
|
| 318 |
svg_analyzer.set_graphviz_version(gvc->version()); |
| 319 |
svg_analyzer.set_graphviz_build_date(gvc->buildDate()); |
| 320 |
|
| 321 |
return svg_analyzer; |
| 322 |
} |
| 323 |
|
| 324 |
std::string_view SVGAnalyzer::original_svg() const { return m_original_svg; } |
| 325 |
|
| 326 |
void SVGAnalyzer::set_transform(double a, double b, double c, double d, |
| 327 |
double e, double f) { |
| 328 |
current_element().attributes.transform = {a, b, c, d, e, f}; |
| 329 |
} |
| 330 |
|
| 331 |
void SVGAnalyzer::set_viewBox(double x, double y, double width, double height) { |
| 332 |
current_element().attributes.viewBox = {x, y, width, height}; |
| 333 |
} |
| 334 |
|
| 335 |
void SVGAnalyzer::set_graphviz_version(std::string_view version) { |
| 336 |
m_svg.graphviz_version = version; |
| 337 |
} |
| 338 |
|
| 339 |
void SVGAnalyzer::set_graphviz_build_date(std::string_view build_date) { |
| 340 |
m_svg.graphviz_build_date = build_date; |
| 341 |
} |
| 342 |
|
| 343 |
std::string SVGAnalyzer::svg_string(std::size_t indent_size) const { |
| 344 |
return m_svg.to_string(indent_size); |
| 345 |
} |