]> granicus.if.org Git - graphviz/commitdiff
tests: SvgAnalyzer: add handling of the 'points' attribute
authorMagnus Jacobsson <Magnus.Jacobsson@berotec.se>
Wed, 27 Jul 2022 14:17:28 +0000 (16:17 +0200)
committerMagnus Jacobsson <Magnus.Jacobsson@berotec.se>
Tue, 16 Aug 2022 10:21:45 +0000 (12:21 +0200)
tests/svg_analyzer.cpp
tests/svg_analyzer.h
tests/svg_analyzer_interface.h
tests/svg_element.cpp
tests/svg_element.h
tests/svgpp_context.cpp
tests/svgpp_context.h
tests/svgpp_document_traverser.cpp
tests/test_svg_analyzer.cpp

index 501171ec27ee53518357d498ad6822ad5e141471..3c004959bfa09b99334eeab2857aa43ded14f7ff 100644 (file)
@@ -131,6 +131,10 @@ void SVGAnalyzer::set_id(std::string_view id) {
   current_element().attributes.id = id;
 }
 
+void SVGAnalyzer::set_point(std::pair<double, double> point) {
+  current_element().attributes.points.emplace_back(point.first, point.second);
+}
+
 void SVGAnalyzer::set_text(std::string_view text) {
   auto &element = current_element();
   element.text = text;
index 212fe66a629cd877a4e240919123d6dbd4d9b5f1..a21fec1aff80297f435940ec1cfd38335de47f82 100644 (file)
@@ -32,6 +32,7 @@ public:
   void set_id(std::string_view id) override;
   void set_class(std::string_view) override;
   void set_stroke(std::string_view stroke) override;
+  void set_point(std::pair<double, double> point) override;
   void set_text(std::string_view text) override;
   void set_transform(double a, double b, double c, double d, double e,
                      double f) override;
index 086b9ff1dd36c9117a31af7b3f4751272f4a36e3..907c2112a7ea0ffce3407a788ae742cb38f45ea9 100644 (file)
@@ -30,6 +30,7 @@ public:
   virtual void set_fill(std::string_view fill) = 0;
   virtual void set_height(double height) = 0;
   virtual void set_id(std::string_view id) = 0;
+  virtual void set_point(std::pair<double, double> point) = 0;
   virtual void set_stroke(std::string_view stroke) = 0;
   virtual void set_text(std::string_view text) = 0;
   virtual void set_transform(double a, double b, double c, double d, double e,
index 2d1702331d40e192cdd0db935cbc32ed51ea5f93..3a8084abc650437e21a0918111ce8d5d42901f0e 100644 (file)
@@ -82,6 +82,18 @@ std::string SVG::SVGElement::id_attribute_to_string() const {
   return fmt::format(R"(id="{}")", attributes.id);
 }
 
+std::string SVG::SVGElement::points_attribute_to_string() const {
+  std::string points_attribute_str = R"|(points=")|";
+  const char *separator = "";
+  for (const auto &point : attributes.points) {
+    points_attribute_str += separator + fmt::format("{},{}", point.x, point.y);
+    separator = " ";
+  }
+  points_attribute_str += '"';
+
+  return points_attribute_str;
+}
+
 std::string SVG::SVGElement::stroke_attribute_to_string() const {
   if (attributes.stroke.empty()) {
     return "";
@@ -148,10 +160,12 @@ void SVG::SVGElement::to_string_impl(std::string &output,
   case SVG::SVGElementType::Polygon:
     append_attribute(attributes_str, fill_attribute_to_string());
     append_attribute(attributes_str, stroke_attribute_to_string());
+    append_attribute(attributes_str, points_attribute_to_string());
     break;
   case SVG::SVGElementType::Polyline:
     append_attribute(attributes_str, fill_attribute_to_string());
     append_attribute(attributes_str, stroke_attribute_to_string());
+    append_attribute(attributes_str, points_attribute_to_string());
     break;
   case SVG::SVGElementType::Svg:
     attributes_str += fmt::format(
index 4e701c52799152ff57e1665e13a1327adfe73442..a2301421203f102ce52e6400db83c67592bde982 100644 (file)
@@ -8,6 +8,11 @@
 
 namespace SVG {
 
+struct SVGPoint {
+  double x;
+  double y;
+};
+
 struct SVGRect {
   double x;
   double y;
@@ -45,6 +50,7 @@ struct SVGAttributes {
   std::string fill;
   double height;
   std::string id;
+  std::vector<SVGPoint> points;
   std::string stroke;
   std::optional<SVGMatrix> transform;
   SVGRect viewBox;
@@ -85,6 +91,7 @@ private:
                         const std::string &attribute) const;
   std::string id_attribute_to_string() const;
   std::string fill_attribute_to_string() const;
+  std::string points_attribute_to_string() const;
   std::string stroke_attribute_to_string() const;
   std::string stroke_to_graphviz_color(const std::string &color) const;
   void to_string_impl(std::string &output, std::size_t indent_size,
index e75a7cf816ea42b8e663ba51e598d19f0d71a3cb..6b17f1d09843085f776c63b3f06a20efd4ab87e6 100644 (file)
@@ -235,11 +235,11 @@ void SvgppContext::set(svgpp::tag::attribute::viewBox, const double v1,
   m_svgAnalyzer->set_viewBox(v1, v2, v3, v4);
 }
 
-void SvgppContext::set_impl(svgpp::tag::attribute::points &points,
-                            const std::any &range) {
-  (void)points;
-  (void)range;
-  // ignore for now
+void SvgppContext::set(svgpp::tag::attribute::points,
+                       const SvgppContext::PointsRange &range) {
+  for (auto &it : range) {
+    m_svgAnalyzer->set_point(it);
+  }
 }
 
 void SvgppContext::set_text(boost::iterator_range<const char *> v) {
index 35b88d275e63077822e58393affe486262bb9f65..cfbbfb60ab7ac55f0d3f551e0d43fc47c86ac351 100644 (file)
@@ -1,8 +1,8 @@
 #pragma once
 
-#include <any>
 #include <stdexcept>
 
+#include <boost/range/any_range.hpp>
 #include <boost/range/iterator_range_core.hpp>
 #include <svgpp/svgpp.hpp>
 
@@ -157,10 +157,11 @@ public:
   void set(svgpp::tag::attribute::y1 y1, double v);
   void set(svgpp::tag::attribute::x2 x2, double v);
   void set(svgpp::tag::attribute::y2 y2, double v);
-  template <typename Range>
-  void set(svgpp::tag::attribute::points points, const Range &range) {
-    set_impl(points, range);
-  }
+  typedef boost::any_range<std::pair<double, double>,
+                           boost::single_pass_traversal_tag,
+                           const std::pair<double, double> &, std::ptrdiff_t>
+      PointsRange;
+  void set(svgpp::tag::attribute::points points, const PointsRange &range);
   void set(svgpp::tag::attribute::x a, double v);
   void set(svgpp::tag::attribute::y y, double v);
   void set(svgpp::tag::attribute::width width, double v);
@@ -173,7 +174,5 @@ public:
   void set_text(boost::iterator_range<const char *> v);
 
 private:
-  void set_impl(svgpp::tag::attribute::points &points, const std::any &range);
-
   ISVGAnalyzer *m_svgAnalyzer = nullptr;
 };
index 3894ded48c21ce5d8012555465aa28933527ac96..9dfed9dfb50e39a87e5372829f75010ec8863ca0 100644 (file)
@@ -30,6 +30,7 @@ void traverseDocumentWithSvgpp(SvgppContext &context, char *text) {
                         svgpp::tag::attribute::fill,      //
                         svgpp::tag::attribute::height,    //
                         svgpp::tag::attribute::id,        //
+                        svgpp::tag::attribute::points,    //
                         svgpp::tag::attribute::stroke,    //
                         svgpp::tag::attribute::transform, //
                         svgpp::tag::attribute::viewBox,   //
index 32197d7ea69f7fe9caa3b29355b5dd2b00ca5db3..cbb1d2a68204b0c2e52c5797da343b448228eae6 100644 (file)
@@ -123,10 +123,20 @@ TEST_CASE(
     boost::split(recreated_svg_lines, recreated_svg, boost::is_any_of("\n"));
     for (std::size_t i = 0; i < original_svg_lines.size(); i++) {
       REQUIRE(i < recreated_svg_lines.size());
+      if (recreated_svg_lines[i].starts_with("<ellipse fill=\"")) {
+        // stop comparison here for ellipse based node shapes since we do not
+        // yet handle all attributes on the 'ellipse' element
+        break;
+      }
       if (recreated_svg_lines[i] ==
-          "<polygon fill=\"white\" stroke=\"none\"/>") {
-        // stop comparison here since we do not yet handle all attributes on the
-        // 'polygon' element
+          ("<path fill=\"none\" stroke=\"black\"/>")) {
+        // stop comparison here for the 'cylinder' node shape since we do not
+        // yet handle all attributes on the 'path' element
+        break;
+      }
+      if (recreated_svg_lines[i] == "<text>a</text>") {
+        // stop comparison here for polygon based shapes since we do not yet
+        // handle all attributes on the 'text' element
         break;
       }
       REQUIRE(recreated_svg_lines[i] == original_svg_lines[i]);
@@ -141,8 +151,6 @@ TEST_CASE(
       CHECK(recreated_svg.find("<text>a</text>") != std::string::npos);
       CHECK(recreated_svg.find("<text>b</text>") != std::string::npos);
     }
-    CHECK(recreated_svg.find("<polygon fill=\"white\" stroke=\"none\"/>") !=
-          std::string::npos);
     CHECK(recreated_svg.find("<path fill=\"none\" stroke=\"black\"/>") !=
           std::string::npos);
     CHECK(recreated_svg.find("<!-- a -->") != std::string::npos);