]> granicus.if.org Git - graphviz/commitdiff
tests: SvgAnalyzer: add handling of the 'transform' attribute
authorMagnus Jacobsson <Magnus.Jacobsson@berotec.se>
Wed, 27 Jul 2022 10:40:42 +0000 (12:40 +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 cd1e88a99450ed5f83d22b7212429a5cbeb601ce..339a88d019016ed93f79f71285e628595c4c8e3d 100644 (file)
@@ -147,6 +147,11 @@ void SVGAnalyzer::set_width(double width) {
   current_element().attributes.width = width;
 }
 
+void SVGAnalyzer::set_transform(double a, double b, double c, double d,
+                                double e, double f) {
+  current_element().attributes.transform = {a, b, c, d, e, f};
+}
+
 void SVGAnalyzer::set_viewBox(double x, double y, double width, double height) {
   current_element().attributes.viewBox = {x, y, width, height};
 }
index 26c6bd633fe55f8223b6113ee7e54da6576b3814..5a2e777701bff05110f9c5eaa7430003dc4d7f2e 100644 (file)
@@ -31,6 +31,8 @@ public:
   void set_id(std::string_view id) override;
   void set_class(std::string_view) 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;
   void set_viewBox(double x, double y, double width, double height) override;
   void set_width(double width) override;
   std::size_t num_svgs() const { return m_num_svgs; };
index 48a10f2c4c02534f8cd1d7da876c84dcd159c77a..d2091a231d1024bb28d9d5599e787be6f5c17c85 100644 (file)
@@ -30,6 +30,8 @@ public:
   virtual void set_height(double height) = 0;
   virtual void set_id(std::string_view id) = 0;
   virtual void set_text(std::string_view text) = 0;
+  virtual void set_transform(double a, double b, double c, double d, double e,
+                             double f) = 0;
   virtual void set_viewBox(double x, double y, double width, double height) = 0;
   virtual void set_width(double width) = 0;
 };
index 2a50161c5a46f0136802014b79939caccc8a04ff..35768d83aa272f605cbee1a10489866ab57affae 100644 (file)
@@ -97,6 +97,12 @@ void SVG::SVGElement::to_string_impl(std::string &output,
     break;
   case SVG::SVGElementType::Group:
     attributes_str += fmt::format(R"( class="{}")", attributes.class_);
+    if (attributes.transform.has_value()) {
+      const auto transform = attributes.transform;
+      attributes_str += fmt::format(
+          R"|( transform="scale({} {}) rotate({}) translate({} {})")|",
+          transform->a, transform->d, transform->c, transform->e, transform->f);
+    }
     break;
   case SVG::SVGElementType::Path:
     // ignore for now
index e910a7cb104f776de8f703d4deb63fcc2d058422..8b8d6cc457c0ac2a8a4c3dd2ba949e618a030968 100644 (file)
@@ -1,6 +1,7 @@
 #pragma once
 
 #include <cmath>
+#include <optional>
 #include <string>
 #include <string_view>
 #include <vector>
@@ -14,6 +15,15 @@ struct SVGRect {
   double height;
 };
 
+struct SVGMatrix {
+  double a;
+  double b;
+  double c;
+  double d;
+  double e;
+  double f;
+};
+
 enum class SVGElementType {
   Circle,
   Ellipse,
@@ -34,6 +44,7 @@ struct SVGAttributes {
   std::string class_;
   double height;
   std::string id;
+  std::optional<SVGMatrix> transform;
   SVGRect viewBox;
   double width;
 };
index 24d33c3944df969754f2f8cd677b1e4bcedebf46..b8a14853fa1fa0eeae35a2e0ad5fe6c0259cbfc2 100644 (file)
@@ -1,5 +1,7 @@
 #include <any>
 
+#include <boost/array.hpp>
+
 #include "svg_analyzer_interface.h"
 #include "svgpp_context.h"
 
@@ -117,6 +119,16 @@ void SvgppContext::set(svgpp::tag::attribute::cx a, const double v) {
   (void)v;
 }
 
+void SvgppContext::transform_matrix(const boost::array<double, 6> &matrix) {
+  double a = matrix.at(0);
+  double b = matrix.at(1);
+  double c = matrix.at(2);
+  double d = matrix.at(3);
+  double e = matrix.at(4);
+  double f = matrix.at(5);
+  m_svgAnalyzer->set_transform(a, b, c, d, e, f);
+}
+
 void SvgppContext::set(svgpp::tag::attribute::r a, const double v) {
   (void)a;
   (void)v;
index ec9a5b68be59b6fc603b9fefd04f389fb77f0e3f..8c0568b0962145499c26fc1b075f3339238d1e8b 100644 (file)
@@ -48,6 +48,7 @@ public:
   void path_exit();
   void set(svgpp::tag::attribute::cy cy, double v);
   void set(svgpp::tag::attribute::cx cx, double v);
+  void transform_matrix(const boost::array<double, 6> &matrix);
   void set(svgpp::tag::attribute::r r, double v);
   void set(svgpp::tag::attribute::rx rx, double v);
   void set(svgpp::tag::attribute::ry ry, double v);
index ad63276c1a8706e4c000ffee9e36cf349d68b565..19133e7bcd633efe5c842a7d975f6bf7ed1c9a37 100644 (file)
@@ -26,11 +26,12 @@ void traverseDocumentWithSvgpp(SvgppContext &context, char *text) {
 
     using processed_attributes_t =
         boost::mpl::set<svgpp::traits::shapes_attributes_by_element,
-                        svgpp::tag::attribute::class_,  //
-                        svgpp::tag::attribute::height,  //
-                        svgpp::tag::attribute::id,      //
-                        svgpp::tag::attribute::viewBox, //
-                        svgpp::tag::attribute::width    //
+                        svgpp::tag::attribute::class_,    //
+                        svgpp::tag::attribute::height,    //
+                        svgpp::tag::attribute::id,        //
+                        svgpp::tag::attribute::transform, //
+                        svgpp::tag::attribute::viewBox,   //
+                        svgpp::tag::attribute::width      //
                         >::type;
 
     svgpp::document_traversal<
index 786681f62c35bea8abc2af21817e31b69e0b667e..3fe9749f46bedb39c2972b745904513561bdd789 100644 (file)
@@ -123,9 +123,9 @@ 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] == "<g id=\"graph0\" class=\"graph\">") {
+      if (recreated_svg_lines[i] == "<polygon/>") {
         // stop comparison here since we do not yet handle all attributes on the
-        // 'g' element
+        // 'polygon' element
         break;
       }
       REQUIRE(recreated_svg_lines[i] == original_svg_lines[i]);
@@ -133,15 +133,6 @@ TEST_CASE(
 
     // do some sanity checks of the parts of the recreated SVG that we cannot
     // yet compare with the original SVG
-    CHECK(recreated_svg.find("<g id=\"graph0\" class=\"graph\">") !=
-          std::string::npos);
-    CHECK(recreated_svg.find("<g id=\"node1\" class=\"node\">") !=
-          std::string::npos);
-    CHECK(recreated_svg.find("<g id=\"node2\" class=\"node\">") !=
-          std::string::npos);
-    CHECK(recreated_svg.find("<g id=\"edge1\" class=\"edge\">") !=
-          std::string::npos);
-    CHECK(recreated_svg.find("</g>") != std::string::npos);
     CHECK(recreated_svg.find("<title>g1</title>") != std::string::npos);
     CHECK(recreated_svg.find("<title>a</title>") != std::string::npos);
     CHECK(recreated_svg.find("<title>b</title>") != std::string::npos);