]> granicus.if.org Git - graphviz/commitdiff
tests: SvgAnalyzer: add handling of the 'width' and 'height' attributes
authorMagnus Jacobsson <Magnus.Jacobsson@berotec.se>
Tue, 26 Jul 2022 13:22:29 +0000 (15:22 +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_document_traverser.cpp
tests/test_svg_analyzer.cpp

index e688b7825fe1b9f457b1c09211ccee9d6e8b8811..39ee82e15278579d13ac3a2f98c1d2b502970ab3 100644 (file)
@@ -115,6 +115,10 @@ void SVGAnalyzer::set_class(std::string_view class_) {
   current_element().attributes.class_ = class_;
 }
 
+void SVGAnalyzer::set_height(double height) {
+  current_element().attributes.height = height;
+}
+
 void SVGAnalyzer::set_text(std::string_view text) {
   auto &element = current_element();
   element.text = text;
@@ -135,6 +139,10 @@ void SVGAnalyzer::set_text(std::string_view text) {
   }
 }
 
+void SVGAnalyzer::set_width(double width) {
+  current_element().attributes.width = width;
+}
+
 void SVGAnalyzer::set_graphviz_version(std::string_view version) {
   m_svg.graphviz_version = version;
 }
index f8e7a4c9de6b5fd8012fb2dd5dc9e32c6b6ef01b..c2aa32c3ac6d9919448d0714dab203fbd9eaf2bc 100644 (file)
@@ -27,8 +27,10 @@ public:
   void on_enter_element_text() override;
   void on_enter_element_title() override;
   void on_exit_element() override;
+  void set_height(double height) override;
   void set_class(std::string_view) override;
   void set_text(std::string_view text) override;
+  void set_width(double width) 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 a81a5804b9c989caf40ae76ead52a9a13d0e33ec..0b15f163e0e4945f5282407fec47d7e5fc7fad09 100644 (file)
@@ -27,5 +27,7 @@ public:
   virtual void on_enter_element_title() = 0;
   virtual void on_exit_element() = 0;
   virtual void set_class(std::string_view) = 0;
+  virtual void set_height(double height) = 0;
   virtual void set_text(std::string_view text) = 0;
+  virtual void set_width(double width) = 0;
 };
index efce9e1c992264ad0fecf7f0cd04c71d8491ec85..b8225ca81b98ab4d3bb829bc8dab7608a2041825 100644 (file)
@@ -7,6 +7,12 @@
 
 SVG::SVGElement::SVGElement(SVGElementType type) : type(type) {}
 
+static double px_to_pt(double px) {
+  // a `pt` is 0.75 `px`. See e.g.
+  // https://oreillymedia.github.io/Using_SVG/guide/units.html
+  return px * 3 / 4;
+}
+
 static std::string xml_encode(const std::string &text) {
   std::string out;
   for (const char &ch : text) {
@@ -82,7 +88,9 @@ void SVG::SVGElement::to_string_impl(std::string &output,
     // ignore for now
     break;
   case SVG::SVGElementType::Svg:
-    // ignore for now
+    attributes_str += fmt::format(R"(width="{}pt" height="{}pt")",
+                                  std::lround(px_to_pt(attributes.width)),
+                                  std::lround(px_to_pt(attributes.height)));
     break;
   case SVG::SVGElementType::Text:
     // ignore for now
index bf3e206b0f5280b62f2e2fee9849f00423ce2477..b8979bcb7c09db57265cbcd6b133c74a9c3013c3 100644 (file)
@@ -1,5 +1,6 @@
 #pragma once
 
+#include <cmath>
 #include <string>
 #include <string_view>
 #include <vector>
@@ -24,6 +25,8 @@ std::string_view tag(SVG::SVGElementType type);
 
 struct SVGAttributes {
   std::string class_;
+  double height;
+  double width;
 };
 
 /**
index f09ec6d449d3be713dde7019f2f8a211a5f4c797..f28421d75099cbd83210eab2f6b177ca4e3251b5 100644 (file)
@@ -162,14 +162,12 @@ void SvgppContext::set(svgpp::tag::attribute::y a, const double v) {
   (void)v;
 }
 
-void SvgppContext::set(svgpp::tag::attribute::width a, const double v) {
-  (void)a;
-  (void)v;
+void SvgppContext::set(svgpp::tag::attribute::width, const double v) {
+  m_svgAnalyzer->set_width(v);
 }
 
-void SvgppContext::set(svgpp::tag::attribute::height a, const double v) {
-  (void)a;
-  (void)v;
+void SvgppContext::set(svgpp::tag::attribute::height, const double v) {
+  m_svgAnalyzer->set_height(v);
 }
 
 void SvgppContext::set(svgpp::tag::attribute::class_,
index c22cfd2967426f8cda1292143f078284b86e3419..9dc865c1df371a2e93e35138a02e9f7451570fe9 100644 (file)
@@ -26,7 +26,9 @@ 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::class_, //
+                        svgpp::tag::attribute::height, //
+                        svgpp::tag::attribute::width   //
                         >::type;
 
     svgpp::document_traversal<
index 2a32ebcb583c4807ed309c02fcbd22a0a5b00ecd..48f60172f75af7201c420fff97c54d9e17339c82 100644 (file)
@@ -123,8 +123,8 @@ 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] == "<svg>") {
-        // stop comparison here since we do not yet handle attributes on the
+      if (recreated_svg_lines[i].starts_with("<svg width=\"")) {
+        // stop comparison here since we do not yet handle all attributes on the
         // 'svg' element
         break;
       }
@@ -133,7 +133,8 @@ 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("<svg>") != std::string::npos);
+    CHECK(recreated_svg.find("<svg width=\"") != std::string::npos);
+    CHECK(recreated_svg.find("\" height=\"") != std::string::npos);
     CHECK(recreated_svg.find("</svg>") != std::string::npos);
     CHECK(recreated_svg.find("<g class=\"graph\">") != std::string::npos);
     CHECK(recreated_svg.find("<g class=\"node\">") != std::string::npos);