2 * This file is part of Libsvgtiny
3 * Licensed under the MIT License,
4 * http://opensource.org/licenses/mit-license.php
5 * Copyright 2008 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 static svgtiny_code
svgtiny_parse_svg(xmlNode
*svg
,
23 struct svgtiny_parse_state state
);
24 static svgtiny_code
svgtiny_parse_path(xmlNode
*path
,
25 struct svgtiny_parse_state state
);
26 static svgtiny_code
svgtiny_parse_rect(xmlNode
*rect
,
27 struct svgtiny_parse_state state
);
28 static svgtiny_code
svgtiny_parse_circle(xmlNode
*circle
,
29 struct svgtiny_parse_state state
);
30 static svgtiny_code
svgtiny_parse_line(xmlNode
*line
,
31 struct svgtiny_parse_state state
);
32 static svgtiny_code
svgtiny_parse_poly(xmlNode
*poly
,
33 struct svgtiny_parse_state state
, bool polygon
);
34 static svgtiny_code
svgtiny_parse_text(xmlNode
*text
,
35 struct svgtiny_parse_state state
);
36 static void svgtiny_parse_position_attributes(const xmlNode
*node
,
37 const struct svgtiny_parse_state state
,
38 float *x
, float *y
, float *width
, float *height
);
39 static void svgtiny_parse_paint_attributes(const xmlNode
*node
,
40 struct svgtiny_parse_state
*state
);
41 static void svgtiny_parse_font_attributes(const xmlNode
*node
,
42 struct svgtiny_parse_state
*state
);
43 static void svgtiny_parse_transform_attributes(xmlNode
*node
,
44 struct svgtiny_parse_state
*state
);
45 static svgtiny_code
svgtiny_add_path(float *p
, unsigned int n
,
46 struct svgtiny_parse_state
*state
);
50 * Create a new svgtiny_diagram structure.
53 struct svgtiny_diagram
*svgtiny_create(void)
55 struct svgtiny_diagram
*diagram
;
57 diagram
= malloc(sizeof *diagram
);
62 diagram
->shape_count
= 0;
63 diagram
->error_line
= 0;
64 diagram
->error_message
= 0;
71 * Parse a block of memory into a svgtiny_diagram.
74 svgtiny_code
svgtiny_parse(struct svgtiny_diagram
*diagram
,
75 const char *buffer
, size_t size
, const char *url
,
76 int viewport_width
, int viewport_height
)
80 struct svgtiny_parse_state state
;
81 float x
, y
, width
, height
;
88 /* parse XML to tree */
89 document
= xmlReadMemory(buffer
, size
, url
, 0,
90 XML_PARSE_NONET
| XML_PARSE_COMPACT
);
92 return svgtiny_LIBXML_ERROR
;
94 /*xmlDebugDumpDocument(stderr, document);*/
96 /* find root <svg> element */
97 svg
= xmlDocGetRootElement(document
);
99 return svgtiny_NOT_SVG
;
100 if (strcmp((const char *) svg
->name
, "svg") != 0)
101 return svgtiny_NOT_SVG
;
103 /* get graphic dimensions */
104 state
.diagram
= diagram
;
105 state
.document
= document
;
106 state
.viewport_width
= viewport_width
;
107 state
.viewport_height
= viewport_height
;
108 svgtiny_parse_position_attributes(svg
, state
, &x
, &y
, &width
, &height
);
109 diagram
->width
= width
;
110 diagram
->height
= height
;
112 /* set up parsing state */
113 state
.viewport_width
= width
;
114 state
.viewport_height
= height
;
115 state
.ctm
.a
= 1; /*(float) viewport_width / (float) width;*/
118 state
.ctm
.d
= 1; /*(float) viewport_height / (float) height;*/
119 state
.ctm
.e
= 0; /*x;*/
120 state
.ctm
.f
= 0; /*y;*/
121 /*state.style = css_base_style;
122 state.style.font_size.value.length.value = option_font_size * 0.1;*/
123 state
.fill
= 0x000000;
124 state
.stroke
= svgtiny_TRANSPARENT
;
125 state
.stroke_width
= 1;
126 state
.linear_gradient_stop_count
= 0;
129 code
= svgtiny_parse_svg(svg
, state
);
132 xmlFreeDoc(document
);
139 * Parse a <svg> or <g> element node.
142 svgtiny_code
svgtiny_parse_svg(xmlNode
*svg
,
143 struct svgtiny_parse_state state
)
145 float x
, y
, width
, height
;
147 svgtiny_parse_position_attributes(svg
, state
, &x
, &y
, &width
, &height
);
148 svgtiny_parse_paint_attributes(svg
, &state
);
149 svgtiny_parse_font_attributes(svg
, &state
);
152 xmlAttr
*view_box
= xmlHasProp(svg
, (const xmlChar
*) "viewBox");
154 const char *s
= (const char *) view_box
->children
->content
;
155 float min_x
, min_y
, vwidth
, vheight
;
156 if (sscanf(s
, "%f,%f,%f,%f",
157 &min_x
, &min_y
, &vwidth
, &vheight
) == 4 ||
158 sscanf(s
, "%f %f %f %f",
159 &min_x
, &min_y
, &vwidth
, &vheight
) == 4) {
160 state
.ctm
.a
= (float) state
.viewport_width
/ vwidth
;
161 state
.ctm
.d
= (float) state
.viewport_height
/ vheight
;
162 state
.ctm
.e
+= -min_x
* state
.ctm
.a
;
163 state
.ctm
.f
+= -min_y
* state
.ctm
.d
;
167 svgtiny_parse_transform_attributes(svg
, &state
);
169 for (xmlNode
*child
= svg
->children
; child
; child
= child
->next
) {
170 svgtiny_code code
= svgtiny_OK
;
172 if (child
->type
== XML_ELEMENT_NODE
) {
173 const char *name
= (const char *) child
->name
;
174 if (strcmp(name
, "svg") == 0)
175 code
= svgtiny_parse_svg(child
, state
);
176 else if (strcmp(name
, "g") == 0)
177 code
= svgtiny_parse_svg(child
, state
);
178 else if (strcmp(name
, "a") == 0)
179 code
= svgtiny_parse_svg(child
, state
);
180 else if (strcmp(name
, "path") == 0)
181 code
= svgtiny_parse_path(child
, state
);
182 else if (strcmp(name
, "rect") == 0)
183 code
= svgtiny_parse_rect(child
, state
);
184 else if (strcmp(name
, "circle") == 0)
185 code
= svgtiny_parse_circle(child
, state
);
186 else if (strcmp(name
, "line") == 0)
187 code
= svgtiny_parse_line(child
, state
);
188 else if (strcmp(name
, "polyline") == 0)
189 code
= svgtiny_parse_poly(child
, state
, false);
190 else if (strcmp(name
, "polygon") == 0)
191 code
= svgtiny_parse_poly(child
, state
, true);
192 else if (strcmp(name
, "text") == 0)
193 code
= svgtiny_parse_text(child
, state
);
196 if (code
!= svgtiny_OK
)
206 * Parse a <path> element node.
208 * http://www.w3.org/TR/SVG11/paths#PathElement
211 svgtiny_code
svgtiny_parse_path(xmlNode
*path
,
212 struct svgtiny_parse_state state
)
216 svgtiny_parse_paint_attributes(path
, &state
);
217 svgtiny_parse_transform_attributes(path
, &state
);
219 /* read d attribute */
220 s
= path_d
= (char *) xmlGetProp(path
, (const xmlChar
*) "d");
222 state
.diagram
->error_line
= path
->line
;
223 state
.diagram
->error_message
= "path: missing d attribute";
224 return svgtiny_SVG_ERROR
;
227 /* allocate space for path: it will never have more elements than d */
228 float *p
= malloc(sizeof p
[0] * strlen(s
));
230 return svgtiny_OUT_OF_MEMORY
;
232 /* parse d and build path */
233 for (unsigned int i
= 0; s
[i
]; i
++)
237 float last_x
= 0, last_y
= 0;
238 float last_cubic_x
= 0, last_cubic_y
= 0;
239 float last_quad_x
= 0, last_quad_y
= 0;
243 float x
, y
, x1
, y1
, x2
, y2
;
246 /* moveto (M, m), lineto (L, l) (2 arguments) */
247 if (sscanf(s
, " %1[MmLl] %f %f %n", command
, &x
, &y
, &n
) == 3) {
248 /*LOG(("moveto or lineto"));*/
249 if (*command
== 'M' || *command
== 'm')
250 plot_command
= svgtiny_PATH_MOVE
;
252 plot_command
= svgtiny_PATH_LINE
;
254 p
[i
++] = plot_command
;
255 if ('a' <= *command
) {
259 p
[i
++] = last_cubic_x
= last_quad_x
= last_x
261 p
[i
++] = last_cubic_y
= last_quad_y
= last_y
264 plot_command
= svgtiny_PATH_LINE
;
265 } while (sscanf(s
, "%f %f %n", &x
, &y
, &n
) == 2);
267 /* closepath (Z, z) (no arguments) */
268 } else if (sscanf(s
, " %1[Zz] %n", command
, &n
) == 1) {
269 /*LOG(("closepath"));*/
270 p
[i
++] = svgtiny_PATH_CLOSE
;
273 /* horizontal lineto (H, h) (1 argument) */
274 } else if (sscanf(s
, " %1[Hh] %f %n", command
, &x
, &n
) == 2) {
275 /*LOG(("horizontal lineto"));*/
277 p
[i
++] = svgtiny_PATH_LINE
;
280 p
[i
++] = last_cubic_x
= last_quad_x
= last_x
282 p
[i
++] = last_cubic_y
= last_quad_y
= last_y
;
284 } while (sscanf(s
, "%f %n", &x
, &n
) == 1);
286 /* vertical lineto (V, v) (1 argument) */
287 } else if (sscanf(s
, " %1[Vv] %f %n", command
, &y
, &n
) == 2) {
288 /*LOG(("vertical lineto"));*/
290 p
[i
++] = svgtiny_PATH_LINE
;
293 p
[i
++] = last_cubic_x
= last_quad_x
= last_x
;
294 p
[i
++] = last_cubic_y
= last_quad_y
= last_y
297 } while (sscanf(s
, "%f %n", &x
, &n
) == 1);
299 /* curveto (C, c) (6 arguments) */
300 } else if (sscanf(s
, " %1[Cc] %f %f %f %f %f %f %n", command
,
301 &x1
, &y1
, &x2
, &y2
, &x
, &y
, &n
) == 7) {
302 /*LOG(("curveto"));*/
304 p
[i
++] = svgtiny_PATH_BEZIER
;
305 if (*command
== 'c') {
315 p
[i
++] = last_cubic_x
= x2
;
316 p
[i
++] = last_cubic_y
= y2
;
317 p
[i
++] = last_quad_x
= last_x
= x
;
318 p
[i
++] = last_quad_y
= last_y
= y
;
320 } while (sscanf(s
, "%f %f %f %f %f %f %n",
321 &x1
, &y1
, &x2
, &y2
, &x
, &y
, &n
) == 6);
323 /* shorthand/smooth curveto (S, s) (4 arguments) */
324 } else if (sscanf(s
, " %1[Ss] %f %f %f %f %n", command
,
325 &x2
, &y2
, &x
, &y
, &n
) == 5) {
326 /*LOG(("shorthand/smooth curveto"));*/
328 p
[i
++] = svgtiny_PATH_BEZIER
;
329 x1
= last_x
+ (last_x
- last_cubic_x
);
330 y1
= last_y
+ (last_y
- last_cubic_y
);
331 if (*command
== 's') {
339 p
[i
++] = last_cubic_x
= x2
;
340 p
[i
++] = last_cubic_y
= y2
;
341 p
[i
++] = last_quad_x
= last_x
= x
;
342 p
[i
++] = last_quad_y
= last_y
= y
;
344 } while (sscanf(s
, "%f %f %f %f %n",
345 &x2
, &y2
, &x
, &y
, &n
) == 4);
347 /* quadratic Bezier curveto (Q, q) (4 arguments) */
348 } else if (sscanf(s
, " %1[Qq] %f %f %f %f %n", command
,
349 &x1
, &y1
, &x
, &y
, &n
) == 5) {
350 /*LOG(("quadratic Bezier curveto"));*/
352 p
[i
++] = svgtiny_PATH_BEZIER
;
355 if (*command
== 'q') {
361 p
[i
++] = 1./3 * last_x
+ 2./3 * x1
;
362 p
[i
++] = 1./3 * last_y
+ 2./3 * y1
;
363 p
[i
++] = 2./3 * x1
+ 1./3 * x
;
364 p
[i
++] = 2./3 * y1
+ 1./3 * y
;
365 p
[i
++] = last_cubic_x
= last_x
= x
;
366 p
[i
++] = last_cubic_y
= last_y
= y
;
368 } while (sscanf(s
, "%f %f %f %f %n",
369 &x1
, &y1
, &x
, &y
, &n
) == 4);
371 /* shorthand/smooth quadratic Bezier curveto (T, t)
373 } else if (sscanf(s
, " %1[Tt] %f %f %n", command
,
375 /*LOG(("shorthand/smooth quadratic Bezier curveto"));*/
377 p
[i
++] = svgtiny_PATH_BEZIER
;
378 x1
= last_x
+ (last_x
- last_quad_x
);
379 y1
= last_y
+ (last_y
- last_quad_y
);
382 if (*command
== 't') {
388 p
[i
++] = 1./3 * last_x
+ 2./3 * x1
;
389 p
[i
++] = 1./3 * last_y
+ 2./3 * y1
;
390 p
[i
++] = 2./3 * x1
+ 1./3 * x
;
391 p
[i
++] = 2./3 * y1
+ 1./3 * y
;
392 p
[i
++] = last_cubic_x
= last_x
= x
;
393 p
[i
++] = last_cubic_y
= last_y
= y
;
395 } while (sscanf(s
, "%f %f %n",
399 /*LOG(("parse failed at \"%s\"", s));*/
406 return svgtiny_add_path(p
, i
, &state
);
411 * Parse a <rect> element node.
413 * http://www.w3.org/TR/SVG11/shapes#RectElement
416 svgtiny_code
svgtiny_parse_rect(xmlNode
*rect
,
417 struct svgtiny_parse_state state
)
419 float x
, y
, width
, height
;
421 svgtiny_parse_position_attributes(rect
, state
,
422 &x
, &y
, &width
, &height
);
423 svgtiny_parse_paint_attributes(rect
, &state
);
424 svgtiny_parse_transform_attributes(rect
, &state
);
426 float *p
= malloc(13 * sizeof p
[0]);
428 return svgtiny_OUT_OF_MEMORY
;
430 p
[0] = svgtiny_PATH_MOVE
;
433 p
[3] = svgtiny_PATH_LINE
;
436 p
[6] = svgtiny_PATH_LINE
;
439 p
[9] = svgtiny_PATH_LINE
;
442 p
[12] = svgtiny_PATH_CLOSE
;
444 return svgtiny_add_path(p
, 13, &state
);
449 * Parse a <circle> element node.
452 svgtiny_code
svgtiny_parse_circle(xmlNode
*circle
,
453 struct svgtiny_parse_state state
)
455 float x
= 0, y
= 0, r
= 0;
456 const float kappa
= 0.5522847498;
458 for (xmlAttr
*attr
= circle
->properties
; attr
; attr
= attr
->next
) {
459 const char *name
= (const char *) attr
->name
;
460 const char *content
= (const char *) attr
->children
->content
;
461 if (strcmp(name
, "cx") == 0)
462 x
= svgtiny_parse_length(content
,
463 state
.viewport_width
, state
);
464 else if (strcmp(name
, "cy") == 0)
465 y
= svgtiny_parse_length(content
,
466 state
.viewport_height
, state
);
467 else if (strcmp(name
, "r") == 0)
468 r
= svgtiny_parse_length(content
,
469 state
.viewport_width
, state
);
471 svgtiny_parse_paint_attributes(circle
, &state
);
472 svgtiny_parse_transform_attributes(circle
, &state
);
474 float *p
= malloc(32 * sizeof p
[0]);
476 return svgtiny_OUT_OF_MEMORY
;
478 p
[0] = svgtiny_PATH_MOVE
;
481 p
[3] = svgtiny_PATH_BEZIER
;
483 p
[5] = y
+ r
* kappa
;
484 p
[6] = x
- r
* kappa
;
488 p
[10] = svgtiny_PATH_BEZIER
;
489 p
[11] = x
+ r
* kappa
;
492 p
[14] = y
+ r
* kappa
;
495 p
[17] = svgtiny_PATH_BEZIER
;
497 p
[19] = y
- r
* kappa
;
498 p
[20] = x
+ r
* kappa
;
502 p
[24] = svgtiny_PATH_BEZIER
;
503 p
[25] = x
- r
* kappa
;
506 p
[28] = y
- r
* kappa
;
509 p
[31] = svgtiny_PATH_CLOSE
;
511 return svgtiny_add_path(p
, 32, &state
);
516 * Parse a <line> element node.
519 svgtiny_code
svgtiny_parse_line(xmlNode
*line
,
520 struct svgtiny_parse_state state
)
522 float x1
= 0, y1
= 0, x2
= 0, y2
= 0;
524 for (xmlAttr
*attr
= line
->properties
; attr
; attr
= attr
->next
) {
525 const char *name
= (const char *) attr
->name
;
526 const char *content
= (const char *) attr
->children
->content
;
527 if (strcmp(name
, "x1") == 0)
528 x1
= svgtiny_parse_length(content
,
529 state
.viewport_width
, state
);
530 else if (strcmp(name
, "y1") == 0)
531 y1
= svgtiny_parse_length(content
,
532 state
.viewport_height
, state
);
533 else if (strcmp(name
, "x2") == 0)
534 x2
= svgtiny_parse_length(content
,
535 state
.viewport_width
, state
);
536 else if (strcmp(name
, "y2") == 0)
537 y2
= svgtiny_parse_length(content
,
538 state
.viewport_height
, state
);
540 svgtiny_parse_paint_attributes(line
, &state
);
541 svgtiny_parse_transform_attributes(line
, &state
);
543 float *p
= malloc(7 * sizeof p
[0]);
545 return svgtiny_OUT_OF_MEMORY
;
547 p
[0] = svgtiny_PATH_MOVE
;
550 p
[3] = svgtiny_PATH_LINE
;
553 p
[6] = svgtiny_PATH_CLOSE
;
555 return svgtiny_add_path(p
, 7, &state
);
560 * Parse a <polyline> or <polygon> element node.
562 * http://www.w3.org/TR/SVG11/shapes#PolylineElement
563 * http://www.w3.org/TR/SVG11/shapes#PolygonElement
566 svgtiny_code
svgtiny_parse_poly(xmlNode
*poly
,
567 struct svgtiny_parse_state state
, bool polygon
)
571 svgtiny_parse_paint_attributes(poly
, &state
);
572 svgtiny_parse_transform_attributes(poly
, &state
);
574 /* read points attribute */
575 s
= points
= (char *) xmlGetProp(poly
, (const xmlChar
*) "points");
577 state
.diagram
->error_line
= poly
->line
;
578 state
.diagram
->error_message
=
579 "polyline/polygon: missing points attribute";
580 return svgtiny_SVG_ERROR
;
583 /* allocate space for path: it will never have more elements than s */
584 float *p
= malloc(sizeof p
[0] * strlen(s
));
587 return svgtiny_OUT_OF_MEMORY
;
590 /* parse s and build path */
591 for (unsigned int i
= 0; s
[i
]; i
++)
599 if (sscanf(s
, "%f %f %n", &x
, &y
, &n
) == 2) {
601 p
[i
++] = svgtiny_PATH_MOVE
;
603 p
[i
++] = svgtiny_PATH_LINE
;
612 p
[i
++] = svgtiny_PATH_CLOSE
;
616 return svgtiny_add_path(p
, i
, &state
);
621 * Parse a <text> or <tspan> element node.
624 svgtiny_code
svgtiny_parse_text(xmlNode
*text
,
625 struct svgtiny_parse_state state
)
627 float x
, y
, width
, height
;
629 svgtiny_parse_position_attributes(text
, state
,
630 &x
, &y
, &width
, &height
);
631 svgtiny_parse_font_attributes(text
, &state
);
632 svgtiny_parse_transform_attributes(text
, &state
);
634 float px
= state
.ctm
.a
* x
+ state
.ctm
.c
* y
+ state
.ctm
.e
;
635 float py
= state
.ctm
.b
* x
+ state
.ctm
.d
* y
+ state
.ctm
.f
;
636 /* state.ctm.e = px - state.origin_x; */
637 /* state.ctm.f = py - state.origin_y; */
639 /*struct css_style style = state.style;
640 style.font_size.value.length.value *= state.ctm.a;*/
642 for (xmlNode
*child
= text
->children
; child
; child
= child
->next
) {
643 svgtiny_code code
= svgtiny_OK
;
645 if (child
->type
== XML_TEXT_NODE
) {
646 struct svgtiny_shape
*shape
= svgtiny_add_shape(&state
);
648 return svgtiny_OUT_OF_MEMORY
;
649 shape
->text
= strdup((const char *) child
->content
);
652 state
.diagram
->shape_count
++;
654 } else if (child
->type
== XML_ELEMENT_NODE
&&
655 strcmp((const char *) child
->name
,
657 code
= svgtiny_parse_text(child
, state
);
660 if (!code
!= svgtiny_OK
)
669 * Parse x, y, width, and height attributes, if present.
672 void svgtiny_parse_position_attributes(const xmlNode
*node
,
673 const struct svgtiny_parse_state state
,
674 float *x
, float *y
, float *width
, float *height
)
678 *width
= state
.viewport_width
;
679 *height
= state
.viewport_height
;
681 for (xmlAttr
*attr
= node
->properties
; attr
; attr
= attr
->next
) {
682 const char *name
= (const char *) attr
->name
;
683 const char *content
= (const char *) attr
->children
->content
;
684 if (strcmp(name
, "x") == 0)
685 *x
= svgtiny_parse_length(content
,
686 state
.viewport_width
, state
);
687 else if (strcmp(name
, "y") == 0)
688 *y
= svgtiny_parse_length(content
,
689 state
.viewport_height
, state
);
690 else if (strcmp(name
, "width") == 0)
691 *width
= svgtiny_parse_length(content
,
692 state
.viewport_width
, state
);
693 else if (strcmp(name
, "height") == 0)
694 *height
= svgtiny_parse_length(content
,
695 state
.viewport_height
, state
);
701 * Parse a length as a number of pixels.
704 float svgtiny_parse_length(const char *s
, int viewport_size
,
705 const struct svgtiny_parse_state state
)
707 int num_length
= strspn(s
, "0123456789+-.");
708 const char *unit
= s
+ num_length
;
709 float n
= atof((const char *) s
);
710 float font_size
= 20; /*css_len2px(&state.style.font_size.value.length, 0);*/
714 } else if (unit
[0] == '%') {
715 return n
/ 100.0 * viewport_size
;
716 } else if (unit
[0] == 'e' && unit
[1] == 'm') {
717 return n
* font_size
;
718 } else if (unit
[0] == 'e' && unit
[1] == 'x') {
719 return n
/ 2.0 * font_size
;
720 } else if (unit
[0] == 'p' && unit
[1] == 'x') {
722 } else if (unit
[0] == 'p' && unit
[1] == 't') {
724 } else if (unit
[0] == 'p' && unit
[1] == 'c') {
726 } else if (unit
[0] == 'm' && unit
[1] == 'm') {
728 } else if (unit
[0] == 'c' && unit
[1] == 'm') {
730 } else if (unit
[0] == 'i' && unit
[1] == 'n') {
739 * Parse paint attributes, if present.
742 void svgtiny_parse_paint_attributes(const xmlNode
*node
,
743 struct svgtiny_parse_state
*state
)
745 for (const xmlAttr
*attr
= node
->properties
; attr
; attr
= attr
->next
) {
746 const char *name
= (const char *) attr
->name
;
747 const char *content
= (const char *) attr
->children
->content
;
748 if (strcmp(name
, "fill") == 0)
749 svgtiny_parse_color(content
, &state
->fill
, state
);
750 else if (strcmp(name
, "stroke") == 0)
751 svgtiny_parse_color(content
, &state
->stroke
, state
);
752 else if (strcmp(name
, "stroke-width") == 0)
753 state
->stroke_width
= svgtiny_parse_length(content
,
754 state
->viewport_width
, *state
);
755 else if (strcmp(name
, "style") == 0) {
756 const char *style
= (const char *)
757 attr
->children
->content
;
760 if ((s
= strstr(style
, "fill:"))) {
764 value
= strndup(s
, strcspn(s
, "; "));
765 svgtiny_parse_color(value
, &state
->fill
, state
);
768 if ((s
= strstr(style
, "stroke:"))) {
772 value
= strndup(s
, strcspn(s
, "; "));
773 svgtiny_parse_color(value
, &state
->stroke
, state
);
776 if ((s
= strstr(style
, "stroke-width:"))) {
780 state
->stroke_width
= svgtiny_parse_length(s
,
781 state
->viewport_width
, *state
);
792 void svgtiny_parse_color(const char *s
, svgtiny_colour
*c
,
793 struct svgtiny_parse_state
*state
)
795 unsigned int r
, g
, b
;
797 size_t len
= strlen(s
);
798 char *id
= 0, *rparen
;
800 if (len
== 4 && s
[0] == '#') {
801 if (sscanf(s
+ 1, "%1x%1x%1x", &r
, &g
, &b
) == 3)
802 *c
= svgtiny_RGB(r
| r
<< 4, g
| g
<< 4, b
| b
<< 4);
804 } else if (len
== 7 && s
[0] == '#') {
805 if (sscanf(s
+ 1, "%2x%2x%2x", &r
, &g
, &b
) == 3)
806 *c
= svgtiny_RGB(r
, g
, b
);
808 } else if (10 <= len
&& s
[0] == 'r' && s
[1] == 'g' && s
[2] == 'b' &&
809 s
[3] == '(' && s
[len
- 1] == ')') {
810 if (sscanf(s
+ 4, "%i,%i,%i", &r
, &g
, &b
) == 3)
811 *c
= svgtiny_RGB(r
, g
, b
);
812 else if (sscanf(s
+ 4, "%f%%,%f%%,%f%%", &rf
, &gf
, &bf
) == 3) {
816 *c
= svgtiny_RGB(r
, g
, b
);
819 } else if (len
== 4 && strcmp(s
, "none") == 0) {
820 *c
= svgtiny_TRANSPARENT
;
822 } else if (5 < len
&& s
[0] == 'u' && s
[1] == 'r' && s
[2] == 'l' &&
828 rparen
= strchr(id
, ')');
831 svgtiny_find_gradient(id
, state
);
833 fprintf(stderr
, "linear_gradient_stop_count %i\n",
834 state
->linear_gradient_stop_count
);
835 if (state
->linear_gradient_stop_count
== 0)
836 *c
= svgtiny_TRANSPARENT
;
837 else if (state
->linear_gradient_stop_count
== 1)
838 *c
= state
->gradient_stop
[0].color
;
840 *c
= svgtiny_LINEAR_GRADIENT
;
844 const struct svgtiny_named_color
*named_color
;
845 named_color
= svgtiny_color_lookup(s
, strlen(s
));
847 *c
= named_color
->color
;
853 * Parse font attributes, if present.
856 void svgtiny_parse_font_attributes(const xmlNode
*node
,
857 struct svgtiny_parse_state
*state
)
859 for (const xmlAttr
*attr
= node
->properties
; attr
; attr
= attr
->next
) {
860 if (strcmp((const char *) attr
->name
, "font-size") == 0) {
861 /*if (css_parse_length(
862 (const char *) attr->children->content,
863 &state->style.font_size.value.length,
865 state->style.font_size.size =
866 CSS_FONT_SIZE_LENGTH;
874 * Parse transform attributes, if present.
876 * http://www.w3.org/TR/SVG11/coords#TransformAttribute
879 void svgtiny_parse_transform_attributes(xmlNode
*node
,
880 struct svgtiny_parse_state
*state
)
883 float a
, b
, c
, d
, e
, f
;
884 float ctm_a
, ctm_b
, ctm_c
, ctm_d
, ctm_e
, ctm_f
;
888 /* parse transform */
889 s
= transform
= (char *) xmlGetProp(node
,
890 (const xmlChar
*) "transform");
892 for (unsigned int i
= 0; transform
[i
]; i
++)
893 if (transform
[i
] == ',')
900 if (sscanf(s
, "matrix (%f %f %f %f %f %f) %n",
901 &a
, &b
, &c
, &d
, &e
, &f
, &n
) == 6)
903 else if (sscanf(s
, "translate (%f %f) %n",
906 else if (sscanf(s
, "translate (%f) %n",
909 else if (sscanf(s
, "scale (%f %f) %n",
912 else if (sscanf(s
, "scale (%f) %n",
915 else if (sscanf(s
, "rotate (%f %f %f) %n",
916 &angle
, &x
, &y
, &n
) == 3) {
917 angle
= angle
/ 180 * M_PI
;
922 e
= -x
* cos(angle
) + y
* sin(angle
) + x
;
923 f
= -x
* sin(angle
) - y
* cos(angle
) + y
;
924 } else if (sscanf(s
, "rotate (%f) %n",
926 angle
= angle
/ 180 * M_PI
;
931 } else if (sscanf(s
, "skewX (%f) %n",
933 angle
= angle
/ 180 * M_PI
;
935 } else if (sscanf(s
, "skewY (%f) %n",
937 angle
= angle
/ 180 * M_PI
;
941 ctm_a
= state
->ctm
.a
* a
+ state
->ctm
.c
* b
;
942 ctm_b
= state
->ctm
.b
* a
+ state
->ctm
.d
* b
;
943 ctm_c
= state
->ctm
.a
* c
+ state
->ctm
.c
* d
;
944 ctm_d
= state
->ctm
.b
* c
+ state
->ctm
.d
* d
;
945 ctm_e
= state
->ctm
.a
* e
+ state
->ctm
.c
* f
+
947 ctm_f
= state
->ctm
.b
* e
+ state
->ctm
.d
* f
+
949 state
->ctm
.a
= ctm_a
;
950 state
->ctm
.b
= ctm_b
;
951 state
->ctm
.c
= ctm_c
;
952 state
->ctm
.d
= ctm_d
;
953 state
->ctm
.e
= ctm_e
;
954 state
->ctm
.f
= ctm_f
;
964 * Add a path to the svgtiny_diagram.
967 svgtiny_code
svgtiny_add_path(float *p
, unsigned int n
,
968 struct svgtiny_parse_state
*state
)
970 if (state
->fill
== svgtiny_LINEAR_GRADIENT
)
971 return svgtiny_add_path_linear_gradient(p
, n
, state
);
973 svgtiny_transform_path(p
, n
, state
);
975 struct svgtiny_shape
*shape
= svgtiny_add_shape(state
);
978 return svgtiny_OUT_OF_MEMORY
;
981 shape
->path_length
= n
;
982 state
->diagram
->shape_count
++;
989 * Add a svgtiny_shape to the svgtiny_diagram.
992 struct svgtiny_shape
*svgtiny_add_shape(struct svgtiny_parse_state
*state
)
994 struct svgtiny_shape
*shape
= realloc(state
->diagram
->shape
,
995 (state
->diagram
->shape_count
+ 1) *
996 sizeof (state
->diagram
->shape
[0]));
999 state
->diagram
->shape
= shape
;
1001 shape
+= state
->diagram
->shape_count
;
1003 shape
->path_length
= 0;
1005 shape
->fill
= state
->fill
;
1006 shape
->stroke
= state
->stroke
;
1007 shape
->stroke_width
= 1; /*state->stroke_width *
1008 (state->ctm.a + state->ctm.d) / 2;*/
1015 * Apply the current transformation matrix to a path.
1018 void svgtiny_transform_path(float *p
, unsigned int n
,
1019 struct svgtiny_parse_state
*state
)
1021 for (unsigned int j
= 0; j
!= n
; ) {
1022 unsigned int points
= 0;
1023 switch ((int) p
[j
]) {
1024 case svgtiny_PATH_MOVE
:
1025 case svgtiny_PATH_LINE
:
1028 case svgtiny_PATH_CLOSE
:
1031 case svgtiny_PATH_BEZIER
:
1038 for (unsigned int k
= 0; k
!= points
; k
++) {
1039 float x0
= p
[j
], y0
= p
[j
+ 1];
1040 float x
= state
->ctm
.a
* x0
+ state
->ctm
.c
* y0
+
1042 float y
= state
->ctm
.b
* x0
+ state
->ctm
.d
* y0
+
1053 * Free all memory used by a diagram.
1056 void svgtiny_free(struct svgtiny_diagram
*svg
)
1060 for (unsigned int i
= 0; i
!= svg
->shape_count
; i
++) {
1061 free(svg
->shape
[i
].path
);
1062 free(svg
->shape
[i
].text
);