| 1 |
#include <any> |
| 2 |
#include <format> |
| 3 |
#include <stdexcept> |
| 4 |
|
| 5 |
#include <boost/array.hpp> |
| 6 |
|
| 7 |
#include "svg_analyzer_interface.h" |
| 8 |
#include "svgpp_context.h" |
| 9 |
|
| 10 |
static std::string to_color_string(SvgppContext::color_t color) { |
| 11 |
return std::format("rgb({},{},{})", (color >> 16) & 0xff, (color >> 8) & 0xff, |
| 12 |
(color >> 0) & 0xff); |
| 13 |
} |
| 14 |
|
| 15 |
SvgppContext::SvgppContext(ISVGAnalyzer *svg_analyzer) |
| 16 |
: m_svg_analyzer(svg_analyzer) {}; |
| 17 |
|
| 18 |
void SvgppContext::on_enter_element(svgpp::tag::element::svg) { |
| 19 |
m_svg_analyzer->on_enter_element_svg(); |
| 20 |
} |
| 21 |
|
| 22 |
void SvgppContext::on_enter_element(svgpp::tag::element::g) { |
| 23 |
m_svg_analyzer->on_enter_element_g(); |
| 24 |
} |
| 25 |
|
| 26 |
void SvgppContext::on_enter_element(svgpp::tag::element::circle) { |
| 27 |
m_svg_analyzer->on_enter_element_circle(); |
| 28 |
} |
| 29 |
|
| 30 |
void SvgppContext::on_enter_element(svgpp::tag::element::ellipse) { |
| 31 |
m_svg_analyzer->on_enter_element_ellipse(); |
| 32 |
} |
| 33 |
|
| 34 |
void SvgppContext::on_enter_element(svgpp::tag::element::line) { |
| 35 |
m_svg_analyzer->on_enter_element_line(); |
| 36 |
} |
| 37 |
|
| 38 |
void SvgppContext::on_enter_element(svgpp::tag::element::path) { |
| 39 |
m_svg_analyzer->on_enter_element_path(); |
| 40 |
} |
| 41 |
|
| 42 |
void SvgppContext::on_enter_element(svgpp::tag::element::polygon) { |
| 43 |
m_svg_analyzer->on_enter_element_polygon(); |
| 44 |
} |
| 45 |
|
| 46 |
void SvgppContext::on_enter_element(svgpp::tag::element::polyline) { |
| 47 |
m_svg_analyzer->on_enter_element_polyline(); |
| 48 |
} |
| 49 |
|
| 50 |
void SvgppContext::on_enter_element(svgpp::tag::element::rect) { |
| 51 |
m_svg_analyzer->on_enter_element_rect(); |
| 52 |
} |
| 53 |
|
| 54 |
void SvgppContext::on_enter_element(svgpp::tag::element::text) { |
| 55 |
m_svg_analyzer->on_enter_element_text(); |
| 56 |
} |
| 57 |
|
| 58 |
void SvgppContext::on_enter_element(svgpp::tag::element::title) { |
| 59 |
m_svg_analyzer->on_enter_element_title(); |
| 60 |
} |
| 61 |
|
| 62 |
void SvgppContext::on_exit_element() { m_svg_analyzer->on_exit_element(); } |
| 63 |
|
| 64 |
void SvgppContext::path_move_to(double x, double y, |
| 65 |
svgpp::tag::coordinate::absolute c) { |
| 66 |
if (!c.is_absolute) { |
| 67 |
throw std::runtime_error{ |
| 68 |
"'path_move_to' using relative coordinates is not yet implemented"}; |
| 69 |
} |
| 70 |
m_svg_analyzer->path_move_to(x, y); |
| 71 |
} |
| 72 |
|
| 73 |
void SvgppContext::path_line_to(double, double, |
| 74 |
svgpp::tag::coordinate::absolute) { |
| 75 |
throw std::runtime_error{"'path_line_to' is not yet implemented"}; |
| 76 |
} |
| 77 |
|
| 78 |
void SvgppContext::path_cubic_bezier_to(double x1, double y1, double x2, |
| 79 |
double y2, double x, double y, |
| 80 |
svgpp::tag::coordinate::absolute c) { |
| 81 |
if (!c.is_absolute) { |
| 82 |
throw std::runtime_error{ |
| 83 |
"'path_cubic_bezier_to' using relative coordinates " |
| 84 |
"is not yet implemented"}; |
| 85 |
} |
| 86 |
m_svg_analyzer->path_cubic_bezier_to(x1, y1, x2, y2, x, y); |
| 87 |
} |
| 88 |
|
| 89 |
void SvgppContext::path_quadratic_bezier_to(double, double, double, double, |
| 90 |
svgpp::tag::coordinate::absolute) { |
| 91 |
throw std::runtime_error{"'path_quadratic_bezier_to' is not yet implemented"}; |
| 92 |
} |
| 93 |
|
| 94 |
void SvgppContext::path_elliptical_arc_to(double, double, double, bool, bool, |
| 95 |
double, double, |
| 96 |
svgpp::tag::coordinate::absolute) { |
| 97 |
throw std::runtime_error{"'path_elliptical_arc_to' is not yet implemented"}; |
| 98 |
} |
| 99 |
|
| 100 |
void SvgppContext::path_close_subpath() { |
| 101 |
throw std::runtime_error{"'path_close_subpath' is not yet implemented"}; |
| 102 |
} |
| 103 |
|
| 104 |
void SvgppContext::path_exit() { m_svg_analyzer->path_exit(); } |
| 105 |
|
| 106 |
void SvgppContext::set(svgpp::tag::attribute::cy, const double v) { |
| 107 |
m_svg_analyzer->set_cy(v); |
| 108 |
} |
| 109 |
|
| 110 |
void SvgppContext::set(svgpp::tag::attribute::cx, const double v) { |
| 111 |
m_svg_analyzer->set_cx(v); |
| 112 |
} |
| 113 |
|
| 114 |
void SvgppContext::set(svgpp::tag::attribute::fill, svgpp::tag::value::none) { |
| 115 |
m_svg_analyzer->set_fill("none"); |
| 116 |
} |
| 117 |
|
| 118 |
void SvgppContext::set(svgpp::tag::attribute::fill, |
| 119 |
svgpp::tag::value::currentColor) { |
| 120 |
throw std::runtime_error{ |
| 121 |
"the 'fill' attribute 'currentColor' value is not yet implemented"}; |
| 122 |
} |
| 123 |
|
| 124 |
void SvgppContext::set(svgpp::tag::attribute::fill, color_t color, |
| 125 |
svgpp::tag::skip_icc_color) { |
| 126 |
m_svg_analyzer->set_fill(to_color_string(color)); |
| 127 |
} |
| 128 |
|
| 129 |
void SvgppContext::set(svgpp::tag::attribute::fill_opacity, const double v) { |
| 130 |
m_svg_analyzer->set_fill_opacity(v); |
| 131 |
} |
| 132 |
|
| 133 |
void SvgppContext::set(svgpp::tag::attribute::stroke, svgpp::tag::value::none) { |
| 134 |
m_svg_analyzer->set_stroke("none"); |
| 135 |
} |
| 136 |
|
| 137 |
void SvgppContext::set(svgpp::tag::attribute::stroke, |
| 138 |
svgpp::tag::value::currentColor) { |
| 139 |
throw std::runtime_error{ |
| 140 |
"the 'stroke' attribute 'currentColor' value is not yet implemented"}; |
| 141 |
} |
| 142 |
|
| 143 |
void SvgppContext::set(svgpp::tag::attribute::stroke, |
| 144 |
SvgppContext::color_t color, |
| 145 |
svgpp::tag::skip_icc_color) { |
| 146 |
m_svg_analyzer->set_stroke(to_color_string(color)); |
| 147 |
} |
| 148 |
|
| 149 |
void SvgppContext::set(svgpp::tag::attribute::stroke_opacity, const double v) { |
| 150 |
m_svg_analyzer->set_stroke_opacity(v); |
| 151 |
} |
| 152 |
|
| 153 |
void SvgppContext::set(svgpp::tag::attribute::stroke_width, const double v) { |
| 154 |
m_svg_analyzer->set_stroke_width(v); |
| 155 |
} |
| 156 |
|
| 157 |
void SvgppContext::transform_matrix(const boost::array<double, 6> &matrix) { |
| 158 |
double a = matrix.at(0); |
| 159 |
double b = matrix.at(1); |
| 160 |
double c = matrix.at(2); |
| 161 |
double d = matrix.at(3); |
| 162 |
double e = matrix.at(4); |
| 163 |
double f = matrix.at(5); |
| 164 |
m_svg_analyzer->set_transform(a, b, c, d, e, f); |
| 165 |
} |
| 166 |
|
| 167 |
void SvgppContext::set(svgpp::tag::attribute::r, const double) { |
| 168 |
throw std::runtime_error{"the 'r' attribute is not yet implemented"}; |
| 169 |
} |
| 170 |
|
| 171 |
void SvgppContext::set(svgpp::tag::attribute::rx, const double v) { |
| 172 |
m_svg_analyzer->set_rx(v); |
| 173 |
} |
| 174 |
|
| 175 |
void SvgppContext::set(svgpp::tag::attribute::ry, const double v) { |
| 176 |
m_svg_analyzer->set_ry(v); |
| 177 |
} |
| 178 |
|
| 179 |
void SvgppContext::set(svgpp::tag::attribute::x1, const double) { |
| 180 |
throw std::runtime_error{"the 'x1' attribute is not yet implemented"}; |
| 181 |
} |
| 182 |
|
| 183 |
void SvgppContext::set(svgpp::tag::attribute::y1, const double) { |
| 184 |
throw std::runtime_error{"the 'y1' attribute is not yet implemented"}; |
| 185 |
} |
| 186 |
|
| 187 |
void SvgppContext::set(svgpp::tag::attribute::x2, const double) { |
| 188 |
throw std::runtime_error{"the 'x2' attribute is not yet implemented"}; |
| 189 |
} |
| 190 |
|
| 191 |
void SvgppContext::set(svgpp::tag::attribute::y2, const double) { |
| 192 |
throw std::runtime_error{"the 'y2' attribute is not yet implemented"}; |
| 193 |
} |
| 194 |
|
| 195 |
void SvgppContext::set(svgpp::tag::attribute::x, const double v) { |
| 196 |
m_svg_analyzer->set_x(v); |
| 197 |
} |
| 198 |
|
| 199 |
void SvgppContext::set(svgpp::tag::attribute::x, const NumbersRange &range) { |
| 200 |
if (boost::size(range) != 1) { |
| 201 |
throw std::runtime_error{ |
| 202 |
"Multiple value list for the 'x' attribute is not yet implemented"}; |
| 203 |
} |
| 204 |
m_svg_analyzer->set_x(*range.begin()); |
| 205 |
} |
| 206 |
|
| 207 |
void SvgppContext::set(svgpp::tag::attribute::y, const double v) { |
| 208 |
m_svg_analyzer->set_y(v); |
| 209 |
} |
| 210 |
|
| 211 |
void SvgppContext::set(svgpp::tag::attribute::y, const NumbersRange &range) { |
| 212 |
if (boost::size(range) != 1) { |
| 213 |
throw std::runtime_error{ |
| 214 |
"Multiple value list for the 'y' attribute is not yet implemented"}; |
| 215 |
} |
| 216 |
m_svg_analyzer->set_y(*range.begin()); |
| 217 |
} |
| 218 |
|
| 219 |
void SvgppContext::set(svgpp::tag::attribute::width, const double v) { |
| 220 |
m_svg_analyzer->set_width(v); |
| 221 |
} |
| 222 |
|
| 223 |
void SvgppContext::set(svgpp::tag::attribute::height, const double v) { |
| 224 |
m_svg_analyzer->set_height(v); |
| 225 |
} |
| 226 |
|
| 227 |
void SvgppContext::set(svgpp::tag::attribute::id, |
| 228 |
boost::iterator_range<const char *> v) { |
| 229 |
m_svg_analyzer->set_id({v.begin(), v.end()}); |
| 230 |
} |
| 231 |
|
| 232 |
void SvgppContext::set(svgpp::tag::attribute::class_, |
| 233 |
boost::iterator_range<const char *> v) { |
| 234 |
m_svg_analyzer->set_class({v.begin(), v.end()}); |
| 235 |
} |
| 236 |
|
| 237 |
void SvgppContext::set(svgpp::tag::attribute::font_family, |
| 238 |
boost::iterator_range<const char *> v) { |
| 239 |
m_svg_analyzer->set_font_family({v.begin(), v.end()}); |
| 240 |
} |
| 241 |
|
| 242 |
void SvgppContext::set(svgpp::tag::attribute::font_size, const double v) { |
| 243 |
m_svg_analyzer->set_font_size(v); |
| 244 |
} |
| 245 |
|
| 246 |
void SvgppContext::set(svgpp::tag::attribute::font_size, |
| 247 |
svgpp::tag::value::xx_small) { |
| 248 |
throw std::runtime_error{ |
| 249 |
"the 'font_size' attribute 'xx_small' value is not yet implemented"}; |
| 250 |
} |
| 251 |
|
| 252 |
void SvgppContext::set(svgpp::tag::attribute::font_size, |
| 253 |
svgpp::tag::value::x_small) { |
| 254 |
throw std::runtime_error{ |
| 255 |
"the 'font_size' attribute 'x_small' value is not yet implemented"}; |
| 256 |
} |
| 257 |
|
| 258 |
void SvgppContext::set(svgpp::tag::attribute::font_size, |
| 259 |
svgpp::tag::value::smaller) { |
| 260 |
throw std::runtime_error{ |
| 261 |
"the 'font_size' attribute 'smaller' value is not yet implemented"}; |
| 262 |
} |
| 263 |
|
| 264 |
void SvgppContext::set(svgpp::tag::attribute::font_size, |
| 265 |
svgpp::tag::value::small) { |
| 266 |
throw std::runtime_error{ |
| 267 |
"the 'font_size' attribute 'small' value is not yet implemented"}; |
| 268 |
} |
| 269 |
|
| 270 |
void SvgppContext::set(svgpp::tag::attribute::font_size, |
| 271 |
svgpp::tag::value::medium) { |
| 272 |
throw std::runtime_error{ |
| 273 |
"the 'font_size' attribute 'medium' value is not yet implemented"}; |
| 274 |
} |
| 275 |
|
| 276 |
void SvgppContext::set(svgpp::tag::attribute::font_size, |
| 277 |
svgpp::tag::value::large) { |
| 278 |
throw std::runtime_error{ |
| 279 |
"the 'font_size' attribute 'large' value is not yet implemented"}; |
| 280 |
} |
| 281 |
|
| 282 |
void SvgppContext::set(svgpp::tag::attribute::font_size, |
| 283 |
svgpp::tag::value::larger) { |
| 284 |
throw std::runtime_error{ |
| 285 |
"the 'font_size' attribute 'larger' value is not yet implemented"}; |
| 286 |
} |
| 287 |
|
| 288 |
void SvgppContext::set(svgpp::tag::attribute::font_size, |
| 289 |
svgpp::tag::value::x_large) { |
| 290 |
throw std::runtime_error{ |
| 291 |
"the 'font_size' attribute 'x_large' value is not yet implemented"}; |
| 292 |
} |
| 293 |
|
| 294 |
void SvgppContext::set(svgpp::tag::attribute::font_size, |
| 295 |
svgpp::tag::value::xx_large) { |
| 296 |
throw std::runtime_error{ |
| 297 |
"the 'font_size' attribute 'xx_large' value is not yet implemented"}; |
| 298 |
} |
| 299 |
|
| 300 |
void SvgppContext::set(svgpp::tag::attribute::text_anchor, |
| 301 |
svgpp::tag::value::start) { |
| 302 |
m_svg_analyzer->set_text_anchor("start"); |
| 303 |
} |
| 304 |
|
| 305 |
void SvgppContext::set(svgpp::tag::attribute::text_anchor, |
| 306 |
svgpp::tag::value::middle) { |
| 307 |
m_svg_analyzer->set_text_anchor("middle"); |
| 308 |
} |
| 309 |
|
| 310 |
void SvgppContext::set(svgpp::tag::attribute::text_anchor, |
| 311 |
svgpp::tag::value::end) { |
| 312 |
m_svg_analyzer->set_text_anchor("end"); |
| 313 |
} |
| 314 |
|
| 315 |
void SvgppContext::set(svgpp::tag::attribute::viewBox, const double v1, |
| 316 |
const double v2, const double v3, const double v4) { |
| 317 |
m_svg_analyzer->set_viewBox(v1, v2, v3, v4); |
| 318 |
} |
| 319 |
|
| 320 |
void SvgppContext::set(svgpp::tag::attribute::points, |
| 321 |
const SvgppContext::PointsRange &range) { |
| 322 |
for (auto &it : range) { |
| 323 |
m_svg_analyzer->set_point(it); |
| 324 |
} |
| 325 |
} |
| 326 |
|
| 327 |
void SvgppContext::set_text(boost::iterator_range<const char *> v) { |
| 328 |
m_svg_analyzer->set_text({v.begin(), v.end()}); |
| 329 |
} |