current_element().attributes.height = height;
}
+void SVGAnalyzer::set_id(std::string_view id) {
+ current_element().attributes.id = id;
+}
+
void SVGAnalyzer::set_text(std::string_view text) {
auto &element = current_element();
element.text = text;
void on_enter_element_title() override;
void on_exit_element() override;
void set_height(double height) override;
+ void set_id(std::string_view id) 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;
virtual void on_exit_element() = 0;
virtual void set_class(std::string_view) = 0;
virtual void set_height(double height) = 0;
+ virtual void set_id(std::string_view id) = 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;
return out;
}
+void SVG::SVGElement::append_attribute(std::string &output,
+ const std::string &attribute) const {
+ if (attribute.empty()) {
+ return;
+ }
+ if (!output.empty()) {
+ output += " ";
+ }
+ output += attribute;
+}
+
+std::string SVG::SVGElement::id_attribute_to_string() const {
+ if (attributes.id.empty()) {
+ return "";
+ }
+
+ return fmt::format(R"(id="{}")", attributes.id);
+}
+
std::string SVG::SVGElement::to_string(std::size_t indent_size = 2) const {
std::string output;
output += R"(<?xml version="1.0" encoding="UTF-8" standalone="no"?>)"
output += tag(type);
std::string attributes_str{};
+ append_attribute(attributes_str, id_attribute_to_string());
switch (type) {
case SVG::SVGElementType::Ellipse:
// ignore for now
break;
case SVG::SVGElementType::Group:
- attributes_str += fmt::format(R"(class="{}")", attributes.class_);
+ attributes_str += fmt::format(R"( class="{}")", attributes.class_);
break;
case SVG::SVGElementType::Path:
// ignore for now
struct SVGAttributes {
std::string class_;
double height;
+ std::string id;
SVGRect viewBox;
double width;
};
const SVGElementType type;
private:
+ /// append a string possibly containing an attribute to another string,
+ /// handling space separation
+ void append_attribute(std::string &output,
+ const std::string &attribute) const;
+ std::string id_attribute_to_string() const;
void to_string_impl(std::string &output, std::size_t indent_size,
std::size_t current_indent) const;
};
m_svgAnalyzer->set_height(v);
}
+void SvgppContext::set(svgpp::tag::attribute::id,
+ boost::iterator_range<const char *> v) {
+ m_svgAnalyzer->set_id({v.begin(), v.end()});
+}
+
void SvgppContext::set(svgpp::tag::attribute::class_,
boost::iterator_range<const char *> v) {
m_svgAnalyzer->set_class({v.begin(), v.end()});
void set(svgpp::tag::attribute::y y, double v);
void set(svgpp::tag::attribute::width width, double v);
void set(svgpp::tag::attribute::height height, double v);
+ void set(svgpp::tag::attribute::id a, boost::iterator_range<const char *> v);
void set(svgpp::tag::attribute::class_ a,
boost::iterator_range<const char *> v);
void set(svgpp::tag::attribute::viewBox a, double v1, double v2, double v3,
boost::mpl::set<svgpp::traits::shapes_attributes_by_element,
svgpp::tag::attribute::class_, //
svgpp::tag::attribute::height, //
+ svgpp::tag::attribute::id, //
svgpp::tag::attribute::viewBox, //
svgpp::tag::attribute::width //
>::type;
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] == "<g class=\"graph\">") {
+ if (recreated_svg_lines[i] == "<g id=\"graph0\" class=\"graph\">") {
// stop comparison here since we do not yet handle all attributes on the
// 'g' element
break;
// do some sanity checks of the parts of the recreated SVG that we cannot
// yet compare with the original SVG
- CHECK(recreated_svg.find("<g class=\"graph\">") != std::string::npos);
- CHECK(recreated_svg.find("<g class=\"node\">") != std::string::npos);
- CHECK(recreated_svg.find("<g class=\"edge\">") != std::string::npos);
+ CHECK(recreated_svg.find("<g id=\"graph0\" class=\"graph\">") !=
+ std::string::npos);
+ CHECK(recreated_svg.find("<g id=\"node1\" class=\"node\">") !=
+ std::string::npos);
+ CHECK(recreated_svg.find("<g id=\"node2\" class=\"node\">") !=
+ std::string::npos);
+ CHECK(recreated_svg.find("<g id=\"edge1\" class=\"edge\">") !=
+ std::string::npos);
CHECK(recreated_svg.find("</g>") != std::string::npos);
CHECK(recreated_svg.find("<title>g1</title>") != std::string::npos);
CHECK(recreated_svg.find("<title>a</title>") != std::string::npos);