From 850ad3d8552a0e6a4b3465942e81a6403fa42e15 Mon Sep 17 00:00:00 2001 From: Magnus Jacobsson Date: Tue, 26 Jul 2022 20:51:08 +0200 Subject: [PATCH] tests: SvgAnalyzer: add handling of the 'viewBox' attribute --- tests/svg_analyzer.cpp | 4 ++++ tests/svg_analyzer.h | 1 + tests/svg_analyzer_interface.h | 1 + tests/svg_element.cpp | 11 ++++++++--- tests/svg_element.h | 8 ++++++++ tests/svgpp_context.cpp | 5 +++++ tests/svgpp_context.h | 2 ++ tests/svgpp_document_traverser.cpp | 7 ++++--- tests/test_svg_analyzer.cpp | 1 + 9 files changed, 34 insertions(+), 6 deletions(-) diff --git a/tests/svg_analyzer.cpp b/tests/svg_analyzer.cpp index 39ee82e15..118f869af 100644 --- a/tests/svg_analyzer.cpp +++ b/tests/svg_analyzer.cpp @@ -143,6 +143,10 @@ void SVGAnalyzer::set_width(double width) { current_element().attributes.width = width; } +void SVGAnalyzer::set_viewBox(double x, double y, double width, double height) { + current_element().attributes.viewBox = {x, y, width, height}; +} + void SVGAnalyzer::set_graphviz_version(std::string_view version) { m_svg.graphviz_version = version; } diff --git a/tests/svg_analyzer.h b/tests/svg_analyzer.h index c2aa32c3a..541d600cd 100644 --- a/tests/svg_analyzer.h +++ b/tests/svg_analyzer.h @@ -30,6 +30,7 @@ public: void set_height(double height) override; void set_class(std::string_view) override; void set_text(std::string_view text) 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; }; std::size_t num_groups() const { return m_num_groups; }; diff --git a/tests/svg_analyzer_interface.h b/tests/svg_analyzer_interface.h index 0b15f163e..ac186e953 100644 --- a/tests/svg_analyzer_interface.h +++ b/tests/svg_analyzer_interface.h @@ -29,5 +29,6 @@ public: 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_viewBox(double x, double y, double width, double height) = 0; virtual void set_width(double width) = 0; }; diff --git a/tests/svg_element.cpp b/tests/svg_element.cpp index b8225ca81..2e33b33a9 100644 --- a/tests/svg_element.cpp +++ b/tests/svg_element.cpp @@ -88,9 +88,14 @@ void SVG::SVGElement::to_string_impl(std::string &output, // ignore for now break; case SVG::SVGElementType::Svg: - attributes_str += fmt::format(R"(width="{}pt" height="{}pt")", - std::lround(px_to_pt(attributes.width)), - std::lround(px_to_pt(attributes.height))); + attributes_str += + fmt::format(R"(width="{}pt" height="{}pt")" + "\n" + R"( viewBox="{:.2f} {:.2f} {:.2f} {:.2f}")", + std::lround(px_to_pt(attributes.width)), + std::lround(px_to_pt(attributes.height)), + attributes.viewBox.x, attributes.viewBox.y, + attributes.viewBox.width, attributes.viewBox.height); break; case SVG::SVGElementType::Text: // ignore for now diff --git a/tests/svg_element.h b/tests/svg_element.h index b8979bcb7..628bede7c 100644 --- a/tests/svg_element.h +++ b/tests/svg_element.h @@ -7,6 +7,13 @@ namespace SVG { +struct SVGRect { + double x; + double y; + double width; + double height; +}; + enum class SVGElementType { Circle, Ellipse, @@ -26,6 +33,7 @@ std::string_view tag(SVG::SVGElementType type); struct SVGAttributes { std::string class_; double height; + SVGRect viewBox; double width; }; diff --git a/tests/svgpp_context.cpp b/tests/svgpp_context.cpp index f28421d75..1a311a220 100644 --- a/tests/svgpp_context.cpp +++ b/tests/svgpp_context.cpp @@ -175,6 +175,11 @@ void SvgppContext::set(svgpp::tag::attribute::class_, m_svgAnalyzer->set_class({v.begin(), v.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); +} + void SvgppContext::set_impl(svgpp::tag::attribute::points &points, const std::any &range) { (void)points; diff --git a/tests/svgpp_context.h b/tests/svgpp_context.h index c66abb169..d52053720 100644 --- a/tests/svgpp_context.h +++ b/tests/svgpp_context.h @@ -65,6 +65,8 @@ public: void set(svgpp::tag::attribute::height height, double v); void set(svgpp::tag::attribute::class_ a, boost::iterator_range v); + void set(svgpp::tag::attribute::viewBox a, double v1, double v2, double v3, + double v4); void set_text(boost::iterator_range v); private: diff --git a/tests/svgpp_document_traverser.cpp b/tests/svgpp_document_traverser.cpp index 9dc865c1d..824834bd2 100644 --- a/tests/svgpp_document_traverser.cpp +++ b/tests/svgpp_document_traverser.cpp @@ -26,9 +26,10 @@ void traverseDocumentWithSvgpp(SvgppContext &context, char *text) { using processed_attributes_t = boost::mpl::set::type; svgpp::document_traversal< diff --git a/tests/test_svg_analyzer.cpp b/tests/test_svg_analyzer.cpp index 48f60172f..ff49e0dca 100644 --- a/tests/test_svg_analyzer.cpp +++ b/tests/test_svg_analyzer.cpp @@ -135,6 +135,7 @@ TEST_CASE( // yet compare with the original SVG CHECK(recreated_svg.find("") != std::string::npos); CHECK(recreated_svg.find("") != std::string::npos); CHECK(recreated_svg.find("") != std::string::npos); -- 2.40.0