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>
6 * Copyright 2012 Daniel Silverstone <dsilvers@netsurf-browser.org>
18 #include <dom/bindings/xml/xmlparser.h>
21 #include "svgtiny_internal.h"
24 #define M_PI 3.14159265358979323846
27 #define KAPPA 0.5522847498
29 static svgtiny_code
svgtiny_parse_svg(dom_element
*svg
,
30 struct svgtiny_parse_state state
);
31 static svgtiny_code
svgtiny_parse_path(dom_element
*path
,
32 struct svgtiny_parse_state state
);
33 static svgtiny_code
svgtiny_parse_rect(dom_element
*rect
,
34 struct svgtiny_parse_state state
);
35 static svgtiny_code
svgtiny_parse_circle(dom_element
*circle
,
36 struct svgtiny_parse_state state
);
37 static svgtiny_code
svgtiny_parse_ellipse(dom_element
*ellipse
,
38 struct svgtiny_parse_state state
);
39 static svgtiny_code
svgtiny_parse_line(dom_element
*line
,
40 struct svgtiny_parse_state state
);
41 static svgtiny_code
svgtiny_parse_poly(dom_element
*poly
,
42 struct svgtiny_parse_state state
, bool polygon
);
43 static svgtiny_code
svgtiny_parse_text(dom_element
*text
,
44 struct svgtiny_parse_state state
);
45 static void svgtiny_parse_position_attributes(const dom_element
*node
,
46 const struct svgtiny_parse_state state
,
47 float *x
, float *y
, float *width
, float *height
);
48 static void svgtiny_parse_paint_attributes(const dom_element
*node
,
49 struct svgtiny_parse_state
*state
);
50 static void svgtiny_parse_font_attributes(const dom_element
*node
,
51 struct svgtiny_parse_state
*state
);
52 static void svgtiny_parse_transform_attributes(dom_element
*node
,
53 struct svgtiny_parse_state
*state
);
54 static svgtiny_code
svgtiny_add_path(float *p
, unsigned int n
,
55 struct svgtiny_parse_state
*state
);
59 * Create a new svgtiny_diagram structure.
62 struct svgtiny_diagram
*svgtiny_create(void)
64 struct svgtiny_diagram
*diagram
;
66 diagram
= calloc(sizeof(*diagram
), 1);
75 static void ignore_msg(uint32_t severity
, void *ctx
, const char *msg
, ...)
83 * Parse a block of memory into a svgtiny_diagram.
86 svgtiny_code
svgtiny_parse(struct svgtiny_diagram
*diagram
,
87 const char *buffer
, size_t size
, const char *url
,
88 int viewport_width
, int viewport_height
)
90 dom_document
*document
;
92 dom_xml_parser
*parser
;
96 lwc_string
*svg_name_lwc
;
97 struct svgtiny_parse_state state
;
98 float x
, y
, width
, height
;
107 parser
= dom_xml_parser_create(NULL
, NULL
,
108 ignore_msg
, NULL
, &document
);
111 return svgtiny_LIBDOM_ERROR
;
113 err
= dom_xml_parser_parse_chunk(parser
, (uint8_t *)buffer
, size
);
114 if (err
!= DOM_XML_OK
) {
115 dom_node_unref(document
);
116 dom_xml_parser_destroy(parser
);
117 return svgtiny_LIBDOM_ERROR
;
120 err
= dom_xml_parser_completed(parser
);
121 if (err
!= DOM_XML_OK
) {
122 dom_node_unref(document
);
123 dom_xml_parser_destroy(parser
);
124 return svgtiny_LIBDOM_ERROR
;
127 /* We're done parsing, drop the parser.
128 * We now own the document entirely.
130 dom_xml_parser_destroy(parser
);
132 /* find root <svg> element */
133 exc
= dom_document_get_document_element(document
, &svg
);
134 if (exc
!= DOM_NO_ERR
) {
135 dom_node_unref(document
);
136 return svgtiny_LIBDOM_ERROR
;
138 exc
= dom_node_get_node_name(svg
, &svg_name
);
139 if (exc
!= DOM_NO_ERR
) {
141 dom_node_unref(document
);
142 return svgtiny_LIBDOM_ERROR
;
144 if (lwc_intern_string("svg", 3 /* SLEN("svg") */,
145 &svg_name_lwc
) != lwc_error_ok
) {
146 dom_string_unref(svg_name
);
148 dom_node_unref(document
);
149 return svgtiny_LIBDOM_ERROR
;
151 if (!dom_string_caseless_lwc_isequal(svg_name
, svg_name_lwc
)) {
152 lwc_string_unref(svg_name_lwc
);
153 dom_string_unref(svg_name
);
155 dom_node_unref(document
);
156 return svgtiny_NOT_SVG
;
159 lwc_string_unref(svg_name_lwc
);
160 dom_string_unref(svg_name
);
162 /* get graphic dimensions */
163 state
.diagram
= diagram
;
164 state
.document
= document
;
165 state
.viewport_width
= viewport_width
;
166 state
.viewport_height
= viewport_height
;
168 #define SVGTINY_STRING_ACTION(s) \
169 if (dom_string_create_interned((const uint8_t *) #s, \
170 strlen(#s), &state.interned_##s) \
172 code = svgtiny_LIBDOM_ERROR; \
175 #include "svgtiny_strings.h"
176 #undef SVGTINY_STRING_ACTION
178 svgtiny_parse_position_attributes(svg
, state
, &x
, &y
, &width
, &height
);
179 diagram
->width
= width
;
180 diagram
->height
= height
;
182 /* set up parsing state */
183 state
.viewport_width
= width
;
184 state
.viewport_height
= height
;
185 state
.ctm
.a
= 1; /*(float) viewport_width / (float) width;*/
188 state
.ctm
.d
= 1; /*(float) viewport_height / (float) height;*/
189 state
.ctm
.e
= 0; /*x;*/
190 state
.ctm
.f
= 0; /*y;*/
191 /*state.style = css_base_style;
192 state.style.font_size.value.length.value = option_font_size * 0.1;*/
193 state
.fill
= 0x000000;
194 state
.stroke
= svgtiny_TRANSPARENT
;
195 state
.stroke_width
= 1;
196 state
.linear_gradient_stop_count
= 0;
199 code
= svgtiny_parse_svg(svg
, state
);
202 dom_node_unref(document
);
205 #define SVGTINY_STRING_ACTION(s) \
206 if (state.interned_##s != NULL) \
207 dom_string_unref(state.interned_##s);
208 //#include "svgtiny_strings.h"
209 #undef SVGTINY_STRING_ACTION
215 * Parse a <svg> or <g> element node.
218 svgtiny_code
svgtiny_parse_svg(dom_element
*svg
,
219 struct svgtiny_parse_state state
)
221 float x
, y
, width
, height
;
222 dom_string
*view_box
;
226 svgtiny_parse_position_attributes(svg
, state
, &x
, &y
, &width
, &height
);
227 svgtiny_parse_paint_attributes(svg
, &state
);
228 svgtiny_parse_font_attributes(svg
, &state
);
230 exc
= dom_element_get_attribute(svg
, state
.interned_viewBox
,
232 if (exc
!= DOM_NO_ERR
) {
233 return svgtiny_LIBDOM_ERROR
;
237 char *s
= strndup(dom_string_data(view_box
),
238 dom_string_length(view_box
));
239 float min_x
, min_y
, vwidth
, vheight
;
240 if (sscanf(s
, "%f,%f,%f,%f",
241 &min_x
, &min_y
, &vwidth
, &vheight
) == 4 ||
242 sscanf(s
, "%f %f %f %f",
243 &min_x
, &min_y
, &vwidth
, &vheight
) == 4) {
244 state
.ctm
.a
= (float) state
.viewport_width
/ vwidth
;
245 state
.ctm
.d
= (float) state
.viewport_height
/ vheight
;
246 state
.ctm
.e
+= -min_x
* state
.ctm
.a
;
247 state
.ctm
.f
+= -min_y
* state
.ctm
.d
;
250 dom_string_unref(view_box
);
253 svgtiny_parse_transform_attributes(svg
, &state
);
255 exc
= dom_node_get_first_child(svg
, &child
);
256 if (exc
!= DOM_NO_ERR
) {
257 return svgtiny_LIBDOM_ERROR
;
259 while (child
!= NULL
) {
261 dom_node_type nodetype
;
262 svgtiny_code code
= svgtiny_OK
;
264 exc
= dom_node_get_node_type(child
, &nodetype
);
265 if (exc
!= DOM_NO_ERR
) {
266 dom_node_unref(child
);
267 return svgtiny_LIBDOM_ERROR
;
269 if (nodetype
== DOM_ELEMENT_NODE
) {
270 dom_string
*nodename
;
271 exc
= dom_node_get_node_name(child
, &nodename
);
272 if (exc
!= DOM_NO_ERR
) {
273 dom_node_unref(child
);
274 return svgtiny_LIBDOM_ERROR
;
276 if (dom_string_caseless_isequal(state
.interned_svg
,
278 code
= svgtiny_parse_svg(child
, state
);
279 else if (dom_string_caseless_isequal(state
.interned_g
,
281 code
= svgtiny_parse_svg(child
, state
);
282 else if (dom_string_caseless_isequal(state
.interned_a
,
284 code
= svgtiny_parse_svg(child
, state
);
285 else if (dom_string_caseless_isequal(state
.interned_path
,
287 code
= svgtiny_parse_path(child
, state
);
288 else if (dom_string_caseless_isequal(state
.interned_rect
,
290 code
= svgtiny_parse_rect(child
, state
);
291 else if (dom_string_caseless_isequal(state
.interned_circle
,
293 code
= svgtiny_parse_circle(child
, state
);
294 else if (dom_string_caseless_isequal(state
.interned_ellipse
,
296 code
= svgtiny_parse_ellipse(child
, state
);
297 else if (dom_string_caseless_isequal(state
.interned_line
,
299 code
= svgtiny_parse_line(child
, state
);
300 else if (dom_string_caseless_isequal(state
.interned_polyline
,
302 code
= svgtiny_parse_poly(child
, state
, false);
303 else if (dom_string_caseless_isequal(state
.interned_polygon
,
305 code
= svgtiny_parse_poly(child
, state
, true);
306 else if (dom_string_caseless_isequal(state
.interned_text
,
308 code
= svgtiny_parse_text(child
, state
);
309 dom_string_unref(nodename
);
311 if (code
!= svgtiny_OK
) {
312 dom_node_unref(child
);
315 exc
= dom_node_get_next_sibling(child
, &next
);
316 dom_node_unref(child
);
317 if (exc
!= DOM_NO_ERR
) {
318 return svgtiny_LIBDOM_ERROR
;
329 * Parse a <path> element node.
331 * http://www.w3.org/TR/SVG11/paths#PathElement
334 svgtiny_code
svgtiny_parse_path(dom_element
*path
,
335 struct svgtiny_parse_state state
)
340 float last_x
= 0, last_y
= 0;
341 float last_cubic_x
= 0, last_cubic_y
= 0;
342 float last_quad_x
= 0, last_quad_y
= 0;
344 svgtiny_parse_paint_attributes(path
, &state
);
345 svgtiny_parse_transform_attributes(path
, &state
);
347 /* read d attribute */
348 s
= path_d
= (char *) xmlGetProp(path
, (const xmlChar
*) "d");
350 state
.diagram
->error_line
= path
->line
;
351 state
.diagram
->error_message
= "path: missing d attribute";
352 return svgtiny_SVG_ERROR
;
355 /* allocate space for path: it will never have more elements than d */
356 p
= malloc(sizeof p
[0] * strlen(s
));
358 return svgtiny_OUT_OF_MEMORY
;
360 /* parse d and build path */
361 for (i
= 0; s
[i
]; i
++)
368 float x
, y
, x1
, y1
, x2
, y2
, rx
, ry
, rotation
, large_arc
, sweep
;
371 /* moveto (M, m), lineto (L, l) (2 arguments) */
372 if (sscanf(s
, " %1[MmLl] %f %f %n", command
, &x
, &y
, &n
) == 3) {
373 /*LOG(("moveto or lineto"));*/
374 if (*command
== 'M' || *command
== 'm')
375 plot_command
= svgtiny_PATH_MOVE
;
377 plot_command
= svgtiny_PATH_LINE
;
379 p
[i
++] = plot_command
;
380 if ('a' <= *command
) {
384 p
[i
++] = last_cubic_x
= last_quad_x
= last_x
386 p
[i
++] = last_cubic_y
= last_quad_y
= last_y
389 plot_command
= svgtiny_PATH_LINE
;
390 } while (sscanf(s
, "%f %f %n", &x
, &y
, &n
) == 2);
392 /* closepath (Z, z) (no arguments) */
393 } else if (sscanf(s
, " %1[Zz] %n", command
, &n
) == 1) {
394 /*LOG(("closepath"));*/
395 p
[i
++] = svgtiny_PATH_CLOSE
;
398 /* horizontal lineto (H, h) (1 argument) */
399 } else if (sscanf(s
, " %1[Hh] %f %n", command
, &x
, &n
) == 2) {
400 /*LOG(("horizontal lineto"));*/
402 p
[i
++] = svgtiny_PATH_LINE
;
405 p
[i
++] = last_cubic_x
= last_quad_x
= last_x
407 p
[i
++] = last_cubic_y
= last_quad_y
= last_y
;
409 } while (sscanf(s
, "%f %n", &x
, &n
) == 1);
411 /* vertical lineto (V, v) (1 argument) */
412 } else if (sscanf(s
, " %1[Vv] %f %n", command
, &y
, &n
) == 2) {
413 /*LOG(("vertical lineto"));*/
415 p
[i
++] = svgtiny_PATH_LINE
;
418 p
[i
++] = last_cubic_x
= last_quad_x
= last_x
;
419 p
[i
++] = last_cubic_y
= last_quad_y
= last_y
422 } while (sscanf(s
, "%f %n", &x
, &n
) == 1);
424 /* curveto (C, c) (6 arguments) */
425 } else if (sscanf(s
, " %1[Cc] %f %f %f %f %f %f %n", command
,
426 &x1
, &y1
, &x2
, &y2
, &x
, &y
, &n
) == 7) {
427 /*LOG(("curveto"));*/
429 p
[i
++] = svgtiny_PATH_BEZIER
;
430 if (*command
== 'c') {
440 p
[i
++] = last_cubic_x
= x2
;
441 p
[i
++] = last_cubic_y
= y2
;
442 p
[i
++] = last_quad_x
= last_x
= x
;
443 p
[i
++] = last_quad_y
= last_y
= y
;
445 } while (sscanf(s
, "%f %f %f %f %f %f %n",
446 &x1
, &y1
, &x2
, &y2
, &x
, &y
, &n
) == 6);
448 /* shorthand/smooth curveto (S, s) (4 arguments) */
449 } else if (sscanf(s
, " %1[Ss] %f %f %f %f %n", command
,
450 &x2
, &y2
, &x
, &y
, &n
) == 5) {
451 /*LOG(("shorthand/smooth curveto"));*/
453 p
[i
++] = svgtiny_PATH_BEZIER
;
454 x1
= last_x
+ (last_x
- last_cubic_x
);
455 y1
= last_y
+ (last_y
- last_cubic_y
);
456 if (*command
== 's') {
464 p
[i
++] = last_cubic_x
= x2
;
465 p
[i
++] = last_cubic_y
= y2
;
466 p
[i
++] = last_quad_x
= last_x
= x
;
467 p
[i
++] = last_quad_y
= last_y
= y
;
469 } while (sscanf(s
, "%f %f %f %f %n",
470 &x2
, &y2
, &x
, &y
, &n
) == 4);
472 /* quadratic Bezier curveto (Q, q) (4 arguments) */
473 } else if (sscanf(s
, " %1[Qq] %f %f %f %f %n", command
,
474 &x1
, &y1
, &x
, &y
, &n
) == 5) {
475 /*LOG(("quadratic Bezier curveto"));*/
477 p
[i
++] = svgtiny_PATH_BEZIER
;
480 if (*command
== 'q') {
486 p
[i
++] = 1./3 * last_x
+ 2./3 * x1
;
487 p
[i
++] = 1./3 * last_y
+ 2./3 * y1
;
488 p
[i
++] = 2./3 * x1
+ 1./3 * x
;
489 p
[i
++] = 2./3 * y1
+ 1./3 * y
;
490 p
[i
++] = last_cubic_x
= last_x
= x
;
491 p
[i
++] = last_cubic_y
= last_y
= y
;
493 } while (sscanf(s
, "%f %f %f %f %n",
494 &x1
, &y1
, &x
, &y
, &n
) == 4);
496 /* shorthand/smooth quadratic Bezier curveto (T, t)
498 } else if (sscanf(s
, " %1[Tt] %f %f %n", command
,
500 /*LOG(("shorthand/smooth quadratic Bezier curveto"));*/
502 p
[i
++] = svgtiny_PATH_BEZIER
;
503 x1
= last_x
+ (last_x
- last_quad_x
);
504 y1
= last_y
+ (last_y
- last_quad_y
);
507 if (*command
== 't') {
513 p
[i
++] = 1./3 * last_x
+ 2./3 * x1
;
514 p
[i
++] = 1./3 * last_y
+ 2./3 * y1
;
515 p
[i
++] = 2./3 * x1
+ 1./3 * x
;
516 p
[i
++] = 2./3 * y1
+ 1./3 * y
;
517 p
[i
++] = last_cubic_x
= last_x
= x
;
518 p
[i
++] = last_cubic_y
= last_y
= y
;
520 } while (sscanf(s
, "%f %f %n",
523 /* elliptical arc (A, a) (7 arguments) */
524 } else if (sscanf(s
, " %1[Aa] %f %f %f %f %f %f %f %n", command
,
525 &rx
, &ry
, &rotation
, &large_arc
, &sweep
,
528 p
[i
++] = svgtiny_PATH_LINE
;
529 if (*command
== 'a') {
533 p
[i
++] = last_cubic_x
= last_quad_x
= last_x
535 p
[i
++] = last_cubic_y
= last_quad_y
= last_y
538 } while (sscanf(s
, "%f %f %f %f %f %f %f %n",
539 &rx
, &ry
, &rotation
, &large_arc
, &sweep
,
543 fprintf(stderr
, "parse failed at \"%s\"\n", s
);
551 /* no real segments in path */
556 return svgtiny_add_path(p
, i
, &state
);
561 * Parse a <rect> element node.
563 * http://www.w3.org/TR/SVG11/shapes#RectElement
566 svgtiny_code
svgtiny_parse_rect(dom_element
*rect
,
567 struct svgtiny_parse_state state
)
569 float x
, y
, width
, height
;
572 svgtiny_parse_position_attributes(rect
, state
,
573 &x
, &y
, &width
, &height
);
574 svgtiny_parse_paint_attributes(rect
, &state
);
575 svgtiny_parse_transform_attributes(rect
, &state
);
577 p
= malloc(13 * sizeof p
[0]);
579 return svgtiny_OUT_OF_MEMORY
;
581 p
[0] = svgtiny_PATH_MOVE
;
584 p
[3] = svgtiny_PATH_LINE
;
587 p
[6] = svgtiny_PATH_LINE
;
590 p
[9] = svgtiny_PATH_LINE
;
593 p
[12] = svgtiny_PATH_CLOSE
;
595 return svgtiny_add_path(p
, 13, &state
);
600 * Parse a <circle> element node.
603 svgtiny_code
svgtiny_parse_circle(dom_element
*circle
,
604 struct svgtiny_parse_state state
)
606 float x
= 0, y
= 0, r
= -1;
610 for (attr
= circle
->properties
; attr
; attr
= attr
->next
) {
611 const char *name
= (const char *) attr
->name
;
612 const char *content
= (const char *) attr
->children
->content
;
613 if (strcmp(name
, "cx") == 0)
614 x
= svgtiny_parse_length(content
,
615 state
.viewport_width
, state
);
616 else if (strcmp(name
, "cy") == 0)
617 y
= svgtiny_parse_length(content
,
618 state
.viewport_height
, state
);
619 else if (strcmp(name
, "r") == 0)
620 r
= svgtiny_parse_length(content
,
621 state
.viewport_width
, state
);
623 svgtiny_parse_paint_attributes(circle
, &state
);
624 svgtiny_parse_transform_attributes(circle
, &state
);
627 state
.diagram
->error_line
= circle
->line
;
628 state
.diagram
->error_message
= "circle: r missing or negative";
629 return svgtiny_SVG_ERROR
;
634 p
= malloc(32 * sizeof p
[0]);
636 return svgtiny_OUT_OF_MEMORY
;
638 p
[0] = svgtiny_PATH_MOVE
;
641 p
[3] = svgtiny_PATH_BEZIER
;
643 p
[5] = y
+ r
* KAPPA
;
644 p
[6] = x
+ r
* KAPPA
;
648 p
[10] = svgtiny_PATH_BEZIER
;
649 p
[11] = x
- r
* KAPPA
;
652 p
[14] = y
+ r
* KAPPA
;
655 p
[17] = svgtiny_PATH_BEZIER
;
657 p
[19] = y
- r
* KAPPA
;
658 p
[20] = x
- r
* KAPPA
;
662 p
[24] = svgtiny_PATH_BEZIER
;
663 p
[25] = x
+ r
* KAPPA
;
666 p
[28] = y
- r
* KAPPA
;
669 p
[31] = svgtiny_PATH_CLOSE
;
671 return svgtiny_add_path(p
, 32, &state
);
676 * Parse an <ellipse> element node.
679 svgtiny_code
svgtiny_parse_ellipse(dom_element
*ellipse
,
680 struct svgtiny_parse_state state
)
682 float x
= 0, y
= 0, rx
= -1, ry
= -1;
686 for (attr
= ellipse
->properties
; attr
; attr
= attr
->next
) {
687 const char *name
= (const char *) attr
->name
;
688 const char *content
= (const char *) attr
->children
->content
;
689 if (strcmp(name
, "cx") == 0)
690 x
= svgtiny_parse_length(content
,
691 state
.viewport_width
, state
);
692 else if (strcmp(name
, "cy") == 0)
693 y
= svgtiny_parse_length(content
,
694 state
.viewport_height
, state
);
695 else if (strcmp(name
, "rx") == 0)
696 rx
= svgtiny_parse_length(content
,
697 state
.viewport_width
, state
);
698 else if (strcmp(name
, "ry") == 0)
699 ry
= svgtiny_parse_length(content
,
700 state
.viewport_width
, state
);
702 svgtiny_parse_paint_attributes(ellipse
, &state
);
703 svgtiny_parse_transform_attributes(ellipse
, &state
);
705 if (rx
< 0 || ry
< 0) {
706 state
.diagram
->error_line
= ellipse
->line
;
707 state
.diagram
->error_message
= "ellipse: rx or ry missing "
709 return svgtiny_SVG_ERROR
;
711 if (rx
== 0 || ry
== 0)
714 p
= malloc(32 * sizeof p
[0]);
716 return svgtiny_OUT_OF_MEMORY
;
718 p
[0] = svgtiny_PATH_MOVE
;
721 p
[3] = svgtiny_PATH_BEZIER
;
723 p
[5] = y
+ ry
* KAPPA
;
724 p
[6] = x
+ rx
* KAPPA
;
728 p
[10] = svgtiny_PATH_BEZIER
;
729 p
[11] = x
- rx
* KAPPA
;
732 p
[14] = y
+ ry
* KAPPA
;
735 p
[17] = svgtiny_PATH_BEZIER
;
737 p
[19] = y
- ry
* KAPPA
;
738 p
[20] = x
- rx
* KAPPA
;
742 p
[24] = svgtiny_PATH_BEZIER
;
743 p
[25] = x
+ rx
* KAPPA
;
746 p
[28] = y
- ry
* KAPPA
;
749 p
[31] = svgtiny_PATH_CLOSE
;
751 return svgtiny_add_path(p
, 32, &state
);
756 * Parse a <line> element node.
759 svgtiny_code
svgtiny_parse_line(dom_element
*line
,
760 struct svgtiny_parse_state state
)
762 float x1
= 0, y1
= 0, x2
= 0, y2
= 0;
766 for (attr
= line
->properties
; attr
; attr
= attr
->next
) {
767 const char *name
= (const char *) attr
->name
;
768 const char *content
= (const char *) attr
->children
->content
;
769 if (strcmp(name
, "x1") == 0)
770 x1
= svgtiny_parse_length(content
,
771 state
.viewport_width
, state
);
772 else if (strcmp(name
, "y1") == 0)
773 y1
= svgtiny_parse_length(content
,
774 state
.viewport_height
, state
);
775 else if (strcmp(name
, "x2") == 0)
776 x2
= svgtiny_parse_length(content
,
777 state
.viewport_width
, state
);
778 else if (strcmp(name
, "y2") == 0)
779 y2
= svgtiny_parse_length(content
,
780 state
.viewport_height
, state
);
782 svgtiny_parse_paint_attributes(line
, &state
);
783 svgtiny_parse_transform_attributes(line
, &state
);
785 p
= malloc(7 * sizeof p
[0]);
787 return svgtiny_OUT_OF_MEMORY
;
789 p
[0] = svgtiny_PATH_MOVE
;
792 p
[3] = svgtiny_PATH_LINE
;
795 p
[6] = svgtiny_PATH_CLOSE
;
797 return svgtiny_add_path(p
, 7, &state
);
802 * Parse a <polyline> or <polygon> element node.
804 * http://www.w3.org/TR/SVG11/shapes#PolylineElement
805 * http://www.w3.org/TR/SVG11/shapes#PolygonElement
808 svgtiny_code
svgtiny_parse_poly(dom_element
*poly
,
809 struct svgtiny_parse_state state
, bool polygon
)
815 svgtiny_parse_paint_attributes(poly
, &state
);
816 svgtiny_parse_transform_attributes(poly
, &state
);
818 /* read points attribute */
819 s
= points
= (char *) xmlGetProp(poly
, (const xmlChar
*) "points");
821 state
.diagram
->error_line
= poly
->line
;
822 state
.diagram
->error_message
=
823 "polyline/polygon: missing points attribute";
824 return svgtiny_SVG_ERROR
;
827 /* allocate space for path: it will never have more elements than s */
828 p
= malloc(sizeof p
[0] * strlen(s
));
831 return svgtiny_OUT_OF_MEMORY
;
834 /* parse s and build path */
835 for (i
= 0; s
[i
]; i
++)
843 if (sscanf(s
, "%f %f %n", &x
, &y
, &n
) == 2) {
845 p
[i
++] = svgtiny_PATH_MOVE
;
847 p
[i
++] = svgtiny_PATH_LINE
;
856 p
[i
++] = svgtiny_PATH_CLOSE
;
860 return svgtiny_add_path(p
, i
, &state
);
865 * Parse a <text> or <tspan> element node.
868 svgtiny_code
svgtiny_parse_text(dom_element
*text
,
869 struct svgtiny_parse_state state
)
871 float x
, y
, width
, height
;
875 svgtiny_parse_position_attributes(text
, state
,
876 &x
, &y
, &width
, &height
);
877 svgtiny_parse_font_attributes(text
, &state
);
878 svgtiny_parse_transform_attributes(text
, &state
);
880 px
= state
.ctm
.a
* x
+ state
.ctm
.c
* y
+ state
.ctm
.e
;
881 py
= state
.ctm
.b
* x
+ state
.ctm
.d
* y
+ state
.ctm
.f
;
882 /* state.ctm.e = px - state.origin_x; */
883 /* state.ctm.f = py - state.origin_y; */
885 /*struct css_style style = state.style;
886 style.font_size.value.length.value *= state.ctm.a;*/
888 for (child
= text
->children
; child
; child
= child
->next
) {
889 svgtiny_code code
= svgtiny_OK
;
891 if (child
->type
== XML_TEXT_NODE
) {
892 struct svgtiny_shape
*shape
= svgtiny_add_shape(&state
);
894 return svgtiny_OUT_OF_MEMORY
;
895 shape
->text
= strdup((const char *) child
->content
);
898 state
.diagram
->shape_count
++;
900 } else if (child
->type
== XML_ELEMENT_NODE
&&
901 strcmp((const char *) child
->name
,
903 code
= svgtiny_parse_text(child
, state
);
906 if (!code
!= svgtiny_OK
)
915 * Parse x, y, width, and height attributes, if present.
918 void svgtiny_parse_position_attributes(const dom_element
*node
,
919 const struct svgtiny_parse_state state
,
920 float *x
, float *y
, float *width
, float *height
)
926 *width
= state
.viewport_width
;
927 *height
= state
.viewport_height
;
929 for (attr
= node
->properties
; attr
; attr
= attr
->next
) {
930 const char *name
= (const char *) attr
->name
;
931 const char *content
= (const char *) attr
->children
->content
;
932 if (strcmp(name
, "x") == 0)
933 *x
= svgtiny_parse_length(content
,
934 state
.viewport_width
, state
);
935 else if (strcmp(name
, "y") == 0)
936 *y
= svgtiny_parse_length(content
,
937 state
.viewport_height
, state
);
938 else if (strcmp(name
, "width") == 0)
939 *width
= svgtiny_parse_length(content
,
940 state
.viewport_width
, state
);
941 else if (strcmp(name
, "height") == 0)
942 *height
= svgtiny_parse_length(content
,
943 state
.viewport_height
, state
);
949 * Parse a length as a number of pixels.
952 float svgtiny_parse_length(const char *s
, int viewport_size
,
953 const struct svgtiny_parse_state state
)
955 int num_length
= strspn(s
, "0123456789+-.");
956 const char *unit
= s
+ num_length
;
957 float n
= atof((const char *) s
);
958 float font_size
= 20; /*css_len2px(&state.style.font_size.value.length, 0);*/
964 } else if (unit
[0] == '%') {
965 return n
/ 100.0 * viewport_size
;
966 } else if (unit
[0] == 'e' && unit
[1] == 'm') {
967 return n
* font_size
;
968 } else if (unit
[0] == 'e' && unit
[1] == 'x') {
969 return n
/ 2.0 * font_size
;
970 } else if (unit
[0] == 'p' && unit
[1] == 'x') {
972 } else if (unit
[0] == 'p' && unit
[1] == 't') {
974 } else if (unit
[0] == 'p' && unit
[1] == 'c') {
976 } else if (unit
[0] == 'm' && unit
[1] == 'm') {
978 } else if (unit
[0] == 'c' && unit
[1] == 'm') {
980 } else if (unit
[0] == 'i' && unit
[1] == 'n') {
989 * Parse paint attributes, if present.
992 void svgtiny_parse_paint_attributes(const dom_element
*node
,
993 struct svgtiny_parse_state
*state
)
997 for (attr
= node
->properties
; attr
; attr
= attr
->next
) {
998 const char *name
= (const char *) attr
->name
;
999 const char *content
= (const char *) attr
->children
->content
;
1000 if (strcmp(name
, "fill") == 0)
1001 svgtiny_parse_color(content
, &state
->fill
, state
);
1002 else if (strcmp(name
, "stroke") == 0)
1003 svgtiny_parse_color(content
, &state
->stroke
, state
);
1004 else if (strcmp(name
, "stroke-width") == 0)
1005 state
->stroke_width
= svgtiny_parse_length(content
,
1006 state
->viewport_width
, *state
);
1007 else if (strcmp(name
, "style") == 0) {
1008 const char *style
= (const char *)
1009 attr
->children
->content
;
1012 if ((s
= strstr(style
, "fill:"))) {
1016 value
= strndup(s
, strcspn(s
, "; "));
1017 svgtiny_parse_color(value
, &state
->fill
, state
);
1020 if ((s
= strstr(style
, "stroke:"))) {
1024 value
= strndup(s
, strcspn(s
, "; "));
1025 svgtiny_parse_color(value
, &state
->stroke
, state
);
1028 if ((s
= strstr(style
, "stroke-width:"))) {
1032 value
= strndup(s
, strcspn(s
, "; "));
1033 state
->stroke_width
= svgtiny_parse_length(value
,
1034 state
->viewport_width
, *state
);
1046 void svgtiny_parse_color(const char *s
, svgtiny_colour
*c
,
1047 struct svgtiny_parse_state
*state
)
1049 unsigned int r
, g
, b
;
1051 size_t len
= strlen(s
);
1052 char *id
= 0, *rparen
;
1054 if (len
== 4 && s
[0] == '#') {
1055 if (sscanf(s
+ 1, "%1x%1x%1x", &r
, &g
, &b
) == 3)
1056 *c
= svgtiny_RGB(r
| r
<< 4, g
| g
<< 4, b
| b
<< 4);
1058 } else if (len
== 7 && s
[0] == '#') {
1059 if (sscanf(s
+ 1, "%2x%2x%2x", &r
, &g
, &b
) == 3)
1060 *c
= svgtiny_RGB(r
, g
, b
);
1062 } else if (10 <= len
&& s
[0] == 'r' && s
[1] == 'g' && s
[2] == 'b' &&
1063 s
[3] == '(' && s
[len
- 1] == ')') {
1064 if (sscanf(s
+ 4, "%u,%u,%u", &r
, &g
, &b
) == 3)
1065 *c
= svgtiny_RGB(r
, g
, b
);
1066 else if (sscanf(s
+ 4, "%f%%,%f%%,%f%%", &rf
, &gf
, &bf
) == 3) {
1070 *c
= svgtiny_RGB(r
, g
, b
);
1073 } else if (len
== 4 && strcmp(s
, "none") == 0) {
1074 *c
= svgtiny_TRANSPARENT
;
1076 } else if (5 < len
&& s
[0] == 'u' && s
[1] == 'r' && s
[2] == 'l' &&
1082 rparen
= strchr(id
, ')');
1085 svgtiny_find_gradient(id
, state
);
1087 fprintf(stderr
, "linear_gradient_stop_count %i\n",
1088 state
->linear_gradient_stop_count
);
1089 if (state
->linear_gradient_stop_count
== 0)
1090 *c
= svgtiny_TRANSPARENT
;
1091 else if (state
->linear_gradient_stop_count
== 1)
1092 *c
= state
->gradient_stop
[0].color
;
1094 *c
= svgtiny_LINEAR_GRADIENT
;
1098 const struct svgtiny_named_color
*named_color
;
1099 named_color
= svgtiny_color_lookup(s
, strlen(s
));
1101 *c
= named_color
->color
;
1107 * Parse font attributes, if present.
1110 void svgtiny_parse_font_attributes(const dom_element
*node
,
1111 struct svgtiny_parse_state
*state
)
1113 const xmlAttr
*attr
;
1117 for (attr
= node
->properties
; attr
; attr
= attr
->next
) {
1118 if (strcmp((const char *) attr
->name
, "font-size") == 0) {
1119 /*if (css_parse_length(
1120 (const char *) attr->children->content,
1121 &state->style.font_size.value.length,
1123 state->style.font_size.size =
1124 CSS_FONT_SIZE_LENGTH;
1132 * Parse transform attributes, if present.
1134 * http://www.w3.org/TR/SVG11/coords#TransformAttribute
1137 void svgtiny_parse_transform_attributes(dom_element
*node
,
1138 struct svgtiny_parse_state
*state
)
1142 /* parse transform */
1143 transform
= (char *) xmlGetProp(node
, (const xmlChar
*) "transform");
1145 svgtiny_parse_transform(transform
, &state
->ctm
.a
, &state
->ctm
.b
,
1146 &state
->ctm
.c
, &state
->ctm
.d
,
1147 &state
->ctm
.e
, &state
->ctm
.f
);
1154 * Parse a transform string.
1157 void svgtiny_parse_transform(char *s
, float *ma
, float *mb
,
1158 float *mc
, float *md
, float *me
, float *mf
)
1160 float a
, b
, c
, d
, e
, f
;
1161 float za
, zb
, zc
, zd
, ze
, zf
;
1166 for (i
= 0; s
[i
]; i
++)
1174 if (sscanf(s
, "matrix (%f %f %f %f %f %f) %n",
1175 &a
, &b
, &c
, &d
, &e
, &f
, &n
) == 6)
1177 else if (sscanf(s
, "translate (%f %f) %n",
1180 else if (sscanf(s
, "translate (%f) %n",
1183 else if (sscanf(s
, "scale (%f %f) %n",
1186 else if (sscanf(s
, "scale (%f) %n",
1189 else if (sscanf(s
, "rotate (%f %f %f) %n",
1190 &angle
, &x
, &y
, &n
) == 3) {
1191 angle
= angle
/ 180 * M_PI
;
1196 e
= -x
* cos(angle
) + y
* sin(angle
) + x
;
1197 f
= -x
* sin(angle
) - y
* cos(angle
) + y
;
1198 } else if (sscanf(s
, "rotate (%f) %n",
1200 angle
= angle
/ 180 * M_PI
;
1205 } else if (sscanf(s
, "skewX (%f) %n",
1207 angle
= angle
/ 180 * M_PI
;
1209 } else if (sscanf(s
, "skewY (%f) %n",
1211 angle
= angle
/ 180 * M_PI
;
1215 za
= *ma
* a
+ *mc
* b
;
1216 zb
= *mb
* a
+ *md
* b
;
1217 zc
= *ma
* c
+ *mc
* d
;
1218 zd
= *mb
* c
+ *md
* d
;
1219 ze
= *ma
* e
+ *mc
* f
+ *me
;
1220 zf
= *mb
* e
+ *md
* f
+ *mf
;
1233 * Add a path to the svgtiny_diagram.
1236 svgtiny_code
svgtiny_add_path(float *p
, unsigned int n
,
1237 struct svgtiny_parse_state
*state
)
1239 struct svgtiny_shape
*shape
;
1241 if (state
->fill
== svgtiny_LINEAR_GRADIENT
)
1242 return svgtiny_add_path_linear_gradient(p
, n
, state
);
1244 svgtiny_transform_path(p
, n
, state
);
1246 shape
= svgtiny_add_shape(state
);
1249 return svgtiny_OUT_OF_MEMORY
;
1252 shape
->path_length
= n
;
1253 state
->diagram
->shape_count
++;
1260 * Add a svgtiny_shape to the svgtiny_diagram.
1263 struct svgtiny_shape
*svgtiny_add_shape(struct svgtiny_parse_state
*state
)
1265 struct svgtiny_shape
*shape
= realloc(state
->diagram
->shape
,
1266 (state
->diagram
->shape_count
+ 1) *
1267 sizeof (state
->diagram
->shape
[0]));
1270 state
->diagram
->shape
= shape
;
1272 shape
+= state
->diagram
->shape_count
;
1274 shape
->path_length
= 0;
1276 shape
->fill
= state
->fill
;
1277 shape
->stroke
= state
->stroke
;
1278 shape
->stroke_width
= lroundf((float) state
->stroke_width
*
1279 (state
->ctm
.a
+ state
->ctm
.d
) / 2.0);
1280 if (0 < state
->stroke_width
&& shape
->stroke_width
== 0)
1281 shape
->stroke_width
= 1;
1288 * Apply the current transformation matrix to a path.
1291 void svgtiny_transform_path(float *p
, unsigned int n
,
1292 struct svgtiny_parse_state
*state
)
1296 for (j
= 0; j
!= n
; ) {
1297 unsigned int points
= 0;
1299 switch ((int) p
[j
]) {
1300 case svgtiny_PATH_MOVE
:
1301 case svgtiny_PATH_LINE
:
1304 case svgtiny_PATH_CLOSE
:
1307 case svgtiny_PATH_BEZIER
:
1314 for (k
= 0; k
!= points
; k
++) {
1315 float x0
= p
[j
], y0
= p
[j
+ 1];
1316 float x
= state
->ctm
.a
* x0
+ state
->ctm
.c
* y0
+
1318 float y
= state
->ctm
.b
* x0
+ state
->ctm
.d
* y0
+
1329 * Free all memory used by a diagram.
1332 void svgtiny_free(struct svgtiny_diagram
*svg
)
1337 for (i
= 0; i
!= svg
->shape_count
; i
++) {
1338 free(svg
->shape
[i
].path
);
1339 free(svg
->shape
[i
].text
);
1347 #ifndef HAVE_STRNDUP
1348 char *svgtiny_strndup(const char *s
, size_t n
)
1353 for (len
= 0; len
!= n
&& s
[len
]; len
++)
1356 s2
= malloc(len
+ 1);