Debian Codegraph @

examples/svg_viewer/agg_svg_parser.cpp

raw
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]> [ellipse and circle code]
5 //
6 // Permission to copy, use, modify, sell and distribute this software
7 // is granted provided this copyright notice appears in all copies.
8 // This software is provided "as is" without express or implied
9 // warranty, and with no claim as to its suitability for any purpose.
10 //
11 //----------------------------------------------------------------------------
12 // Contact: [email protected]
13 // [email protected]
14 // http://www.antigrain.com
15 //----------------------------------------------------------------------------
16 //
17 // SVG parser.
18 //
19 //----------------------------------------------------------------------------
20
21 #include <stdio.h>
22 #include <string.h>
23 #include <ctype.h>
24 #include "agg_svg_parser.h"
25 #include "expat.h"
26
27 namespace agg
28 {
29 namespace svg
30 {
31 struct named_color
32 {
33 char name[22];
34 int8u r, g, b, a;
35 };
36
37 named_color colors[] =
38 {
39 { "aliceblue",240,248,255, 255 },
40 { "antiquewhite",250,235,215, 255 },
41 { "aqua",0,255,255, 255 },
42 { "aquamarine",127,255,212, 255 },
43 { "azure",240,255,255, 255 },
44 { "beige",245,245,220, 255 },
45 { "bisque",255,228,196, 255 },
46 { "black",0,0,0, 255 },
47 { "blanchedalmond",255,235,205, 255 },
48 { "blue",0,0,255, 255 },
49 { "blueviolet",138,43,226, 255 },
50 { "brown",165,42,42, 255 },
51 { "burlywood",222,184,135, 255 },
52 { "cadetblue",95,158,160, 255 },
53 { "chartreuse",127,255,0, 255 },
54 { "chocolate",210,105,30, 255 },
55 { "coral",255,127,80, 255 },
56 { "cornflowerblue",100,149,237, 255 },
57 { "cornsilk",255,248,220, 255 },
58 { "crimson",220,20,60, 255 },
59 { "cyan",0,255,255, 255 },
60 { "darkblue",0,0,139, 255 },
61 { "darkcyan",0,139,139, 255 },
62 { "darkgoldenrod",184,134,11, 255 },
63 { "darkgray",169,169,169, 255 },
64 { "darkgreen",0,100,0, 255 },
65 { "darkgrey",169,169,169, 255 },
66 { "darkkhaki",189,183,107, 255 },
67 { "darkmagenta",139,0,139, 255 },
68 { "darkolivegreen",85,107,47, 255 },
69 { "darkorange",255,140,0, 255 },
70 { "darkorchid",153,50,204, 255 },
71 { "darkred",139,0,0, 255 },
72 { "darksalmon",233,150,122, 255 },
73 { "darkseagreen",143,188,143, 255 },
74 { "darkslateblue",72,61,139, 255 },
75 { "darkslategray",47,79,79, 255 },
76 { "darkslategrey",47,79,79, 255 },
77 { "darkturquoise",0,206,209, 255 },
78 { "darkviolet",148,0,211, 255 },
79 { "deeppink",255,20,147, 255 },
80 { "deepskyblue",0,191,255, 255 },
81 { "dimgray",105,105,105, 255 },
82 { "dimgrey",105,105,105, 255 },
83 { "dodgerblue",30,144,255, 255 },
84 { "firebrick",178,34,34, 255 },
85 { "floralwhite",255,250,240, 255 },
86 { "forestgreen",34,139,34, 255 },
87 { "fuchsia",255,0,255, 255 },
88 { "gainsboro",220,220,220, 255 },
89 { "ghostwhite",248,248,255, 255 },
90 { "gold",255,215,0, 255 },
91 { "goldenrod",218,165,32, 255 },
92 { "gray",128,128,128, 255 },
93 { "green",0,128,0, 255 },
94 { "greenyellow",173,255,47, 255 },
95 { "grey",128,128,128, 255 },
96 { "honeydew",240,255,240, 255 },
97 { "hotpink",255,105,180, 255 },
98 { "indianred",205,92,92, 255 },
99 { "indigo",75,0,130, 255 },
100 { "ivory",255,255,240, 255 },
101 { "khaki",240,230,140, 255 },
102 { "lavender",230,230,250, 255 },
103 { "lavenderblush",255,240,245, 255 },
104 { "lawngreen",124,252,0, 255 },
105 { "lemonchiffon",255,250,205, 255 },
106 { "lightblue",173,216,230, 255 },
107 { "lightcoral",240,128,128, 255 },
108 { "lightcyan",224,255,255, 255 },
109 { "lightgoldenrodyellow",250,250,210, 255 },
110 { "lightgray",211,211,211, 255 },
111 { "lightgreen",144,238,144, 255 },
112 { "lightgrey",211,211,211, 255 },
113 { "lightpink",255,182,193, 255 },
114 { "lightsalmon",255,160,122, 255 },
115 { "lightseagreen",32,178,170, 255 },
116 { "lightskyblue",135,206,250, 255 },
117 { "lightslategray",119,136,153, 255 },
118 { "lightslategrey",119,136,153, 255 },
119 { "lightsteelblue",176,196,222, 255 },
120 { "lightyellow",255,255,224, 255 },
121 { "lime",0,255,0, 255 },
122 { "limegreen",50,205,50, 255 },
123 { "linen",250,240,230, 255 },
124 { "magenta",255,0,255, 255 },
125 { "maroon",128,0,0, 255 },
126 { "mediumaquamarine",102,205,170, 255 },
127 { "mediumblue",0,0,205, 255 },
128 { "mediumorchid",186,85,211, 255 },
129 { "mediumpurple",147,112,219, 255 },
130 { "mediumseagreen",60,179,113, 255 },
131 { "mediumslateblue",123,104,238, 255 },
132 { "mediumspringgreen",0,250,154, 255 },
133 { "mediumturquoise",72,209,204, 255 },
134 { "mediumvioletred",199,21,133, 255 },
135 { "midnightblue",25,25,112, 255 },
136 { "mintcream",245,255,250, 255 },
137 { "mistyrose",255,228,225, 255 },
138 { "moccasin",255,228,181, 255 },
139 { "navajowhite",255,222,173, 255 },
140 { "navy",0,0,128, 255 },
141 { "oldlace",253,245,230, 255 },
142 { "olive",128,128,0, 255 },
143 { "olivedrab",107,142,35, 255 },
144 { "orange",255,165,0, 255 },
145 { "orangered",255,69,0, 255 },
146 { "orchid",218,112,214, 255 },
147 { "palegoldenrod",238,232,170, 255 },
148 { "palegreen",152,251,152, 255 },
149 { "paleturquoise",175,238,238, 255 },
150 { "palevioletred",219,112,147, 255 },
151 { "papayawhip",255,239,213, 255 },
152 { "peachpuff",255,218,185, 255 },
153 { "peru",205,133,63, 255 },
154 { "pink",255,192,203, 255 },
155 { "plum",221,160,221, 255 },
156 { "powderblue",176,224,230, 255 },
157 { "purple",128,0,128, 255 },
158 { "red",255,0,0, 255 },
159 { "rosybrown",188,143,143, 255 },
160 { "royalblue",65,105,225, 255 },
161 { "saddlebrown",139,69,19, 255 },
162 { "salmon",250,128,114, 255 },
163 { "sandybrown",244,164,96, 255 },
164 { "seagreen",46,139,87, 255 },
165 { "seashell",255,245,238, 255 },
166 { "sienna",160,82,45, 255 },
167 { "silver",192,192,192, 255 },
168 { "skyblue",135,206,235, 255 },
169 { "slateblue",106,90,205, 255 },
170 { "slategray",112,128,144, 255 },
171 { "slategrey",112,128,144, 255 },
172 { "snow",255,250,250, 255 },
173 { "springgreen",0,255,127, 255 },
174 { "steelblue",70,130,180, 255 },
175 { "tan",210,180,140, 255 },
176 { "teal",0,128,128, 255 },
177 { "thistle",216,191,216, 255 },
178 { "tomato",255,99,71, 255 },
179 { "turquoise",64,224,208, 255 },
180 { "violet",238,130,238, 255 },
181 { "wheat",245,222,179, 255 },
182 { "white",255,255,255, 255 },
183 { "whitesmoke",245,245,245, 255 },
184 { "yellow",255,255,0, 255 },
185 { "yellowgreen",154,205,50, 255 },
186 { "zzzzzzzzzzz",0,0,0, 0 }
187 };
188
189
190 //------------------------------------------------------------------------
191 parser::~parser()
192 {
193 delete [] m_attr_value;
194 delete [] m_attr_name;
195 delete [] m_buf;
196 delete [] m_title;
197 }
198
199 //------------------------------------------------------------------------
200 parser::parser(path_renderer& path) :
201 m_path(path),
202 m_tokenizer(),
203 m_buf(new char[buf_size]),
204 m_title(new char[256]),
205 m_title_len(0),
206 m_title_flag(false),
207 m_path_flag(false),
208 m_attr_name(new char[128]),
209 m_attr_value(new char[1024]),
210 m_attr_name_len(127),
211 m_attr_value_len(1023)
212 {
213 m_title[0] = 0;
214 }
215
216 //------------------------------------------------------------------------
217 void parser::parse(const char* fname)
218 {
219 char msg[1024];
220 XML_Parser p = XML_ParserCreate(NULL);
221 if(p == 0)
222 {
223 throw exception("Couldn't allocate memory for parser");
224 }
225
226 XML_SetUserData(p, this);
227 XML_SetElementHandler(p, start_element, end_element);
228 XML_SetCharacterDataHandler(p, content);
229
230 FILE* fd = fopen(fname, "r");
231 if(fd == 0)
232 {
233 sprintf(msg, "Couldn't open file %s", fname);
234 throw exception(msg);
235 }
236
237 bool done = false;
238 do
239 {
240 size_t len = fread(m_buf, 1, buf_size, fd);
241 done = len < buf_size;
242 if(!XML_Parse(p, m_buf, len, done))
243 {
244 sprintf(msg,
245 "%s at line %lu\n",
246 XML_ErrorString(XML_GetErrorCode(p)),
247 XML_GetCurrentLineNumber(p));
248 throw exception(msg);
249 }
250 }
251 while(!done);
252 fclose(fd);
253 XML_ParserFree(p);
254
255 char* ts = m_title;
256 while(*ts)
257 {
258 if(*ts < ' ') *ts = ' ';
259 ++ts;
260 }
261 }
262
263
264 //------------------------------------------------------------------------
265 void parser::start_element(void* data, const char* el, const char** attr)
266 {
267 parser& self = *(parser*)data;
268
269 if(strcmp(el, "title") == 0)
270 {
271 self.m_title_flag = true;
272 }
273 else
274 if(strcmp(el, "g") == 0)
275 {
276 self.m_path.push_attr();
277 self.parse_attr(attr);
278 }
279 else
280 if(strcmp(el, "path") == 0)
281 {
282 if(self.m_path_flag)
283 {
284 throw exception("start_element: Nested path");
285 }
286 self.m_path.begin_path();
287 self.parse_path(attr);
288 self.m_path.end_path();
289 self.m_path_flag = true;
290 }
291 else
292 if(strcmp(el, "rect") == 0)
293 {
294 self.parse_rect(attr);
295 }
296 else
297 if(strcmp(el, "line") == 0)
298 {
299 self.parse_line(attr);
300 }
301 else
302 if(strcmp(el, "polyline") == 0)
303 {
304 self.parse_poly(attr, false);
305 }
306 else
307 if(strcmp(el, "polygon") == 0)
308 {
309 self.parse_poly(attr, true);
310 }
311 else
312 if(strcmp(el, "circle") == 0)
313 {
314 self.parse_circle(attr);
315 }
316 else
317 if(strcmp(el, "ellipse") == 0)
318 {
319 self.parse_ellipse(attr);
320 }
321 //else
322 //if(strcmp(el, "<OTHER_ELEMENTS>") == 0)
323 //{
324 //}
325 // . . .
326 }
327
328
329 //------------------------------------------------------------------------
330 void parser::end_element(void* data, const char* el)
331 {
332 parser& self = *(parser*)data;
333
334 if(strcmp(el, "title") == 0)
335 {
336 self.m_title_flag = false;
337 }
338 else
339 if(strcmp(el, "g") == 0)
340 {
341 self.m_path.pop_attr();
342 }
343 else
344 if(strcmp(el, "path") == 0)
345 {
346 self.m_path_flag = false;
347 }
348 //else
349 //if(strcmp(el, "<OTHER_ELEMENTS>") == 0)
350 //{
351 //}
352 // . . .
353 }
354
355
356 //------------------------------------------------------------------------
357 void parser::content(void* data, const char* s, int len)
358 {
359 parser& self = *(parser*)data;
360
361 // m_title_flag signals that the <title> tag is being parsed now.
362 // The following code concatenates the pieces of content of the <title> tag.
363 if(self.m_title_flag)
364 {
365 if(len + self.m_title_len > 255) len = 255 - self.m_title_len;
366 if(len > 0)
367 {
368 memcpy(self.m_title + self.m_title_len, s, len);
369 self.m_title_len += len;
370 self.m_title[self.m_title_len] = 0;
371 }
372 }
373 }
374
375
376 //------------------------------------------------------------------------
377 void parser::parse_attr(const char** attr)
378 {
379 int i;
380 for(i = 0; attr[i]; i += 2)
381 {
382 if(strcmp(attr[i], "style") == 0)
383 {
384 parse_style(attr[i + 1]);
385 }
386 else
387 {
388 parse_attr(attr[i], attr[i + 1]);
389 }
390 }
391 }
392
393 //-------------------------------------------------------------
394 void parser::parse_path(const char** attr)
395 {
396 int i;
397
398 for(i = 0; attr[i]; i += 2)
399 {
400 // The <path> tag can consist of the path itself ("d=")
401 // as well as of other parameters like "style=", "transform=", etc.
402 // In the last case we simply rely on the function of parsing
403 // attributes (see 'else' branch).
404 if(strcmp(attr[i], "d") == 0)
405 {
406 m_tokenizer.set_path_str(attr[i + 1]);
407 m_path.parse_path(m_tokenizer);
408 }
409 else
410 {
411 // Create a temporary single pair "name-value" in order
412 // to avoid multiple calls for the same attribute.
413 const char* tmp[4];
414 tmp[0] = attr[i];
415 tmp[1] = attr[i + 1];
416 tmp[2] = 0;
417 tmp[3] = 0;
418 parse_attr(tmp);
419 }
420 }
421 }
422
423
424 //-------------------------------------------------------------
425 int cmp_color(const void* p1, const void* p2)
426 {
427 return strcmp(((named_color*)p1)->name, ((named_color*)p2)->name);
428 }
429
430 //-------------------------------------------------------------
431 rgba8 parse_color(const char* str)
432 {
433 while(*str == ' ') ++str;
434 unsigned c = 0;
435 if(*str == '#')
436 {
437 sscanf(str + 1, "%x", &c);
438 return rgb8_packed(c);
439 }
440 else
441 {
442 named_color c;
443 unsigned len = strlen(str);
444 if(len > sizeof(c.name) - 1)
445 {
446 throw exception("parse_color: Invalid color name '%s'", str);
447 }
448 strcpy(c.name, str);
449 const void* p = bsearch(&c,
450 colors,
451 sizeof(colors) / sizeof(colors[0]),
452 sizeof(colors[0]),
453 cmp_color);
454 if(p == 0)
455 {
456 throw exception("parse_color: Invalid color name '%s'", str);
457 }
458 const named_color* pc = (const named_color*)p;
459 return rgba8(pc->r, pc->g, pc->b, pc->a);
460 }
461 }
462
463 double parse_double(const char* str)
464 {
465 while(*str == ' ') ++str;
466 return atof(str);
467 }
468
469
470
471 //-------------------------------------------------------------
472 bool parser::parse_attr(const char* name, const char* value)
473 {
474 if(strcmp(name, "style") == 0)
475 {
476 parse_style(value);
477 }
478 else
479 if(strcmp(name, "fill") == 0)
480 {
481 if(strcmp(value, "none") == 0)
482 {
483 m_path.fill_none();
484 }
485 else
486 {
487 m_path.fill(parse_color(value));
488 }
489 }
490 else
491 if(strcmp(name, "fill-opacity") == 0)
492 {
493 m_path.fill_opacity(parse_double(value));
494 }
495 else
496 if(strcmp(name, "stroke") == 0)
497 {
498 if(strcmp(value, "none") == 0)
499 {
500 m_path.stroke_none();
501 }
502 else
503 {
504 m_path.stroke(parse_color(value));
505 }
506 }
507 else
508 if(strcmp(name, "stroke-width") == 0)
509 {
510 m_path.stroke_width(parse_double(value));
511 }
512 else
513 if(strcmp(name, "stroke-linecap") == 0)
514 {
515 if(strcmp(value, "butt") == 0) m_path.line_cap(butt_cap);
516 else if(strcmp(value, "round") == 0) m_path.line_cap(round_cap);
517 else if(strcmp(value, "square") == 0) m_path.line_cap(square_cap);
518 }
519 else
520 if(strcmp(name, "stroke-linejoin") == 0)
521 {
522 if(strcmp(value, "miter") == 0) m_path.line_join(miter_join);
523 else if(strcmp(value, "round") == 0) m_path.line_join(round_join);
524 else if(strcmp(value, "bevel") == 0) m_path.line_join(bevel_join);
525 }
526 else
527 if(strcmp(name, "stroke-miterlimit") == 0)
528 {
529 m_path.miter_limit(parse_double(value));
530 }
531 else
532 if(strcmp(name, "stroke-opacity") == 0)
533 {
534 m_path.stroke_opacity(parse_double(value));
535 }
536 else
537 if(strcmp(name, "transform") == 0)
538 {
539 parse_transform(value);
540 }
541 else
542 if(strcmp(name, "fill-rule") == 0)
543 {
544 m_path.even_odd(strcmp(value, "evenodd") == 0);
545 }
546 //else
547 //if(strcmp(el, "<OTHER_ATTRIBUTES>") == 0)
548 //{
549 //}
550 // . . .
551 else
552 {
553 return false;
554 }
555 return true;
556 }
557
558
559
560 //-------------------------------------------------------------
561 void parser::copy_name(const char* start, const char* end)
562 {
563 unsigned len = unsigned(end - start);
564 if(m_attr_name_len == 0 || len > m_attr_name_len)
565 {
566 delete [] m_attr_name;
567 m_attr_name = new char[len + 1];
568 m_attr_name_len = len;
569 }
570 if(len) memcpy(m_attr_name, start, len);
571 m_attr_name[len] = 0;
572 }
573
574
575
576 //-------------------------------------------------------------
577 void parser::copy_value(const char* start, const char* end)
578 {
579 unsigned len = unsigned(end - start);
580 if(m_attr_value_len == 0 || len > m_attr_value_len)
581 {
582 delete [] m_attr_value;
583 m_attr_value = new char[len + 1];
584 m_attr_value_len = len;
585 }
586 if(len) memcpy(m_attr_value, start, len);
587 m_attr_value[len] = 0;
588 }
589
590
591 //-------------------------------------------------------------
592 bool parser::parse_name_value(const char* nv_start, const char* nv_end)
593 {
594 const char* str = nv_start;
595 while(str < nv_end && *str != ':') ++str;
596
597 const char* val = str;
598
599 // Right Trim
600 while(str > nv_start &&
601 (*str == ':' || isspace(*str))) --str;
602 ++str;
603
604 copy_name(nv_start, str);
605
606 while(val < nv_end && (*val == ':' || isspace(*val))) ++val;
607
608 copy_value(val, nv_end);
609 return parse_attr(m_attr_name, m_attr_value);
610 }
611
612
613
614 //-------------------------------------------------------------
615 void parser::parse_style(const char* str)
616 {
617 while(*str)
618 {
619 // Left Trim
620 while(*str && isspace(*str)) ++str;
621 const char* nv_start = str;
622 while(*str && *str != ';') ++str;
623 const char* nv_end = str;
624
625 // Right Trim
626 while(nv_end > nv_start &&
627 (*nv_end == ';' || isspace(*nv_end))) --nv_end;
628 ++nv_end;
629
630 parse_name_value(nv_start, nv_end);
631 if(*str) ++str;
632 }
633
634 }
635
636
637 //-------------------------------------------------------------
638 void parser::parse_rect(const char** attr)
639 {
640 int i;
641 double x = 0.0;
642 double y = 0.0;
643 double w = 0.0;
644 double h = 0.0;
645
646 m_path.begin_path();
647 for(i = 0; attr[i]; i += 2)
648 {
649 if(!parse_attr(attr[i], attr[i + 1]))
650 {
651 if(strcmp(attr[i], "x") == 0) x = parse_double(attr[i + 1]);
652 if(strcmp(attr[i], "y") == 0) y = parse_double(attr[i + 1]);
653 if(strcmp(attr[i], "width") == 0) w = parse_double(attr[i + 1]);
654 if(strcmp(attr[i], "height") == 0) h = parse_double(attr[i + 1]);
655 // rx - to be implemented
656 // ry - to be implemented
657 }
658 }
659
660
661 if(w != 0.0 && h != 0.0)
662 {
663 if(w < 0.0) throw exception("parse_rect: Invalid width: %f", w);
664 if(h < 0.0) throw exception("parse_rect: Invalid height: %f", h);
665
666 m_path.move_to(x, y);
667 m_path.line_to(x + w, y);
668 m_path.line_to(x + w, y + h);
669 m_path.line_to(x, y + h);
670 m_path.close_subpath();
671 }
672 m_path.end_path();
673 }
674
675
676 //-------------------------------------------------------------
677 void parser::parse_line(const char** attr)
678 {
679 int i;
680 double x1 = 0.0;
681 double y1 = 0.0;
682 double x2 = 0.0;
683 double y2 = 0.0;
684
685 m_path.begin_path();
686 for(i = 0; attr[i]; i += 2)
687 {
688 if(!parse_attr(attr[i], attr[i + 1]))
689 {
690 if(strcmp(attr[i], "x1") == 0) x1 = parse_double(attr[i + 1]);
691 if(strcmp(attr[i], "y1") == 0) y1 = parse_double(attr[i + 1]);
692 if(strcmp(attr[i], "x2") == 0) x2 = parse_double(attr[i + 1]);
693 if(strcmp(attr[i], "y2") == 0) y2 = parse_double(attr[i + 1]);
694 }
695 }
696
697 m_path.move_to(x1, y1);
698 m_path.line_to(x2, y2);
699 m_path.end_path();
700 }
701
702
703 //-------------------------------------------------------------
704 void parser::parse_poly(const char** attr, bool close_flag)
705 {
706 int i;
707 double x = 0.0;
708 double y = 0.0;
709
710 m_path.begin_path();
711 for(i = 0; attr[i]; i += 2)
712 {
713 if(!parse_attr(attr[i], attr[i + 1]))
714 {
715 if(strcmp(attr[i], "points") == 0)
716 {
717 m_tokenizer.set_path_str(attr[i + 1]);
718 if(!m_tokenizer.next())
719 {
720 throw exception("parse_poly: Too few coordinates");
721 }
722 x = m_tokenizer.last_number();
723 if(!m_tokenizer.next())
724 {
725 throw exception("parse_poly: Too few coordinates");
726 }
727 y = m_tokenizer.last_number();
728 m_path.move_to(x, y);
729 while(m_tokenizer.next())
730 {
731 x = m_tokenizer.last_number();
732 if(!m_tokenizer.next())
733 {
734 throw exception("parse_poly: Odd number of coordinates");
735 }
736 y = m_tokenizer.last_number();
737 m_path.line_to(x, y);
738 }
739 }
740 }
741 }
742 if(close_flag)
743 {
744 m_path.close_subpath();
745 }
746 m_path.end_path();
747 }
748
749 //-------------------------------------------------------------
750 void parser::parse_circle(const char** attr)
751 {
752 int i;
753 double cx = 0.0;
754 double cy = 0.0;
755 double r = 0.0;
756
757 m_path.begin_path();
758 for(i = 0; attr[i]; i += 2)
759 {
760 if(!parse_attr(attr[i], attr[i + 1]))
761 {
762 if(strcmp(attr[i], "cx") == 0) cx = parse_double(attr[i + 1]);
763 if(strcmp(attr[i], "cy") == 0) cy = parse_double(attr[i + 1]);
764 if(strcmp(attr[i], "r") == 0) r = parse_double(attr[i + 1]);
765 }
766 }
767
768 m_path.move_to(cx-r, cy);
769 m_path.arc(r, r, 360, true, true, 0, .0001, true);
770 m_path.end_path();
771 }
772
773
774 void parser::parse_ellipse(const char** attr)
775 {
776 int i;
777 double cx = 0.0;
778 double cy = 0.0;
779 double rx = 0.0;
780 double ry = 0.0;
781
782 m_path.begin_path();
783 for(i = 0; attr[i]; i += 2)
784 {
785 if(!parse_attr(attr[i], attr[i + 1]))
786 {
787 if(strcmp(attr[i], "cx") == 0) cx = parse_double(attr[i + 1]);
788 if(strcmp(attr[i], "cy") == 0) cy = parse_double(attr[i + 1]);
789 if(strcmp(attr[i], "rx") == 0) rx = parse_double(attr[i + 1]);
790 if(strcmp(attr[i], "ry") == 0) ry = parse_double(attr[i + 1]);
791 }
792 }
793
794 m_path.move_to(cx-rx, cy);
795 m_path.arc(rx, ry, 360, true, true, 0, .0001, true);
796 m_path.end_path();
797 }
798
799
800 //-------------------------------------------------------------
801 void parser::parse_transform(const char* str)
802 {
803 while(*str)
804 {
805 if(islower(*str))
806 {
807 if(strncmp(str, "matrix", 6) == 0) str += parse_matrix(str); else
808 if(strncmp(str, "translate", 9) == 0) str += parse_translate(str); else
809 if(strncmp(str, "rotate", 6) == 0) str += parse_rotate(str); else
810 if(strncmp(str, "scale", 5) == 0) str += parse_scale(str); else
811 if(strncmp(str, "skewX", 5) == 0) str += parse_skew_x(str); else
812 if(strncmp(str, "skewY", 5) == 0) str += parse_skew_y(str); else
813 {
814 ++str;
815 }
816 }
817 else
818 {
819 ++str;
820 }
821 }
822 }
823
824
825 //-------------------------------------------------------------
826 static bool is_numeric(char c)
827 {
828 return strchr("0123456789+-.eE", c) != 0;
829 }
830
831 //-------------------------------------------------------------
832 static unsigned parse_transform_args(const char* str,
833 double* args,
834 unsigned max_na,
835 unsigned* na)
836 {
837 *na = 0;
838 const char* ptr = str;
839 while(*ptr && *ptr != '(') ++ptr;
840 if(*ptr == 0)
841 {
842 throw exception("parse_transform_args: Invalid syntax");
843 }
844 const char* end = ptr;
845 while(*end && *end != ')') ++end;
846 if(*end == 0)
847 {
848 throw exception("parse_transform_args: Invalid syntax");
849 }
850
851 while(ptr < end)
852 {
853 if(is_numeric(*ptr))
854 {
855 if(*na >= max_na)
856 {
857 throw exception("parse_transform_args: Too many arguments");
858 }
859 args[(*na)++] = atof(ptr);
860 while(ptr < end && is_numeric(*ptr)) ++ptr;
861 }
862 else
863 {
864 ++ptr;
865 }
866 }
867 return unsigned(end - str);
868 }
869
870 //-------------------------------------------------------------
871 unsigned parser::parse_matrix(const char* str)
872 {
873 double args[6];
874 unsigned na = 0;
875 unsigned len = parse_transform_args(str, args, 6, &na);
876 if(na != 6)
877 {
878 throw exception("parse_matrix: Invalid number of arguments");
879 }
880 m_path.transform().premultiply(trans_affine(args[0], args[1], args[2], args[3], args[4], args[5]));
881 return len;
882 }
883
884 //-------------------------------------------------------------
885 unsigned parser::parse_translate(const char* str)
886 {
887 double args[2];
888 unsigned na = 0;
889 unsigned len = parse_transform_args(str, args, 2, &na);
890 if(na == 1) args[1] = 0.0;
891 m_path.transform().premultiply(trans_affine_translation(args[0], args[1]));
892 return len;
893 }
894
895 //-------------------------------------------------------------
896 unsigned parser::parse_rotate(const char* str)
897 {
898 double args[3];
899 unsigned na = 0;
900 unsigned len = parse_transform_args(str, args, 3, &na);
901 if(na == 1)
902 {
903 m_path.transform().premultiply(trans_affine_rotation(deg2rad(args[0])));
904 }
905 else if(na == 3)
906 {
907 trans_affine t = trans_affine_translation(-args[1], -args[2]);
908 t *= trans_affine_rotation(deg2rad(args[0]));
909 t *= trans_affine_translation(args[1], args[2]);
910 m_path.transform().premultiply(t);
911 }
912 else
913 {
914 throw exception("parse_rotate: Invalid number of arguments");
915 }
916 return len;
917 }
918
919 //-------------------------------------------------------------
920 unsigned parser::parse_scale(const char* str)
921 {
922 double args[2];
923 unsigned na = 0;
924 unsigned len = parse_transform_args(str, args, 2, &na);
925 if(na == 1) args[1] = args[0];
926 m_path.transform().premultiply(trans_affine_scaling(args[0], args[1]));
927 return len;
928 }
929
930 //-------------------------------------------------------------
931 unsigned parser::parse_skew_x(const char* str)
932 {
933 double arg;
934 unsigned na = 0;
935 unsigned len = parse_transform_args(str, &arg, 1, &na);
936 m_path.transform().premultiply(trans_affine_skewing(deg2rad(arg), 0.0));
937 return len;
938 }
939
940 //-------------------------------------------------------------
941 unsigned parser::parse_skew_y(const char* str)
942 {
943 double arg;
944 unsigned na = 0;
945 unsigned len = parse_transform_args(str, &arg, 1, &na);
946 m_path.transform().premultiply(trans_affine_skewing(0.0, deg2rad(arg)));
947 return len;
948 }
949
950 }
951 }
952
953