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 state
.diagram
= diagram
;
166 state
.document
= document
;
167 state
.viewport_width
= viewport_width
;
168 state
.viewport_height
= viewport_height
;
170 #define SVGTINY_STRING_ACTION2(s,n) \
171 if (dom_string_create_interned((const uint8_t *) #n, \
172 strlen(#n), &state.interned_##s) \
174 code = svgtiny_LIBDOM_ERROR; \
177 #include "svgtiny_strings.h"
178 #undef SVGTINY_STRING_ACTION2
180 svgtiny_parse_position_attributes(svg
, state
, &x
, &y
, &width
, &height
);
181 diagram
->width
= width
;
182 diagram
->height
= height
;
184 /* set up parsing state */
185 state
.viewport_width
= width
;
186 state
.viewport_height
= height
;
187 state
.ctm
.a
= 1; /*(float) viewport_width / (float) width;*/
190 state
.ctm
.d
= 1; /*(float) viewport_height / (float) height;*/
191 state
.ctm
.e
= 0; /*x;*/
192 state
.ctm
.f
= 0; /*y;*/
193 /*state.style = css_base_style;
194 state.style.font_size.value.length.value = option_font_size * 0.1;*/
195 state
.fill
= 0x000000;
196 state
.stroke
= svgtiny_TRANSPARENT
;
197 state
.stroke_width
= 1;
198 state
.linear_gradient_stop_count
= 0;
201 code
= svgtiny_parse_svg(svg
, state
);
204 dom_node_unref(document
);
207 #define SVGTINY_STRING_ACTION2(s,n) \
208 if (state.interned_##s != NULL) \
209 dom_string_unref(state.interned_##s);
210 //#include "svgtiny_strings.h"
211 #undef SVGTINY_STRING_ACTION2
217 * Parse a <svg> or <g> element node.
220 svgtiny_code
svgtiny_parse_svg(dom_element
*svg
,
221 struct svgtiny_parse_state state
)
223 float x
, y
, width
, height
;
224 dom_string
*view_box
;
228 svgtiny_parse_position_attributes(svg
, state
, &x
, &y
, &width
, &height
);
229 svgtiny_parse_paint_attributes(svg
, &state
);
230 svgtiny_parse_font_attributes(svg
, &state
);
232 exc
= dom_element_get_attribute(svg
, state
.interned_viewBox
,
234 if (exc
!= DOM_NO_ERR
) {
235 return svgtiny_LIBDOM_ERROR
;
239 char *s
= strndup(dom_string_data(view_box
),
240 dom_string_length(view_box
));
241 float min_x
, min_y
, vwidth
, vheight
;
242 if (sscanf(s
, "%f,%f,%f,%f",
243 &min_x
, &min_y
, &vwidth
, &vheight
) == 4 ||
244 sscanf(s
, "%f %f %f %f",
245 &min_x
, &min_y
, &vwidth
, &vheight
) == 4) {
246 state
.ctm
.a
= (float) state
.viewport_width
/ vwidth
;
247 state
.ctm
.d
= (float) state
.viewport_height
/ vheight
;
248 state
.ctm
.e
+= -min_x
* state
.ctm
.a
;
249 state
.ctm
.f
+= -min_y
* state
.ctm
.d
;
252 dom_string_unref(view_box
);
255 svgtiny_parse_transform_attributes(svg
, &state
);
257 exc
= dom_node_get_first_child(svg
, &child
);
258 if (exc
!= DOM_NO_ERR
) {
259 return svgtiny_LIBDOM_ERROR
;
261 while (child
!= NULL
) {
263 dom_node_type nodetype
;
264 svgtiny_code code
= svgtiny_OK
;
266 exc
= dom_node_get_node_type(child
, &nodetype
);
267 if (exc
!= DOM_NO_ERR
) {
268 dom_node_unref(child
);
269 return svgtiny_LIBDOM_ERROR
;
271 if (nodetype
== DOM_ELEMENT_NODE
) {
272 dom_string
*nodename
;
273 exc
= dom_node_get_node_name(child
, &nodename
);
274 if (exc
!= DOM_NO_ERR
) {
275 dom_node_unref(child
);
276 return svgtiny_LIBDOM_ERROR
;
278 if (dom_string_caseless_isequal(state
.interned_svg
,
280 code
= svgtiny_parse_svg(child
, state
);
281 else if (dom_string_caseless_isequal(state
.interned_g
,
283 code
= svgtiny_parse_svg(child
, state
);
284 else if (dom_string_caseless_isequal(state
.interned_a
,
286 code
= svgtiny_parse_svg(child
, state
);
287 else if (dom_string_caseless_isequal(state
.interned_path
,
289 code
= svgtiny_parse_path(child
, state
);
290 else if (dom_string_caseless_isequal(state
.interned_rect
,
292 code
= svgtiny_parse_rect(child
, state
);
293 else if (dom_string_caseless_isequal(state
.interned_circle
,
295 code
= svgtiny_parse_circle(child
, state
);
296 else if (dom_string_caseless_isequal(state
.interned_ellipse
,
298 code
= svgtiny_parse_ellipse(child
, state
);
299 else if (dom_string_caseless_isequal(state
.interned_line
,
301 code
= svgtiny_parse_line(child
, state
);
302 else if (dom_string_caseless_isequal(state
.interned_polyline
,
304 code
= svgtiny_parse_poly(child
, state
, false);
305 else if (dom_string_caseless_isequal(state
.interned_polygon
,
307 code
= svgtiny_parse_poly(child
, state
, true);
308 else if (dom_string_caseless_isequal(state
.interned_text
,
310 code
= svgtiny_parse_text(child
, state
);
311 dom_string_unref(nodename
);
313 if (code
!= svgtiny_OK
) {
314 dom_node_unref(child
);
317 exc
= dom_node_get_next_sibling(child
, &next
);
318 dom_node_unref(child
);
319 if (exc
!= DOM_NO_ERR
) {
320 return svgtiny_LIBDOM_ERROR
;
331 * Parse a <path> element node.
333 * http://www.w3.org/TR/SVG11/paths#PathElement
336 svgtiny_code
svgtiny_parse_path(dom_element
*path
,
337 struct svgtiny_parse_state state
)
339 dom_string
*path_d_str
;
344 float last_x
= 0, last_y
= 0;
345 float last_cubic_x
= 0, last_cubic_y
= 0;
346 float last_quad_x
= 0, last_quad_y
= 0;
348 svgtiny_parse_paint_attributes(path
, &state
);
349 svgtiny_parse_transform_attributes(path
, &state
);
351 /* read d attribute */
352 exc
= dom_element_get_attribute(path
, state
.interned_d
, &path_d_str
);
353 if (exc
!= DOM_NO_ERR
) {
354 state
.diagram
->error_line
= -1; /* path->line; */
355 state
.diagram
->error_message
= "path: error retrieving d attribute";
356 return svgtiny_SVG_ERROR
;
359 if (path_d_str
== NULL
) {
360 state
.diagram
->error_line
= -1; /* path->line; */
361 state
.diagram
->error_message
= "path: missing d attribute";
362 return svgtiny_SVG_ERROR
;
365 s
= path_d
= strndup(dom_string_data(path_d_str
),
366 dom_string_length(path_d_str
));
367 dom_string_unref(path_d_str
);
369 return svgtiny_OUT_OF_MEMORY
;
371 /* allocate space for path: it will never have more elements than d */
372 p
= malloc(sizeof p
[0] * strlen(s
));
375 return svgtiny_OUT_OF_MEMORY
;
378 /* parse d and build path */
379 for (i
= 0; s
[i
]; i
++)
386 float x
, y
, x1
, y1
, x2
, y2
, rx
, ry
, rotation
, large_arc
, sweep
;
389 /* moveto (M, m), lineto (L, l) (2 arguments) */
390 if (sscanf(s
, " %1[MmLl] %f %f %n", command
, &x
, &y
, &n
) == 3) {
391 /*LOG(("moveto or lineto"));*/
392 if (*command
== 'M' || *command
== 'm')
393 plot_command
= svgtiny_PATH_MOVE
;
395 plot_command
= svgtiny_PATH_LINE
;
397 p
[i
++] = plot_command
;
398 if ('a' <= *command
) {
402 p
[i
++] = last_cubic_x
= last_quad_x
= last_x
404 p
[i
++] = last_cubic_y
= last_quad_y
= last_y
407 plot_command
= svgtiny_PATH_LINE
;
408 } while (sscanf(s
, "%f %f %n", &x
, &y
, &n
) == 2);
410 /* closepath (Z, z) (no arguments) */
411 } else if (sscanf(s
, " %1[Zz] %n", command
, &n
) == 1) {
412 /*LOG(("closepath"));*/
413 p
[i
++] = svgtiny_PATH_CLOSE
;
416 /* horizontal lineto (H, h) (1 argument) */
417 } else if (sscanf(s
, " %1[Hh] %f %n", command
, &x
, &n
) == 2) {
418 /*LOG(("horizontal lineto"));*/
420 p
[i
++] = svgtiny_PATH_LINE
;
423 p
[i
++] = last_cubic_x
= last_quad_x
= last_x
425 p
[i
++] = last_cubic_y
= last_quad_y
= last_y
;
427 } while (sscanf(s
, "%f %n", &x
, &n
) == 1);
429 /* vertical lineto (V, v) (1 argument) */
430 } else if (sscanf(s
, " %1[Vv] %f %n", command
, &y
, &n
) == 2) {
431 /*LOG(("vertical lineto"));*/
433 p
[i
++] = svgtiny_PATH_LINE
;
436 p
[i
++] = last_cubic_x
= last_quad_x
= last_x
;
437 p
[i
++] = last_cubic_y
= last_quad_y
= last_y
440 } while (sscanf(s
, "%f %n", &x
, &n
) == 1);
442 /* curveto (C, c) (6 arguments) */
443 } else if (sscanf(s
, " %1[Cc] %f %f %f %f %f %f %n", command
,
444 &x1
, &y1
, &x2
, &y2
, &x
, &y
, &n
) == 7) {
445 /*LOG(("curveto"));*/
447 p
[i
++] = svgtiny_PATH_BEZIER
;
448 if (*command
== 'c') {
458 p
[i
++] = last_cubic_x
= x2
;
459 p
[i
++] = last_cubic_y
= y2
;
460 p
[i
++] = last_quad_x
= last_x
= x
;
461 p
[i
++] = last_quad_y
= last_y
= y
;
463 } while (sscanf(s
, "%f %f %f %f %f %f %n",
464 &x1
, &y1
, &x2
, &y2
, &x
, &y
, &n
) == 6);
466 /* shorthand/smooth curveto (S, s) (4 arguments) */
467 } else if (sscanf(s
, " %1[Ss] %f %f %f %f %n", command
,
468 &x2
, &y2
, &x
, &y
, &n
) == 5) {
469 /*LOG(("shorthand/smooth curveto"));*/
471 p
[i
++] = svgtiny_PATH_BEZIER
;
472 x1
= last_x
+ (last_x
- last_cubic_x
);
473 y1
= last_y
+ (last_y
- last_cubic_y
);
474 if (*command
== 's') {
482 p
[i
++] = last_cubic_x
= x2
;
483 p
[i
++] = last_cubic_y
= y2
;
484 p
[i
++] = last_quad_x
= last_x
= x
;
485 p
[i
++] = last_quad_y
= last_y
= y
;
487 } while (sscanf(s
, "%f %f %f %f %n",
488 &x2
, &y2
, &x
, &y
, &n
) == 4);
490 /* quadratic Bezier curveto (Q, q) (4 arguments) */
491 } else if (sscanf(s
, " %1[Qq] %f %f %f %f %n", command
,
492 &x1
, &y1
, &x
, &y
, &n
) == 5) {
493 /*LOG(("quadratic Bezier curveto"));*/
495 p
[i
++] = svgtiny_PATH_BEZIER
;
498 if (*command
== 'q') {
504 p
[i
++] = 1./3 * last_x
+ 2./3 * x1
;
505 p
[i
++] = 1./3 * last_y
+ 2./3 * y1
;
506 p
[i
++] = 2./3 * x1
+ 1./3 * x
;
507 p
[i
++] = 2./3 * y1
+ 1./3 * y
;
508 p
[i
++] = last_cubic_x
= last_x
= x
;
509 p
[i
++] = last_cubic_y
= last_y
= y
;
511 } while (sscanf(s
, "%f %f %f %f %n",
512 &x1
, &y1
, &x
, &y
, &n
) == 4);
514 /* shorthand/smooth quadratic Bezier curveto (T, t)
516 } else if (sscanf(s
, " %1[Tt] %f %f %n", command
,
518 /*LOG(("shorthand/smooth quadratic Bezier curveto"));*/
520 p
[i
++] = svgtiny_PATH_BEZIER
;
521 x1
= last_x
+ (last_x
- last_quad_x
);
522 y1
= last_y
+ (last_y
- last_quad_y
);
525 if (*command
== 't') {
531 p
[i
++] = 1./3 * last_x
+ 2./3 * x1
;
532 p
[i
++] = 1./3 * last_y
+ 2./3 * y1
;
533 p
[i
++] = 2./3 * x1
+ 1./3 * x
;
534 p
[i
++] = 2./3 * y1
+ 1./3 * y
;
535 p
[i
++] = last_cubic_x
= last_x
= x
;
536 p
[i
++] = last_cubic_y
= last_y
= y
;
538 } while (sscanf(s
, "%f %f %n",
541 /* elliptical arc (A, a) (7 arguments) */
542 } else if (sscanf(s
, " %1[Aa] %f %f %f %f %f %f %f %n", command
,
543 &rx
, &ry
, &rotation
, &large_arc
, &sweep
,
546 p
[i
++] = svgtiny_PATH_LINE
;
547 if (*command
== 'a') {
551 p
[i
++] = last_cubic_x
= last_quad_x
= last_x
553 p
[i
++] = last_cubic_y
= last_quad_y
= last_y
556 } while (sscanf(s
, "%f %f %f %f %f %f %f %n",
557 &rx
, &ry
, &rotation
, &large_arc
, &sweep
,
561 fprintf(stderr
, "parse failed at \"%s\"\n", s
);
569 /* no real segments in path */
574 return svgtiny_add_path(p
, i
, &state
);
579 * Parse a <rect> element node.
581 * http://www.w3.org/TR/SVG11/shapes#RectElement
584 svgtiny_code
svgtiny_parse_rect(dom_element
*rect
,
585 struct svgtiny_parse_state state
)
587 float x
, y
, width
, height
;
590 svgtiny_parse_position_attributes(rect
, state
,
591 &x
, &y
, &width
, &height
);
592 svgtiny_parse_paint_attributes(rect
, &state
);
593 svgtiny_parse_transform_attributes(rect
, &state
);
595 p
= malloc(13 * sizeof p
[0]);
597 return svgtiny_OUT_OF_MEMORY
;
599 p
[0] = svgtiny_PATH_MOVE
;
602 p
[3] = svgtiny_PATH_LINE
;
605 p
[6] = svgtiny_PATH_LINE
;
608 p
[9] = svgtiny_PATH_LINE
;
611 p
[12] = svgtiny_PATH_CLOSE
;
613 return svgtiny_add_path(p
, 13, &state
);
618 * Parse a <circle> element node.
621 svgtiny_code
svgtiny_parse_circle(dom_element
*circle
,
622 struct svgtiny_parse_state state
)
624 float x
= 0, y
= 0, r
= -1;
629 exc
= dom_element_get_attribute(circle
, state
.interned_cx
, &attr
);
630 if (exc
!= DOM_NO_ERR
)
631 return svgtiny_LIBDOM_ERROR
;
633 x
= svgtiny_parse_length(attr
, state
.viewport_width
, state
);
635 dom_string_unref(attr
);
637 exc
= dom_element_get_attribute(circle
, state
.interned_cy
, &attr
);
638 if (exc
!= DOM_NO_ERR
)
639 return svgtiny_LIBDOM_ERROR
;
641 y
= svgtiny_parse_length(attr
, state
.viewport_height
, state
);
643 dom_string_unref(attr
);
645 exc
= dom_element_get_attribute(circle
, state
.interned_r
, &attr
);
646 if (exc
!= DOM_NO_ERR
)
647 return svgtiny_LIBDOM_ERROR
;
649 r
= svgtiny_parse_length(attr
, state
.viewport_width
, state
);
651 dom_string_unref(attr
);
653 svgtiny_parse_paint_attributes(circle
, &state
);
654 svgtiny_parse_transform_attributes(circle
, &state
);
657 state
.diagram
->error_line
= -1; /* circle->line; */
658 state
.diagram
->error_message
= "circle: r missing or negative";
659 return svgtiny_SVG_ERROR
;
664 p
= malloc(32 * sizeof p
[0]);
666 return svgtiny_OUT_OF_MEMORY
;
668 p
[0] = svgtiny_PATH_MOVE
;
671 p
[3] = svgtiny_PATH_BEZIER
;
673 p
[5] = y
+ r
* KAPPA
;
674 p
[6] = x
+ r
* KAPPA
;
678 p
[10] = svgtiny_PATH_BEZIER
;
679 p
[11] = x
- r
* KAPPA
;
682 p
[14] = y
+ r
* KAPPA
;
685 p
[17] = svgtiny_PATH_BEZIER
;
687 p
[19] = y
- r
* KAPPA
;
688 p
[20] = x
- r
* KAPPA
;
692 p
[24] = svgtiny_PATH_BEZIER
;
693 p
[25] = x
+ r
* KAPPA
;
696 p
[28] = y
- r
* KAPPA
;
699 p
[31] = svgtiny_PATH_CLOSE
;
701 return svgtiny_add_path(p
, 32, &state
);
706 * Parse an <ellipse> element node.
709 svgtiny_code
svgtiny_parse_ellipse(dom_element
*ellipse
,
710 struct svgtiny_parse_state state
)
712 float x
= 0, y
= 0, rx
= -1, ry
= -1;
717 exc
= dom_element_get_attribute(ellipse
, state
.interned_cx
, &attr
);
718 if (exc
!= DOM_NO_ERR
)
719 return svgtiny_LIBDOM_ERROR
;
721 x
= svgtiny_parse_length(attr
, state
.viewport_width
, state
);
723 dom_string_unref(attr
);
725 exc
= dom_element_get_attribute(ellipse
, state
.interned_cy
, &attr
);
726 if (exc
!= DOM_NO_ERR
)
727 return svgtiny_LIBDOM_ERROR
;
729 y
= svgtiny_parse_length(attr
, state
.viewport_height
, state
);
731 dom_string_unref(attr
);
733 exc
= dom_element_get_attribute(ellipse
, state
.interned_rx
, &attr
);
734 if (exc
!= DOM_NO_ERR
)
735 return svgtiny_LIBDOM_ERROR
;
737 rx
= svgtiny_parse_length(attr
, state
.viewport_width
, state
);
739 dom_string_unref(attr
);
741 exc
= dom_element_get_attribute(ellipse
, state
.interned_ry
, &attr
);
742 if (exc
!= DOM_NO_ERR
)
743 return svgtiny_LIBDOM_ERROR
;
745 ry
= svgtiny_parse_length(attr
, state
.viewport_width
, state
);
747 dom_string_unref(attr
);
749 svgtiny_parse_paint_attributes(ellipse
, &state
);
750 svgtiny_parse_transform_attributes(ellipse
, &state
);
752 if (rx
< 0 || ry
< 0) {
753 state
.diagram
->error_line
= -1; /* ellipse->line; */
754 state
.diagram
->error_message
= "ellipse: rx or ry missing "
756 return svgtiny_SVG_ERROR
;
758 if (rx
== 0 || ry
== 0)
761 p
= malloc(32 * sizeof p
[0]);
763 return svgtiny_OUT_OF_MEMORY
;
765 p
[0] = svgtiny_PATH_MOVE
;
768 p
[3] = svgtiny_PATH_BEZIER
;
770 p
[5] = y
+ ry
* KAPPA
;
771 p
[6] = x
+ rx
* KAPPA
;
775 p
[10] = svgtiny_PATH_BEZIER
;
776 p
[11] = x
- rx
* KAPPA
;
779 p
[14] = y
+ ry
* KAPPA
;
782 p
[17] = svgtiny_PATH_BEZIER
;
784 p
[19] = y
- ry
* KAPPA
;
785 p
[20] = x
- rx
* KAPPA
;
789 p
[24] = svgtiny_PATH_BEZIER
;
790 p
[25] = x
+ rx
* KAPPA
;
793 p
[28] = y
- ry
* KAPPA
;
796 p
[31] = svgtiny_PATH_CLOSE
;
798 return svgtiny_add_path(p
, 32, &state
);
803 * Parse a <line> element node.
806 svgtiny_code
svgtiny_parse_line(dom_element
*line
,
807 struct svgtiny_parse_state state
)
809 float x1
= 0, y1
= 0, x2
= 0, y2
= 0;
814 exc
= dom_element_get_attribute(line
, state
.interned_x1
, &attr
);
815 if (exc
!= DOM_NO_ERR
)
816 return svgtiny_LIBDOM_ERROR
;
818 x1
= svgtiny_parse_length(attr
, state
.viewport_width
, state
);
820 dom_string_unref(attr
);
822 exc
= dom_element_get_attribute(line
, state
.interned_y1
, &attr
);
823 if (exc
!= DOM_NO_ERR
)
824 return svgtiny_LIBDOM_ERROR
;
826 y1
= svgtiny_parse_length(attr
, state
.viewport_height
, state
);
828 dom_string_unref(attr
);
830 exc
= dom_element_get_attribute(line
, state
.interned_x2
, &attr
);
831 if (exc
!= DOM_NO_ERR
)
832 return svgtiny_LIBDOM_ERROR
;
834 x2
= svgtiny_parse_length(attr
, state
.viewport_width
, state
);
836 dom_string_unref(attr
);
838 exc
= dom_element_get_attribute(line
, state
.interned_y2
, &attr
);
839 if (exc
!= DOM_NO_ERR
)
840 return svgtiny_LIBDOM_ERROR
;
842 y2
= svgtiny_parse_length(attr
, state
.viewport_height
, state
);
844 dom_string_unref(attr
);
846 svgtiny_parse_paint_attributes(line
, &state
);
847 svgtiny_parse_transform_attributes(line
, &state
);
849 p
= malloc(7 * sizeof p
[0]);
851 return svgtiny_OUT_OF_MEMORY
;
853 p
[0] = svgtiny_PATH_MOVE
;
856 p
[3] = svgtiny_PATH_LINE
;
859 p
[6] = svgtiny_PATH_CLOSE
;
861 return svgtiny_add_path(p
, 7, &state
);
866 * Parse a <polyline> or <polygon> element node.
868 * http://www.w3.org/TR/SVG11/shapes#PolylineElement
869 * http://www.w3.org/TR/SVG11/shapes#PolygonElement
872 svgtiny_code
svgtiny_parse_poly(dom_element
*poly
,
873 struct svgtiny_parse_state state
, bool polygon
)
875 dom_string
*points_str
;
881 svgtiny_parse_paint_attributes(poly
, &state
);
882 svgtiny_parse_transform_attributes(poly
, &state
);
884 exc
= dom_element_get_attribute(poly
, state
.interned_points
,
886 if (exc
!= DOM_NO_ERR
)
887 return svgtiny_LIBDOM_ERROR
;
889 if (points_str
== NULL
) {
890 state
.diagram
->error_line
= -1; /* poly->line; */
891 state
.diagram
->error_message
=
892 "polyline/polygon: missing points attribute";
893 return svgtiny_SVG_ERROR
;
896 s
= points
= strndup(dom_string_data(points_str
),
897 dom_string_length(points_str
));
898 dom_string_unref(points_str
);
899 /* read points attribute */
901 return svgtiny_OUT_OF_MEMORY
;
902 /* allocate space for path: it will never have more elements than s */
903 p
= malloc(sizeof p
[0] * strlen(s
));
906 return svgtiny_OUT_OF_MEMORY
;
909 /* parse s and build path */
910 for (i
= 0; s
[i
]; i
++)
918 if (sscanf(s
, "%f %f %n", &x
, &y
, &n
) == 2) {
920 p
[i
++] = svgtiny_PATH_MOVE
;
922 p
[i
++] = svgtiny_PATH_LINE
;
931 p
[i
++] = svgtiny_PATH_CLOSE
;
935 return svgtiny_add_path(p
, i
, &state
);
940 * Parse a <text> or <tspan> element node.
943 svgtiny_code
svgtiny_parse_text(dom_element
*text
,
944 struct svgtiny_parse_state state
)
946 float x
, y
, width
, height
;
951 svgtiny_parse_position_attributes(text
, state
,
952 &x
, &y
, &width
, &height
);
953 svgtiny_parse_font_attributes(text
, &state
);
954 svgtiny_parse_transform_attributes(text
, &state
);
956 px
= state
.ctm
.a
* x
+ state
.ctm
.c
* y
+ state
.ctm
.e
;
957 py
= state
.ctm
.b
* x
+ state
.ctm
.d
* y
+ state
.ctm
.f
;
958 /* state.ctm.e = px - state.origin_x; */
959 /* state.ctm.f = py - state.origin_y; */
961 /*struct css_style style = state.style;
962 style.font_size.value.length.value *= state.ctm.a;*/
964 exc
= dom_node_get_first_child(text
, &child
);
965 if (exc
!= DOM_NO_ERR
)
966 return svgtiny_LIBDOM_ERROR
;
967 while (child
!= NULL
) {
969 dom_node_type nodetype
;
970 svgtiny_code code
= svgtiny_OK
;
972 exc
= dom_node_get_node_type(child
, &nodetype
);
973 if (exc
!= DOM_NO_ERR
) {
974 dom_node_unref(child
);
975 return svgtiny_LIBDOM_ERROR
;
977 if (nodetype
== DOM_ELEMENT_NODE
) {
978 dom_string
*nodename
;
979 exc
= dom_node_get_node_name(child
, &nodename
);
980 if (exc
!= DOM_NO_ERR
) {
981 dom_node_unref(child
);
982 return svgtiny_LIBDOM_ERROR
;
984 if (dom_string_caseless_isequal(nodename
,
985 state
.interned_tspan
))
986 code
= svgtiny_parse_text((dom_element
*)child
,
988 dom_string_unref(nodename
);
989 } else if (nodetype
== DOM_TEXT_NODE
) {
990 struct svgtiny_shape
*shape
= svgtiny_add_shape(&state
);
993 dom_node_unref(child
);
994 return svgtiny_OUT_OF_MEMORY
;
996 exc
= dom_text_get_whole_text(child
, &content
);
997 if (exc
!= DOM_NO_ERR
) {
998 dom_node_unref(child
);
999 return svgtiny_LIBDOM_ERROR
;
1001 shape
->text
= strndup(dom_string_data(content
),
1002 dom_string_length(content
));
1003 dom_string_unref(content
);
1006 state
.diagram
->shape_count
++;
1009 if (code
!= svgtiny_OK
) {
1010 dom_node_unref(child
);
1013 exc
= dom_node_get_next_sibling(child
, &next
);
1014 dom_node_unref(child
);
1015 if (exc
!= DOM_NO_ERR
)
1016 return svgtiny_LIBDOM_ERROR
;
1025 * Parse x, y, width, and height attributes, if present.
1028 void svgtiny_parse_position_attributes(const dom_element
*node
,
1029 const struct svgtiny_parse_state state
,
1030 float *x
, float *y
, float *width
, float *height
)
1037 *width
= state
.viewport_width
;
1038 *height
= state
.viewport_height
;
1040 exc
= dom_element_get_attribute(node
, state
.interned_x
, &attr
);
1041 if (exc
== DOM_NO_ERR
&& attr
!= NULL
) {
1042 *x
= svgtiny_parse_length(attr
, state
.viewport_width
, state
);
1043 dom_string_unref(attr
);
1046 exc
= dom_element_get_attribute(node
, state
.interned_y
, &attr
);
1047 if (exc
== DOM_NO_ERR
&& attr
!= NULL
) {
1048 *y
= svgtiny_parse_length(attr
, state
.viewport_height
, state
);
1049 dom_string_unref(attr
);
1052 exc
= dom_element_get_attribute(node
, state
.interned_width
, &attr
);
1053 if (exc
== DOM_NO_ERR
&& attr
!= NULL
) {
1054 *width
= svgtiny_parse_length(attr
, state
.viewport_width
,
1056 dom_string_unref(attr
);
1059 exc
= dom_element_get_attribute(node
, state
.interned_height
, &attr
);
1060 if (exc
== DOM_NO_ERR
&& attr
!= NULL
) {
1061 *height
= svgtiny_parse_length(attr
, state
.viewport_height
,
1063 dom_string_unref(attr
);
1069 * Parse a length as a number of pixels.
1072 static float _svgtiny_parse_length(const char *s
, int viewport_size
,
1073 const struct svgtiny_parse_state state
)
1075 int num_length
= strspn(s
, "0123456789+-.");
1076 const char *unit
= s
+ num_length
;
1077 float n
= atof((const char *) s
);
1078 float font_size
= 20; /*css_len2px(&state.style.font_size.value.length, 0);*/
1084 } else if (unit
[0] == '%') {
1085 return n
/ 100.0 * viewport_size
;
1086 } else if (unit
[0] == 'e' && unit
[1] == 'm') {
1087 return n
* font_size
;
1088 } else if (unit
[0] == 'e' && unit
[1] == 'x') {
1089 return n
/ 2.0 * font_size
;
1090 } else if (unit
[0] == 'p' && unit
[1] == 'x') {
1092 } else if (unit
[0] == 'p' && unit
[1] == 't') {
1094 } else if (unit
[0] == 'p' && unit
[1] == 'c') {
1096 } else if (unit
[0] == 'm' && unit
[1] == 'm') {
1097 return n
* 3.543307;
1098 } else if (unit
[0] == 'c' && unit
[1] == 'm') {
1099 return n
* 35.43307;
1100 } else if (unit
[0] == 'i' && unit
[1] == 'n') {
1107 float svgtiny_parse_length(dom_string
*s
, int viewport_size
,
1108 const struct svgtiny_parse_state state
)
1110 char *ss
= strndup(dom_string_data(s
), dom_string_length(s
));
1111 float ret
= _svgtiny_parse_length(ss
, viewport_size
, state
);
1117 * Parse paint attributes, if present.
1120 void svgtiny_parse_paint_attributes(const dom_element
*node
,
1121 struct svgtiny_parse_state
*state
)
1126 exc
= dom_element_get_attribute(node
, state
->interned_fill
, &attr
);
1127 if (exc
== DOM_NO_ERR
&& attr
!= NULL
) {
1128 svgtiny_parse_color(attr
, &state
->fill
, state
);
1129 dom_string_unref(attr
);
1132 exc
= dom_element_get_attribute(node
, state
->interned_stroke
, &attr
);
1133 if (exc
== DOM_NO_ERR
&& attr
!= NULL
) {
1134 svgtiny_parse_color(attr
, &state
->stroke
, state
);
1135 dom_string_unref(attr
);
1138 exc
= dom_element_get_attribute(node
, state
->interned_stroke_width
, &attr
);
1139 if (exc
== DOM_NO_ERR
&& attr
!= NULL
) {
1140 state
->stroke_width
= svgtiny_parse_length(attr
,
1141 state
->viewport_width
, *state
);
1142 dom_string_unref(attr
);
1145 exc
= dom_element_get_attribute(node
, state
->interned_style
, &attr
);
1146 if (exc
== DOM_NO_ERR
&& attr
!= NULL
) {
1147 char *style
= strndup(dom_string_data(attr
),
1148 dom_string_length(attr
));
1151 if ((s
= strstr(style
, "fill:"))) {
1155 value
= strndup(s
, strcspn(s
, "; "));
1156 _svgtiny_parse_color(value
, &state
->fill
, state
);
1159 if ((s
= strstr(style
, "stroke:"))) {
1163 value
= strndup(s
, strcspn(s
, "; "));
1164 _svgtiny_parse_color(value
, &state
->stroke
, state
);
1167 if ((s
= strstr(style
, "stroke-width:"))) {
1171 value
= strndup(s
, strcspn(s
, "; "));
1172 state
->stroke_width
= _svgtiny_parse_length(value
,
1173 state
->viewport_width
, *state
);
1177 dom_string_unref(attr
);
1186 static void _svgtiny_parse_color(const char *s
, svgtiny_colour
*c
,
1187 struct svgtiny_parse_state
*state
)
1189 unsigned int r
, g
, b
;
1191 size_t len
= strlen(s
);
1192 char *id
= 0, *rparen
;
1194 if (len
== 4 && s
[0] == '#') {
1195 if (sscanf(s
+ 1, "%1x%1x%1x", &r
, &g
, &b
) == 3)
1196 *c
= svgtiny_RGB(r
| r
<< 4, g
| g
<< 4, b
| b
<< 4);
1198 } else if (len
== 7 && s
[0] == '#') {
1199 if (sscanf(s
+ 1, "%2x%2x%2x", &r
, &g
, &b
) == 3)
1200 *c
= svgtiny_RGB(r
, g
, b
);
1202 } else if (10 <= len
&& s
[0] == 'r' && s
[1] == 'g' && s
[2] == 'b' &&
1203 s
[3] == '(' && s
[len
- 1] == ')') {
1204 if (sscanf(s
+ 4, "%u,%u,%u", &r
, &g
, &b
) == 3)
1205 *c
= svgtiny_RGB(r
, g
, b
);
1206 else if (sscanf(s
+ 4, "%f%%,%f%%,%f%%", &rf
, &gf
, &bf
) == 3) {
1210 *c
= svgtiny_RGB(r
, g
, b
);
1213 } else if (len
== 4 && strcmp(s
, "none") == 0) {
1214 *c
= svgtiny_TRANSPARENT
;
1216 } else if (5 < len
&& s
[0] == 'u' && s
[1] == 'r' && s
[2] == 'l' &&
1222 rparen
= strchr(id
, ')');
1225 svgtiny_find_gradient(id
, state
);
1227 fprintf(stderr
, "linear_gradient_stop_count %i\n",
1228 state
->linear_gradient_stop_count
);
1229 if (state
->linear_gradient_stop_count
== 0)
1230 *c
= svgtiny_TRANSPARENT
;
1231 else if (state
->linear_gradient_stop_count
== 1)
1232 *c
= state
->gradient_stop
[0].color
;
1234 *c
= svgtiny_LINEAR_GRADIENT
;
1238 const struct svgtiny_named_color
*named_color
;
1239 named_color
= svgtiny_color_lookup(s
, strlen(s
));
1241 *c
= named_color
->color
;
1245 void svgtiny_parse_color(dom_string
*s
, svgtiny_colour
*c
,
1246 struct svgtiny_parse_state
*state
)
1248 char *ss
= strndup(dom_string_data(s
), dom_string_length(s
));
1249 _svgtiny_parse_color(ss
, c
, state
);
1254 * Parse font attributes, if present.
1257 void svgtiny_parse_font_attributes(const dom_element
*node
,
1258 struct svgtiny_parse_state
*state
)
1260 /* TODO: Implement this, it never used to be */
1263 #ifdef WRITTEN_THIS_PROPERLY
1264 const xmlAttr
*attr
;
1268 for (attr
= node
->properties
; attr
; attr
= attr
->next
) {
1269 if (strcmp((const char *) attr
->name
, "font-size") == 0) {
1270 /*if (css_parse_length(
1271 (const char *) attr->children->content,
1272 &state->style.font_size.value.length,
1274 state->style.font_size.size =
1275 CSS_FONT_SIZE_LENGTH;
1284 * Parse transform attributes, if present.
1286 * http://www.w3.org/TR/SVG11/coords#TransformAttribute
1289 void svgtiny_parse_transform_attributes(dom_element
*node
,
1290 struct svgtiny_parse_state
*state
)
1296 exc
= dom_element_get_attribute(node
, state
->interned_transform
,
1298 if (exc
== DOM_NO_ERR
&& attr
!= NULL
) {
1299 transform
= strndup(dom_string_data(attr
),
1300 dom_string_length(attr
));
1301 svgtiny_parse_transform(transform
, &state
->ctm
.a
, &state
->ctm
.b
,
1302 &state
->ctm
.c
, &state
->ctm
.d
,
1303 &state
->ctm
.e
, &state
->ctm
.f
);
1305 dom_string_unref(attr
);
1311 * Parse a transform string.
1314 void svgtiny_parse_transform(char *s
, float *ma
, float *mb
,
1315 float *mc
, float *md
, float *me
, float *mf
)
1317 float a
, b
, c
, d
, e
, f
;
1318 float za
, zb
, zc
, zd
, ze
, zf
;
1323 for (i
= 0; s
[i
]; i
++)
1331 if (sscanf(s
, "matrix (%f %f %f %f %f %f) %n",
1332 &a
, &b
, &c
, &d
, &e
, &f
, &n
) == 6)
1334 else if (sscanf(s
, "translate (%f %f) %n",
1337 else if (sscanf(s
, "translate (%f) %n",
1340 else if (sscanf(s
, "scale (%f %f) %n",
1343 else if (sscanf(s
, "scale (%f) %n",
1346 else if (sscanf(s
, "rotate (%f %f %f) %n",
1347 &angle
, &x
, &y
, &n
) == 3) {
1348 angle
= angle
/ 180 * M_PI
;
1353 e
= -x
* cos(angle
) + y
* sin(angle
) + x
;
1354 f
= -x
* sin(angle
) - y
* cos(angle
) + y
;
1355 } else if (sscanf(s
, "rotate (%f) %n",
1357 angle
= angle
/ 180 * M_PI
;
1362 } else if (sscanf(s
, "skewX (%f) %n",
1364 angle
= angle
/ 180 * M_PI
;
1366 } else if (sscanf(s
, "skewY (%f) %n",
1368 angle
= angle
/ 180 * M_PI
;
1372 za
= *ma
* a
+ *mc
* b
;
1373 zb
= *mb
* a
+ *md
* b
;
1374 zc
= *ma
* c
+ *mc
* d
;
1375 zd
= *mb
* c
+ *md
* d
;
1376 ze
= *ma
* e
+ *mc
* f
+ *me
;
1377 zf
= *mb
* e
+ *md
* f
+ *mf
;
1390 * Add a path to the svgtiny_diagram.
1393 svgtiny_code
svgtiny_add_path(float *p
, unsigned int n
,
1394 struct svgtiny_parse_state
*state
)
1396 struct svgtiny_shape
*shape
;
1398 if (state
->fill
== svgtiny_LINEAR_GRADIENT
)
1399 return svgtiny_add_path_linear_gradient(p
, n
, state
);
1401 svgtiny_transform_path(p
, n
, state
);
1403 shape
= svgtiny_add_shape(state
);
1406 return svgtiny_OUT_OF_MEMORY
;
1409 shape
->path_length
= n
;
1410 state
->diagram
->shape_count
++;
1417 * Add a svgtiny_shape to the svgtiny_diagram.
1420 struct svgtiny_shape
*svgtiny_add_shape(struct svgtiny_parse_state
*state
)
1422 struct svgtiny_shape
*shape
= realloc(state
->diagram
->shape
,
1423 (state
->diagram
->shape_count
+ 1) *
1424 sizeof (state
->diagram
->shape
[0]));
1427 state
->diagram
->shape
= shape
;
1429 shape
+= state
->diagram
->shape_count
;
1431 shape
->path_length
= 0;
1433 shape
->fill
= state
->fill
;
1434 shape
->stroke
= state
->stroke
;
1435 shape
->stroke_width
= lroundf((float) state
->stroke_width
*
1436 (state
->ctm
.a
+ state
->ctm
.d
) / 2.0);
1437 if (0 < state
->stroke_width
&& shape
->stroke_width
== 0)
1438 shape
->stroke_width
= 1;
1445 * Apply the current transformation matrix to a path.
1448 void svgtiny_transform_path(float *p
, unsigned int n
,
1449 struct svgtiny_parse_state
*state
)
1453 for (j
= 0; j
!= n
; ) {
1454 unsigned int points
= 0;
1456 switch ((int) p
[j
]) {
1457 case svgtiny_PATH_MOVE
:
1458 case svgtiny_PATH_LINE
:
1461 case svgtiny_PATH_CLOSE
:
1464 case svgtiny_PATH_BEZIER
:
1471 for (k
= 0; k
!= points
; k
++) {
1472 float x0
= p
[j
], y0
= p
[j
+ 1];
1473 float x
= state
->ctm
.a
* x0
+ state
->ctm
.c
* y0
+
1475 float y
= state
->ctm
.b
* x0
+ state
->ctm
.d
* y0
+
1486 * Free all memory used by a diagram.
1489 void svgtiny_free(struct svgtiny_diagram
*svg
)
1494 for (i
= 0; i
!= svg
->shape_count
; i
++) {
1495 free(svg
->shape
[i
].path
);
1496 free(svg
->shape
[i
].text
);
1504 #ifndef HAVE_STRNDUP
1505 char *svgtiny_strndup(const char *s
, size_t n
)
1510 for (len
= 0; len
!= n
&& s
[len
]; len
++)
1513 s2
= malloc(len
+ 1);