| 1 |
#pragma once |
| 2 |
|
| 3 |
#include <cstddef> |
| 4 |
#include <string> |
| 5 |
#include <string_view> |
| 6 |
#include <vector> |
| 7 |
|
| 8 |
#include "graphviz_graph.h" |
| 9 |
#include "svg_analyzer_interface.h" |
| 10 |
#include "svg_element.h" |
| 11 |
|
| 12 |
/** |
| 13 |
* @brief The SVGAnalyzer class analyzes the contents of an SVG document and |
| 14 |
* identifies Graphviz graphs, nodes and edges. |
| 15 |
*/ |
| 16 |
|
| 17 |
class SVGAnalyzer : public ISVGAnalyzer { |
| 18 |
public: |
| 19 |
SVGAnalyzer(char *text); |
| 20 |
/// Return a non-mutable reference to the list of Graphviz graphs |
| 21 |
const std::vector<GraphvizGraph> &graphs() const; |
| 22 |
void on_enter_element_svg() override; |
| 23 |
void on_enter_element_g() override; |
| 24 |
void on_enter_element_circle() override; |
| 25 |
void on_enter_element_ellipse() override; |
| 26 |
void on_enter_element_line() override; |
| 27 |
void on_enter_element_path() override; |
| 28 |
void on_enter_element_polygon() override; |
| 29 |
void on_enter_element_polyline() override; |
| 30 |
void on_enter_element_rect() override; |
| 31 |
void on_enter_element_text() override; |
| 32 |
void on_enter_element_title() override; |
| 33 |
void on_exit_element() override; |
| 34 |
void path_cubic_bezier_to(double x1, double y1, double x2, double y2, |
| 35 |
double x, double y) override; |
| 36 |
void path_move_to(double x, double y) override; |
| 37 |
void path_exit() override; |
| 38 |
void set_cx(double cx) override; |
| 39 |
void set_cy(double cy) override; |
| 40 |
void set_font_family(std::string_view font_family) override; |
| 41 |
void set_font_size(double font_size) override; |
| 42 |
void set_fill(std::string_view fill) override; |
| 43 |
void set_fill_opacity(double fill_opacity) override; |
| 44 |
void set_height(double height) override; |
| 45 |
void set_id(std::string_view id) override; |
| 46 |
void set_rx(double rx) override; |
| 47 |
void set_ry(double ry) override; |
| 48 |
void set_class(std::string_view) override; |
| 49 |
void set_stroke(std::string_view stroke) override; |
| 50 |
void set_stroke_opacity(double stroke_opacity) override; |
| 51 |
void set_stroke_width(double stroke_width) override; |
| 52 |
void set_point(std::pair<double, double> pt) override; |
| 53 |
void set_text(std::string_view text) override; |
| 54 |
void set_text_anchor(std::string_view text_anchor) override; |
| 55 |
void set_transform(double a, double b, double c, double d, double e, |
| 56 |
double f) override; |
| 57 |
void set_viewBox(double x, double y, double width, double height) override; |
| 58 |
void set_width(double width) override; |
| 59 |
void set_x(double x) override; |
| 60 |
void set_y(double y) override; |
| 61 |
/// Add SVG `rect` elements representing the bounding boxes of nodes and edges |
| 62 |
/// to the corresponding `g` elements |
| 63 |
void add_bboxes(); |
| 64 |
/// Add SVG rects showing the overlap between the outline bounding boxes of |
| 65 |
/// each node and edge (with penwidth taken into account) |
| 66 |
void add_node_edge_outline_bbox_overlaps(double tolerance = 0); |
| 67 |
/// Add SVG `rect` elements representing the outline bounding boxes of nodes |
| 68 |
/// and edges to the corresponding `g` elements. The outline bounding box is |
| 69 |
/// the bounding box with penwidth taken into account. |
| 70 |
void add_outline_bboxes(); |
| 71 |
/// Create an SVGAnalyzer from DOT source using the `engine` layout engine. |
| 72 |
/// |
| 73 |
/// \param dot_source The DOT source |
| 74 |
/// \param engine The Graphviz layout engine |
| 75 |
static SVGAnalyzer make_from_dot(const std::string &dot_source, |
| 76 |
const std::string &engine = "dot"); |
| 77 |
std::size_t num_svgs() const { return m_num_svgs; }; |
| 78 |
std::size_t num_groups() const { return m_num_groups; }; |
| 79 |
std::size_t num_circles() const { return m_num_circles; }; |
| 80 |
std::size_t num_ellipses() const { return m_num_ellipses; }; |
| 81 |
std::size_t num_lines() const { return m_num_lines; }; |
| 82 |
std::size_t num_paths() const { return m_num_paths; }; |
| 83 |
std::size_t num_polygons() const { return m_num_polygons; }; |
| 84 |
std::size_t num_polylines() const { return m_num_polylines; }; |
| 85 |
std::size_t num_rects() const { return m_num_rects; }; |
| 86 |
std::size_t num_texts() const { return m_num_texts; }; |
| 87 |
std::size_t num_titles() const { return m_num_titles; }; |
| 88 |
/// Return a view of the original SVG |
| 89 |
std::string_view original_svg() const; |
| 90 |
/// Re-create the SVG from the internal data structure and verify it against |
| 91 |
/// the original SVG produced by Graphviz |
| 92 |
void re_create_and_verify_svg(); |
| 93 |
void set_graphviz_version(std::string_view version); |
| 94 |
void set_graphviz_build_date(std::string_view build_date); |
| 95 |
std::string svg_string(std::size_t indent_size = 2) const; |
| 96 |
|
| 97 |
private: |
| 98 |
/// Get the current element being processed by the SVG document traverser |
| 99 |
SVG::SVGElement ¤t_element(); |
| 100 |
/// Enter a new SVG element retrieved by the SVG document traverser into the |
| 101 |
/// list of elements currently being processed |
| 102 |
void enter_element(SVG::SVGElementType type); |
| 103 |
/// Get the grandparent element of the current element being processed by the |
| 104 |
/// SVG document traverser |
| 105 |
SVG::SVGElement &grandparent_element(); |
| 106 |
/// Get the parent element of the current element being processed by the SVG |
| 107 |
/// document traverser |
| 108 |
SVG::SVGElement &parent_element(); |
| 109 |
/// Traverses the processed SVG element hierarchy, identifies nodes and edges, |
| 110 |
/// creates objects representing them and stores them in lists |
| 111 |
void retrieve_graphviz_components(); |
| 112 |
void retrieve_graphviz_components_impl(SVG::SVGElement &parent_svg_element); |
| 113 |
|
| 114 |
/// A list of pointers to elements currently being processed by the SVG++ |
| 115 |
/// document traverser, in hierarchical order. The front element is the top |
| 116 |
/// level SVG and the back element is the current element. |
| 117 |
std::vector<SVG::SVGElement *> m_elements_in_process; |
| 118 |
std::size_t m_num_svgs = 1; // the top level svg is implicit. See |
| 119 |
// https://github.com/svgpp/svgpp/issues/98 |
| 120 |
std::size_t m_num_groups = 0; |
| 121 |
std::size_t m_num_circles = 0; |
| 122 |
std::size_t m_num_ellipses = 0; |
| 123 |
std::size_t m_num_lines = 0; |
| 124 |
std::size_t m_num_paths = 0; |
| 125 |
std::size_t m_num_polygons = 0; |
| 126 |
std::size_t m_num_polylines = 0; |
| 127 |
std::size_t m_num_rects = 0; |
| 128 |
std::size_t m_num_texts = 0; |
| 129 |
std::size_t m_num_titles = 0; |
| 130 |
/// A list of Graphviz recreated graphs |
| 131 |
std::vector<GraphvizGraph> m_graphs; |
| 132 |
/// The original SVG document |
| 133 |
std::string m_original_svg; |
| 134 |
/// The top level SVG `svg` element corresponding to the Graphviz graph |
| 135 |
SVG::SVGElement m_svg; |
| 136 |
}; |