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 #define SVGTINY_STRING_ACTION2(s,n) \
209 if (state.interned_##s != NULL) \
210 dom_string_unref(state.interned_##s);
211 //#include "svgtiny_strings.h"
212 #undef SVGTINY_STRING_ACTION2
218 * Parse a <svg> or <g> element node.
221 svgtiny_code
svgtiny_parse_svg(dom_element
*svg
,
222 struct svgtiny_parse_state state
)
224 float x
, y
, width
, height
;
225 dom_string
*view_box
;
229 svgtiny_parse_position_attributes(svg
, state
, &x
, &y
, &width
, &height
);
230 svgtiny_parse_paint_attributes(svg
, &state
);
231 svgtiny_parse_font_attributes(svg
, &state
);
233 exc
= dom_element_get_attribute(svg
, state
.interned_viewBox
,
235 if (exc
!= DOM_NO_ERR
) {
236 return svgtiny_LIBDOM_ERROR
;
240 char *s
= strndup(dom_string_data(view_box
),
241 dom_string_length(view_box
));
242 float min_x
, min_y
, vwidth
, vheight
;
243 if (sscanf(s
, "%f,%f,%f,%f",
244 &min_x
, &min_y
, &vwidth
, &vheight
) == 4 ||
245 sscanf(s
, "%f %f %f %f",
246 &min_x
, &min_y
, &vwidth
, &vheight
) == 4) {
247 state
.ctm
.a
= (float) state
.viewport_width
/ vwidth
;
248 state
.ctm
.d
= (float) state
.viewport_height
/ vheight
;
249 state
.ctm
.e
+= -min_x
* state
.ctm
.a
;
250 state
.ctm
.f
+= -min_y
* state
.ctm
.d
;
253 dom_string_unref(view_box
);
256 svgtiny_parse_transform_attributes(svg
, &state
);
258 exc
= dom_node_get_first_child(svg
, &child
);
259 if (exc
!= DOM_NO_ERR
) {
260 return svgtiny_LIBDOM_ERROR
;
262 while (child
!= NULL
) {
264 dom_node_type nodetype
;
265 svgtiny_code code
= svgtiny_OK
;
267 exc
= dom_node_get_node_type(child
, &nodetype
);
268 if (exc
!= DOM_NO_ERR
) {
269 dom_node_unref(child
);
270 return svgtiny_LIBDOM_ERROR
;
272 if (nodetype
== DOM_ELEMENT_NODE
) {
273 dom_string
*nodename
;
274 exc
= dom_node_get_node_name(child
, &nodename
);
275 if (exc
!= DOM_NO_ERR
) {
276 dom_node_unref(child
);
277 return svgtiny_LIBDOM_ERROR
;
279 if (dom_string_caseless_isequal(state
.interned_svg
,
281 code
= svgtiny_parse_svg(child
, state
);
282 else if (dom_string_caseless_isequal(state
.interned_g
,
284 code
= svgtiny_parse_svg(child
, state
);
285 else if (dom_string_caseless_isequal(state
.interned_a
,
287 code
= svgtiny_parse_svg(child
, state
);
288 else if (dom_string_caseless_isequal(state
.interned_path
,
290 code
= svgtiny_parse_path(child
, state
);
291 else if (dom_string_caseless_isequal(state
.interned_rect
,
293 code
= svgtiny_parse_rect(child
, state
);
294 else if (dom_string_caseless_isequal(state
.interned_circle
,
296 code
= svgtiny_parse_circle(child
, state
);
297 else if (dom_string_caseless_isequal(state
.interned_ellipse
,
299 code
= svgtiny_parse_ellipse(child
, state
);
300 else if (dom_string_caseless_isequal(state
.interned_line
,
302 code
= svgtiny_parse_line(child
, state
);
303 else if (dom_string_caseless_isequal(state
.interned_polyline
,
305 code
= svgtiny_parse_poly(child
, state
, false);
306 else if (dom_string_caseless_isequal(state
.interned_polygon
,
308 code
= svgtiny_parse_poly(child
, state
, true);
309 else if (dom_string_caseless_isequal(state
.interned_text
,
311 code
= svgtiny_parse_text(child
, state
);
312 dom_string_unref(nodename
);
314 if (code
!= svgtiny_OK
) {
315 dom_node_unref(child
);
318 exc
= dom_node_get_next_sibling(child
, &next
);
319 dom_node_unref(child
);
320 if (exc
!= DOM_NO_ERR
) {
321 return svgtiny_LIBDOM_ERROR
;
332 * Parse a <path> element node.
334 * http://www.w3.org/TR/SVG11/paths#PathElement
337 svgtiny_code
svgtiny_parse_path(dom_element
*path
,
338 struct svgtiny_parse_state state
)
340 dom_string
*path_d_str
;
345 float last_x
= 0, last_y
= 0;
346 float last_cubic_x
= 0, last_cubic_y
= 0;
347 float last_quad_x
= 0, last_quad_y
= 0;
349 svgtiny_parse_paint_attributes(path
, &state
);
350 svgtiny_parse_transform_attributes(path
, &state
);
352 /* read d attribute */
353 exc
= dom_element_get_attribute(path
, state
.interned_d
, &path_d_str
);
354 if (exc
!= DOM_NO_ERR
) {
355 state
.diagram
->error_line
= -1; /* path->line; */
356 state
.diagram
->error_message
= "path: error retrieving d attribute";
357 return svgtiny_SVG_ERROR
;
360 if (path_d_str
== NULL
) {
361 state
.diagram
->error_line
= -1; /* path->line; */
362 state
.diagram
->error_message
= "path: missing d attribute";
363 return svgtiny_SVG_ERROR
;
366 s
= path_d
= strndup(dom_string_data(path_d_str
),
367 dom_string_length(path_d_str
));
368 dom_string_unref(path_d_str
);
370 return svgtiny_OUT_OF_MEMORY
;
372 /* allocate space for path: it will never have more elements than d */
373 p
= malloc(sizeof p
[0] * strlen(s
));
376 return svgtiny_OUT_OF_MEMORY
;
379 /* parse d and build path */
380 for (i
= 0; s
[i
]; i
++)
387 float x
, y
, x1
, y1
, x2
, y2
, rx
, ry
, rotation
, large_arc
, sweep
;
390 /* moveto (M, m), lineto (L, l) (2 arguments) */
391 if (sscanf(s
, " %1[MmLl] %f %f %n", command
, &x
, &y
, &n
) == 3) {
392 /*LOG(("moveto or lineto"));*/
393 if (*command
== 'M' || *command
== 'm')
394 plot_command
= svgtiny_PATH_MOVE
;
396 plot_command
= svgtiny_PATH_LINE
;
398 p
[i
++] = plot_command
;
399 if ('a' <= *command
) {
403 p
[i
++] = last_cubic_x
= last_quad_x
= last_x
405 p
[i
++] = last_cubic_y
= last_quad_y
= last_y
408 plot_command
= svgtiny_PATH_LINE
;
409 } while (sscanf(s
, "%f %f %n", &x
, &y
, &n
) == 2);
411 /* closepath (Z, z) (no arguments) */
412 } else if (sscanf(s
, " %1[Zz] %n", command
, &n
) == 1) {
413 /*LOG(("closepath"));*/
414 p
[i
++] = svgtiny_PATH_CLOSE
;
417 /* horizontal lineto (H, h) (1 argument) */
418 } else if (sscanf(s
, " %1[Hh] %f %n", command
, &x
, &n
) == 2) {
419 /*LOG(("horizontal lineto"));*/
421 p
[i
++] = svgtiny_PATH_LINE
;
424 p
[i
++] = last_cubic_x
= last_quad_x
= last_x
426 p
[i
++] = last_cubic_y
= last_quad_y
= last_y
;
428 } while (sscanf(s
, "%f %n", &x
, &n
) == 1);
430 /* vertical lineto (V, v) (1 argument) */
431 } else if (sscanf(s
, " %1[Vv] %f %n", command
, &y
, &n
) == 2) {
432 /*LOG(("vertical lineto"));*/
434 p
[i
++] = svgtiny_PATH_LINE
;
437 p
[i
++] = last_cubic_x
= last_quad_x
= last_x
;
438 p
[i
++] = last_cubic_y
= last_quad_y
= last_y
441 } while (sscanf(s
, "%f %n", &x
, &n
) == 1);
443 /* curveto (C, c) (6 arguments) */
444 } else if (sscanf(s
, " %1[Cc] %f %f %f %f %f %f %n", command
,
445 &x1
, &y1
, &x2
, &y2
, &x
, &y
, &n
) == 7) {
446 /*LOG(("curveto"));*/
448 p
[i
++] = svgtiny_PATH_BEZIER
;
449 if (*command
== 'c') {
459 p
[i
++] = last_cubic_x
= x2
;
460 p
[i
++] = last_cubic_y
= y2
;
461 p
[i
++] = last_quad_x
= last_x
= x
;
462 p
[i
++] = last_quad_y
= last_y
= y
;
464 } while (sscanf(s
, "%f %f %f %f %f %f %n",
465 &x1
, &y1
, &x2
, &y2
, &x
, &y
, &n
) == 6);
467 /* shorthand/smooth curveto (S, s) (4 arguments) */
468 } else if (sscanf(s
, " %1[Ss] %f %f %f %f %n", command
,
469 &x2
, &y2
, &x
, &y
, &n
) == 5) {
470 /*LOG(("shorthand/smooth curveto"));*/
472 p
[i
++] = svgtiny_PATH_BEZIER
;
473 x1
= last_x
+ (last_x
- last_cubic_x
);
474 y1
= last_y
+ (last_y
- last_cubic_y
);
475 if (*command
== 's') {
483 p
[i
++] = last_cubic_x
= x2
;
484 p
[i
++] = last_cubic_y
= y2
;
485 p
[i
++] = last_quad_x
= last_x
= x
;
486 p
[i
++] = last_quad_y
= last_y
= y
;
488 } while (sscanf(s
, "%f %f %f %f %n",
489 &x2
, &y2
, &x
, &y
, &n
) == 4);
491 /* quadratic Bezier curveto (Q, q) (4 arguments) */
492 } else if (sscanf(s
, " %1[Qq] %f %f %f %f %n", command
,
493 &x1
, &y1
, &x
, &y
, &n
) == 5) {
494 /*LOG(("quadratic Bezier curveto"));*/
496 p
[i
++] = svgtiny_PATH_BEZIER
;
499 if (*command
== 'q') {
505 p
[i
++] = 1./3 * last_x
+ 2./3 * x1
;
506 p
[i
++] = 1./3 * last_y
+ 2./3 * y1
;
507 p
[i
++] = 2./3 * x1
+ 1./3 * x
;
508 p
[i
++] = 2./3 * y1
+ 1./3 * y
;
509 p
[i
++] = last_cubic_x
= last_x
= x
;
510 p
[i
++] = last_cubic_y
= last_y
= y
;
512 } while (sscanf(s
, "%f %f %f %f %n",
513 &x1
, &y1
, &x
, &y
, &n
) == 4);
515 /* shorthand/smooth quadratic Bezier curveto (T, t)
517 } else if (sscanf(s
, " %1[Tt] %f %f %n", command
,
519 /*LOG(("shorthand/smooth quadratic Bezier curveto"));*/
521 p
[i
++] = svgtiny_PATH_BEZIER
;
522 x1
= last_x
+ (last_x
- last_quad_x
);
523 y1
= last_y
+ (last_y
- last_quad_y
);
526 if (*command
== 't') {
532 p
[i
++] = 1./3 * last_x
+ 2./3 * x1
;
533 p
[i
++] = 1./3 * last_y
+ 2./3 * y1
;
534 p
[i
++] = 2./3 * x1
+ 1./3 * x
;
535 p
[i
++] = 2./3 * y1
+ 1./3 * y
;
536 p
[i
++] = last_cubic_x
= last_x
= x
;
537 p
[i
++] = last_cubic_y
= last_y
= y
;
539 } while (sscanf(s
, "%f %f %n",
542 /* elliptical arc (A, a) (7 arguments) */
543 } else if (sscanf(s
, " %1[Aa] %f %f %f %f %f %f %f %n", command
,
544 &rx
, &ry
, &rotation
, &large_arc
, &sweep
,
547 p
[i
++] = svgtiny_PATH_LINE
;
548 if (*command
== 'a') {
552 p
[i
++] = last_cubic_x
= last_quad_x
= last_x
554 p
[i
++] = last_cubic_y
= last_quad_y
= last_y
557 } while (sscanf(s
, "%f %f %f %f %f %f %f %n",
558 &rx
, &ry
, &rotation
, &large_arc
, &sweep
,
562 fprintf(stderr
, "parse failed at \"%s\"\n", s
);
570 /* no real segments in path */
575 return svgtiny_add_path(p
, i
, &state
);
580 * Parse a <rect> element node.
582 * http://www.w3.org/TR/SVG11/shapes#RectElement
585 svgtiny_code
svgtiny_parse_rect(dom_element
*rect
,
586 struct svgtiny_parse_state state
)
588 float x
, y
, width
, height
;
591 svgtiny_parse_position_attributes(rect
, state
,
592 &x
, &y
, &width
, &height
);
593 svgtiny_parse_paint_attributes(rect
, &state
);
594 svgtiny_parse_transform_attributes(rect
, &state
);
596 p
= malloc(13 * sizeof p
[0]);
598 return svgtiny_OUT_OF_MEMORY
;
600 p
[0] = svgtiny_PATH_MOVE
;
603 p
[3] = svgtiny_PATH_LINE
;
606 p
[6] = svgtiny_PATH_LINE
;
609 p
[9] = svgtiny_PATH_LINE
;
612 p
[12] = svgtiny_PATH_CLOSE
;
614 return svgtiny_add_path(p
, 13, &state
);
619 * Parse a <circle> element node.
622 svgtiny_code
svgtiny_parse_circle(dom_element
*circle
,
623 struct svgtiny_parse_state state
)
625 float x
= 0, y
= 0, r
= -1;
630 exc
= dom_element_get_attribute(circle
, state
.interned_cx
, &attr
);
631 if (exc
!= DOM_NO_ERR
)
632 return svgtiny_LIBDOM_ERROR
;
634 x
= svgtiny_parse_length(attr
, state
.viewport_width
, state
);
636 dom_string_unref(attr
);
638 exc
= dom_element_get_attribute(circle
, state
.interned_cy
, &attr
);
639 if (exc
!= DOM_NO_ERR
)
640 return svgtiny_LIBDOM_ERROR
;
642 y
= svgtiny_parse_length(attr
, state
.viewport_height
, state
);
644 dom_string_unref(attr
);
646 exc
= dom_element_get_attribute(circle
, state
.interned_r
, &attr
);
647 if (exc
!= DOM_NO_ERR
)
648 return svgtiny_LIBDOM_ERROR
;
650 r
= svgtiny_parse_length(attr
, state
.viewport_width
, state
);
652 dom_string_unref(attr
);
654 svgtiny_parse_paint_attributes(circle
, &state
);
655 svgtiny_parse_transform_attributes(circle
, &state
);
658 state
.diagram
->error_line
= -1; /* circle->line; */
659 state
.diagram
->error_message
= "circle: r missing or negative";
660 return svgtiny_SVG_ERROR
;
665 p
= malloc(32 * sizeof p
[0]);
667 return svgtiny_OUT_OF_MEMORY
;
669 p
[0] = svgtiny_PATH_MOVE
;
672 p
[3] = svgtiny_PATH_BEZIER
;
674 p
[5] = y
+ r
* KAPPA
;
675 p
[6] = x
+ r
* KAPPA
;
679 p
[10] = svgtiny_PATH_BEZIER
;
680 p
[11] = x
- r
* KAPPA
;
683 p
[14] = y
+ r
* KAPPA
;
686 p
[17] = svgtiny_PATH_BEZIER
;
688 p
[19] = y
- r
* KAPPA
;
689 p
[20] = x
- r
* KAPPA
;
693 p
[24] = svgtiny_PATH_BEZIER
;
694 p
[25] = x
+ r
* KAPPA
;
697 p
[28] = y
- r
* KAPPA
;
700 p
[31] = svgtiny_PATH_CLOSE
;
702 return svgtiny_add_path(p
, 32, &state
);
707 * Parse an <ellipse> element node.
710 svgtiny_code
svgtiny_parse_ellipse(dom_element
*ellipse
,
711 struct svgtiny_parse_state state
)
713 float x
= 0, y
= 0, rx
= -1, ry
= -1;
718 exc
= dom_element_get_attribute(ellipse
, state
.interned_cx
, &attr
);
719 if (exc
!= DOM_NO_ERR
)
720 return svgtiny_LIBDOM_ERROR
;
722 x
= svgtiny_parse_length(attr
, state
.viewport_width
, state
);
724 dom_string_unref(attr
);
726 exc
= dom_element_get_attribute(ellipse
, state
.interned_cy
, &attr
);
727 if (exc
!= DOM_NO_ERR
)
728 return svgtiny_LIBDOM_ERROR
;
730 y
= svgtiny_parse_length(attr
, state
.viewport_height
, state
);
732 dom_string_unref(attr
);
734 exc
= dom_element_get_attribute(ellipse
, state
.interned_rx
, &attr
);
735 if (exc
!= DOM_NO_ERR
)
736 return svgtiny_LIBDOM_ERROR
;
738 rx
= svgtiny_parse_length(attr
, state
.viewport_width
, state
);
740 dom_string_unref(attr
);
742 exc
= dom_element_get_attribute(ellipse
, state
.interned_ry
, &attr
);
743 if (exc
!= DOM_NO_ERR
)
744 return svgtiny_LIBDOM_ERROR
;
746 ry
= svgtiny_parse_length(attr
, state
.viewport_width
, state
);
748 dom_string_unref(attr
);
750 svgtiny_parse_paint_attributes(ellipse
, &state
);
751 svgtiny_parse_transform_attributes(ellipse
, &state
);
753 if (rx
< 0 || ry
< 0) {
754 state
.diagram
->error_line
= -1; /* ellipse->line; */
755 state
.diagram
->error_message
= "ellipse: rx or ry missing "
757 return svgtiny_SVG_ERROR
;
759 if (rx
== 0 || ry
== 0)
762 p
= malloc(32 * sizeof p
[0]);
764 return svgtiny_OUT_OF_MEMORY
;
766 p
[0] = svgtiny_PATH_MOVE
;
769 p
[3] = svgtiny_PATH_BEZIER
;
771 p
[5] = y
+ ry
* KAPPA
;
772 p
[6] = x
+ rx
* KAPPA
;
776 p
[10] = svgtiny_PATH_BEZIER
;
777 p
[11] = x
- rx
* KAPPA
;
780 p
[14] = y
+ ry
* KAPPA
;
783 p
[17] = svgtiny_PATH_BEZIER
;
785 p
[19] = y
- ry
* KAPPA
;
786 p
[20] = x
- rx
* KAPPA
;
790 p
[24] = svgtiny_PATH_BEZIER
;
791 p
[25] = x
+ rx
* KAPPA
;
794 p
[28] = y
- ry
* KAPPA
;
797 p
[31] = svgtiny_PATH_CLOSE
;
799 return svgtiny_add_path(p
, 32, &state
);
804 * Parse a <line> element node.
807 svgtiny_code
svgtiny_parse_line(dom_element
*line
,
808 struct svgtiny_parse_state state
)
810 float x1
= 0, y1
= 0, x2
= 0, y2
= 0;
815 exc
= dom_element_get_attribute(line
, state
.interned_x1
, &attr
);
816 if (exc
!= DOM_NO_ERR
)
817 return svgtiny_LIBDOM_ERROR
;
819 x1
= svgtiny_parse_length(attr
, state
.viewport_width
, state
);
821 dom_string_unref(attr
);
823 exc
= dom_element_get_attribute(line
, state
.interned_y1
, &attr
);
824 if (exc
!= DOM_NO_ERR
)
825 return svgtiny_LIBDOM_ERROR
;
827 y1
= svgtiny_parse_length(attr
, state
.viewport_height
, state
);
829 dom_string_unref(attr
);
831 exc
= dom_element_get_attribute(line
, state
.interned_x2
, &attr
);
832 if (exc
!= DOM_NO_ERR
)
833 return svgtiny_LIBDOM_ERROR
;
835 x2
= svgtiny_parse_length(attr
, state
.viewport_width
, state
);
837 dom_string_unref(attr
);
839 exc
= dom_element_get_attribute(line
, state
.interned_y2
, &attr
);
840 if (exc
!= DOM_NO_ERR
)
841 return svgtiny_LIBDOM_ERROR
;
843 y2
= svgtiny_parse_length(attr
, state
.viewport_height
, state
);
845 dom_string_unref(attr
);
847 svgtiny_parse_paint_attributes(line
, &state
);
848 svgtiny_parse_transform_attributes(line
, &state
);
850 p
= malloc(7 * sizeof p
[0]);
852 return svgtiny_OUT_OF_MEMORY
;
854 p
[0] = svgtiny_PATH_MOVE
;
857 p
[3] = svgtiny_PATH_LINE
;
860 p
[6] = svgtiny_PATH_CLOSE
;
862 return svgtiny_add_path(p
, 7, &state
);
867 * Parse a <polyline> or <polygon> element node.
869 * http://www.w3.org/TR/SVG11/shapes#PolylineElement
870 * http://www.w3.org/TR/SVG11/shapes#PolygonElement
873 svgtiny_code
svgtiny_parse_poly(dom_element
*poly
,
874 struct svgtiny_parse_state state
, bool polygon
)
876 dom_string
*points_str
;
882 svgtiny_parse_paint_attributes(poly
, &state
);
883 svgtiny_parse_transform_attributes(poly
, &state
);
885 exc
= dom_element_get_attribute(poly
, state
.interned_points
,
887 if (exc
!= DOM_NO_ERR
)
888 return svgtiny_LIBDOM_ERROR
;
890 if (points_str
== NULL
) {
891 state
.diagram
->error_line
= -1; /* poly->line; */
892 state
.diagram
->error_message
=
893 "polyline/polygon: missing points attribute";
894 return svgtiny_SVG_ERROR
;
897 s
= points
= strndup(dom_string_data(points_str
),
898 dom_string_length(points_str
));
899 dom_string_unref(points_str
);
900 /* read points attribute */
902 return svgtiny_OUT_OF_MEMORY
;
903 /* allocate space for path: it will never have more elements than s */
904 p
= malloc(sizeof p
[0] * strlen(s
));
907 return svgtiny_OUT_OF_MEMORY
;
910 /* parse s and build path */
911 for (i
= 0; s
[i
]; i
++)
919 if (sscanf(s
, "%f %f %n", &x
, &y
, &n
) == 2) {
921 p
[i
++] = svgtiny_PATH_MOVE
;
923 p
[i
++] = svgtiny_PATH_LINE
;
932 p
[i
++] = svgtiny_PATH_CLOSE
;
936 return svgtiny_add_path(p
, i
, &state
);
941 * Parse a <text> or <tspan> element node.
944 svgtiny_code
svgtiny_parse_text(dom_element
*text
,
945 struct svgtiny_parse_state state
)
947 float x
, y
, width
, height
;
952 svgtiny_parse_position_attributes(text
, state
,
953 &x
, &y
, &width
, &height
);
954 svgtiny_parse_font_attributes(text
, &state
);
955 svgtiny_parse_transform_attributes(text
, &state
);
957 px
= state
.ctm
.a
* x
+ state
.ctm
.c
* y
+ state
.ctm
.e
;
958 py
= state
.ctm
.b
* x
+ state
.ctm
.d
* y
+ state
.ctm
.f
;
959 /* state.ctm.e = px - state.origin_x; */
960 /* state.ctm.f = py - state.origin_y; */
962 /*struct css_style style = state.style;
963 style.font_size.value.length.value *= state.ctm.a;*/
965 exc
= dom_node_get_first_child(text
, &child
);
966 if (exc
!= DOM_NO_ERR
)
967 return svgtiny_LIBDOM_ERROR
;
968 while (child
!= NULL
) {
970 dom_node_type nodetype
;
971 svgtiny_code code
= svgtiny_OK
;
973 exc
= dom_node_get_node_type(child
, &nodetype
);
974 if (exc
!= DOM_NO_ERR
) {
975 dom_node_unref(child
);
976 return svgtiny_LIBDOM_ERROR
;
978 if (nodetype
== DOM_ELEMENT_NODE
) {
979 dom_string
*nodename
;
980 exc
= dom_node_get_node_name(child
, &nodename
);
981 if (exc
!= DOM_NO_ERR
) {
982 dom_node_unref(child
);
983 return svgtiny_LIBDOM_ERROR
;
985 if (dom_string_caseless_isequal(nodename
,
986 state
.interned_tspan
))
987 code
= svgtiny_parse_text((dom_element
*)child
,
989 dom_string_unref(nodename
);
990 } else if (nodetype
== DOM_TEXT_NODE
) {
991 struct svgtiny_shape
*shape
= svgtiny_add_shape(&state
);
994 dom_node_unref(child
);
995 return svgtiny_OUT_OF_MEMORY
;
997 exc
= dom_text_get_whole_text(child
, &content
);
998 if (exc
!= DOM_NO_ERR
) {
999 dom_node_unref(child
);
1000 return svgtiny_LIBDOM_ERROR
;
1002 shape
->text
= strndup(dom_string_data(content
),
1003 dom_string_length(content
));
1004 dom_string_unref(content
);
1007 state
.diagram
->shape_count
++;
1010 if (code
!= svgtiny_OK
) {
1011 dom_node_unref(child
);
1014 exc
= dom_node_get_next_sibling(child
, &next
);
1015 dom_node_unref(child
);
1016 if (exc
!= DOM_NO_ERR
)
1017 return svgtiny_LIBDOM_ERROR
;
1026 * Parse x, y, width, and height attributes, if present.
1029 void svgtiny_parse_position_attributes(const dom_element
*node
,
1030 const struct svgtiny_parse_state state
,
1031 float *x
, float *y
, float *width
, float *height
)
1038 *width
= state
.viewport_width
;
1039 *height
= state
.viewport_height
;
1041 exc
= dom_element_get_attribute(node
, state
.interned_x
, &attr
);
1042 if (exc
== DOM_NO_ERR
&& attr
!= NULL
) {
1043 *x
= svgtiny_parse_length(attr
, state
.viewport_width
, state
);
1044 dom_string_unref(attr
);
1047 exc
= dom_element_get_attribute(node
, state
.interned_y
, &attr
);
1048 if (exc
== DOM_NO_ERR
&& attr
!= NULL
) {
1049 *y
= svgtiny_parse_length(attr
, state
.viewport_height
, state
);
1050 dom_string_unref(attr
);
1053 exc
= dom_element_get_attribute(node
, state
.interned_width
, &attr
);
1054 if (exc
== DOM_NO_ERR
&& attr
!= NULL
) {
1055 *width
= svgtiny_parse_length(attr
, state
.viewport_width
,
1057 dom_string_unref(attr
);
1060 exc
= dom_element_get_attribute(node
, state
.interned_height
, &attr
);
1061 if (exc
== DOM_NO_ERR
&& attr
!= NULL
) {
1062 *height
= svgtiny_parse_length(attr
, state
.viewport_height
,
1064 dom_string_unref(attr
);
1070 * Parse a length as a number of pixels.
1073 static float _svgtiny_parse_length(const char *s
, int viewport_size
,
1074 const struct svgtiny_parse_state state
)
1076 int num_length
= strspn(s
, "0123456789+-.");
1077 const char *unit
= s
+ num_length
;
1078 float n
= atof((const char *) s
);
1079 float font_size
= 20; /*css_len2px(&state.style.font_size.value.length, 0);*/
1085 } else if (unit
[0] == '%') {
1086 return n
/ 100.0 * viewport_size
;
1087 } else if (unit
[0] == 'e' && unit
[1] == 'm') {
1088 return n
* font_size
;
1089 } else if (unit
[0] == 'e' && unit
[1] == 'x') {
1090 return n
/ 2.0 * font_size
;
1091 } else if (unit
[0] == 'p' && unit
[1] == 'x') {
1093 } else if (unit
[0] == 'p' && unit
[1] == 't') {
1095 } else if (unit
[0] == 'p' && unit
[1] == 'c') {
1097 } else if (unit
[0] == 'm' && unit
[1] == 'm') {
1098 return n
* 3.543307;
1099 } else if (unit
[0] == 'c' && unit
[1] == 'm') {
1100 return n
* 35.43307;
1101 } else if (unit
[0] == 'i' && unit
[1] == 'n') {
1108 float svgtiny_parse_length(dom_string
*s
, int viewport_size
,
1109 const struct svgtiny_parse_state state
)
1111 char *ss
= strndup(dom_string_data(s
), dom_string_length(s
));
1112 float ret
= _svgtiny_parse_length(ss
, viewport_size
, state
);
1118 * Parse paint attributes, if present.
1121 void svgtiny_parse_paint_attributes(const dom_element
*node
,
1122 struct svgtiny_parse_state
*state
)
1127 exc
= dom_element_get_attribute(node
, state
->interned_fill
, &attr
);
1128 if (exc
== DOM_NO_ERR
&& attr
!= NULL
) {
1129 svgtiny_parse_color(attr
, &state
->fill
, state
);
1130 dom_string_unref(attr
);
1133 exc
= dom_element_get_attribute(node
, state
->interned_stroke
, &attr
);
1134 if (exc
== DOM_NO_ERR
&& attr
!= NULL
) {
1135 svgtiny_parse_color(attr
, &state
->stroke
, state
);
1136 dom_string_unref(attr
);
1139 exc
= dom_element_get_attribute(node
, state
->interned_stroke_width
, &attr
);
1140 if (exc
== DOM_NO_ERR
&& attr
!= NULL
) {
1141 state
->stroke_width
= svgtiny_parse_length(attr
,
1142 state
->viewport_width
, *state
);
1143 dom_string_unref(attr
);
1146 exc
= dom_element_get_attribute(node
, state
->interned_style
, &attr
);
1147 if (exc
== DOM_NO_ERR
&& attr
!= NULL
) {
1148 char *style
= strndup(dom_string_data(attr
),
1149 dom_string_length(attr
));
1152 if ((s
= strstr(style
, "fill:"))) {
1156 value
= strndup(s
, strcspn(s
, "; "));
1157 _svgtiny_parse_color(value
, &state
->fill
, state
);
1160 if ((s
= strstr(style
, "stroke:"))) {
1164 value
= strndup(s
, strcspn(s
, "; "));
1165 _svgtiny_parse_color(value
, &state
->stroke
, state
);
1168 if ((s
= strstr(style
, "stroke-width:"))) {
1172 value
= strndup(s
, strcspn(s
, "; "));
1173 state
->stroke_width
= _svgtiny_parse_length(value
,
1174 state
->viewport_width
, *state
);
1178 dom_string_unref(attr
);
1187 static void _svgtiny_parse_color(const char *s
, svgtiny_colour
*c
,
1188 struct svgtiny_parse_state
*state
)
1190 unsigned int r
, g
, b
;
1192 size_t len
= strlen(s
);
1193 char *id
= 0, *rparen
;
1195 if (len
== 4 && s
[0] == '#') {
1196 if (sscanf(s
+ 1, "%1x%1x%1x", &r
, &g
, &b
) == 3)
1197 *c
= svgtiny_RGB(r
| r
<< 4, g
| g
<< 4, b
| b
<< 4);
1199 } else if (len
== 7 && s
[0] == '#') {
1200 if (sscanf(s
+ 1, "%2x%2x%2x", &r
, &g
, &b
) == 3)
1201 *c
= svgtiny_RGB(r
, g
, b
);
1203 } else if (10 <= len
&& s
[0] == 'r' && s
[1] == 'g' && s
[2] == 'b' &&
1204 s
[3] == '(' && s
[len
- 1] == ')') {
1205 if (sscanf(s
+ 4, "%u,%u,%u", &r
, &g
, &b
) == 3)
1206 *c
= svgtiny_RGB(r
, g
, b
);
1207 else if (sscanf(s
+ 4, "%f%%,%f%%,%f%%", &rf
, &gf
, &bf
) == 3) {
1211 *c
= svgtiny_RGB(r
, g
, b
);
1214 } else if (len
== 4 && strcmp(s
, "none") == 0) {
1215 *c
= svgtiny_TRANSPARENT
;
1217 } else if (5 < len
&& s
[0] == 'u' && s
[1] == 'r' && s
[2] == 'l' &&
1223 rparen
= strchr(id
, ')');
1226 svgtiny_find_gradient(id
, state
);
1228 fprintf(stderr
, "linear_gradient_stop_count %i\n",
1229 state
->linear_gradient_stop_count
);
1230 if (state
->linear_gradient_stop_count
== 0)
1231 *c
= svgtiny_TRANSPARENT
;
1232 else if (state
->linear_gradient_stop_count
== 1)
1233 *c
= state
->gradient_stop
[0].color
;
1235 *c
= svgtiny_LINEAR_GRADIENT
;
1239 const struct svgtiny_named_color
*named_color
;
1240 named_color
= svgtiny_color_lookup(s
, strlen(s
));
1242 *c
= named_color
->color
;
1246 void svgtiny_parse_color(dom_string
*s
, svgtiny_colour
*c
,
1247 struct svgtiny_parse_state
*state
)
1249 char *ss
= strndup(dom_string_data(s
), dom_string_length(s
));
1250 _svgtiny_parse_color(ss
, c
, state
);
1255 * Parse font attributes, if present.
1258 void svgtiny_parse_font_attributes(const dom_element
*node
,
1259 struct svgtiny_parse_state
*state
)
1261 /* TODO: Implement this, it never used to be */
1264 #ifdef WRITTEN_THIS_PROPERLY
1265 const xmlAttr
*attr
;
1269 for (attr
= node
->properties
; attr
; attr
= attr
->next
) {
1270 if (strcmp((const char *) attr
->name
, "font-size") == 0) {
1271 /*if (css_parse_length(
1272 (const char *) attr->children->content,
1273 &state->style.font_size.value.length,
1275 state->style.font_size.size =
1276 CSS_FONT_SIZE_LENGTH;
1285 * Parse transform attributes, if present.
1287 * http://www.w3.org/TR/SVG11/coords#TransformAttribute
1290 void svgtiny_parse_transform_attributes(dom_element
*node
,
1291 struct svgtiny_parse_state
*state
)
1297 exc
= dom_element_get_attribute(node
, state
->interned_transform
,
1299 if (exc
== DOM_NO_ERR
&& attr
!= NULL
) {
1300 transform
= strndup(dom_string_data(attr
),
1301 dom_string_length(attr
));
1302 svgtiny_parse_transform(transform
, &state
->ctm
.a
, &state
->ctm
.b
,
1303 &state
->ctm
.c
, &state
->ctm
.d
,
1304 &state
->ctm
.e
, &state
->ctm
.f
);
1306 dom_string_unref(attr
);
1312 * Parse a transform string.
1315 void svgtiny_parse_transform(char *s
, float *ma
, float *mb
,
1316 float *mc
, float *md
, float *me
, float *mf
)
1318 float a
, b
, c
, d
, e
, f
;
1319 float za
, zb
, zc
, zd
, ze
, zf
;
1324 for (i
= 0; s
[i
]; i
++)
1332 if (sscanf(s
, "matrix (%f %f %f %f %f %f) %n",
1333 &a
, &b
, &c
, &d
, &e
, &f
, &n
) == 6)
1335 else if (sscanf(s
, "translate (%f %f) %n",
1338 else if (sscanf(s
, "translate (%f) %n",
1341 else if (sscanf(s
, "scale (%f %f) %n",
1344 else if (sscanf(s
, "scale (%f) %n",
1347 else if (sscanf(s
, "rotate (%f %f %f) %n",
1348 &angle
, &x
, &y
, &n
) == 3) {
1349 angle
= angle
/ 180 * M_PI
;
1354 e
= -x
* cos(angle
) + y
* sin(angle
) + x
;
1355 f
= -x
* sin(angle
) - y
* cos(angle
) + y
;
1356 } else if (sscanf(s
, "rotate (%f) %n",
1358 angle
= angle
/ 180 * M_PI
;
1363 } else if (sscanf(s
, "skewX (%f) %n",
1365 angle
= angle
/ 180 * M_PI
;
1367 } else if (sscanf(s
, "skewY (%f) %n",
1369 angle
= angle
/ 180 * M_PI
;
1373 za
= *ma
* a
+ *mc
* b
;
1374 zb
= *mb
* a
+ *md
* b
;
1375 zc
= *ma
* c
+ *mc
* d
;
1376 zd
= *mb
* c
+ *md
* d
;
1377 ze
= *ma
* e
+ *mc
* f
+ *me
;
1378 zf
= *mb
* e
+ *md
* f
+ *mf
;
1391 * Add a path to the svgtiny_diagram.
1394 svgtiny_code
svgtiny_add_path(float *p
, unsigned int n
,
1395 struct svgtiny_parse_state
*state
)
1397 struct svgtiny_shape
*shape
;
1399 if (state
->fill
== svgtiny_LINEAR_GRADIENT
)
1400 return svgtiny_add_path_linear_gradient(p
, n
, state
);
1402 svgtiny_transform_path(p
, n
, state
);
1404 shape
= svgtiny_add_shape(state
);
1407 return svgtiny_OUT_OF_MEMORY
;
1410 shape
->path_length
= n
;
1411 state
->diagram
->shape_count
++;
1418 * Add a svgtiny_shape to the svgtiny_diagram.
1421 struct svgtiny_shape
*svgtiny_add_shape(struct svgtiny_parse_state
*state
)
1423 struct svgtiny_shape
*shape
= realloc(state
->diagram
->shape
,
1424 (state
->diagram
->shape_count
+ 1) *
1425 sizeof (state
->diagram
->shape
[0]));
1428 state
->diagram
->shape
= shape
;
1430 shape
+= state
->diagram
->shape_count
;
1432 shape
->path_length
= 0;
1434 shape
->fill
= state
->fill
;
1435 shape
->stroke
= state
->stroke
;
1436 shape
->stroke_width
= lroundf((float) state
->stroke_width
*
1437 (state
->ctm
.a
+ state
->ctm
.d
) / 2.0);
1438 if (0 < state
->stroke_width
&& shape
->stroke_width
== 0)
1439 shape
->stroke_width
= 1;
1446 * Apply the current transformation matrix to a path.
1449 void svgtiny_transform_path(float *p
, unsigned int n
,
1450 struct svgtiny_parse_state
*state
)
1454 for (j
= 0; j
!= n
; ) {
1455 unsigned int points
= 0;
1457 switch ((int) p
[j
]) {
1458 case svgtiny_PATH_MOVE
:
1459 case svgtiny_PATH_LINE
:
1462 case svgtiny_PATH_CLOSE
:
1465 case svgtiny_PATH_BEZIER
:
1472 for (k
= 0; k
!= points
; k
++) {
1473 float x0
= p
[j
], y0
= p
[j
+ 1];
1474 float x
= state
->ctm
.a
* x0
+ state
->ctm
.c
* y0
+
1476 float y
= state
->ctm
.b
* x0
+ state
->ctm
.d
* y0
+
1487 * Free all memory used by a diagram.
1490 void svgtiny_free(struct svgtiny_diagram
*svg
)
1495 for (i
= 0; i
!= svg
->shape_count
; i
++) {
1496 free(svg
->shape
[i
].path
);
1497 free(svg
->shape
[i
].text
);
1505 #ifndef HAVE_STRNDUP
1506 char *svgtiny_strndup(const char *s
, size_t n
)
1511 for (len
= 0; len
!= n
&& s
[len
]; len
++)
1514 s2
= malloc(len
+ 1);