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;
411 svgtiny_setup_state_local(&state
);
413 svgtiny_parse_paint_attributes(path
, &state
);
414 svgtiny_parse_transform_attributes(path
, &state
);
416 /* read d attribute */
417 exc
= dom_element_get_attribute(path
, state
.interned_d
, &path_d_str
);
418 if (exc
!= DOM_NO_ERR
) {
419 state
.diagram
->error_line
= -1; /* path->line; */
420 state
.diagram
->error_message
= "path: error retrieving d attribute";
421 svgtiny_cleanup_state_local(&state
);
422 return svgtiny_SVG_ERROR
;
425 if (path_d_str
== NULL
) {
426 state
.diagram
->error_line
= -1; /* path->line; */
427 state
.diagram
->error_message
= "path: missing d attribute";
428 svgtiny_cleanup_state_local(&state
);
429 return svgtiny_SVG_ERROR
;
432 s
= path_d
= strndup(dom_string_data(path_d_str
),
433 dom_string_byte_length(path_d_str
));
434 dom_string_unref(path_d_str
);
436 svgtiny_cleanup_state_local(&state
);
437 return svgtiny_OUT_OF_MEMORY
;
439 /* allocate space for path: it will never have more elements than d */
440 p
= malloc(sizeof p
[0] * strlen(s
));
443 svgtiny_cleanup_state_local(&state
);
444 return svgtiny_OUT_OF_MEMORY
;
447 /* parse d and build path */
448 for (i
= 0; s
[i
]; i
++)
455 float x
, y
, x1
, y1
, x2
, y2
, rx
, ry
, rotation
, large_arc
, sweep
;
458 /* moveto (M, m), lineto (L, l) (2 arguments) */
459 if (sscanf(s
, " %1[MmLl] %f %f %n", command
, &x
, &y
, &n
) == 3) {
460 /*LOG(("moveto or lineto"));*/
461 if (*command
== 'M' || *command
== 'm')
462 plot_command
= svgtiny_PATH_MOVE
;
464 plot_command
= svgtiny_PATH_LINE
;
466 p
[i
++] = plot_command
;
467 if ('a' <= *command
) {
471 p
[i
++] = last_cubic_x
= last_quad_x
= last_x
473 p
[i
++] = last_cubic_y
= last_quad_y
= last_y
476 plot_command
= svgtiny_PATH_LINE
;
477 } while (sscanf(s
, "%f %f %n", &x
, &y
, &n
) == 2);
479 /* closepath (Z, z) (no arguments) */
480 } else if (sscanf(s
, " %1[Zz] %n", command
, &n
) == 1) {
481 /*LOG(("closepath"));*/
482 p
[i
++] = svgtiny_PATH_CLOSE
;
485 /* horizontal lineto (H, h) (1 argument) */
486 } else if (sscanf(s
, " %1[Hh] %f %n", command
, &x
, &n
) == 2) {
487 /*LOG(("horizontal lineto"));*/
489 p
[i
++] = svgtiny_PATH_LINE
;
492 p
[i
++] = last_cubic_x
= last_quad_x
= last_x
494 p
[i
++] = last_cubic_y
= last_quad_y
= last_y
;
496 } while (sscanf(s
, "%f %n", &x
, &n
) == 1);
498 /* vertical lineto (V, v) (1 argument) */
499 } else if (sscanf(s
, " %1[Vv] %f %n", command
, &y
, &n
) == 2) {
500 /*LOG(("vertical lineto"));*/
502 p
[i
++] = svgtiny_PATH_LINE
;
505 p
[i
++] = last_cubic_x
= last_quad_x
= last_x
;
506 p
[i
++] = last_cubic_y
= last_quad_y
= last_y
509 } while (sscanf(s
, "%f %n", &x
, &n
) == 1);
511 /* curveto (C, c) (6 arguments) */
512 } else if (sscanf(s
, " %1[Cc] %f %f %f %f %f %f %n", command
,
513 &x1
, &y1
, &x2
, &y2
, &x
, &y
, &n
) == 7) {
514 /*LOG(("curveto"));*/
516 p
[i
++] = svgtiny_PATH_BEZIER
;
517 if (*command
== 'c') {
527 p
[i
++] = last_cubic_x
= x2
;
528 p
[i
++] = last_cubic_y
= y2
;
529 p
[i
++] = last_quad_x
= last_x
= x
;
530 p
[i
++] = last_quad_y
= last_y
= y
;
532 } while (sscanf(s
, "%f %f %f %f %f %f %n",
533 &x1
, &y1
, &x2
, &y2
, &x
, &y
, &n
) == 6);
535 /* shorthand/smooth curveto (S, s) (4 arguments) */
536 } else if (sscanf(s
, " %1[Ss] %f %f %f %f %n", command
,
537 &x2
, &y2
, &x
, &y
, &n
) == 5) {
538 /*LOG(("shorthand/smooth curveto"));*/
540 p
[i
++] = svgtiny_PATH_BEZIER
;
541 x1
= last_x
+ (last_x
- last_cubic_x
);
542 y1
= last_y
+ (last_y
- last_cubic_y
);
543 if (*command
== 's') {
551 p
[i
++] = last_cubic_x
= x2
;
552 p
[i
++] = last_cubic_y
= y2
;
553 p
[i
++] = last_quad_x
= last_x
= x
;
554 p
[i
++] = last_quad_y
= last_y
= y
;
556 } while (sscanf(s
, "%f %f %f %f %n",
557 &x2
, &y2
, &x
, &y
, &n
) == 4);
559 /* quadratic Bezier curveto (Q, q) (4 arguments) */
560 } else if (sscanf(s
, " %1[Qq] %f %f %f %f %n", command
,
561 &x1
, &y1
, &x
, &y
, &n
) == 5) {
562 /*LOG(("quadratic Bezier curveto"));*/
564 p
[i
++] = svgtiny_PATH_BEZIER
;
567 if (*command
== 'q') {
573 p
[i
++] = 1./3 * last_x
+ 2./3 * x1
;
574 p
[i
++] = 1./3 * last_y
+ 2./3 * y1
;
575 p
[i
++] = 2./3 * x1
+ 1./3 * x
;
576 p
[i
++] = 2./3 * y1
+ 1./3 * y
;
577 p
[i
++] = last_cubic_x
= last_x
= x
;
578 p
[i
++] = last_cubic_y
= last_y
= y
;
580 } while (sscanf(s
, "%f %f %f %f %n",
581 &x1
, &y1
, &x
, &y
, &n
) == 4);
583 /* shorthand/smooth quadratic Bezier curveto (T, t)
585 } else if (sscanf(s
, " %1[Tt] %f %f %n", command
,
587 /*LOG(("shorthand/smooth quadratic Bezier curveto"));*/
589 p
[i
++] = svgtiny_PATH_BEZIER
;
590 x1
= last_x
+ (last_x
- last_quad_x
);
591 y1
= last_y
+ (last_y
- last_quad_y
);
594 if (*command
== 't') {
600 p
[i
++] = 1./3 * last_x
+ 2./3 * x1
;
601 p
[i
++] = 1./3 * last_y
+ 2./3 * y1
;
602 p
[i
++] = 2./3 * x1
+ 1./3 * x
;
603 p
[i
++] = 2./3 * y1
+ 1./3 * y
;
604 p
[i
++] = last_cubic_x
= last_x
= x
;
605 p
[i
++] = last_cubic_y
= last_y
= y
;
607 } while (sscanf(s
, "%f %f %n",
610 /* elliptical arc (A, a) (7 arguments) */
611 } else if (sscanf(s
, " %1[Aa] %f %f %f %f %f %f %f %n", command
,
612 &rx
, &ry
, &rotation
, &large_arc
, &sweep
,
615 p
[i
++] = svgtiny_PATH_LINE
;
616 if (*command
== 'a') {
620 p
[i
++] = last_cubic_x
= last_quad_x
= last_x
622 p
[i
++] = last_cubic_y
= last_quad_y
= last_y
625 } while (sscanf(s
, "%f %f %f %f %f %f %f %n",
626 &rx
, &ry
, &rotation
, &large_arc
, &sweep
,
630 fprintf(stderr
, "parse failed at \"%s\"\n", s
);
638 /* no real segments in path */
640 svgtiny_cleanup_state_local(&state
);
644 err
= svgtiny_add_path(p
, i
, &state
);
646 svgtiny_cleanup_state_local(&state
);
653 * Parse a <rect> element node.
655 * http://www.w3.org/TR/SVG11/shapes#RectElement
658 svgtiny_code
svgtiny_parse_rect(dom_element
*rect
,
659 struct svgtiny_parse_state state
)
662 float x
, y
, width
, height
;
665 svgtiny_setup_state_local(&state
);
667 svgtiny_parse_position_attributes(rect
, state
,
668 &x
, &y
, &width
, &height
);
669 svgtiny_parse_paint_attributes(rect
, &state
);
670 svgtiny_parse_transform_attributes(rect
, &state
);
672 p
= malloc(13 * sizeof p
[0]);
674 svgtiny_cleanup_state_local(&state
);
675 return svgtiny_OUT_OF_MEMORY
;
678 p
[0] = svgtiny_PATH_MOVE
;
681 p
[3] = svgtiny_PATH_LINE
;
684 p
[6] = svgtiny_PATH_LINE
;
687 p
[9] = svgtiny_PATH_LINE
;
690 p
[12] = svgtiny_PATH_CLOSE
;
692 err
= svgtiny_add_path(p
, 13, &state
);
694 svgtiny_cleanup_state_local(&state
);
701 * Parse a <circle> element node.
704 svgtiny_code
svgtiny_parse_circle(dom_element
*circle
,
705 struct svgtiny_parse_state state
)
708 float x
= 0, y
= 0, r
= -1;
713 svgtiny_setup_state_local(&state
);
715 exc
= dom_element_get_attribute(circle
, state
.interned_cx
, &attr
);
716 if (exc
!= DOM_NO_ERR
) {
717 svgtiny_cleanup_state_local(&state
);
718 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(circle
, state
.interned_cy
, &attr
);
726 if (exc
!= DOM_NO_ERR
) {
727 svgtiny_cleanup_state_local(&state
);
728 return svgtiny_LIBDOM_ERROR
;
731 y
= svgtiny_parse_length(attr
, state
.viewport_height
, state
);
733 dom_string_unref(attr
);
735 exc
= dom_element_get_attribute(circle
, state
.interned_r
, &attr
);
736 if (exc
!= DOM_NO_ERR
) {
737 svgtiny_cleanup_state_local(&state
);
738 return svgtiny_LIBDOM_ERROR
;
741 r
= svgtiny_parse_length(attr
, state
.viewport_width
, state
);
743 dom_string_unref(attr
);
745 svgtiny_parse_paint_attributes(circle
, &state
);
746 svgtiny_parse_transform_attributes(circle
, &state
);
749 state
.diagram
->error_line
= -1; /* circle->line; */
750 state
.diagram
->error_message
= "circle: r missing or negative";
751 svgtiny_cleanup_state_local(&state
);
752 return svgtiny_SVG_ERROR
;
755 svgtiny_cleanup_state_local(&state
);
759 p
= malloc(32 * sizeof p
[0]);
761 svgtiny_cleanup_state_local(&state
);
762 return svgtiny_OUT_OF_MEMORY
;
765 p
[0] = svgtiny_PATH_MOVE
;
768 p
[3] = svgtiny_PATH_BEZIER
;
770 p
[5] = y
+ r
* KAPPA
;
771 p
[6] = x
+ r
* KAPPA
;
775 p
[10] = svgtiny_PATH_BEZIER
;
776 p
[11] = x
- r
* KAPPA
;
779 p
[14] = y
+ r
* KAPPA
;
782 p
[17] = svgtiny_PATH_BEZIER
;
784 p
[19] = y
- r
* KAPPA
;
785 p
[20] = x
- r
* KAPPA
;
789 p
[24] = svgtiny_PATH_BEZIER
;
790 p
[25] = x
+ r
* KAPPA
;
793 p
[28] = y
- r
* KAPPA
;
796 p
[31] = svgtiny_PATH_CLOSE
;
798 err
= svgtiny_add_path(p
, 32, &state
);
800 svgtiny_cleanup_state_local(&state
);
807 * Parse an <ellipse> element node.
810 svgtiny_code
svgtiny_parse_ellipse(dom_element
*ellipse
,
811 struct svgtiny_parse_state state
)
814 float x
= 0, y
= 0, rx
= -1, ry
= -1;
819 svgtiny_setup_state_local(&state
);
821 exc
= dom_element_get_attribute(ellipse
, state
.interned_cx
, &attr
);
822 if (exc
!= DOM_NO_ERR
) {
823 svgtiny_cleanup_state_local(&state
);
824 return svgtiny_LIBDOM_ERROR
;
827 x
= svgtiny_parse_length(attr
, state
.viewport_width
, state
);
829 dom_string_unref(attr
);
831 exc
= dom_element_get_attribute(ellipse
, state
.interned_cy
, &attr
);
832 if (exc
!= DOM_NO_ERR
) {
833 svgtiny_cleanup_state_local(&state
);
834 return svgtiny_LIBDOM_ERROR
;
837 y
= svgtiny_parse_length(attr
, state
.viewport_height
, state
);
839 dom_string_unref(attr
);
841 exc
= dom_element_get_attribute(ellipse
, state
.interned_rx
, &attr
);
842 if (exc
!= DOM_NO_ERR
) {
843 svgtiny_cleanup_state_local(&state
);
844 return svgtiny_LIBDOM_ERROR
;
847 rx
= svgtiny_parse_length(attr
, state
.viewport_width
, state
);
849 dom_string_unref(attr
);
851 exc
= dom_element_get_attribute(ellipse
, state
.interned_ry
, &attr
);
852 if (exc
!= DOM_NO_ERR
) {
853 svgtiny_cleanup_state_local(&state
);
854 return svgtiny_LIBDOM_ERROR
;
857 ry
= svgtiny_parse_length(attr
, state
.viewport_width
, state
);
859 dom_string_unref(attr
);
861 svgtiny_parse_paint_attributes(ellipse
, &state
);
862 svgtiny_parse_transform_attributes(ellipse
, &state
);
864 if (rx
< 0 || ry
< 0) {
865 state
.diagram
->error_line
= -1; /* ellipse->line; */
866 state
.diagram
->error_message
= "ellipse: rx or ry missing "
868 svgtiny_cleanup_state_local(&state
);
869 return svgtiny_SVG_ERROR
;
871 if (rx
== 0 || ry
== 0) {
872 svgtiny_cleanup_state_local(&state
);
876 p
= malloc(32 * sizeof p
[0]);
878 svgtiny_cleanup_state_local(&state
);
879 return svgtiny_OUT_OF_MEMORY
;
882 p
[0] = svgtiny_PATH_MOVE
;
885 p
[3] = svgtiny_PATH_BEZIER
;
887 p
[5] = y
+ ry
* KAPPA
;
888 p
[6] = x
+ rx
* KAPPA
;
892 p
[10] = svgtiny_PATH_BEZIER
;
893 p
[11] = x
- rx
* KAPPA
;
896 p
[14] = y
+ ry
* KAPPA
;
899 p
[17] = svgtiny_PATH_BEZIER
;
901 p
[19] = y
- ry
* KAPPA
;
902 p
[20] = x
- rx
* KAPPA
;
906 p
[24] = svgtiny_PATH_BEZIER
;
907 p
[25] = x
+ rx
* KAPPA
;
910 p
[28] = y
- ry
* KAPPA
;
913 p
[31] = svgtiny_PATH_CLOSE
;
915 err
= svgtiny_add_path(p
, 32, &state
);
917 svgtiny_cleanup_state_local(&state
);
924 * Parse a <line> element node.
927 svgtiny_code
svgtiny_parse_line(dom_element
*line
,
928 struct svgtiny_parse_state state
)
931 float x1
= 0, y1
= 0, x2
= 0, y2
= 0;
936 svgtiny_setup_state_local(&state
);
938 exc
= dom_element_get_attribute(line
, state
.interned_x1
, &attr
);
939 if (exc
!= DOM_NO_ERR
) {
940 svgtiny_cleanup_state_local(&state
);
941 return svgtiny_LIBDOM_ERROR
;
944 x1
= svgtiny_parse_length(attr
, state
.viewport_width
, state
);
946 dom_string_unref(attr
);
948 exc
= dom_element_get_attribute(line
, state
.interned_y1
, &attr
);
949 if (exc
!= DOM_NO_ERR
) {
950 svgtiny_cleanup_state_local(&state
);
951 return svgtiny_LIBDOM_ERROR
;
954 y1
= svgtiny_parse_length(attr
, state
.viewport_height
, state
);
956 dom_string_unref(attr
);
958 exc
= dom_element_get_attribute(line
, state
.interned_x2
, &attr
);
959 if (exc
!= DOM_NO_ERR
) {
960 svgtiny_cleanup_state_local(&state
);
961 return svgtiny_LIBDOM_ERROR
;
964 x2
= svgtiny_parse_length(attr
, state
.viewport_width
, state
);
966 dom_string_unref(attr
);
968 exc
= dom_element_get_attribute(line
, state
.interned_y2
, &attr
);
969 if (exc
!= DOM_NO_ERR
) {
970 svgtiny_cleanup_state_local(&state
);
971 return svgtiny_LIBDOM_ERROR
;
974 y2
= svgtiny_parse_length(attr
, state
.viewport_height
, state
);
976 dom_string_unref(attr
);
978 svgtiny_parse_paint_attributes(line
, &state
);
979 svgtiny_parse_transform_attributes(line
, &state
);
981 p
= malloc(7 * sizeof p
[0]);
983 svgtiny_cleanup_state_local(&state
);
984 return svgtiny_OUT_OF_MEMORY
;
987 p
[0] = svgtiny_PATH_MOVE
;
990 p
[3] = svgtiny_PATH_LINE
;
993 p
[6] = svgtiny_PATH_CLOSE
;
995 err
= svgtiny_add_path(p
, 7, &state
);
997 svgtiny_cleanup_state_local(&state
);
1004 * Parse a <polyline> or <polygon> element node.
1006 * http://www.w3.org/TR/SVG11/shapes#PolylineElement
1007 * http://www.w3.org/TR/SVG11/shapes#PolygonElement
1010 svgtiny_code
svgtiny_parse_poly(dom_element
*poly
,
1011 struct svgtiny_parse_state state
, bool polygon
)
1014 dom_string
*points_str
;
1020 svgtiny_setup_state_local(&state
);
1022 svgtiny_parse_paint_attributes(poly
, &state
);
1023 svgtiny_parse_transform_attributes(poly
, &state
);
1025 exc
= dom_element_get_attribute(poly
, state
.interned_points
,
1027 if (exc
!= DOM_NO_ERR
) {
1028 svgtiny_cleanup_state_local(&state
);
1029 return svgtiny_LIBDOM_ERROR
;
1032 if (points_str
== NULL
) {
1033 state
.diagram
->error_line
= -1; /* poly->line; */
1034 state
.diagram
->error_message
=
1035 "polyline/polygon: missing points attribute";
1036 svgtiny_cleanup_state_local(&state
);
1037 return svgtiny_SVG_ERROR
;
1040 s
= points
= strndup(dom_string_data(points_str
),
1041 dom_string_byte_length(points_str
));
1042 dom_string_unref(points_str
);
1043 /* read points attribute */
1045 svgtiny_cleanup_state_local(&state
);
1046 return svgtiny_OUT_OF_MEMORY
;
1048 /* allocate space for path: it will never have more elements than s */
1049 p
= malloc(sizeof p
[0] * strlen(s
));
1052 svgtiny_cleanup_state_local(&state
);
1053 return svgtiny_OUT_OF_MEMORY
;
1056 /* parse s and build path */
1057 for (i
= 0; s
[i
]; i
++)
1065 if (sscanf(s
, "%f %f %n", &x
, &y
, &n
) == 2) {
1067 p
[i
++] = svgtiny_PATH_MOVE
;
1069 p
[i
++] = svgtiny_PATH_LINE
;
1078 p
[i
++] = svgtiny_PATH_CLOSE
;
1082 err
= svgtiny_add_path(p
, i
, &state
);
1084 svgtiny_cleanup_state_local(&state
);
1091 * Parse a <text> or <tspan> element node.
1094 svgtiny_code
svgtiny_parse_text(dom_element
*text
,
1095 struct svgtiny_parse_state state
)
1097 float x
, y
, width
, height
;
1102 svgtiny_setup_state_local(&state
);
1104 svgtiny_parse_position_attributes(text
, state
,
1105 &x
, &y
, &width
, &height
);
1106 svgtiny_parse_font_attributes(text
, &state
);
1107 svgtiny_parse_transform_attributes(text
, &state
);
1109 px
= state
.ctm
.a
* x
+ state
.ctm
.c
* y
+ state
.ctm
.e
;
1110 py
= state
.ctm
.b
* x
+ state
.ctm
.d
* y
+ state
.ctm
.f
;
1111 /* state.ctm.e = px - state.origin_x; */
1112 /* state.ctm.f = py - state.origin_y; */
1114 /*struct css_style style = state.style;
1115 style.font_size.value.length.value *= state.ctm.a;*/
1117 exc
= dom_node_get_first_child(text
, &child
);
1118 if (exc
!= DOM_NO_ERR
) {
1119 return svgtiny_LIBDOM_ERROR
;
1120 svgtiny_cleanup_state_local(&state
);
1122 while (child
!= NULL
) {
1124 dom_node_type nodetype
;
1125 svgtiny_code code
= svgtiny_OK
;
1127 exc
= dom_node_get_node_type(child
, &nodetype
);
1128 if (exc
!= DOM_NO_ERR
) {
1129 dom_node_unref(child
);
1130 svgtiny_cleanup_state_local(&state
);
1131 return svgtiny_LIBDOM_ERROR
;
1133 if (nodetype
== DOM_ELEMENT_NODE
) {
1134 dom_string
*nodename
;
1135 exc
= dom_node_get_node_name(child
, &nodename
);
1136 if (exc
!= DOM_NO_ERR
) {
1137 dom_node_unref(child
);
1138 svgtiny_cleanup_state_local(&state
);
1139 return svgtiny_LIBDOM_ERROR
;
1141 if (dom_string_caseless_isequal(nodename
,
1142 state
.interned_tspan
))
1143 code
= svgtiny_parse_text((dom_element
*)child
,
1145 dom_string_unref(nodename
);
1146 } else if (nodetype
== DOM_TEXT_NODE
) {
1147 struct svgtiny_shape
*shape
= svgtiny_add_shape(&state
);
1148 dom_string
*content
;
1149 if (shape
== NULL
) {
1150 dom_node_unref(child
);
1151 svgtiny_cleanup_state_local(&state
);
1152 return svgtiny_OUT_OF_MEMORY
;
1154 exc
= dom_text_get_whole_text(child
, &content
);
1155 if (exc
!= DOM_NO_ERR
) {
1156 dom_node_unref(child
);
1157 svgtiny_cleanup_state_local(&state
);
1158 return svgtiny_LIBDOM_ERROR
;
1160 if (content
!= NULL
) {
1161 shape
->text
= strndup(dom_string_data(content
),
1162 dom_string_byte_length(content
));
1163 dom_string_unref(content
);
1165 shape
->text
= strdup("");
1169 state
.diagram
->shape_count
++;
1172 if (code
!= svgtiny_OK
) {
1173 dom_node_unref(child
);
1174 svgtiny_cleanup_state_local(&state
);
1177 exc
= dom_node_get_next_sibling(child
, &next
);
1178 dom_node_unref(child
);
1179 if (exc
!= DOM_NO_ERR
) {
1180 svgtiny_cleanup_state_local(&state
);
1181 return svgtiny_LIBDOM_ERROR
;
1186 svgtiny_cleanup_state_local(&state
);
1193 * Parse x, y, width, and height attributes, if present.
1196 void svgtiny_parse_position_attributes(dom_element
*node
,
1197 const struct svgtiny_parse_state state
,
1198 float *x
, float *y
, float *width
, float *height
)
1205 *width
= state
.viewport_width
;
1206 *height
= state
.viewport_height
;
1208 exc
= dom_element_get_attribute(node
, state
.interned_x
, &attr
);
1209 if (exc
== DOM_NO_ERR
&& attr
!= NULL
) {
1210 *x
= svgtiny_parse_length(attr
, state
.viewport_width
, state
);
1211 dom_string_unref(attr
);
1214 exc
= dom_element_get_attribute(node
, state
.interned_y
, &attr
);
1215 if (exc
== DOM_NO_ERR
&& attr
!= NULL
) {
1216 *y
= svgtiny_parse_length(attr
, state
.viewport_height
, state
);
1217 dom_string_unref(attr
);
1220 exc
= dom_element_get_attribute(node
, state
.interned_width
, &attr
);
1221 if (exc
== DOM_NO_ERR
&& attr
!= NULL
) {
1222 *width
= svgtiny_parse_length(attr
, state
.viewport_width
,
1224 dom_string_unref(attr
);
1227 exc
= dom_element_get_attribute(node
, state
.interned_height
, &attr
);
1228 if (exc
== DOM_NO_ERR
&& attr
!= NULL
) {
1229 *height
= svgtiny_parse_length(attr
, state
.viewport_height
,
1231 dom_string_unref(attr
);
1237 * Parse a length as a number of pixels.
1240 static float _svgtiny_parse_length(const char *s
, int viewport_size
,
1241 const struct svgtiny_parse_state state
)
1243 int num_length
= strspn(s
, "0123456789+-.");
1244 const char *unit
= s
+ num_length
;
1245 float n
= atof((const char *) s
);
1246 float font_size
= 20; /*css_len2px(&state.style.font_size.value.length, 0);*/
1252 } else if (unit
[0] == '%') {
1253 return n
/ 100.0 * viewport_size
;
1254 } else if (unit
[0] == 'e' && unit
[1] == 'm') {
1255 return n
* font_size
;
1256 } else if (unit
[0] == 'e' && unit
[1] == 'x') {
1257 return n
/ 2.0 * font_size
;
1258 } else if (unit
[0] == 'p' && unit
[1] == 'x') {
1260 } else if (unit
[0] == 'p' && unit
[1] == 't') {
1262 } else if (unit
[0] == 'p' && unit
[1] == 'c') {
1264 } else if (unit
[0] == 'm' && unit
[1] == 'm') {
1265 return n
* 3.543307;
1266 } else if (unit
[0] == 'c' && unit
[1] == 'm') {
1267 return n
* 35.43307;
1268 } else if (unit
[0] == 'i' && unit
[1] == 'n') {
1275 float svgtiny_parse_length(dom_string
*s
, int viewport_size
,
1276 const struct svgtiny_parse_state state
)
1278 char *ss
= strndup(dom_string_data(s
), dom_string_byte_length(s
));
1279 float ret
= _svgtiny_parse_length(ss
, viewport_size
, state
);
1285 * Parse paint attributes, if present.
1288 void svgtiny_parse_paint_attributes(dom_element
*node
,
1289 struct svgtiny_parse_state
*state
)
1294 exc
= dom_element_get_attribute(node
, state
->interned_fill
, &attr
);
1295 if (exc
== DOM_NO_ERR
&& attr
!= NULL
) {
1296 svgtiny_parse_color(attr
, &state
->fill
, state
);
1297 dom_string_unref(attr
);
1300 exc
= dom_element_get_attribute(node
, state
->interned_stroke
, &attr
);
1301 if (exc
== DOM_NO_ERR
&& attr
!= NULL
) {
1302 svgtiny_parse_color(attr
, &state
->stroke
, state
);
1303 dom_string_unref(attr
);
1306 exc
= dom_element_get_attribute(node
, state
->interned_stroke_width
, &attr
);
1307 if (exc
== DOM_NO_ERR
&& attr
!= NULL
) {
1308 state
->stroke_width
= svgtiny_parse_length(attr
,
1309 state
->viewport_width
, *state
);
1310 dom_string_unref(attr
);
1313 exc
= dom_element_get_attribute(node
, state
->interned_style
, &attr
);
1314 if (exc
== DOM_NO_ERR
&& attr
!= NULL
) {
1315 char *style
= strndup(dom_string_data(attr
),
1316 dom_string_byte_length(attr
));
1319 if ((s
= strstr(style
, "fill:"))) {
1323 value
= strndup(s
, strcspn(s
, "; "));
1324 _svgtiny_parse_color(value
, &state
->fill
, state
);
1327 if ((s
= strstr(style
, "stroke:"))) {
1331 value
= strndup(s
, strcspn(s
, "; "));
1332 _svgtiny_parse_color(value
, &state
->stroke
, state
);
1335 if ((s
= strstr(style
, "stroke-width:"))) {
1339 value
= strndup(s
, strcspn(s
, "; "));
1340 state
->stroke_width
= _svgtiny_parse_length(value
,
1341 state
->viewport_width
, *state
);
1345 dom_string_unref(attr
);
1354 static void _svgtiny_parse_color(const char *s
, svgtiny_colour
*c
,
1355 struct svgtiny_parse_state
*state
)
1357 unsigned int r
, g
, b
;
1359 size_t len
= strlen(s
);
1360 char *id
= 0, *rparen
;
1362 if (len
== 4 && s
[0] == '#') {
1363 if (sscanf(s
+ 1, "%1x%1x%1x", &r
, &g
, &b
) == 3)
1364 *c
= svgtiny_RGB(r
| r
<< 4, g
| g
<< 4, b
| b
<< 4);
1366 } else if (len
== 7 && s
[0] == '#') {
1367 if (sscanf(s
+ 1, "%2x%2x%2x", &r
, &g
, &b
) == 3)
1368 *c
= svgtiny_RGB(r
, g
, b
);
1370 } else if (10 <= len
&& s
[0] == 'r' && s
[1] == 'g' && s
[2] == 'b' &&
1371 s
[3] == '(' && s
[len
- 1] == ')') {
1372 if (sscanf(s
+ 4, "%u,%u,%u", &r
, &g
, &b
) == 3)
1373 *c
= svgtiny_RGB(r
, g
, b
);
1374 else if (sscanf(s
+ 4, "%f%%,%f%%,%f%%", &rf
, &gf
, &bf
) == 3) {
1378 *c
= svgtiny_RGB(r
, g
, b
);
1381 } else if (len
== 4 && strcmp(s
, "none") == 0) {
1382 *c
= svgtiny_TRANSPARENT
;
1384 } else if (5 < len
&& s
[0] == 'u' && s
[1] == 'r' && s
[2] == 'l' &&
1390 rparen
= strchr(id
, ')');
1393 svgtiny_find_gradient(id
, state
);
1395 if (state
->linear_gradient_stop_count
== 0)
1396 *c
= svgtiny_TRANSPARENT
;
1397 else if (state
->linear_gradient_stop_count
== 1)
1398 *c
= state
->gradient_stop
[0].color
;
1400 *c
= svgtiny_LINEAR_GRADIENT
;
1404 const struct svgtiny_named_color
*named_color
;
1405 named_color
= svgtiny_color_lookup(s
, strlen(s
));
1407 *c
= named_color
->color
;
1411 void svgtiny_parse_color(dom_string
*s
, svgtiny_colour
*c
,
1412 struct svgtiny_parse_state
*state
)
1414 char *ss
= strndup(dom_string_data(s
), dom_string_byte_length(s
));
1415 _svgtiny_parse_color(ss
, c
, state
);
1420 * Parse font attributes, if present.
1423 void svgtiny_parse_font_attributes(dom_element
*node
,
1424 struct svgtiny_parse_state
*state
)
1426 /* TODO: Implement this, it never used to be */
1429 #ifdef WRITTEN_THIS_PROPERLY
1430 const xmlAttr
*attr
;
1434 for (attr
= node
->properties
; attr
; attr
= attr
->next
) {
1435 if (strcmp((const char *) attr
->name
, "font-size") == 0) {
1436 /*if (css_parse_length(
1437 (const char *) attr->children->content,
1438 &state->style.font_size.value.length,
1440 state->style.font_size.size =
1441 CSS_FONT_SIZE_LENGTH;
1450 * Parse transform attributes, if present.
1452 * http://www.w3.org/TR/SVG11/coords#TransformAttribute
1455 void svgtiny_parse_transform_attributes(dom_element
*node
,
1456 struct svgtiny_parse_state
*state
)
1462 exc
= dom_element_get_attribute(node
, state
->interned_transform
,
1464 if (exc
== DOM_NO_ERR
&& attr
!= NULL
) {
1465 transform
= strndup(dom_string_data(attr
),
1466 dom_string_byte_length(attr
));
1467 svgtiny_parse_transform(transform
, &state
->ctm
.a
, &state
->ctm
.b
,
1468 &state
->ctm
.c
, &state
->ctm
.d
,
1469 &state
->ctm
.e
, &state
->ctm
.f
);
1471 dom_string_unref(attr
);
1477 * Parse a transform string.
1480 void svgtiny_parse_transform(char *s
, float *ma
, float *mb
,
1481 float *mc
, float *md
, float *me
, float *mf
)
1483 float a
, b
, c
, d
, e
, f
;
1484 float za
, zb
, zc
, zd
, ze
, zf
;
1489 for (i
= 0; s
[i
]; i
++)
1497 if (sscanf(s
, "matrix (%f %f %f %f %f %f) %n",
1498 &a
, &b
, &c
, &d
, &e
, &f
, &n
) == 6)
1500 else if (sscanf(s
, "translate (%f %f) %n",
1503 else if (sscanf(s
, "translate (%f) %n",
1506 else if (sscanf(s
, "scale (%f %f) %n",
1509 else if (sscanf(s
, "scale (%f) %n",
1512 else if (sscanf(s
, "rotate (%f %f %f) %n",
1513 &angle
, &x
, &y
, &n
) == 3) {
1514 angle
= angle
/ 180 * M_PI
;
1519 e
= -x
* cos(angle
) + y
* sin(angle
) + x
;
1520 f
= -x
* sin(angle
) - y
* cos(angle
) + y
;
1521 } else if (sscanf(s
, "rotate (%f) %n",
1523 angle
= angle
/ 180 * M_PI
;
1528 } else if (sscanf(s
, "skewX (%f) %n",
1530 angle
= angle
/ 180 * M_PI
;
1532 } else if (sscanf(s
, "skewY (%f) %n",
1534 angle
= angle
/ 180 * M_PI
;
1538 za
= *ma
* a
+ *mc
* b
;
1539 zb
= *mb
* a
+ *md
* b
;
1540 zc
= *ma
* c
+ *mc
* d
;
1541 zd
= *mb
* c
+ *md
* d
;
1542 ze
= *ma
* e
+ *mc
* f
+ *me
;
1543 zf
= *mb
* e
+ *md
* f
+ *mf
;
1556 * Add a path to the svgtiny_diagram.
1559 svgtiny_code
svgtiny_add_path(float *p
, unsigned int n
,
1560 struct svgtiny_parse_state
*state
)
1562 struct svgtiny_shape
*shape
;
1564 if (state
->fill
== svgtiny_LINEAR_GRADIENT
)
1565 return svgtiny_add_path_linear_gradient(p
, n
, state
);
1567 svgtiny_transform_path(p
, n
, state
);
1569 shape
= svgtiny_add_shape(state
);
1572 return svgtiny_OUT_OF_MEMORY
;
1575 shape
->path_length
= n
;
1576 state
->diagram
->shape_count
++;
1583 * Add a svgtiny_shape to the svgtiny_diagram.
1586 struct svgtiny_shape
*svgtiny_add_shape(struct svgtiny_parse_state
*state
)
1588 struct svgtiny_shape
*shape
= realloc(state
->diagram
->shape
,
1589 (state
->diagram
->shape_count
+ 1) *
1590 sizeof (state
->diagram
->shape
[0]));
1593 state
->diagram
->shape
= shape
;
1595 shape
+= state
->diagram
->shape_count
;
1597 shape
->path_length
= 0;
1599 shape
->fill
= state
->fill
;
1600 shape
->stroke
= state
->stroke
;
1601 shape
->stroke_width
= lroundf((float) state
->stroke_width
*
1602 (state
->ctm
.a
+ state
->ctm
.d
) / 2.0);
1603 if (0 < state
->stroke_width
&& shape
->stroke_width
== 0)
1604 shape
->stroke_width
= 1;
1611 * Apply the current transformation matrix to a path.
1614 void svgtiny_transform_path(float *p
, unsigned int n
,
1615 struct svgtiny_parse_state
*state
)
1619 for (j
= 0; j
!= n
; ) {
1620 unsigned int points
= 0;
1622 switch ((int) p
[j
]) {
1623 case svgtiny_PATH_MOVE
:
1624 case svgtiny_PATH_LINE
:
1627 case svgtiny_PATH_CLOSE
:
1630 case svgtiny_PATH_BEZIER
:
1637 for (k
= 0; k
!= points
; k
++) {
1638 float x0
= p
[j
], y0
= p
[j
+ 1];
1639 float x
= state
->ctm
.a
* x0
+ state
->ctm
.c
* y0
+
1641 float y
= state
->ctm
.b
* x0
+ state
->ctm
.d
* y0
+
1652 * Free all memory used by a diagram.
1655 void svgtiny_free(struct svgtiny_diagram
*svg
)
1660 for (i
= 0; i
!= svg
->shape_count
; i
++) {
1661 free(svg
->shape
[i
].path
);
1662 free(svg
->shape
[i
].text
);
1670 #ifndef HAVE_STRNDUP
1671 char *svgtiny_strndup(const char *s
, size_t n
)
1676 for (len
= 0; len
!= n
&& s
[len
]; len
++)
1679 s2
= malloc(len
+ 1);