From: Magnus Jacobsson Date: Sat, 27 Aug 2022 11:13:59 +0000 (+0200) Subject: tests: SVGAnalyzer: add support for adding outline bounding boxes to the re-created SVG X-Git-Tag: 6.0.2~43^2~2 X-Git-Url: https://granicus.if.org/sourcecode?a=commitdiff_plain;h=06d1ece9bd190164b6db7e07e4ef89b6c4a8db6a;p=graphviz tests: SVGAnalyzer: add support for adding outline bounding boxes to the re-created SVG This will be used in upcoming commits to highlight Graphviz component boundaries. Towards https://gitlab.com/graphviz/graphviz/-/issues/372. --- diff --git a/tests/graphviz_edge.cpp b/tests/graphviz_edge.cpp index b32f3495f..40ffd8e9e 100644 --- a/tests/graphviz_edge.cpp +++ b/tests/graphviz_edge.cpp @@ -8,6 +8,8 @@ GraphvizEdge::GraphvizEdge(SVG::SVGElement &svg_g_element) void GraphvizEdge::add_bbox() { m_svg_g_element.add_bbox(); } +void GraphvizEdge::add_outline_bbox() { m_svg_g_element.add_outline_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 c3845719b..aec812438 100644 --- a/tests/graphviz_edge.h +++ b/tests/graphviz_edge.h @@ -18,6 +18,10 @@ public: /// 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(); + /// Add an SVG `rect` element to the edge's corresponding `g` element. The + /// `rect` represents the outline bounding box of the edge. The outline + /// bounding box is the bounding box with penwidth taken into account. + void add_outline_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 732c4cf26..9b68e1bed 100644 --- a/tests/graphviz_graph.cpp +++ b/tests/graphviz_graph.cpp @@ -55,3 +55,12 @@ void GraphvizGraph::add_bboxes() { edge.add_bbox(); } } + +void GraphvizGraph::add_outline_bboxes() { + for (auto &node : m_nodes) { + node.add_outline_bbox(); + } + for (auto &edge : m_edges) { + edge.add_outline_bbox(); + } +} diff --git a/tests/graphviz_graph.h b/tests/graphviz_graph.h index 78902c51a..d5d771511 100644 --- a/tests/graphviz_graph.h +++ b/tests/graphviz_graph.h @@ -24,6 +24,10 @@ public: void add_edge(SVG::SVGElement &svg_g_element); /// Add a Graphviz node to the graph void add_node(SVG::SVGElement &svg_g_element); + /// Add SVG `rect` elements representing the outline bounding boxes of nodes + /// and edges to the corresponding `g` elements. The outline bounding box is + /// the bounding box with penwidth taken into account. + void add_outline_bboxes(); const GraphvizEdge &edge(std::string_view edgeop) const; /// Return a non-mutable reference to the list of Graphviz edges const std::vector &edges() const; diff --git a/tests/graphviz_node.cpp b/tests/graphviz_node.cpp index 209439cec..33872260f 100644 --- a/tests/graphviz_node.cpp +++ b/tests/graphviz_node.cpp @@ -8,6 +8,8 @@ GraphvizNode::GraphvizNode(SVG::SVGElement &svg_element) void GraphvizNode::add_bbox() { m_svg_g_element.add_bbox(); } +void GraphvizNode::add_outline_bbox() { m_svg_g_element.add_outline_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 cb3f89386..9713dd3ef 100644 --- a/tests/graphviz_node.h +++ b/tests/graphviz_node.h @@ -18,6 +18,10 @@ public: /// Add an SVG `rect` element representing the bounding box of the node to the /// corresponding `g` element void add_bbox(); + /// Add an SVG `rect` element representing the outline bounding box of the + /// node to the corresponding `g` element. The outline bounding box is the + /// bounding box with penwidth taken into account. + void add_outline_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 5f3659744..ab3182584 100644 --- a/tests/svg_analyzer.cpp +++ b/tests/svg_analyzer.cpp @@ -289,6 +289,12 @@ void SVGAnalyzer::add_bboxes() { } } +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}; diff --git a/tests/svg_analyzer.h b/tests/svg_analyzer.h index eb5fa20bc..72c745734 100644 --- a/tests/svg_analyzer.h +++ b/tests/svg_analyzer.h @@ -61,6 +61,10 @@ public: /// Add SVG `rect` elements representing the bounding boxes of nodes and edges /// to the corresponding `g` elements void add_bboxes(); + /// Add SVG `rect` elements representing the outline bounding boxes of nodes + /// and edges to the corresponding `g` elements. The outline bounding box is + /// the bounding box with penwidth taken into account. + void add_outline_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 90769b63d..eb7d3d31f 100644 --- a/tests/svg_element.cpp +++ b/tests/svg_element.cpp @@ -126,6 +126,11 @@ void SVG::SVGElement::add_rect(SVGRect rect, const std::string color) { children.push_back(element); } +void SVG::SVGElement::add_outline_bbox() { + const auto bbox = SVGElement::outline_bbox(); + add_rect(bbox, "blue"); +} + SVG::SVGRect SVG::SVGElement::bbox(bool throw_if_bbox_not_defined) { if (!m_bbox.has_value()) { // negative width and height bbox that will be immediately replaced by the diff --git a/tests/svg_element.h b/tests/svg_element.h index 5c48850d2..1640995a8 100644 --- a/tests/svg_element.h +++ b/tests/svg_element.h @@ -101,6 +101,10 @@ public: /// Add an SVG `rect` element representing the bounding box of the edge to the /// element void add_bbox(); + /// Add an SVG `rect` element representing the outline bounding box of the + /// edge to the element. The outline bounding box is the bounding box with + /// stroke width taken into account. + void add_outline_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