]> gitweb.michael.orlitzky.com - libsvgtiny.git/blob - src/svgtiny.c
d16a761300ad7b0657eb7b59478a085af06b1cae
[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 <libcss/libcss.h>
21
22 #include "svgtiny.h"
23 #include "svgtiny_internal.h"
24
25 /* Source file generated by `gperf`. */
26 #include "autogenerated_colors.c"
27
28 #define TAU 6.28318530717958647692
29
30 #ifndef M_PI
31 #define M_PI 3.14159265358979323846
32 #endif
33
34 #ifndef M_PI_2
35 #define M_PI_2 1.57079632679489661923
36 #endif
37
38 #define KAPPA 0.5522847498
39
40 #define degToRad(angleInDegrees) ((angleInDegrees) * M_PI / 180.0)
41 #define radToDeg(angleInRadians) ((angleInRadians) * 180.0 / M_PI)
42
43 static svgtiny_code svgtiny_parse_style_element(dom_element *style,
44 struct svgtiny_parse_state state);
45 static css_stylesheet *svgtiny_parse_style_inline(const uint8_t *data,
46 size_t len);
47 static svgtiny_code svgtiny_preparse_styles(dom_element *svg,
48 struct svgtiny_parse_state state);
49 static svgtiny_code svgtiny_parse_svg(dom_element *svg,
50 struct svgtiny_parse_state state);
51 static svgtiny_code svgtiny_parse_path(dom_element *path,
52 struct svgtiny_parse_state state);
53 static svgtiny_code svgtiny_parse_rect(dom_element *rect,
54 struct svgtiny_parse_state state);
55 static svgtiny_code svgtiny_parse_circle(dom_element *circle,
56 struct svgtiny_parse_state state);
57 static svgtiny_code svgtiny_parse_ellipse(dom_element *ellipse,
58 struct svgtiny_parse_state state);
59 static svgtiny_code svgtiny_parse_line(dom_element *line,
60 struct svgtiny_parse_state state);
61 static svgtiny_code svgtiny_parse_poly(dom_element *poly,
62 struct svgtiny_parse_state state, bool polygon);
63 static svgtiny_code svgtiny_parse_text(dom_element *text,
64 struct svgtiny_parse_state state);
65 static void svgtiny_parse_position_attributes(dom_element *node,
66 const struct svgtiny_parse_state state,
67 float *x, float *y, float *width, float *height);
68 static void svgtiny_parse_paint_attributes(dom_element *node,
69 struct svgtiny_parse_state *state);
70 static void svgtiny_parse_font_attributes(dom_element *node,
71 struct svgtiny_parse_state *state);
72 static void svgtiny_parse_transform_attributes(dom_element *node,
73 struct svgtiny_parse_state *state);
74 static svgtiny_code svgtiny_add_path(float *p, unsigned int n,
75 struct svgtiny_parse_state *state);
76 static void _svgtiny_parse_color(const char *s, svgtiny_colour *c,
77 struct svgtiny_parse_state_gradient *grad,
78 struct svgtiny_parse_state *state);
79
80 /**
81 * rotate midpoint vector
82 */
83 static void
84 rotate_midpoint_vector(float ax, float ay,
85 float bx, float by,
86 double radangle,
87 double *x_out, double *y_out)
88 {
89 double dx2; /* midpoint x coordinate */
90 double dy2; /* midpoint y coordinate */
91 double cosangle; /* cosine of rotation angle */
92 double sinangle; /* sine of rotation angle */
93
94 /* compute the sin and cos of the angle */
95 cosangle = cos(radangle);
96 sinangle = sin(radangle);
97
98 /* compute the midpoint between start and end points */
99 dx2 = (ax - bx) / 2.0;
100 dy2 = (ay - by) / 2.0;
101
102 /* rotate vector to remove angle */
103 *x_out = ((cosangle * dx2) + (sinangle * dy2));
104 *y_out = ((-sinangle * dx2) + (cosangle * dy2));
105 }
106
107
108 /**
109 * ensure the arc radii are large enough and scale as appropriate
110 *
111 * the radii need to be large enough if they are not they must be
112 * adjusted. This allows for elimination of differences between
113 * implementations especialy with rounding.
114 */
115 static void
116 ensure_radii_scale(double x1_sq, double y1_sq,
117 float *rx, float *ry,
118 double *rx_sq, double *ry_sq)
119 {
120 double radiisum;
121 double radiiscale;
122
123 /* set radii square values */
124 (*rx_sq) = (*rx) * (*rx);
125 (*ry_sq) = (*ry) * (*ry);
126
127 radiisum = (x1_sq / (*rx_sq)) + (y1_sq / (*ry_sq));
128 if (radiisum > 0.99999) {
129 /* need to scale radii */
130 radiiscale = sqrt(radiisum) * 1.00001;
131 *rx = (float)(radiiscale * (*rx));
132 *ry = (float)(radiiscale * (*ry));
133 /* update squares too */
134 (*rx_sq) = (*rx) * (*rx);
135 (*ry_sq) = (*ry) * (*ry);
136 }
137 }
138
139
140 /**
141 * compute the transformed centre point
142 */
143 static void
144 compute_transformed_centre_point(double sign, float rx, float ry,
145 double rx_sq, double ry_sq,
146 double x1, double y1,
147 double x1_sq, double y1_sq,
148 double *cx1, double *cy1)
149 {
150 double sq;
151 double coef;
152 sq = ((rx_sq * ry_sq) - (rx_sq * y1_sq) - (ry_sq * x1_sq)) /
153 ((rx_sq * y1_sq) + (ry_sq * x1_sq));
154 sq = (sq < 0) ? 0 : sq;
155
156 coef = (sign * sqrt(sq));
157
158 *cx1 = coef * ((rx * y1) / ry);
159 *cy1 = coef * -((ry * x1) / rx);
160 }
161
162
163 /**
164 * compute untransformed centre point
165 *
166 * \param ax The first point x coordinate
167 * \param ay The first point y coordinate
168 * \param bx The second point x coordinate
169 * \param ay The second point y coordinate
170 */
171 static void
172 compute_centre_point(float ax, float ay,
173 float bx, float by,
174 double cx1, double cy1,
175 double radangle,
176 double *x_out, double *y_out)
177 {
178 double sx2;
179 double sy2;
180 double cosangle; /* cosine of rotation angle */
181 double sinangle; /* sine of rotation angle */
182
183 /* compute the sin and cos of the angle */
184 cosangle = cos(radangle);
185 sinangle = sin(radangle);
186
187 sx2 = (ax + bx) / 2.0;
188 sy2 = (ay + by) / 2.0;
189
190 *x_out = sx2 + (cosangle * cx1 - sinangle * cy1);
191 *y_out = sy2 + (sinangle * cx1 + cosangle * cy1);
192 }
193
194
195 /**
196 * compute the angle start and extent
197 */
198 static void
199 compute_angle_start_extent(float rx, float ry,
200 double x1, double y1,
201 double cx1, double cy1,
202 double *start, double *extent)
203 {
204 double sign;
205 double ux;
206 double uy;
207 double vx;
208 double vy;
209 double p, n;
210 double actmp;
211
212 /*
213 * Angle betwen two vectors is +/- acos( u.v / len(u) * len(v))
214 * Where:
215 * '.' is the dot product.
216 * +/- is calculated from the sign of the cross product (u x v)
217 */
218
219 ux = (x1 - cx1) / rx;
220 uy = (y1 - cy1) / ry;
221 vx = (-x1 - cx1) / rx;
222 vy = (-y1 - cy1) / ry;
223
224 /* compute the start angle */
225 /* The angle between (ux, uy) and the 0 angle */
226
227 /* len(u) * len(1,0) == len(u) */
228 n = sqrt((ux * ux) + (uy * uy));
229 /* u.v == (ux,uy).(1,0) == (1 * ux) + (0 * uy) == ux */
230 p = ux;
231 /* u x v == (1 * uy - ux * 0) == uy */
232 sign = (uy < 0) ? -1.0 : 1.0;
233 /* (p >= n) so safe */
234 *start = sign * acos(p / n);
235
236 /* compute the extent angle */
237 n = sqrt(((ux * ux) + (uy * uy)) * ((vx * vx) + (vy * vy)));
238 p = (ux * vx) + (uy * vy);
239 sign = ((ux * vy) - (uy * vx) < 0) ? -1.0f : 1.0f;
240
241 /* arc cos must operate between -1 and 1 */
242 actmp = p / n;
243 if (actmp < -1.0) {
244 *extent = sign * M_PI;
245 } else if (actmp > 1.0) {
246 *extent = 0;
247 } else {
248 *extent = sign * acos(actmp);
249 }
250 }
251
252
253 /**
254 * converts a circle centered unit circle arc to a series of bezier curves
255 *
256 * Each bezier is stored as six values of three pairs of coordinates
257 *
258 * The beziers are stored without their start point as that is assumed
259 * to be the preceding elements end point.
260 *
261 * \param start The start angle of the arc (in radians)
262 * \param extent The size of the arc (in radians)
263 * \param bzpt The array to store the bezier values in
264 * \return The number of bezier segments output (max 4)
265 */
266 static int
267 circle_arc_to_bezier(double start, double extent, double *bzpt)
268 {
269 int bzsegments;
270 double increment;
271 double controllen;
272 int pos = 0;
273 int segment;
274 double angle;
275 double dx, dy;
276
277 bzsegments = (int) ceil(fabs(extent) / M_PI_2);
278 increment = extent / bzsegments;
279 controllen = 4.0 / 3.0 * sin(increment / 2.0) / (1.0 + cos(increment / 2.0));
280
281 for (segment = 0; segment < bzsegments; segment++) {
282 /* first control point */
283 angle = start + (segment * increment);
284 dx = cos(angle);
285 dy = sin(angle);
286 bzpt[pos++] = dx - controllen * dy;
287 bzpt[pos++] = dy + controllen * dx;
288 /* second control point */
289 angle+=increment;
290 dx = cos(angle);
291 dy = sin(angle);
292 bzpt[pos++] = dx + controllen * dy;
293 bzpt[pos++] = dy - controllen * dx;
294 /* endpoint */
295 bzpt[pos++] = dx;
296 bzpt[pos++] = dy;
297
298 }
299 return bzsegments;
300 }
301
302
303 /**
304 * transform coordinate list
305 *
306 * perform a scale, rotate and translate on list of coordinates
307 *
308 * scale(rx,ry)
309 * rotate(an)
310 * translate (cx, cy)
311 *
312 * homogeneous transforms
313 *
314 * scaling
315 * | rx 0 0 |
316 * S = | 0 ry 0 |
317 * | 0 0 1 |
318 *
319 * rotate
320 * | cos(an) -sin(an) 0 |
321 * R = | sin(an) cos(an) 0 |
322 * | 0 0 1 |
323 *
324 * {{cos(a), -sin(a) 0}, {sin(a), cos(a),0}, {0,0,1}}
325 *
326 * translate
327 * | 1 0 cx |
328 * T = | 0 1 cy |
329 * | 0 0 1 |
330 *
331 * note order is significat here and the combined matrix is
332 * M = T.R.S
333 *
334 * | cos(an) -sin(an) cx |
335 * T.R = | sin(an) cos(an) cy |
336 * | 0 0 1 |
337 *
338 * | rx * cos(an) ry * -sin(an) cx |
339 * T.R.S = | rx * sin(an) ry * cos(an) cy |
340 * | 0 0 1 |
341 *
342 * {{Cos[a], -Sin[a], c}, {Sin[a], Cos[a], d}, {0, 0, 1}} . {{r, 0, 0}, {0, s, 0}, {0, 0, 1}}
343 *
344 * Each point
345 * | x1 |
346 * P = | y1 |
347 * | 1 |
348 *
349 * output
350 * | x2 |
351 * | y2 | = M . P
352 * | 1 |
353 *
354 * x2 = cx + (rx * x1 * cos(a)) + (ry * y1 * -1 * sin(a))
355 * y2 = cy + (ry * y1 * cos(a)) + (rx * x1 * sin(a))
356 *
357 *
358 * \param rx X scaling to apply
359 * \param ry Y scaling to apply
360 * \param radangle rotation to apply (in radians)
361 * \param cx X translation to apply
362 * \param cy Y translation to apply
363 * \param points The size of the bzpoints array
364 * \param bzpoints an array of x,y values to apply the transform to
365 */
366 static void
367 scale_rotate_translate_points(double rx, double ry,
368 double radangle,
369 double cx, double cy,
370 int pntsize,
371 double *points)
372 {
373 int pnt;
374 double cosangle; /* cosine of rotation angle */
375 double sinangle; /* sine of rotation angle */
376 double rxcosangle, rxsinangle, rycosangle, rynsinangle;
377 double x2,y2;
378
379 /* compute the sin and cos of the angle */
380 cosangle = cos(radangle);
381 sinangle = sin(radangle);
382
383 rxcosangle = rx * cosangle;
384 rxsinangle = rx * sinangle;
385 rycosangle = ry * cosangle;
386 rynsinangle = ry * -1 * sinangle;
387
388 for (pnt = 0; pnt < pntsize; pnt+=2) {
389 x2 = cx + (points[pnt] * rxcosangle) + (points[pnt + 1] * rynsinangle);
390 y2 = cy + (points[pnt + 1] * rycosangle) + (points[pnt] * rxsinangle);
391 points[pnt] = x2;
392 points[pnt + 1] = y2;
393 }
394 }
395
396
397 /**
398 * convert an svg path arc to a bezier curve
399 *
400 * This function perfoms a transform on the nine arc parameters
401 * (coordinate pairs for start and end together with the radii of the
402 * elipse, the rotation angle and which of the four arcs to draw)
403 * which generates the parameters (coordinate pairs for start,
404 * end and their control points) for a set of up to four bezier curves.
405 *
406 * Obviously the start and end coordinates are not altered between
407 * representations so the aim is to calculate the coordinate pairs for
408 * the bezier control points.
409 *
410 * \param bzpoints the array to fill with bezier curves
411 * \return the number of bezier segments generated or -1 for a line
412 */
413 static int
414 svgarc_to_bezier(float start_x,
415 float start_y,
416 float end_x,
417 float end_y,
418 float rx,
419 float ry,
420 float angle,
421 bool largearc,
422 bool sweep,
423 double *bzpoints)
424 {
425 double radangle; /* normalised elipsis rotation angle in radians */
426 double rx_sq; /* x radius squared */
427 double ry_sq; /* y radius squared */
428 double x1, y1; /* rotated midpoint vector */
429 double x1_sq, y1_sq; /* x1 vector squared */
430 double cx1,cy1; /* transformed circle center */
431 double cx,cy; /* circle center */
432 double start, extent;
433 int bzsegments;
434
435 if ((start_x == end_x) && (start_y == end_y)) {
436 /*
437 * if the start and end coordinates are the same the
438 * svg spec says this is equivalent to having no segment
439 * at all
440 */
441 return 0;
442 }
443
444 if ((rx == 0) || (ry == 0)) {
445 /*
446 * if either radii is zero the specified behaviour is a line
447 */
448 return -1;
449 }
450
451 /* obtain the absolute values of the radii */
452 rx = fabsf(rx);
453 ry = fabsf(ry);
454
455 /* convert normalised angle to radians */
456 radangle = degToRad(fmod(angle, 360.0));
457
458 /* step 1 */
459 /* x1,x2 is the midpoint vector rotated to remove the arc angle */
460 rotate_midpoint_vector(start_x, start_y, end_x, end_y, radangle, &x1, &y1);
461
462 /* step 2 */
463 /* get squared x1 values */
464 x1_sq = x1 * x1;
465 y1_sq = y1 * y1;
466
467 /* ensure radii are correctly scaled */
468 ensure_radii_scale(x1_sq, y1_sq, &rx, &ry, &rx_sq, &ry_sq);
469
470 /* compute the transformed centre point */
471 compute_transformed_centre_point(largearc == sweep?-1:1,
472 rx, ry,
473 rx_sq, ry_sq,
474 x1, y1,
475 x1_sq, y1_sq,
476 &cx1, &cy1);
477
478 /* step 3 */
479 /* get the untransformed centre point */
480 compute_centre_point(start_x, start_y,
481 end_x, end_y,
482 cx1, cy1,
483 radangle,
484 &cx, &cy);
485
486 /* step 4 */
487 /* compute anglestart and extent */
488 compute_angle_start_extent(rx,ry,
489 x1,y1,
490 cx1, cy1,
491 &start, &extent);
492
493 /* extent of 0 is a straight line */
494 if (extent == 0) {
495 return -1;
496 }
497
498 /* take account of sweep */
499 if (!sweep && extent > 0) {
500 extent -= TAU;
501 } else if (sweep && extent < 0) {
502 extent += TAU;
503 }
504
505 /* normalise start and extent */
506 extent = fmod(extent, TAU);
507 start = fmod(start, TAU);
508
509 /* convert the arc to unit circle bezier curves */
510 bzsegments = circle_arc_to_bezier(start, extent, bzpoints);
511
512 /* transform the bezier curves */
513 scale_rotate_translate_points(rx, ry,
514 radangle,
515 cx, cy,
516 bzsegments * 6,
517 bzpoints);
518
519 return bzsegments;
520 }
521
522
523 /**
524 * Call this to ref the strings in a gradient state.
525 */
526 static void svgtiny_grad_string_ref(struct svgtiny_parse_state_gradient *grad)
527 {
528 if (grad->gradient_x1 != NULL) {
529 dom_string_ref(grad->gradient_x1);
530 }
531 if (grad->gradient_y1 != NULL) {
532 dom_string_ref(grad->gradient_y1);
533 }
534 if (grad->gradient_x2 != NULL) {
535 dom_string_ref(grad->gradient_x2);
536 }
537 if (grad->gradient_y2 != NULL) {
538 dom_string_ref(grad->gradient_y2);
539 }
540 }
541
542 /**
543 * Call this to clean up the strings in a gradient state.
544 */
545 static void svgtiny_grad_string_cleanup(
546 struct svgtiny_parse_state_gradient *grad)
547 {
548 if (grad->gradient_x1 != NULL) {
549 dom_string_unref(grad->gradient_x1);
550 grad->gradient_x1 = NULL;
551 }
552 if (grad->gradient_y1 != NULL) {
553 dom_string_unref(grad->gradient_y1);
554 grad->gradient_y1 = NULL;
555 }
556 if (grad->gradient_x2 != NULL) {
557 dom_string_unref(grad->gradient_x2);
558 grad->gradient_x2 = NULL;
559 }
560 if (grad->gradient_y2 != NULL) {
561 dom_string_unref(grad->gradient_y2);
562 grad->gradient_y2 = NULL;
563 }
564 }
565
566 /**
567 * Set the local externally-stored parts of a parse state.
568 * Call this in functions that made a new state on the stack.
569 * Doesn't make own copy of global state, such as the interned string list.
570 */
571 static void svgtiny_setup_state_local(struct svgtiny_parse_state *state)
572 {
573 svgtiny_grad_string_ref(&(state->fill_grad));
574 svgtiny_grad_string_ref(&(state->stroke_grad));
575 }
576
577 /**
578 * Cleanup the local externally-stored parts of a parse state.
579 * Call this in functions that made a new state on the stack.
580 * Doesn't cleanup global state, such as the interned string list.
581 */
582 static void svgtiny_cleanup_state_local(struct svgtiny_parse_state *state)
583 {
584 svgtiny_grad_string_cleanup(&(state->fill_grad));
585 svgtiny_grad_string_cleanup(&(state->stroke_grad));
586 }
587
588
589 /**
590 * Create a new svgtiny_diagram structure.
591 */
592
593 struct svgtiny_diagram *svgtiny_create(void)
594 {
595 struct svgtiny_diagram *diagram;
596
597 diagram = calloc(sizeof(*diagram), 1);
598 if (!diagram)
599 return 0;
600
601 return diagram;
602 free(diagram);
603 return NULL;
604 }
605
606 static void ignore_msg(uint32_t severity, void *ctx, const char *msg, ...)
607 {
608 UNUSED(severity);
609 UNUSED(ctx);
610 UNUSED(msg);
611 }
612
613 /**
614 * Parse a block of memory into a svgtiny_diagram.
615 */
616
617 svgtiny_code svgtiny_parse(struct svgtiny_diagram *diagram,
618 const char *buffer, size_t size, const char *url,
619 int viewport_width, int viewport_height)
620 {
621 css_error css_code;
622 dom_document *document;
623 dom_exception exc;
624 dom_xml_parser *parser;
625 dom_xml_error err;
626 dom_element *svg;
627 dom_string *svg_name;
628 lwc_string *svg_name_lwc;
629 struct svgtiny_parse_state state;
630 float x, y, width, height;
631 svgtiny_code code;
632
633 assert(diagram);
634 assert(buffer);
635 assert(url);
636
637 UNUSED(url);
638
639 parser = dom_xml_parser_create(NULL, NULL,
640 ignore_msg, NULL, &document);
641
642 if (parser == NULL)
643 return svgtiny_LIBDOM_ERROR;
644
645 err = dom_xml_parser_parse_chunk(parser, (uint8_t *)buffer, size);
646 if (err != DOM_XML_OK) {
647 dom_node_unref(document);
648 dom_xml_parser_destroy(parser);
649 return svgtiny_LIBDOM_ERROR;
650 }
651
652 err = dom_xml_parser_completed(parser);
653 if (err != DOM_XML_OK) {
654 dom_node_unref(document);
655 dom_xml_parser_destroy(parser);
656 return svgtiny_LIBDOM_ERROR;
657 }
658
659 /* We're done parsing, drop the parser.
660 * We now own the document entirely.
661 */
662 dom_xml_parser_destroy(parser);
663
664 /* find root <svg> element */
665 exc = dom_document_get_document_element(document, &svg);
666 if (exc != DOM_NO_ERR) {
667 dom_node_unref(document);
668 return svgtiny_LIBDOM_ERROR;
669 }
670 if (svg == NULL) {
671 /* no root svg element */
672 dom_node_unref(document);
673 return svgtiny_SVG_ERROR;
674 }
675
676 exc = dom_node_get_node_name(svg, &svg_name);
677 if (exc != DOM_NO_ERR) {
678 dom_node_unref(svg);
679 dom_node_unref(document);
680 return svgtiny_LIBDOM_ERROR;
681 }
682 if (lwc_intern_string("svg", 3 /* SLEN("svg") */,
683 &svg_name_lwc) != lwc_error_ok) {
684 dom_string_unref(svg_name);
685 dom_node_unref(svg);
686 dom_node_unref(document);
687 return svgtiny_LIBDOM_ERROR;
688 }
689 if (!dom_string_caseless_lwc_isequal(svg_name, svg_name_lwc)) {
690 lwc_string_unref(svg_name_lwc);
691 dom_string_unref(svg_name);
692 dom_node_unref(svg);
693 dom_node_unref(document);
694 return svgtiny_NOT_SVG;
695 }
696
697 lwc_string_unref(svg_name_lwc);
698 dom_string_unref(svg_name);
699
700 /* get graphic dimensions */
701 memset(&state, 0, sizeof(state));
702 state.diagram = diagram;
703 state.document = document;
704 state.viewport_width = viewport_width;
705 state.viewport_height = viewport_height;
706
707
708 /* Initialize CSS context */
709 if (state.select_ctx == NULL) {
710 css_code = css_select_ctx_create(&state.select_ctx);
711 if (css_code != CSS_OK) {
712 dom_node_unref(svg);
713 dom_node_unref(document);
714 return svgtiny_LIBCSS_ERROR;
715 }
716 }
717
718 #define SVGTINY_STRING_ACTION2(s,n) \
719 if (dom_string_create_interned((const uint8_t *) #n, \
720 strlen(#n), &state.interned_##s) \
721 != DOM_NO_ERR) { \
722 code = svgtiny_LIBDOM_ERROR; \
723 goto cleanup; \
724 }
725 #include "svgtiny_strings.h"
726 #undef SVGTINY_STRING_ACTION2
727
728 svgtiny_parse_position_attributes(svg, state, &x, &y, &width, &height);
729 diagram->width = width;
730 diagram->height = height;
731
732 /* set up parsing state */
733 state.viewport_width = width;
734 state.viewport_height = height;
735 state.ctm.a = 1; /*(float) viewport_width / (float) width;*/
736 state.ctm.b = 0;
737 state.ctm.c = 0;
738 state.ctm.d = 1; /*(float) viewport_height / (float) height;*/
739 state.ctm.e = 0; /*x;*/
740 state.ctm.f = 0; /*y;*/
741 state.fill = 0x000000;
742 state.stroke = svgtiny_TRANSPARENT;
743 state.stroke_width = 1;
744
745 /* parse tree */
746 code = svgtiny_preparse_styles(svg, state);
747 if (code == svgtiny_OK) {
748 code = svgtiny_parse_svg(svg, state);
749 }
750
751 dom_node_unref(svg);
752 dom_node_unref(document);
753 css_code = css_select_ctx_destroy(state.select_ctx);
754 if (css_code != CSS_OK) {
755 code = svgtiny_LIBCSS_ERROR;
756 }
757
758 cleanup:
759 svgtiny_cleanup_state_local(&state);
760 #define SVGTINY_STRING_ACTION2(s,n) \
761 if (state.interned_##s != NULL) \
762 dom_string_unref(state.interned_##s);
763 #include "svgtiny_strings.h"
764 #undef SVGTINY_STRING_ACTION2
765 return code;
766 }
767
768
769 /**
770 * Parse a single <style> element, appending the result to the CSS
771 * select context within the given parser state.
772 */
773 svgtiny_code svgtiny_parse_style_element(dom_element *style,
774 struct svgtiny_parse_state state)
775 {
776 css_stylesheet *sheet;
777 css_error code;
778 dom_exception exc;
779
780 code = svgtiny_create_stylesheet(&sheet, false);
781 if (code != CSS_OK) {
782 return svgtiny_LIBCSS_ERROR;
783 }
784
785 /* Parse the style element's "media" attribute if it has
786 one. We don't do anything with it right now. */
787 dom_string *media_attr;
788 exc = dom_element_get_attribute(style, state.interned_media,
789 &media_attr);
790 if (exc != DOM_NO_ERR) {
791 css_stylesheet_destroy(sheet);
792 return svgtiny_LIBDOM_ERROR;
793 }
794
795 if (media_attr) {
796 /* Here's where we'd actually change the media type if
797 we were going to use it */
798 dom_string_unref(media_attr);
799 }
800
801 dom_string *data;
802 dom_node_get_text_content(style, &data);
803 if (data == NULL) {
804 /* Empty stylesheet? That's fine. */
805 css_stylesheet_destroy(sheet);
806 return svgtiny_OK;
807 }
808
809 code = css_stylesheet_append_data(sheet,
810 (uint8_t *)dom_string_data(data),
811 dom_string_byte_length(data));
812 if (code != CSS_OK && code != CSS_NEEDDATA) {
813 dom_string_unref(data);
814 css_stylesheet_destroy(sheet);
815 return svgtiny_LIBCSS_ERROR;
816 }
817
818 code = css_stylesheet_data_done(sheet);
819 if (code != CSS_OK) {
820 dom_string_unref(data);
821 css_stylesheet_destroy(sheet);
822 return svgtiny_LIBCSS_ERROR;
823 }
824
825 code = css_select_ctx_append_sheet(state.select_ctx,
826 sheet,
827 CSS_ORIGIN_AUTHOR,
828 NULL);
829 if (code != CSS_OK) {
830 dom_string_unref(data);
831 return svgtiny_LIBCSS_ERROR;
832 }
833
834 dom_string_unref(data);
835 return svgtiny_OK;
836 }
837
838
839 /**
840 * Parse the contents of an inline style and return (a pointer to) the
841 * corresponding stylesheet for use with css_select_style(). Returns
842 * NULL if anything goes wrong.
843 */
844 css_stylesheet *svgtiny_parse_style_inline(const uint8_t *data,
845 size_t len)
846 {
847 css_stylesheet *sheet;
848 css_error code;
849
850 code = svgtiny_create_stylesheet(&sheet, true);
851 if (code != CSS_OK) {
852 return NULL;
853 }
854
855 code = css_stylesheet_append_data(sheet, data, len);
856 if (code != CSS_OK && code != CSS_NEEDDATA) {
857 css_stylesheet_destroy(sheet);
858 return NULL;
859 }
860
861 code = css_stylesheet_data_done(sheet);
862 if (code != CSS_OK) {
863 css_stylesheet_destroy(sheet);
864 return NULL;
865 }
866
867 return sheet;
868 }
869
870 /**
871 * Parse all <style> elements within a root <svg> element. This
872 * should be called before svgtiny_parse_svg() because that function
873 * makes a single pass through the document and we'd like all style
874 * information to be available during that pass. Specifically, we'd
875 * like a <style> sheet at the end of the document to affect the
876 * rendering of elements at its beginning.
877 *
878 * The element-parsing inner loop here is essentially the same as
879 * that within svgtiny_parse_svg().
880 */
881 svgtiny_code svgtiny_preparse_styles(dom_element *svg,
882 struct svgtiny_parse_state state)
883 {
884 dom_element *child;
885 dom_exception exc;
886
887 exc = dom_node_get_first_child(svg, (dom_node **) (void *) &child);
888 if (exc != DOM_NO_ERR) {
889 return svgtiny_LIBDOM_ERROR;
890 }
891 while (child != NULL) {
892 dom_element *next;
893 dom_node_type nodetype;
894 svgtiny_code code = svgtiny_OK;
895
896 exc = dom_node_get_node_type(child, &nodetype);
897 if (exc != DOM_NO_ERR) {
898 dom_node_unref(child);
899 return svgtiny_LIBDOM_ERROR;
900 }
901 if (nodetype == DOM_ELEMENT_NODE) {
902 dom_string *nodename;
903 exc = dom_node_get_node_name(child, &nodename);
904 if (exc != DOM_NO_ERR) {
905 dom_node_unref(child);
906 return svgtiny_LIBDOM_ERROR;
907 }
908
909 if (dom_string_caseless_isequal(state.interned_style,
910 nodename)) {
911 /* We have a <style> element, parse it */
912 code = svgtiny_parse_style_element(child,
913 state);
914 }
915
916
917 dom_string_unref(nodename);
918 }
919 if (code != svgtiny_OK) {
920 dom_node_unref(child);
921 return code;
922 }
923 exc = dom_node_get_next_sibling(child,
924 (dom_node **) (void *) &next);
925 dom_node_unref(child);
926 if (exc != DOM_NO_ERR) {
927 return svgtiny_LIBDOM_ERROR;
928 }
929 child = next;
930 }
931
932 return svgtiny_OK;
933 }
934
935 /**
936 * Parse a <svg> or <g> element node.
937 */
938
939 svgtiny_code svgtiny_parse_svg(dom_element *svg,
940 struct svgtiny_parse_state state)
941 {
942 float x, y, width, height;
943 dom_string *view_box;
944 dom_element *child;
945 dom_exception exc;
946
947 svgtiny_setup_state_local(&state);
948
949 svgtiny_parse_position_attributes(svg, state, &x, &y, &width, &height);
950 svgtiny_parse_paint_attributes(svg, &state);
951 svgtiny_parse_font_attributes(svg, &state);
952
953 exc = dom_element_get_attribute(svg, state.interned_viewBox,
954 &view_box);
955 if (exc != DOM_NO_ERR) {
956 svgtiny_cleanup_state_local(&state);
957 return svgtiny_LIBDOM_ERROR;
958 }
959
960 if (view_box) {
961 char *s = strndup(dom_string_data(view_box),
962 dom_string_byte_length(view_box));
963 float min_x, min_y, vwidth, vheight;
964 if (sscanf(s, "%f,%f,%f,%f",
965 &min_x, &min_y, &vwidth, &vheight) == 4 ||
966 sscanf(s, "%f %f %f %f",
967 &min_x, &min_y, &vwidth, &vheight) == 4) {
968 state.ctm.a = (float) state.viewport_width / vwidth;
969 state.ctm.d = (float) state.viewport_height / vheight;
970 state.ctm.e += -min_x * state.ctm.a;
971 state.ctm.f += -min_y * state.ctm.d;
972 }
973 free(s);
974 dom_string_unref(view_box);
975 }
976
977 svgtiny_parse_transform_attributes(svg, &state);
978
979 exc = dom_node_get_first_child(svg, (dom_node **) (void *) &child);
980 if (exc != DOM_NO_ERR) {
981 svgtiny_cleanup_state_local(&state);
982 return svgtiny_LIBDOM_ERROR;
983 }
984 while (child != NULL) {
985 dom_element *next;
986 dom_node_type nodetype;
987 svgtiny_code code = svgtiny_OK;
988
989 exc = dom_node_get_node_type(child, &nodetype);
990 if (exc != DOM_NO_ERR) {
991 dom_node_unref(child);
992 return svgtiny_LIBDOM_ERROR;
993 }
994 if (nodetype == DOM_ELEMENT_NODE) {
995 dom_string *nodename;
996 exc = dom_node_get_node_name(child, &nodename);
997 if (exc != DOM_NO_ERR) {
998 dom_node_unref(child);
999 svgtiny_cleanup_state_local(&state);
1000 return svgtiny_LIBDOM_ERROR;
1001 }
1002 if (dom_string_caseless_isequal(state.interned_svg,
1003 nodename))
1004 code = svgtiny_parse_svg(child, state);
1005 else if (dom_string_caseless_isequal(state.interned_g,
1006 nodename))
1007 code = svgtiny_parse_svg(child, state);
1008 else if (dom_string_caseless_isequal(state.interned_a,
1009 nodename))
1010 code = svgtiny_parse_svg(child, state);
1011 else if (dom_string_caseless_isequal(state.interned_path,
1012 nodename))
1013 code = svgtiny_parse_path(child, state);
1014 else if (dom_string_caseless_isequal(state.interned_rect,
1015 nodename))
1016 code = svgtiny_parse_rect(child, state);
1017 else if (dom_string_caseless_isequal(state.interned_circle,
1018 nodename))
1019 code = svgtiny_parse_circle(child, state);
1020 else if (dom_string_caseless_isequal(state.interned_ellipse,
1021 nodename))
1022 code = svgtiny_parse_ellipse(child, state);
1023 else if (dom_string_caseless_isequal(state.interned_line,
1024 nodename))
1025 code = svgtiny_parse_line(child, state);
1026 else if (dom_string_caseless_isequal(state.interned_polyline,
1027 nodename))
1028 code = svgtiny_parse_poly(child, state, false);
1029 else if (dom_string_caseless_isequal(state.interned_polygon,
1030 nodename))
1031 code = svgtiny_parse_poly(child, state, true);
1032 else if (dom_string_caseless_isequal(state.interned_text,
1033 nodename))
1034 code = svgtiny_parse_text(child, state);
1035 dom_string_unref(nodename);
1036 }
1037 if (code != svgtiny_OK) {
1038 dom_node_unref(child);
1039 svgtiny_cleanup_state_local(&state);
1040 return code;
1041 }
1042 exc = dom_node_get_next_sibling(child,
1043 (dom_node **) (void *) &next);
1044 dom_node_unref(child);
1045 if (exc != DOM_NO_ERR) {
1046 svgtiny_cleanup_state_local(&state);
1047 return svgtiny_LIBDOM_ERROR;
1048 }
1049 child = next;
1050 }
1051
1052 svgtiny_cleanup_state_local(&state);
1053 return svgtiny_OK;
1054 }
1055
1056
1057
1058 /**
1059 * Parse a <path> element node.
1060 *
1061 * http://www.w3.org/TR/SVG11/paths#PathElement
1062 */
1063
1064 svgtiny_code svgtiny_parse_path(dom_element *path,
1065 struct svgtiny_parse_state state)
1066 {
1067 svgtiny_code err;
1068 dom_string *path_d_str;
1069 dom_exception exc;
1070 char *s, *path_d;
1071 float *p; /* path elemets */
1072 unsigned int palloc; /* number of path elements allocated */
1073 unsigned int i;
1074 float last_x = 0, last_y = 0;
1075 float last_cubic_x = 0, last_cubic_y = 0;
1076 float last_quad_x = 0, last_quad_y = 0;
1077 float subpath_first_x = 0, subpath_first_y = 0;
1078
1079 svgtiny_setup_state_local(&state);
1080
1081 svgtiny_parse_paint_attributes(path, &state);
1082 svgtiny_parse_transform_attributes(path, &state);
1083
1084 /* read d attribute */
1085 exc = dom_element_get_attribute(path, state.interned_d, &path_d_str);
1086 if (exc != DOM_NO_ERR) {
1087 state.diagram->error_line = -1; /* path->line; */
1088 state.diagram->error_message = "path: error retrieving d attribute";
1089 svgtiny_cleanup_state_local(&state);
1090 return svgtiny_SVG_ERROR;
1091 }
1092
1093 if (path_d_str == NULL) {
1094 state.diagram->error_line = -1; /* path->line; */
1095 state.diagram->error_message = "path: missing d attribute";
1096 svgtiny_cleanup_state_local(&state);
1097 return svgtiny_SVG_ERROR;
1098 }
1099
1100 /* empty path is permitted it just disables the path */
1101 palloc = dom_string_byte_length(path_d_str);
1102 if (palloc == 0) {
1103 dom_string_unref(path_d_str);
1104 svgtiny_cleanup_state_local(&state);
1105 return svgtiny_OK;
1106 }
1107
1108 /* local copy of the path data allowing in-place modification */
1109 s = path_d = strndup(dom_string_data(path_d_str), palloc);
1110 dom_string_unref(path_d_str);
1111 if (s == NULL) {
1112 svgtiny_cleanup_state_local(&state);
1113 return svgtiny_OUT_OF_MEMORY;
1114 }
1115
1116 /* ensure path element allocation is sensibly bounded */
1117 if (palloc < 8) {
1118 palloc = 8;
1119 } else if (palloc > 64) {
1120 palloc = palloc / 2;
1121 }
1122
1123 /* allocate initial space for path elements */
1124 p = malloc(sizeof p[0] * palloc);
1125 if (p == NULL) {
1126 free(path_d);
1127 svgtiny_cleanup_state_local(&state);
1128 return svgtiny_OUT_OF_MEMORY;
1129 }
1130
1131 /* parse d and build path */
1132 for (i = 0; s[i]; i++)
1133 if (s[i] == ',')
1134 s[i] = ' ';
1135 i = 0;
1136 while (*s) {
1137 char command[2];
1138 int plot_command;
1139 float x, y, x1, y1, x2, y2, rx, ry, rotation, large_arc, sweep;
1140 int n;
1141
1142 /* Ensure there is sufficient space for path elements */
1143 #define ALLOC_PATH_ELEMENTS(NUM_ELEMENTS) \
1144 do { \
1145 if ((palloc - i) < NUM_ELEMENTS) { \
1146 float *tp; \
1147 palloc = (palloc * 2) + (palloc / 2); \
1148 tp = realloc(p, sizeof p[0] * palloc); \
1149 if (tp == NULL) { \
1150 free(p); \
1151 free(path_d); \
1152 svgtiny_cleanup_state_local(&state); \
1153 return svgtiny_OUT_OF_MEMORY; \
1154 } \
1155 p = tp; \
1156 } \
1157 } while(0)
1158
1159
1160 /* moveto (M, m), lineto (L, l) (2 arguments) */
1161 if (sscanf(s, " %1[MmLl] %f %f %n", command, &x, &y, &n) == 3) {
1162 /*LOG(("moveto or lineto"));*/
1163 if (*command == 'M' || *command == 'm')
1164 plot_command = svgtiny_PATH_MOVE;
1165 else
1166 plot_command = svgtiny_PATH_LINE;
1167 do {
1168 ALLOC_PATH_ELEMENTS(3);
1169 p[i++] = plot_command;
1170 if ('a' <= *command) {
1171 x += last_x;
1172 y += last_y;
1173 }
1174 if (plot_command == svgtiny_PATH_MOVE) {
1175 subpath_first_x = x;
1176 subpath_first_y = y;
1177 }
1178 p[i++] = last_cubic_x = last_quad_x = last_x
1179 = x;
1180 p[i++] = last_cubic_y = last_quad_y = last_y
1181 = y;
1182 s += n;
1183 plot_command = svgtiny_PATH_LINE;
1184 } while (sscanf(s, "%f %f %n", &x, &y, &n) == 2);
1185
1186 /* closepath (Z, z) (no arguments) */
1187 } else if (sscanf(s, " %1[Zz] %n", command, &n) == 1) {
1188 /*LOG(("closepath"));*/
1189 ALLOC_PATH_ELEMENTS(1);
1190
1191 p[i++] = svgtiny_PATH_CLOSE;
1192 s += n;
1193 last_cubic_x = last_quad_x = last_x = subpath_first_x;
1194 last_cubic_y = last_quad_y = last_y = subpath_first_y;
1195
1196 /* horizontal lineto (H, h) (1 argument) */
1197 } else if (sscanf(s, " %1[Hh] %f %n", command, &x, &n) == 2) {
1198 /*LOG(("horizontal lineto"));*/
1199 do {
1200 ALLOC_PATH_ELEMENTS(3);
1201
1202 p[i++] = svgtiny_PATH_LINE;
1203 if (*command == 'h')
1204 x += last_x;
1205 p[i++] = last_cubic_x = last_quad_x = last_x
1206 = x;
1207 p[i++] = last_cubic_y = last_quad_y = last_y;
1208 s += n;
1209 } while (sscanf(s, "%f %n", &x, &n) == 1);
1210
1211 /* vertical lineto (V, v) (1 argument) */
1212 } else if (sscanf(s, " %1[Vv] %f %n", command, &y, &n) == 2) {
1213 /*LOG(("vertical lineto"));*/
1214 do {
1215 ALLOC_PATH_ELEMENTS(3);
1216
1217 p[i++] = svgtiny_PATH_LINE;
1218 if (*command == 'v')
1219 y += last_y;
1220 p[i++] = last_cubic_x = last_quad_x = last_x;
1221 p[i++] = last_cubic_y = last_quad_y = last_y
1222 = y;
1223 s += n;
1224 } while (sscanf(s, "%f %n", &y, &n) == 1);
1225
1226 /* curveto (C, c) (6 arguments) */
1227 } else if (sscanf(s, " %1[Cc] %f %f %f %f %f %f %n", command,
1228 &x1, &y1, &x2, &y2, &x, &y, &n) == 7) {
1229 /*LOG(("curveto"));*/
1230 do {
1231 ALLOC_PATH_ELEMENTS(7);
1232
1233 p[i++] = svgtiny_PATH_BEZIER;
1234 if (*command == 'c') {
1235 x1 += last_x;
1236 y1 += last_y;
1237 x2 += last_x;
1238 y2 += last_y;
1239 x += last_x;
1240 y += last_y;
1241 }
1242 p[i++] = x1;
1243 p[i++] = y1;
1244 p[i++] = last_cubic_x = x2;
1245 p[i++] = last_cubic_y = y2;
1246 p[i++] = last_quad_x = last_x = x;
1247 p[i++] = last_quad_y = last_y = y;
1248 s += n;
1249 } while (sscanf(s, "%f %f %f %f %f %f %n",
1250 &x1, &y1, &x2, &y2, &x, &y, &n) == 6);
1251
1252 /* shorthand/smooth curveto (S, s) (4 arguments) */
1253 } else if (sscanf(s, " %1[Ss] %f %f %f %f %n", command,
1254 &x2, &y2, &x, &y, &n) == 5) {
1255 /*LOG(("shorthand/smooth curveto"));*/
1256 do {
1257 ALLOC_PATH_ELEMENTS(7);
1258
1259 p[i++] = svgtiny_PATH_BEZIER;
1260 x1 = last_x + (last_x - last_cubic_x);
1261 y1 = last_y + (last_y - last_cubic_y);
1262 if (*command == 's') {
1263 x2 += last_x;
1264 y2 += last_y;
1265 x += last_x;
1266 y += last_y;
1267 }
1268 p[i++] = x1;
1269 p[i++] = y1;
1270 p[i++] = last_cubic_x = x2;
1271 p[i++] = last_cubic_y = y2;
1272 p[i++] = last_quad_x = last_x = x;
1273 p[i++] = last_quad_y = last_y = y;
1274 s += n;
1275 } while (sscanf(s, "%f %f %f %f %n",
1276 &x2, &y2, &x, &y, &n) == 4);
1277
1278 /* quadratic Bezier curveto (Q, q) (4 arguments) */
1279 } else if (sscanf(s, " %1[Qq] %f %f %f %f %n", command,
1280 &x1, &y1, &x, &y, &n) == 5) {
1281 /*LOG(("quadratic Bezier curveto"));*/
1282 do {
1283 ALLOC_PATH_ELEMENTS(7);
1284
1285 p[i++] = svgtiny_PATH_BEZIER;
1286 last_quad_x = x1;
1287 last_quad_y = y1;
1288 if (*command == 'q') {
1289 x1 += last_x;
1290 y1 += last_y;
1291 x += last_x;
1292 y += last_y;
1293 }
1294 p[i++] = 1./3 * last_x + 2./3 * x1;
1295 p[i++] = 1./3 * last_y + 2./3 * y1;
1296 p[i++] = 2./3 * x1 + 1./3 * x;
1297 p[i++] = 2./3 * y1 + 1./3 * y;
1298 p[i++] = last_cubic_x = last_x = x;
1299 p[i++] = last_cubic_y = last_y = y;
1300 s += n;
1301 } while (sscanf(s, "%f %f %f %f %n",
1302 &x1, &y1, &x, &y, &n) == 4);
1303
1304 /* shorthand/smooth quadratic Bezier curveto (T, t)
1305 (2 arguments) */
1306 } else if (sscanf(s, " %1[Tt] %f %f %n", command,
1307 &x, &y, &n) == 3) {
1308 /*LOG(("shorthand/smooth quadratic Bezier curveto"));*/
1309 do {
1310 ALLOC_PATH_ELEMENTS(7);
1311
1312 p[i++] = svgtiny_PATH_BEZIER;
1313 x1 = last_x + (last_x - last_quad_x);
1314 y1 = last_y + (last_y - last_quad_y);
1315 last_quad_x = x1;
1316 last_quad_y = y1;
1317 if (*command == 't') {
1318 x1 += last_x;
1319 y1 += last_y;
1320 x += last_x;
1321 y += last_y;
1322 }
1323 p[i++] = 1./3 * last_x + 2./3 * x1;
1324 p[i++] = 1./3 * last_y + 2./3 * y1;
1325 p[i++] = 2./3 * x1 + 1./3 * x;
1326 p[i++] = 2./3 * y1 + 1./3 * y;
1327 p[i++] = last_cubic_x = last_x = x;
1328 p[i++] = last_cubic_y = last_y = y;
1329 s += n;
1330 } while (sscanf(s, "%f %f %n",
1331 &x, &y, &n) == 2);
1332
1333 /* elliptical arc (A, a) (7 arguments) */
1334 } else if (sscanf(s, " %1[Aa] %f %f %f %f %f %f %f %n", command,
1335 &rx, &ry, &rotation, &large_arc, &sweep,
1336 &x, &y, &n) == 8) {
1337 do {
1338 int bzsegments;
1339 double bzpoints[6*4]; /* allow for up to four bezier segments per arc */
1340
1341 if (*command == 'a') {
1342 x += last_x;
1343 y += last_y;
1344 }
1345
1346 bzsegments = svgarc_to_bezier(last_x, last_y,
1347 x, y,
1348 rx, ry,
1349 rotation,
1350 large_arc,
1351 sweep,
1352 bzpoints);
1353 if (bzsegments == -1) {
1354 /* draw a line */
1355 ALLOC_PATH_ELEMENTS(3);
1356 p[i++] = svgtiny_PATH_LINE;
1357 p[i++] = x;
1358 p[i++] = y;
1359 } else if (bzsegments > 0) {
1360 int bzpnt;
1361 ALLOC_PATH_ELEMENTS((unsigned int)bzsegments * 7);
1362 for (bzpnt = 0;bzpnt < (bzsegments * 6); bzpnt+=6) {
1363 p[i++] = svgtiny_PATH_BEZIER;
1364 p[i++] = bzpoints[bzpnt];
1365 p[i++] = bzpoints[bzpnt+1];
1366 p[i++] = bzpoints[bzpnt+2];
1367 p[i++] = bzpoints[bzpnt+3];
1368 p[i++] = bzpoints[bzpnt+4];
1369 p[i++] = bzpoints[bzpnt+5];
1370 }
1371 }
1372 if (bzsegments != 0) {
1373 last_cubic_x = last_quad_x = last_x = p[i-2];
1374 last_cubic_y = last_quad_y = last_y = p[i-1];
1375 }
1376
1377
1378 s += n;
1379 } while (sscanf(s, "%f %f %f %f %f %f %f %n",
1380 &rx, &ry, &rotation, &large_arc, &sweep,
1381 &x, &y, &n) == 7);
1382
1383 } else {
1384 /* fprintf(stderr, "parse failed at \"%s\"\n", s); */
1385 break;
1386 }
1387 }
1388
1389 free(path_d);
1390
1391 if (i <= 4) {
1392 /* no real segments in path */
1393 free(p);
1394 svgtiny_cleanup_state_local(&state);
1395 return svgtiny_OK;
1396 }
1397
1398 /* resize path element array to not be over allocated */
1399 if (palloc != i) {
1400 float *tp;
1401
1402 /* try the resize, if it fails just continue to use the old
1403 * allocation
1404 */
1405 tp = realloc(p, sizeof p[0] * i);
1406 if (tp != NULL) {
1407 p = tp;
1408 }
1409 }
1410
1411 err = svgtiny_add_path(p, i, &state);
1412
1413 svgtiny_cleanup_state_local(&state);
1414
1415 return err;
1416 }
1417
1418
1419 /**
1420 * Parse a <rect> element node.
1421 *
1422 * http://www.w3.org/TR/SVG11/shapes#RectElement
1423 */
1424
1425 svgtiny_code svgtiny_parse_rect(dom_element *rect,
1426 struct svgtiny_parse_state state)
1427 {
1428 svgtiny_code err;
1429 float x, y, width, height;
1430 float *p;
1431
1432 svgtiny_setup_state_local(&state);
1433
1434 svgtiny_parse_position_attributes(rect, state,
1435 &x, &y, &width, &height);
1436 svgtiny_parse_paint_attributes(rect, &state);
1437 svgtiny_parse_transform_attributes(rect, &state);
1438
1439 p = malloc(13 * sizeof p[0]);
1440 if (!p) {
1441 svgtiny_cleanup_state_local(&state);
1442 return svgtiny_OUT_OF_MEMORY;
1443 }
1444
1445 p[0] = svgtiny_PATH_MOVE;
1446 p[1] = x;
1447 p[2] = y;
1448 p[3] = svgtiny_PATH_LINE;
1449 p[4] = x + width;
1450 p[5] = y;
1451 p[6] = svgtiny_PATH_LINE;
1452 p[7] = x + width;
1453 p[8] = y + height;
1454 p[9] = svgtiny_PATH_LINE;
1455 p[10] = x;
1456 p[11] = y + height;
1457 p[12] = svgtiny_PATH_CLOSE;
1458
1459 err = svgtiny_add_path(p, 13, &state);
1460
1461 svgtiny_cleanup_state_local(&state);
1462
1463 return err;
1464 }
1465
1466
1467 /**
1468 * Parse a <circle> element node.
1469 */
1470
1471 svgtiny_code svgtiny_parse_circle(dom_element *circle,
1472 struct svgtiny_parse_state state)
1473 {
1474 svgtiny_code err;
1475 float x = 0, y = 0, r = -1;
1476 float *p;
1477 dom_string *attr;
1478 dom_exception exc;
1479
1480 svgtiny_setup_state_local(&state);
1481
1482 exc = dom_element_get_attribute(circle, state.interned_cx, &attr);
1483 if (exc != DOM_NO_ERR) {
1484 svgtiny_cleanup_state_local(&state);
1485 return svgtiny_LIBDOM_ERROR;
1486 }
1487 if (attr != NULL) {
1488 x = svgtiny_parse_length(attr, state.viewport_width, state);
1489 }
1490 dom_string_unref(attr);
1491
1492 exc = dom_element_get_attribute(circle, state.interned_cy, &attr);
1493 if (exc != DOM_NO_ERR) {
1494 svgtiny_cleanup_state_local(&state);
1495 return svgtiny_LIBDOM_ERROR;
1496 }
1497 if (attr != NULL) {
1498 y = svgtiny_parse_length(attr, state.viewport_height, state);
1499 }
1500 dom_string_unref(attr);
1501
1502 exc = dom_element_get_attribute(circle, state.interned_r, &attr);
1503 if (exc != DOM_NO_ERR) {
1504 svgtiny_cleanup_state_local(&state);
1505 return svgtiny_LIBDOM_ERROR;
1506 }
1507 if (attr != NULL) {
1508 r = svgtiny_parse_length(attr, state.viewport_width, state);
1509 }
1510 dom_string_unref(attr);
1511
1512 svgtiny_parse_paint_attributes(circle, &state);
1513 svgtiny_parse_transform_attributes(circle, &state);
1514
1515 if (r < 0) {
1516 state.diagram->error_line = -1; /* circle->line; */
1517 state.diagram->error_message = "circle: r missing or negative";
1518 svgtiny_cleanup_state_local(&state);
1519 return svgtiny_SVG_ERROR;
1520 }
1521 if (r == 0) {
1522 svgtiny_cleanup_state_local(&state);
1523 return svgtiny_OK;
1524 }
1525
1526 p = malloc(32 * sizeof p[0]);
1527 if (!p) {
1528 svgtiny_cleanup_state_local(&state);
1529 return svgtiny_OUT_OF_MEMORY;
1530 }
1531
1532 p[0] = svgtiny_PATH_MOVE;
1533 p[1] = x + r;
1534 p[2] = y;
1535 p[3] = svgtiny_PATH_BEZIER;
1536 p[4] = x + r;
1537 p[5] = y + r * KAPPA;
1538 p[6] = x + r * KAPPA;
1539 p[7] = y + r;
1540 p[8] = x;
1541 p[9] = y + r;
1542 p[10] = svgtiny_PATH_BEZIER;
1543 p[11] = x - r * KAPPA;
1544 p[12] = y + r;
1545 p[13] = x - r;
1546 p[14] = y + r * KAPPA;
1547 p[15] = x - r;
1548 p[16] = y;
1549 p[17] = svgtiny_PATH_BEZIER;
1550 p[18] = x - r;
1551 p[19] = y - r * KAPPA;
1552 p[20] = x - r * KAPPA;
1553 p[21] = y - r;
1554 p[22] = x;
1555 p[23] = y - r;
1556 p[24] = svgtiny_PATH_BEZIER;
1557 p[25] = x + r * KAPPA;
1558 p[26] = y - r;
1559 p[27] = x + r;
1560 p[28] = y - r * KAPPA;
1561 p[29] = x + r;
1562 p[30] = y;
1563 p[31] = svgtiny_PATH_CLOSE;
1564
1565 err = svgtiny_add_path(p, 32, &state);
1566
1567 svgtiny_cleanup_state_local(&state);
1568
1569 return err;
1570 }
1571
1572
1573 /**
1574 * Parse an <ellipse> element node.
1575 */
1576
1577 svgtiny_code svgtiny_parse_ellipse(dom_element *ellipse,
1578 struct svgtiny_parse_state state)
1579 {
1580 svgtiny_code err;
1581 float x = 0, y = 0, rx = -1, ry = -1;
1582 float *p;
1583 dom_string *attr;
1584 dom_exception exc;
1585
1586 svgtiny_setup_state_local(&state);
1587
1588 exc = dom_element_get_attribute(ellipse, state.interned_cx, &attr);
1589 if (exc != DOM_NO_ERR) {
1590 svgtiny_cleanup_state_local(&state);
1591 return svgtiny_LIBDOM_ERROR;
1592 }
1593 if (attr != NULL) {
1594 x = svgtiny_parse_length(attr, state.viewport_width, state);
1595 }
1596 dom_string_unref(attr);
1597
1598 exc = dom_element_get_attribute(ellipse, state.interned_cy, &attr);
1599 if (exc != DOM_NO_ERR) {
1600 svgtiny_cleanup_state_local(&state);
1601 return svgtiny_LIBDOM_ERROR;
1602 }
1603 if (attr != NULL) {
1604 y = svgtiny_parse_length(attr, state.viewport_height, state);
1605 }
1606 dom_string_unref(attr);
1607
1608 exc = dom_element_get_attribute(ellipse, state.interned_rx, &attr);
1609 if (exc != DOM_NO_ERR) {
1610 svgtiny_cleanup_state_local(&state);
1611 return svgtiny_LIBDOM_ERROR;
1612 }
1613 if (attr != NULL) {
1614 rx = svgtiny_parse_length(attr, state.viewport_width, state);
1615 }
1616 dom_string_unref(attr);
1617
1618 exc = dom_element_get_attribute(ellipse, state.interned_ry, &attr);
1619 if (exc != DOM_NO_ERR) {
1620 svgtiny_cleanup_state_local(&state);
1621 return svgtiny_LIBDOM_ERROR;
1622 }
1623 if (attr != NULL) {
1624 ry = svgtiny_parse_length(attr, state.viewport_width, state);
1625 }
1626 dom_string_unref(attr);
1627
1628 svgtiny_parse_paint_attributes(ellipse, &state);
1629 svgtiny_parse_transform_attributes(ellipse, &state);
1630
1631 if (rx < 0 || ry < 0) {
1632 state.diagram->error_line = -1; /* ellipse->line; */
1633 state.diagram->error_message = "ellipse: rx or ry missing "
1634 "or negative";
1635 svgtiny_cleanup_state_local(&state);
1636 return svgtiny_SVG_ERROR;
1637 }
1638 if (rx == 0 || ry == 0) {
1639 svgtiny_cleanup_state_local(&state);
1640 return svgtiny_OK;
1641 }
1642
1643 p = malloc(32 * sizeof p[0]);
1644 if (!p) {
1645 svgtiny_cleanup_state_local(&state);
1646 return svgtiny_OUT_OF_MEMORY;
1647 }
1648
1649 p[0] = svgtiny_PATH_MOVE;
1650 p[1] = x + rx;
1651 p[2] = y;
1652 p[3] = svgtiny_PATH_BEZIER;
1653 p[4] = x + rx;
1654 p[5] = y + ry * KAPPA;
1655 p[6] = x + rx * KAPPA;
1656 p[7] = y + ry;
1657 p[8] = x;
1658 p[9] = y + ry;
1659 p[10] = svgtiny_PATH_BEZIER;
1660 p[11] = x - rx * KAPPA;
1661 p[12] = y + ry;
1662 p[13] = x - rx;
1663 p[14] = y + ry * KAPPA;
1664 p[15] = x - rx;
1665 p[16] = y;
1666 p[17] = svgtiny_PATH_BEZIER;
1667 p[18] = x - rx;
1668 p[19] = y - ry * KAPPA;
1669 p[20] = x - rx * KAPPA;
1670 p[21] = y - ry;
1671 p[22] = x;
1672 p[23] = y - ry;
1673 p[24] = svgtiny_PATH_BEZIER;
1674 p[25] = x + rx * KAPPA;
1675 p[26] = y - ry;
1676 p[27] = x + rx;
1677 p[28] = y - ry * KAPPA;
1678 p[29] = x + rx;
1679 p[30] = y;
1680 p[31] = svgtiny_PATH_CLOSE;
1681
1682 err = svgtiny_add_path(p, 32, &state);
1683
1684 svgtiny_cleanup_state_local(&state);
1685
1686 return err;
1687 }
1688
1689
1690 /**
1691 * Parse a <line> element node.
1692 */
1693
1694 svgtiny_code svgtiny_parse_line(dom_element *line,
1695 struct svgtiny_parse_state state)
1696 {
1697 svgtiny_code err;
1698 float x1 = 0, y1 = 0, x2 = 0, y2 = 0;
1699 float *p;
1700 dom_string *attr;
1701 dom_exception exc;
1702
1703 svgtiny_setup_state_local(&state);
1704
1705 exc = dom_element_get_attribute(line, state.interned_x1, &attr);
1706 if (exc != DOM_NO_ERR) {
1707 svgtiny_cleanup_state_local(&state);
1708 return svgtiny_LIBDOM_ERROR;
1709 }
1710 if (attr != NULL) {
1711 x1 = svgtiny_parse_length(attr, state.viewport_width, state);
1712 }
1713 dom_string_unref(attr);
1714
1715 exc = dom_element_get_attribute(line, state.interned_y1, &attr);
1716 if (exc != DOM_NO_ERR) {
1717 svgtiny_cleanup_state_local(&state);
1718 return svgtiny_LIBDOM_ERROR;
1719 }
1720 if (attr != NULL) {
1721 y1 = svgtiny_parse_length(attr, state.viewport_height, state);
1722 }
1723 dom_string_unref(attr);
1724
1725 exc = dom_element_get_attribute(line, state.interned_x2, &attr);
1726 if (exc != DOM_NO_ERR) {
1727 svgtiny_cleanup_state_local(&state);
1728 return svgtiny_LIBDOM_ERROR;
1729 }
1730 if (attr != NULL) {
1731 x2 = svgtiny_parse_length(attr, state.viewport_width, state);
1732 }
1733 dom_string_unref(attr);
1734
1735 exc = dom_element_get_attribute(line, state.interned_y2, &attr);
1736 if (exc != DOM_NO_ERR) {
1737 svgtiny_cleanup_state_local(&state);
1738 return svgtiny_LIBDOM_ERROR;
1739 }
1740 if (attr != NULL) {
1741 y2 = svgtiny_parse_length(attr, state.viewport_height, state);
1742 }
1743 dom_string_unref(attr);
1744
1745 svgtiny_parse_paint_attributes(line, &state);
1746 svgtiny_parse_transform_attributes(line, &state);
1747
1748 p = malloc(7 * sizeof p[0]);
1749 if (!p) {
1750 svgtiny_cleanup_state_local(&state);
1751 return svgtiny_OUT_OF_MEMORY;
1752 }
1753
1754 p[0] = svgtiny_PATH_MOVE;
1755 p[1] = x1;
1756 p[2] = y1;
1757 p[3] = svgtiny_PATH_LINE;
1758 p[4] = x2;
1759 p[5] = y2;
1760 p[6] = svgtiny_PATH_CLOSE;
1761
1762 err = svgtiny_add_path(p, 7, &state);
1763
1764 svgtiny_cleanup_state_local(&state);
1765
1766 return err;
1767 }
1768
1769
1770 /**
1771 * Parse a <polyline> or <polygon> element node.
1772 *
1773 * http://www.w3.org/TR/SVG11/shapes#PolylineElement
1774 * http://www.w3.org/TR/SVG11/shapes#PolygonElement
1775 */
1776
1777 svgtiny_code svgtiny_parse_poly(dom_element *poly,
1778 struct svgtiny_parse_state state, bool polygon)
1779 {
1780 svgtiny_code err;
1781 dom_string *points_str;
1782 dom_exception exc;
1783 char *s, *points;
1784 float *p;
1785 unsigned int i;
1786
1787 svgtiny_setup_state_local(&state);
1788
1789 svgtiny_parse_paint_attributes(poly, &state);
1790 svgtiny_parse_transform_attributes(poly, &state);
1791
1792 exc = dom_element_get_attribute(poly, state.interned_points,
1793 &points_str);
1794 if (exc != DOM_NO_ERR) {
1795 svgtiny_cleanup_state_local(&state);
1796 return svgtiny_LIBDOM_ERROR;
1797 }
1798
1799 if (points_str == NULL) {
1800 state.diagram->error_line = -1; /* poly->line; */
1801 state.diagram->error_message =
1802 "polyline/polygon: missing points attribute";
1803 svgtiny_cleanup_state_local(&state);
1804 return svgtiny_SVG_ERROR;
1805 }
1806
1807 s = points = strndup(dom_string_data(points_str),
1808 dom_string_byte_length(points_str));
1809 dom_string_unref(points_str);
1810 /* read points attribute */
1811 if (s == NULL) {
1812 svgtiny_cleanup_state_local(&state);
1813 return svgtiny_OUT_OF_MEMORY;
1814 }
1815 /* allocate space for path: it will never have more elements than s */
1816 p = malloc(sizeof p[0] * strlen(s));
1817 if (!p) {
1818 free(points);
1819 svgtiny_cleanup_state_local(&state);
1820 return svgtiny_OUT_OF_MEMORY;
1821 }
1822
1823 /* parse s and build path */
1824 for (i = 0; s[i]; i++)
1825 if (s[i] == ',')
1826 s[i] = ' ';
1827 i = 0;
1828 while (*s) {
1829 float x, y;
1830 int n;
1831
1832 if (sscanf(s, "%f %f %n", &x, &y, &n) == 2) {
1833 if (i == 0)
1834 p[i++] = svgtiny_PATH_MOVE;
1835 else
1836 p[i++] = svgtiny_PATH_LINE;
1837 p[i++] = x;
1838 p[i++] = y;
1839 s += n;
1840 } else {
1841 break;
1842 }
1843 }
1844 if (polygon)
1845 p[i++] = svgtiny_PATH_CLOSE;
1846
1847 free(points);
1848
1849 err = svgtiny_add_path(p, i, &state);
1850
1851 svgtiny_cleanup_state_local(&state);
1852
1853 return err;
1854 }
1855
1856
1857 /**
1858 * Parse a <text> or <tspan> element node.
1859 */
1860
1861 svgtiny_code svgtiny_parse_text(dom_element *text,
1862 struct svgtiny_parse_state state)
1863 {
1864 float x, y, width, height;
1865 float px, py;
1866 dom_node *child;
1867 dom_exception exc;
1868
1869 svgtiny_setup_state_local(&state);
1870
1871 svgtiny_parse_position_attributes(text, state,
1872 &x, &y, &width, &height);
1873 svgtiny_parse_font_attributes(text, &state);
1874 svgtiny_parse_transform_attributes(text, &state);
1875
1876 px = state.ctm.a * x + state.ctm.c * y + state.ctm.e;
1877 py = state.ctm.b * x + state.ctm.d * y + state.ctm.f;
1878 /* state.ctm.e = px - state.origin_x; */
1879 /* state.ctm.f = py - state.origin_y; */
1880
1881 exc = dom_node_get_first_child(text, &child);
1882 if (exc != DOM_NO_ERR) {
1883 return svgtiny_LIBDOM_ERROR;
1884 svgtiny_cleanup_state_local(&state);
1885 }
1886 while (child != NULL) {
1887 dom_node *next;
1888 dom_node_type nodetype;
1889 svgtiny_code code = svgtiny_OK;
1890
1891 exc = dom_node_get_node_type(child, &nodetype);
1892 if (exc != DOM_NO_ERR) {
1893 dom_node_unref(child);
1894 svgtiny_cleanup_state_local(&state);
1895 return svgtiny_LIBDOM_ERROR;
1896 }
1897 if (nodetype == DOM_ELEMENT_NODE) {
1898 dom_string *nodename;
1899 exc = dom_node_get_node_name(child, &nodename);
1900 if (exc != DOM_NO_ERR) {
1901 dom_node_unref(child);
1902 svgtiny_cleanup_state_local(&state);
1903 return svgtiny_LIBDOM_ERROR;
1904 }
1905 if (dom_string_caseless_isequal(nodename,
1906 state.interned_tspan))
1907 code = svgtiny_parse_text((dom_element *)child,
1908 state);
1909 dom_string_unref(nodename);
1910 } else if (nodetype == DOM_TEXT_NODE) {
1911 struct svgtiny_shape *shape = svgtiny_add_shape(&state);
1912 dom_string *content;
1913 if (shape == NULL) {
1914 dom_node_unref(child);
1915 svgtiny_cleanup_state_local(&state);
1916 return svgtiny_OUT_OF_MEMORY;
1917 }
1918 exc = dom_text_get_whole_text(child, &content);
1919 if (exc != DOM_NO_ERR) {
1920 dom_node_unref(child);
1921 svgtiny_cleanup_state_local(&state);
1922 return svgtiny_LIBDOM_ERROR;
1923 }
1924 if (content != NULL) {
1925 shape->text = strndup(dom_string_data(content),
1926 dom_string_byte_length(content));
1927 dom_string_unref(content);
1928 } else {
1929 shape->text = strdup("");
1930 }
1931 shape->text_x = px;
1932 shape->text_y = py;
1933 state.diagram->shape_count++;
1934 }
1935
1936 if (code != svgtiny_OK) {
1937 dom_node_unref(child);
1938 svgtiny_cleanup_state_local(&state);
1939 return code;
1940 }
1941 exc = dom_node_get_next_sibling(child, &next);
1942 dom_node_unref(child);
1943 if (exc != DOM_NO_ERR) {
1944 svgtiny_cleanup_state_local(&state);
1945 return svgtiny_LIBDOM_ERROR;
1946 }
1947 child = next;
1948 }
1949
1950 svgtiny_cleanup_state_local(&state);
1951
1952 return svgtiny_OK;
1953 }
1954
1955
1956 /**
1957 * Parse x, y, width, and height attributes, if present.
1958 */
1959
1960 void svgtiny_parse_position_attributes(dom_element *node,
1961 const struct svgtiny_parse_state state,
1962 float *x, float *y, float *width, float *height)
1963 {
1964 dom_string *attr;
1965 dom_exception exc;
1966
1967 *x = 0;
1968 *y = 0;
1969 *width = state.viewport_width;
1970 *height = state.viewport_height;
1971
1972 exc = dom_element_get_attribute(node, state.interned_x, &attr);
1973 if (exc == DOM_NO_ERR && attr != NULL) {
1974 *x = svgtiny_parse_length(attr, state.viewport_width, state);
1975 dom_string_unref(attr);
1976 }
1977
1978 exc = dom_element_get_attribute(node, state.interned_y, &attr);
1979 if (exc == DOM_NO_ERR && attr != NULL) {
1980 *y = svgtiny_parse_length(attr, state.viewport_height, state);
1981 dom_string_unref(attr);
1982 }
1983
1984 exc = dom_element_get_attribute(node, state.interned_width, &attr);
1985 if (exc == DOM_NO_ERR && attr != NULL) {
1986 *width = svgtiny_parse_length(attr, state.viewport_width,
1987 state);
1988 dom_string_unref(attr);
1989 }
1990
1991 exc = dom_element_get_attribute(node, state.interned_height, &attr);
1992 if (exc == DOM_NO_ERR && attr != NULL) {
1993 *height = svgtiny_parse_length(attr, state.viewport_height,
1994 state);
1995 dom_string_unref(attr);
1996 }
1997 }
1998
1999
2000 /**
2001 * Parse a length as a number of pixels.
2002 */
2003
2004 static float _svgtiny_parse_length(const char *s, int viewport_size,
2005 const struct svgtiny_parse_state state)
2006 {
2007 int num_length = strspn(s, "0123456789+-.");
2008 const char *unit = s + num_length;
2009 float n = atof((const char *) s);
2010 float font_size = 20;
2011
2012 UNUSED(state);
2013
2014 if (unit[0] == 0) {
2015 return n;
2016 } else if (unit[0] == '%') {
2017 return n / 100.0 * viewport_size;
2018 } else if (unit[0] == 'e' && unit[1] == 'm') {
2019 return n * font_size;
2020 } else if (unit[0] == 'e' && unit[1] == 'x') {
2021 return n / 2.0 * font_size;
2022 } else if (unit[0] == 'p' && unit[1] == 'x') {
2023 return n;
2024 } else if (unit[0] == 'p' && unit[1] == 't') {
2025 return n * 1.25;
2026 } else if (unit[0] == 'p' && unit[1] == 'c') {
2027 return n * 15.0;
2028 } else if (unit[0] == 'm' && unit[1] == 'm') {
2029 return n * 3.543307;
2030 } else if (unit[0] == 'c' && unit[1] == 'm') {
2031 return n * 35.43307;
2032 } else if (unit[0] == 'i' && unit[1] == 'n') {
2033 return n * 90;
2034 }
2035
2036 return 0;
2037 }
2038
2039 float svgtiny_parse_length(dom_string *s, int viewport_size,
2040 const struct svgtiny_parse_state state)
2041 {
2042 char *ss = strndup(dom_string_data(s), dom_string_byte_length(s));
2043 float ret = _svgtiny_parse_length(ss, viewport_size, state);
2044 free(ss);
2045 return ret;
2046 }
2047
2048 /**
2049 * Parse paint attributes, if present.
2050 */
2051
2052 void svgtiny_parse_paint_attributes(dom_element *node,
2053 struct svgtiny_parse_state *state)
2054 {
2055 dom_string *attr;
2056 dom_exception exc;
2057
2058 /* We store the result of svgtiny_parse_style_inline() in
2059 * inline_sheet, and that function returns NULL on error; in
2060 * particular you do not need to css_stylesheet_destroy() the
2061 * result if it is NULL. We initialize inline_sheet to NULL to
2062 * retain the same semantics. */
2063 css_stylesheet *inline_sheet = NULL;
2064
2065 exc = dom_element_get_attribute(node, state->interned_fill, &attr);
2066 if (exc == DOM_NO_ERR && attr != NULL) {
2067 svgtiny_parse_color(attr, &state->fill, &state->fill_grad, state);
2068 dom_string_unref(attr);
2069 }
2070
2071 exc = dom_element_get_attribute(node, state->interned_stroke, &attr);
2072 if (exc == DOM_NO_ERR && attr != NULL) {
2073 svgtiny_parse_color(attr, &state->stroke, &state->stroke_grad, state);
2074 dom_string_unref(attr);
2075 }
2076
2077 exc = dom_element_get_attribute(node, state->interned_stroke_width, &attr);
2078 if (exc == DOM_NO_ERR && attr != NULL) {
2079 state->stroke_width = svgtiny_parse_length(attr,
2080 state->viewport_width, *state);
2081 dom_string_unref(attr);
2082 }
2083
2084 exc = dom_element_get_attribute(node, state->interned_style, &attr);
2085 if (exc == DOM_NO_ERR && attr != NULL) {
2086 /* First parse the style attribute into a libcss stylesheet
2087 in case any of its properties are known to libcss. */
2088 inline_sheet = svgtiny_parse_style_inline(
2089 (uint8_t *)dom_string_data(attr),
2090 dom_string_byte_length(attr));
2091
2092 /* Parse any other properties "by hand" until they can
2093 be supported in libcss. */
2094 char *style = strndup(dom_string_data(attr),
2095 dom_string_byte_length(attr));
2096 const char *s;
2097 char *value;
2098 if ((s = strstr(style, "fill:"))) {
2099 s += 5;
2100 while (*s == ' ')
2101 s++;
2102 value = strndup(s, strcspn(s, "; "));
2103 _svgtiny_parse_color(value, &state->fill, &state->fill_grad, state);
2104 free(value);
2105 }
2106 if ((s = strstr(style, "stroke:"))) {
2107 s += 7;
2108 while (*s == ' ')
2109 s++;
2110 value = strndup(s, strcspn(s, "; "));
2111 _svgtiny_parse_color(value, &state->stroke, &state->stroke_grad, state);
2112 free(value);
2113 }
2114 if ((s = strstr(style, "stroke-width:"))) {
2115 s += 13;
2116 while (*s == ' ')
2117 s++;
2118 value = strndup(s, strcspn(s, "; "));
2119 state->stroke_width = _svgtiny_parse_length(value,
2120 state->viewport_width, *state);
2121 free(value);
2122 }
2123 free(style);
2124 dom_string_unref(attr);
2125 }
2126
2127 if (inline_sheet != NULL) {
2128 css_stylesheet_destroy(inline_sheet);
2129 }
2130 }
2131
2132
2133 /**
2134 * Parse a colour.
2135 */
2136
2137 static void _svgtiny_parse_color(const char *s, svgtiny_colour *c,
2138 struct svgtiny_parse_state_gradient *grad,
2139 struct svgtiny_parse_state *state)
2140 {
2141 unsigned int r, g, b;
2142 float rf, gf, bf;
2143 size_t len = strlen(s);
2144 char *id = 0, *rparen;
2145
2146 if (len == 4 && s[0] == '#') {
2147 if (sscanf(s + 1, "%1x%1x%1x", &r, &g, &b) == 3)
2148 *c = svgtiny_RGB(r | r << 4, g | g << 4, b | b << 4);
2149
2150 } else if (len == 7 && s[0] == '#') {
2151 if (sscanf(s + 1, "%2x%2x%2x", &r, &g, &b) == 3)
2152 *c = svgtiny_RGB(r, g, b);
2153
2154 } else if (10 <= len && s[0] == 'r' && s[1] == 'g' && s[2] == 'b' &&
2155 s[3] == '(' && s[len - 1] == ')') {
2156 if (sscanf(s + 4, "%u,%u,%u", &r, &g, &b) == 3)
2157 *c = svgtiny_RGB(r, g, b);
2158 else if (sscanf(s + 4, "%f%%,%f%%,%f%%", &rf, &gf, &bf) == 3) {
2159 b = bf * 255 / 100;
2160 g = gf * 255 / 100;
2161 r = rf * 255 / 100;
2162 *c = svgtiny_RGB(r, g, b);
2163 }
2164
2165 } else if (len == 4 && strcmp(s, "none") == 0) {
2166 *c = svgtiny_TRANSPARENT;
2167
2168 } else if (5 < len && s[0] == 'u' && s[1] == 'r' && s[2] == 'l' &&
2169 s[3] == '(') {
2170 if (grad == NULL) {
2171 *c = svgtiny_RGB(0, 0, 0);
2172 } else if (s[4] == '#') {
2173 id = strdup(s + 5);
2174 if (!id)
2175 return;
2176 rparen = strchr(id, ')');
2177 if (rparen)
2178 *rparen = 0;
2179 svgtiny_find_gradient(id, grad, state);
2180 free(id);
2181 if (grad->linear_gradient_stop_count == 0)
2182 *c = svgtiny_TRANSPARENT;
2183 else if (grad->linear_gradient_stop_count == 1)
2184 *c = grad->gradient_stop[0].color;
2185 else
2186 *c = svgtiny_LINEAR_GRADIENT;
2187 }
2188
2189 } else {
2190 const struct svgtiny_named_color *named_color;
2191 named_color = svgtiny_color_lookup(s, strlen(s));
2192 if (named_color)
2193 *c = named_color->color;
2194 }
2195 }
2196
2197 void svgtiny_parse_color(dom_string *s, svgtiny_colour *c,
2198 struct svgtiny_parse_state_gradient *grad,
2199 struct svgtiny_parse_state *state)
2200 {
2201 dom_string_ref(s);
2202 _svgtiny_parse_color(dom_string_data(s), c, grad, state);
2203 dom_string_unref(s);
2204 }
2205
2206 /**
2207 * Parse font attributes, if present.
2208 */
2209
2210 void svgtiny_parse_font_attributes(dom_element *node,
2211 struct svgtiny_parse_state *state)
2212 {
2213 /* TODO: Implement this, it never used to be */
2214 UNUSED(node);
2215 UNUSED(state);
2216 #ifdef WRITTEN_THIS_PROPERLY
2217 const xmlAttr *attr;
2218
2219 UNUSED(state);
2220
2221 for (attr = node->properties; attr; attr = attr->next) {
2222 if (strcmp((const char *) attr->name, "font-size") == 0) {
2223 /* TODO */
2224 }
2225 }
2226 #endif
2227 }
2228
2229
2230 /**
2231 * Parse transform attributes, if present.
2232 *
2233 * http://www.w3.org/TR/SVG11/coords#TransformAttribute
2234 */
2235
2236 void svgtiny_parse_transform_attributes(dom_element *node,
2237 struct svgtiny_parse_state *state)
2238 {
2239 char *transform;
2240 dom_string *attr;
2241 dom_exception exc;
2242
2243 exc = dom_element_get_attribute(node, state->interned_transform,
2244 &attr);
2245 if (exc == DOM_NO_ERR && attr != NULL) {
2246 transform = strndup(dom_string_data(attr),
2247 dom_string_byte_length(attr));
2248 svgtiny_parse_transform(transform, &state->ctm.a, &state->ctm.b,
2249 &state->ctm.c, &state->ctm.d,
2250 &state->ctm.e, &state->ctm.f);
2251 free(transform);
2252 dom_string_unref(attr);
2253 }
2254 }
2255
2256
2257 /**
2258 * Parse a transform string.
2259 */
2260
2261 void svgtiny_parse_transform(char *s, float *ma, float *mb,
2262 float *mc, float *md, float *me, float *mf)
2263 {
2264 float a, b, c, d, e, f;
2265 float za, zb, zc, zd, ze, zf;
2266 float angle, x, y;
2267 int n;
2268 unsigned int i;
2269
2270 for (i = 0; s[i]; i++)
2271 if (s[i] == ',')
2272 s[i] = ' ';
2273
2274 while (*s) {
2275 a = d = 1;
2276 b = c = 0;
2277 e = f = 0;
2278 n = 0;
2279 if ((sscanf(s, " matrix (%f %f %f %f %f %f ) %n",
2280 &a, &b, &c, &d, &e, &f, &n) == 6) && (n > 0))
2281 ;
2282 else if ((sscanf(s, " translate (%f %f ) %n",
2283 &e, &f, &n) == 2) && (n > 0))
2284 ;
2285 else if ((sscanf(s, " translate (%f ) %n",
2286 &e, &n) == 1) && (n > 0))
2287 ;
2288 else if ((sscanf(s, " scale (%f %f ) %n",
2289 &a, &d, &n) == 2) && (n > 0))
2290 ;
2291 else if ((sscanf(s, " scale (%f ) %n",
2292 &a, &n) == 1) && (n > 0))
2293 d = a;
2294 else if ((sscanf(s, " rotate (%f %f %f ) %n",
2295 &angle, &x, &y, &n) == 3) && (n > 0)) {
2296 angle = angle / 180 * M_PI;
2297 a = cos(angle);
2298 b = sin(angle);
2299 c = -sin(angle);
2300 d = cos(angle);
2301 e = -x * cos(angle) + y * sin(angle) + x;
2302 f = -x * sin(angle) - y * cos(angle) + y;
2303 } else if ((sscanf(s, " rotate (%f ) %n",
2304 &angle, &n) == 1) && (n > 0)) {
2305 angle = angle / 180 * M_PI;
2306 a = cos(angle);
2307 b = sin(angle);
2308 c = -sin(angle);
2309 d = cos(angle);
2310 } else if ((sscanf(s, " skewX (%f ) %n",
2311 &angle, &n) == 1) && (n > 0)) {
2312 angle = angle / 180 * M_PI;
2313 c = tan(angle);
2314 } else if ((sscanf(s, " skewY (%f ) %n",
2315 &angle, &n) == 1) && (n > 0)) {
2316 angle = angle / 180 * M_PI;
2317 b = tan(angle);
2318 } else
2319 break;
2320 za = *ma * a + *mc * b;
2321 zb = *mb * a + *md * b;
2322 zc = *ma * c + *mc * d;
2323 zd = *mb * c + *md * d;
2324 ze = *ma * e + *mc * f + *me;
2325 zf = *mb * e + *md * f + *mf;
2326 *ma = za;
2327 *mb = zb;
2328 *mc = zc;
2329 *md = zd;
2330 *me = ze;
2331 *mf = zf;
2332 s += n;
2333 }
2334 }
2335
2336
2337 /**
2338 * Add a path to the svgtiny_diagram.
2339 */
2340
2341 svgtiny_code svgtiny_add_path(float *p, unsigned int n,
2342 struct svgtiny_parse_state *state)
2343 {
2344 struct svgtiny_shape *shape;
2345
2346 if (state->fill == svgtiny_LINEAR_GRADIENT)
2347 return svgtiny_add_path_linear_gradient(p, n, state);
2348
2349 svgtiny_transform_path(p, n, state);
2350
2351 shape = svgtiny_add_shape(state);
2352 if (!shape) {
2353 free(p);
2354 return svgtiny_OUT_OF_MEMORY;
2355 }
2356 shape->path = p;
2357 shape->path_length = n;
2358 state->diagram->shape_count++;
2359
2360 return svgtiny_OK;
2361 }
2362
2363
2364 /**
2365 * Add a svgtiny_shape to the svgtiny_diagram.
2366 */
2367
2368 struct svgtiny_shape *svgtiny_add_shape(struct svgtiny_parse_state *state)
2369 {
2370 struct svgtiny_shape *shape = realloc(state->diagram->shape,
2371 (state->diagram->shape_count + 1) *
2372 sizeof (state->diagram->shape[0]));
2373 if (!shape)
2374 return 0;
2375 state->diagram->shape = shape;
2376
2377 shape += state->diagram->shape_count;
2378 shape->path = 0;
2379 shape->path_length = 0;
2380 shape->text = 0;
2381 shape->fill = state->fill;
2382 shape->stroke = state->stroke;
2383 shape->stroke_width = lroundf((float) state->stroke_width *
2384 (state->ctm.a + state->ctm.d) / 2.0);
2385 if (0 < state->stroke_width && shape->stroke_width == 0)
2386 shape->stroke_width = 1;
2387
2388 return shape;
2389 }
2390
2391
2392 /**
2393 * Apply the current transformation matrix to a path.
2394 */
2395
2396 void svgtiny_transform_path(float *p, unsigned int n,
2397 struct svgtiny_parse_state *state)
2398 {
2399 unsigned int j;
2400
2401 for (j = 0; j != n; ) {
2402 unsigned int points = 0;
2403 unsigned int k;
2404 switch ((int) p[j]) {
2405 case svgtiny_PATH_MOVE:
2406 case svgtiny_PATH_LINE:
2407 points = 1;
2408 break;
2409 case svgtiny_PATH_CLOSE:
2410 points = 0;
2411 break;
2412 case svgtiny_PATH_BEZIER:
2413 points = 3;
2414 break;
2415 default:
2416 assert(0);
2417 }
2418 j++;
2419 for (k = 0; k != points; k++) {
2420 float x0 = p[j], y0 = p[j + 1];
2421 float x = state->ctm.a * x0 + state->ctm.c * y0 +
2422 state->ctm.e;
2423 float y = state->ctm.b * x0 + state->ctm.d * y0 +
2424 state->ctm.f;
2425 p[j] = x;
2426 p[j + 1] = y;
2427 j += 2;
2428 }
2429 }
2430 }
2431
2432
2433 /**
2434 * Free all memory used by a diagram.
2435 */
2436
2437 void svgtiny_free(struct svgtiny_diagram *svg)
2438 {
2439 unsigned int i;
2440 assert(svg);
2441
2442 for (i = 0; i != svg->shape_count; i++) {
2443 free(svg->shape[i].path);
2444 free(svg->shape[i].text);
2445 }
2446
2447 free(svg->shape);
2448
2449 free(svg);
2450 }
2451
2452 #ifndef HAVE_STRNDUP
2453 char *svgtiny_strndup(const char *s, size_t n)
2454 {
2455 size_t len;
2456 char *s2;
2457
2458 for (len = 0; len != n && s[len]; len++)
2459 continue;
2460
2461 s2 = malloc(len + 1);
2462 if (s2 == NULL)
2463 return NULL;
2464
2465 memcpy(s2, s, len);
2466 s2[len] = '\0';
2467
2468 return s2;
2469 }
2470 #endif