]> granicus.if.org Git - graphviz/commitdiff
tests: SVGAnalyzer: add support for adding and re-creating a `rect` to SVGElement
authorMagnus Jacobsson <Magnus.Jacobsson@berotec.se>
Wed, 24 Aug 2022 10:20:13 +0000 (12:20 +0200)
committerMagnus Jacobsson <Magnus.Jacobsson@berotec.se>
Mon, 12 Sep 2022 07:46:17 +0000 (09:46 +0200)
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.

tests/svg_element.cpp
tests/svg_element.h

index c76428029d3f7ee5c592105dbcfcaa5c7611adea..b3cf3a9949d821dddf84bb929997e26351cd9bc9 100644 (file)
@@ -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")"
index 9113d2798e814fef4106660a29dcbd905705894a..c76952121af752b7149c7d58c740c639cf10c407 100644 (file)
@@ -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
   ///