| 1 |
//---------------------------------------------------------------------------- |
| 2 |
// Anti-Grain Geometry - Version 2.3 |
| 3 |
// Copyright (C) 2002-2005 Maxim Shemanarev (http://www.antigrain.com) |
| 4 |
// Copyright (c) 2008 Rene Rebe <[email protected]> [arc parser code] |
| 5 |
// Copyright (c) 2017 John Horigan <[email protected]> [align_subpath()] |
| 6 |
// |
| 7 |
// Permission to copy, use, modify, sell and distribute this software |
| 8 |
// is granted provided this copyright notice appears in all copies. |
| 9 |
// This software is provided "as is" without express or implied |
| 10 |
// warranty, and with no claim as to its suitability for any purpose. |
| 11 |
// |
| 12 |
//---------------------------------------------------------------------------- |
| 13 |
// Contact: [email protected] |
| 14 |
// [email protected] |
| 15 |
// http://www.antigrain.com |
| 16 |
//---------------------------------------------------------------------------- |
| 17 |
// |
| 18 |
// SVG path renderer. |
| 19 |
// |
| 20 |
//---------------------------------------------------------------------------- |
| 21 |
|
| 22 |
#include <stdio.h> |
| 23 |
#include <cmath> |
| 24 |
#include "agg_svg_path_renderer.h" |
| 25 |
|
| 26 |
namespace agg |
| 27 |
{ |
| 28 |
namespace svg |
| 29 |
{ |
| 30 |
|
| 31 |
//------------------------------------------------------------------------ |
| 32 |
path_renderer::path_renderer() : |
| 33 |
m_curved(m_storage), |
| 34 |
m_curved_count(m_curved), |
| 35 |
|
| 36 |
m_curved_stroked(m_curved_count), |
| 37 |
m_curved_stroked_trans(m_curved_stroked, m_transform), |
| 38 |
|
| 39 |
m_curved_trans(m_curved_count, m_transform), |
| 40 |
m_curved_trans_contour(m_curved_trans) |
| 41 |
{ |
| 42 |
m_curved_trans_contour.auto_detect_orientation(false); |
| 43 |
} |
| 44 |
|
| 45 |
|
| 46 |
//------------------------------------------------------------------------ |
| 47 |
void path_renderer::remove_all() |
| 48 |
{ |
| 49 |
m_storage.remove_all(); |
| 50 |
m_attr_storage.remove_all(); |
| 51 |
m_attr_stack.remove_all(); |
| 52 |
m_transform.reset(); |
| 53 |
} |
| 54 |
|
| 55 |
//------------------------------------------------------------------------ |
| 56 |
void path_renderer::begin_path() |
| 57 |
{ |
| 58 |
push_attr(); |
| 59 |
unsigned idx = m_storage.start_new_path(); |
| 60 |
m_attr_storage.add(path_attributes(cur_attr(), idx)); |
| 61 |
} |
| 62 |
|
| 63 |
//------------------------------------------------------------------------ |
| 64 |
void path_renderer::end_path() |
| 65 |
{ |
| 66 |
if(m_attr_storage.size() == 0) |
| 67 |
{ |
| 68 |
throw exception("end_path : The path was not begun"); |
| 69 |
} |
| 70 |
path_attributes attr = cur_attr(); |
| 71 |
unsigned idx = m_attr_storage[m_attr_storage.size() - 1].index; |
| 72 |
attr.index = idx; |
| 73 |
m_attr_storage[m_attr_storage.size() - 1] = attr; |
| 74 |
pop_attr(); |
| 75 |
} |
| 76 |
|
| 77 |
//------------------------------------------------------------------------ |
| 78 |
void path_renderer::move_to(double x, double y, bool rel) // M, m |
| 79 |
{ |
| 80 |
if(rel) m_storage.rel_to_abs(&x, &y); |
| 81 |
m_storage.move_to(x, y); |
| 82 |
} |
| 83 |
|
| 84 |
//------------------------------------------------------------------------ |
| 85 |
void path_renderer::line_to(double x, double y, bool rel) // L, l |
| 86 |
{ |
| 87 |
if(rel) m_storage.rel_to_abs(&x, &y); |
| 88 |
m_storage.line_to(x, y); |
| 89 |
} |
| 90 |
|
| 91 |
//------------------------------------------------------------------------ |
| 92 |
void path_renderer::hline_to(double x, bool rel) // H, h |
| 93 |
{ |
| 94 |
double x2 = 0.0; |
| 95 |
double y2 = 0.0; |
| 96 |
if(m_storage.total_vertices()) |
| 97 |
{ |
| 98 |
m_storage.vertex(m_storage.total_vertices() - 1, &x2, &y2); |
| 99 |
if(rel) x += x2; |
| 100 |
m_storage.line_to(x, y2); |
| 101 |
} |
| 102 |
} |
| 103 |
|
| 104 |
//------------------------------------------------------------------------ |
| 105 |
void path_renderer::vline_to(double y, bool rel) // V, v |
| 106 |
{ |
| 107 |
double x2 = 0.0; |
| 108 |
double y2 = 0.0; |
| 109 |
if(m_storage.total_vertices()) |
| 110 |
{ |
| 111 |
m_storage.vertex(m_storage.total_vertices() - 1, &x2, &y2); |
| 112 |
if(rel) y += y2; |
| 113 |
m_storage.line_to(x2, y); |
| 114 |
} |
| 115 |
} |
| 116 |
|
| 117 |
//------------------------------------------------------------------------ |
| 118 |
void path_renderer::curve3(double x1, double y1, // Q, q |
| 119 |
double x, double y, bool rel) |
| 120 |
{ |
| 121 |
if(rel) |
| 122 |
{ |
| 123 |
m_storage.rel_to_abs(&x1, &y1); |
| 124 |
m_storage.rel_to_abs(&x, &y); |
| 125 |
} |
| 126 |
m_storage.curve3(x1, y1, x, y); |
| 127 |
} |
| 128 |
|
| 129 |
//------------------------------------------------------------------------ |
| 130 |
void path_renderer::curve3(double x, double y, bool rel) // T, t |
| 131 |
{ |
| 132 |
if(rel) |
| 133 |
{ |
| 134 |
m_storage.curve3_rel(x, y); |
| 135 |
} else |
| 136 |
{ |
| 137 |
m_storage.curve3(x, y); |
| 138 |
} |
| 139 |
} |
| 140 |
|
| 141 |
//------------------------------------------------------------------------ |
| 142 |
void path_renderer::curve4(double x1, double y1, // C, c |
| 143 |
double x2, double y2, |
| 144 |
double x, double y, bool rel) |
| 145 |
{ |
| 146 |
if(rel) |
| 147 |
{ |
| 148 |
m_storage.rel_to_abs(&x1, &y1); |
| 149 |
m_storage.rel_to_abs(&x2, &y2); |
| 150 |
m_storage.rel_to_abs(&x, &y); |
| 151 |
} |
| 152 |
m_storage.curve4(x1, y1, x2, y2, x, y); |
| 153 |
} |
| 154 |
|
| 155 |
//------------------------------------------------------------------------ |
| 156 |
void path_renderer::curve4(double x2, double y2, // S, s |
| 157 |
double x, double y, bool rel) |
| 158 |
{ |
| 159 |
if(rel) |
| 160 |
{ |
| 161 |
m_storage.curve4_rel(x2, y2, x, y); |
| 162 |
} else |
| 163 |
{ |
| 164 |
m_storage.curve4(x2, y2, x, y); |
| 165 |
} |
| 166 |
} |
| 167 |
|
| 168 |
//------------------------------------------------------------------------ |
| 169 |
void path_renderer::arc(double rx, double ry, |
| 170 |
double angle, |
| 171 |
bool large_arc_flag, |
| 172 |
bool sweep_flag, |
| 173 |
double x, double y, bool rel) |
| 174 |
{ |
| 175 |
angle = deg2rad (angle); |
| 176 |
if(rel) |
| 177 |
{ |
| 178 |
m_storage.arc_rel(rx, ry, angle, large_arc_flag, sweep_flag, x, y); |
| 179 |
} else |
| 180 |
{ |
| 181 |
m_storage.arc_to(rx, ry, angle, large_arc_flag, sweep_flag, x, y); |
| 182 |
} |
| 183 |
} |
| 184 |
|
| 185 |
//------------------------------------------------------------------------ |
| 186 |
void path_renderer::close_subpath() |
| 187 |
{ |
| 188 |
m_storage.end_poly(path_flags_close); |
| 189 |
} |
| 190 |
|
| 191 |
//------------------------------------------------------------------------ |
| 192 |
// If the end points of a subpath are very, very close then make them |
| 193 |
// exactly equal so that AGG is not confused. |
| 194 |
//------------------------------------------------------------------------ |
| 195 |
void path_renderer::align_subpath(unsigned start_idx) |
| 196 |
{ |
| 197 |
m_storage.align_path(start_idx); |
| 198 |
} |
| 199 |
|
| 200 |
//------------------------------------------------------------------------ |
| 201 |
path_attributes& path_renderer::cur_attr() |
| 202 |
{ |
| 203 |
if(m_attr_stack.size() == 0) |
| 204 |
{ |
| 205 |
throw exception("cur_attr : Attribute stack is empty"); |
| 206 |
} |
| 207 |
return m_attr_stack[m_attr_stack.size() - 1]; |
| 208 |
} |
| 209 |
|
| 210 |
//------------------------------------------------------------------------ |
| 211 |
void path_renderer::push_attr() |
| 212 |
{ |
| 213 |
m_attr_stack.add(m_attr_stack.size() ? |
| 214 |
m_attr_stack[m_attr_stack.size() - 1] : |
| 215 |
path_attributes()); |
| 216 |
} |
| 217 |
|
| 218 |
//------------------------------------------------------------------------ |
| 219 |
void path_renderer::pop_attr() |
| 220 |
{ |
| 221 |
if(m_attr_stack.size() == 0) |
| 222 |
{ |
| 223 |
throw exception("pop_attr : Attribute stack is empty"); |
| 224 |
} |
| 225 |
m_attr_stack.remove_last(); |
| 226 |
} |
| 227 |
|
| 228 |
//------------------------------------------------------------------------ |
| 229 |
void path_renderer::fill(const rgba8& f) |
| 230 |
{ |
| 231 |
path_attributes& attr = cur_attr(); |
| 232 |
attr.fill_color = f; |
| 233 |
attr.fill_flag = true; |
| 234 |
} |
| 235 |
|
| 236 |
//------------------------------------------------------------------------ |
| 237 |
void path_renderer::stroke(const rgba8& s) |
| 238 |
{ |
| 239 |
path_attributes& attr = cur_attr(); |
| 240 |
attr.stroke_color = s; |
| 241 |
attr.stroke_flag = true; |
| 242 |
} |
| 243 |
|
| 244 |
//------------------------------------------------------------------------ |
| 245 |
void path_renderer::even_odd(bool flag) |
| 246 |
{ |
| 247 |
cur_attr().even_odd_flag = flag; |
| 248 |
} |
| 249 |
|
| 250 |
//------------------------------------------------------------------------ |
| 251 |
void path_renderer::stroke_width(double w) |
| 252 |
{ |
| 253 |
cur_attr().stroke_width = w; |
| 254 |
} |
| 255 |
|
| 256 |
//------------------------------------------------------------------------ |
| 257 |
void path_renderer::fill_none() |
| 258 |
{ |
| 259 |
cur_attr().fill_flag = false; |
| 260 |
} |
| 261 |
|
| 262 |
//------------------------------------------------------------------------ |
| 263 |
void path_renderer::stroke_none() |
| 264 |
{ |
| 265 |
cur_attr().stroke_flag = false; |
| 266 |
} |
| 267 |
|
| 268 |
//------------------------------------------------------------------------ |
| 269 |
void path_renderer::fill_opacity(double op) |
| 270 |
{ |
| 271 |
cur_attr().fill_color.opacity(op); |
| 272 |
} |
| 273 |
|
| 274 |
//------------------------------------------------------------------------ |
| 275 |
void path_renderer::stroke_opacity(double op) |
| 276 |
{ |
| 277 |
cur_attr().stroke_color.opacity(op); |
| 278 |
} |
| 279 |
|
| 280 |
//------------------------------------------------------------------------ |
| 281 |
void path_renderer::line_join(line_join_e join) |
| 282 |
{ |
| 283 |
cur_attr().line_join = join; |
| 284 |
} |
| 285 |
|
| 286 |
//------------------------------------------------------------------------ |
| 287 |
void path_renderer::line_cap(line_cap_e cap) |
| 288 |
{ |
| 289 |
cur_attr().line_cap = cap; |
| 290 |
} |
| 291 |
|
| 292 |
//------------------------------------------------------------------------ |
| 293 |
void path_renderer::miter_limit(double ml) |
| 294 |
{ |
| 295 |
cur_attr().miter_limit = ml; |
| 296 |
} |
| 297 |
|
| 298 |
//------------------------------------------------------------------------ |
| 299 |
trans_affine& path_renderer::transform() |
| 300 |
{ |
| 301 |
return cur_attr().transform; |
| 302 |
} |
| 303 |
|
| 304 |
//------------------------------------------------------------------------ |
| 305 |
void path_renderer::parse_path(path_tokenizer& tok) |
| 306 |
{ |
| 307 |
unsigned move_idx = 0; |
| 308 |
while(tok.next()) |
| 309 |
{ |
| 310 |
double arg[10]; |
| 311 |
char cmd = tok.last_command(); |
| 312 |
if (m_storage.total_vertices() == 0 && !(cmd == 'M' || cmd == 'm')) |
| 313 |
{ |
| 314 |
throw exception("path element data attributes must begin with a 'move to'"); |
| 315 |
} |
| 316 |
unsigned i; |
| 317 |
switch(cmd) |
| 318 |
{ |
| 319 |
case 'M': case 'm': |
| 320 |
align_subpath(move_idx); |
| 321 |
arg[0] = tok.last_number(); |
| 322 |
arg[1] = tok.next(cmd); |
| 323 |
move_to(arg[0], arg[1], cmd == 'm'); |
| 324 |
move_idx = m_storage.total_vertices() - 1; |
| 325 |
break; |
| 326 |
|
| 327 |
case 'L': case 'l': |
| 328 |
arg[0] = tok.last_number(); |
| 329 |
arg[1] = tok.next(cmd); |
| 330 |
line_to(arg[0], arg[1], cmd == 'l'); |
| 331 |
break; |
| 332 |
|
| 333 |
case 'V': case 'v': |
| 334 |
vline_to(tok.last_number(), cmd == 'v'); |
| 335 |
break; |
| 336 |
|
| 337 |
case 'H': case 'h': |
| 338 |
hline_to(tok.last_number(), cmd == 'h'); |
| 339 |
break; |
| 340 |
|
| 341 |
case 'Q': case 'q': |
| 342 |
arg[0] = tok.last_number(); |
| 343 |
for(i = 1; i < 4; i++) |
| 344 |
{ |
| 345 |
arg[i] = tok.next(cmd); |
| 346 |
} |
| 347 |
curve3(arg[0], arg[1], arg[2], arg[3], cmd == 'q'); |
| 348 |
break; |
| 349 |
|
| 350 |
case 'T': case 't': |
| 351 |
arg[0] = tok.last_number(); |
| 352 |
arg[1] = tok.next(cmd); |
| 353 |
curve3(arg[0], arg[1], cmd == 't'); |
| 354 |
break; |
| 355 |
|
| 356 |
case 'C': case 'c': |
| 357 |
arg[0] = tok.last_number(); |
| 358 |
for(i = 1; i < 6; i++) |
| 359 |
{ |
| 360 |
arg[i] = tok.next(cmd); |
| 361 |
} |
| 362 |
curve4(arg[0], arg[1], arg[2], arg[3], arg[4], arg[5], cmd == 'c'); |
| 363 |
break; |
| 364 |
|
| 365 |
case 'S': case 's': |
| 366 |
arg[0] = tok.last_number(); |
| 367 |
for(i = 1; i < 4; i++) |
| 368 |
{ |
| 369 |
arg[i] = tok.next(cmd); |
| 370 |
} |
| 371 |
curve4(arg[0], arg[1], arg[2], arg[3], cmd == 's'); |
| 372 |
break; |
| 373 |
|
| 374 |
case 'A': case 'a': |
| 375 |
arg[0] = tok.last_number(); |
| 376 |
for(i = 1; i < 7; ++i) |
| 377 |
{ |
| 378 |
arg[i] = tok.next(cmd); |
| 379 |
} |
| 380 |
arc(arg[0], arg[1], arg[2], arg[3], arg[4], arg[5], arg[6], |
| 381 |
cmd == 'a'); |
| 382 |
break; |
| 383 |
|
| 384 |
case 'Z': case 'z': |
| 385 |
align_subpath(move_idx); |
| 386 |
close_subpath(); |
| 387 |
break; |
| 388 |
|
| 389 |
default: |
| 390 |
{ |
| 391 |
char buf[100]; |
| 392 |
sprintf(buf, "parse_path: Invalid Command %c", cmd); |
| 393 |
throw exception(buf); |
| 394 |
} |
| 395 |
} |
| 396 |
} |
| 397 |
align_subpath(move_idx); |
| 398 |
m_storage.start_new_path(); |
| 399 |
} |
| 400 |
|
| 401 |
} |
| 402 |
} |
| 403 |
|