File: svg_analyzer.cpp

package info (click to toggle)
graphviz 14.0.5-2
  • links: PTS
  • area: main
  • in suites: forky
  • size: 139,388 kB
  • sloc: ansic: 141,938; cpp: 11,957; python: 7,766; makefile: 4,043; yacc: 3,030; xml: 2,972; tcl: 2,495; sh: 1,388; objc: 1,159; java: 560; lex: 423; perl: 243; awk: 156; pascal: 139; php: 58; ruby: 49; cs: 31; sed: 1
file content (345 lines) | stat: -rw-r--r-- 10,429 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
#include <format>
#include <memory>
#include <stdexcept>

#include <boost/algorithm/string.hpp>
#include <catch2/catch_all.hpp>

#include "svg_analyzer.h"
#include "svgpp_context.h"
#include "svgpp_document_traverser.h"
#include <cgraph++/AGraph.h>
#include <gvc++/GVContext.h>
#include <gvc++/GVLayout.h>
#include <gvc++/GVRenderData.h>

SVGAnalyzer::SVGAnalyzer(char *text)
    : m_original_svg(text), m_svg(SVG::SVGElement(SVG::SVGElementType::Svg)) {
  m_elements_in_process.push_back(&m_svg);
  SvgppContext context{this};
  traverseDocumentWithSvgpp(context, text);
  if (m_elements_in_process.size() != 1) {
    throw std::runtime_error{
        "Wrong number of elements in process after traversing SVG document"};
  }
  m_elements_in_process.pop_back();
  retrieve_graphviz_components();
}

const std::vector<GraphvizGraph> &SVGAnalyzer::graphs() const {
  return m_graphs;
}

void SVGAnalyzer::on_enter_element_svg() { m_num_svgs++; }

void SVGAnalyzer::on_enter_element_g() {
  enter_element(SVG::SVGElementType::Group);
  m_num_groups++;
}

void SVGAnalyzer::on_enter_element_circle() {
  enter_element(SVG::SVGElementType::Circle);
  m_num_circles++;
}

void SVGAnalyzer::on_enter_element_ellipse() {
  enter_element(SVG::SVGElementType::Ellipse);
  m_num_ellipses++;
}

void SVGAnalyzer::on_enter_element_line() {
  enter_element(SVG::SVGElementType::Line);
  m_num_lines++;
}

void SVGAnalyzer::on_enter_element_path() {
  enter_element(SVG::SVGElementType::Path);
  m_num_paths++;
}

void SVGAnalyzer::on_enter_element_polygon() {
  enter_element(SVG::SVGElementType::Polygon);
  m_num_polygons++;
}

void SVGAnalyzer::on_enter_element_polyline() {
  enter_element(SVG::SVGElementType::Polyline);
  m_num_polylines++;
}

void SVGAnalyzer::on_enter_element_rect() {
  enter_element(SVG::SVGElementType::Rect);
  m_num_rects++;
}

void SVGAnalyzer::on_enter_element_text() {
  enter_element(SVG::SVGElementType::Text);
  m_num_texts++;
}

void SVGAnalyzer::on_enter_element_title() {
  enter_element(SVG::SVGElementType::Title);
  m_num_titles++;
}

void SVGAnalyzer::on_exit_element() { m_elements_in_process.pop_back(); }

void SVGAnalyzer::path_cubic_bezier_to(double x1, double y1, double x2,
                                       double y2, double x, double y) {
  auto &path_points = current_element().path_points;
  path_points.emplace_back(x1, y1);
  path_points.emplace_back(x2, y2);
  path_points.emplace_back(x, y);
}

void SVGAnalyzer::path_move_to(double x, double y) {
  auto &paths = current_element().path_points;
  paths.emplace_back(x, y);
}

void SVGAnalyzer::path_exit() {}

SVG::SVGElement &SVGAnalyzer::grandparent_element() {
  if (m_elements_in_process.empty()) {
    throw std::runtime_error{"No current element to get grandparent of"};
  }
  if (m_elements_in_process.size() == 1) {
    throw std::runtime_error{"No parent element"};
  }
  if (m_elements_in_process.size() == 2) {
    throw std::runtime_error{"No grandparent element"};
  }
  return *m_elements_in_process.end()[-3];
}

