From: Magnus Jacobsson Date: Tue, 4 Oct 2022 12:30:12 +0000 (+0200) Subject: tests: test_edge_node_overlap_utilities: factor out GraphvizEdge::arrowhead_outline_b... X-Git-Tag: 7.0.0~10^2~7 X-Git-Url: https://granicus.if.org/sourcecode?a=commitdiff_plain;h=f9c2168f7750680582e7a02a4b310a6872eb6d89;p=graphviz tests: test_edge_node_overlap_utilities: factor out GraphvizEdge::arrowhead_outline_bbox() --- diff --git a/tests/graphviz_edge.cpp b/tests/graphviz_edge.cpp index 866e70ed5..980fd7ddb 100644 --- a/tests/graphviz_edge.cpp +++ b/tests/graphviz_edge.cpp @@ -1,4 +1,6 @@ +#include #include +#include #include "graphviz_edge.h" #include "graphviz_node.h" @@ -20,6 +22,28 @@ SVG::SVGElement &GraphvizEdge::arrow(const std::size_t index) const { return m_svg_g_element.find_child(SVG::SVGElementType::Polygon, index); } +static const std::unordered_set + supported_primitive_arrow_shapes = { + "inv", // + "normal", // +}; + +SVG::SVGRect GraphvizEdge::arrowhead_outline_bbox( + std::string_view dir, std::string_view primitive_arrow_shape) const { + assert((dir == "forward" || dir == "both") && + "no arrowhead for this edge direction"); + + if (!supported_primitive_arrow_shapes.contains(primitive_arrow_shape)) { + throw std::runtime_error{ + fmt::format("primitive arrow shape {} is not yet supported", + primitive_arrow_shape)}; + } + auto edge_arrowhead = dir == "forward" ? arrow(0) : arrow(1); + const auto edge_arrowhead_bbox = edge_arrowhead.outline_bbox(); + + return edge_arrowhead_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 e2fcde1d8..a9d57156e 100644 --- a/tests/graphviz_edge.h +++ b/tests/graphviz_edge.h @@ -33,6 +33,11 @@ public: /// arrowhead is at index 1. If there's only one, it's at index 0. Throws an /// exception if there's no arrow at the specified index. SVG::SVGElement &arrow(std::size_t index = 0) const; + /// Return the outline bounding box of the edge arrowhead. The outline + /// bounding box is the bounding box with penwidth taken into account. + SVG::SVGRect + arrowhead_outline_bbox(std::string_view dir, + std::string_view primitive_arrow_shape) const; /// Return the bounding box of the edge SVG::SVGRect bbox() const; /// Return the center of the edge's bounding box diff --git a/tests/test_edge_node_overlap_utilities.cpp b/tests/test_edge_node_overlap_utilities.cpp index e4349bb37..97b13102e 100644 --- a/tests/test_edge_node_overlap_utilities.cpp +++ b/tests/test_edge_node_overlap_utilities.cpp @@ -182,6 +182,8 @@ static bool check_analyzed_svg(SVGAnalyzer &svg_analyzer, const auto rankdir = graph_options.rankdir; const auto node_shape = graph_options.node_shape; const auto dir = graph_options.dir; + const auto primitive_arrowhead_shape = + graph_options.primitive_arrowhead_shape; REQUIRE(svg_analyzer.graphs().size() == 1); auto &recreated_graph = svg_analyzer.graphs().back(); @@ -266,8 +268,8 @@ static bool check_analyzed_svg(SVGAnalyzer &svg_analyzer, // check overlap of edge stem and arrowhead if (dir == "forward" || dir == "both") { - auto edge_arrowhead = dir == "forward" ? edge.arrow(0) : edge.arrow(1); - const auto edge_arrowhead_bbox = edge_arrowhead.outline_bbox(); + const auto edge_arrowhead_bbox = + edge.arrowhead_outline_bbox(dir, primitive_arrowhead_shape); const auto overlap_bbox = edge_stem_bbox.intersection(edge_arrowhead_bbox); INFO("Edge stem and arrowhead overlap:"); INFO(fmt::format(" width: {:.3f}", overlap_bbox.width));