From: Magnus Jacobsson Date: Wed, 24 Aug 2022 10:20:13 +0000 (+0200) Subject: tests: SVGAnalyzer: add support for adding and re-creating a `rect` to SVGElement X-Git-Tag: 6.0.2~43^2~4 X-Git-Url: https://granicus.if.org/sourcecode?a=commitdiff_plain;h=8219942ddd4510771a0e0982c4201a41146cfbf6;p=graphviz tests: SVGAnalyzer: add support for adding and re-creating a `rect` to SVGElement This will be used in upcoming commits to annotate the re-created SVG with bounding boxes to highlight Graphviz component boundaries and overlaps. Towards https://gitlab.com/graphviz/graphviz/-/issues/372. --- diff --git a/tests/svg_element.cpp b/tests/svg_element.cpp index c76428029..b3cf3a994 100644 --- a/tests/svg_element.cpp +++ b/tests/svg_element.cpp @@ -109,6 +109,18 @@ std::string SVG::to_dot_color(const std::string &color, double opacity) { return rgb_to_hex(color, opacity); } +void SVG::SVGElement::add_rect(SVGRect rect, const std::string color) { + SVG::SVGElement element{SVG::SVGElementType::Rect}; + element.attributes.x = rect.x; + element.attributes.y = rect.y; + element.attributes.width = rect.width; + element.attributes.height = rect.height; + element.attributes.stroke_width = 0.1; + element.attributes.stroke = color; + element.attributes.fill = "none"; + children.push_back(element); +} + SVG::SVGRect SVG::SVGElement::bbox(bool throw_if_bbox_not_defined) { if (!m_bbox.has_value()) { // negative width and height bbox that will be immediately replaced by the @@ -611,6 +623,15 @@ void SVG::SVGElement::to_string_impl(std::string &output, append_attribute(attributes_str, stroke_opacity_attribute_to_string()); append_attribute(attributes_str, points_attribute_to_string()); break; + case SVG::SVGElementType::Rect: + attributes_str += + fmt::format(R"(x="{}" y="{}" width="{}" height="{}")", attributes.x, + attributes.y, attributes.width, attributes.height); + append_attribute(attributes_str, fill_attribute_to_string()); + append_attribute(attributes_str, stroke_attribute_to_string()); + append_attribute(attributes_str, stroke_width_attribute_to_string()); + append_attribute(attributes_str, stroke_opacity_attribute_to_string()); + break; case SVG::SVGElementType::Svg: attributes_str += fmt::format( R"(width="{}pt" height="{}pt")" diff --git a/tests/svg_element.h b/tests/svg_element.h index 9113d2798..c76952121 100644 --- a/tests/svg_element.h +++ b/tests/svg_element.h @@ -98,6 +98,8 @@ public: SVGElement() = delete; explicit SVGElement(SVG::SVGElementType type); + /// Add an SVG `rect` element as a child to the element + void add_rect(SVG::SVGRect rect, std::string color); /// \brief Return the value of an attribute retrieved from the element and its /// children ///