SVG::SVGElement &SVGAnalyzer::parent_element() {
  if (m_elements_in_process.empty()) {
    throw std::runtime_error{"No current element to get parent of"};
  }
  if (m_elements_in_process.size() == 1) {
    throw std::runtime_error{"No parent element"};
  }
  return *m_elements_in_process.end()[-2];
}

SVG::SVGElement &SVGAnalyzer::current_element() {
  if (m_elements_in_process.empty()) {
    throw std::runtime_error{"No current element"};
  }
  return *m_elements_in_process.back();
}

void SVGAnalyzer::enter_element(SVG::SVGElementType elem_type) {
  if (m_elements_in_process.empty()) {
    throw std::runtime_error{
        "No element is currently being processed by the SVG++ document "
        "traverser when entering a new element. Expecting at least the top "
        "level 'svg' to be in process"};
  }
  auto &element = current_element();
  element.children.emplace_back(elem_type);
  m_elements_in_process.push_back(&element.children.back());
}

void SVGAnalyzer::set_class(std::string_view class_) {
  current_element().attributes.class_ = class_;
}

void SVGAnalyzer::retrieve_graphviz_components() {
  retrieve_graphviz_components_impl(m_svg);
}

void SVGAnalyzer::retrieve_graphviz_components_impl(
    SVG::SVGElement &svg_element) {
  if (svg_element.elem_type == SVG::SVGElementType::Group) {
    // The SVG 'class' attribute determines which type of Graphviz element a 'g'
    // element corresponds to
    const auto &class_ = svg_element.attributes.class_;
    if (class_ == "graph") {
      m_graphs.emplace_back(svg_element);
    } else if (class_ == "node") {
      if (m_graphs.empty()) {
        throw std::runtime_error{
            std::format("No graph to add node {} to", svg_element.graphviz_id)};
      }
      m_graphs.back().add_node(svg_element);
    } else if (class_ == "edge") {
      if (m_graphs.empty()) {
        throw std::runtime_error{
            std::format("No graph to add edge {} to", svg_element.graphviz_id)};
      }
      m_graphs.back().add_edge(svg_element);
    } else if (class_ == "cluster") {
      // ignore for now
    } else {
      throw std::runtime_error{std::format("Unknown class {}", class_)};
    }
  }

  for (auto &child : svg_element.children) {
    retrieve_graphviz_components_impl(child);
  }
}

void SVGAnalyzer::re_create_and_verify_svg() {

  const auto indent_size = 0;
  auto recreated_svg = svg_string(indent_size);

  // compare the recreated SVG with the original SVG
  if (recreated_svg != m_original_svg) {
    std::vector<std::string> original_svg_lines;
    boost::split(original_svg_lines, m_original_svg, boost::is_any_of("\n"));

    std::vector<std::string> recreated_svg_lines;
    boost::split(recreated_svg_lines, recreated_svg, boost::is_any_of("\n"));

    for (std::size_t i = 0; i < original_svg_lines.size(); i++) {
      REQUIRE(i < recreated_svg_lines.size());
      REQUIRE(recreated_svg_lines[i] == original_svg_lines[i]);
    }

    REQUIRE(recreated_svg_lines.size() == original_svg_lines.size());
  }
}

void SVGAnalyzer::set_cx(double cx) { current_element().attributes.cx = cx; }

void SVGAnalyzer::set_cy(double cy) { current_element().attributes.cy = cy; }

void SVGAnalyzer::set_font_family(std::string_view font_family) {
  current_element().attributes.font_family = font_family;
}

void SVGAnalyzer::set_font_size(double font_size) {
  current_element().attributes.font_size = font_size;
}

void SVGAnalyzer::set_fill(std::string_view fill) {
  current_element().attributes.fill = fill;
}

void SVGAnalyzer::set_fill_opacity(double fill_opacity) {
  current_element().attributes.fill_opacity = fill_opacity;
}

