]> granicus.if.org Git - graphviz/commitdiff
tests: SvgAnalyzer: add handling of the 'text-anchor' attribute
authorMagnus Jacobsson <Magnus.Jacobsson@berotec.se>
Thu, 28 Jul 2022 08:27:29 +0000 (10:27 +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 aa47b67649267dca8f34b9f81531b511b7991fe1..b691f5128774a051debde779c892c0f5c87b8999 100644 (file)
@@ -163,6 +163,10 @@ void SVGAnalyzer::set_text(std::string_view text) {
   }
 }
 
+void SVGAnalyzer::set_text_anchor(std::string_view text_anchor) {
+  current_element().attributes.text_anchor = text_anchor;
+}
+
 void SVGAnalyzer::set_width(double width) {
   current_element().attributes.width = width;
 }
index 9eff98df92ff8924f7ac7f2d48b2c1639a147b60..586783e11885bb6b05a4410f01ff343727b2fe0b 100644 (file)
@@ -38,6 +38,7 @@ public:
   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_text_anchor(std::string_view text_anchor) 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;
index ea5a4a28f7c188f409e94c95b806652e6240acdd..ceba1910cf142a9a0cb16bcb7c2498e483504e1a 100644 (file)
@@ -37,6 +37,7 @@ public:
   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_text_anchor(std::string_view text_anchor) = 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;
index 6e29f588cf11d8c749ae39243481bf5f31130744..b6326adea9253adfb7c78aeb9220dc017ec6a4b9 100644 (file)
@@ -181,7 +181,8 @@ void SVG::SVGElement::to_string_impl(std::string &output,
         attributes.viewBox.height);
     break;
   case SVG::SVGElementType::Text:
-    // ignore for now
+    attributes_str +=
+        fmt::format(R"(text-anchor="{}")", attributes.text_anchor);
     break;
   case SVG::SVGElementType::Title:
     // Graphviz doesn't generate attributes on 'title' elements
index 7d554554d2a46dbbb3bbca82d11ff99a3b5f9a3e..ad5091431f265835ef05fffe1182caf1130ab677 100644 (file)
@@ -56,6 +56,7 @@ struct SVGAttributes {
   double rx;
   double ry;
   std::string stroke;
+  std::string text_anchor;
   std::optional<SVGMatrix> transform;
   SVGRect viewBox;
   double width;
index 1bdd293d6da6d41c62a999b54aacc740c5be8de9..1423505c7398dd44fad8f7df99cf7369399b65a8 100644 (file)
@@ -226,6 +226,21 @@ void SvgppContext::set(svgpp::tag::attribute::class_,
   m_svgAnalyzer->set_class({v.begin(), v.end()});
 }
 
+void SvgppContext::set(svgpp::tag::attribute::text_anchor,
+                       svgpp::tag::value::start) {
+  m_svgAnalyzer->set_text_anchor("start");
+}
+
+void SvgppContext::set(svgpp::tag::attribute::text_anchor,
+                       svgpp::tag::value::middle) {
+  m_svgAnalyzer->set_text_anchor("middle");
+}
+
+void SvgppContext::set(svgpp::tag::attribute::text_anchor,
+                       svgpp::tag::value::end) {
+  m_svgAnalyzer->set_text_anchor("end");
+}
+
 void SvgppContext::set(svgpp::tag::attribute::viewBox, const double v1,
                        const double v2, const double v3, const double v4) {
   m_svgAnalyzer->set_viewBox(v1, v2, v3, v4);
index cfbbfb60ab7ac55f0d3f551e0d43fc47c86ac351..06b00377de1e0d266e25aa0f7e1d3ed4719979ab 100644 (file)
@@ -169,6 +169,9 @@ public:
   void set(svgpp::tag::attribute::id a, boost::iterator_range<const char *> v);
   void set(svgpp::tag::attribute::class_ a,
            boost::iterator_range<const char *> v);
+  void set(svgpp::tag::attribute::text_anchor a, svgpp::tag::value::start v);
+  void set(svgpp::tag::attribute::text_anchor a, svgpp::tag::value::middle v);
+  void set(svgpp::tag::attribute::text_anchor a, svgpp::tag::value::end v);
   void set(svgpp::tag::attribute::viewBox a, double v1, double v2, double v3,
            double v4);
   void set_text(boost::iterator_range<const char *> v);
index 9ed7421a6d0fd82782ae6d80a24115a00d5839ea..45148e019d225aee482743ac2aae65f759156852 100644 (file)
@@ -26,19 +26,20 @@ 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::cx,        //
-                        svgpp::tag::attribute::cy,        //
-                        svgpp::tag::attribute::fill,      //
-                        svgpp::tag::attribute::height,    //
-                        svgpp::tag::attribute::id,        //
-                        svgpp::tag::attribute::points,    //
-                        svgpp::tag::attribute::rx,        //
-                        svgpp::tag::attribute::ry,        //
-                        svgpp::tag::attribute::stroke,    //
-                        svgpp::tag::attribute::transform, //
-                        svgpp::tag::attribute::viewBox,   //
-                        svgpp::tag::attribute::width      //
+                        svgpp::tag::attribute::class_,      //
+                        svgpp::tag::attribute::cx,          //
+                        svgpp::tag::attribute::cy,          //
+                        svgpp::tag::attribute::fill,        //
+                        svgpp::tag::attribute::height,      //
+                        svgpp::tag::attribute::id,          //
+                        svgpp::tag::attribute::points,      //
+                        svgpp::tag::attribute::rx,          //
+                        svgpp::tag::attribute::ry,          //
+                        svgpp::tag::attribute::stroke,      //
+                        svgpp::tag::attribute::text_anchor, //
+                        svgpp::tag::attribute::transform,   //
+                        svgpp::tag::attribute::viewBox,     //
+                        svgpp::tag::attribute::width        //
                         >::type;
 
     svgpp::document_traversal<
index 34469a9e529f0e3a3e6d76dc3f41f105a6ee1abf..d9efa0df7a660c56ed2f36747b8ba53fb0d920d0 100644 (file)
@@ -129,9 +129,9 @@ TEST_CASE(
         // 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
+      if (recreated_svg_lines[i] == "<text text-anchor=\"middle\">a</text>") {
+        // stop comparison here since we do not yet handle all attributes on the
+        // 'text' element
         break;
       }
       REQUIRE(recreated_svg_lines[i] == original_svg_lines[i]);
@@ -143,8 +143,10 @@ 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>a</text>") != std::string::npos);
-      CHECK(recreated_svg.find("<text>b</text>") != std::string::npos);
+      CHECK(recreated_svg.find("<text text-anchor=\"middle\">a</text>") !=
+            std::string::npos);
+      CHECK(recreated_svg.find("<text text-anchor=\"middle\">b</text>") !=
+            std::string::npos);
     }
     CHECK(recreated_svg.find("<path fill=\"none\" stroke=\"black\"/>") !=
           std::string::npos);