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
);
56 static void _svgtiny_parse_color(const char *s
, svgtiny_colour
*c
,
57 struct svgtiny_parse_state
*state
);
61 * Create a new svgtiny_diagram structure.
64 struct svgtiny_diagram
*svgtiny_create(void)
66 struct svgtiny_diagram
*diagram
;
68 diagram
= calloc(sizeof(*diagram
), 1);
77 static void ignore_msg(uint32_t severity
, void *ctx
, const char *msg
, ...)
85 * Parse a block of memory into a svgtiny_diagram.
88 svgtiny_code
svgtiny_parse(struct svgtiny_diagram
*diagram
,
89 const char *buffer
, size_t size
, const char *url
,
90 int viewport_width
, int viewport_height
)
92 dom_document
*document
;
94 dom_xml_parser
*parser
;
98 lwc_string
*svg_name_lwc
;
99 struct svgtiny_parse_state state
;
100 float x
, y
, width
, height
;
109 parser
= dom_xml_parser_create(NULL
, NULL
,
110 ignore_msg
, NULL
, &document
);
113 return svgtiny_LIBDOM_ERROR
;
115 err
= dom_xml_parser_parse_chunk(parser
, (uint8_t *)buffer
, size
);
116 if (err
!= DOM_XML_OK
) {
117 dom_node_unref(document
);
118 dom_xml_parser_destroy(parser
);
119 return svgtiny_LIBDOM_ERROR
;
122 err
= dom_xml_parser_completed(parser
);
123 if (err
!= DOM_XML_OK
) {
124 dom_node_unref(document
);
125 dom_xml_parser_destroy(parser
);
126 return svgtiny_LIBDOM_ERROR
;
129 /* We're done parsing, drop the parser.
130 * We now own the document entirely.
132 dom_xml_parser_destroy(parser
);
134 /* find root <svg> element */
135 exc
= dom_document_get_document_element(document
, &svg
);
136 if (exc
!= DOM_NO_ERR
) {
137 dom_node_unref(document
);
138 return svgtiny_LIBDOM_ERROR
;
140 exc
= dom_node_get_node_name(svg
, &svg_name
);
141 if (exc
!= DOM_NO_ERR
) {
143 dom_node_unref(document
);
144 return svgtiny_LIBDOM_ERROR
;
146 if (lwc_intern_string("svg", 3 /* SLEN("svg") */,
147 &svg_name_lwc
) != lwc_error_ok
) {
148 dom_string_unref(svg_name
);
150 dom_node_unref(document
);
151 return svgtiny_LIBDOM_ERROR
;
153 if (!dom_string_caseless_lwc_isequal(svg_name
, svg_name_lwc
)) {
154 lwc_string_unref(svg_name_lwc
);
155 dom_string_unref(svg_name
);
157 dom_node_unref(document
);
158 return svgtiny_NOT_SVG
;
161 lwc_string_unref(svg_name_lwc
);
162 dom_string_unref(svg_name
);
164 /* get graphic dimensions */
165 memset(&state
, 0, sizeof(state
));
166 state
.diagram
= diagram
;
167 state
.document
= document
;
168 state
.viewport_width
= viewport_width
;
169 state
.viewport_height
= viewport_height
;
171 #define SVGTINY_STRING_ACTION2(s,n) \
172 if (dom_string_create_interned((const uint8_t *) #n, \
173 strlen(#n), &state.interned_##s) \
175 code = svgtiny_LIBDOM_ERROR; \
178 #include "svgtiny_strings.h"
179 #undef SVGTINY_STRING_ACTION2
181 svgtiny_parse_position_attributes(svg
, state
, &x
, &y
, &width
, &height
);
182 diagram
->width
= width
;
183 diagram
->height
= height
;
185 /* set up parsing state */
186 state
.viewport_width
= width
;
187 state
.viewport_height
= height
;
188 state
.ctm
.a
= 1; /*(float) viewport_width / (float) width;*/
191 state
.ctm
.d
= 1; /*(float) viewport_height / (float) height;*/
192 state
.ctm
.e
= 0; /*x;*/
193 state
.ctm
.f
= 0; /*y;*/
194 /*state.style = css_base_style;
195 state.style.font_size.value.length.value = option_font_size * 0.1;*/
196 state
.fill
= 0x000000;
197 state
.stroke
= svgtiny_TRANSPARENT
;
198 state
.stroke_width
= 1;
199 state
.linear_gradient_stop_count
= 0;
202 code
= svgtiny_parse_svg(svg
, state
);
205 dom_node_unref(document
);
208 if (state
.gradient_x1
!= NULL
)
209 dom_string_unref(state
.gradient_x1
);
210 if (state
.gradient_x2
!= NULL
)
211 dom_string_unref(state
.gradient_x2
);
212 if (state
.gradient_y1
!= NULL
)
213 dom_string_unref(state
.gradient_y1
);
214 if (state
.gradient_y2
!= NULL
)
215 dom_string_unref(state
.gradient_y2
);
216 #define SVGTINY_STRING_ACTION2(s,n) \
217 if (state.interned_##s != NULL) \
218 dom_string_unref(state.interned_##s);
219 #include "svgtiny_strings.h"
220 #undef SVGTINY_STRING_ACTION2
226 * Parse a <svg> or <g> element node.
229 svgtiny_code
svgtiny_parse_svg(dom_element
*svg
,
230 struct svgtiny_parse_state state
)
232 float x
, y
, width
, height
;
233 dom_string
*view_box
;
237 svgtiny_parse_position_attributes(svg
, state
, &x
, &y
, &width
, &height
);
238 svgtiny_parse_paint_attributes(svg
, &state
);
239 svgtiny_parse_font_attributes(svg
, &state
);
241 exc
= dom_element_get_attribute(svg
, state
.interned_viewBox
,
243 if (exc
!= DOM_NO_ERR
) {
244 return svgtiny_LIBDOM_ERROR
;
248 char *s
= strndup(dom_string_data(view_box
),
249 dom_string_length(view_box
));
250 float min_x
, min_y
, vwidth
, vheight
;
251 if (sscanf(s
, "%f,%f,%f,%f",
252 &min_x
, &min_y
, &vwidth
, &vheight
) == 4 ||
253 sscanf(s
, "%f %f %f %f",
254 &min_x
, &min_y
, &vwidth
, &vheight
) == 4) {
255 state
.ctm
.a
= (float) state
.viewport_width
/ vwidth
;
256 state
.ctm
.d
= (float) state
.viewport_height
/ vheight
;
257 state
.ctm
.e
+= -min_x
* state
.ctm
.a
;
258 state
.ctm
.f
+= -min_y
* state
.ctm
.d
;
261 dom_string_unref(view_box
);
264 svgtiny_parse_transform_attributes(svg
, &state
);
266 exc
= dom_node_get_first_child(svg
, &child
);
267 if (exc
!= DOM_NO_ERR
) {
268 return svgtiny_LIBDOM_ERROR
;
270 while (child
!= NULL
) {
272 dom_node_type nodetype
;
273 svgtiny_code code
= svgtiny_OK
;
275 exc
= dom_node_get_node_type(child
, &nodetype
);
276 if (exc
!= DOM_NO_ERR
) {
277 dom_node_unref(child
);
278 return svgtiny_LIBDOM_ERROR
;
280 if (nodetype
== DOM_ELEMENT_NODE
) {
281 dom_string
*nodename
;
282 exc
= dom_node_get_node_name(child
, &nodename
);
283 if (exc
!= DOM_NO_ERR
) {
284 dom_node_unref(child
);
285 return svgtiny_LIBDOM_ERROR
;
287 if (dom_string_caseless_isequal(state
.interned_svg
,
289 code
= svgtiny_parse_svg(child
, state
);
290 else if (dom_string_caseless_isequal(state
.interned_g
,
292 code
= svgtiny_parse_svg(child
, state
);
293 else if (dom_string_caseless_isequal(state
.interned_a
,
295 code
= svgtiny_parse_svg(child
, state
);
296 else if (dom_string_caseless_isequal(state
.interned_path
,
298 code
= svgtiny_parse_path(child
, state
);
299 else if (dom_string_caseless_isequal(state
.interned_rect
,
301 code
= svgtiny_parse_rect(child
, state
);
302 else if (dom_string_caseless_isequal(state
.interned_circle
,
304 code
= svgtiny_parse_circle(child
, state
);
305 else if (dom_string_caseless_isequal(state
.interned_ellipse
,
307 code
= svgtiny_parse_ellipse(child
, state
);
308 else if (dom_string_caseless_isequal(state
.interned_line
,
310 code
= svgtiny_parse_line(child
, state
);
311 else if (dom_string_caseless_isequal(state
.interned_polyline
,
313 code
= svgtiny_parse_poly(child
, state
, false);
314 else if (dom_string_caseless_isequal(state
.interned_polygon
,
316 code
= svgtiny_parse_poly(child
, state
, true);
317 else if (dom_string_caseless_isequal(state
.interned_text
,
319 code
= svgtiny_parse_text(child
, state
);
320 dom_string_unref(nodename
);
322 if (code
!= svgtiny_OK
) {
323 dom_node_unref(child
);
326 exc
= dom_node_get_next_sibling(child
, &next
);
327 dom_node_unref(child
);
328 if (exc
!= DOM_NO_ERR
) {
329 return svgtiny_LIBDOM_ERROR
;
340 * Parse a <path> element node.
342 * http://www.w3.org/TR/SVG11/paths#PathElement
345 svgtiny_code
svgtiny_parse_path(dom_element
*path
,
346 struct svgtiny_parse_state state
)
348 dom_string
*path_d_str
;
353 float last_x
= 0, last_y
= 0;
354 float last_cubic_x
= 0, last_cubic_y
= 0;
355 float last_quad_x
= 0, last_quad_y
= 0;
357 svgtiny_parse_paint_attributes(path
, &state
);
358 svgtiny_parse_transform_attributes(path
, &state
);
360 /* read d attribute */
361 exc
= dom_element_get_attribute(path
, state
.interned_d
, &path_d_str
);
362 if (exc
!= DOM_NO_ERR
) {
363 state
.diagram
->error_line
= -1; /* path->line; */
364 state
.diagram
->error_message
= "path: error retrieving d attribute";
365 return svgtiny_SVG_ERROR
;
368 if (path_d_str
== NULL
) {
369 state
.diagram
->error_line
= -1; /* path->line; */
370 state
.diagram
->error_message
= "path: missing d attribute";
371 return svgtiny_SVG_ERROR
;
374 s
= path_d
= strndup(dom_string_data(path_d_str
),
375 dom_string_length(path_d_str
));
376 dom_string_unref(path_d_str
);
378 return svgtiny_OUT_OF_MEMORY
;
380 /* allocate space for path: it will never have more elements than d */
381 p
= malloc(sizeof p
[0] * strlen(s
));
384 return svgtiny_OUT_OF_MEMORY
;
387 /* parse d and build path */
388 for (i
= 0; s
[i
]; i
++)
395 float x
, y
, x1
, y1
, x2
, y2
, rx
, ry
, rotation
, large_arc
, sweep
;
398 /* moveto (M, m), lineto (L, l) (2 arguments) */
399 if (sscanf(s
, " %1[MmLl] %f %f %n", command
, &x
, &y
, &n
) == 3) {
400 /*LOG(("moveto or lineto"));*/
401 if (*command
== 'M' || *command
== 'm')
402 plot_command
= svgtiny_PATH_MOVE
;
404 plot_command
= svgtiny_PATH_LINE
;
406 p
[i
++] = plot_command
;
407 if ('a' <= *command
) {
411 p
[i
++] = last_cubic_x
= last_quad_x
= last_x
413 p
[i
++] = last_cubic_y
= last_quad_y
= last_y
416 plot_command
= svgtiny_PATH_LINE
;
417 } while (sscanf(s
, "%f %f %n", &x
, &y
, &n
) == 2);
419 /* closepath (Z, z) (no arguments) */
420 } else if (sscanf(s
, " %1[Zz] %n", command
, &n
) == 1) {
421 /*LOG(("closepath"));*/
422 p
[i
++] = svgtiny_PATH_CLOSE
;
425 /* horizontal lineto (H, h) (1 argument) */
426 } else if (sscanf(s
, " %1[Hh] %f %n", command
, &x
, &n
) == 2) {
427 /*LOG(("horizontal lineto"));*/
429 p
[i
++] = svgtiny_PATH_LINE
;
432 p
[i
++] = last_cubic_x
= last_quad_x
= last_x
434 p
[i
++] = last_cubic_y
= last_quad_y
= last_y
;
436 } while (sscanf(s
, "%f %n", &x
, &n
) == 1);
438 /* vertical lineto (V, v) (1 argument) */
439 } else if (sscanf(s
, " %1[Vv] %f %n", command
, &y
, &n
) == 2) {
440 /*LOG(("vertical lineto"));*/
442 p
[i
++] = svgtiny_PATH_LINE
;
445 p
[i
++] = last_cubic_x
= last_quad_x
= last_x
;
446 p
[i
++] = last_cubic_y
= last_quad_y
= last_y
449 } while (sscanf(s
, "%f %n", &x
, &n
) == 1);
451 /* curveto (C, c) (6 arguments) */
452 } else if (sscanf(s
, " %1[Cc] %f %f %f %f %f %f %n", command
,
453 &x1
, &y1
, &x2
, &y2
, &x
, &y
, &n
) == 7) {
454 /*LOG(("curveto"));*/
456 p
[i
++] = svgtiny_PATH_BEZIER
;
457 if (*command
== 'c') {
467 p
[i
++] = last_cubic_x
= x2
;
468 p
[i
++] = last_cubic_y
= y2
;
469 p
[i
++] = last_quad_x
= last_x
= x
;
470 p
[i
++] = last_quad_y
= last_y
= y
;
472 } while (sscanf(s
, "%f %f %f %f %f %f %n",
473 &x1
, &y1
, &x2
, &y2
, &x
, &y
, &n
) == 6);
475 /* shorthand/smooth curveto (S, s) (4 arguments) */
476 } else if (sscanf(s
, " %1[Ss] %f %f %f %f %n", command
,
477 &x2
, &y2
, &x
, &y
, &n
) == 5) {
478 /*LOG(("shorthand/smooth curveto"));*/
480 p
[i
++] = svgtiny_PATH_BEZIER
;
481 x1
= last_x
+ (last_x
- last_cubic_x
);
482 y1
= last_y
+ (last_y
- last_cubic_y
);
483 if (*command
== 's') {
491 p
[i
++] = last_cubic_x
= x2
;
492 p
[i
++] = last_cubic_y
= y2
;
493 p
[i
++] = last_quad_x
= last_x
= x
;
494 p
[i
++] = last_quad_y
= last_y
= y
;
496 } while (sscanf(s
, "%f %f %f %f %n",
497 &x2
, &y2
, &x
, &y
, &n
) == 4);
499 /* quadratic Bezier curveto (Q, q) (4 arguments) */
500 } else if (sscanf(s
, " %1[Qq] %f %f %f %f %n", command
,
501 &x1
, &y1
, &x
, &y
, &n
) == 5) {
502 /*LOG(("quadratic Bezier curveto"));*/
504 p
[i
++] = svgtiny_PATH_BEZIER
;
507 if (*command
== 'q') {
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 %f %f %n",
521 &x1
, &y1
, &x
, &y
, &n
) == 4);
523 /* shorthand/smooth quadratic Bezier curveto (T, t)
525 } else if (sscanf(s
, " %1[Tt] %f %f %n", command
,
527 /*LOG(("shorthand/smooth quadratic Bezier curveto"));*/
529 p
[i
++] = svgtiny_PATH_BEZIER
;
530 x1
= last_x
+ (last_x
- last_quad_x
);
531 y1
= last_y
+ (last_y
- last_quad_y
);
534 if (*command
== 't') {
540 p
[i
++] = 1./3 * last_x
+ 2./3 * x1
;
541 p
[i
++] = 1./3 * last_y
+ 2./3 * y1
;
542 p
[i
++] = 2./3 * x1
+ 1./3 * x
;
543 p
[i
++] = 2./3 * y1
+ 1./3 * y
;
544 p
[i
++] = last_cubic_x
= last_x
= x
;
545 p
[i
++] = last_cubic_y
= last_y
= y
;
547 } while (sscanf(s
, "%f %f %n",
550 /* elliptical arc (A, a) (7 arguments) */
551 } else if (sscanf(s
, " %1[Aa] %f %f %f %f %f %f %f %n", command
,
552 &rx
, &ry
, &rotation
, &large_arc
, &sweep
,
555 p
[i
++] = svgtiny_PATH_LINE
;
556 if (*command
== 'a') {
560 p
[i
++] = last_cubic_x
= last_quad_x
= last_x
562 p
[i
++] = last_cubic_y
= last_quad_y
= last_y
565 } while (sscanf(s
, "%f %f %f %f %f %f %f %n",
566 &rx
, &ry
, &rotation
, &large_arc
, &sweep
,
570 fprintf(stderr
, "parse failed at \"%s\"\n", s
);
578 /* no real segments in path */
583 return svgtiny_add_path(p
, i
, &state
);
588 * Parse a <rect> element node.
590 * http://www.w3.org/TR/SVG11/shapes#RectElement
593 svgtiny_code
svgtiny_parse_rect(dom_element
*rect
,
594 struct svgtiny_parse_state state
)
596 float x
, y
, width
, height
;
599 svgtiny_parse_position_attributes(rect
, state
,
600 &x
, &y
, &width
, &height
);
601 svgtiny_parse_paint_attributes(rect
, &state
);
602 svgtiny_parse_transform_attributes(rect
, &state
);
604 p
= malloc(13 * sizeof p
[0]);
606 return svgtiny_OUT_OF_MEMORY
;
608 p
[0] = svgtiny_PATH_MOVE
;
611 p
[3] = svgtiny_PATH_LINE
;
614 p
[6] = svgtiny_PATH_LINE
;
617 p
[9] = svgtiny_PATH_LINE
;
620 p
[12] = svgtiny_PATH_CLOSE
;
622 return svgtiny_add_path(p
, 13, &state
);
627 * Parse a <circle> element node.
630 svgtiny_code
svgtiny_parse_circle(dom_element
*circle
,
631 struct svgtiny_parse_state state
)
633 float x
= 0, y
= 0, r
= -1;
638 exc
= dom_element_get_attribute(circle
, state
.interned_cx
, &attr
);
639 if (exc
!= DOM_NO_ERR
)
640 return svgtiny_LIBDOM_ERROR
;
642 x
= svgtiny_parse_length(attr
, state
.viewport_width
, state
);
644 dom_string_unref(attr
);
646 exc
= dom_element_get_attribute(circle
, state
.interned_cy
, &attr
);
647 if (exc
!= DOM_NO_ERR
)
648 return svgtiny_LIBDOM_ERROR
;
650 y
= svgtiny_parse_length(attr
, state
.viewport_height
, state
);
652 dom_string_unref(attr
);
654 exc
= dom_element_get_attribute(circle
, state
.interned_r
, &attr
);
655 if (exc
!= DOM_NO_ERR
)
656 return svgtiny_LIBDOM_ERROR
;
658 r
= svgtiny_parse_length(attr
, state
.viewport_width
, state
);
660 dom_string_unref(attr
);
662 svgtiny_parse_paint_attributes(circle
, &state
);
663 svgtiny_parse_transform_attributes(circle
, &state
);
666 state
.diagram
->error_line
= -1; /* circle->line; */
667 state
.diagram
->error_message
= "circle: r missing or negative";
668 return svgtiny_SVG_ERROR
;
673 p
= malloc(32 * sizeof p
[0]);
675 return svgtiny_OUT_OF_MEMORY
;
677 p
[0] = svgtiny_PATH_MOVE
;
680 p
[3] = svgtiny_PATH_BEZIER
;
682 p
[5] = y
+ r
* KAPPA
;
683 p
[6] = x
+ r
* KAPPA
;
687 p
[10] = svgtiny_PATH_BEZIER
;
688 p
[11] = x
- r
* KAPPA
;
691 p
[14] = y
+ r
* KAPPA
;
694 p
[17] = svgtiny_PATH_BEZIER
;
696 p
[19] = y
- r
* KAPPA
;
697 p
[20] = x
- r
* KAPPA
;
701 p
[24] = svgtiny_PATH_BEZIER
;
702 p
[25] = x
+ r
* KAPPA
;
705 p
[28] = y
- r
* KAPPA
;
708 p
[31] = svgtiny_PATH_CLOSE
;
710 return svgtiny_add_path(p
, 32, &state
);
715 * Parse an <ellipse> element node.
718 svgtiny_code
svgtiny_parse_ellipse(dom_element
*ellipse
,
719 struct svgtiny_parse_state state
)
721 float x
= 0, y
= 0, rx
= -1, ry
= -1;
726 exc
= dom_element_get_attribute(ellipse
, state
.interned_cx
, &attr
);
727 if (exc
!= DOM_NO_ERR
)
728 return svgtiny_LIBDOM_ERROR
;
730 x
= svgtiny_parse_length(attr
, state
.viewport_width
, state
);
732 dom_string_unref(attr
);
734 exc
= dom_element_get_attribute(ellipse
, state
.interned_cy
, &attr
);
735 if (exc
!= DOM_NO_ERR
)
736 return svgtiny_LIBDOM_ERROR
;
738 y
= svgtiny_parse_length(attr
, state
.viewport_height
, state
);
740 dom_string_unref(attr
);
742 exc
= dom_element_get_attribute(ellipse
, state
.interned_rx
, &attr
);
743 if (exc
!= DOM_NO_ERR
)
744 return svgtiny_LIBDOM_ERROR
;
746 rx
= svgtiny_parse_length(attr
, state
.viewport_width
, state
);
748 dom_string_unref(attr
);
750 exc
= dom_element_get_attribute(ellipse
, state
.interned_ry
, &attr
);
751 if (exc
!= DOM_NO_ERR
)
752 return svgtiny_LIBDOM_ERROR
;
754 ry
= svgtiny_parse_length(attr
, state
.viewport_width
, state
);
756 dom_string_unref(attr
);
758 svgtiny_parse_paint_attributes(ellipse
, &state
);
759 svgtiny_parse_transform_attributes(ellipse
, &state
);
761 if (rx
< 0 || ry
< 0) {
762 state
.diagram
->error_line
= -1; /* ellipse->line; */
763 state
.diagram
->error_message
= "ellipse: rx or ry missing "
765 return svgtiny_SVG_ERROR
;
767 if (rx
== 0 || ry
== 0)
770 p
= malloc(32 * sizeof p
[0]);
772 return svgtiny_OUT_OF_MEMORY
;
774 p
[0] = svgtiny_PATH_MOVE
;
777 p
[3] = svgtiny_PATH_BEZIER
;
779 p
[5] = y
+ ry
* KAPPA
;
780 p
[6] = x
+ rx
* KAPPA
;
784 p
[10] = svgtiny_PATH_BEZIER
;
785 p
[11] = x
- rx
* KAPPA
;
788 p
[14] = y
+ ry
* KAPPA
;
791 p
[17] = svgtiny_PATH_BEZIER
;
793 p
[19] = y
- ry
* KAPPA
;
794 p
[20] = x
- rx
* KAPPA
;
798 p
[24] = svgtiny_PATH_BEZIER
;
799 p
[25] = x
+ rx
* KAPPA
;
802 p
[28] = y
- ry
* KAPPA
;
805 p
[31] = svgtiny_PATH_CLOSE
;
807 return svgtiny_add_path(p
, 32, &state
);
812 * Parse a <line> element node.
815 svgtiny_code
svgtiny_parse_line(dom_element
*line
,
816 struct svgtiny_parse_state state
)
818 float x1
= 0, y1
= 0, x2
= 0, y2
= 0;
823 exc
= dom_element_get_attribute(line
, state
.interned_x1
, &attr
);
824 if (exc
!= DOM_NO_ERR
)
825 return svgtiny_LIBDOM_ERROR
;
827 x1
= svgtiny_parse_length(attr
, state
.viewport_width
, state
);
829 dom_string_unref(attr
);
831 exc
= dom_element_get_attribute(line
, state
.interned_y1
, &attr
);
832 if (exc
!= DOM_NO_ERR
)
833 return svgtiny_LIBDOM_ERROR
;
835 y1
= svgtiny_parse_length(attr
, state
.viewport_height
, state
);
837 dom_string_unref(attr
);
839 exc
= dom_element_get_attribute(line
, state
.interned_x2
, &attr
);
840 if (exc
!= DOM_NO_ERR
)
841 return svgtiny_LIBDOM_ERROR
;
843 x2
= svgtiny_parse_length(attr
, state
.viewport_width
, state
);
845 dom_string_unref(attr
);
847 exc
= dom_element_get_attribute(line
, state
.interned_y2
, &attr
);
848 if (exc
!= DOM_NO_ERR
)
849 return svgtiny_LIBDOM_ERROR
;
851 y2
= svgtiny_parse_length(attr
, state
.viewport_height
, state
);
853 dom_string_unref(attr
);
855 svgtiny_parse_paint_attributes(line
, &state
);
856 svgtiny_parse_transform_attributes(line
, &state
);
858 p
= malloc(7 * sizeof p
[0]);
860 return svgtiny_OUT_OF_MEMORY
;
862 p
[0] = svgtiny_PATH_MOVE
;
865 p
[3] = svgtiny_PATH_LINE
;
868 p
[6] = svgtiny_PATH_CLOSE
;
870 return svgtiny_add_path(p
, 7, &state
);
875 * Parse a <polyline> or <polygon> element node.
877 * http://www.w3.org/TR/SVG11/shapes#PolylineElement
878 * http://www.w3.org/TR/SVG11/shapes#PolygonElement
881 svgtiny_code
svgtiny_parse_poly(dom_element
*poly
,
882 struct svgtiny_parse_state state
, bool polygon
)
884 dom_string
*points_str
;
890 svgtiny_parse_paint_attributes(poly
, &state
);
891 svgtiny_parse_transform_attributes(poly
, &state
);
893 exc
= dom_element_get_attribute(poly
, state
.interned_points
,
895 if (exc
!= DOM_NO_ERR
)
896 return svgtiny_LIBDOM_ERROR
;
898 if (points_str
== NULL
) {
899 state
.diagram
->error_line
= -1; /* poly->line; */
900 state
.diagram
->error_message
=
901 "polyline/polygon: missing points attribute";
902 return svgtiny_SVG_ERROR
;
905 s
= points
= strndup(dom_string_data(points_str
),
906 dom_string_length(points_str
));
907 dom_string_unref(points_str
);
908 /* read points attribute */
910 return svgtiny_OUT_OF_MEMORY
;
911 /* allocate space for path: it will never have more elements than s */
912 p
= malloc(sizeof p
[0] * strlen(s
));
915 return svgtiny_OUT_OF_MEMORY
;
918 /* parse s and build path */
919 for (i
= 0; s
[i
]; i
++)
927 if (sscanf(s
, "%f %f %n", &x
, &y
, &n
) == 2) {
929 p
[i
++] = svgtiny_PATH_MOVE
;
931 p
[i
++] = svgtiny_PATH_LINE
;
940 p
[i
++] = svgtiny_PATH_CLOSE
;
944 return svgtiny_add_path(p
, i
, &state
);
949 * Parse a <text> or <tspan> element node.
952 svgtiny_code
svgtiny_parse_text(dom_element
*text
,
953 struct svgtiny_parse_state state
)
955 float x
, y
, width
, height
;
960 svgtiny_parse_position_attributes(text
, state
,
961 &x
, &y
, &width
, &height
);
962 svgtiny_parse_font_attributes(text
, &state
);
963 svgtiny_parse_transform_attributes(text
, &state
);
965 px
= state
.ctm
.a
* x
+ state
.ctm
.c
* y
+ state
.ctm
.e
;
966 py
= state
.ctm
.b
* x
+ state
.ctm
.d
* y
+ state
.ctm
.f
;
967 /* state.ctm.e = px - state.origin_x; */
968 /* state.ctm.f = py - state.origin_y; */
970 /*struct css_style style = state.style;
971 style.font_size.value.length.value *= state.ctm.a;*/
973 exc
= dom_node_get_first_child(text
, &child
);
974 if (exc
!= DOM_NO_ERR
)
975 return svgtiny_LIBDOM_ERROR
;
976 while (child
!= NULL
) {
978 dom_node_type nodetype
;
979 svgtiny_code code
= svgtiny_OK
;
981 exc
= dom_node_get_node_type(child
, &nodetype
);
982 if (exc
!= DOM_NO_ERR
) {
983 dom_node_unref(child
);
984 return svgtiny_LIBDOM_ERROR
;
986 if (nodetype
== DOM_ELEMENT_NODE
) {
987 dom_string
*nodename
;
988 exc
= dom_node_get_node_name(child
, &nodename
);
989 if (exc
!= DOM_NO_ERR
) {
990 dom_node_unref(child
);
991 return svgtiny_LIBDOM_ERROR
;
993 if (dom_string_caseless_isequal(nodename
,
994 state
.interned_tspan
))
995 code
= svgtiny_parse_text((dom_element
*)child
,
997 dom_string_unref(nodename
);
998 } else if (nodetype
== DOM_TEXT_NODE
) {
999 struct svgtiny_shape
*shape
= svgtiny_add_shape(&state
);
1000 dom_string
*content
;
1001 if (shape
== NULL
) {
1002 dom_node_unref(child
);
1003 return svgtiny_OUT_OF_MEMORY
;
1005 exc
= dom_text_get_whole_text(child
, &content
);
1006 if (exc
!= DOM_NO_ERR
) {
1007 dom_node_unref(child
);
1008 return svgtiny_LIBDOM_ERROR
;
1010 shape
->text
= strndup(dom_string_data(content
),
1011 dom_string_length(content
));
1012 dom_string_unref(content
);
1015 state
.diagram
->shape_count
++;
1018 if (code
!= svgtiny_OK
) {
1019 dom_node_unref(child
);
1022 exc
= dom_node_get_next_sibling(child
, &next
);
1023 dom_node_unref(child
);
1024 if (exc
!= DOM_NO_ERR
)
1025 return svgtiny_LIBDOM_ERROR
;
1034 * Parse x, y, width, and height attributes, if present.
1037 void svgtiny_parse_position_attributes(const dom_element
*node
,
1038 const struct svgtiny_parse_state state
,
1039 float *x
, float *y
, float *width
, float *height
)
1046 *width
= state
.viewport_width
;
1047 *height
= state
.viewport_height
;
1049 exc
= dom_element_get_attribute(node
, state
.interned_x
, &attr
);
1050 if (exc
== DOM_NO_ERR
&& attr
!= NULL
) {
1051 *x
= svgtiny_parse_length(attr
, state
.viewport_width
, state
);
1052 dom_string_unref(attr
);
1055 exc
= dom_element_get_attribute(node
, state
.interned_y
, &attr
);
1056 if (exc
== DOM_NO_ERR
&& attr
!= NULL
) {
1057 *y
= svgtiny_parse_length(attr
, state
.viewport_height
, state
);
1058 dom_string_unref(attr
);
1061 exc
= dom_element_get_attribute(node
, state
.interned_width
, &attr
);
1062 if (exc
== DOM_NO_ERR
&& attr
!= NULL
) {
1063 *width
= svgtiny_parse_length(attr
, state
.viewport_width
,
1065 dom_string_unref(attr
);
1068 exc
= dom_element_get_attribute(node
, state
.interned_height
, &attr
);
1069 if (exc
== DOM_NO_ERR
&& attr
!= NULL
) {
1070 *height
= svgtiny_parse_length(attr
, state
.viewport_height
,
1072 dom_string_unref(attr
);
1078 * Parse a length as a number of pixels.
1081 static float _svgtiny_parse_length(const char *s
, int viewport_size
,
1082 const struct svgtiny_parse_state state
)
1084 int num_length
= strspn(s
, "0123456789+-.");
1085 const char *unit
= s
+ num_length
;
1086 float n
= atof((const char *) s
);
1087 float font_size
= 20; /*css_len2px(&state.style.font_size.value.length, 0);*/
1093 } else if (unit
[0] == '%') {
1094 return n
/ 100.0 * viewport_size
;
1095 } else if (unit
[0] == 'e' && unit
[1] == 'm') {
1096 return n
* font_size
;
1097 } else if (unit
[0] == 'e' && unit
[1] == 'x') {
1098 return n
/ 2.0 * font_size
;
1099 } else if (unit
[0] == 'p' && unit
[1] == 'x') {
1101 } else if (unit
[0] == 'p' && unit
[1] == 't') {
1103 } else if (unit
[0] == 'p' && unit
[1] == 'c') {
1105 } else if (unit
[0] == 'm' && unit
[1] == 'm') {
1106 return n
* 3.543307;
1107 } else if (unit
[0] == 'c' && unit
[1] == 'm') {
1108 return n
* 35.43307;
1109 } else if (unit
[0] == 'i' && unit
[1] == 'n') {
1116 float svgtiny_parse_length(dom_string
*s
, int viewport_size
,
1117 const struct svgtiny_parse_state state
)
1119 char *ss
= strndup(dom_string_data(s
), dom_string_length(s
));
1120 float ret
= _svgtiny_parse_length(ss
, viewport_size
, state
);
1126 * Parse paint attributes, if present.
1129 void svgtiny_parse_paint_attributes(const dom_element
*node
,
1130 struct svgtiny_parse_state
*state
)
1135 exc
= dom_element_get_attribute(node
, state
->interned_fill
, &attr
);
1136 if (exc
== DOM_NO_ERR
&& attr
!= NULL
) {
1137 svgtiny_parse_color(attr
, &state
->fill
, state
);
1138 dom_string_unref(attr
);
1141 exc
= dom_element_get_attribute(node
, state
->interned_stroke
, &attr
);
1142 if (exc
== DOM_NO_ERR
&& attr
!= NULL
) {
1143 svgtiny_parse_color(attr
, &state
->stroke
, state
);
1144 dom_string_unref(attr
);
1147 exc
= dom_element_get_attribute(node
, state
->interned_stroke_width
, &attr
);
1148 if (exc
== DOM_NO_ERR
&& attr
!= NULL
) {
1149 state
->stroke_width
= svgtiny_parse_length(attr
,
1150 state
->viewport_width
, *state
);
1151 dom_string_unref(attr
);
1154 exc
= dom_element_get_attribute(node
, state
->interned_style
, &attr
);
1155 if (exc
== DOM_NO_ERR
&& attr
!= NULL
) {
1156 char *style
= strndup(dom_string_data(attr
),
1157 dom_string_length(attr
));
1160 if ((s
= strstr(style
, "fill:"))) {
1164 value
= strndup(s
, strcspn(s
, "; "));
1165 _svgtiny_parse_color(value
, &state
->fill
, state
);
1168 if ((s
= strstr(style
, "stroke:"))) {
1172 value
= strndup(s
, strcspn(s
, "; "));
1173 _svgtiny_parse_color(value
, &state
->stroke
, state
);
1176 if ((s
= strstr(style
, "stroke-width:"))) {
1180 value
= strndup(s
, strcspn(s
, "; "));
1181 state
->stroke_width
= _svgtiny_parse_length(value
,
1182 state
->viewport_width
, *state
);
1186 dom_string_unref(attr
);
1195 static void _svgtiny_parse_color(const char *s
, svgtiny_colour
*c
,
1196 struct svgtiny_parse_state
*state
)
1198 unsigned int r
, g
, b
;
1200 size_t len
= strlen(s
);
1201 char *id
= 0, *rparen
;
1203 if (len
== 4 && s
[0] == '#') {
1204 if (sscanf(s
+ 1, "%1x%1x%1x", &r
, &g
, &b
) == 3)
1205 *c
= svgtiny_RGB(r
| r
<< 4, g
| g
<< 4, b
| b
<< 4);
1207 } else if (len
== 7 && s
[0] == '#') {
1208 if (sscanf(s
+ 1, "%2x%2x%2x", &r
, &g
, &b
) == 3)
1209 *c
= svgtiny_RGB(r
, g
, b
);
1211 } else if (10 <= len
&& s
[0] == 'r' && s
[1] == 'g' && s
[2] == 'b' &&
1212 s
[3] == '(' && s
[len
- 1] == ')') {
1213 if (sscanf(s
+ 4, "%u,%u,%u", &r
, &g
, &b
) == 3)
1214 *c
= svgtiny_RGB(r
, g
, b
);
1215 else if (sscanf(s
+ 4, "%f%%,%f%%,%f%%", &rf
, &gf
, &bf
) == 3) {
1219 *c
= svgtiny_RGB(r
, g
, b
);
1222 } else if (len
== 4 && strcmp(s
, "none") == 0) {
1223 *c
= svgtiny_TRANSPARENT
;
1225 } else if (5 < len
&& s
[0] == 'u' && s
[1] == 'r' && s
[2] == 'l' &&
1231 rparen
= strchr(id
, ')');
1234 svgtiny_find_gradient(id
, state
);
1236 fprintf(stderr
, "linear_gradient_stop_count %i\n",
1237 state
->linear_gradient_stop_count
);
1238 if (state
->linear_gradient_stop_count
== 0)
1239 *c
= svgtiny_TRANSPARENT
;
1240 else if (state
->linear_gradient_stop_count
== 1)
1241 *c
= state
->gradient_stop
[0].color
;
1243 *c
= svgtiny_LINEAR_GRADIENT
;
1247 const struct svgtiny_named_color
*named_color
;
1248 named_color
= svgtiny_color_lookup(s
, strlen(s
));
1250 *c
= named_color
->color
;
1254 void svgtiny_parse_color(dom_string
*s
, svgtiny_colour
*c
,
1255 struct svgtiny_parse_state
*state
)
1257 char *ss
= strndup(dom_string_data(s
), dom_string_length(s
));
1258 _svgtiny_parse_color(ss
, c
, state
);
1263 * Parse font attributes, if present.
1266 void svgtiny_parse_font_attributes(const dom_element
*node
,
1267 struct svgtiny_parse_state
*state
)
1269 /* TODO: Implement this, it never used to be */
1272 #ifdef WRITTEN_THIS_PROPERLY
1273 const xmlAttr
*attr
;
1277 for (attr
= node
->properties
; attr
; attr
= attr
->next
) {
1278 if (strcmp((const char *) attr
->name
, "font-size") == 0) {
1279 /*if (css_parse_length(
1280 (const char *) attr->children->content,
1281 &state->style.font_size.value.length,
1283 state->style.font_size.size =
1284 CSS_FONT_SIZE_LENGTH;
1293 * Parse transform attributes, if present.
1295 * http://www.w3.org/TR/SVG11/coords#TransformAttribute
1298 void svgtiny_parse_transform_attributes(dom_element
*node
,
1299 struct svgtiny_parse_state
*state
)
1305 exc
= dom_element_get_attribute(node
, state
->interned_transform
,
1307 if (exc
== DOM_NO_ERR
&& attr
!= NULL
) {
1308 transform
= strndup(dom_string_data(attr
),
1309 dom_string_length(attr
));
1310 svgtiny_parse_transform(transform
, &state
->ctm
.a
, &state
->ctm
.b
,
1311 &state
->ctm
.c
, &state
->ctm
.d
,
1312 &state
->ctm
.e
, &state
->ctm
.f
);
1314 dom_string_unref(attr
);
1320 * Parse a transform string.
1323 void svgtiny_parse_transform(char *s
, float *ma
, float *mb
,
1324 float *mc
, float *md
, float *me
, float *mf
)
1326 float a
, b
, c
, d
, e
, f
;
1327 float za
, zb
, zc
, zd
, ze
, zf
;
1332 for (i
= 0; s
[i
]; i
++)
1340 if (sscanf(s
, "matrix (%f %f %f %f %f %f) %n",
1341 &a
, &b
, &c
, &d
, &e
, &f
, &n
) == 6)
1343 else if (sscanf(s
, "translate (%f %f) %n",
1346 else if (sscanf(s
, "translate (%f) %n",
1349 else if (sscanf(s
, "scale (%f %f) %n",
1352 else if (sscanf(s
, "scale (%f) %n",
1355 else if (sscanf(s
, "rotate (%f %f %f) %n",
1356 &angle
, &x
, &y
, &n
) == 3) {
1357 angle
= angle
/ 180 * M_PI
;
1362 e
= -x
* cos(angle
) + y
* sin(angle
) + x
;
1363 f
= -x
* sin(angle
) - y
* cos(angle
) + y
;
1364 } else if (sscanf(s
, "rotate (%f) %n",
1366 angle
= angle
/ 180 * M_PI
;
1371 } else if (sscanf(s
, "skewX (%f) %n",
1373 angle
= angle
/ 180 * M_PI
;
1375 } else if (sscanf(s
, "skewY (%f) %n",
1377 angle
= angle
/ 180 * M_PI
;
1381 za
= *ma
* a
+ *mc
* b
;
1382 zb
= *mb
* a
+ *md
* b
;
1383 zc
= *ma
* c
+ *mc
* d
;
1384 zd
= *mb
* c
+ *md
* d
;
1385 ze
= *ma
* e
+ *mc
* f
+ *me
;
1386 zf
= *mb
* e
+ *md
* f
+ *mf
;
1399 * Add a path to the svgtiny_diagram.
1402 svgtiny_code
svgtiny_add_path(float *p
, unsigned int n
,
1403 struct svgtiny_parse_state
*state
)
1405 struct svgtiny_shape
*shape
;
1407 if (state
->fill
== svgtiny_LINEAR_GRADIENT
)
1408 return svgtiny_add_path_linear_gradient(p
, n
, state
);
1410 svgtiny_transform_path(p
, n
, state
);
1412 shape
= svgtiny_add_shape(state
);
1415 return svgtiny_OUT_OF_MEMORY
;
1418 shape
->path_length
= n
;
1419 state
->diagram
->shape_count
++;
1426 * Add a svgtiny_shape to the svgtiny_diagram.
1429 struct svgtiny_shape
*svgtiny_add_shape(struct svgtiny_parse_state
*state
)
1431 struct svgtiny_shape
*shape
= realloc(state
->diagram
->shape
,
1432 (state
->diagram
->shape_count
+ 1) *
1433 sizeof (state
->diagram
->shape
[0]));
1436 state
->diagram
->shape
= shape
;
1438 shape
+= state
->diagram
->shape_count
;
1440 shape
->path_length
= 0;
1442 shape
->fill
= state
->fill
;
1443 shape
->stroke
= state
->stroke
;
1444 shape
->stroke_width
= lroundf((float) state
->stroke_width
*
1445 (state
->ctm
.a
+ state
->ctm
.d
) / 2.0);
1446 if (0 < state
->stroke_width
&& shape
->stroke_width
== 0)
1447 shape
->stroke_width
= 1;
1454 * Apply the current transformation matrix to a path.
1457 void svgtiny_transform_path(float *p
, unsigned int n
,
1458 struct svgtiny_parse_state
*state
)
1462 for (j
= 0; j
!= n
; ) {
1463 unsigned int points
= 0;
1465 switch ((int) p
[j
]) {
1466 case svgtiny_PATH_MOVE
:
1467 case svgtiny_PATH_LINE
:
1470 case svgtiny_PATH_CLOSE
:
1473 case svgtiny_PATH_BEZIER
:
1480 for (k
= 0; k
!= points
; k
++) {
1481 float x0
= p
[j
], y0
= p
[j
+ 1];
1482 float x
= state
->ctm
.a
* x0
+ state
->ctm
.c
* y0
+
1484 float y
= state
->ctm
.b
* x0
+ state
->ctm
.d
* y0
+
1495 * Free all memory used by a diagram.
1498 void svgtiny_free(struct svgtiny_diagram
*svg
)
1503 for (i
= 0; i
!= svg
->shape_count
; i
++) {
1504 free(svg
->shape
[i
].path
);
1505 free(svg
->shape
[i
].text
);
1513 #ifndef HAVE_STRNDUP
1514 char *svgtiny_strndup(const char *s
, size_t n
)
1519 for (len
= 0; len
!= n
&& s
[len
]; len
++)
1522 s2
= malloc(len
+ 1);