2 * This file is part of Libsvgtiny
3 * Licensed under the MIT License,
4 * http://opensource.org/licenses/mit-license.php
5 * Copyright 2008-2009 James Bursa <james@semichrome.net>
8 #define _GNU_SOURCE /* for strndup */
16 #include <libxml/parser.h>
17 #include <libxml/debugXML.h>
19 #include "svgtiny_internal.h"
22 #define M_PI 3.14159265358979323846
25 #define KAPPA 0.5522847498
27 static svgtiny_code
svgtiny_parse_svg(xmlNode
*svg
,
28 struct svgtiny_parse_state state
);
29 static svgtiny_code
svgtiny_parse_path(xmlNode
*path
,
30 struct svgtiny_parse_state state
);
31 static svgtiny_code
svgtiny_parse_rect(xmlNode
*rect
,
32 struct svgtiny_parse_state state
);
33 static svgtiny_code
svgtiny_parse_circle(xmlNode
*circle
,
34 struct svgtiny_parse_state state
);
35 static svgtiny_code
svgtiny_parse_ellipse(xmlNode
*ellipse
,
36 struct svgtiny_parse_state state
);
37 static svgtiny_code
svgtiny_parse_line(xmlNode
*line
,
38 struct svgtiny_parse_state state
);
39 static svgtiny_code
svgtiny_parse_poly(xmlNode
*poly
,
40 struct svgtiny_parse_state state
, bool polygon
);
41 static svgtiny_code
svgtiny_parse_text(xmlNode
*text
,
42 struct svgtiny_parse_state state
);
43 static void svgtiny_parse_position_attributes(const xmlNode
*node
,
44 const struct svgtiny_parse_state state
,
45 float *x
, float *y
, float *width
, float *height
);
46 static void svgtiny_parse_paint_attributes(const xmlNode
*node
,
47 struct svgtiny_parse_state
*state
);
48 static void svgtiny_parse_font_attributes(const xmlNode
*node
,
49 struct svgtiny_parse_state
*state
);
50 static void svgtiny_parse_transform_attributes(xmlNode
*node
,
51 struct svgtiny_parse_state
*state
);
52 static svgtiny_code
svgtiny_add_path(float *p
, unsigned int n
,
53 struct svgtiny_parse_state
*state
);
57 * Create a new svgtiny_diagram structure.
60 struct svgtiny_diagram
*svgtiny_create(void)
62 struct svgtiny_diagram
*diagram
;
64 diagram
= malloc(sizeof *diagram
);
69 diagram
->shape_count
= 0;
70 diagram
->error_line
= 0;
71 diagram
->error_message
= 0;
78 * Parse a block of memory into a svgtiny_diagram.
81 svgtiny_code
svgtiny_parse(struct svgtiny_diagram
*diagram
,
82 const char *buffer
, size_t size
, const char *url
,
83 int viewport_width
, int viewport_height
)
87 struct svgtiny_parse_state state
;
88 float x
, y
, width
, height
;
95 /* parse XML to tree */
96 document
= xmlReadMemory(buffer
, size
, url
, 0,
97 XML_PARSE_NONET
| XML_PARSE_COMPACT
);
99 return svgtiny_LIBXML_ERROR
;
101 /*xmlDebugDumpDocument(stderr, document);*/
103 /* find root <svg> element */
104 svg
= xmlDocGetRootElement(document
);
106 return svgtiny_NOT_SVG
;
107 if (strcmp((const char *) svg
->name
, "svg") != 0)
108 return svgtiny_NOT_SVG
;
110 /* get graphic dimensions */
111 state
.diagram
= diagram
;
112 state
.document
= document
;
113 state
.viewport_width
= viewport_width
;
114 state
.viewport_height
= viewport_height
;
115 svgtiny_parse_position_attributes(svg
, state
, &x
, &y
, &width
, &height
);
116 diagram
->width
= width
;
117 diagram
->height
= height
;
119 /* set up parsing state */
120 state
.viewport_width
= width
;
121 state
.viewport_height
= height
;
122 state
.ctm
.a
= 1; /*(float) viewport_width / (float) width;*/
125 state
.ctm
.d
= 1; /*(float) viewport_height / (float) height;*/
126 state
.ctm
.e
= 0; /*x;*/
127 state
.ctm
.f
= 0; /*y;*/
128 /*state.style = css_base_style;
129 state.style.font_size.value.length.value = option_font_size * 0.1;*/
130 state
.fill
= 0x000000;
131 state
.stroke
= svgtiny_TRANSPARENT
;
132 state
.stroke_width
= 1;
133 state
.linear_gradient_stop_count
= 0;
136 code
= svgtiny_parse_svg(svg
, state
);
139 xmlFreeDoc(document
);
146 * Parse a <svg> or <g> element node.
149 svgtiny_code
svgtiny_parse_svg(xmlNode
*svg
,
150 struct svgtiny_parse_state state
)
152 float x
, y
, width
, height
;
156 svgtiny_parse_position_attributes(svg
, state
, &x
, &y
, &width
, &height
);
157 svgtiny_parse_paint_attributes(svg
, &state
);
158 svgtiny_parse_font_attributes(svg
, &state
);
161 view_box
= xmlHasProp(svg
, (const xmlChar
*) "viewBox");
163 const char *s
= (const char *) view_box
->children
->content
;
164 float min_x
, min_y
, vwidth
, vheight
;
165 if (sscanf(s
, "%f,%f,%f,%f",
166 &min_x
, &min_y
, &vwidth
, &vheight
) == 4 ||
167 sscanf(s
, "%f %f %f %f",
168 &min_x
, &min_y
, &vwidth
, &vheight
) == 4) {
169 state
.ctm
.a
= (float) state
.viewport_width
/ vwidth
;
170 state
.ctm
.d
= (float) state
.viewport_height
/ vheight
;
171 state
.ctm
.e
+= -min_x
* state
.ctm
.a
;
172 state
.ctm
.f
+= -min_y
* state
.ctm
.d
;
176 svgtiny_parse_transform_attributes(svg
, &state
);
178 for (child
= svg
->children
; child
; child
= child
->next
) {
179 svgtiny_code code
= svgtiny_OK
;
181 if (child
->type
== XML_ELEMENT_NODE
) {
182 const char *name
= (const char *) child
->name
;
183 if (strcmp(name
, "svg") == 0)
184 code
= svgtiny_parse_svg(child
, state
);
185 else if (strcmp(name
, "g") == 0)
186 code
= svgtiny_parse_svg(child
, state
);
187 else if (strcmp(name
, "a") == 0)
188 code
= svgtiny_parse_svg(child
, state
);
189 else if (strcmp(name
, "path") == 0)
190 code
= svgtiny_parse_path(child
, state
);
191 else if (strcmp(name
, "rect") == 0)
192 code
= svgtiny_parse_rect(child
, state
);
193 else if (strcmp(name
, "circle") == 0)
194 code
= svgtiny_parse_circle(child
, state
);
195 else if (strcmp(name
, "ellipse") == 0)
196 code
= svgtiny_parse_ellipse(child
, state
);
197 else if (strcmp(name
, "line") == 0)
198 code
= svgtiny_parse_line(child
, state
);
199 else if (strcmp(name
, "polyline") == 0)
200 code
= svgtiny_parse_poly(child
, state
, false);
201 else if (strcmp(name
, "polygon") == 0)
202 code
= svgtiny_parse_poly(child
, state
, true);
203 else if (strcmp(name
, "text") == 0)
204 code
= svgtiny_parse_text(child
, state
);
207 if (code
!= svgtiny_OK
)
217 * Parse a <path> element node.
219 * http://www.w3.org/TR/SVG11/paths#PathElement
222 svgtiny_code
svgtiny_parse_path(xmlNode
*path
,
223 struct svgtiny_parse_state state
)
228 float last_x
= 0, last_y
= 0;
229 float last_cubic_x
= 0, last_cubic_y
= 0;
230 float last_quad_x
= 0, last_quad_y
= 0;
232 svgtiny_parse_paint_attributes(path
, &state
);
233 svgtiny_parse_transform_attributes(path
, &state
);
235 /* read d attribute */
236 s
= path_d
= (char *) xmlGetProp(path
, (const xmlChar
*) "d");
238 state
.diagram
->error_line
= path
->line
;
239 state
.diagram
->error_message
= "path: missing d attribute";
240 return svgtiny_SVG_ERROR
;
243 /* allocate space for path: it will never have more elements than d */
244 p
= malloc(sizeof p
[0] * strlen(s
));
246 return svgtiny_OUT_OF_MEMORY
;
248 /* parse d and build path */
249 for (i
= 0; s
[i
]; i
++)
256 float x
, y
, x1
, y1
, x2
, y2
, rx
, ry
, rotation
, large_arc
, sweep
;
259 /* moveto (M, m), lineto (L, l) (2 arguments) */
260 if (sscanf(s
, " %1[MmLl] %f %f %n", command
, &x
, &y
, &n
) == 3) {
261 /*LOG(("moveto or lineto"));*/
262 if (*command
== 'M' || *command
== 'm')
263 plot_command
= svgtiny_PATH_MOVE
;
265 plot_command
= svgtiny_PATH_LINE
;
267 p
[i
++] = plot_command
;
268 if ('a' <= *command
) {
272 p
[i
++] = last_cubic_x
= last_quad_x
= last_x
274 p
[i
++] = last_cubic_y
= last_quad_y
= last_y
277 plot_command
= svgtiny_PATH_LINE
;
278 } while (sscanf(s
, "%f %f %n", &x
, &y
, &n
) == 2);
280 /* closepath (Z, z) (no arguments) */
281 } else if (sscanf(s
, " %1[Zz] %n", command
, &n
) == 1) {
282 /*LOG(("closepath"));*/
283 p
[i
++] = svgtiny_PATH_CLOSE
;
286 /* horizontal lineto (H, h) (1 argument) */
287 } else if (sscanf(s
, " %1[Hh] %f %n", command
, &x
, &n
) == 2) {
288 /*LOG(("horizontal lineto"));*/
290 p
[i
++] = svgtiny_PATH_LINE
;
293 p
[i
++] = last_cubic_x
= last_quad_x
= last_x
295 p
[i
++] = last_cubic_y
= last_quad_y
= last_y
;
297 } while (sscanf(s
, "%f %n", &x
, &n
) == 1);
299 /* vertical lineto (V, v) (1 argument) */
300 } else if (sscanf(s
, " %1[Vv] %f %n", command
, &y
, &n
) == 2) {
301 /*LOG(("vertical lineto"));*/
303 p
[i
++] = svgtiny_PATH_LINE
;
306 p
[i
++] = last_cubic_x
= last_quad_x
= last_x
;
307 p
[i
++] = last_cubic_y
= last_quad_y
= last_y
310 } while (sscanf(s
, "%f %n", &x
, &n
) == 1);
312 /* curveto (C, c) (6 arguments) */
313 } else if (sscanf(s
, " %1[Cc] %f %f %f %f %f %f %n", command
,
314 &x1
, &y1
, &x2
, &y2
, &x
, &y
, &n
) == 7) {
315 /*LOG(("curveto"));*/
317 p
[i
++] = svgtiny_PATH_BEZIER
;
318 if (*command
== 'c') {
328 p
[i
++] = last_cubic_x
= x2
;
329 p
[i
++] = last_cubic_y
= y2
;
330 p
[i
++] = last_quad_x
= last_x
= x
;
331 p
[i
++] = last_quad_y
= last_y
= y
;
333 } while (sscanf(s
, "%f %f %f %f %f %f %n",
334 &x1
, &y1
, &x2
, &y2
, &x
, &y
, &n
) == 6);
336 /* shorthand/smooth curveto (S, s) (4 arguments) */
337 } else if (sscanf(s
, " %1[Ss] %f %f %f %f %n", command
,
338 &x2
, &y2
, &x
, &y
, &n
) == 5) {
339 /*LOG(("shorthand/smooth curveto"));*/
341 p
[i
++] = svgtiny_PATH_BEZIER
;
342 x1
= last_x
+ (last_x
- last_cubic_x
);
343 y1
= last_y
+ (last_y
- last_cubic_y
);
344 if (*command
== 's') {
352 p
[i
++] = last_cubic_x
= x2
;
353 p
[i
++] = last_cubic_y
= y2
;
354 p
[i
++] = last_quad_x
= last_x
= x
;
355 p
[i
++] = last_quad_y
= last_y
= y
;
357 } while (sscanf(s
, "%f %f %f %f %n",
358 &x2
, &y2
, &x
, &y
, &n
) == 4);
360 /* quadratic Bezier curveto (Q, q) (4 arguments) */
361 } else if (sscanf(s
, " %1[Qq] %f %f %f %f %n", command
,
362 &x1
, &y1
, &x
, &y
, &n
) == 5) {
363 /*LOG(("quadratic Bezier curveto"));*/
365 p
[i
++] = svgtiny_PATH_BEZIER
;
368 if (*command
== 'q') {
374 p
[i
++] = 1./3 * last_x
+ 2./3 * x1
;
375 p
[i
++] = 1./3 * last_y
+ 2./3 * y1
;
376 p
[i
++] = 2./3 * x1
+ 1./3 * x
;
377 p
[i
++] = 2./3 * y1
+ 1./3 * y
;
378 p
[i
++] = last_cubic_x
= last_x
= x
;
379 p
[i
++] = last_cubic_y
= last_y
= y
;
381 } while (sscanf(s
, "%f %f %f %f %n",
382 &x1
, &y1
, &x
, &y
, &n
) == 4);
384 /* shorthand/smooth quadratic Bezier curveto (T, t)
386 } else if (sscanf(s
, " %1[Tt] %f %f %n", command
,
388 /*LOG(("shorthand/smooth quadratic Bezier curveto"));*/
390 p
[i
++] = svgtiny_PATH_BEZIER
;
391 x1
= last_x
+ (last_x
- last_quad_x
);
392 y1
= last_y
+ (last_y
- last_quad_y
);
395 if (*command
== 't') {
401 p
[i
++] = 1./3 * last_x
+ 2./3 * x1
;
402 p
[i
++] = 1./3 * last_y
+ 2./3 * y1
;
403 p
[i
++] = 2./3 * x1
+ 1./3 * x
;
404 p
[i
++] = 2./3 * y1
+ 1./3 * y
;
405 p
[i
++] = last_cubic_x
= last_x
= x
;
406 p
[i
++] = last_cubic_y
= last_y
= y
;
408 } while (sscanf(s
, "%f %f %n",
411 /* elliptical arc (A, a) (7 arguments) */
412 } else if (sscanf(s
, " %1[Aa] %f %f %f %f %f %f %f %n", command
,
413 &rx
, &ry
, &rotation
, &large_arc
, &sweep
,
416 p
[i
++] = svgtiny_PATH_LINE
;
417 if (*command
== 'a') {
421 p
[i
++] = last_cubic_x
= last_quad_x
= last_x
423 p
[i
++] = last_cubic_y
= last_quad_y
= last_y
426 } while (sscanf(s
, "%f %f %f %f %f %f %f %n",
427 &rx
, &ry
, &rotation
, &large_arc
, &sweep
,
431 fprintf(stderr
, "parse failed at \"%s\"\n", s
);
439 /* no real segments in path */
444 return svgtiny_add_path(p
, i
, &state
);
449 * Parse a <rect> element node.
451 * http://www.w3.org/TR/SVG11/shapes#RectElement
454 svgtiny_code
svgtiny_parse_rect(xmlNode
*rect
,
455 struct svgtiny_parse_state state
)
457 float x
, y
, width
, height
;
460 svgtiny_parse_position_attributes(rect
, state
,
461 &x
, &y
, &width
, &height
);
462 svgtiny_parse_paint_attributes(rect
, &state
);
463 svgtiny_parse_transform_attributes(rect
, &state
);
465 p
= malloc(13 * sizeof p
[0]);
467 return svgtiny_OUT_OF_MEMORY
;
469 p
[0] = svgtiny_PATH_MOVE
;
472 p
[3] = svgtiny_PATH_LINE
;
475 p
[6] = svgtiny_PATH_LINE
;
478 p
[9] = svgtiny_PATH_LINE
;
481 p
[12] = svgtiny_PATH_CLOSE
;
483 return svgtiny_add_path(p
, 13, &state
);
488 * Parse a <circle> element node.
491 svgtiny_code
svgtiny_parse_circle(xmlNode
*circle
,
492 struct svgtiny_parse_state state
)
494 float x
= 0, y
= 0, r
= -1;
498 for (attr
= circle
->properties
; attr
; attr
= attr
->next
) {
499 const char *name
= (const char *) attr
->name
;
500 const char *content
= (const char *) attr
->children
->content
;
501 if (strcmp(name
, "cx") == 0)
502 x
= svgtiny_parse_length(content
,
503 state
.viewport_width
, state
);
504 else if (strcmp(name
, "cy") == 0)
505 y
= svgtiny_parse_length(content
,
506 state
.viewport_height
, state
);
507 else if (strcmp(name
, "r") == 0)
508 r
= svgtiny_parse_length(content
,
509 state
.viewport_width
, state
);
511 svgtiny_parse_paint_attributes(circle
, &state
);
512 svgtiny_parse_transform_attributes(circle
, &state
);
515 state
.diagram
->error_line
= circle
->line
;
516 state
.diagram
->error_message
= "circle: r missing or negative";
517 return svgtiny_SVG_ERROR
;
522 p
= malloc(32 * sizeof p
[0]);
524 return svgtiny_OUT_OF_MEMORY
;
526 p
[0] = svgtiny_PATH_MOVE
;
529 p
[3] = svgtiny_PATH_BEZIER
;
531 p
[5] = y
+ r
* KAPPA
;
532 p
[6] = x
+ r
* KAPPA
;
536 p
[10] = svgtiny_PATH_BEZIER
;
537 p
[11] = x
- r
* KAPPA
;
540 p
[14] = y
+ r
* KAPPA
;
543 p
[17] = svgtiny_PATH_BEZIER
;
545 p
[19] = y
- r
* KAPPA
;
546 p
[20] = x
- r
* KAPPA
;
550 p
[24] = svgtiny_PATH_BEZIER
;
551 p
[25] = x
+ r
* KAPPA
;
554 p
[28] = y
- r
* KAPPA
;
557 p
[31] = svgtiny_PATH_CLOSE
;
559 return svgtiny_add_path(p
, 32, &state
);
564 * Parse an <ellipse> element node.
567 svgtiny_code
svgtiny_parse_ellipse(xmlNode
*ellipse
,
568 struct svgtiny_parse_state state
)
570 float x
= 0, y
= 0, rx
= -1, ry
= -1;
574 for (attr
= ellipse
->properties
; attr
; attr
= attr
->next
) {
575 const char *name
= (const char *) attr
->name
;
576 const char *content
= (const char *) attr
->children
->content
;
577 if (strcmp(name
, "cx") == 0)
578 x
= svgtiny_parse_length(content
,
579 state
.viewport_width
, state
);
580 else if (strcmp(name
, "cy") == 0)
581 y
= svgtiny_parse_length(content
,
582 state
.viewport_height
, state
);
583 else if (strcmp(name
, "rx") == 0)
584 rx
= svgtiny_parse_length(content
,
585 state
.viewport_width
, state
);
586 else if (strcmp(name
, "ry") == 0)
587 ry
= svgtiny_parse_length(content
,
588 state
.viewport_width
, state
);
590 svgtiny_parse_paint_attributes(ellipse
, &state
);
591 svgtiny_parse_transform_attributes(ellipse
, &state
);
593 if (rx
< 0 || ry
< 0) {
594 state
.diagram
->error_line
= ellipse
->line
;
595 state
.diagram
->error_message
= "ellipse: rx or ry missing "
597 return svgtiny_SVG_ERROR
;
599 if (rx
== 0 || ry
== 0)
602 p
= malloc(32 * sizeof p
[0]);
604 return svgtiny_OUT_OF_MEMORY
;
606 p
[0] = svgtiny_PATH_MOVE
;
609 p
[3] = svgtiny_PATH_BEZIER
;
611 p
[5] = y
+ ry
* KAPPA
;
612 p
[6] = x
+ rx
* KAPPA
;
616 p
[10] = svgtiny_PATH_BEZIER
;
617 p
[11] = x
- rx
* KAPPA
;
620 p
[14] = y
+ ry
* KAPPA
;
623 p
[17] = svgtiny_PATH_BEZIER
;
625 p
[19] = y
- ry
* KAPPA
;
626 p
[20] = x
- rx
* KAPPA
;
630 p
[24] = svgtiny_PATH_BEZIER
;
631 p
[25] = x
+ rx
* KAPPA
;
634 p
[28] = y
- ry
* KAPPA
;
637 p
[31] = svgtiny_PATH_CLOSE
;
639 return svgtiny_add_path(p
, 32, &state
);
644 * Parse a <line> element node.
647 svgtiny_code
svgtiny_parse_line(xmlNode
*line
,
648 struct svgtiny_parse_state state
)
650 float x1
= 0, y1
= 0, x2
= 0, y2
= 0;
654 for (attr
= line
->properties
; attr
; attr
= attr
->next
) {
655 const char *name
= (const char *) attr
->name
;
656 const char *content
= (const char *) attr
->children
->content
;
657 if (strcmp(name
, "x1") == 0)
658 x1
= svgtiny_parse_length(content
,
659 state
.viewport_width
, state
);
660 else if (strcmp(name
, "y1") == 0)
661 y1
= svgtiny_parse_length(content
,
662 state
.viewport_height
, state
);
663 else if (strcmp(name
, "x2") == 0)
664 x2
= svgtiny_parse_length(content
,
665 state
.viewport_width
, state
);
666 else if (strcmp(name
, "y2") == 0)
667 y2
= svgtiny_parse_length(content
,
668 state
.viewport_height
, state
);
670 svgtiny_parse_paint_attributes(line
, &state
);
671 svgtiny_parse_transform_attributes(line
, &state
);
673 p
= malloc(7 * sizeof p
[0]);
675 return svgtiny_OUT_OF_MEMORY
;
677 p
[0] = svgtiny_PATH_MOVE
;
680 p
[3] = svgtiny_PATH_LINE
;
683 p
[6] = svgtiny_PATH_CLOSE
;
685 return svgtiny_add_path(p
, 7, &state
);
690 * Parse a <polyline> or <polygon> element node.
692 * http://www.w3.org/TR/SVG11/shapes#PolylineElement
693 * http://www.w3.org/TR/SVG11/shapes#PolygonElement
696 svgtiny_code
svgtiny_parse_poly(xmlNode
*poly
,
697 struct svgtiny_parse_state state
, bool polygon
)
703 svgtiny_parse_paint_attributes(poly
, &state
);
704 svgtiny_parse_transform_attributes(poly
, &state
);
706 /* read points attribute */
707 s
= points
= (char *) xmlGetProp(poly
, (const xmlChar
*) "points");
709 state
.diagram
->error_line
= poly
->line
;
710 state
.diagram
->error_message
=
711 "polyline/polygon: missing points attribute";
712 return svgtiny_SVG_ERROR
;
715 /* allocate space for path: it will never have more elements than s */
716 p
= malloc(sizeof p
[0] * strlen(s
));
719 return svgtiny_OUT_OF_MEMORY
;
722 /* parse s and build path */
723 for (i
= 0; s
[i
]; i
++)
731 if (sscanf(s
, "%f %f %n", &x
, &y
, &n
) == 2) {
733 p
[i
++] = svgtiny_PATH_MOVE
;
735 p
[i
++] = svgtiny_PATH_LINE
;
744 p
[i
++] = svgtiny_PATH_CLOSE
;
748 return svgtiny_add_path(p
, i
, &state
);
753 * Parse a <text> or <tspan> element node.
756 svgtiny_code
svgtiny_parse_text(xmlNode
*text
,
757 struct svgtiny_parse_state state
)
759 float x
, y
, width
, height
;
763 svgtiny_parse_position_attributes(text
, state
,
764 &x
, &y
, &width
, &height
);
765 svgtiny_parse_font_attributes(text
, &state
);
766 svgtiny_parse_transform_attributes(text
, &state
);
768 px
= state
.ctm
.a
* x
+ state
.ctm
.c
* y
+ state
.ctm
.e
;
769 py
= state
.ctm
.b
* x
+ state
.ctm
.d
* y
+ state
.ctm
.f
;
770 /* state.ctm.e = px - state.origin_x; */
771 /* state.ctm.f = py - state.origin_y; */
773 /*struct css_style style = state.style;
774 style.font_size.value.length.value *= state.ctm.a;*/
776 for (child
= text
->children
; child
; child
= child
->next
) {
777 svgtiny_code code
= svgtiny_OK
;
779 if (child
->type
== XML_TEXT_NODE
) {
780 struct svgtiny_shape
*shape
= svgtiny_add_shape(&state
);
782 return svgtiny_OUT_OF_MEMORY
;
783 shape
->text
= strdup((const char *) child
->content
);
786 state
.diagram
->shape_count
++;
788 } else if (child
->type
== XML_ELEMENT_NODE
&&
789 strcmp((const char *) child
->name
,
791 code
= svgtiny_parse_text(child
, state
);
794 if (!code
!= svgtiny_OK
)
803 * Parse x, y, width, and height attributes, if present.
806 void svgtiny_parse_position_attributes(const xmlNode
*node
,
807 const struct svgtiny_parse_state state
,
808 float *x
, float *y
, float *width
, float *height
)
814 *width
= state
.viewport_width
;
815 *height
= state
.viewport_height
;
817 for (attr
= node
->properties
; attr
; attr
= attr
->next
) {
818 const char *name
= (const char *) attr
->name
;
819 const char *content
= (const char *) attr
->children
->content
;
820 if (strcmp(name
, "x") == 0)
821 *x
= svgtiny_parse_length(content
,
822 state
.viewport_width
, state
);
823 else if (strcmp(name
, "y") == 0)
824 *y
= svgtiny_parse_length(content
,
825 state
.viewport_height
, state
);
826 else if (strcmp(name
, "width") == 0)
827 *width
= svgtiny_parse_length(content
,
828 state
.viewport_width
, state
);
829 else if (strcmp(name
, "height") == 0)
830 *height
= svgtiny_parse_length(content
,
831 state
.viewport_height
, state
);
837 * Parse a length as a number of pixels.
840 float svgtiny_parse_length(const char *s
, int viewport_size
,
841 const struct svgtiny_parse_state state
)
843 int num_length
= strspn(s
, "0123456789+-.");
844 const char *unit
= s
+ num_length
;
845 float n
= atof((const char *) s
);
846 float font_size
= 20; /*css_len2px(&state.style.font_size.value.length, 0);*/
852 } else if (unit
[0] == '%') {
853 return n
/ 100.0 * viewport_size
;
854 } else if (unit
[0] == 'e' && unit
[1] == 'm') {
855 return n
* font_size
;
856 } else if (unit
[0] == 'e' && unit
[1] == 'x') {
857 return n
/ 2.0 * font_size
;
858 } else if (unit
[0] == 'p' && unit
[1] == 'x') {
860 } else if (unit
[0] == 'p' && unit
[1] == 't') {
862 } else if (unit
[0] == 'p' && unit
[1] == 'c') {
864 } else if (unit
[0] == 'm' && unit
[1] == 'm') {
866 } else if (unit
[0] == 'c' && unit
[1] == 'm') {
868 } else if (unit
[0] == 'i' && unit
[1] == 'n') {
877 * Parse paint attributes, if present.
880 void svgtiny_parse_paint_attributes(const xmlNode
*node
,
881 struct svgtiny_parse_state
*state
)
885 for (attr
= node
->properties
; attr
; attr
= attr
->next
) {
886 const char *name
= (const char *) attr
->name
;
887 const char *content
= (const char *) attr
->children
->content
;
888 if (strcmp(name
, "fill") == 0)
889 svgtiny_parse_color(content
, &state
->fill
, state
);
890 else if (strcmp(name
, "stroke") == 0)
891 svgtiny_parse_color(content
, &state
->stroke
, state
);
892 else if (strcmp(name
, "stroke-width") == 0)
893 state
->stroke_width
= svgtiny_parse_length(content
,
894 state
->viewport_width
, *state
);
895 else if (strcmp(name
, "style") == 0) {
896 const char *style
= (const char *)
897 attr
->children
->content
;
900 if ((s
= strstr(style
, "fill:"))) {
904 value
= strndup(s
, strcspn(s
, "; "));
905 svgtiny_parse_color(value
, &state
->fill
, state
);
908 if ((s
= strstr(style
, "stroke:"))) {
912 value
= strndup(s
, strcspn(s
, "; "));
913 svgtiny_parse_color(value
, &state
->stroke
, state
);
916 if ((s
= strstr(style
, "stroke-width:"))) {
920 state
->stroke_width
= svgtiny_parse_length(s
,
921 state
->viewport_width
, *state
);
932 void svgtiny_parse_color(const char *s
, svgtiny_colour
*c
,
933 struct svgtiny_parse_state
*state
)
935 unsigned int r
, g
, b
;
937 size_t len
= strlen(s
);
938 char *id
= 0, *rparen
;
940 if (len
== 4 && s
[0] == '#') {
941 if (sscanf(s
+ 1, "%1x%1x%1x", &r
, &g
, &b
) == 3)
942 *c
= svgtiny_RGB(r
| r
<< 4, g
| g
<< 4, b
| b
<< 4);
944 } else if (len
== 7 && s
[0] == '#') {
945 if (sscanf(s
+ 1, "%2x%2x%2x", &r
, &g
, &b
) == 3)
946 *c
= svgtiny_RGB(r
, g
, b
);
948 } else if (10 <= len
&& s
[0] == 'r' && s
[1] == 'g' && s
[2] == 'b' &&
949 s
[3] == '(' && s
[len
- 1] == ')') {
950 if (sscanf(s
+ 4, "%u,%u,%u", &r
, &g
, &b
) == 3)
951 *c
= svgtiny_RGB(r
, g
, b
);
952 else if (sscanf(s
+ 4, "%f%%,%f%%,%f%%", &rf
, &gf
, &bf
) == 3) {
956 *c
= svgtiny_RGB(r
, g
, b
);
959 } else if (len
== 4 && strcmp(s
, "none") == 0) {
960 *c
= svgtiny_TRANSPARENT
;
962 } else if (5 < len
&& s
[0] == 'u' && s
[1] == 'r' && s
[2] == 'l' &&
968 rparen
= strchr(id
, ')');
971 svgtiny_find_gradient(id
, state
);
973 fprintf(stderr
, "linear_gradient_stop_count %i\n",
974 state
->linear_gradient_stop_count
);
975 if (state
->linear_gradient_stop_count
== 0)
976 *c
= svgtiny_TRANSPARENT
;
977 else if (state
->linear_gradient_stop_count
== 1)
978 *c
= state
->gradient_stop
[0].color
;
980 *c
= svgtiny_LINEAR_GRADIENT
;
984 const struct svgtiny_named_color
*named_color
;
985 named_color
= svgtiny_color_lookup(s
, strlen(s
));
987 *c
= named_color
->color
;
993 * Parse font attributes, if present.
996 void svgtiny_parse_font_attributes(const xmlNode
*node
,
997 struct svgtiny_parse_state
*state
)
1003 for (attr
= node
->properties
; attr
; attr
= attr
->next
) {
1004 if (strcmp((const char *) attr
->name
, "font-size") == 0) {
1005 /*if (css_parse_length(
1006 (const char *) attr->children->content,
1007 &state->style.font_size.value.length,
1009 state->style.font_size.size =
1010 CSS_FONT_SIZE_LENGTH;
1018 * Parse transform attributes, if present.
1020 * http://www.w3.org/TR/SVG11/coords#TransformAttribute
1023 void svgtiny_parse_transform_attributes(xmlNode
*node
,
1024 struct svgtiny_parse_state
*state
)
1028 /* parse transform */
1029 transform
= (char *) xmlGetProp(node
, (const xmlChar
*) "transform");
1031 svgtiny_parse_transform(transform
, &state
->ctm
.a
, &state
->ctm
.b
,
1032 &state
->ctm
.c
, &state
->ctm
.d
,
1033 &state
->ctm
.e
, &state
->ctm
.f
);
1040 * Parse a transform string.
1043 void svgtiny_parse_transform(char *s
, float *ma
, float *mb
,
1044 float *mc
, float *md
, float *me
, float *mf
)
1046 float a
, b
, c
, d
, e
, f
;
1047 float za
, zb
, zc
, zd
, ze
, zf
;
1052 for (i
= 0; s
[i
]; i
++)
1060 if (sscanf(s
, "matrix (%f %f %f %f %f %f) %n",
1061 &a
, &b
, &c
, &d
, &e
, &f
, &n
) == 6)
1063 else if (sscanf(s
, "translate (%f %f) %n",
1066 else if (sscanf(s
, "translate (%f) %n",
1069 else if (sscanf(s
, "scale (%f %f) %n",
1072 else if (sscanf(s
, "scale (%f) %n",
1075 else if (sscanf(s
, "rotate (%f %f %f) %n",
1076 &angle
, &x
, &y
, &n
) == 3) {
1077 angle
= angle
/ 180 * M_PI
;
1082 e
= -x
* cos(angle
) + y
* sin(angle
) + x
;
1083 f
= -x
* sin(angle
) - y
* cos(angle
) + y
;
1084 } else if (sscanf(s
, "rotate (%f) %n",
1086 angle
= angle
/ 180 * M_PI
;
1091 } else if (sscanf(s
, "skewX (%f) %n",
1093 angle
= angle
/ 180 * M_PI
;
1095 } else if (sscanf(s
, "skewY (%f) %n",
1097 angle
= angle
/ 180 * M_PI
;
1101 za
= *ma
* a
+ *mc
* b
;
1102 zb
= *mb
* a
+ *md
* b
;
1103 zc
= *ma
* c
+ *mc
* d
;
1104 zd
= *mb
* c
+ *md
* d
;
1105 ze
= *ma
* e
+ *mc
* f
+ *me
;
1106 zf
= *mb
* e
+ *md
* f
+ *mf
;
1119 * Add a path to the svgtiny_diagram.
1122 svgtiny_code
svgtiny_add_path(float *p
, unsigned int n
,
1123 struct svgtiny_parse_state
*state
)
1125 struct svgtiny_shape
*shape
;
1127 if (state
->fill
== svgtiny_LINEAR_GRADIENT
)
1128 return svgtiny_add_path_linear_gradient(p
, n
, state
);
1130 svgtiny_transform_path(p
, n
, state
);
1132 shape
= svgtiny_add_shape(state
);
1135 return svgtiny_OUT_OF_MEMORY
;
1138 shape
->path_length
= n
;
1139 state
->diagram
->shape_count
++;
1146 * Add a svgtiny_shape to the svgtiny_diagram.
1149 struct svgtiny_shape
*svgtiny_add_shape(struct svgtiny_parse_state
*state
)
1151 struct svgtiny_shape
*shape
= realloc(state
->diagram
->shape
,
1152 (state
->diagram
->shape_count
+ 1) *
1153 sizeof (state
->diagram
->shape
[0]));
1156 state
->diagram
->shape
= shape
;
1158 shape
+= state
->diagram
->shape_count
;
1160 shape
->path_length
= 0;
1162 shape
->fill
= state
->fill
;
1163 shape
->stroke
= state
->stroke
;
1164 shape
->stroke_width
= lroundf((float) state
->stroke_width
*
1165 (state
->ctm
.a
+ state
->ctm
.d
) / 2.0);
1166 if (0 < state
->stroke_width
&& shape
->stroke_width
== 0)
1167 shape
->stroke_width
= 1;
1174 * Apply the current transformation matrix to a path.
1177 void svgtiny_transform_path(float *p
, unsigned int n
,
1178 struct svgtiny_parse_state
*state
)
1182 for (j
= 0; j
!= n
; ) {
1183 unsigned int points
= 0;
1185 switch ((int) p
[j
]) {
1186 case svgtiny_PATH_MOVE
:
1187 case svgtiny_PATH_LINE
:
1190 case svgtiny_PATH_CLOSE
:
1193 case svgtiny_PATH_BEZIER
:
1200 for (k
= 0; k
!= points
; k
++) {
1201 float x0
= p
[j
], y0
= p
[j
+ 1];
1202 float x
= state
->ctm
.a
* x0
+ state
->ctm
.c
* y0
+
1204 float y
= state
->ctm
.b
* x0
+ state
->ctm
.d
* y0
+
1215 * Free all memory used by a diagram.
1218 void svgtiny_free(struct svgtiny_diagram
*svg
)
1223 for (i
= 0; i
!= svg
->shape_count
; i
++) {
1224 free(svg
->shape
[i
].path
);
1225 free(svg
->shape
[i
].text
);