]> granicus.if.org Git - graphviz/commitdiff
tests: SvgAnalyzer: add handling of the 'x' and 'y' attributes
authorMagnus Jacobsson <Magnus.Jacobsson@berotec.se>
Thu, 28 Jul 2022 08:43:07 +0000 (10:43 +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 b691f5128774a051debde779c892c0f5c87b8999..8fbed77794ffa16a0dbb8a2c98a56fad95679ed9 100644 (file)
@@ -171,6 +171,10 @@ void SVGAnalyzer::set_width(double width) {
   current_element().attributes.width = width;
 }
 
+void SVGAnalyzer::set_x(double x) { current_element().attributes.x = x; }
+
+void SVGAnalyzer::set_y(double y) { current_element().attributes.y = y; }
+
 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};
index 586783e11885bb6b05a4410f01ff343727b2fe0b..a83e7081afd7f9077aa8d0a2c335aa8d6eb8580c 100644 (file)
@@ -43,6 +43,8 @@ public:
                      double f) override;
   void set_viewBox(double x, double y, double width, double height) override;
   void set_width(double width) override;
+  void set_x(double x) override;
+  void set_y(double y) override;
   std::size_t num_svgs() const { return m_num_svgs; };
   std::size_t num_groups() const { return m_num_groups; };
   std::size_t num_circles() const { return m_num_circles; };
index ceba1910cf142a9a0cb16bcb7c2498e483504e1a..2df23e0d78d572604b742edb7dafc79fa4b0478d 100644 (file)
@@ -42,4 +42,6 @@ public:
                              double f) = 0;
   virtual void set_viewBox(double x, double y, double width, double height) = 0;
   virtual void set_width(double width) = 0;
+  virtual void set_x(double x) = 0;
+  virtual void set_y(double y) = 0;
 };
index b6326adea9253adfb7c78aeb9220dc017ec6a4b9..85d9802082f5ae2f1e76c6a3449c8517e6983eed 100644 (file)
@@ -182,7 +182,8 @@ void SVG::SVGElement::to_string_impl(std::string &output,
     break;
   case SVG::SVGElementType::Text:
     attributes_str +=
-        fmt::format(R"(text-anchor="{}")", attributes.text_anchor);
+        fmt::format(R"(text-anchor="{}" x="{}" y="{}")", attributes.text_anchor,
+                    attributes.x, attributes.y);
     break;
   case SVG::SVGElementType::Title:
     // Graphviz doesn't generate attributes on 'title' elements
index ad5091431f265835ef05fffe1182caf1130ab677..e30b8fbe98517ce1126ddd5ad8ff3ca6b30e82df 100644 (file)
@@ -60,6 +60,8 @@ struct SVGAttributes {
   std::optional<SVGMatrix> transform;
   SVGRect viewBox;
   double width;
+  double x;
+  double y;
 };
 
 /**
index 1423505c7398dd44fad8f7df99cf7369399b65a8..92d825fef73f4c63872c71e432408d9cb4717109 100644 (file)
@@ -198,14 +198,28 @@ void SvgppContext::set(svgpp::tag::attribute::y2 a, const double v) {
   (void)v;
 }
 
-void SvgppContext::set(svgpp::tag::attribute::x a, const double v) {
-  (void)a;
-  (void)v;
+void SvgppContext::set(svgpp::tag::attribute::x, const double v) {
+  m_svgAnalyzer->set_x(v);
 }
 
-void SvgppContext::set(svgpp::tag::attribute::y a, const double v) {
-  (void)a;
-  (void)v;
+void SvgppContext::set(svgpp::tag::attribute::x, const NumbersRange &range) {
+  if (boost::size(range) != 1) {
+    throw std::runtime_error{
+        "Multiple value list for the 'x' attribute is not yet implemented"};
+  }
+  m_svgAnalyzer->set_x(*range.begin());
+}
+
+void SvgppContext::set(svgpp::tag::attribute::y, const double v) {
+  m_svgAnalyzer->set_y(v);
+}
+
+void SvgppContext::set(svgpp::tag::attribute::y, const NumbersRange &range) {
+  if (boost::size(range) != 1) {
+    throw std::runtime_error{
+        "Multiple value list for the 'y' attribute is not yet implemented"};
+  }
+  m_svgAnalyzer->set_y(*range.begin());
 }
 
 void SvgppContext::set(svgpp::tag::attribute::width, const double v) {
index 06b00377de1e0d266e25aa0f7e1d3ed4719979ab..1ec8ed58ac94b763366adbfc7cbcf2f0c553fd1a 100644 (file)
@@ -162,8 +162,13 @@ public:
                            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::x, double v);
+  typedef boost::any_range<double, boost::single_pass_traversal_tag, double,
+                           std::ptrdiff_t>
+      NumbersRange;
+  void set(svgpp::tag::attribute::x, const NumbersRange &range);
+  void set(svgpp::tag::attribute::y, double v);
+  void set(svgpp::tag::attribute::y, const NumbersRange &range);
   void set(svgpp::tag::attribute::width width, double v);
   void set(svgpp::tag::attribute::height height, double v);
   void set(svgpp::tag::attribute::id a, boost::iterator_range<const char *> v);
index 45148e019d225aee482743ac2aae65f759156852..bd1a12647b7943dddd79ad5b48080d1b8d39b489 100644 (file)
@@ -39,7 +39,9 @@ void traverseDocumentWithSvgpp(SvgppContext &context, char *text) {
                         svgpp::tag::attribute::text_anchor, //
                         svgpp::tag::attribute::transform,   //
                         svgpp::tag::attribute::viewBox,     //
-                        svgpp::tag::attribute::width        //
+                        svgpp::tag::attribute::width,       //
+                        svgpp::tag::attribute::x,           //
+                        svgpp::tag::attribute::y            //
                         >::type;
 
     svgpp::document_traversal<
index d9efa0df7a660c56ed2f36747b8ba53fb0d920d0..46ae43358fa052dc587588403121ccdd13a4d2bb 100644 (file)
@@ -129,7 +129,8 @@ TEST_CASE(
         // yet handle all attributes on the 'path' element
         break;
       }
-      if (recreated_svg_lines[i] == "<text text-anchor=\"middle\">a</text>") {
+      if (recreated_svg_lines[i].starts_with(
+              "<text text-anchor=\"middle\" x=\"")) {
         // stop comparison here since we do not yet handle all attributes on the
         // 'text' element
         break;
@@ -143,10 +144,11 @@ TEST_CASE(
     CHECK(recreated_svg.find("<title>a</title>") != std::string::npos);
     CHECK(recreated_svg.find("<title>b</title>") != std::string::npos);
     if (shape != "point") {
-      CHECK(recreated_svg.find("<text text-anchor=\"middle\">a</text>") !=
-            std::string::npos);
-      CHECK(recreated_svg.find("<text text-anchor=\"middle\">b</text>") !=
+      CHECK(recreated_svg.find("<text text-anchor=\"middle\" x=\"") !=
             std::string::npos);
+      CHECK(recreated_svg.find("\" y=\"") != std::string::npos);
+      CHECK(recreated_svg.find("a</text>") != std::string::npos);
+      CHECK(recreated_svg.find("b</text>") != std::string::npos);
     }
     CHECK(recreated_svg.find("<path fill=\"none\" stroke=\"black\"/>") !=
           std::string::npos);