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(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(dom_element
*node
,
49 struct svgtiny_parse_state
*state
);
50 static void svgtiny_parse_font_attributes(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
);
60 * Set the local externally-stored parts of a parse state.
61 * Call this in functions that made a new state on the stack.
62 * Doesn't make own copy of global state, such as the interned string list.
64 static void svgtiny_setup_state_local(struct svgtiny_parse_state
*state
)
66 if (state
->gradient_x1
!= NULL
) {
67 dom_string_ref(state
->gradient_x1
);
69 if (state
->gradient_y1
!= NULL
) {
70 dom_string_ref(state
->gradient_y1
);
72 if (state
->gradient_x2
!= NULL
) {
73 dom_string_ref(state
->gradient_x2
);
75 if (state
->gradient_y2
!= NULL
) {
76 dom_string_ref(state
->gradient_y2
);
81 * Cleanup the local externally-stored parts of a parse state.
82 * Call this in functions that made a new state on the stack.
83 * Doesn't cleanup global state, such as the interned string list.
85 static void svgtiny_cleanup_state_local(struct svgtiny_parse_state
*state
)
87 if (state
->gradient_x1
!= NULL
) {
88 dom_string_unref(state
->gradient_x1
);
89 state
->gradient_x1
= NULL
;
91 if (state
->gradient_y1
!= NULL
) {
92 dom_string_unref(state
->gradient_y1
);
93 state
->gradient_y1
= NULL
;
95 if (state
->gradient_x2
!= NULL
) {
96 dom_string_unref(state
->gradient_x2
);
97 state
->gradient_x2
= NULL
;
99 if (state
->gradient_y2
!= NULL
) {
100 dom_string_unref(state
->gradient_y2
);
101 state
->gradient_y2
= NULL
;
107 * Create a new svgtiny_diagram structure.
110 struct svgtiny_diagram
*svgtiny_create(void)
112 struct svgtiny_diagram
*diagram
;
114 diagram
= calloc(sizeof(*diagram
), 1);
123 static void ignore_msg(uint32_t severity
, void *ctx
, const char *msg
, ...)
131 * Parse a block of memory into a svgtiny_diagram.
134 svgtiny_code
svgtiny_parse(struct svgtiny_diagram
*diagram
,
135 const char *buffer
, size_t size
, const char *url
,
136 int viewport_width
, int viewport_height
)
138 dom_document
*document
;
140 dom_xml_parser
*parser
;
143 dom_string
*svg_name
;
144 lwc_string
*svg_name_lwc
;
145 struct svgtiny_parse_state state
;
146 float x
, y
, width
, height
;
155 state
.gradient_x1
= NULL
;
156 state
.gradient_y1
= NULL
;
157 state
.gradient_x2
= NULL
;
158 state
.gradient_y2
= NULL
;
160 parser
= dom_xml_parser_create(NULL
, NULL
,
161 ignore_msg
, NULL
, &document
);
164 return svgtiny_LIBDOM_ERROR
;
166 err
= dom_xml_parser_parse_chunk(parser
, (uint8_t *)buffer
, size
);
167 if (err
!= DOM_XML_OK
) {
168 dom_node_unref(document
);
169 dom_xml_parser_destroy(parser
);
170 return svgtiny_LIBDOM_ERROR
;
173 err
= dom_xml_parser_completed(parser
);
174 if (err
!= DOM_XML_OK
) {
175 dom_node_unref(document
);
176 dom_xml_parser_destroy(parser
);
177 return svgtiny_LIBDOM_ERROR
;
180 /* We're done parsing, drop the parser.
181 * We now own the document entirely.
183 dom_xml_parser_destroy(parser
);
185 /* find root <svg> element */
186 exc
= dom_document_get_document_element(document
, &svg
);
187 if (exc
!= DOM_NO_ERR
) {
188 dom_node_unref(document
);
189 return svgtiny_LIBDOM_ERROR
;
191 exc
= dom_node_get_node_name(svg
, &svg_name
);
192 if (exc
!= DOM_NO_ERR
) {
194 dom_node_unref(document
);
195 return svgtiny_LIBDOM_ERROR
;
197 if (lwc_intern_string("svg", 3 /* SLEN("svg") */,
198 &svg_name_lwc
) != lwc_error_ok
) {
199 dom_string_unref(svg_name
);
201 dom_node_unref(document
);
202 return svgtiny_LIBDOM_ERROR
;
204 if (!dom_string_caseless_lwc_isequal(svg_name
, svg_name_lwc
)) {
205 lwc_string_unref(svg_name_lwc
);
206 dom_string_unref(svg_name
);
208 dom_node_unref(document
);
209 return svgtiny_NOT_SVG
;
212 lwc_string_unref(svg_name_lwc
);
213 dom_string_unref(svg_name
);
215 /* get graphic dimensions */
216 memset(&state
, 0, sizeof(state
));
217 state
.diagram
= diagram
;
218 state
.document
= document
;
219 state
.viewport_width
= viewport_width
;
220 state
.viewport_height
= viewport_height
;
222 #define SVGTINY_STRING_ACTION2(s,n) \
223 if (dom_string_create_interned((const uint8_t *) #n, \
224 strlen(#n), &state.interned_##s) \
226 code = svgtiny_LIBDOM_ERROR; \
229 #include "svgtiny_strings.h"
230 #undef SVGTINY_STRING_ACTION2
232 svgtiny_parse_position_attributes(svg
, state
, &x
, &y
, &width
, &height
);
233 diagram
->width
= width
;
234 diagram
->height
= height
;
236 /* set up parsing state */
237 state
.viewport_width
= width
;
238 state
.viewport_height
= height
;
239 state
.ctm
.a
= 1; /*(float) viewport_width / (float) width;*/
242 state
.ctm
.d
= 1; /*(float) viewport_height / (float) height;*/
243 state
.ctm
.e
= 0; /*x;*/
244 state
.ctm
.f
= 0; /*y;*/
245 /*state.style = css_base_style;
246 state.style.font_size.value.length.value = option_font_size * 0.1;*/
247 state
.fill
= 0x000000;
248 state
.stroke
= svgtiny_TRANSPARENT
;
249 state
.stroke_width
= 1;
250 state
.linear_gradient_stop_count
= 0;
253 code
= svgtiny_parse_svg(svg
, state
);
256 dom_node_unref(document
);
259 svgtiny_cleanup_state_local(&state
);
260 #define SVGTINY_STRING_ACTION2(s,n) \
261 if (state.interned_##s != NULL) \
262 dom_string_unref(state.interned_##s);
263 #include "svgtiny_strings.h"
264 #undef SVGTINY_STRING_ACTION2
270 * Parse a <svg> or <g> element node.
273 svgtiny_code
svgtiny_parse_svg(dom_element
*svg
,
274 struct svgtiny_parse_state state
)
276 float x
, y
, width
, height
;
277 dom_string
*view_box
;
281 svgtiny_setup_state_local(&state
);
283 svgtiny_parse_position_attributes(svg
, state
, &x
, &y
, &width
, &height
);
284 svgtiny_parse_paint_attributes(svg
, &state
);
285 svgtiny_parse_font_attributes(svg
, &state
);
287 exc
= dom_element_get_attribute(svg
, state
.interned_viewBox
,
289 if (exc
!= DOM_NO_ERR
) {
290 svgtiny_cleanup_state_local(&state
);
291 return svgtiny_LIBDOM_ERROR
;
295 char *s
= strndup(dom_string_data(view_box
),
296 dom_string_byte_length(view_box
));
297 float min_x
, min_y
, vwidth
, vheight
;
298 if (sscanf(s
, "%f,%f,%f,%f",
299 &min_x
, &min_y
, &vwidth
, &vheight
) == 4 ||
300 sscanf(s
, "%f %f %f %f",
301 &min_x
, &min_y
, &vwidth
, &vheight
) == 4) {
302 state
.ctm
.a
= (float) state
.viewport_width
/ vwidth
;
303 state
.ctm
.d
= (float) state
.viewport_height
/ vheight
;
304 state
.ctm
.e
+= -min_x
* state
.ctm
.a
;
305 state
.ctm
.f
+= -min_y
* state
.ctm
.d
;
308 dom_string_unref(view_box
);
311 svgtiny_parse_transform_attributes(svg
, &state
);
313 exc
= dom_node_get_first_child(svg
, (dom_node
**) (void *) &child
);
314 if (exc
!= DOM_NO_ERR
) {
315 svgtiny_cleanup_state_local(&state
);
316 return svgtiny_LIBDOM_ERROR
;
318 while (child
!= NULL
) {
320 dom_node_type nodetype
;
321 svgtiny_code code
= svgtiny_OK
;
323 exc
= dom_node_get_node_type(child
, &nodetype
);
324 if (exc
!= DOM_NO_ERR
) {
325 dom_node_unref(child
);
326 return svgtiny_LIBDOM_ERROR
;
328 if (nodetype
== DOM_ELEMENT_NODE
) {
329 dom_string
*nodename
;
330 exc
= dom_node_get_node_name(child
, &nodename
);
331 if (exc
!= DOM_NO_ERR
) {
332 dom_node_unref(child
);
333 svgtiny_cleanup_state_local(&state
);
334 return svgtiny_LIBDOM_ERROR
;
336 if (dom_string_caseless_isequal(state
.interned_svg
,
338 code
= svgtiny_parse_svg(child
, state
);
339 else if (dom_string_caseless_isequal(state
.interned_g
,
341 code
= svgtiny_parse_svg(child
, state
);
342 else if (dom_string_caseless_isequal(state
.interned_a
,
344 code
= svgtiny_parse_svg(child
, state
);
345 else if (dom_string_caseless_isequal(state
.interned_path
,
347 code
= svgtiny_parse_path(child
, state
);
348 else if (dom_string_caseless_isequal(state
.interned_rect
,
350 code
= svgtiny_parse_rect(child
, state
);
351 else if (dom_string_caseless_isequal(state
.interned_circle
,
353 code
= svgtiny_parse_circle(child
, state
);
354 else if (dom_string_caseless_isequal(state
.interned_ellipse
,
356 code
= svgtiny_parse_ellipse(child
, state
);
357 else if (dom_string_caseless_isequal(state
.interned_line
,
359 code
= svgtiny_parse_line(child
, state
);
360 else if (dom_string_caseless_isequal(state
.interned_polyline
,
362 code
= svgtiny_parse_poly(child
, state
, false);
363 else if (dom_string_caseless_isequal(state
.interned_polygon
,
365 code
= svgtiny_parse_poly(child
, state
, true);
366 else if (dom_string_caseless_isequal(state
.interned_text
,
368 code
= svgtiny_parse_text(child
, state
);
369 dom_string_unref(nodename
);
371 if (code
!= svgtiny_OK
) {
372 dom_node_unref(child
);
373 svgtiny_cleanup_state_local(&state
);
376 exc
= dom_node_get_next_sibling(child
,
377 (dom_node
**) (void *) &next
);
378 dom_node_unref(child
);
379 if (exc
!= DOM_NO_ERR
) {
380 svgtiny_cleanup_state_local(&state
);
381 return svgtiny_LIBDOM_ERROR
;
386 svgtiny_cleanup_state_local(&state
);
393 * Parse a <path> element node.
395 * http://www.w3.org/TR/SVG11/paths#PathElement
398 svgtiny_code
svgtiny_parse_path(dom_element
*path
,
399 struct svgtiny_parse_state state
)
402 dom_string
*path_d_str
;
405 float *p
; /* path elemets */
406 unsigned int palloc
; /* number of path elements allocated */
408 float last_x
= 0, last_y
= 0;
409 float last_cubic_x
= 0, last_cubic_y
= 0;
410 float last_quad_x
= 0, last_quad_y
= 0;
411 float subpath_first_x
= 0, subpath_first_y
= 0;
413 svgtiny_setup_state_local(&state
);
415 svgtiny_parse_paint_attributes(path
, &state
);
416 svgtiny_parse_transform_attributes(path
, &state
);
418 /* read d attribute */
419 exc
= dom_element_get_attribute(path
, state
.interned_d
, &path_d_str
);
420 if (exc
!= DOM_NO_ERR
) {
421 state
.diagram
->error_line
= -1; /* path->line; */
422 state
.diagram
->error_message
= "path: error retrieving d attribute";
423 svgtiny_cleanup_state_local(&state
);
424 return svgtiny_SVG_ERROR
;
427 if (path_d_str
== NULL
) {
428 state
.diagram
->error_line
= -1; /* path->line; */
429 state
.diagram
->error_message
= "path: missing d attribute";
430 svgtiny_cleanup_state_local(&state
);
431 return svgtiny_SVG_ERROR
;
434 /* empty path is permitted it just disables the path */
435 palloc
= dom_string_byte_length(path_d_str
);
437 svgtiny_cleanup_state_local(&state
);
441 /* local copy of the path data allowing in-place modification */
442 s
= path_d
= strndup(dom_string_data(path_d_str
), palloc
);
443 dom_string_unref(path_d_str
);
445 svgtiny_cleanup_state_local(&state
);
446 return svgtiny_OUT_OF_MEMORY
;
449 /* ensure path element allocation is sensibly bounded */
452 } else if (palloc
> 64) {
456 /* allocate initial space for path elements */
457 p
= malloc(sizeof p
[0] * palloc
);
460 svgtiny_cleanup_state_local(&state
);
461 return svgtiny_OUT_OF_MEMORY
;
464 /* parse d and build path */
465 for (i
= 0; s
[i
]; i
++)
472 float x
, y
, x1
, y1
, x2
, y2
, rx
, ry
, rotation
, large_arc
, sweep
;
475 /* Ensure there is sufficient space for path elements */
476 if ((palloc
- i
) < 7) {
478 palloc
= (palloc
* 2) + (palloc
/ 2);
479 tp
= realloc(p
, sizeof p
[0] * palloc
);
483 svgtiny_cleanup_state_local(&state
);
484 return svgtiny_OUT_OF_MEMORY
;
490 /* moveto (M, m), lineto (L, l) (2 arguments) */
491 if (sscanf(s
, " %1[MmLl] %f %f %n", command
, &x
, &y
, &n
) == 3) {
492 /*LOG(("moveto or lineto"));*/
493 if (*command
== 'M' || *command
== 'm')
494 plot_command
= svgtiny_PATH_MOVE
;
496 plot_command
= svgtiny_PATH_LINE
;
498 p
[i
++] = plot_command
;
499 if ('a' <= *command
) {
503 if (plot_command
== svgtiny_PATH_MOVE
) {
507 p
[i
++] = last_cubic_x
= last_quad_x
= last_x
509 p
[i
++] = last_cubic_y
= last_quad_y
= last_y
512 plot_command
= svgtiny_PATH_LINE
;
513 } while (sscanf(s
, "%f %f %n", &x
, &y
, &n
) == 2);
515 /* closepath (Z, z) (no arguments) */
516 } else if (sscanf(s
, " %1[Zz] %n", command
, &n
) == 1) {
517 /*LOG(("closepath"));*/
518 p
[i
++] = svgtiny_PATH_CLOSE
;
520 last_cubic_x
= last_quad_x
= last_x
= subpath_first_x
;
521 last_cubic_y
= last_quad_y
= last_y
= subpath_first_y
;
523 /* horizontal lineto (H, h) (1 argument) */
524 } else if (sscanf(s
, " %1[Hh] %f %n", command
, &x
, &n
) == 2) {
525 /*LOG(("horizontal lineto"));*/
527 p
[i
++] = svgtiny_PATH_LINE
;
530 p
[i
++] = last_cubic_x
= last_quad_x
= last_x
532 p
[i
++] = last_cubic_y
= last_quad_y
= last_y
;
534 } while (sscanf(s
, "%f %n", &x
, &n
) == 1);
536 /* vertical lineto (V, v) (1 argument) */
537 } else if (sscanf(s
, " %1[Vv] %f %n", command
, &y
, &n
) == 2) {
538 /*LOG(("vertical lineto"));*/
540 p
[i
++] = svgtiny_PATH_LINE
;
543 p
[i
++] = last_cubic_x
= last_quad_x
= last_x
;
544 p
[i
++] = last_cubic_y
= last_quad_y
= last_y
547 } while (sscanf(s
, "%f %n", &x
, &n
) == 1);
549 /* curveto (C, c) (6 arguments) */
550 } else if (sscanf(s
, " %1[Cc] %f %f %f %f %f %f %n", command
,
551 &x1
, &y1
, &x2
, &y2
, &x
, &y
, &n
) == 7) {
552 /*LOG(("curveto"));*/
554 p
[i
++] = svgtiny_PATH_BEZIER
;
555 if (*command
== 'c') {
565 p
[i
++] = last_cubic_x
= x2
;
566 p
[i
++] = last_cubic_y
= y2
;
567 p
[i
++] = last_quad_x
= last_x
= x
;
568 p
[i
++] = last_quad_y
= last_y
= y
;
570 } while (sscanf(s
, "%f %f %f %f %f %f %n",
571 &x1
, &y1
, &x2
, &y2
, &x
, &y
, &n
) == 6);
573 /* shorthand/smooth curveto (S, s) (4 arguments) */
574 } else if (sscanf(s
, " %1[Ss] %f %f %f %f %n", command
,
575 &x2
, &y2
, &x
, &y
, &n
) == 5) {
576 /*LOG(("shorthand/smooth curveto"));*/
578 p
[i
++] = svgtiny_PATH_BEZIER
;
579 x1
= last_x
+ (last_x
- last_cubic_x
);
580 y1
= last_y
+ (last_y
- last_cubic_y
);
581 if (*command
== 's') {
589 p
[i
++] = last_cubic_x
= x2
;
590 p
[i
++] = last_cubic_y
= y2
;
591 p
[i
++] = last_quad_x
= last_x
= x
;
592 p
[i
++] = last_quad_y
= last_y
= y
;
594 } while (sscanf(s
, "%f %f %f %f %n",
595 &x2
, &y2
, &x
, &y
, &n
) == 4);
597 /* quadratic Bezier curveto (Q, q) (4 arguments) */
598 } else if (sscanf(s
, " %1[Qq] %f %f %f %f %n", command
,
599 &x1
, &y1
, &x
, &y
, &n
) == 5) {
600 /*LOG(("quadratic Bezier curveto"));*/
602 p
[i
++] = svgtiny_PATH_BEZIER
;
605 if (*command
== 'q') {
611 p
[i
++] = 1./3 * last_x
+ 2./3 * x1
;
612 p
[i
++] = 1./3 * last_y
+ 2./3 * y1
;
613 p
[i
++] = 2./3 * x1
+ 1./3 * x
;
614 p
[i
++] = 2./3 * y1
+ 1./3 * y
;
615 p
[i
++] = last_cubic_x
= last_x
= x
;
616 p
[i
++] = last_cubic_y
= last_y
= y
;
618 } while (sscanf(s
, "%f %f %f %f %n",
619 &x1
, &y1
, &x
, &y
, &n
) == 4);
621 /* shorthand/smooth quadratic Bezier curveto (T, t)
623 } else if (sscanf(s
, " %1[Tt] %f %f %n", command
,
625 /*LOG(("shorthand/smooth quadratic Bezier curveto"));*/
627 p
[i
++] = svgtiny_PATH_BEZIER
;
628 x1
= last_x
+ (last_x
- last_quad_x
);
629 y1
= last_y
+ (last_y
- last_quad_y
);
632 if (*command
== 't') {
638 p
[i
++] = 1./3 * last_x
+ 2./3 * x1
;
639 p
[i
++] = 1./3 * last_y
+ 2./3 * y1
;
640 p
[i
++] = 2./3 * x1
+ 1./3 * x
;
641 p
[i
++] = 2./3 * y1
+ 1./3 * y
;
642 p
[i
++] = last_cubic_x
= last_x
= x
;
643 p
[i
++] = last_cubic_y
= last_y
= y
;
645 } while (sscanf(s
, "%f %f %n",
648 /* elliptical arc (A, a) (7 arguments) */
649 } else if (sscanf(s
, " %1[Aa] %f %f %f %f %f %f %f %n", command
,
650 &rx
, &ry
, &rotation
, &large_arc
, &sweep
,
653 p
[i
++] = svgtiny_PATH_LINE
;
654 if (*command
== 'a') {
658 p
[i
++] = last_cubic_x
= last_quad_x
= last_x
660 p
[i
++] = last_cubic_y
= last_quad_y
= last_y
663 } while (sscanf(s
, "%f %f %f %f %f %f %f %n",
664 &rx
, &ry
, &rotation
, &large_arc
, &sweep
,
668 fprintf(stderr
, "parse failed at \"%s\"\n", s
);
676 /* no real segments in path */
678 svgtiny_cleanup_state_local(&state
);
682 /* resize path element array to not be over allocated */
686 /* try the resize, if it fails just continue to use the old
689 tp
= realloc(p
, sizeof p
[0] * i
);
695 err
= svgtiny_add_path(p
, i
, &state
);
697 svgtiny_cleanup_state_local(&state
);
704 * Parse a <rect> element node.
706 * http://www.w3.org/TR/SVG11/shapes#RectElement
709 svgtiny_code
svgtiny_parse_rect(dom_element
*rect
,
710 struct svgtiny_parse_state state
)
713 float x
, y
, width
, height
;
716 svgtiny_setup_state_local(&state
);
718 svgtiny_parse_position_attributes(rect
, state
,
719 &x
, &y
, &width
, &height
);
720 svgtiny_parse_paint_attributes(rect
, &state
);
721 svgtiny_parse_transform_attributes(rect
, &state
);
723 p
= malloc(13 * sizeof p
[0]);
725 svgtiny_cleanup_state_local(&state
);
726 return svgtiny_OUT_OF_MEMORY
;
729 p
[0] = svgtiny_PATH_MOVE
;
732 p
[3] = svgtiny_PATH_LINE
;
735 p
[6] = svgtiny_PATH_LINE
;
738 p
[9] = svgtiny_PATH_LINE
;
741 p
[12] = svgtiny_PATH_CLOSE
;
743 err
= svgtiny_add_path(p
, 13, &state
);
745 svgtiny_cleanup_state_local(&state
);
752 * Parse a <circle> element node.
755 svgtiny_code
svgtiny_parse_circle(dom_element
*circle
,
756 struct svgtiny_parse_state state
)
759 float x
= 0, y
= 0, r
= -1;
764 svgtiny_setup_state_local(&state
);
766 exc
= dom_element_get_attribute(circle
, state
.interned_cx
, &attr
);
767 if (exc
!= DOM_NO_ERR
) {
768 svgtiny_cleanup_state_local(&state
);
769 return svgtiny_LIBDOM_ERROR
;
772 x
= svgtiny_parse_length(attr
, state
.viewport_width
, state
);
774 dom_string_unref(attr
);
776 exc
= dom_element_get_attribute(circle
, state
.interned_cy
, &attr
);
777 if (exc
!= DOM_NO_ERR
) {
778 svgtiny_cleanup_state_local(&state
);
779 return svgtiny_LIBDOM_ERROR
;
782 y
= svgtiny_parse_length(attr
, state
.viewport_height
, state
);
784 dom_string_unref(attr
);
786 exc
= dom_element_get_attribute(circle
, state
.interned_r
, &attr
);
787 if (exc
!= DOM_NO_ERR
) {
788 svgtiny_cleanup_state_local(&state
);
789 return svgtiny_LIBDOM_ERROR
;
792 r
= svgtiny_parse_length(attr
, state
.viewport_width
, state
);
794 dom_string_unref(attr
);
796 svgtiny_parse_paint_attributes(circle
, &state
);
797 svgtiny_parse_transform_attributes(circle
, &state
);
800 state
.diagram
->error_line
= -1; /* circle->line; */
801 state
.diagram
->error_message
= "circle: r missing or negative";
802 svgtiny_cleanup_state_local(&state
);
803 return svgtiny_SVG_ERROR
;
806 svgtiny_cleanup_state_local(&state
);
810 p
= malloc(32 * sizeof p
[0]);
812 svgtiny_cleanup_state_local(&state
);
813 return svgtiny_OUT_OF_MEMORY
;
816 p
[0] = svgtiny_PATH_MOVE
;
819 p
[3] = svgtiny_PATH_BEZIER
;
821 p
[5] = y
+ r
* KAPPA
;
822 p
[6] = x
+ r
* KAPPA
;
826 p
[10] = svgtiny_PATH_BEZIER
;
827 p
[11] = x
- r
* KAPPA
;
830 p
[14] = y
+ r
* KAPPA
;
833 p
[17] = svgtiny_PATH_BEZIER
;
835 p
[19] = y
- r
* KAPPA
;
836 p
[20] = x
- r
* KAPPA
;
840 p
[24] = svgtiny_PATH_BEZIER
;
841 p
[25] = x
+ r
* KAPPA
;
844 p
[28] = y
- r
* KAPPA
;
847 p
[31] = svgtiny_PATH_CLOSE
;
849 err
= svgtiny_add_path(p
, 32, &state
);
851 svgtiny_cleanup_state_local(&state
);
858 * Parse an <ellipse> element node.
861 svgtiny_code
svgtiny_parse_ellipse(dom_element
*ellipse
,
862 struct svgtiny_parse_state state
)
865 float x
= 0, y
= 0, rx
= -1, ry
= -1;
870 svgtiny_setup_state_local(&state
);
872 exc
= dom_element_get_attribute(ellipse
, state
.interned_cx
, &attr
);
873 if (exc
!= DOM_NO_ERR
) {
874 svgtiny_cleanup_state_local(&state
);
875 return svgtiny_LIBDOM_ERROR
;
878 x
= svgtiny_parse_length(attr
, state
.viewport_width
, state
);
880 dom_string_unref(attr
);
882 exc
= dom_element_get_attribute(ellipse
, state
.interned_cy
, &attr
);
883 if (exc
!= DOM_NO_ERR
) {
884 svgtiny_cleanup_state_local(&state
);
885 return svgtiny_LIBDOM_ERROR
;
888 y
= svgtiny_parse_length(attr
, state
.viewport_height
, state
);
890 dom_string_unref(attr
);
892 exc
= dom_element_get_attribute(ellipse
, state
.interned_rx
, &attr
);
893 if (exc
!= DOM_NO_ERR
) {
894 svgtiny_cleanup_state_local(&state
);
895 return svgtiny_LIBDOM_ERROR
;
898 rx
= svgtiny_parse_length(attr
, state
.viewport_width
, state
);
900 dom_string_unref(attr
);
902 exc
= dom_element_get_attribute(ellipse
, state
.interned_ry
, &attr
);
903 if (exc
!= DOM_NO_ERR
) {
904 svgtiny_cleanup_state_local(&state
);
905 return svgtiny_LIBDOM_ERROR
;
908 ry
= svgtiny_parse_length(attr
, state
.viewport_width
, state
);
910 dom_string_unref(attr
);
912 svgtiny_parse_paint_attributes(ellipse
, &state
);
913 svgtiny_parse_transform_attributes(ellipse
, &state
);
915 if (rx
< 0 || ry
< 0) {
916 state
.diagram
->error_line
= -1; /* ellipse->line; */
917 state
.diagram
->error_message
= "ellipse: rx or ry missing "
919 svgtiny_cleanup_state_local(&state
);
920 return svgtiny_SVG_ERROR
;
922 if (rx
== 0 || ry
== 0) {
923 svgtiny_cleanup_state_local(&state
);
927 p
= malloc(32 * sizeof p
[0]);
929 svgtiny_cleanup_state_local(&state
);
930 return svgtiny_OUT_OF_MEMORY
;
933 p
[0] = svgtiny_PATH_MOVE
;
936 p
[3] = svgtiny_PATH_BEZIER
;
938 p
[5] = y
+ ry
* KAPPA
;
939 p
[6] = x
+ rx
* KAPPA
;
943 p
[10] = svgtiny_PATH_BEZIER
;
944 p
[11] = x
- rx
* KAPPA
;
947 p
[14] = y
+ ry
* KAPPA
;
950 p
[17] = svgtiny_PATH_BEZIER
;
952 p
[19] = y
- ry
* KAPPA
;
953 p
[20] = x
- rx
* KAPPA
;
957 p
[24] = svgtiny_PATH_BEZIER
;
958 p
[25] = x
+ rx
* KAPPA
;
961 p
[28] = y
- ry
* KAPPA
;
964 p
[31] = svgtiny_PATH_CLOSE
;
966 err
= svgtiny_add_path(p
, 32, &state
);
968 svgtiny_cleanup_state_local(&state
);
975 * Parse a <line> element node.
978 svgtiny_code
svgtiny_parse_line(dom_element
*line
,
979 struct svgtiny_parse_state state
)
982 float x1
= 0, y1
= 0, x2
= 0, y2
= 0;
987 svgtiny_setup_state_local(&state
);
989 exc
= dom_element_get_attribute(line
, state
.interned_x1
, &attr
);
990 if (exc
!= DOM_NO_ERR
) {
991 svgtiny_cleanup_state_local(&state
);
992 return svgtiny_LIBDOM_ERROR
;
995 x1
= svgtiny_parse_length(attr
, state
.viewport_width
, state
);
997 dom_string_unref(attr
);
999 exc
= dom_element_get_attribute(line
, state
.interned_y1
, &attr
);
1000 if (exc
!= DOM_NO_ERR
) {
1001 svgtiny_cleanup_state_local(&state
);
1002 return svgtiny_LIBDOM_ERROR
;
1005 y1
= svgtiny_parse_length(attr
, state
.viewport_height
, state
);
1007 dom_string_unref(attr
);
1009 exc
= dom_element_get_attribute(line
, state
.interned_x2
, &attr
);
1010 if (exc
!= DOM_NO_ERR
) {
1011 svgtiny_cleanup_state_local(&state
);
1012 return svgtiny_LIBDOM_ERROR
;
1015 x2
= svgtiny_parse_length(attr
, state
.viewport_width
, state
);
1017 dom_string_unref(attr
);
1019 exc
= dom_element_get_attribute(line
, state
.interned_y2
, &attr
);
1020 if (exc
!= DOM_NO_ERR
) {
1021 svgtiny_cleanup_state_local(&state
);
1022 return svgtiny_LIBDOM_ERROR
;
1025 y2
= svgtiny_parse_length(attr
, state
.viewport_height
, state
);
1027 dom_string_unref(attr
);
1029 svgtiny_parse_paint_attributes(line
, &state
);
1030 svgtiny_parse_transform_attributes(line
, &state
);
1032 p
= malloc(7 * sizeof p
[0]);
1034 svgtiny_cleanup_state_local(&state
);
1035 return svgtiny_OUT_OF_MEMORY
;
1038 p
[0] = svgtiny_PATH_MOVE
;
1041 p
[3] = svgtiny_PATH_LINE
;
1044 p
[6] = svgtiny_PATH_CLOSE
;
1046 err
= svgtiny_add_path(p
, 7, &state
);
1048 svgtiny_cleanup_state_local(&state
);
1055 * Parse a <polyline> or <polygon> element node.
1057 * http://www.w3.org/TR/SVG11/shapes#PolylineElement
1058 * http://www.w3.org/TR/SVG11/shapes#PolygonElement
1061 svgtiny_code
svgtiny_parse_poly(dom_element
*poly
,
1062 struct svgtiny_parse_state state
, bool polygon
)
1065 dom_string
*points_str
;
1071 svgtiny_setup_state_local(&state
);
1073 svgtiny_parse_paint_attributes(poly
, &state
);
1074 svgtiny_parse_transform_attributes(poly
, &state
);
1076 exc
= dom_element_get_attribute(poly
, state
.interned_points
,
1078 if (exc
!= DOM_NO_ERR
) {
1079 svgtiny_cleanup_state_local(&state
);
1080 return svgtiny_LIBDOM_ERROR
;
1083 if (points_str
== NULL
) {
1084 state
.diagram
->error_line
= -1; /* poly->line; */
1085 state
.diagram
->error_message
=
1086 "polyline/polygon: missing points attribute";
1087 svgtiny_cleanup_state_local(&state
);
1088 return svgtiny_SVG_ERROR
;
1091 s
= points
= strndup(dom_string_data(points_str
),
1092 dom_string_byte_length(points_str
));
1093 dom_string_unref(points_str
);
1094 /* read points attribute */
1096 svgtiny_cleanup_state_local(&state
);
1097 return svgtiny_OUT_OF_MEMORY
;
1099 /* allocate space for path: it will never have more elements than s */
1100 p
= malloc(sizeof p
[0] * strlen(s
));
1103 svgtiny_cleanup_state_local(&state
);
1104 return svgtiny_OUT_OF_MEMORY
;
1107 /* parse s and build path */
1108 for (i
= 0; s
[i
]; i
++)
1116 if (sscanf(s
, "%f %f %n", &x
, &y
, &n
) == 2) {
1118 p
[i
++] = svgtiny_PATH_MOVE
;
1120 p
[i
++] = svgtiny_PATH_LINE
;
1129 p
[i
++] = svgtiny_PATH_CLOSE
;
1133 err
= svgtiny_add_path(p
, i
, &state
);
1135 svgtiny_cleanup_state_local(&state
);
1142 * Parse a <text> or <tspan> element node.
1145 svgtiny_code
svgtiny_parse_text(dom_element
*text
,
1146 struct svgtiny_parse_state state
)
1148 float x
, y
, width
, height
;
1153 svgtiny_setup_state_local(&state
);
1155 svgtiny_parse_position_attributes(text
, state
,
1156 &x
, &y
, &width
, &height
);
1157 svgtiny_parse_font_attributes(text
, &state
);
1158 svgtiny_parse_transform_attributes(text
, &state
);
1160 px
= state
.ctm
.a
* x
+ state
.ctm
.c
* y
+ state
.ctm
.e
;
1161 py
= state
.ctm
.b
* x
+ state
.ctm
.d
* y
+ state
.ctm
.f
;
1162 /* state.ctm.e = px - state.origin_x; */
1163 /* state.ctm.f = py - state.origin_y; */
1165 /*struct css_style style = state.style;
1166 style.font_size.value.length.value *= state.ctm.a;*/
1168 exc
= dom_node_get_first_child(text
, &child
);
1169 if (exc
!= DOM_NO_ERR
) {
1170 return svgtiny_LIBDOM_ERROR
;
1171 svgtiny_cleanup_state_local(&state
);
1173 while (child
!= NULL
) {
1175 dom_node_type nodetype
;
1176 svgtiny_code code
= svgtiny_OK
;
1178 exc
= dom_node_get_node_type(child
, &nodetype
);
1179 if (exc
!= DOM_NO_ERR
) {
1180 dom_node_unref(child
);
1181 svgtiny_cleanup_state_local(&state
);
1182 return svgtiny_LIBDOM_ERROR
;
1184 if (nodetype
== DOM_ELEMENT_NODE
) {
1185 dom_string
*nodename
;
1186 exc
= dom_node_get_node_name(child
, &nodename
);
1187 if (exc
!= DOM_NO_ERR
) {
1188 dom_node_unref(child
);
1189 svgtiny_cleanup_state_local(&state
);
1190 return svgtiny_LIBDOM_ERROR
;
1192 if (dom_string_caseless_isequal(nodename
,
1193 state
.interned_tspan
))
1194 code
= svgtiny_parse_text((dom_element
*)child
,
1196 dom_string_unref(nodename
);
1197 } else if (nodetype
== DOM_TEXT_NODE
) {
1198 struct svgtiny_shape
*shape
= svgtiny_add_shape(&state
);
1199 dom_string
*content
;
1200 if (shape
== NULL
) {
1201 dom_node_unref(child
);
1202 svgtiny_cleanup_state_local(&state
);
1203 return svgtiny_OUT_OF_MEMORY
;
1205 exc
= dom_text_get_whole_text(child
, &content
);
1206 if (exc
!= DOM_NO_ERR
) {
1207 dom_node_unref(child
);
1208 svgtiny_cleanup_state_local(&state
);
1209 return svgtiny_LIBDOM_ERROR
;
1211 if (content
!= NULL
) {
1212 shape
->text
= strndup(dom_string_data(content
),
1213 dom_string_byte_length(content
));
1214 dom_string_unref(content
);
1216 shape
->text
= strdup("");
1220 state
.diagram
->shape_count
++;
1223 if (code
!= svgtiny_OK
) {
1224 dom_node_unref(child
);
1225 svgtiny_cleanup_state_local(&state
);
1228 exc
= dom_node_get_next_sibling(child
, &next
);
1229 dom_node_unref(child
);
1230 if (exc
!= DOM_NO_ERR
) {
1231 svgtiny_cleanup_state_local(&state
);
1232 return svgtiny_LIBDOM_ERROR
;
1237 svgtiny_cleanup_state_local(&state
);
1244 * Parse x, y, width, and height attributes, if present.
1247 void svgtiny_parse_position_attributes(dom_element
*node
,
1248 const struct svgtiny_parse_state state
,
1249 float *x
, float *y
, float *width
, float *height
)
1256 *width
= state
.viewport_width
;
1257 *height
= state
.viewport_height
;
1259 exc
= dom_element_get_attribute(node
, state
.interned_x
, &attr
);
1260 if (exc
== DOM_NO_ERR
&& attr
!= NULL
) {
1261 *x
= svgtiny_parse_length(attr
, state
.viewport_width
, state
);
1262 dom_string_unref(attr
);
1265 exc
= dom_element_get_attribute(node
, state
.interned_y
, &attr
);
1266 if (exc
== DOM_NO_ERR
&& attr
!= NULL
) {
1267 *y
= svgtiny_parse_length(attr
, state
.viewport_height
, state
);
1268 dom_string_unref(attr
);
1271 exc
= dom_element_get_attribute(node
, state
.interned_width
, &attr
);
1272 if (exc
== DOM_NO_ERR
&& attr
!= NULL
) {
1273 *width
= svgtiny_parse_length(attr
, state
.viewport_width
,
1275 dom_string_unref(attr
);
1278 exc
= dom_element_get_attribute(node
, state
.interned_height
, &attr
);
1279 if (exc
== DOM_NO_ERR
&& attr
!= NULL
) {
1280 *height
= svgtiny_parse_length(attr
, state
.viewport_height
,
1282 dom_string_unref(attr
);
1288 * Parse a length as a number of pixels.
1291 static float _svgtiny_parse_length(const char *s
, int viewport_size
,
1292 const struct svgtiny_parse_state state
)
1294 int num_length
= strspn(s
, "0123456789+-.");
1295 const char *unit
= s
+ num_length
;
1296 float n
= atof((const char *) s
);
1297 float font_size
= 20; /*css_len2px(&state.style.font_size.value.length, 0);*/
1303 } else if (unit
[0] == '%') {
1304 return n
/ 100.0 * viewport_size
;
1305 } else if (unit
[0] == 'e' && unit
[1] == 'm') {
1306 return n
* font_size
;
1307 } else if (unit
[0] == 'e' && unit
[1] == 'x') {
1308 return n
/ 2.0 * font_size
;
1309 } else if (unit
[0] == 'p' && unit
[1] == 'x') {
1311 } else if (unit
[0] == 'p' && unit
[1] == 't') {
1313 } else if (unit
[0] == 'p' && unit
[1] == 'c') {
1315 } else if (unit
[0] == 'm' && unit
[1] == 'm') {
1316 return n
* 3.543307;
1317 } else if (unit
[0] == 'c' && unit
[1] == 'm') {
1318 return n
* 35.43307;
1319 } else if (unit
[0] == 'i' && unit
[1] == 'n') {
1326 float svgtiny_parse_length(dom_string
*s
, int viewport_size
,
1327 const struct svgtiny_parse_state state
)
1329 char *ss
= strndup(dom_string_data(s
), dom_string_byte_length(s
));
1330 float ret
= _svgtiny_parse_length(ss
, viewport_size
, state
);
1336 * Parse paint attributes, if present.
1339 void svgtiny_parse_paint_attributes(dom_element
*node
,
1340 struct svgtiny_parse_state
*state
)
1345 exc
= dom_element_get_attribute(node
, state
->interned_fill
, &attr
);
1346 if (exc
== DOM_NO_ERR
&& attr
!= NULL
) {
1347 svgtiny_parse_color(attr
, &state
->fill
, state
);
1348 dom_string_unref(attr
);
1351 exc
= dom_element_get_attribute(node
, state
->interned_stroke
, &attr
);
1352 if (exc
== DOM_NO_ERR
&& attr
!= NULL
) {
1353 svgtiny_parse_color(attr
, &state
->stroke
, state
);
1354 dom_string_unref(attr
);
1357 exc
= dom_element_get_attribute(node
, state
->interned_stroke_width
, &attr
);
1358 if (exc
== DOM_NO_ERR
&& attr
!= NULL
) {
1359 state
->stroke_width
= svgtiny_parse_length(attr
,
1360 state
->viewport_width
, *state
);
1361 dom_string_unref(attr
);
1364 exc
= dom_element_get_attribute(node
, state
->interned_style
, &attr
);
1365 if (exc
== DOM_NO_ERR
&& attr
!= NULL
) {
1366 char *style
= strndup(dom_string_data(attr
),
1367 dom_string_byte_length(attr
));
1370 if ((s
= strstr(style
, "fill:"))) {
1374 value
= strndup(s
, strcspn(s
, "; "));
1375 _svgtiny_parse_color(value
, &state
->fill
, state
);
1378 if ((s
= strstr(style
, "stroke:"))) {
1382 value
= strndup(s
, strcspn(s
, "; "));
1383 _svgtiny_parse_color(value
, &state
->stroke
, state
);
1386 if ((s
= strstr(style
, "stroke-width:"))) {
1390 value
= strndup(s
, strcspn(s
, "; "));
1391 state
->stroke_width
= _svgtiny_parse_length(value
,
1392 state
->viewport_width
, *state
);
1396 dom_string_unref(attr
);
1405 static void _svgtiny_parse_color(const char *s
, svgtiny_colour
*c
,
1406 struct svgtiny_parse_state
*state
)
1408 unsigned int r
, g
, b
;
1410 size_t len
= strlen(s
);
1411 char *id
= 0, *rparen
;
1413 if (len
== 4 && s
[0] == '#') {
1414 if (sscanf(s
+ 1, "%1x%1x%1x", &r
, &g
, &b
) == 3)
1415 *c
= svgtiny_RGB(r
| r
<< 4, g
| g
<< 4, b
| b
<< 4);
1417 } else if (len
== 7 && s
[0] == '#') {
1418 if (sscanf(s
+ 1, "%2x%2x%2x", &r
, &g
, &b
) == 3)
1419 *c
= svgtiny_RGB(r
, g
, b
);
1421 } else if (10 <= len
&& s
[0] == 'r' && s
[1] == 'g' && s
[2] == 'b' &&
1422 s
[3] == '(' && s
[len
- 1] == ')') {
1423 if (sscanf(s
+ 4, "%u,%u,%u", &r
, &g
, &b
) == 3)
1424 *c
= svgtiny_RGB(r
, g
, b
);
1425 else if (sscanf(s
+ 4, "%f%%,%f%%,%f%%", &rf
, &gf
, &bf
) == 3) {
1429 *c
= svgtiny_RGB(r
, g
, b
);
1432 } else if (len
== 4 && strcmp(s
, "none") == 0) {
1433 *c
= svgtiny_TRANSPARENT
;
1435 } else if (5 < len
&& s
[0] == 'u' && s
[1] == 'r' && s
[2] == 'l' &&
1441 rparen
= strchr(id
, ')');
1444 svgtiny_find_gradient(id
, state
);
1446 if (state
->linear_gradient_stop_count
== 0)
1447 *c
= svgtiny_TRANSPARENT
;
1448 else if (state
->linear_gradient_stop_count
== 1)
1449 *c
= state
->gradient_stop
[0].color
;
1451 *c
= svgtiny_LINEAR_GRADIENT
;
1455 const struct svgtiny_named_color
*named_color
;
1456 named_color
= svgtiny_color_lookup(s
, strlen(s
));
1458 *c
= named_color
->color
;
1462 void svgtiny_parse_color(dom_string
*s
, svgtiny_colour
*c
,
1463 struct svgtiny_parse_state
*state
)
1465 char *ss
= strndup(dom_string_data(s
), dom_string_byte_length(s
));
1466 _svgtiny_parse_color(ss
, c
, state
);
1471 * Parse font attributes, if present.
1474 void svgtiny_parse_font_attributes(dom_element
*node
,
1475 struct svgtiny_parse_state
*state
)
1477 /* TODO: Implement this, it never used to be */
1480 #ifdef WRITTEN_THIS_PROPERLY
1481 const xmlAttr
*attr
;
1485 for (attr
= node
->properties
; attr
; attr
= attr
->next
) {
1486 if (strcmp((const char *) attr
->name
, "font-size") == 0) {
1487 /*if (css_parse_length(
1488 (const char *) attr->children->content,
1489 &state->style.font_size.value.length,
1491 state->style.font_size.size =
1492 CSS_FONT_SIZE_LENGTH;
1501 * Parse transform attributes, if present.
1503 * http://www.w3.org/TR/SVG11/coords#TransformAttribute
1506 void svgtiny_parse_transform_attributes(dom_element
*node
,
1507 struct svgtiny_parse_state
*state
)
1513 exc
= dom_element_get_attribute(node
, state
->interned_transform
,
1515 if (exc
== DOM_NO_ERR
&& attr
!= NULL
) {
1516 transform
= strndup(dom_string_data(attr
),
1517 dom_string_byte_length(attr
));
1518 svgtiny_parse_transform(transform
, &state
->ctm
.a
, &state
->ctm
.b
,
1519 &state
->ctm
.c
, &state
->ctm
.d
,
1520 &state
->ctm
.e
, &state
->ctm
.f
);
1522 dom_string_unref(attr
);
1528 * Parse a transform string.
1531 void svgtiny_parse_transform(char *s
, float *ma
, float *mb
,
1532 float *mc
, float *md
, float *me
, float *mf
)
1534 float a
, b
, c
, d
, e
, f
;
1535 float za
, zb
, zc
, zd
, ze
, zf
;
1540 for (i
= 0; s
[i
]; i
++)
1548 if ((sscanf(s
, " matrix (%f %f %f %f %f %f ) %n",
1549 &a
, &b
, &c
, &d
, &e
, &f
, &n
) == 6) && (n
> 0))
1551 else if ((sscanf(s
, " translate (%f %f ) %n",
1552 &e
, &f
, &n
) == 2) && (n
> 0))
1554 else if ((sscanf(s
, " translate (%f ) %n",
1555 &e
, &n
) == 1) && (n
> 0))
1557 else if ((sscanf(s
, " scale (%f %f ) %n",
1558 &a
, &d
, &n
) == 2) && (n
> 0))
1560 else if ((sscanf(s
, " scale (%f ) %n",
1561 &a
, &n
) == 1) && (n
> 0))
1563 else if ((sscanf(s
, " rotate (%f %f %f ) %n",
1564 &angle
, &x
, &y
, &n
) == 3) && (n
> 0)) {
1565 angle
= angle
/ 180 * M_PI
;
1570 e
= -x
* cos(angle
) + y
* sin(angle
) + x
;
1571 f
= -x
* sin(angle
) - y
* cos(angle
) + y
;
1572 } else if ((sscanf(s
, " rotate (%f ) %n",
1573 &angle
, &n
) == 1) && (n
> 0)) {
1574 angle
= angle
/ 180 * M_PI
;
1579 } else if ((sscanf(s
, " skewX (%f ) %n",
1580 &angle
, &n
) == 1) && (n
> 0)) {
1581 angle
= angle
/ 180 * M_PI
;
1583 } else if ((sscanf(s
, " skewY (%f ) %n",
1584 &angle
, &n
) == 1) && (n
> 0)) {
1585 angle
= angle
/ 180 * M_PI
;
1589 za
= *ma
* a
+ *mc
* b
;
1590 zb
= *mb
* a
+ *md
* b
;
1591 zc
= *ma
* c
+ *mc
* d
;
1592 zd
= *mb
* c
+ *md
* d
;
1593 ze
= *ma
* e
+ *mc
* f
+ *me
;
1594 zf
= *mb
* e
+ *md
* f
+ *mf
;
1607 * Add a path to the svgtiny_diagram.
1610 svgtiny_code
svgtiny_add_path(float *p
, unsigned int n
,
1611 struct svgtiny_parse_state
*state
)
1613 struct svgtiny_shape
*shape
;
1615 if (state
->fill
== svgtiny_LINEAR_GRADIENT
)
1616 return svgtiny_add_path_linear_gradient(p
, n
, state
);
1618 svgtiny_transform_path(p
, n
, state
);
1620 shape
= svgtiny_add_shape(state
);
1623 return svgtiny_OUT_OF_MEMORY
;
1626 shape
->path_length
= n
;
1627 state
->diagram
->shape_count
++;
1634 * Add a svgtiny_shape to the svgtiny_diagram.
1637 struct svgtiny_shape
*svgtiny_add_shape(struct svgtiny_parse_state
*state
)
1639 struct svgtiny_shape
*shape
= realloc(state
->diagram
->shape
,
1640 (state
->diagram
->shape_count
+ 1) *
1641 sizeof (state
->diagram
->shape
[0]));
1644 state
->diagram
->shape
= shape
;
1646 shape
+= state
->diagram
->shape_count
;
1648 shape
->path_length
= 0;
1650 shape
->fill
= state
->fill
;
1651 shape
->stroke
= state
->stroke
;
1652 shape
->stroke_width
= lroundf((float) state
->stroke_width
*
1653 (state
->ctm
.a
+ state
->ctm
.d
) / 2.0);
1654 if (0 < state
->stroke_width
&& shape
->stroke_width
== 0)
1655 shape
->stroke_width
= 1;
1662 * Apply the current transformation matrix to a path.
1665 void svgtiny_transform_path(float *p
, unsigned int n
,
1666 struct svgtiny_parse_state
*state
)
1670 for (j
= 0; j
!= n
; ) {
1671 unsigned int points
= 0;
1673 switch ((int) p
[j
]) {
1674 case svgtiny_PATH_MOVE
:
1675 case svgtiny_PATH_LINE
:
1678 case svgtiny_PATH_CLOSE
:
1681 case svgtiny_PATH_BEZIER
:
1688 for (k
= 0; k
!= points
; k
++) {
1689 float x0
= p
[j
], y0
= p
[j
+ 1];
1690 float x
= state
->ctm
.a
* x0
+ state
->ctm
.c
* y0
+
1692 float y
= state
->ctm
.b
* x0
+ state
->ctm
.d
* y0
+
1703 * Free all memory used by a diagram.
1706 void svgtiny_free(struct svgtiny_diagram
*svg
)
1711 for (i
= 0; i
!= svg
->shape_count
; i
++) {
1712 free(svg
->shape
[i
].path
);
1713 free(svg
->shape
[i
].text
);
1721 #ifndef HAVE_STRNDUP
1722 char *svgtiny_strndup(const char *s
, size_t n
)
1727 for (len
= 0; len
!= n
&& s
[len
]; len
++)
1730 s2
= malloc(len
+ 1);