]> gitweb.michael.orlitzky.com - libsvgtiny.git/blob - src/svgtiny.c
6215b04adb316b6baa971ac13d6ba5771fdc4b38
[libsvgtiny.git] / src / svgtiny.c
1 /*
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>
7 */
8
9 #include <assert.h>
10 #include <math.h>
11 #include <setjmp.h>
12 #include <stdbool.h>
13 #include <stdio.h>
14 #include <stdlib.h>
15 #include <string.h>
16
17 #include <dom/dom.h>
18 #include <dom/bindings/xml/xmlparser.h>
19
20 #include "svgtiny.h"
21 #include "svgtiny_internal.h"
22
23 #ifndef M_PI
24 #define M_PI 3.14159265358979323846
25 #endif
26
27 #define KAPPA 0.5522847498
28
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);
58
59 /**
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.
63 */
64 static void svgtiny_setup_state_local(struct svgtiny_parse_state *state)
65 {
66 if (state->gradient_x1 != NULL) {
67 dom_string_ref(state->gradient_x1);
68 }
69 if (state->gradient_y1 != NULL) {
70 dom_string_ref(state->gradient_y1);
71 }
72 if (state->gradient_x2 != NULL) {
73 dom_string_ref(state->gradient_x2);
74 }
75 if (state->gradient_y2 != NULL) {
76 dom_string_ref(state->gradient_y2);
77 }
78 }
79
80 /**
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.
84 */
85 static void svgtiny_cleanup_state_local(struct svgtiny_parse_state *state)
86 {
87 if (state->gradient_x1 != NULL) {
88 dom_string_unref(state->gradient_x1);
89 state->gradient_x1 = NULL;
90 }
91 if (state->gradient_y1 != NULL) {
92 dom_string_unref(state->gradient_y1);
93 state->gradient_y1 = NULL;
94 }
95 if (state->gradient_x2 != NULL) {
96 dom_string_unref(state->gradient_x2);
97 state->gradient_x2 = NULL;
98 }
99 if (state->gradient_y2 != NULL) {
100 dom_string_unref(state->gradient_y2);
101 state->gradient_y2 = NULL;
102 }
103 }
104
105
106 /**
107 * Create a new svgtiny_diagram structure.
108 */
109
110 struct svgtiny_diagram *svgtiny_create(void)
111 {
112 struct svgtiny_diagram *diagram;
113
114 diagram = calloc(sizeof(*diagram), 1);
115 if (!diagram)
116 return 0;
117
118 return diagram;
119 free(diagram);
120 return NULL;
121 }
122
123 static void ignore_msg(uint32_t severity, void *ctx, const char *msg, ...)
124 {
125 UNUSED(severity);
126 UNUSED(ctx);
127 UNUSED(msg);
128 }
129
130 /**
131 * Parse a block of memory into a svgtiny_diagram.
132 */
133
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)
137 {
138 dom_document *document;
139 dom_exception exc;
140 dom_xml_parser *parser;
141 dom_xml_error err;
142 dom_element *svg;
143 dom_string *svg_name;
144 lwc_string *svg_name_lwc;
145 struct svgtiny_parse_state state;
146 float x, y, width, height;
147 svgtiny_code code;
148
149 assert(diagram);
150 assert(buffer);
151 assert(url);
152
153 UNUSED(url);
154
155 state.gradient_x1 = NULL;
156 state.gradient_y1 = NULL;
157 state.gradient_x2 = NULL;
158 state.gradient_y2 = NULL;
159
160 parser = dom_xml_parser_create(NULL, NULL,
161 ignore_msg, NULL, &document);
162
163 if (parser == NULL)
164 return svgtiny_LIBDOM_ERROR;
165
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;
171 }
172
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;
178 }
179
180 /* We're done parsing, drop the parser.
181 * We now own the document entirely.
182 */
183 dom_xml_parser_destroy(parser);
184
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;
190 }
191 exc = dom_node_get_node_name(svg, &svg_name);
192 if (exc != DOM_NO_ERR) {
193 dom_node_unref(svg);
194 dom_node_unref(document);
195 return svgtiny_LIBDOM_ERROR;
196 }
197 if (lwc_intern_string("svg", 3 /* SLEN("svg") */,
198 &svg_name_lwc) != lwc_error_ok) {
199 dom_string_unref(svg_name);
200 dom_node_unref(svg);
201 dom_node_unref(document);
202 return svgtiny_LIBDOM_ERROR;
203 }
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);
207 dom_node_unref(svg);
208 dom_node_unref(document);
209 return svgtiny_NOT_SVG;
210 }
211
212 lwc_string_unref(svg_name_lwc);
213 dom_string_unref(svg_name);
214
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;
221
222 #define SVGTINY_STRING_ACTION2(s,n) \
223 if (dom_string_create_interned((const uint8_t *) #n, \
224 strlen(#n), &state.interned_##s) \
225 != DOM_NO_ERR) { \
226 code = svgtiny_LIBDOM_ERROR; \
227 goto cleanup; \
228 }
229 #include "svgtiny_strings.h"
230 #undef SVGTINY_STRING_ACTION2
231
232 svgtiny_parse_position_attributes(svg, state, &x, &y, &width, &height);
233 diagram->width = width;
234 diagram->height = height;
235
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;*/
240 state.ctm.b = 0;
241 state.ctm.c = 0;
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;
251
252 /* parse tree */
253 code = svgtiny_parse_svg(svg, state);
254
255 dom_node_unref(svg);
256 dom_node_unref(document);
257
258 cleanup:
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
265 return code;
266 }
267
268
269 /**
270 * Parse a <svg> or <g> element node.
271 */
272
273 svgtiny_code svgtiny_parse_svg(dom_element *svg,
274 struct svgtiny_parse_state state)
275 {
276 float x, y, width, height;
277 dom_string *view_box;
278 dom_element *child;
279 dom_exception exc;
280
281 svgtiny_setup_state_local(&state);
282
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);
286
287 exc = dom_element_get_attribute(svg, state.interned_viewBox,
288 &view_box);
289 if (exc != DOM_NO_ERR) {
290 svgtiny_cleanup_state_local(&state);
291 return svgtiny_LIBDOM_ERROR;
292 }
293
294 if (view_box) {
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;
306 }
307 free(s);
308 dom_string_unref(view_box);
309 }
310
311 svgtiny_parse_transform_attributes(svg, &state);
312
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;
317 }
318 while (child != NULL) {
319 dom_element *next;
320 dom_node_type nodetype;
321 svgtiny_code code = svgtiny_OK;
322
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;
327 }
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;
335 }
336 if (dom_string_caseless_isequal(state.interned_svg,
337 nodename))
338 code = svgtiny_parse_svg(child, state);
339 else if (dom_string_caseless_isequal(state.interned_g,
340 nodename))
341 code = svgtiny_parse_svg(child, state);
342 else if (dom_string_caseless_isequal(state.interned_a,
343 nodename))
344 code = svgtiny_parse_svg(child, state);
345 else if (dom_string_caseless_isequal(state.interned_path,
346 nodename))
347 code = svgtiny_parse_path(child, state);
348 else if (dom_string_caseless_isequal(state.interned_rect,
349 nodename))
350 code = svgtiny_parse_rect(child, state);
351 else if (dom_string_caseless_isequal(state.interned_circle,
352 nodename))
353 code = svgtiny_parse_circle(child, state);
354 else if (dom_string_caseless_isequal(state.interned_ellipse,
355 nodename))
356 code = svgtiny_parse_ellipse(child, state);
357 else if (dom_string_caseless_isequal(state.interned_line,
358 nodename))
359 code = svgtiny_parse_line(child, state);
360 else if (dom_string_caseless_isequal(state.interned_polyline,
361 nodename))
362 code = svgtiny_parse_poly(child, state, false);
363 else if (dom_string_caseless_isequal(state.interned_polygon,
364 nodename))
365 code = svgtiny_parse_poly(child, state, true);
366 else if (dom_string_caseless_isequal(state.interned_text,
367 nodename))
368 code = svgtiny_parse_text(child, state);
369 dom_string_unref(nodename);
370 }
371 if (code != svgtiny_OK) {
372 dom_node_unref(child);
373 svgtiny_cleanup_state_local(&state);
374 return code;
375 }
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;
382 }
383 child = next;
384 }
385
386 svgtiny_cleanup_state_local(&state);
387 return svgtiny_OK;
388 }
389
390
391
392 /**
393 * Parse a <path> element node.
394 *
395 * http://www.w3.org/TR/SVG11/paths#PathElement
396 */
397
398 svgtiny_code svgtiny_parse_path(dom_element *path,
399 struct svgtiny_parse_state state)
400 {
401 svgtiny_code err;
402 dom_string *path_d_str;
403 dom_exception exc;
404 char *s, *path_d;
405 float *p; /* path elemets */
406 unsigned int palloc; /* number of path elements allocated */
407 unsigned int i;
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;
412
413 svgtiny_setup_state_local(&state);
414
415 svgtiny_parse_paint_attributes(path, &state);
416 svgtiny_parse_transform_attributes(path, &state);
417
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;
425 }
426
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;
432 }
433
434 /* empty path is permitted it just disables the path */
435 palloc = dom_string_byte_length(path_d_str);
436 if (palloc == 0) {
437 svgtiny_cleanup_state_local(&state);
438 return svgtiny_OK;
439 }
440
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);
444 if (s == NULL) {
445 svgtiny_cleanup_state_local(&state);
446 return svgtiny_OUT_OF_MEMORY;
447 }
448
449 /* ensure path element allocation is sensibly bounded */
450 if (palloc < 8) {
451 palloc = 8;
452 } else if (palloc > 64) {
453 palloc = palloc / 2;
454 }
455
456 /* allocate initial space for path elements */
457 p = malloc(sizeof p[0] * palloc);
458 if (p == NULL) {
459 free(path_d);
460 svgtiny_cleanup_state_local(&state);
461 return svgtiny_OUT_OF_MEMORY;
462 }
463
464 /* parse d and build path */
465 for (i = 0; s[i]; i++)
466 if (s[i] == ',')
467 s[i] = ' ';
468 i = 0;
469 while (*s) {
470 char command[2];
471 int plot_command;
472 float x, y, x1, y1, x2, y2, rx, ry, rotation, large_arc, sweep;
473 int n;
474
475 /* Ensure there is sufficient space for path elements */
476 if ((palloc - i) < 7) {
477 float *tp;
478 palloc = (palloc * 2) + (palloc / 2);
479 tp = realloc(p, sizeof p[0] * palloc);
480 if (tp == NULL) {
481 free(p);
482 free(path_d);
483 svgtiny_cleanup_state_local(&state);
484 return svgtiny_OUT_OF_MEMORY;
485 }
486 p = tp;
487 }
488
489
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;
495 else
496 plot_command = svgtiny_PATH_LINE;
497 do {
498 p[i++] = plot_command;
499 if ('a' <= *command) {
500 x += last_x;
501 y += last_y;
502 }
503 if (plot_command == svgtiny_PATH_MOVE) {
504 subpath_first_x = x;
505 subpath_first_y = y;
506 }
507 p[i++] = last_cubic_x = last_quad_x = last_x
508 = x;
509 p[i++] = last_cubic_y = last_quad_y = last_y
510 = y;
511 s += n;
512 plot_command = svgtiny_PATH_LINE;
513 } while (sscanf(s, "%f %f %n", &x, &y, &n) == 2);
514
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;
519 s += n;
520 last_cubic_x = last_quad_x = last_x = subpath_first_x;
521 last_cubic_y = last_quad_y = last_y = subpath_first_y;
522
523 /* horizontal lineto (H, h) (1 argument) */
524 } else if (sscanf(s, " %1[Hh] %f %n", command, &x, &n) == 2) {
525 /*LOG(("horizontal lineto"));*/
526 do {
527 p[i++] = svgtiny_PATH_LINE;
528 if (*command == 'h')
529 x += last_x;
530 p[i++] = last_cubic_x = last_quad_x = last_x
531 = x;
532 p[i++] = last_cubic_y = last_quad_y = last_y;
533 s += n;
534 } while (sscanf(s, "%f %n", &x, &n) == 1);
535
536 /* vertical lineto (V, v) (1 argument) */
537 } else if (sscanf(s, " %1[Vv] %f %n", command, &y, &n) == 2) {
538 /*LOG(("vertical lineto"));*/
539 do {
540 p[i++] = svgtiny_PATH_LINE;
541 if (*command == 'v')
542 y += last_y;
543 p[i++] = last_cubic_x = last_quad_x = last_x;
544 p[i++] = last_cubic_y = last_quad_y = last_y
545 = y;
546 s += n;
547 } while (sscanf(s, "%f %n", &x, &n) == 1);
548
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"));*/
553 do {
554 p[i++] = svgtiny_PATH_BEZIER;
555 if (*command == 'c') {
556 x1 += last_x;
557 y1 += last_y;
558 x2 += last_x;
559 y2 += last_y;
560 x += last_x;
561 y += last_y;
562 }
563 p[i++] = x1;
564 p[i++] = y1;
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;
569 s += n;
570 } while (sscanf(s, "%f %f %f %f %f %f %n",
571 &x1, &y1, &x2, &y2, &x, &y, &n) == 6);
572
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"));*/
577 do {
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') {
582 x2 += last_x;
583 y2 += last_y;
584 x += last_x;
585 y += last_y;
586 }
587 p[i++] = x1;
588 p[i++] = y1;
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;
593 s += n;
594 } while (sscanf(s, "%f %f %f %f %n",
595 &x2, &y2, &x, &y, &n) == 4);
596
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"));*/
601 do {
602 p[i++] = svgtiny_PATH_BEZIER;
603 last_quad_x = x1;
604 last_quad_y = y1;
605 if (*command == 'q') {
606 x1 += last_x;
607 y1 += last_y;
608 x += last_x;
609 y += last_y;
610 }
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;
617 s += n;
618 } while (sscanf(s, "%f %f %f %f %n",
619 &x1, &y1, &x, &y, &n) == 4);
620
621 /* shorthand/smooth quadratic Bezier curveto (T, t)
622 (2 arguments) */
623 } else if (sscanf(s, " %1[Tt] %f %f %n", command,
624 &x, &y, &n) == 3) {
625 /*LOG(("shorthand/smooth quadratic Bezier curveto"));*/
626 do {
627 p[i++] = svgtiny_PATH_BEZIER;
628 x1 = last_x + (last_x - last_quad_x);
629 y1 = last_y + (last_y - last_quad_y);
630 last_quad_x = x1;
631 last_quad_y = y1;
632 if (*command == 't') {
633 x1 += last_x;
634 y1 += last_y;
635 x += last_x;
636 y += last_y;
637 }
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;
644 s += n;
645 } while (sscanf(s, "%f %f %n",
646 &x, &y, &n) == 2);
647
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,
651 &x, &y, &n) == 8) {
652 do {
653 p[i++] = svgtiny_PATH_LINE;
654 if (*command == 'a') {
655 x += last_x;
656 y += last_y;
657 }
658 p[i++] = last_cubic_x = last_quad_x = last_x
659 = x;
660 p[i++] = last_cubic_y = last_quad_y = last_y
661 = y;
662 s += n;
663 } while (sscanf(s, "%f %f %f %f %f %f %f %n",
664 &rx, &ry, &rotation, &large_arc, &sweep,
665 &x, &y, &n) == 7);
666
667 } else {
668 fprintf(stderr, "parse failed at \"%s\"\n", s);
669 break;
670 }
671 }
672
673 free(path_d);
674
675 if (i <= 4) {
676 /* no real segments in path */
677 free(p);
678 svgtiny_cleanup_state_local(&state);
679 return svgtiny_OK;
680 }
681
682 /* resize path element array to not be over allocated */
683 if (palloc != i) {
684 float *tp;
685
686 /* try the resize, if it fails just continue to use the old
687 * allocation
688 */
689 tp = realloc(p, sizeof p[0] * i);
690 if (tp != NULL) {
691 p = tp;
692 }
693 }
694
695 err = svgtiny_add_path(p, i, &state);
696
697 svgtiny_cleanup_state_local(&state);
698
699 return err;
700 }
701
702
703 /**
704 * Parse a <rect> element node.
705 *
706 * http://www.w3.org/TR/SVG11/shapes#RectElement
707 */
708
709 svgtiny_code svgtiny_parse_rect(dom_element *rect,
710 struct svgtiny_parse_state state)
711 {
712 svgtiny_code err;
713 float x, y, width, height;
714 float *p;
715
716 svgtiny_setup_state_local(&state);
717
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);
722
723 p = malloc(13 * sizeof p[0]);
724 if (!p) {
725 svgtiny_cleanup_state_local(&state);
726 return svgtiny_OUT_OF_MEMORY;
727 }
728
729 p[0] = svgtiny_PATH_MOVE;
730 p[1] = x;
731 p[2] = y;
732 p[3] = svgtiny_PATH_LINE;
733 p[4] = x + width;
734 p[5] = y;
735 p[6] = svgtiny_PATH_LINE;
736 p[7] = x + width;
737 p[8] = y + height;
738 p[9] = svgtiny_PATH_LINE;
739 p[10] = x;
740 p[11] = y + height;
741 p[12] = svgtiny_PATH_CLOSE;
742
743 err = svgtiny_add_path(p, 13, &state);
744
745 svgtiny_cleanup_state_local(&state);
746
747 return err;
748 }
749
750
751 /**
752 * Parse a <circle> element node.
753 */
754
755 svgtiny_code svgtiny_parse_circle(dom_element *circle,
756 struct svgtiny_parse_state state)
757 {
758 svgtiny_code err;
759 float x = 0, y = 0, r = -1;
760 float *p;
761 dom_string *attr;
762 dom_exception exc;
763
764 svgtiny_setup_state_local(&state);
765
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;
770 }
771 if (attr != NULL) {
772 x = svgtiny_parse_length(attr, state.viewport_width, state);
773 }
774 dom_string_unref(attr);
775
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;
780 }
781 if (attr != NULL) {
782 y = svgtiny_parse_length(attr, state.viewport_height, state);
783 }
784 dom_string_unref(attr);
785
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;
790 }
791 if (attr != NULL) {
792 r = svgtiny_parse_length(attr, state.viewport_width, state);
793 }
794 dom_string_unref(attr);
795
796 svgtiny_parse_paint_attributes(circle, &state);
797 svgtiny_parse_transform_attributes(circle, &state);
798
799 if (r < 0) {
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;
804 }
805 if (r == 0) {
806 svgtiny_cleanup_state_local(&state);
807 return svgtiny_OK;
808 }
809
810 p = malloc(32 * sizeof p[0]);
811 if (!p) {
812 svgtiny_cleanup_state_local(&state);
813 return svgtiny_OUT_OF_MEMORY;
814 }
815
816 p[0] = svgtiny_PATH_MOVE;
817 p[1] = x + r;
818 p[2] = y;
819 p[3] = svgtiny_PATH_BEZIER;
820 p[4] = x + r;
821 p[5] = y + r * KAPPA;
822 p[6] = x + r * KAPPA;
823 p[7] = y + r;
824 p[8] = x;
825 p[9] = y + r;
826 p[10] = svgtiny_PATH_BEZIER;
827 p[11] = x - r * KAPPA;
828 p[12] = y + r;
829 p[13] = x - r;
830 p[14] = y + r * KAPPA;
831 p[15] = x - r;
832 p[16] = y;
833 p[17] = svgtiny_PATH_BEZIER;
834 p[18] = x - r;
835 p[19] = y - r * KAPPA;
836 p[20] = x - r * KAPPA;
837 p[21] = y - r;
838 p[22] = x;
839 p[23] = y - r;
840 p[24] = svgtiny_PATH_BEZIER;
841 p[25] = x + r * KAPPA;
842 p[26] = y - r;
843 p[27] = x + r;
844 p[28] = y - r * KAPPA;
845 p[29] = x + r;
846 p[30] = y;
847 p[31] = svgtiny_PATH_CLOSE;
848
849 err = svgtiny_add_path(p, 32, &state);
850
851 svgtiny_cleanup_state_local(&state);
852
853 return err;
854 }
855
856
857 /**
858 * Parse an <ellipse> element node.
859 */
860
861 svgtiny_code svgtiny_parse_ellipse(dom_element *ellipse,
862 struct svgtiny_parse_state state)
863 {
864 svgtiny_code err;
865 float x = 0, y = 0, rx = -1, ry = -1;
866 float *p;
867 dom_string *attr;
868 dom_exception exc;
869
870 svgtiny_setup_state_local(&state);
871
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;
876 }
877 if (attr != NULL) {
878 x = svgtiny_parse_length(attr, state.viewport_width, state);
879 }
880 dom_string_unref(attr);
881
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;
886 }
887 if (attr != NULL) {
888 y = svgtiny_parse_length(attr, state.viewport_height, state);
889 }
890 dom_string_unref(attr);
891
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;
896 }
897 if (attr != NULL) {
898 rx = svgtiny_parse_length(attr, state.viewport_width, state);
899 }
900 dom_string_unref(attr);
901
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;
906 }
907 if (attr != NULL) {
908 ry = svgtiny_parse_length(attr, state.viewport_width, state);
909 }
910 dom_string_unref(attr);
911
912 svgtiny_parse_paint_attributes(ellipse, &state);
913 svgtiny_parse_transform_attributes(ellipse, &state);
914
915 if (rx < 0 || ry < 0) {
916 state.diagram->error_line = -1; /* ellipse->line; */
917 state.diagram->error_message = "ellipse: rx or ry missing "
918 "or negative";
919 svgtiny_cleanup_state_local(&state);
920 return svgtiny_SVG_ERROR;
921 }
922 if (rx == 0 || ry == 0) {
923 svgtiny_cleanup_state_local(&state);
924 return svgtiny_OK;
925 }
926
927 p = malloc(32 * sizeof p[0]);
928 if (!p) {
929 svgtiny_cleanup_state_local(&state);
930 return svgtiny_OUT_OF_MEMORY;
931 }
932
933 p[0] = svgtiny_PATH_MOVE;
934 p[1] = x + rx;
935 p[2] = y;
936 p[3] = svgtiny_PATH_BEZIER;
937 p[4] = x + rx;
938 p[5] = y + ry * KAPPA;
939 p[6] = x + rx * KAPPA;
940 p[7] = y + ry;
941 p[8] = x;
942 p[9] = y + ry;
943 p[10] = svgtiny_PATH_BEZIER;
944 p[11] = x - rx * KAPPA;
945 p[12] = y + ry;
946 p[13] = x - rx;
947 p[14] = y + ry * KAPPA;
948 p[15] = x - rx;
949 p[16] = y;
950 p[17] = svgtiny_PATH_BEZIER;
951 p[18] = x - rx;
952 p[19] = y - ry * KAPPA;
953 p[20] = x - rx * KAPPA;
954 p[21] = y - ry;
955 p[22] = x;
956 p[23] = y - ry;
957 p[24] = svgtiny_PATH_BEZIER;
958 p[25] = x + rx * KAPPA;
959 p[26] = y - ry;
960 p[27] = x + rx;
961 p[28] = y - ry * KAPPA;
962 p[29] = x + rx;
963 p[30] = y;
964 p[31] = svgtiny_PATH_CLOSE;
965
966 err = svgtiny_add_path(p, 32, &state);
967
968 svgtiny_cleanup_state_local(&state);
969
970 return err;
971 }
972
973
974 /**
975 * Parse a <line> element node.
976 */
977
978 svgtiny_code svgtiny_parse_line(dom_element *line,
979 struct svgtiny_parse_state state)
980 {
981 svgtiny_code err;
982 float x1 = 0, y1 = 0, x2 = 0, y2 = 0;
983 float *p;
984 dom_string *attr;
985 dom_exception exc;
986
987 svgtiny_setup_state_local(&state);
988
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;
993 }
994 if (attr != NULL) {
995 x1 = svgtiny_parse_length(attr, state.viewport_width, state);
996 }
997 dom_string_unref(attr);
998
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;
1003 }
1004 if (attr != NULL) {
1005 y1 = svgtiny_parse_length(attr, state.viewport_height, state);
1006 }
1007 dom_string_unref(attr);
1008
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;
1013 }
1014 if (attr != NULL) {
1015 x2 = svgtiny_parse_length(attr, state.viewport_width, state);
1016 }
1017 dom_string_unref(attr);
1018
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;
1023 }
1024 if (attr != NULL) {
1025 y2 = svgtiny_parse_length(attr, state.viewport_height, state);
1026 }
1027 dom_string_unref(attr);
1028
1029 svgtiny_parse_paint_attributes(line, &state);
1030 svgtiny_parse_transform_attributes(line, &state);
1031
1032 p = malloc(7 * sizeof p[0]);
1033 if (!p) {
1034 svgtiny_cleanup_state_local(&state);
1035 return svgtiny_OUT_OF_MEMORY;
1036 }
1037
1038 p[0] = svgtiny_PATH_MOVE;
1039 p[1] = x1;
1040 p[2] = y1;
1041 p[3] = svgtiny_PATH_LINE;
1042 p[4] = x2;
1043 p[5] = y2;
1044 p[6] = svgtiny_PATH_CLOSE;
1045
1046 err = svgtiny_add_path(p, 7, &state);
1047
1048 svgtiny_cleanup_state_local(&state);
1049
1050 return err;
1051 }
1052
1053
1054 /**
1055 * Parse a <polyline> or <polygon> element node.
1056 *
1057 * http://www.w3.org/TR/SVG11/shapes#PolylineElement
1058 * http://www.w3.org/TR/SVG11/shapes#PolygonElement
1059 */
1060
1061 svgtiny_code svgtiny_parse_poly(dom_element *poly,
1062 struct svgtiny_parse_state state, bool polygon)
1063 {
1064 svgtiny_code err;
1065 dom_string *points_str;
1066 dom_exception exc;
1067 char *s, *points;
1068 float *p;
1069 unsigned int i;
1070
1071 svgtiny_setup_state_local(&state);
1072
1073 svgtiny_parse_paint_attributes(poly, &state);
1074 svgtiny_parse_transform_attributes(poly, &state);
1075
1076 exc = dom_element_get_attribute(poly, state.interned_points,
1077 &points_str);
1078 if (exc != DOM_NO_ERR) {
1079 svgtiny_cleanup_state_local(&state);
1080 return svgtiny_LIBDOM_ERROR;
1081 }
1082
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;
1089 }
1090
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 */
1095 if (s == NULL) {
1096 svgtiny_cleanup_state_local(&state);
1097 return svgtiny_OUT_OF_MEMORY;
1098 }
1099 /* allocate space for path: it will never have more elements than s */
1100 p = malloc(sizeof p[0] * strlen(s));
1101 if (!p) {
1102 free(points);
1103 svgtiny_cleanup_state_local(&state);
1104 return svgtiny_OUT_OF_MEMORY;
1105 }
1106
1107 /* parse s and build path */
1108 for (i = 0; s[i]; i++)
1109 if (s[i] == ',')
1110 s[i] = ' ';
1111 i = 0;
1112 while (*s) {
1113 float x, y;
1114 int n;
1115
1116 if (sscanf(s, "%f %f %n", &x, &y, &n) == 2) {
1117 if (i == 0)
1118 p[i++] = svgtiny_PATH_MOVE;
1119 else
1120 p[i++] = svgtiny_PATH_LINE;
1121 p[i++] = x;
1122 p[i++] = y;
1123 s += n;
1124 } else {
1125 break;
1126 }
1127 }
1128 if (polygon)
1129 p[i++] = svgtiny_PATH_CLOSE;
1130
1131 free(points);
1132
1133 err = svgtiny_add_path(p, i, &state);
1134
1135 svgtiny_cleanup_state_local(&state);
1136
1137 return err;
1138 }
1139
1140
1141 /**
1142 * Parse a <text> or <tspan> element node.
1143 */
1144
1145 svgtiny_code svgtiny_parse_text(dom_element *text,
1146 struct svgtiny_parse_state state)
1147 {
1148 float x, y, width, height;
1149 float px, py;
1150 dom_node *child;
1151 dom_exception exc;
1152
1153 svgtiny_setup_state_local(&state);
1154
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);
1159
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; */
1164
1165 /*struct css_style style = state.style;
1166 style.font_size.value.length.value *= state.ctm.a;*/
1167
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);
1172 }
1173 while (child != NULL) {
1174 dom_node *next;
1175 dom_node_type nodetype;
1176 svgtiny_code code = svgtiny_OK;
1177
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;
1183 }
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;
1191 }
1192 if (dom_string_caseless_isequal(nodename,
1193 state.interned_tspan))
1194 code = svgtiny_parse_text((dom_element *)child,
1195 state);
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;
1204 }
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;
1210 }
1211 if (content != NULL) {
1212 shape->text = strndup(dom_string_data(content),
1213 dom_string_byte_length(content));
1214 dom_string_unref(content);
1215 } else {
1216 shape->text = strdup("");
1217 }
1218 shape->text_x = px;
1219 shape->text_y = py;
1220 state.diagram->shape_count++;
1221 }
1222
1223 if (code != svgtiny_OK) {
1224 dom_node_unref(child);
1225 svgtiny_cleanup_state_local(&state);
1226 return code;
1227 }
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;
1233 }
1234 child = next;
1235 }
1236
1237 svgtiny_cleanup_state_local(&state);
1238
1239 return svgtiny_OK;
1240 }
1241
1242
1243 /**
1244 * Parse x, y, width, and height attributes, if present.
1245 */
1246
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)
1250 {
1251 dom_string *attr;
1252 dom_exception exc;
1253
1254 *x = 0;
1255 *y = 0;
1256 *width = state.viewport_width;
1257 *height = state.viewport_height;
1258
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);
1263 }
1264
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);
1269 }
1270
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,
1274 state);
1275 dom_string_unref(attr);
1276 }
1277
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,
1281 state);
1282 dom_string_unref(attr);
1283 }
1284 }
1285
1286
1287 /**
1288 * Parse a length as a number of pixels.
1289 */
1290
1291 static float _svgtiny_parse_length(const char *s, int viewport_size,
1292 const struct svgtiny_parse_state state)
1293 {
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);*/
1298
1299 UNUSED(state);
1300
1301 if (unit[0] == 0) {
1302 return n;
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') {
1310 return n;
1311 } else if (unit[0] == 'p' && unit[1] == 't') {
1312 return n * 1.25;
1313 } else if (unit[0] == 'p' && unit[1] == 'c') {
1314 return n * 15.0;
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') {
1320 return n * 90;
1321 }
1322
1323 return 0;
1324 }
1325
1326 float svgtiny_parse_length(dom_string *s, int viewport_size,
1327 const struct svgtiny_parse_state state)
1328 {
1329 char *ss = strndup(dom_string_data(s), dom_string_byte_length(s));
1330 float ret = _svgtiny_parse_length(ss, viewport_size, state);
1331 free(ss);
1332 return ret;
1333 }
1334
1335 /**
1336 * Parse paint attributes, if present.
1337 */
1338
1339 void svgtiny_parse_paint_attributes(dom_element *node,
1340 struct svgtiny_parse_state *state)
1341 {
1342 dom_string *attr;
1343 dom_exception exc;
1344
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);
1349 }
1350
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);
1355 }
1356
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);
1362 }
1363
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));
1368 const char *s;
1369 char *value;
1370 if ((s = strstr(style, "fill:"))) {
1371 s += 5;
1372 while (*s == ' ')
1373 s++;
1374 value = strndup(s, strcspn(s, "; "));
1375 _svgtiny_parse_color(value, &state->fill, state);
1376 free(value);
1377 }
1378 if ((s = strstr(style, "stroke:"))) {
1379 s += 7;
1380 while (*s == ' ')
1381 s++;
1382 value = strndup(s, strcspn(s, "; "));
1383 _svgtiny_parse_color(value, &state->stroke, state);
1384 free(value);
1385 }
1386 if ((s = strstr(style, "stroke-width:"))) {
1387 s += 13;
1388 while (*s == ' ')
1389 s++;
1390 value = strndup(s, strcspn(s, "; "));
1391 state->stroke_width = _svgtiny_parse_length(value,
1392 state->viewport_width, *state);
1393 free(value);
1394 }
1395 free(style);
1396 dom_string_unref(attr);
1397 }
1398 }
1399
1400
1401 /**
1402 * Parse a colour.
1403 */
1404
1405 static void _svgtiny_parse_color(const char *s, svgtiny_colour *c,
1406 struct svgtiny_parse_state *state)
1407 {
1408 unsigned int r, g, b;
1409 float rf, gf, bf;
1410 size_t len = strlen(s);
1411 char *id = 0, *rparen;
1412
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);
1416
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);
1420
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) {
1426 b = bf * 255 / 100;
1427 g = gf * 255 / 100;
1428 r = rf * 255 / 100;
1429 *c = svgtiny_RGB(r, g, b);
1430 }
1431
1432 } else if (len == 4 && strcmp(s, "none") == 0) {
1433 *c = svgtiny_TRANSPARENT;
1434
1435 } else if (5 < len && s[0] == 'u' && s[1] == 'r' && s[2] == 'l' &&
1436 s[3] == '(') {
1437 if (s[4] == '#') {
1438 id = strdup(s + 5);
1439 if (!id)
1440 return;
1441 rparen = strchr(id, ')');
1442 if (rparen)
1443 *rparen = 0;
1444 svgtiny_find_gradient(id, state);
1445 free(id);
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;
1450 else
1451 *c = svgtiny_LINEAR_GRADIENT;
1452 }
1453
1454 } else {
1455 const struct svgtiny_named_color *named_color;
1456 named_color = svgtiny_color_lookup(s, strlen(s));
1457 if (named_color)
1458 *c = named_color->color;
1459 }
1460 }
1461
1462 void svgtiny_parse_color(dom_string *s, svgtiny_colour *c,
1463 struct svgtiny_parse_state *state)
1464 {
1465 char *ss = strndup(dom_string_data(s), dom_string_byte_length(s));
1466 _svgtiny_parse_color(ss, c, state);
1467 free(ss);
1468 }
1469
1470 /**
1471 * Parse font attributes, if present.
1472 */
1473
1474 void svgtiny_parse_font_attributes(dom_element *node,
1475 struct svgtiny_parse_state *state)
1476 {
1477 /* TODO: Implement this, it never used to be */
1478 UNUSED(node);
1479 UNUSED(state);
1480 #ifdef WRITTEN_THIS_PROPERLY
1481 const xmlAttr *attr;
1482
1483 UNUSED(state);
1484
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,
1490 true, true)) {
1491 state->style.font_size.size =
1492 CSS_FONT_SIZE_LENGTH;
1493 }*/
1494 }
1495 }
1496 #endif
1497 }
1498
1499
1500 /**
1501 * Parse transform attributes, if present.
1502 *
1503 * http://www.w3.org/TR/SVG11/coords#TransformAttribute
1504 */
1505
1506 void svgtiny_parse_transform_attributes(dom_element *node,
1507 struct svgtiny_parse_state *state)
1508 {
1509 char *transform;
1510 dom_string *attr;
1511 dom_exception exc;
1512
1513 exc = dom_element_get_attribute(node, state->interned_transform,
1514 &attr);
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);
1521 free(transform);
1522 dom_string_unref(attr);
1523 }
1524 }
1525
1526
1527 /**
1528 * Parse a transform string.
1529 */
1530
1531 void svgtiny_parse_transform(char *s, float *ma, float *mb,
1532 float *mc, float *md, float *me, float *mf)
1533 {
1534 float a, b, c, d, e, f;
1535 float za, zb, zc, zd, ze, zf;
1536 float angle, x, y;
1537 int n;
1538 unsigned int i;
1539
1540 for (i = 0; s[i]; i++)
1541 if (s[i] == ',')
1542 s[i] = ' ';
1543
1544 while (*s) {
1545 a = d = 1;
1546 b = c = 0;
1547 e = f = 0;
1548 if ((sscanf(s, " matrix (%f %f %f %f %f %f ) %n",
1549 &a, &b, &c, &d, &e, &f, &n) == 6) && (n > 0))
1550 ;
1551 else if ((sscanf(s, " translate (%f %f ) %n",
1552 &e, &f, &n) == 2) && (n > 0))
1553 ;
1554 else if ((sscanf(s, " translate (%f ) %n",
1555 &e, &n) == 1) && (n > 0))
1556 ;
1557 else if ((sscanf(s, " scale (%f %f ) %n",
1558 &a, &d, &n) == 2) && (n > 0))
1559 ;
1560 else if ((sscanf(s, " scale (%f ) %n",
1561 &a, &n) == 1) && (n > 0))
1562 d = a;
1563 else if ((sscanf(s, " rotate (%f %f %f ) %n",
1564 &angle, &x, &y, &n) == 3) && (n > 0)) {
1565 angle = angle / 180 * M_PI;
1566 a = cos(angle);
1567 b = sin(angle);
1568 c = -sin(angle);
1569 d = cos(angle);
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;
1575 a = cos(angle);
1576 b = sin(angle);
1577 c = -sin(angle);
1578 d = cos(angle);
1579 } else if ((sscanf(s, " skewX (%f ) %n",
1580 &angle, &n) == 1) && (n > 0)) {
1581 angle = angle / 180 * M_PI;
1582 c = tan(angle);
1583 } else if ((sscanf(s, " skewY (%f ) %n",
1584 &angle, &n) == 1) && (n > 0)) {
1585 angle = angle / 180 * M_PI;
1586 b = tan(angle);
1587 } else
1588 break;
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;
1595 *ma = za;
1596 *mb = zb;
1597 *mc = zc;
1598 *md = zd;
1599 *me = ze;
1600 *mf = zf;
1601 s += n;
1602 }
1603 }
1604
1605
1606 /**
1607 * Add a path to the svgtiny_diagram.
1608 */
1609
1610 svgtiny_code svgtiny_add_path(float *p, unsigned int n,
1611 struct svgtiny_parse_state *state)
1612 {
1613 struct svgtiny_shape *shape;
1614
1615 if (state->fill == svgtiny_LINEAR_GRADIENT)
1616 return svgtiny_add_path_linear_gradient(p, n, state);
1617
1618 svgtiny_transform_path(p, n, state);
1619
1620 shape = svgtiny_add_shape(state);
1621 if (!shape) {
1622 free(p);
1623 return svgtiny_OUT_OF_MEMORY;
1624 }
1625 shape->path = p;
1626 shape->path_length = n;
1627 state->diagram->shape_count++;
1628
1629 return svgtiny_OK;
1630 }
1631
1632
1633 /**
1634 * Add a svgtiny_shape to the svgtiny_diagram.
1635 */
1636
1637 struct svgtiny_shape *svgtiny_add_shape(struct svgtiny_parse_state *state)
1638 {
1639 struct svgtiny_shape *shape = realloc(state->diagram->shape,
1640 (state->diagram->shape_count + 1) *
1641 sizeof (state->diagram->shape[0]));
1642 if (!shape)
1643 return 0;
1644 state->diagram->shape = shape;
1645
1646 shape += state->diagram->shape_count;
1647 shape->path = 0;
1648 shape->path_length = 0;
1649 shape->text = 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;
1656
1657 return shape;
1658 }
1659
1660
1661 /**
1662 * Apply the current transformation matrix to a path.
1663 */
1664
1665 void svgtiny_transform_path(float *p, unsigned int n,
1666 struct svgtiny_parse_state *state)
1667 {
1668 unsigned int j;
1669
1670 for (j = 0; j != n; ) {
1671 unsigned int points = 0;
1672 unsigned int k;
1673 switch ((int) p[j]) {
1674 case svgtiny_PATH_MOVE:
1675 case svgtiny_PATH_LINE:
1676 points = 1;
1677 break;
1678 case svgtiny_PATH_CLOSE:
1679 points = 0;
1680 break;
1681 case svgtiny_PATH_BEZIER:
1682 points = 3;
1683 break;
1684 default:
1685 assert(0);
1686 }
1687 j++;
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 +
1691 state->ctm.e;
1692 float y = state->ctm.b * x0 + state->ctm.d * y0 +
1693 state->ctm.f;
1694 p[j] = x;
1695 p[j + 1] = y;
1696 j += 2;
1697 }
1698 }
1699 }
1700
1701
1702 /**
1703 * Free all memory used by a diagram.
1704 */
1705
1706 void svgtiny_free(struct svgtiny_diagram *svg)
1707 {
1708 unsigned int i;
1709 assert(svg);
1710
1711 for (i = 0; i != svg->shape_count; i++) {
1712 free(svg->shape[i].path);
1713 free(svg->shape[i].text);
1714 }
1715
1716 free(svg->shape);
1717
1718 free(svg);
1719 }
1720
1721 #ifndef HAVE_STRNDUP
1722 char *svgtiny_strndup(const char *s, size_t n)
1723 {
1724 size_t len;
1725 char *s2;
1726
1727 for (len = 0; len != n && s[len]; len++)
1728 continue;
1729
1730 s2 = malloc(len + 1);
1731 if (s2 == NULL)
1732 return NULL;
1733
1734 memcpy(s2, s, len);
1735 s2[len] = '\0';
1736
1737 return s2;
1738 }
1739 #endif
1740