From: Magnus Jacobsson Date: Wed, 27 Jul 2022 13:40:08 +0000 (+0200) Subject: tests: SvgAnalyzer: add handling of the 'stroke' attribute X-Git-Tag: 5.0.1~7^2~10 X-Git-Url: https://granicus.if.org/sourcecode?a=commitdiff_plain;h=3596f7a12771d65828cf3d5a3389e4e7d2124d33;p=graphviz tests: SvgAnalyzer: add handling of the 'stroke' attribute --- diff --git a/tests/svg_analyzer.cpp b/tests/svg_analyzer.cpp index 2bc011657..501171ec2 100644 --- a/tests/svg_analyzer.cpp +++ b/tests/svg_analyzer.cpp @@ -123,6 +123,10 @@ void SVGAnalyzer::set_height(double height) { current_element().attributes.height = height; } +void SVGAnalyzer::set_stroke(std::string_view stroke) { + current_element().attributes.stroke = stroke; +} + void SVGAnalyzer::set_id(std::string_view id) { current_element().attributes.id = id; } diff --git a/tests/svg_analyzer.h b/tests/svg_analyzer.h index 12a201f9a..212fe66a6 100644 --- a/tests/svg_analyzer.h +++ b/tests/svg_analyzer.h @@ -31,6 +31,7 @@ public: void set_height(double height) override; void set_id(std::string_view id) override; void set_class(std::string_view) override; + void set_stroke(std::string_view stroke) override; void set_text(std::string_view text) override; void set_transform(double a, double b, double c, double d, double e, double f) override; diff --git a/tests/svg_analyzer_interface.h b/tests/svg_analyzer_interface.h index a574770d3..086b9ff1d 100644 --- a/tests/svg_analyzer_interface.h +++ b/tests/svg_analyzer_interface.h @@ -30,6 +30,7 @@ public: virtual void set_fill(std::string_view fill) = 0; virtual void set_height(double height) = 0; virtual void set_id(std::string_view id) = 0; + virtual void set_stroke(std::string_view stroke) = 0; virtual void set_text(std::string_view text) = 0; virtual void set_transform(double a, double b, double c, double d, double e, double f) = 0; diff --git a/tests/svg_element.cpp b/tests/svg_element.cpp index 55e5c5676..2d1702331 100644 --- a/tests/svg_element.cpp +++ b/tests/svg_element.cpp @@ -82,6 +82,15 @@ std::string SVG::SVGElement::id_attribute_to_string() const { return fmt::format(R"(id="{}")", attributes.id); } +std::string SVG::SVGElement::stroke_attribute_to_string() const { + if (attributes.stroke.empty()) { + return ""; + } + + return fmt::format(R"(stroke="{}")", + stroke_to_graphviz_color(attributes.stroke)); +} + std::string SVG::SVGElement::to_string(std::size_t indent_size = 2) const { std::string output; output += R"()" @@ -121,6 +130,7 @@ void SVG::SVGElement::to_string_impl(std::string &output, switch (type) { case SVG::SVGElementType::Ellipse: append_attribute(attributes_str, fill_attribute_to_string()); + append_attribute(attributes_str, stroke_attribute_to_string()); break; case SVG::SVGElementType::Group: attributes_str += fmt::format(R"( class="{}")", attributes.class_); @@ -133,12 +143,15 @@ void SVG::SVGElement::to_string_impl(std::string &output, break; case SVG::SVGElementType::Path: append_attribute(attributes_str, fill_attribute_to_string()); + append_attribute(attributes_str, stroke_attribute_to_string()); break; case SVG::SVGElementType::Polygon: append_attribute(attributes_str, fill_attribute_to_string()); + append_attribute(attributes_str, stroke_attribute_to_string()); break; case SVG::SVGElementType::Polyline: append_attribute(attributes_str, fill_attribute_to_string()); + append_attribute(attributes_str, stroke_attribute_to_string()); break; case SVG::SVGElementType::Svg: attributes_str += fmt::format( @@ -185,6 +198,11 @@ void SVG::SVGElement::to_string_impl(std::string &output, } } +std::string +SVG::SVGElement::stroke_to_graphviz_color(const std::string &color) const { + return to_graphviz_color(color); +} + std::string_view SVG::tag(SVGElementType type) { switch (type) { case SVG::SVGElementType::Circle: diff --git a/tests/svg_element.h b/tests/svg_element.h index 87d27bfc4..4e701c527 100644 --- a/tests/svg_element.h +++ b/tests/svg_element.h @@ -45,6 +45,7 @@ struct SVGAttributes { std::string fill; double height; std::string id; + std::string stroke; std::optional transform; SVGRect viewBox; double width; @@ -84,6 +85,8 @@ private: const std::string &attribute) const; std::string id_attribute_to_string() const; std::string fill_attribute_to_string() const; + std::string stroke_attribute_to_string() const; + std::string stroke_to_graphviz_color(const std::string &color) const; void to_string_impl(std::string &output, std::size_t indent_size, std::size_t current_indent) const; }; diff --git a/tests/svgpp_context.cpp b/tests/svgpp_context.cpp index ea32be5e3..e75a7cf81 100644 --- a/tests/svgpp_context.cpp +++ b/tests/svgpp_context.cpp @@ -141,6 +141,22 @@ void SvgppContext::set(svgpp::tag::attribute::fill, color_t color, m_svgAnalyzer->set_fill(to_color_string(color)); } +void SvgppContext::set(svgpp::tag::attribute::stroke, svgpp::tag::value::none) { + m_svgAnalyzer->set_stroke("none"); +} + +void SvgppContext::set(svgpp::tag::attribute::stroke, + svgpp::tag::value::currentColor) { + throw std::runtime_error{ + "the 'stroke' attribute 'currentColor' value is not yet implemented"}; +} + +void SvgppContext::set(svgpp::tag::attribute::stroke, + SvgppContext::color_t color, + svgpp::tag::skip_icc_color) { + m_svgAnalyzer->set_stroke(to_color_string(color)); +} + void SvgppContext::transform_matrix(const boost::array &matrix) { double a = matrix.at(0); double b = matrix.at(1); diff --git a/tests/svgpp_context.h b/tests/svgpp_context.h index f024f0e3e..35b88d275 100644 --- a/tests/svgpp_context.h +++ b/tests/svgpp_context.h @@ -99,6 +99,56 @@ public: throw std::runtime_error{ "this flavor of the 'fill' attribute is not yet implemented"}; }; + void set(svgpp::tag::attribute::stroke, svgpp::tag::value::none); + void set(svgpp::tag::attribute::stroke, svgpp::tag::value::currentColor); + void set(svgpp::tag::attribute::stroke, color_t color, + svgpp::tag::skip_icc_color = svgpp::tag::skip_icc_color()); + template void set(svgpp::tag::attribute::stroke, IRI const &) { + throw std::runtime_error{ + "this flavor of the 'stroke' attribute is not yet implemented"}; + }; + template + void set(svgpp::tag::attribute::stroke, svgpp::tag::iri_fragment, + IRI const &) { + throw std::runtime_error{ + "this flavor of the 'stroke' attribute is not yet implemented"}; + }; + template + void set(svgpp::tag::attribute::stroke, IRI const &, + svgpp::tag::value::none) { + throw std::runtime_error{ + "this flavor of the 'stroke' attribute is not yet implemented"}; + }; + template + void set(svgpp::tag::attribute::stroke, svgpp::tag::iri_fragment, IRI const &, + svgpp::tag::value::none) { + throw std::runtime_error{ + "this flavor of the 'stroke' attribute is not yet implemented"}; + }; + template + void set(svgpp::tag::attribute::stroke, IRI const &, + svgpp::tag::value::currentColor) { + throw std::runtime_error{ + "this flavor of the 'stroke' attribute is not yet implemented"}; + }; + template + void set(svgpp::tag::attribute::stroke, svgpp::tag::iri_fragment, IRI const &, + svgpp::tag::value::currentColor) { + throw std::runtime_error{ + "this flavor of the 'stroke' attribute is not yet implemented"}; + }; + template + void set(svgpp::tag::attribute::stroke, IRI const &, color_t, + svgpp::tag::skip_icc_color = svgpp::tag::skip_icc_color()) { + throw std::runtime_error{ + "this flavor of the 'stroke' attribute is not yet implemented"}; + }; + template + void set(svgpp::tag::attribute::stroke, svgpp::tag::iri_fragment, IRI const &, + color_t, svgpp::tag::skip_icc_color = svgpp::tag::skip_icc_color()) { + throw std::runtime_error{ + "this flavor of the 'stroke' attribute is not yet implemented"}; + }; void transform_matrix(const boost::array &matrix); void set(svgpp::tag::attribute::r r, double v); void set(svgpp::tag::attribute::rx rx, double v); diff --git a/tests/svgpp_document_traverser.cpp b/tests/svgpp_document_traverser.cpp index a73c9599c..3894ded48 100644 --- a/tests/svgpp_document_traverser.cpp +++ b/tests/svgpp_document_traverser.cpp @@ -30,6 +30,7 @@ void traverseDocumentWithSvgpp(SvgppContext &context, char *text) { svgpp::tag::attribute::fill, // svgpp::tag::attribute::height, // svgpp::tag::attribute::id, // + svgpp::tag::attribute::stroke, // svgpp::tag::attribute::transform, // svgpp::tag::attribute::viewBox, // svgpp::tag::attribute::width // diff --git a/tests/test_svg_analyzer.cpp b/tests/test_svg_analyzer.cpp index 12bb9ae97..32197d7ea 100644 --- a/tests/test_svg_analyzer.cpp +++ b/tests/test_svg_analyzer.cpp @@ -123,7 +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] == "") { + if (recreated_svg_lines[i] == + "") { // stop comparison here since we do not yet handle all attributes on the // 'polygon' element break; @@ -140,8 +141,10 @@ TEST_CASE( CHECK(recreated_svg.find("a") != std::string::npos); CHECK(recreated_svg.find("b") != std::string::npos); } - CHECK(recreated_svg.find("") != std::string::npos); - CHECK(recreated_svg.find("") != std::string::npos); + CHECK(recreated_svg.find("") != + std::string::npos); + CHECK(recreated_svg.find("") != + std::string::npos); CHECK(recreated_svg.find("") != std::string::npos); CHECK(recreated_svg.find("") != std::string::npos); CHECK(recreated_svg.find("") != std::string::npos);