]> granicus.if.org Git - graphviz/commitdiff
tests: SVGAnalyzer: add support for adding bounding boxes to the re-created SVG
authorMagnus Jacobsson <Magnus.Jacobsson@berotec.se>
Sat, 27 Aug 2022 11:13:59 +0000 (13:13 +0200)
committerMagnus Jacobsson <Magnus.Jacobsson@berotec.se>
Mon, 12 Sep 2022 07:46:17 +0000 (09:46 +0200)
This will be used in upcoming commits to highlight Graphviz component
boundaries and overlaps.

Towards https://gitlab.com/graphviz/graphviz/-/issues/372.

tests/graphviz_edge.cpp
tests/graphviz_edge.h
tests/graphviz_graph.cpp
tests/graphviz_graph.h
tests/graphviz_node.cpp
tests/graphviz_node.h
tests/svg_analyzer.cpp
tests/svg_analyzer.h
tests/svg_element.cpp
tests/svg_element.h

index f222e0b4df442daf68a6e68b7bc5261d63218ea0..b32f3495ff48459abd14cc2e73dd12911ccdc27b 100644 (file)
@@ -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 {
index 470f352a40bf1b7c21b7c4ecf76fbc6f1d9eddb7..c3845719b7074f2aed2b4913325f5c974d91f0bd 100644 (file)
@@ -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
index 54ca7a665d6e0bf4939269641b51aa15b0d44608..732c4cf26885af69c84037da760b56620440b52f 100644 (file)
@@ -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();
+  }
+}
index f1433a8b5f8ac769b8ecd3d749d9dbd05a866747..78902c51a76a15731dfa974287c241d776328a94 100644 (file)
@@ -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
index e563736b62c6870e8191455d2c7613afe8bd656a..209439cecb018108c66666ec6769f7dcbe1a4884 100644 (file)
@@ -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 {
index 097ce281ed210732caedef31b49401c8a9cef162..cb3f8938624266b00dcecb4b7280a5db23004e47 100644 (file)
@@ -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
index f6f2df93fccdcef22a8263d9b672b7443136fe1a..5f365974489e68a6a39e2e8987be9923a7539f08 100644 (file)
@@ -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};
index e2cd2e3f15a7a31a67e2c30f08442cf56018d94b..eb5fa20bc652a204e33126bb2e9c1c014e07dd45 100644 (file)
@@ -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
index b3cf3a9949d821dddf84bb929997e26351cd9bc9..90769b63dbbc8afa13362a81eadcb36f1923f25d 100644 (file)
@@ -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;
index c76952121af752b7149c7d58c740c639cf10c407..5c48850d219e0437c122272e024f678a6bc639b0 100644 (file)
@@ -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