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