| 1 |
#pragma once |
| 2 |
|
| 3 |
#include <cmath> |
| 4 |
#include <format> |
| 5 |
#include <optional> |
| 6 |
#include <stdexcept> |
| 7 |
#include <string> |
| 8 |
#include <string_view> |
| 9 |
#include <vector> |
| 10 |
|
| 11 |
namespace SVG { |
| 12 |
|
| 13 |
struct SVGPoint { |
| 14 |
double x; |
| 15 |
double y; |
| 16 |
bool operator==(const SVGPoint &rhs) const; |
| 17 |
bool operator!=(const SVGPoint &rhs) const; |
| 18 |
bool is_higher_than(const SVGPoint &other) const; |
| 19 |
bool is_lower_than(const SVGPoint &other) const; |
| 20 |
bool is_more_left_than(const SVGPoint &other) const; |
| 21 |
bool is_more_right_than(const SVGPoint &other) const; |
| 22 |
}; |
| 23 |
|
| 24 |
using SVGPoints = std::vector<SVGPoint>; |
| 25 |
|
| 26 |
struct SVGLine { |
| 27 |
double x1; |
| 28 |
double y1; |
| 29 |
double x2; |
| 30 |
double y2; |
| 31 |
}; |
| 32 |
|
| 33 |
struct SVGRect { |
| 34 |
double x; |
| 35 |
double y; |
| 36 |
double width; |
| 37 |
double height; |
| 38 |
SVGPoint center() const; |
| 39 |
SVGRect intersection(SVGRect other) const; |
| 40 |
void extend(const SVGPoint &point); |
| 41 |
void extend(const SVGRect &other); |
| 42 |
}; |
| 43 |
|
| 44 |
struct SVGMatrix { |
| 45 |
double a; |
| 46 |
double b; |
| 47 |
double c; |
| 48 |
double d; |
| 49 |
double e; |
| 50 |
double f; |
| 51 |
}; |
| 52 |
|
| 53 |
enum class SVGElementType { |
| 54 |
Circle, |
| 55 |
Ellipse, |
| 56 |
Group, |
| 57 |
Line, |
| 58 |
Path, |
| 59 |
Polygon, |
| 60 |
Polyline, |
| 61 |
Rect, |
| 62 |
Svg, |
| 63 |
Text, |
| 64 |
Title, |
| 65 |
}; |
| 66 |
|
| 67 |
std::string_view tag(SVG::SVGElementType elem_type); |
| 68 |
|
| 69 |
struct SVGAttributes { |
| 70 |
std::string class_; |
| 71 |
double cx; |
| 72 |
double cy; |
| 73 |
std::string fill; |
| 74 |
double fill_opacity = 1; |
| 75 |
std::string font_family; |
| 76 |
double font_size; |
| 77 |
double height; |
| 78 |
std::string id; |
| 79 |
SVGPoints points; |
| 80 |
double rx; |
| 81 |
double ry; |
| 82 |
std::string stroke; |
| 83 |
double stroke_opacity = 1; |
| 84 |
double stroke_width = 1; |
| 85 |
std::string text_anchor; |
| 86 |
std::optional<SVGMatrix> transform; |
| 87 |
SVGRect viewBox; |
| 88 |
double width; |
| 89 |
double x; |
| 90 |
double y; |
| 91 |
}; |
| 92 |
|
| 93 |
std::string to_dot_color(const std::string &color, double opacity = 1.0); |
| 94 |
|
| 95 |
/** |
| 96 |
* @brief The SVGElement class represents an SVG element |
| 97 |
*/ |
| 98 |
|
| 99 |
class SVGElement { |
| 100 |
public: |
| 101 |
SVGElement() = delete; |
| 102 |
explicit SVGElement(SVG::SVGElementType etype); |
| 103 |
|
| 104 |
/// Add an SVG `rect` element representing the bounding box of the edge to the |
| 105 |
/// element |
| 106 |
void add_bbox(); |
| 107 |
/// Add an SVG `rect` element representing the outline bounding box of the |
| 108 |
/// edge to the element. The outline bounding box is the bounding box with |
| 109 |
/// stroke width taken into account. |
| 110 |
void add_outline_bbox(); |
| 111 |
/// Add an SVG `rect` element representing the overlap between the outline |
| 112 |
/// bounding box of the element and another element. The outline bounding box |
| 113 |
/// is the bounding box with penwidth taken into account. |
| 114 |
void add_outline_overlap_bbox(SVG::SVGElement other, double tolerance = 0); |
| 115 |
/// Add an SVG `rect` element as a child to the element |
| 116 |
void add_rect(SVG::SVGRect rect, std::string color); |
| 117 |
/// \brief Return the value of an attribute retrieved from the element and its |
| 118 |
/// children |
| 119 |
/// |
| 120 |
/// @param attribute_ptr pointer to a member of the `SVGAttributes` class. |
| 121 |
/// @param predicate pointer to a member function of the `SVGElement` class |
| 122 |
/// which returns `true` for elements on which the attribute exists. |
| 123 |
/// @param default_value the value to return if the attribute does not exist |
| 124 |
/// on any of the elements. |
| 125 |
/// |
| 126 |
/// Returns the value of the attribute if it has the same value on all |
| 127 |
/// elements on which it exists and throws an exception if the value is not |
| 128 |
/// consistent on those elements. Returns the default value if the attribute |
| 129 |
/// does not exist on any of the elements. |
| 130 |
template <typename T> |
| 131 |
T attribute_from_subtree(T SVGAttributes::*attribute_ptr, |
| 132 |
bool (SVGElement::*predicate)() const, |
| 133 |
T default_value) const { |
| 134 |
std::optional<T> attribute; |
| 135 |
if ((this->*predicate)()) { |
| 136 |
attribute = this->attributes.*attribute_ptr; |
| 137 |
} |
| 138 |
for (const auto &child : children) { |
| 139 |
if (!(child.*predicate)()) { |
| 140 |
continue; |
| 141 |
} |
| 142 |
const auto child_attribute = child.attributes.*attribute_ptr; |
| 143 |
if (!attribute.has_value()) { |
| 144 |
attribute = child_attribute; |
| 145 |
continue; |
| 146 |
} |
| 147 |
if (attribute.value() != child_attribute) { |
| 148 |
throw std::runtime_error{std::format( |
| 149 |
"Inconsistent value of attribute: current {}: {}, child {}: {}", |
| 150 |
tag(elem_type), attribute.value(), tag(child.elem_type), |
| 151 |
child_attribute)}; |
| 152 |
} |
| 153 |
} |
| 154 |
return attribute.value_or(default_value); |
| 155 |
} |
| 156 |
/// Return the bounding box of the element and its children. The bounding box |
| 157 |
/// is calculated and stored the first time this function is called and later |
| 158 |
/// calls will return the already calculated value. If this function is called |
| 159 |
/// for an SVG element for which the bounding box is not defined, it will |
| 160 |
/// throw an exception unless the `throw_if_bbox_not_defined` parameter is |
| 161 |
/// `false`. |
| 162 |
SVG::SVGRect bbox(bool throw_if_bbox_not_defined = true); |
| 163 |
/// Return the element of the given type with the specified index found among |
| 164 |
/// the element's children. Throwns an exception if there's no such element |
| 165 |
/// with the specified index. |
| 166 |
SVG::SVGElement &find_child(SVG::SVGElementType etype, std::size_t index = 0); |
| 167 |
bool is_closed_shape_element() const; |
| 168 |
bool is_shape_element() const; |
| 169 |
/// Return the outline bounding box of the element. The outline bounding box |
| 170 |
/// is the bounding box with penwidth taken into account. If this function is |
| 171 |
/// called for an SVG element for which the bounding box is not defined, it |
| 172 |
/// will throw an exception unless the `throw_if_bbox_not_defined` parameter |
| 173 |
/// is `false`. |
| 174 |
SVG::SVGRect outline_bbox(bool throw_if_bbox_not_defined = true); |
| 175 |
std::string to_string(std::size_t indent_size) const; |
| 176 |
|
| 177 |
SVGAttributes attributes; |
| 178 |
/// The Graphviz build date |
| 179 |
std::string graphviz_build_date; |
| 180 |
std::vector<SVGElement> children; |
| 181 |
/// The `graph_id`, `node_id` or `edgeop` according to the DOT language |
| 182 |
/// specification. Note that this is not the same as the `id` attribute of the |
| 183 |
/// SVG element |
| 184 |
std::string graphviz_id; |
| 185 |
/// The Graphviz release version |
| 186 |
std::string graphviz_version; |
| 187 |
/// The points given by the `d` attribute of a path element |
| 188 |
SVGPoints path_points; |
| 189 |
/// The SVG element text node contents. Not to be confused with an SVG `text` |
| 190 |
/// element |
| 191 |
std::string text; |
| 192 |
/// The type of SVG element |
| 193 |
const SVGElementType elem_type; |
| 194 |
|
| 195 |
private: |
| 196 |
/// append a string possibly containing an attribute to another string, |
| 197 |
/// handling space separation |
| 198 |
void append_attribute(std::string &output, |
| 199 |
const std::string &attribute) const; |
| 200 |
/// calculate a point along a cubic Bezier curve |
| 201 |
SVG::SVGPoint cubic_bezier(double t); |
| 202 |
// Return true if the points of a polygon are defined clockwise |
| 203 |
bool has_clockwise_points() const; |
| 204 |
bool has_all_points_equal() const; |
| 205 |
std::string id_attribute_to_string() const; |
| 206 |
std::string fill_attribute_to_string() const; |
| 207 |
std::string fill_opacity_attribute_to_string() const; |
| 208 |
/// Compute the stroke shape miter point according to |
| 209 |
/// https://www.w3.org/TR/SVG2/painting.html#StrokeShape. |
| 210 |
SVG::SVGPoints miter_shape(SVG::SVGPoint segment_start, |
| 211 |
SVG::SVGPoint segment_end, |
| 212 |
SVG::SVGPoint following_segment_end) const; |
| 213 |
std::string points_attribute_to_string() const; |
| 214 |
std::string stroke_attribute_to_string() const; |
| 215 |
std::string stroke_opacity_attribute_to_string() const; |
| 216 |
std::string stroke_width_attribute_to_string() const; |
| 217 |
std::string stroke_to_graphviz_color(const std::string &color) const; |
| 218 |
SVG::SVGRect text_bbox() const; |
| 219 |
void to_string_impl(std::string &output, std::size_t indent_size, |
| 220 |
std::size_t current_indent) const; |
| 221 |
|
| 222 |
/// The bounding box of the element and its children. Stored the first time |
| 223 |
/// it's computed |
| 224 |
std::optional<SVG::SVGRect> m_bbox; |
| 225 |
std::optional<SVG::SVGRect> m_outline_bbox; |
| 226 |
}; |
| 227 |
|
| 228 |
} // namespace SVG |