From: Magnus Jacobsson Date: Tue, 26 Jul 2022 13:22:29 +0000 (+0200) Subject: tests: SvgAnalyzer: add handling of the 'width' and 'height' attributes X-Git-Tag: 5.0.1~7^2~16 X-Git-Url: https://granicus.if.org/sourcecode?a=commitdiff_plain;h=34b6db9aad9a85dffa9665d91806e8cef0fa8924;p=graphviz tests: SvgAnalyzer: add handling of the 'width' and 'height' attributes --- diff --git a/tests/svg_analyzer.cpp b/tests/svg_analyzer.cpp index e688b7825..39ee82e15 100644 --- a/tests/svg_analyzer.cpp +++ b/tests/svg_analyzer.cpp @@ -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; } diff --git a/tests/svg_analyzer.h b/tests/svg_analyzer.h index f8e7a4c9d..c2aa32c3a 100644 --- a/tests/svg_analyzer.h +++ b/tests/svg_analyzer.h @@ -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; }; diff --git a/tests/svg_analyzer_interface.h b/tests/svg_analyzer_interface.h index a81a5804b..0b15f163e 100644 --- a/tests/svg_analyzer_interface.h +++ b/tests/svg_analyzer_interface.h @@ -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; }; diff --git a/tests/svg_element.cpp b/tests/svg_element.cpp index efce9e1c9..b8225ca81 100644 --- a/tests/svg_element.cpp +++ b/tests/svg_element.cpp @@ -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 diff --git a/tests/svg_element.h b/tests/svg_element.h index bf3e206b0..b8979bcb7 100644 --- a/tests/svg_element.h +++ b/tests/svg_element.h @@ -1,5 +1,6 @@ #pragma once +#include #include #include #include @@ -24,6 +25,8 @@ std::string_view tag(SVG::SVGElementType type); struct SVGAttributes { std::string class_; + double height; + double width; }; /** diff --git a/tests/svgpp_context.cpp b/tests/svgpp_context.cpp index f09ec6d44..f28421d75 100644 --- a/tests/svgpp_context.cpp +++ b/tests/svgpp_context.cpp @@ -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_, diff --git a/tests/svgpp_document_traverser.cpp b/tests/svgpp_document_traverser.cpp index c22cfd296..9dc865c1d 100644 --- a/tests/svgpp_document_traverser.cpp +++ b/tests/svgpp_document_traverser.cpp @@ -26,7 +26,9 @@ 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 2a32ebcb5..48f60172f 100644 --- a/tests/test_svg_analyzer.cpp +++ b/tests/test_svg_analyzer.cpp @@ -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] == "") { - // stop comparison here since we do not yet handle attributes on the + if (recreated_svg_lines[i].starts_with("") != std::string::npos); + CHECK(recreated_svg.find("") != std::string::npos); CHECK(recreated_svg.find("") != std::string::npos); CHECK(recreated_svg.find("") != std::string::npos);