]> granicus.if.org Git - graphviz/commitdiff
tests: SVGAnalyzer: add support for retrieving the Graphviz edge penwidth
authorMagnus Jacobsson <Magnus.Jacobsson@berotec.se>
Tue, 23 Aug 2022 08:26:24 +0000 (10:26 +0200)
committerMagnus Jacobsson <Magnus.Jacobsson@berotec.se>
Mon, 5 Sep 2022 06:39:39 +0000 (08:39 +0200)
tests/CMakeLists.txt
tests/graphviz_edge.cpp
tests/graphviz_edge.h
tests/test_edge_penwidth.cpp [new file with mode: 0644]
tests/test_utilities.cpp
tests/test_utilities.h

index bf134144218553638fa1b62caeccf608c6693c01..702ed9db30491238d4776a9443e1759576068289 100644 (file)
@@ -78,6 +78,7 @@ endmacro()
 
 CREATE_TEST(AGraph_construction)
 CREATE_TEST(clusters)
+CREATE_TEST(edge_penwidth)
 CREATE_TEST(engines)
 CREATE_TEST(GVContext_construction)
 CREATE_TEST(GVContext_render_svg)
index 332b2504b76ec8230ab9c54e8e20c36441298273..893580b9df62db0d1531e3945923e4fbbe47cf95 100644 (file)
@@ -12,3 +12,8 @@ const SVG::SVGElement &GraphvizEdge::svg_g_element() const {
 SVG::SVGRect GraphvizEdge::bbox() const { return m_svg_g_element.bbox(); }
 
 SVG::SVGPoint GraphvizEdge::center() const { return bbox().center(); }
+
+double GraphvizEdge::penwidth() const {
+  return m_svg_g_element.attribute_from_subtree<double>(
+      &SVG::SVGAttributes::stroke_width, &SVG::SVGElement::is_shape_element, 1);
+}
index eeeef8ba5efc35eceb11241a083f17e65e314e17..fdc0a0d9d42c0c78862d7f1987893ddd4672a9d2 100644 (file)
@@ -22,6 +22,8 @@ public:
   /// Return the 'edgeop' according to the DOT language specification. Note that
   /// this is not the same as the 'id' attribute of an edge.
   std::string_view edgeop() const;
+  /// Return the edge's `penwidth` attribute
+  double penwidth() const;
   /// Return a non-mutable reference to the SVG `g` element corresponding to the
   /// edge
   const SVG::SVGElement &svg_g_element() const;
diff --git a/tests/test_edge_penwidth.cpp b/tests/test_edge_penwidth.cpp
new file mode 100644 (file)
index 0000000..e37ce8c
--- /dev/null
@@ -0,0 +1,30 @@
+#include <catch2/catch.hpp>
+#include <fmt/format.h>
+
+#include "svg_analyzer.h"
+#include "test_utilities.h"
+
+TEST_CASE("Edge penwidth",
+          "Test that the Graphviz 'penwidth' attribute is used to set the "
+          "'stroke-width' attribute correctly for edges in the generated SVG") {
+
+  const auto primitive_arrow_shape =
+      GENERATE(from_range(all_primitive_arrow_shapes));
+  INFO(fmt::format("Primitive arrow shape: {}", primitive_arrow_shape));
+
+  const auto edge_penwidth = GENERATE(0.5, 1.0, 2.0);
+  INFO(fmt::format("Edge penwidth: {}", edge_penwidth));
+
+  auto dot =
+      fmt::format("digraph g1 {{edge [arrowhead={} penwidth={}]; a -> b}}",
+                  primitive_arrow_shape, edge_penwidth);
+
+  const auto engine = "dot";
+  auto svg_analyzer = SVGAnalyzer::make_from_dot(dot, engine);
+
+  for (const auto &graph : svg_analyzer.graphs()) {
+    for (const auto &edge : graph.edges()) {
+      CHECK(edge.penwidth() == edge_penwidth);
+    }
+  }
+}
index c9c9dbfc65ecce0846d21cc369577917bbcbcc45..8f0c621f2a93962a5d3b87f5e7138e45d7589bd9 100644 (file)
@@ -154,5 +154,15 @@ bool contains_ellipse_shape(std::string_view shape) {
          node_shapes_consisting_of_ellipse_and_polyline.contains(shape);
 }
 
+const std::unordered_set<std::string_view> primitive_polygon_arrow_shapes = {
+    "crow", "diamond", "inv", "normal", "vee"};
+
+const std::unordered_set<std::string_view>
+    primitive_polygon_and_polyline_arrow_shapes = {"box", "tee"};
+
+const std::unordered_set<std::string_view> all_primitive_arrow_shapes = {
+    "box", "crow", "curve",  "diamond", "dot", "icurve",
+    "inv", "none", "normal", "tee",     "vee"};
+
 const std::unordered_set<std::string_view> all_rank_directions = {"TB", "BT",
                                                                   "LR", "RL"};
index bc78525e3cbf5956497c7cb319077bf9e3ccdaa7..9bab6cac294c9407b38e71af6658e91469923377 100644 (file)
@@ -21,5 +21,12 @@ extern const std::unordered_set<std::string_view> node_shapes_without_svg_shape;
 bool contains_ellipse_shape(std::string_view shape);
 bool contains_polygon_shape(std::string_view shape);
 
+/// arrow shapes
+extern const std::unordered_set<std::string_view>
+    primitive_polygon_arrow_shapes;
+extern const std::unordered_set<std::string_view>
+    primitive_polygon_and_polyline_arrow_shapes;
+extern const std::unordered_set<std::string_view> all_primitive_arrow_shapes;
+
 /// rank directions
 extern const std::unordered_set<std::string_view> all_rank_directions;