From: Magnus Jacobsson Date: Sat, 27 Aug 2022 11:13:59 +0000 (+0200) Subject: tests: SVGAnalyzer: add support for adding bounding boxes to the re-created SVG X-Git-Tag: 6.0.2~43^2~3 X-Git-Url: https://granicus.if.org/sourcecode?a=commitdiff_plain;h=b2508fd8ef2cbe4864f150be9b04fb82883f1298;p=graphviz tests: SVGAnalyzer: add support for adding bounding boxes to the re-created SVG This will be used in upcoming commits to highlight Graphviz component boundaries and overlaps. Towards https://gitlab.com/graphviz/graphviz/-/issues/372. --- diff --git a/tests/graphviz_edge.cpp b/tests/graphviz_edge.cpp index f222e0b4d..b32f3495f 100644 --- a/tests/graphviz_edge.cpp +++ b/tests/graphviz_edge.cpp @@ -6,6 +6,8 @@ GraphvizEdge::GraphvizEdge(SVG::SVGElement &svg_g_element) : m_edgeop(svg_g_element.graphviz_id), m_svg_g_element(svg_g_element) {} +void GraphvizEdge::add_bbox() { m_svg_g_element.add_bbox(); } + std::string_view GraphvizEdge::edgeop() const { return m_edgeop; } std::string GraphvizEdge::fillcolor() const { diff --git a/tests/graphviz_edge.h b/tests/graphviz_edge.h index 470f352a4..c3845719b 100644 --- a/tests/graphviz_edge.h +++ b/tests/graphviz_edge.h @@ -15,6 +15,9 @@ public: GraphvizEdge() = delete; explicit GraphvizEdge(SVG::SVGElement &svg_g_element); + /// Add an SVG `rect` element to the edge's corresponding `g` element. The + /// `rect` is represes the bounding box of the edge. + void add_bbox(); /// Return the bounding box of the edge SVG::SVGRect bbox() const; /// Return the center of the edge's bounding box diff --git a/tests/graphviz_graph.cpp b/tests/graphviz_graph.cpp index 54ca7a665..732c4cf26 100644 --- a/tests/graphviz_graph.cpp +++ b/tests/graphviz_graph.cpp @@ -46,3 +46,12 @@ const GraphvizEdge &GraphvizGraph::edge(std::string_view edgeop) const { throw std::runtime_error{ fmt::format("Unknown edge '{}' in graph '{}'", edgeop, m_graph_id)}; } + +void GraphvizGraph::add_bboxes() { + for (auto &node : m_nodes) { + node.add_bbox(); + } + for (auto &edge : m_edges) { + edge.add_bbox(); + } +} diff --git a/tests/graphviz_graph.h b/tests/graphviz_graph.h index f1433a8b5..78902c51a 100644 --- a/tests/graphviz_graph.h +++ b/tests/graphviz_graph.h @@ -17,6 +17,9 @@ public: GraphvizGraph() = delete; explicit GraphvizGraph(SVG::SVGElement &g_element); + /// Add SVG `rect` elements representing the bounding boxes of nodes and edges + /// to the corresponding `g` elements + void add_bboxes(); /// Add a Graphviz edge to the graph void add_edge(SVG::SVGElement &svg_g_element); /// Add a Graphviz node to the graph diff --git a/tests/graphviz_node.cpp b/tests/graphviz_node.cpp index e563736b6..209439cec 100644 --- a/tests/graphviz_node.cpp +++ b/tests/graphviz_node.cpp @@ -6,6 +6,8 @@ GraphvizNode::GraphvizNode(SVG::SVGElement &svg_element) : m_node_id(svg_element.graphviz_id), m_svg_g_element(svg_element) {} +void GraphvizNode::add_bbox() { m_svg_g_element.add_bbox(); } + SVG::SVGPoint GraphvizNode::center() const { return bbox().center(); } std::string GraphvizNode::color() const { diff --git a/tests/graphviz_node.h b/tests/graphviz_node.h index 097ce281e..cb3f89386 100644 --- a/tests/graphviz_node.h +++ b/tests/graphviz_node.h @@ -15,6 +15,9 @@ public: GraphvizNode() = delete; explicit GraphvizNode(SVG::SVGElement &svg_element); + /// Add an SVG `rect` element representing the bounding box of the node to the + /// corresponding `g` element + void add_bbox(); /// Return the node's bounding box SVG::SVGRect bbox() const; /// Return the center of the node's bounding box diff --git a/tests/svg_analyzer.cpp b/tests/svg_analyzer.cpp index f6f2df93f..5f3659744 100644 --- a/tests/svg_analyzer.cpp +++ b/tests/svg_analyzer.cpp @@ -283,6 +283,12 @@ 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(); + } +} + SVGAnalyzer SVGAnalyzer::make_from_dot(const std::string &dot_source, const std::string &engine) { auto g = CGraph::AGraph{dot_source}; diff --git a/tests/svg_analyzer.h b/tests/svg_analyzer.h index e2cd2e3f1..eb5fa20bc 100644 --- a/tests/svg_analyzer.h +++ b/tests/svg_analyzer.h @@ -58,6 +58,9 @@ public: void set_width(double width) override; void set_x(double x) override; void set_y(double y) override; + /// Add SVG `rect` elements representing the bounding boxes of nodes and edges + /// to the corresponding `g` elements + void add_bboxes(); /// Create an SVGAnalyzer from DOT source using the `engine` layout engine. /// /// \param dot_source The DOT source diff --git a/tests/svg_element.cpp b/tests/svg_element.cpp index b3cf3a994..90769b63d 100644 --- a/tests/svg_element.cpp +++ b/tests/svg_element.cpp @@ -109,6 +109,11 @@ std::string SVG::to_dot_color(const std::string &color, double opacity) { return rgb_to_hex(color, opacity); } +void SVG::SVGElement::add_bbox() { + const auto bbox = SVGElement::bbox(); + add_rect(bbox, "green"); +} + void SVG::SVGElement::add_rect(SVGRect rect, const std::string color) { SVG::SVGElement element{SVG::SVGElementType::Rect}; element.attributes.x = rect.x; diff --git a/tests/svg_element.h b/tests/svg_element.h index c76952121..5c48850d2 100644 --- a/tests/svg_element.h +++ b/tests/svg_element.h @@ -98,6 +98,9 @@ public: SVGElement() = delete; explicit SVGElement(SVG::SVGElementType type); + /// Add an SVG `rect` element representing the bounding box of the edge to the + /// element + void add_bbox(); /// Add an SVG `rect` element as a child to the element void add_rect(SVG::SVGRect rect, std::string color); /// \brief Return the value of an attribute retrieved from the element and its