void SVGAnalyzer::set_height(double height) {
  current_element().attributes.height = height;
}

void SVGAnalyzer::set_stroke(std::string_view stroke) {
  current_element().attributes.stroke = stroke;
}

void SVGAnalyzer::set_stroke_opacity(double stroke_opacity) {
  current_element().attributes.stroke_opacity = stroke_opacity;
}

void SVGAnalyzer::set_stroke_width(double stroke_width) {
  current_element().attributes.stroke_width = stroke_width;
}

void SVGAnalyzer::set_id(std::string_view id) {
  current_element().attributes.id = id;
}

void SVGAnalyzer::set_rx(double rx) { current_element().attributes.rx = rx; }

void SVGAnalyzer::set_ry(double ry) { current_element().attributes.ry = ry; }

void SVGAnalyzer::set_point(std::pair<double, double> pt) {
  current_element().attributes.points.emplace_back(pt.first, pt.second);
}

void SVGAnalyzer::set_text(std::string_view text) {
  auto &element = current_element();
  element.text = text;

  if (element.elem_type == SVG::SVGElementType::Title) {
    // The title text is normally the 'graph_id', 'node_id' or 'edgeop'
    // according to the DOT language specification. Save it on the parent 'g'
    // element to avoid having to look it up later.
    if (parent_element().elem_type != SVG::SVGElementType::Group) {
      throw std::runtime_error{"Unexpected parent element of 'title' element"};
    }
    parent_element().graphviz_id = text;
    // If the 'g' element corresponds to the graph, set the Graphviz ID also on
    // the top level 'svg' element.
    if (grandparent_element().elem_type == SVG::SVGElementType::Svg) {
      grandparent_element().graphviz_id = text;
    }
  }
}

void SVGAnalyzer::set_text_anchor(std::string_view text_anchor) {
  current_element().attributes.text_anchor = text_anchor;
}

void SVGAnalyzer::set_width(double width) {
  current_element().attributes.width = width;
}

void SVGAnalyzer::set_x(double x) { current_element().attributes.x = x; }

void SVGAnalyzer::set_y(double y) { current_element().attributes.y = y; }

void SVGAnalyzer::add_bboxes() {
  for (auto &graph : m_graphs) {
    graph.add_bboxes();
  }
}

void SVGAnalyzer::add_node_edge_outline_bbox_overlaps(double tolerance) {
  for (auto &graph : m_graphs) {
    graph.add_node_edge_outline_bbox_overlaps(tolerance);
  }
}

void SVGAnalyzer::add_outline_bboxes() {
  for (auto &graph : m_graphs) {
    graph.add_outline_bboxes();
  }
}

SVGAnalyzer SVGAnalyzer::make_from_dot(const std::string &dot_source,
                                       const std::string &engine) {
  auto g = CGraph::AGraph{dot_source};

  const auto demand_loading = false;
  auto gvc =
      std::make_shared<GVC::GVContext>(lt_preloaded_symbols, demand_loading);

  const auto layout = GVC::GVLayout(gvc, std::move(g), engine);

  const auto result = layout.render("svg");

  auto svg_analyzer = SVGAnalyzer{result.c_str()};

  svg_analyzer.set_graphviz_version(gvc->version());
  svg_analyzer.set_graphviz_build_date(gvc->buildDate());

  return svg_analyzer;
}

std::string_view SVGAnalyzer::original_svg() const { return m_original_svg; }

void SVGAnalyzer::set_transform(double a, double b, double c, double d,
                                double e, double f) {
  current_element().attributes.transform = {a, b, c, d, e, f};
}

void SVGAnalyzer::set_viewBox(double x, double y, double width, double height) {
  current_element().attributes.viewBox = {x, y, width, height};
}

void SVGAnalyzer::set_graphviz_version(std::string_view version) {
  m_svg.graphviz_version = version;
}

void SVGAnalyzer::set_graphviz_build_date(std::string_view build_date) {
  m_svg.graphviz_build_date = build_date;
}

std::string SVGAnalyzer::svg_string(std::size_t indent_size) const {
  return m_svg.to_string(indent_size);
}