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
;
407 float last_x
= 0, last_y
= 0;
408 float last_cubic_x
= 0, last_cubic_y
= 0;
409 float last_quad_x
= 0, last_quad_y
= 0;
410 float subpath_first_x
= 0, subpath_first_y
= 0;
412 svgtiny_setup_state_local(&state
);
414 svgtiny_parse_paint_attributes(path
, &state
);
415 svgtiny_parse_transform_attributes(path
, &state
);
417 /* read d attribute */
418 exc
= dom_element_get_attribute(path
, state
.interned_d
, &path_d_str
);
419 if (exc
!= DOM_NO_ERR
) {
420 state
.diagram
->error_line
= -1; /* path->line; */
421 state
.diagram
->error_message
= "path: error retrieving d attribute";
422 svgtiny_cleanup_state_local(&state
);
423 return svgtiny_SVG_ERROR
;
426 if (path_d_str
== NULL
) {
427 state
.diagram
->error_line
= -1; /* path->line; */
428 state
.diagram
->error_message
= "path: missing d attribute";
429 svgtiny_cleanup_state_local(&state
);
430 return svgtiny_SVG_ERROR
;
433 s
= path_d
= strndup(dom_string_data(path_d_str
),
434 dom_string_byte_length(path_d_str
));
435 dom_string_unref(path_d_str
);
437 svgtiny_cleanup_state_local(&state
);
438 return svgtiny_OUT_OF_MEMORY
;
440 /* allocate space for path: it will never have more elements than d */
441 p
= malloc(sizeof p
[0] * strlen(s
));
444 svgtiny_cleanup_state_local(&state
);
445 return svgtiny_OUT_OF_MEMORY
;
448 /* parse d and build path */
449 for (i
= 0; s
[i
]; i
++)
456 float x
, y
, x1
, y1
, x2
, y2
, rx
, ry
, rotation
, large_arc
, sweep
;
459 /* moveto (M, m), lineto (L, l) (2 arguments) */
460 if (sscanf(s
, " %1[MmLl] %f %f %n", command
, &x
, &y
, &n
) == 3) {
461 /*LOG(("moveto or lineto"));*/
462 if (*command
== 'M' || *command
== 'm')
463 plot_command
= svgtiny_PATH_MOVE
;
465 plot_command
= svgtiny_PATH_LINE
;
467 p
[i
++] = plot_command
;
468 if ('a' <= *command
) {
472 if (plot_command
== svgtiny_PATH_MOVE
) {
476 p
[i
++] = last_cubic_x
= last_quad_x
= last_x
478 p
[i
++] = last_cubic_y
= last_quad_y
= last_y
481 plot_command
= svgtiny_PATH_LINE
;
482 } while (sscanf(s
, "%f %f %n", &x
, &y
, &n
) == 2);
484 /* closepath (Z, z) (no arguments) */
485 } else if (sscanf(s
, " %1[Zz] %n", command
, &n
) == 1) {
486 /*LOG(("closepath"));*/
487 p
[i
++] = svgtiny_PATH_CLOSE
;
489 last_cubic_x
= last_quad_x
= last_x
= subpath_first_x
;
490 last_cubic_y
= last_quad_y
= last_y
= subpath_first_y
;
492 /* horizontal lineto (H, h) (1 argument) */
493 } else if (sscanf(s
, " %1[Hh] %f %n", command
, &x
, &n
) == 2) {
494 /*LOG(("horizontal lineto"));*/
496 p
[i
++] = svgtiny_PATH_LINE
;
499 p
[i
++] = last_cubic_x
= last_quad_x
= last_x
501 p
[i
++] = last_cubic_y
= last_quad_y
= last_y
;
503 } while (sscanf(s
, "%f %n", &x
, &n
) == 1);
505 /* vertical lineto (V, v) (1 argument) */
506 } else if (sscanf(s
, " %1[Vv] %f %n", command
, &y
, &n
) == 2) {
507 /*LOG(("vertical lineto"));*/
509 p
[i
++] = svgtiny_PATH_LINE
;
512 p
[i
++] = last_cubic_x
= last_quad_x
= last_x
;
513 p
[i
++] = last_cubic_y
= last_quad_y
= last_y
516 } while (sscanf(s
, "%f %n", &x
, &n
) == 1);
518 /* curveto (C, c) (6 arguments) */
519 } else if (sscanf(s
, " %1[Cc] %f %f %f %f %f %f %n", command
,
520 &x1
, &y1
, &x2
, &y2
, &x
, &y
, &n
) == 7) {
521 /*LOG(("curveto"));*/
523 p
[i
++] = svgtiny_PATH_BEZIER
;
524 if (*command
== 'c') {
534 p
[i
++] = last_cubic_x
= x2
;
535 p
[i
++] = last_cubic_y
= y2
;
536 p
[i
++] = last_quad_x
= last_x
= x
;
537 p
[i
++] = last_quad_y
= last_y
= y
;
539 } while (sscanf(s
, "%f %f %f %f %f %f %n",
540 &x1
, &y1
, &x2
, &y2
, &x
, &y
, &n
) == 6);
542 /* shorthand/smooth curveto (S, s) (4 arguments) */
543 } else if (sscanf(s
, " %1[Ss] %f %f %f %f %n", command
,
544 &x2
, &y2
, &x
, &y
, &n
) == 5) {
545 /*LOG(("shorthand/smooth curveto"));*/
547 p
[i
++] = svgtiny_PATH_BEZIER
;
548 x1
= last_x
+ (last_x
- last_cubic_x
);
549 y1
= last_y
+ (last_y
- last_cubic_y
);
550 if (*command
== 's') {
558 p
[i
++] = last_cubic_x
= x2
;
559 p
[i
++] = last_cubic_y
= y2
;
560 p
[i
++] = last_quad_x
= last_x
= x
;
561 p
[i
++] = last_quad_y
= last_y
= y
;
563 } while (sscanf(s
, "%f %f %f %f %n",
564 &x2
, &y2
, &x
, &y
, &n
) == 4);
566 /* quadratic Bezier curveto (Q, q) (4 arguments) */
567 } else if (sscanf(s
, " %1[Qq] %f %f %f %f %n", command
,
568 &x1
, &y1
, &x
, &y
, &n
) == 5) {
569 /*LOG(("quadratic Bezier curveto"));*/
571 p
[i
++] = svgtiny_PATH_BEZIER
;
574 if (*command
== 'q') {
580 p
[i
++] = 1./3 * last_x
+ 2./3 * x1
;
581 p
[i
++] = 1./3 * last_y
+ 2./3 * y1
;
582 p
[i
++] = 2./3 * x1
+ 1./3 * x
;
583 p
[i
++] = 2./3 * y1
+ 1./3 * y
;
584 p
[i
++] = last_cubic_x
= last_x
= x
;
585 p
[i
++] = last_cubic_y
= last_y
= y
;
587 } while (sscanf(s
, "%f %f %f %f %n",
588 &x1
, &y1
, &x
, &y
, &n
) == 4);
590 /* shorthand/smooth quadratic Bezier curveto (T, t)
592 } else if (sscanf(s
, " %1[Tt] %f %f %n", command
,
594 /*LOG(("shorthand/smooth quadratic Bezier curveto"));*/
596 p
[i
++] = svgtiny_PATH_BEZIER
;
597 x1
= last_x
+ (last_x
- last_quad_x
);
598 y1
= last_y
+ (last_y
- last_quad_y
);
601 if (*command
== 't') {
607 p
[i
++] = 1./3 * last_x
+ 2./3 * x1
;
608 p
[i
++] = 1./3 * last_y
+ 2./3 * y1
;
609 p
[i
++] = 2./3 * x1
+ 1./3 * x
;
610 p
[i
++] = 2./3 * y1
+ 1./3 * y
;
611 p
[i
++] = last_cubic_x
= last_x
= x
;
612 p
[i
++] = last_cubic_y
= last_y
= y
;
614 } while (sscanf(s
, "%f %f %n",
617 /* elliptical arc (A, a) (7 arguments) */
618 } else if (sscanf(s
, " %1[Aa] %f %f %f %f %f %f %f %n", command
,
619 &rx
, &ry
, &rotation
, &large_arc
, &sweep
,
622 p
[i
++] = svgtiny_PATH_LINE
;
623 if (*command
== 'a') {
627 p
[i
++] = last_cubic_x
= last_quad_x
= last_x
629 p
[i
++] = last_cubic_y
= last_quad_y
= last_y
632 } while (sscanf(s
, "%f %f %f %f %f %f %f %n",
633 &rx
, &ry
, &rotation
, &large_arc
, &sweep
,
637 fprintf(stderr
, "parse failed at \"%s\"\n", s
);
645 /* no real segments in path */
647 svgtiny_cleanup_state_local(&state
);
651 err
= svgtiny_add_path(p
, i
, &state
);
653 svgtiny_cleanup_state_local(&state
);
660 * Parse a <rect> element node.
662 * http://www.w3.org/TR/SVG11/shapes#RectElement
665 svgtiny_code
svgtiny_parse_rect(dom_element
*rect
,
666 struct svgtiny_parse_state state
)
669 float x
, y
, width
, height
;
672 svgtiny_setup_state_local(&state
);
674 svgtiny_parse_position_attributes(rect
, state
,
675 &x
, &y
, &width
, &height
);
676 svgtiny_parse_paint_attributes(rect
, &state
);
677 svgtiny_parse_transform_attributes(rect
, &state
);
679 p
= malloc(13 * sizeof p
[0]);
681 svgtiny_cleanup_state_local(&state
);
682 return svgtiny_OUT_OF_MEMORY
;
685 p
[0] = svgtiny_PATH_MOVE
;
688 p
[3] = svgtiny_PATH_LINE
;
691 p
[6] = svgtiny_PATH_LINE
;
694 p
[9] = svgtiny_PATH_LINE
;
697 p
[12] = svgtiny_PATH_CLOSE
;
699 err
= svgtiny_add_path(p
, 13, &state
);
701 svgtiny_cleanup_state_local(&state
);
708 * Parse a <circle> element node.
711 svgtiny_code
svgtiny_parse_circle(dom_element
*circle
,
712 struct svgtiny_parse_state state
)
715 float x
= 0, y
= 0, r
= -1;
720 svgtiny_setup_state_local(&state
);
722 exc
= dom_element_get_attribute(circle
, state
.interned_cx
, &attr
);
723 if (exc
!= DOM_NO_ERR
) {
724 svgtiny_cleanup_state_local(&state
);
725 return svgtiny_LIBDOM_ERROR
;
728 x
= svgtiny_parse_length(attr
, state
.viewport_width
, state
);
730 dom_string_unref(attr
);
732 exc
= dom_element_get_attribute(circle
, state
.interned_cy
, &attr
);
733 if (exc
!= DOM_NO_ERR
) {
734 svgtiny_cleanup_state_local(&state
);
735 return svgtiny_LIBDOM_ERROR
;
738 y
= svgtiny_parse_length(attr
, state
.viewport_height
, state
);
740 dom_string_unref(attr
);
742 exc
= dom_element_get_attribute(circle
, state
.interned_r
, &attr
);
743 if (exc
!= DOM_NO_ERR
) {
744 svgtiny_cleanup_state_local(&state
);
745 return svgtiny_LIBDOM_ERROR
;
748 r
= svgtiny_parse_length(attr
, state
.viewport_width
, state
);
750 dom_string_unref(attr
);
752 svgtiny_parse_paint_attributes(circle
, &state
);
753 svgtiny_parse_transform_attributes(circle
, &state
);
756 state
.diagram
->error_line
= -1; /* circle->line; */
757 state
.diagram
->error_message
= "circle: r missing or negative";
758 svgtiny_cleanup_state_local(&state
);
759 return svgtiny_SVG_ERROR
;
762 svgtiny_cleanup_state_local(&state
);
766 p
= malloc(32 * sizeof p
[0]);
768 svgtiny_cleanup_state_local(&state
);
769 return svgtiny_OUT_OF_MEMORY
;
772 p
[0] = svgtiny_PATH_MOVE
;
775 p
[3] = svgtiny_PATH_BEZIER
;
777 p
[5] = y
+ r
* KAPPA
;
778 p
[6] = x
+ r
* KAPPA
;
782 p
[10] = svgtiny_PATH_BEZIER
;
783 p
[11] = x
- r
* KAPPA
;
786 p
[14] = y
+ r
* KAPPA
;
789 p
[17] = svgtiny_PATH_BEZIER
;
791 p
[19] = y
- r
* KAPPA
;
792 p
[20] = x
- r
* KAPPA
;
796 p
[24] = svgtiny_PATH_BEZIER
;
797 p
[25] = x
+ r
* KAPPA
;
800 p
[28] = y
- r
* KAPPA
;
803 p
[31] = svgtiny_PATH_CLOSE
;
805 err
= svgtiny_add_path(p
, 32, &state
);
807 svgtiny_cleanup_state_local(&state
);
814 * Parse an <ellipse> element node.
817 svgtiny_code
svgtiny_parse_ellipse(dom_element
*ellipse
,
818 struct svgtiny_parse_state state
)
821 float x
= 0, y
= 0, rx
= -1, ry
= -1;
826 svgtiny_setup_state_local(&state
);
828 exc
= dom_element_get_attribute(ellipse
, state
.interned_cx
, &attr
);
829 if (exc
!= DOM_NO_ERR
) {
830 svgtiny_cleanup_state_local(&state
);
831 return svgtiny_LIBDOM_ERROR
;
834 x
= svgtiny_parse_length(attr
, state
.viewport_width
, state
);
836 dom_string_unref(attr
);
838 exc
= dom_element_get_attribute(ellipse
, state
.interned_cy
, &attr
);
839 if (exc
!= DOM_NO_ERR
) {
840 svgtiny_cleanup_state_local(&state
);
841 return svgtiny_LIBDOM_ERROR
;
844 y
= svgtiny_parse_length(attr
, state
.viewport_height
, state
);
846 dom_string_unref(attr
);
848 exc
= dom_element_get_attribute(ellipse
, state
.interned_rx
, &attr
);
849 if (exc
!= DOM_NO_ERR
) {
850 svgtiny_cleanup_state_local(&state
);
851 return svgtiny_LIBDOM_ERROR
;
854 rx
= svgtiny_parse_length(attr
, state
.viewport_width
, state
);
856 dom_string_unref(attr
);
858 exc
= dom_element_get_attribute(ellipse
, state
.interned_ry
, &attr
);
859 if (exc
!= DOM_NO_ERR
) {
860 svgtiny_cleanup_state_local(&state
);
861 return svgtiny_LIBDOM_ERROR
;
864 ry
= svgtiny_parse_length(attr
, state
.viewport_width
, state
);
866 dom_string_unref(attr
);
868 svgtiny_parse_paint_attributes(ellipse
, &state
);
869 svgtiny_parse_transform_attributes(ellipse
, &state
);
871 if (rx
< 0 || ry
< 0) {
872 state
.diagram
->error_line
= -1; /* ellipse->line; */
873 state
.diagram
->error_message
= "ellipse: rx or ry missing "
875 svgtiny_cleanup_state_local(&state
);
876 return svgtiny_SVG_ERROR
;
878 if (rx
== 0 || ry
== 0) {
879 svgtiny_cleanup_state_local(&state
);
883 p
= malloc(32 * sizeof p
[0]);
885 svgtiny_cleanup_state_local(&state
);
886 return svgtiny_OUT_OF_MEMORY
;
889 p
[0] = svgtiny_PATH_MOVE
;
892 p
[3] = svgtiny_PATH_BEZIER
;
894 p
[5] = y
+ ry
* KAPPA
;
895 p
[6] = x
+ rx
* KAPPA
;
899 p
[10] = svgtiny_PATH_BEZIER
;
900 p
[11] = x
- rx
* KAPPA
;
903 p
[14] = y
+ ry
* KAPPA
;
906 p
[17] = svgtiny_PATH_BEZIER
;
908 p
[19] = y
- ry
* KAPPA
;
909 p
[20] = x
- rx
* KAPPA
;
913 p
[24] = svgtiny_PATH_BEZIER
;
914 p
[25] = x
+ rx
* KAPPA
;
917 p
[28] = y
- ry
* KAPPA
;
920 p
[31] = svgtiny_PATH_CLOSE
;
922 err
= svgtiny_add_path(p
, 32, &state
);
924 svgtiny_cleanup_state_local(&state
);
931 * Parse a <line> element node.
934 svgtiny_code
svgtiny_parse_line(dom_element
*line
,
935 struct svgtiny_parse_state state
)
938 float x1
= 0, y1
= 0, x2
= 0, y2
= 0;
943 svgtiny_setup_state_local(&state
);
945 exc
= dom_element_get_attribute(line
, state
.interned_x1
, &attr
);
946 if (exc
!= DOM_NO_ERR
) {
947 svgtiny_cleanup_state_local(&state
);
948 return svgtiny_LIBDOM_ERROR
;
951 x1
= svgtiny_parse_length(attr
, state
.viewport_width
, state
);
953 dom_string_unref(attr
);
955 exc
= dom_element_get_attribute(line
, state
.interned_y1
, &attr
);
956 if (exc
!= DOM_NO_ERR
) {
957 svgtiny_cleanup_state_local(&state
);
958 return svgtiny_LIBDOM_ERROR
;
961 y1
= svgtiny_parse_length(attr
, state
.viewport_height
, state
);
963 dom_string_unref(attr
);
965 exc
= dom_element_get_attribute(line
, state
.interned_x2
, &attr
);
966 if (exc
!= DOM_NO_ERR
) {
967 svgtiny_cleanup_state_local(&state
);
968 return svgtiny_LIBDOM_ERROR
;
971 x2
= svgtiny_parse_length(attr
, state
.viewport_width
, state
);
973 dom_string_unref(attr
);
975 exc
= dom_element_get_attribute(line
, state
.interned_y2
, &attr
);
976 if (exc
!= DOM_NO_ERR
) {
977 svgtiny_cleanup_state_local(&state
);
978 return svgtiny_LIBDOM_ERROR
;
981 y2
= svgtiny_parse_length(attr
, state
.viewport_height
, state
);
983 dom_string_unref(attr
);
985 svgtiny_parse_paint_attributes(line
, &state
);
986 svgtiny_parse_transform_attributes(line
, &state
);
988 p
= malloc(7 * sizeof p
[0]);
990 svgtiny_cleanup_state_local(&state
);
991 return svgtiny_OUT_OF_MEMORY
;
994 p
[0] = svgtiny_PATH_MOVE
;
997 p
[3] = svgtiny_PATH_LINE
;
1000 p
[6] = svgtiny_PATH_CLOSE
;
1002 err
= svgtiny_add_path(p
, 7, &state
);
1004 svgtiny_cleanup_state_local(&state
);
1011 * Parse a <polyline> or <polygon> element node.
1013 * http://www.w3.org/TR/SVG11/shapes#PolylineElement
1014 * http://www.w3.org/TR/SVG11/shapes#PolygonElement
1017 svgtiny_code
svgtiny_parse_poly(dom_element
*poly
,
1018 struct svgtiny_parse_state state
, bool polygon
)
1021 dom_string
*points_str
;
1027 svgtiny_setup_state_local(&state
);
1029 svgtiny_parse_paint_attributes(poly
, &state
);
1030 svgtiny_parse_transform_attributes(poly
, &state
);
1032 exc
= dom_element_get_attribute(poly
, state
.interned_points
,
1034 if (exc
!= DOM_NO_ERR
) {
1035 svgtiny_cleanup_state_local(&state
);
1036 return svgtiny_LIBDOM_ERROR
;
1039 if (points_str
== NULL
) {
1040 state
.diagram
->error_line
= -1; /* poly->line; */
1041 state
.diagram
->error_message
=
1042 "polyline/polygon: missing points attribute";
1043 svgtiny_cleanup_state_local(&state
);
1044 return svgtiny_SVG_ERROR
;
1047 s
= points
= strndup(dom_string_data(points_str
),
1048 dom_string_byte_length(points_str
));
1049 dom_string_unref(points_str
);
1050 /* read points attribute */
1052 svgtiny_cleanup_state_local(&state
);
1053 return svgtiny_OUT_OF_MEMORY
;
1055 /* allocate space for path: it will never have more elements than s */
1056 p
= malloc(sizeof p
[0] * strlen(s
));
1059 svgtiny_cleanup_state_local(&state
);
1060 return svgtiny_OUT_OF_MEMORY
;
1063 /* parse s and build path */
1064 for (i
= 0; s
[i
]; i
++)
1072 if (sscanf(s
, "%f %f %n", &x
, &y
, &n
) == 2) {
1074 p
[i
++] = svgtiny_PATH_MOVE
;
1076 p
[i
++] = svgtiny_PATH_LINE
;
1085 p
[i
++] = svgtiny_PATH_CLOSE
;
1089 err
= svgtiny_add_path(p
, i
, &state
);
1091 svgtiny_cleanup_state_local(&state
);
1098 * Parse a <text> or <tspan> element node.
1101 svgtiny_code
svgtiny_parse_text(dom_element
*text
,
1102 struct svgtiny_parse_state state
)
1104 float x
, y
, width
, height
;
1109 svgtiny_setup_state_local(&state
);
1111 svgtiny_parse_position_attributes(text
, state
,
1112 &x
, &y
, &width
, &height
);
1113 svgtiny_parse_font_attributes(text
, &state
);
1114 svgtiny_parse_transform_attributes(text
, &state
);
1116 px
= state
.ctm
.a
* x
+ state
.ctm
.c
* y
+ state
.ctm
.e
;
1117 py
= state
.ctm
.b
* x
+ state
.ctm
.d
* y
+ state
.ctm
.f
;
1118 /* state.ctm.e = px - state.origin_x; */
1119 /* state.ctm.f = py - state.origin_y; */
1121 /*struct css_style style = state.style;
1122 style.font_size.value.length.value *= state.ctm.a;*/
1124 exc
= dom_node_get_first_child(text
, &child
);
1125 if (exc
!= DOM_NO_ERR
) {
1126 return svgtiny_LIBDOM_ERROR
;
1127 svgtiny_cleanup_state_local(&state
);
1129 while (child
!= NULL
) {
1131 dom_node_type nodetype
;
1132 svgtiny_code code
= svgtiny_OK
;
1134 exc
= dom_node_get_node_type(child
, &nodetype
);
1135 if (exc
!= DOM_NO_ERR
) {
1136 dom_node_unref(child
);
1137 svgtiny_cleanup_state_local(&state
);
1138 return svgtiny_LIBDOM_ERROR
;
1140 if (nodetype
== DOM_ELEMENT_NODE
) {
1141 dom_string
*nodename
;
1142 exc
= dom_node_get_node_name(child
, &nodename
);
1143 if (exc
!= DOM_NO_ERR
) {
1144 dom_node_unref(child
);
1145 svgtiny_cleanup_state_local(&state
);
1146 return svgtiny_LIBDOM_ERROR
;
1148 if (dom_string_caseless_isequal(nodename
,
1149 state
.interned_tspan
))
1150 code
= svgtiny_parse_text((dom_element
*)child
,
1152 dom_string_unref(nodename
);
1153 } else if (nodetype
== DOM_TEXT_NODE
) {
1154 struct svgtiny_shape
*shape
= svgtiny_add_shape(&state
);
1155 dom_string
*content
;
1156 if (shape
== NULL
) {
1157 dom_node_unref(child
);
1158 svgtiny_cleanup_state_local(&state
);
1159 return svgtiny_OUT_OF_MEMORY
;
1161 exc
= dom_text_get_whole_text(child
, &content
);
1162 if (exc
!= DOM_NO_ERR
) {
1163 dom_node_unref(child
);
1164 svgtiny_cleanup_state_local(&state
);
1165 return svgtiny_LIBDOM_ERROR
;
1167 if (content
!= NULL
) {
1168 shape
->text
= strndup(dom_string_data(content
),
1169 dom_string_byte_length(content
));
1170 dom_string_unref(content
);
1172 shape
->text
= strdup("");
1176 state
.diagram
->shape_count
++;
1179 if (code
!= svgtiny_OK
) {
1180 dom_node_unref(child
);
1181 svgtiny_cleanup_state_local(&state
);
1184 exc
= dom_node_get_next_sibling(child
, &next
);
1185 dom_node_unref(child
);
1186 if (exc
!= DOM_NO_ERR
) {
1187 svgtiny_cleanup_state_local(&state
);
1188 return svgtiny_LIBDOM_ERROR
;
1193 svgtiny_cleanup_state_local(&state
);
1200 * Parse x, y, width, and height attributes, if present.
1203 void svgtiny_parse_position_attributes(dom_element
*node
,
1204 const struct svgtiny_parse_state state
,
1205 float *x
, float *y
, float *width
, float *height
)
1212 *width
= state
.viewport_width
;
1213 *height
= state
.viewport_height
;
1215 exc
= dom_element_get_attribute(node
, state
.interned_x
, &attr
);
1216 if (exc
== DOM_NO_ERR
&& attr
!= NULL
) {
1217 *x
= svgtiny_parse_length(attr
, state
.viewport_width
, state
);
1218 dom_string_unref(attr
);
1221 exc
= dom_element_get_attribute(node
, state
.interned_y
, &attr
);
1222 if (exc
== DOM_NO_ERR
&& attr
!= NULL
) {
1223 *y
= svgtiny_parse_length(attr
, state
.viewport_height
, state
);
1224 dom_string_unref(attr
);
1227 exc
= dom_element_get_attribute(node
, state
.interned_width
, &attr
);
1228 if (exc
== DOM_NO_ERR
&& attr
!= NULL
) {
1229 *width
= svgtiny_parse_length(attr
, state
.viewport_width
,
1231 dom_string_unref(attr
);
1234 exc
= dom_element_get_attribute(node
, state
.interned_height
, &attr
);
1235 if (exc
== DOM_NO_ERR
&& attr
!= NULL
) {
1236 *height
= svgtiny_parse_length(attr
, state
.viewport_height
,
1238 dom_string_unref(attr
);
1244 * Parse a length as a number of pixels.
1247 static float _svgtiny_parse_length(const char *s
, int viewport_size
,
1248 const struct svgtiny_parse_state state
)
1250 int num_length
= strspn(s
, "0123456789+-.");
1251 const char *unit
= s
+ num_length
;
1252 float n
= atof((const char *) s
);
1253 float font_size
= 20; /*css_len2px(&state.style.font_size.value.length, 0);*/
1259 } else if (unit
[0] == '%') {
1260 return n
/ 100.0 * viewport_size
;
1261 } else if (unit
[0] == 'e' && unit
[1] == 'm') {
1262 return n
* font_size
;
1263 } else if (unit
[0] == 'e' && unit
[1] == 'x') {
1264 return n
/ 2.0 * font_size
;
1265 } else if (unit
[0] == 'p' && unit
[1] == 'x') {
1267 } else if (unit
[0] == 'p' && unit
[1] == 't') {
1269 } else if (unit
[0] == 'p' && unit
[1] == 'c') {
1271 } else if (unit
[0] == 'm' && unit
[1] == 'm') {
1272 return n
* 3.543307;
1273 } else if (unit
[0] == 'c' && unit
[1] == 'm') {
1274 return n
* 35.43307;
1275 } else if (unit
[0] == 'i' && unit
[1] == 'n') {
1282 float svgtiny_parse_length(dom_string
*s
, int viewport_size
,
1283 const struct svgtiny_parse_state state
)
1285 char *ss
= strndup(dom_string_data(s
), dom_string_byte_length(s
));
1286 float ret
= _svgtiny_parse_length(ss
, viewport_size
, state
);
1292 * Parse paint attributes, if present.
1295 void svgtiny_parse_paint_attributes(dom_element
*node
,
1296 struct svgtiny_parse_state
*state
)
1301 exc
= dom_element_get_attribute(node
, state
->interned_fill
, &attr
);
1302 if (exc
== DOM_NO_ERR
&& attr
!= NULL
) {
1303 svgtiny_parse_color(attr
, &state
->fill
, state
);
1304 dom_string_unref(attr
);
1307 exc
= dom_element_get_attribute(node
, state
->interned_stroke
, &attr
);
1308 if (exc
== DOM_NO_ERR
&& attr
!= NULL
) {
1309 svgtiny_parse_color(attr
, &state
->stroke
, state
);
1310 dom_string_unref(attr
);
1313 exc
= dom_element_get_attribute(node
, state
->interned_stroke_width
, &attr
);
1314 if (exc
== DOM_NO_ERR
&& attr
!= NULL
) {
1315 state
->stroke_width
= svgtiny_parse_length(attr
,
1316 state
->viewport_width
, *state
);
1317 dom_string_unref(attr
);
1320 exc
= dom_element_get_attribute(node
, state
->interned_style
, &attr
);
1321 if (exc
== DOM_NO_ERR
&& attr
!= NULL
) {
1322 char *style
= strndup(dom_string_data(attr
),
1323 dom_string_byte_length(attr
));
1326 if ((s
= strstr(style
, "fill:"))) {
1330 value
= strndup(s
, strcspn(s
, "; "));
1331 _svgtiny_parse_color(value
, &state
->fill
, state
);
1334 if ((s
= strstr(style
, "stroke:"))) {
1338 value
= strndup(s
, strcspn(s
, "; "));
1339 _svgtiny_parse_color(value
, &state
->stroke
, state
);
1342 if ((s
= strstr(style
, "stroke-width:"))) {
1346 value
= strndup(s
, strcspn(s
, "; "));
1347 state
->stroke_width
= _svgtiny_parse_length(value
,
1348 state
->viewport_width
, *state
);
1352 dom_string_unref(attr
);
1361 static void _svgtiny_parse_color(const char *s
, svgtiny_colour
*c
,
1362 struct svgtiny_parse_state
*state
)
1364 unsigned int r
, g
, b
;
1366 size_t len
= strlen(s
);
1367 char *id
= 0, *rparen
;
1369 if (len
== 4 && s
[0] == '#') {
1370 if (sscanf(s
+ 1, "%1x%1x%1x", &r
, &g
, &b
) == 3)
1371 *c
= svgtiny_RGB(r
| r
<< 4, g
| g
<< 4, b
| b
<< 4);
1373 } else if (len
== 7 && s
[0] == '#') {
1374 if (sscanf(s
+ 1, "%2x%2x%2x", &r
, &g
, &b
) == 3)
1375 *c
= svgtiny_RGB(r
, g
, b
);
1377 } else if (10 <= len
&& s
[0] == 'r' && s
[1] == 'g' && s
[2] == 'b' &&
1378 s
[3] == '(' && s
[len
- 1] == ')') {
1379 if (sscanf(s
+ 4, "%u,%u,%u", &r
, &g
, &b
) == 3)
1380 *c
= svgtiny_RGB(r
, g
, b
);
1381 else if (sscanf(s
+ 4, "%f%%,%f%%,%f%%", &rf
, &gf
, &bf
) == 3) {
1385 *c
= svgtiny_RGB(r
, g
, b
);
1388 } else if (len
== 4 && strcmp(s
, "none") == 0) {
1389 *c
= svgtiny_TRANSPARENT
;
1391 } else if (5 < len
&& s
[0] == 'u' && s
[1] == 'r' && s
[2] == 'l' &&
1397 rparen
= strchr(id
, ')');
1400 svgtiny_find_gradient(id
, state
);
1402 if (state
->linear_gradient_stop_count
== 0)
1403 *c
= svgtiny_TRANSPARENT
;
1404 else if (state
->linear_gradient_stop_count
== 1)
1405 *c
= state
->gradient_stop
[0].color
;
1407 *c
= svgtiny_LINEAR_GRADIENT
;
1411 const struct svgtiny_named_color
*named_color
;
1412 named_color
= svgtiny_color_lookup(s
, strlen(s
));
1414 *c
= named_color
->color
;
1418 void svgtiny_parse_color(dom_string
*s
, svgtiny_colour
*c
,
1419 struct svgtiny_parse_state
*state
)
1421 char *ss
= strndup(dom_string_data(s
), dom_string_byte_length(s
));
1422 _svgtiny_parse_color(ss
, c
, state
);
1427 * Parse font attributes, if present.
1430 void svgtiny_parse_font_attributes(dom_element
*node
,
1431 struct svgtiny_parse_state
*state
)
1433 /* TODO: Implement this, it never used to be */
1436 #ifdef WRITTEN_THIS_PROPERLY
1437 const xmlAttr
*attr
;
1441 for (attr
= node
->properties
; attr
; attr
= attr
->next
) {
1442 if (strcmp((const char *) attr
->name
, "font-size") == 0) {
1443 /*if (css_parse_length(
1444 (const char *) attr->children->content,
1445 &state->style.font_size.value.length,
1447 state->style.font_size.size =
1448 CSS_FONT_SIZE_LENGTH;
1457 * Parse transform attributes, if present.
1459 * http://www.w3.org/TR/SVG11/coords#TransformAttribute
1462 void svgtiny_parse_transform_attributes(dom_element
*node
,
1463 struct svgtiny_parse_state
*state
)
1469 exc
= dom_element_get_attribute(node
, state
->interned_transform
,
1471 if (exc
== DOM_NO_ERR
&& attr
!= NULL
) {
1472 transform
= strndup(dom_string_data(attr
),
1473 dom_string_byte_length(attr
));
1474 svgtiny_parse_transform(transform
, &state
->ctm
.a
, &state
->ctm
.b
,
1475 &state
->ctm
.c
, &state
->ctm
.d
,
1476 &state
->ctm
.e
, &state
->ctm
.f
);
1478 dom_string_unref(attr
);
1484 * Parse a transform string.
1487 void svgtiny_parse_transform(char *s
, float *ma
, float *mb
,
1488 float *mc
, float *md
, float *me
, float *mf
)
1490 float a
, b
, c
, d
, e
, f
;
1491 float za
, zb
, zc
, zd
, ze
, zf
;
1496 for (i
= 0; s
[i
]; i
++)
1504 if ((sscanf(s
, " matrix (%f %f %f %f %f %f ) %n",
1505 &a
, &b
, &c
, &d
, &e
, &f
, &n
) == 6) && (n
> 0))
1507 else if ((sscanf(s
, " translate (%f %f ) %n",
1508 &e
, &f
, &n
) == 2) && (n
> 0))
1510 else if ((sscanf(s
, " translate (%f ) %n",
1511 &e
, &n
) == 1) && (n
> 0))
1513 else if ((sscanf(s
, " scale (%f %f ) %n",
1514 &a
, &d
, &n
) == 2) && (n
> 0))
1516 else if ((sscanf(s
, " scale (%f ) %n",
1517 &a
, &n
) == 1) && (n
> 0))
1519 else if ((sscanf(s
, " rotate (%f %f %f ) %n",
1520 &angle
, &x
, &y
, &n
) == 3) && (n
> 0)) {
1521 angle
= angle
/ 180 * M_PI
;
1526 e
= -x
* cos(angle
) + y
* sin(angle
) + x
;
1527 f
= -x
* sin(angle
) - y
* cos(angle
) + y
;
1528 } else if ((sscanf(s
, " rotate (%f ) %n",
1529 &angle
, &n
) == 1) && (n
> 0)) {
1530 angle
= angle
/ 180 * M_PI
;
1535 } else if ((sscanf(s
, " skewX (%f ) %n",
1536 &angle
, &n
) == 1) && (n
> 0)) {
1537 angle
= angle
/ 180 * M_PI
;
1539 } else if ((sscanf(s
, " skewY (%f ) %n",
1540 &angle
, &n
) == 1) && (n
> 0)) {
1541 angle
= angle
/ 180 * M_PI
;
1545 za
= *ma
* a
+ *mc
* b
;
1546 zb
= *mb
* a
+ *md
* b
;
1547 zc
= *ma
* c
+ *mc
* d
;
1548 zd
= *mb
* c
+ *md
* d
;
1549 ze
= *ma
* e
+ *mc
* f
+ *me
;
1550 zf
= *mb
* e
+ *md
* f
+ *mf
;
1563 * Add a path to the svgtiny_diagram.
1566 svgtiny_code
svgtiny_add_path(float *p
, unsigned int n
,
1567 struct svgtiny_parse_state
*state
)
1569 struct svgtiny_shape
*shape
;
1571 if (state
->fill
== svgtiny_LINEAR_GRADIENT
)
1572 return svgtiny_add_path_linear_gradient(p
, n
, state
);
1574 svgtiny_transform_path(p
, n
, state
);
1576 shape
= svgtiny_add_shape(state
);
1579 return svgtiny_OUT_OF_MEMORY
;
1582 shape
->path_length
= n
;
1583 state
->diagram
->shape_count
++;
1590 * Add a svgtiny_shape to the svgtiny_diagram.
1593 struct svgtiny_shape
*svgtiny_add_shape(struct svgtiny_parse_state
*state
)
1595 struct svgtiny_shape
*shape
= realloc(state
->diagram
->shape
,
1596 (state
->diagram
->shape_count
+ 1) *
1597 sizeof (state
->diagram
->shape
[0]));
1600 state
->diagram
->shape
= shape
;
1602 shape
+= state
->diagram
->shape_count
;
1604 shape
->path_length
= 0;
1606 shape
->fill
= state
->fill
;
1607 shape
->stroke
= state
->stroke
;
1608 shape
->stroke_width
= lroundf((float) state
->stroke_width
*
1609 (state
->ctm
.a
+ state
->ctm
.d
) / 2.0);
1610 if (0 < state
->stroke_width
&& shape
->stroke_width
== 0)
1611 shape
->stroke_width
= 1;
1618 * Apply the current transformation matrix to a path.
1621 void svgtiny_transform_path(float *p
, unsigned int n
,
1622 struct svgtiny_parse_state
*state
)
1626 for (j
= 0; j
!= n
; ) {
1627 unsigned int points
= 0;
1629 switch ((int) p
[j
]) {
1630 case svgtiny_PATH_MOVE
:
1631 case svgtiny_PATH_LINE
:
1634 case svgtiny_PATH_CLOSE
:
1637 case svgtiny_PATH_BEZIER
:
1644 for (k
= 0; k
!= points
; k
++) {
1645 float x0
= p
[j
], y0
= p
[j
+ 1];
1646 float x
= state
->ctm
.a
* x0
+ state
->ctm
.c
* y0
+
1648 float y
= state
->ctm
.b
* x0
+ state
->ctm
.d
* y0
+
1659 * Free all memory used by a diagram.
1662 void svgtiny_free(struct svgtiny_diagram
*svg
)
1667 for (i
= 0; i
!= svg
->shape_count
; i
++) {
1668 free(svg
->shape
[i
].path
);
1669 free(svg
->shape
[i
].text
);
1677 #ifndef HAVE_STRNDUP
1678 char *svgtiny_strndup(const char *s
, size_t n
)
1683 for (len
= 0; len
!= n
&& s
[len
]; len
++)
1686 s2
= malloc(len
+ 1);