2 * This file is part of Libsvgtiny
3 * Licensed under the MIT License,
4 * http://opensource.org/licenses/mit-license.php
5 * Copyright 2008-2009 James Bursa <james@semichrome.net>
6 * Copyright 2012 Daniel Silverstone <dsilvers@netsurf-browser.org>
18 #include <dom/bindings/xml/xmlparser.h>
21 #include "svgtiny_internal.h"
23 /* Source file generated by `gperf`. */
24 #include "autogenerated_colors.c"
26 #define TAU 6.28318530717958647692
29 #define M_PI 3.14159265358979323846
33 #define M_PI_2 1.57079632679489661923
36 #define KAPPA 0.5522847498
38 #define degToRad(angleInDegrees) ((angleInDegrees) * M_PI / 180.0)
39 #define radToDeg(angleInRadians) ((angleInRadians) * 180.0 / M_PI)
41 static svgtiny_code
svgtiny_parse_svg(dom_element
*svg
,
42 struct svgtiny_parse_state state
);
43 static svgtiny_code
svgtiny_parse_path(dom_element
*path
,
44 struct svgtiny_parse_state state
);
45 static svgtiny_code
svgtiny_parse_rect(dom_element
*rect
,
46 struct svgtiny_parse_state state
);
47 static svgtiny_code
svgtiny_parse_circle(dom_element
*circle
,
48 struct svgtiny_parse_state state
);
49 static svgtiny_code
svgtiny_parse_ellipse(dom_element
*ellipse
,
50 struct svgtiny_parse_state state
);
51 static svgtiny_code
svgtiny_parse_line(dom_element
*line
,
52 struct svgtiny_parse_state state
);
53 static svgtiny_code
svgtiny_parse_poly(dom_element
*poly
,
54 struct svgtiny_parse_state state
, bool polygon
);
55 static svgtiny_code
svgtiny_parse_text(dom_element
*text
,
56 struct svgtiny_parse_state state
);
57 static void svgtiny_parse_position_attributes(dom_element
*node
,
58 const struct svgtiny_parse_state state
,
59 float *x
, float *y
, float *width
, float *height
);
60 static void svgtiny_parse_paint_attributes(dom_element
*node
,
61 struct svgtiny_parse_state
*state
);
62 static void svgtiny_parse_font_attributes(dom_element
*node
,
63 struct svgtiny_parse_state
*state
);
64 static void svgtiny_parse_transform_attributes(dom_element
*node
,
65 struct svgtiny_parse_state
*state
);
66 static svgtiny_code
svgtiny_add_path(float *p
, unsigned int n
,
67 struct svgtiny_parse_state
*state
);
68 static void _svgtiny_parse_color(const char *s
, svgtiny_colour
*c
,
69 struct svgtiny_parse_state_gradient
*grad
,
70 struct svgtiny_parse_state
*state
);
73 * rotate midpoint vector
76 rotate_midpoint_vector(float ax
, float ay
,
79 double *x_out
, double *y_out
)
81 double dx2
; /* midpoint x coordinate */
82 double dy2
; /* midpoint y coordinate */
83 double cosangle
; /* cosine of rotation angle */
84 double sinangle
; /* sine of rotation angle */
86 /* compute the sin and cos of the angle */
87 cosangle
= cos(radangle
);
88 sinangle
= sin(radangle
);
90 /* compute the midpoint between start and end points */
91 dx2
= (ax
- bx
) / 2.0;
92 dy2
= (ay
- by
) / 2.0;
94 /* rotate vector to remove angle */
95 *x_out
= ((cosangle
* dx2
) + (sinangle
* dy2
));
96 *y_out
= ((-sinangle
* dx2
) + (cosangle
* dy2
));
101 * ensure the arc radii are large enough and scale as appropriate
103 * the radii need to be large enough if they are not they must be
104 * adjusted. This allows for elimination of differences between
105 * implementations especialy with rounding.
108 ensure_radii_scale(double x1_sq
, double y1_sq
,
109 float *rx
, float *ry
,
110 double *rx_sq
, double *ry_sq
)
115 /* set radii square values */
116 (*rx_sq
) = (*rx
) * (*rx
);
117 (*ry_sq
) = (*ry
) * (*ry
);
119 radiisum
= (x1_sq
/ (*rx_sq
)) + (y1_sq
/ (*ry_sq
));
120 if (radiisum
> 0.99999) {
121 /* need to scale radii */
122 radiiscale
= sqrt(radiisum
) * 1.00001;
123 *rx
= (float)(radiiscale
* (*rx
));
124 *ry
= (float)(radiiscale
* (*ry
));
125 /* update squares too */
126 (*rx_sq
) = (*rx
) * (*rx
);
127 (*ry_sq
) = (*ry
) * (*ry
);
133 * compute the transformed centre point
136 compute_transformed_centre_point(double sign
, float rx
, float ry
,
137 double rx_sq
, double ry_sq
,
138 double x1
, double y1
,
139 double x1_sq
, double y1_sq
,
140 double *cx1
, double *cy1
)
144 sq
= ((rx_sq
* ry_sq
) - (rx_sq
* y1_sq
) - (ry_sq
* x1_sq
)) /
145 ((rx_sq
* y1_sq
) + (ry_sq
* x1_sq
));
146 sq
= (sq
< 0) ? 0 : sq
;
148 coef
= (sign
* sqrt(sq
));
150 *cx1
= coef
* ((rx
* y1
) / ry
);
151 *cy1
= coef
* -((ry
* x1
) / rx
);
156 * compute untransformed centre point
158 * \param ax The first point x coordinate
159 * \param ay The first point y coordinate
160 * \param bx The second point x coordinate
161 * \param ay The second point y coordinate
164 compute_centre_point(float ax
, float ay
,
166 double cx1
, double cy1
,
168 double *x_out
, double *y_out
)
172 double cosangle
; /* cosine of rotation angle */
173 double sinangle
; /* sine of rotation angle */
175 /* compute the sin and cos of the angle */
176 cosangle
= cos(radangle
);
177 sinangle
= sin(radangle
);
179 sx2
= (ax
+ bx
) / 2.0;
180 sy2
= (ay
+ by
) / 2.0;
182 *x_out
= sx2
+ (cosangle
* cx1
- sinangle
* cy1
);
183 *y_out
= sy2
+ (sinangle
* cx1
+ cosangle
* cy1
);
188 * compute the angle start and extent
191 compute_angle_start_extent(float rx
, float ry
,
192 double x1
, double y1
,
193 double cx1
, double cy1
,
194 double *start
, double *extent
)
205 * Angle betwen two vectors is +/- acos( u.v / len(u) * len(v))
207 * '.' is the dot product.
208 * +/- is calculated from the sign of the cross product (u x v)
211 ux
= (x1
- cx1
) / rx
;
212 uy
= (y1
- cy1
) / ry
;
213 vx
= (-x1
- cx1
) / rx
;
214 vy
= (-y1
- cy1
) / ry
;
216 /* compute the start angle */
217 /* The angle between (ux, uy) and the 0 angle */
219 /* len(u) * len(1,0) == len(u) */
220 n
= sqrt((ux
* ux
) + (uy
* uy
));
221 /* u.v == (ux,uy).(1,0) == (1 * ux) + (0 * uy) == ux */
223 /* u x v == (1 * uy - ux * 0) == uy */
224 sign
= (uy
< 0) ? -1.0 : 1.0;
225 /* (p >= n) so safe */
226 *start
= sign
* acos(p
/ n
);
228 /* compute the extent angle */
229 n
= sqrt(((ux
* ux
) + (uy
* uy
)) * ((vx
* vx
) + (vy
* vy
)));
230 p
= (ux
* vx
) + (uy
* vy
);
231 sign
= ((ux
* vy
) - (uy
* vx
) < 0) ? -1.0f
: 1.0f
;
233 /* arc cos must operate between -1 and 1 */
236 *extent
= sign
* M_PI
;
237 } else if (actmp
> 1.0) {
240 *extent
= sign
* acos(actmp
);
246 * converts a circle centered unit circle arc to a series of bezier curves
248 * Each bezier is stored as six values of three pairs of coordinates
250 * The beziers are stored without their start point as that is assumed
251 * to be the preceding elements end point.
253 * \param start The start angle of the arc (in radians)
254 * \param extent The size of the arc (in radians)
255 * \param bzpt The array to store the bezier values in
256 * \return The number of bezier segments output (max 4)
259 circle_arc_to_bezier(double start
, double extent
, double *bzpt
)
269 bzsegments
= (int) ceil(fabs(extent
) / M_PI_2
);
270 increment
= extent
/ bzsegments
;
271 controllen
= 4.0 / 3.0 * sin(increment
/ 2.0) / (1.0 + cos(increment
/ 2.0));
273 for (segment
= 0; segment
< bzsegments
; segment
++) {
274 /* first control point */
275 angle
= start
+ (segment
* increment
);
278 bzpt
[pos
++] = dx
- controllen
* dy
;
279 bzpt
[pos
++] = dy
+ controllen
* dx
;
280 /* second control point */
284 bzpt
[pos
++] = dx
+ controllen
* dy
;
285 bzpt
[pos
++] = dy
- controllen
* dx
;
296 * transform coordinate list
298 * perform a scale, rotate and translate on list of coordinates
304 * homogeneous transforms
312 * | cos(an) -sin(an) 0 |
313 * R = | sin(an) cos(an) 0 |
316 * {{cos(a), -sin(a) 0}, {sin(a), cos(a),0}, {0,0,1}}
323 * note order is significat here and the combined matrix is
326 * | cos(an) -sin(an) cx |
327 * T.R = | sin(an) cos(an) cy |
330 * | rx * cos(an) ry * -sin(an) cx |
331 * T.R.S = | rx * sin(an) ry * cos(an) cy |
334 * {{Cos[a], -Sin[a], c}, {Sin[a], Cos[a], d}, {0, 0, 1}} . {{r, 0, 0}, {0, s, 0}, {0, 0, 1}}
346 * x2 = cx + (rx * x1 * cos(a)) + (ry * y1 * -1 * sin(a))
347 * y2 = cy + (ry * y1 * cos(a)) + (rx * x1 * sin(a))
350 * \param rx X scaling to apply
351 * \param ry Y scaling to apply
352 * \param radangle rotation to apply (in radians)
353 * \param cx X translation to apply
354 * \param cy Y translation to apply
355 * \param points The size of the bzpoints array
356 * \param bzpoints an array of x,y values to apply the transform to
359 scale_rotate_translate_points(double rx
, double ry
,
361 double cx
, double cy
,
366 double cosangle
; /* cosine of rotation angle */
367 double sinangle
; /* sine of rotation angle */
368 double rxcosangle
, rxsinangle
, rycosangle
, rynsinangle
;
371 /* compute the sin and cos of the angle */
372 cosangle
= cos(radangle
);
373 sinangle
= sin(radangle
);
375 rxcosangle
= rx
* cosangle
;
376 rxsinangle
= rx
* sinangle
;
377 rycosangle
= ry
* cosangle
;
378 rynsinangle
= ry
* -1 * sinangle
;
380 for (pnt
= 0; pnt
< pntsize
; pnt
+=2) {
381 x2
= cx
+ (points
[pnt
] * rxcosangle
) + (points
[pnt
+ 1] * rynsinangle
);
382 y2
= cy
+ (points
[pnt
+ 1] * rycosangle
) + (points
[pnt
] * rxsinangle
);
384 points
[pnt
+ 1] = y2
;
390 * convert an svg path arc to a bezier curve
392 * This function perfoms a transform on the nine arc parameters
393 * (coordinate pairs for start and end together with the radii of the
394 * elipse, the rotation angle and which of the four arcs to draw)
395 * which generates the parameters (coordinate pairs for start,
396 * end and their control points) for a set of up to four bezier curves.
398 * Obviously the start and end coordinates are not altered between
399 * representations so the aim is to calculate the coordinate pairs for
400 * the bezier control points.
402 * \param bzpoints the array to fill with bezier curves
403 * \return the number of bezier segments generated or -1 for a line
406 svgarc_to_bezier(float start_x
,
417 double radangle
; /* normalised elipsis rotation angle in radians */
418 double rx_sq
; /* x radius squared */
419 double ry_sq
; /* y radius squared */
420 double x1
, y1
; /* rotated midpoint vector */
421 double x1_sq
, y1_sq
; /* x1 vector squared */
422 double cx1
,cy1
; /* transformed circle center */
423 double cx
,cy
; /* circle center */
424 double start
, extent
;
427 if ((start_x
== end_x
) && (start_y
== end_y
)) {
429 * if the start and end coordinates are the same the
430 * svg spec says this is equivalent to having no segment
436 if ((rx
== 0) || (ry
== 0)) {
438 * if either radii is zero the specified behaviour is a line
443 /* obtain the absolute values of the radii */
447 /* convert normalised angle to radians */
448 radangle
= degToRad(fmod(angle
, 360.0));
451 /* x1,x2 is the midpoint vector rotated to remove the arc angle */
452 rotate_midpoint_vector(start_x
, start_y
, end_x
, end_y
, radangle
, &x1
, &y1
);
455 /* get squared x1 values */
459 /* ensure radii are correctly scaled */
460 ensure_radii_scale(x1_sq
, y1_sq
, &rx
, &ry
, &rx_sq
, &ry_sq
);
462 /* compute the transformed centre point */
463 compute_transformed_centre_point(largearc
== sweep
?-1:1,
471 /* get the untransformed centre point */
472 compute_centre_point(start_x
, start_y
,
479 /* compute anglestart and extent */
480 compute_angle_start_extent(rx
,ry
,
485 /* extent of 0 is a straight line */
490 /* take account of sweep */
491 if (!sweep
&& extent
> 0) {
493 } else if (sweep
&& extent
< 0) {
497 /* normalise start and extent */
498 extent
= fmod(extent
, TAU
);
499 start
= fmod(start
, TAU
);
501 /* convert the arc to unit circle bezier curves */
502 bzsegments
= circle_arc_to_bezier(start
, extent
, bzpoints
);
504 /* transform the bezier curves */
505 scale_rotate_translate_points(rx
, ry
,
516 * Call this to ref the strings in a gradient state.
518 static void svgtiny_grad_string_ref(struct svgtiny_parse_state_gradient
*grad
)
520 if (grad
->gradient_x1
!= NULL
) {
521 dom_string_ref(grad
->gradient_x1
);
523 if (grad
->gradient_y1
!= NULL
) {
524 dom_string_ref(grad
->gradient_y1
);
526 if (grad
->gradient_x2
!= NULL
) {
527 dom_string_ref(grad
->gradient_x2
);
529 if (grad
->gradient_y2
!= NULL
) {
530 dom_string_ref(grad
->gradient_y2
);
535 * Call this to clean up the strings in a gradient state.
537 static void svgtiny_grad_string_cleanup(
538 struct svgtiny_parse_state_gradient
*grad
)
540 if (grad
->gradient_x1
!= NULL
) {
541 dom_string_unref(grad
->gradient_x1
);
542 grad
->gradient_x1
= NULL
;
544 if (grad
->gradient_y1
!= NULL
) {
545 dom_string_unref(grad
->gradient_y1
);
546 grad
->gradient_y1
= NULL
;
548 if (grad
->gradient_x2
!= NULL
) {
549 dom_string_unref(grad
->gradient_x2
);
550 grad
->gradient_x2
= NULL
;
552 if (grad
->gradient_y2
!= NULL
) {
553 dom_string_unref(grad
->gradient_y2
);
554 grad
->gradient_y2
= NULL
;
559 * Set the local externally-stored parts of a parse state.
560 * Call this in functions that made a new state on the stack.
561 * Doesn't make own copy of global state, such as the interned string list.
563 static void svgtiny_setup_state_local(struct svgtiny_parse_state
*state
)
565 svgtiny_grad_string_ref(&(state
->fill_grad
));
566 svgtiny_grad_string_ref(&(state
->stroke_grad
));
570 * Cleanup the local externally-stored parts of a parse state.
571 * Call this in functions that made a new state on the stack.
572 * Doesn't cleanup global state, such as the interned string list.
574 static void svgtiny_cleanup_state_local(struct svgtiny_parse_state
*state
)
576 svgtiny_grad_string_cleanup(&(state
->fill_grad
));
577 svgtiny_grad_string_cleanup(&(state
->stroke_grad
));
582 * Create a new svgtiny_diagram structure.
585 struct svgtiny_diagram
*svgtiny_create(void)
587 struct svgtiny_diagram
*diagram
;
589 diagram
= calloc(sizeof(*diagram
), 1);
598 static void ignore_msg(uint32_t severity
, void *ctx
, const char *msg
, ...)
606 * Parse a block of memory into a svgtiny_diagram.
609 svgtiny_code
svgtiny_parse(struct svgtiny_diagram
*diagram
,
610 const char *buffer
, size_t size
, const char *url
,
611 int viewport_width
, int viewport_height
)
613 dom_document
*document
;
615 dom_xml_parser
*parser
;
618 dom_string
*svg_name
;
619 lwc_string
*svg_name_lwc
;
620 struct svgtiny_parse_state state
;
621 float x
, y
, width
, height
;
630 parser
= dom_xml_parser_create(NULL
, NULL
,
631 ignore_msg
, NULL
, &document
);
634 return svgtiny_LIBDOM_ERROR
;
636 err
= dom_xml_parser_parse_chunk(parser
, (uint8_t *)buffer
, size
);
637 if (err
!= DOM_XML_OK
) {
638 dom_node_unref(document
);
639 dom_xml_parser_destroy(parser
);
640 return svgtiny_LIBDOM_ERROR
;
643 err
= dom_xml_parser_completed(parser
);
644 if (err
!= DOM_XML_OK
) {
645 dom_node_unref(document
);
646 dom_xml_parser_destroy(parser
);
647 return svgtiny_LIBDOM_ERROR
;
650 /* We're done parsing, drop the parser.
651 * We now own the document entirely.
653 dom_xml_parser_destroy(parser
);
655 /* find root <svg> element */
656 exc
= dom_document_get_document_element(document
, &svg
);
657 if (exc
!= DOM_NO_ERR
) {
658 dom_node_unref(document
);
659 return svgtiny_LIBDOM_ERROR
;
662 /* no root svg element */
663 dom_node_unref(document
);
664 return svgtiny_SVG_ERROR
;
667 exc
= dom_node_get_node_name(svg
, &svg_name
);
668 if (exc
!= DOM_NO_ERR
) {
670 dom_node_unref(document
);
671 return svgtiny_LIBDOM_ERROR
;
673 if (lwc_intern_string("svg", 3 /* SLEN("svg") */,
674 &svg_name_lwc
) != lwc_error_ok
) {
675 dom_string_unref(svg_name
);
677 dom_node_unref(document
);
678 return svgtiny_LIBDOM_ERROR
;
680 if (!dom_string_caseless_lwc_isequal(svg_name
, svg_name_lwc
)) {
681 lwc_string_unref(svg_name_lwc
);
682 dom_string_unref(svg_name
);
684 dom_node_unref(document
);
685 return svgtiny_NOT_SVG
;
688 lwc_string_unref(svg_name_lwc
);
689 dom_string_unref(svg_name
);
691 /* get graphic dimensions */
692 memset(&state
, 0, sizeof(state
));
693 state
.diagram
= diagram
;
694 state
.document
= document
;
695 state
.viewport_width
= viewport_width
;
696 state
.viewport_height
= viewport_height
;
698 #define SVGTINY_STRING_ACTION2(s,n) \
699 if (dom_string_create_interned((const uint8_t *) #n, \
700 strlen(#n), &state.interned_##s) \
702 code = svgtiny_LIBDOM_ERROR; \
705 #include "svgtiny_strings.h"
706 #undef SVGTINY_STRING_ACTION2
708 svgtiny_parse_position_attributes(svg
, state
, &x
, &y
, &width
, &height
);
709 diagram
->width
= width
;
710 diagram
->height
= height
;
712 /* set up parsing state */
713 state
.viewport_width
= width
;
714 state
.viewport_height
= height
;
715 state
.ctm
.a
= 1; /*(float) viewport_width / (float) width;*/
718 state
.ctm
.d
= 1; /*(float) viewport_height / (float) height;*/
719 state
.ctm
.e
= 0; /*x;*/
720 state
.ctm
.f
= 0; /*y;*/
721 /*state.style = css_base_style;
722 state.style.font_size.value.length.value = option_font_size * 0.1;*/
723 state
.fill
= 0x000000;
724 state
.stroke
= svgtiny_TRANSPARENT
;
725 state
.stroke_width
= 1;
728 code
= svgtiny_parse_svg(svg
, state
);
731 dom_node_unref(document
);
734 svgtiny_cleanup_state_local(&state
);
735 #define SVGTINY_STRING_ACTION2(s,n) \
736 if (state.interned_##s != NULL) \
737 dom_string_unref(state.interned_##s);
738 #include "svgtiny_strings.h"
739 #undef SVGTINY_STRING_ACTION2
745 * Parse a <svg> or <g> element node.
748 svgtiny_code
svgtiny_parse_svg(dom_element
*svg
,
749 struct svgtiny_parse_state state
)
751 float x
, y
, width
, height
;
752 dom_string
*view_box
;
756 svgtiny_setup_state_local(&state
);
758 svgtiny_parse_position_attributes(svg
, state
, &x
, &y
, &width
, &height
);
759 svgtiny_parse_paint_attributes(svg
, &state
);
760 svgtiny_parse_font_attributes(svg
, &state
);
762 exc
= dom_element_get_attribute(svg
, state
.interned_viewBox
,
764 if (exc
!= DOM_NO_ERR
) {
765 svgtiny_cleanup_state_local(&state
);
766 return svgtiny_LIBDOM_ERROR
;
770 char *s
= strndup(dom_string_data(view_box
),
771 dom_string_byte_length(view_box
));
772 float min_x
, min_y
, vwidth
, vheight
;
773 if (sscanf(s
, "%f,%f,%f,%f",
774 &min_x
, &min_y
, &vwidth
, &vheight
) == 4 ||
775 sscanf(s
, "%f %f %f %f",
776 &min_x
, &min_y
, &vwidth
, &vheight
) == 4) {
777 state
.ctm
.a
= (float) state
.viewport_width
/ vwidth
;
778 state
.ctm
.d
= (float) state
.viewport_height
/ vheight
;
779 state
.ctm
.e
+= -min_x
* state
.ctm
.a
;
780 state
.ctm
.f
+= -min_y
* state
.ctm
.d
;
783 dom_string_unref(view_box
);
786 svgtiny_parse_transform_attributes(svg
, &state
);
788 exc
= dom_node_get_first_child(svg
, (dom_node
**) (void *) &child
);
789 if (exc
!= DOM_NO_ERR
) {
790 svgtiny_cleanup_state_local(&state
);
791 return svgtiny_LIBDOM_ERROR
;
793 while (child
!= NULL
) {
795 dom_node_type nodetype
;
796 svgtiny_code code
= svgtiny_OK
;
798 exc
= dom_node_get_node_type(child
, &nodetype
);
799 if (exc
!= DOM_NO_ERR
) {
800 dom_node_unref(child
);
801 return svgtiny_LIBDOM_ERROR
;
803 if (nodetype
== DOM_ELEMENT_NODE
) {
804 dom_string
*nodename
;
805 exc
= dom_node_get_node_name(child
, &nodename
);
806 if (exc
!= DOM_NO_ERR
) {
807 dom_node_unref(child
);
808 svgtiny_cleanup_state_local(&state
);
809 return svgtiny_LIBDOM_ERROR
;
811 if (dom_string_caseless_isequal(state
.interned_svg
,
813 code
= svgtiny_parse_svg(child
, state
);
814 else if (dom_string_caseless_isequal(state
.interned_g
,
816 code
= svgtiny_parse_svg(child
, state
);
817 else if (dom_string_caseless_isequal(state
.interned_a
,
819 code
= svgtiny_parse_svg(child
, state
);
820 else if (dom_string_caseless_isequal(state
.interned_path
,
822 code
= svgtiny_parse_path(child
, state
);
823 else if (dom_string_caseless_isequal(state
.interned_rect
,
825 code
= svgtiny_parse_rect(child
, state
);
826 else if (dom_string_caseless_isequal(state
.interned_circle
,
828 code
= svgtiny_parse_circle(child
, state
);
829 else if (dom_string_caseless_isequal(state
.interned_ellipse
,
831 code
= svgtiny_parse_ellipse(child
, state
);
832 else if (dom_string_caseless_isequal(state
.interned_line
,
834 code
= svgtiny_parse_line(child
, state
);
835 else if (dom_string_caseless_isequal(state
.interned_polyline
,
837 code
= svgtiny_parse_poly(child
, state
, false);
838 else if (dom_string_caseless_isequal(state
.interned_polygon
,
840 code
= svgtiny_parse_poly(child
, state
, true);
841 else if (dom_string_caseless_isequal(state
.interned_text
,
843 code
= svgtiny_parse_text(child
, state
);
844 dom_string_unref(nodename
);
846 if (code
!= svgtiny_OK
) {
847 dom_node_unref(child
);
848 svgtiny_cleanup_state_local(&state
);
851 exc
= dom_node_get_next_sibling(child
,
852 (dom_node
**) (void *) &next
);
853 dom_node_unref(child
);
854 if (exc
!= DOM_NO_ERR
) {
855 svgtiny_cleanup_state_local(&state
);
856 return svgtiny_LIBDOM_ERROR
;
861 svgtiny_cleanup_state_local(&state
);
868 * Parse a <path> element node.
870 * http://www.w3.org/TR/SVG11/paths#PathElement
873 svgtiny_code
svgtiny_parse_path(dom_element
*path
,
874 struct svgtiny_parse_state state
)
877 dom_string
*path_d_str
;
880 float *p
; /* path elemets */
881 unsigned int palloc
; /* number of path elements allocated */
883 float last_x
= 0, last_y
= 0;
884 float last_cubic_x
= 0, last_cubic_y
= 0;
885 float last_quad_x
= 0, last_quad_y
= 0;
886 float subpath_first_x
= 0, subpath_first_y
= 0;
888 svgtiny_setup_state_local(&state
);
890 svgtiny_parse_paint_attributes(path
, &state
);
891 svgtiny_parse_transform_attributes(path
, &state
);
893 /* read d attribute */
894 exc
= dom_element_get_attribute(path
, state
.interned_d
, &path_d_str
);
895 if (exc
!= DOM_NO_ERR
) {
896 state
.diagram
->error_line
= -1; /* path->line; */
897 state
.diagram
->error_message
= "path: error retrieving d attribute";
898 svgtiny_cleanup_state_local(&state
);
899 return svgtiny_SVG_ERROR
;
902 if (path_d_str
== NULL
) {
903 state
.diagram
->error_line
= -1; /* path->line; */
904 state
.diagram
->error_message
= "path: missing d attribute";
905 svgtiny_cleanup_state_local(&state
);
906 return svgtiny_SVG_ERROR
;
909 /* empty path is permitted it just disables the path */
910 palloc
= dom_string_byte_length(path_d_str
);
912 dom_string_unref(path_d_str
);
913 svgtiny_cleanup_state_local(&state
);
917 /* local copy of the path data allowing in-place modification */
918 s
= path_d
= strndup(dom_string_data(path_d_str
), palloc
);
919 dom_string_unref(path_d_str
);
921 svgtiny_cleanup_state_local(&state
);
922 return svgtiny_OUT_OF_MEMORY
;
925 /* ensure path element allocation is sensibly bounded */
928 } else if (palloc
> 64) {
932 /* allocate initial space for path elements */
933 p
= malloc(sizeof p
[0] * palloc
);
936 svgtiny_cleanup_state_local(&state
);
937 return svgtiny_OUT_OF_MEMORY
;
940 /* parse d and build path */
941 for (i
= 0; s
[i
]; i
++)
948 float x
, y
, x1
, y1
, x2
, y2
, rx
, ry
, rotation
, large_arc
, sweep
;
951 /* Ensure there is sufficient space for path elements */
952 #define ALLOC_PATH_ELEMENTS(NUM_ELEMENTS) \
954 if ((palloc - i) < NUM_ELEMENTS) { \
956 palloc = (palloc * 2) + (palloc / 2); \
957 tp = realloc(p, sizeof p[0] * palloc); \
961 svgtiny_cleanup_state_local(&state); \
962 return svgtiny_OUT_OF_MEMORY; \
969 /* moveto (M, m), lineto (L, l) (2 arguments) */
970 if (sscanf(s
, " %1[MmLl] %f %f %n", command
, &x
, &y
, &n
) == 3) {
971 /*LOG(("moveto or lineto"));*/
972 if (*command
== 'M' || *command
== 'm')
973 plot_command
= svgtiny_PATH_MOVE
;
975 plot_command
= svgtiny_PATH_LINE
;
977 ALLOC_PATH_ELEMENTS(3);
978 p
[i
++] = plot_command
;
979 if ('a' <= *command
) {
983 if (plot_command
== svgtiny_PATH_MOVE
) {
987 p
[i
++] = last_cubic_x
= last_quad_x
= last_x
989 p
[i
++] = last_cubic_y
= last_quad_y
= last_y
992 plot_command
= svgtiny_PATH_LINE
;
993 } while (sscanf(s
, "%f %f %n", &x
, &y
, &n
) == 2);
995 /* closepath (Z, z) (no arguments) */
996 } else if (sscanf(s
, " %1[Zz] %n", command
, &n
) == 1) {
997 /*LOG(("closepath"));*/
998 ALLOC_PATH_ELEMENTS(1);
1000 p
[i
++] = svgtiny_PATH_CLOSE
;
1002 last_cubic_x
= last_quad_x
= last_x
= subpath_first_x
;
1003 last_cubic_y
= last_quad_y
= last_y
= subpath_first_y
;
1005 /* horizontal lineto (H, h) (1 argument) */
1006 } else if (sscanf(s
, " %1[Hh] %f %n", command
, &x
, &n
) == 2) {
1007 /*LOG(("horizontal lineto"));*/
1009 ALLOC_PATH_ELEMENTS(3);
1011 p
[i
++] = svgtiny_PATH_LINE
;
1012 if (*command
== 'h')
1014 p
[i
++] = last_cubic_x
= last_quad_x
= last_x
1016 p
[i
++] = last_cubic_y
= last_quad_y
= last_y
;
1018 } while (sscanf(s
, "%f %n", &x
, &n
) == 1);
1020 /* vertical lineto (V, v) (1 argument) */
1021 } else if (sscanf(s
, " %1[Vv] %f %n", command
, &y
, &n
) == 2) {
1022 /*LOG(("vertical lineto"));*/
1024 ALLOC_PATH_ELEMENTS(3);
1026 p
[i
++] = svgtiny_PATH_LINE
;
1027 if (*command
== 'v')
1029 p
[i
++] = last_cubic_x
= last_quad_x
= last_x
;
1030 p
[i
++] = last_cubic_y
= last_quad_y
= last_y
1033 } while (sscanf(s
, "%f %n", &y
, &n
) == 1);
1035 /* curveto (C, c) (6 arguments) */
1036 } else if (sscanf(s
, " %1[Cc] %f %f %f %f %f %f %n", command
,
1037 &x1
, &y1
, &x2
, &y2
, &x
, &y
, &n
) == 7) {
1038 /*LOG(("curveto"));*/
1040 ALLOC_PATH_ELEMENTS(7);
1042 p
[i
++] = svgtiny_PATH_BEZIER
;
1043 if (*command
== 'c') {
1053 p
[i
++] = last_cubic_x
= x2
;
1054 p
[i
++] = last_cubic_y
= y2
;
1055 p
[i
++] = last_quad_x
= last_x
= x
;
1056 p
[i
++] = last_quad_y
= last_y
= y
;
1058 } while (sscanf(s
, "%f %f %f %f %f %f %n",
1059 &x1
, &y1
, &x2
, &y2
, &x
, &y
, &n
) == 6);
1061 /* shorthand/smooth curveto (S, s) (4 arguments) */
1062 } else if (sscanf(s
, " %1[Ss] %f %f %f %f %n", command
,
1063 &x2
, &y2
, &x
, &y
, &n
) == 5) {
1064 /*LOG(("shorthand/smooth curveto"));*/
1066 ALLOC_PATH_ELEMENTS(7);
1068 p
[i
++] = svgtiny_PATH_BEZIER
;
1069 x1
= last_x
+ (last_x
- last_cubic_x
);
1070 y1
= last_y
+ (last_y
- last_cubic_y
);
1071 if (*command
== 's') {
1079 p
[i
++] = last_cubic_x
= x2
;
1080 p
[i
++] = last_cubic_y
= y2
;
1081 p
[i
++] = last_quad_x
= last_x
= x
;
1082 p
[i
++] = last_quad_y
= last_y
= y
;
1084 } while (sscanf(s
, "%f %f %f %f %n",
1085 &x2
, &y2
, &x
, &y
, &n
) == 4);
1087 /* quadratic Bezier curveto (Q, q) (4 arguments) */
1088 } else if (sscanf(s
, " %1[Qq] %f %f %f %f %n", command
,
1089 &x1
, &y1
, &x
, &y
, &n
) == 5) {
1090 /*LOG(("quadratic Bezier curveto"));*/
1092 ALLOC_PATH_ELEMENTS(7);
1094 p
[i
++] = svgtiny_PATH_BEZIER
;
1097 if (*command
== 'q') {
1103 p
[i
++] = 1./3 * last_x
+ 2./3 * x1
;
1104 p
[i
++] = 1./3 * last_y
+ 2./3 * y1
;
1105 p
[i
++] = 2./3 * x1
+ 1./3 * x
;
1106 p
[i
++] = 2./3 * y1
+ 1./3 * y
;
1107 p
[i
++] = last_cubic_x
= last_x
= x
;
1108 p
[i
++] = last_cubic_y
= last_y
= y
;
1110 } while (sscanf(s
, "%f %f %f %f %n",
1111 &x1
, &y1
, &x
, &y
, &n
) == 4);
1113 /* shorthand/smooth quadratic Bezier curveto (T, t)
1115 } else if (sscanf(s
, " %1[Tt] %f %f %n", command
,
1117 /*LOG(("shorthand/smooth quadratic Bezier curveto"));*/
1119 ALLOC_PATH_ELEMENTS(7);
1121 p
[i
++] = svgtiny_PATH_BEZIER
;
1122 x1
= last_x
+ (last_x
- last_quad_x
);
1123 y1
= last_y
+ (last_y
- last_quad_y
);
1126 if (*command
== 't') {
1132 p
[i
++] = 1./3 * last_x
+ 2./3 * x1
;
1133 p
[i
++] = 1./3 * last_y
+ 2./3 * y1
;
1134 p
[i
++] = 2./3 * x1
+ 1./3 * x
;
1135 p
[i
++] = 2./3 * y1
+ 1./3 * y
;
1136 p
[i
++] = last_cubic_x
= last_x
= x
;
1137 p
[i
++] = last_cubic_y
= last_y
= y
;
1139 } while (sscanf(s
, "%f %f %n",
1142 /* elliptical arc (A, a) (7 arguments) */
1143 } else if (sscanf(s
, " %1[Aa] %f %f %f %f %f %f %f %n", command
,
1144 &rx
, &ry
, &rotation
, &large_arc
, &sweep
,
1148 double bzpoints
[6*4]; /* allow for up to four bezier segments per arc */
1150 if (*command
== 'a') {
1155 bzsegments
= svgarc_to_bezier(last_x
, last_y
,
1162 if (bzsegments
== -1) {
1164 ALLOC_PATH_ELEMENTS(3);
1165 p
[i
++] = svgtiny_PATH_LINE
;
1168 } else if (bzsegments
> 0) {
1170 ALLOC_PATH_ELEMENTS((unsigned int)bzsegments
* 7);
1171 for (bzpnt
= 0;bzpnt
< (bzsegments
* 6); bzpnt
+=6) {
1172 p
[i
++] = svgtiny_PATH_BEZIER
;
1173 p
[i
++] = bzpoints
[bzpnt
];
1174 p
[i
++] = bzpoints
[bzpnt
+1];
1175 p
[i
++] = bzpoints
[bzpnt
+2];
1176 p
[i
++] = bzpoints
[bzpnt
+3];
1177 p
[i
++] = bzpoints
[bzpnt
+4];
1178 p
[i
++] = bzpoints
[bzpnt
+5];
1181 if (bzsegments
!= 0) {
1182 last_cubic_x
= last_quad_x
= last_x
= p
[i
-2];
1183 last_cubic_y
= last_quad_y
= last_y
= p
[i
-1];
1188 } while (sscanf(s
, "%f %f %f %f %f %f %f %n",
1189 &rx
, &ry
, &rotation
, &large_arc
, &sweep
,
1193 /* fprintf(stderr, "parse failed at \"%s\"\n", s); */
1201 /* no real segments in path */
1203 svgtiny_cleanup_state_local(&state
);
1207 /* resize path element array to not be over allocated */
1211 /* try the resize, if it fails just continue to use the old
1214 tp
= realloc(p
, sizeof p
[0] * i
);
1220 err
= svgtiny_add_path(p
, i
, &state
);
1222 svgtiny_cleanup_state_local(&state
);
1229 * Parse a <rect> element node.
1231 * http://www.w3.org/TR/SVG11/shapes#RectElement
1234 svgtiny_code
svgtiny_parse_rect(dom_element
*rect
,
1235 struct svgtiny_parse_state state
)
1238 float x
, y
, width
, height
;
1241 svgtiny_setup_state_local(&state
);
1243 svgtiny_parse_position_attributes(rect
, state
,
1244 &x
, &y
, &width
, &height
);
1245 svgtiny_parse_paint_attributes(rect
, &state
);
1246 svgtiny_parse_transform_attributes(rect
, &state
);
1248 p
= malloc(13 * sizeof p
[0]);
1250 svgtiny_cleanup_state_local(&state
);
1251 return svgtiny_OUT_OF_MEMORY
;
1254 p
[0] = svgtiny_PATH_MOVE
;
1257 p
[3] = svgtiny_PATH_LINE
;
1260 p
[6] = svgtiny_PATH_LINE
;
1263 p
[9] = svgtiny_PATH_LINE
;
1266 p
[12] = svgtiny_PATH_CLOSE
;
1268 err
= svgtiny_add_path(p
, 13, &state
);
1270 svgtiny_cleanup_state_local(&state
);
1277 * Parse a <circle> element node.
1280 svgtiny_code
svgtiny_parse_circle(dom_element
*circle
,
1281 struct svgtiny_parse_state state
)
1284 float x
= 0, y
= 0, r
= -1;
1289 svgtiny_setup_state_local(&state
);
1291 exc
= dom_element_get_attribute(circle
, state
.interned_cx
, &attr
);
1292 if (exc
!= DOM_NO_ERR
) {
1293 svgtiny_cleanup_state_local(&state
);
1294 return svgtiny_LIBDOM_ERROR
;
1297 x
= svgtiny_parse_length(attr
, state
.viewport_width
, state
);
1299 dom_string_unref(attr
);
1301 exc
= dom_element_get_attribute(circle
, state
.interned_cy
, &attr
);
1302 if (exc
!= DOM_NO_ERR
) {
1303 svgtiny_cleanup_state_local(&state
);
1304 return svgtiny_LIBDOM_ERROR
;
1307 y
= svgtiny_parse_length(attr
, state
.viewport_height
, state
);
1309 dom_string_unref(attr
);
1311 exc
= dom_element_get_attribute(circle
, state
.interned_r
, &attr
);
1312 if (exc
!= DOM_NO_ERR
) {
1313 svgtiny_cleanup_state_local(&state
);
1314 return svgtiny_LIBDOM_ERROR
;
1317 r
= svgtiny_parse_length(attr
, state
.viewport_width
, state
);
1319 dom_string_unref(attr
);
1321 svgtiny_parse_paint_attributes(circle
, &state
);
1322 svgtiny_parse_transform_attributes(circle
, &state
);
1325 state
.diagram
->error_line
= -1; /* circle->line; */
1326 state
.diagram
->error_message
= "circle: r missing or negative";
1327 svgtiny_cleanup_state_local(&state
);
1328 return svgtiny_SVG_ERROR
;
1331 svgtiny_cleanup_state_local(&state
);
1335 p
= malloc(32 * sizeof p
[0]);
1337 svgtiny_cleanup_state_local(&state
);
1338 return svgtiny_OUT_OF_MEMORY
;
1341 p
[0] = svgtiny_PATH_MOVE
;
1344 p
[3] = svgtiny_PATH_BEZIER
;
1346 p
[5] = y
+ r
* KAPPA
;
1347 p
[6] = x
+ r
* KAPPA
;
1351 p
[10] = svgtiny_PATH_BEZIER
;
1352 p
[11] = x
- r
* KAPPA
;
1355 p
[14] = y
+ r
* KAPPA
;
1358 p
[17] = svgtiny_PATH_BEZIER
;
1360 p
[19] = y
- r
* KAPPA
;
1361 p
[20] = x
- r
* KAPPA
;
1365 p
[24] = svgtiny_PATH_BEZIER
;
1366 p
[25] = x
+ r
* KAPPA
;
1369 p
[28] = y
- r
* KAPPA
;
1372 p
[31] = svgtiny_PATH_CLOSE
;
1374 err
= svgtiny_add_path(p
, 32, &state
);
1376 svgtiny_cleanup_state_local(&state
);
1383 * Parse an <ellipse> element node.
1386 svgtiny_code
svgtiny_parse_ellipse(dom_element
*ellipse
,
1387 struct svgtiny_parse_state state
)
1390 float x
= 0, y
= 0, rx
= -1, ry
= -1;
1395 svgtiny_setup_state_local(&state
);
1397 exc
= dom_element_get_attribute(ellipse
, state
.interned_cx
, &attr
);
1398 if (exc
!= DOM_NO_ERR
) {
1399 svgtiny_cleanup_state_local(&state
);
1400 return svgtiny_LIBDOM_ERROR
;
1403 x
= svgtiny_parse_length(attr
, state
.viewport_width
, state
);
1405 dom_string_unref(attr
);
1407 exc
= dom_element_get_attribute(ellipse
, state
.interned_cy
, &attr
);
1408 if (exc
!= DOM_NO_ERR
) {
1409 svgtiny_cleanup_state_local(&state
);
1410 return svgtiny_LIBDOM_ERROR
;
1413 y
= svgtiny_parse_length(attr
, state
.viewport_height
, state
);
1415 dom_string_unref(attr
);
1417 exc
= dom_element_get_attribute(ellipse
, state
.interned_rx
, &attr
);
1418 if (exc
!= DOM_NO_ERR
) {
1419 svgtiny_cleanup_state_local(&state
);
1420 return svgtiny_LIBDOM_ERROR
;
1423 rx
= svgtiny_parse_length(attr
, state
.viewport_width
, state
);
1425 dom_string_unref(attr
);
1427 exc
= dom_element_get_attribute(ellipse
, state
.interned_ry
, &attr
);
1428 if (exc
!= DOM_NO_ERR
) {
1429 svgtiny_cleanup_state_local(&state
);
1430 return svgtiny_LIBDOM_ERROR
;
1433 ry
= svgtiny_parse_length(attr
, state
.viewport_width
, state
);
1435 dom_string_unref(attr
);
1437 svgtiny_parse_paint_attributes(ellipse
, &state
);
1438 svgtiny_parse_transform_attributes(ellipse
, &state
);
1440 if (rx
< 0 || ry
< 0) {
1441 state
.diagram
->error_line
= -1; /* ellipse->line; */
1442 state
.diagram
->error_message
= "ellipse: rx or ry missing "
1444 svgtiny_cleanup_state_local(&state
);
1445 return svgtiny_SVG_ERROR
;
1447 if (rx
== 0 || ry
== 0) {
1448 svgtiny_cleanup_state_local(&state
);
1452 p
= malloc(32 * sizeof p
[0]);
1454 svgtiny_cleanup_state_local(&state
);
1455 return svgtiny_OUT_OF_MEMORY
;
1458 p
[0] = svgtiny_PATH_MOVE
;
1461 p
[3] = svgtiny_PATH_BEZIER
;
1463 p
[5] = y
+ ry
* KAPPA
;
1464 p
[6] = x
+ rx
* KAPPA
;
1468 p
[10] = svgtiny_PATH_BEZIER
;
1469 p
[11] = x
- rx
* KAPPA
;
1472 p
[14] = y
+ ry
* KAPPA
;
1475 p
[17] = svgtiny_PATH_BEZIER
;
1477 p
[19] = y
- ry
* KAPPA
;
1478 p
[20] = x
- rx
* KAPPA
;
1482 p
[24] = svgtiny_PATH_BEZIER
;
1483 p
[25] = x
+ rx
* KAPPA
;
1486 p
[28] = y
- ry
* KAPPA
;
1489 p
[31] = svgtiny_PATH_CLOSE
;
1491 err
= svgtiny_add_path(p
, 32, &state
);
1493 svgtiny_cleanup_state_local(&state
);
1500 * Parse a <line> element node.
1503 svgtiny_code
svgtiny_parse_line(dom_element
*line
,
1504 struct svgtiny_parse_state state
)
1507 float x1
= 0, y1
= 0, x2
= 0, y2
= 0;
1512 svgtiny_setup_state_local(&state
);
1514 exc
= dom_element_get_attribute(line
, state
.interned_x1
, &attr
);
1515 if (exc
!= DOM_NO_ERR
) {
1516 svgtiny_cleanup_state_local(&state
);
1517 return svgtiny_LIBDOM_ERROR
;
1520 x1
= svgtiny_parse_length(attr
, state
.viewport_width
, state
);
1522 dom_string_unref(attr
);
1524 exc
= dom_element_get_attribute(line
, state
.interned_y1
, &attr
);
1525 if (exc
!= DOM_NO_ERR
) {
1526 svgtiny_cleanup_state_local(&state
);
1527 return svgtiny_LIBDOM_ERROR
;
1530 y1
= svgtiny_parse_length(attr
, state
.viewport_height
, state
);
1532 dom_string_unref(attr
);
1534 exc
= dom_element_get_attribute(line
, state
.interned_x2
, &attr
);
1535 if (exc
!= DOM_NO_ERR
) {
1536 svgtiny_cleanup_state_local(&state
);
1537 return svgtiny_LIBDOM_ERROR
;
1540 x2
= svgtiny_parse_length(attr
, state
.viewport_width
, state
);
1542 dom_string_unref(attr
);
1544 exc
= dom_element_get_attribute(line
, state
.interned_y2
, &attr
);
1545 if (exc
!= DOM_NO_ERR
) {
1546 svgtiny_cleanup_state_local(&state
);
1547 return svgtiny_LIBDOM_ERROR
;
1550 y2
= svgtiny_parse_length(attr
, state
.viewport_height
, state
);
1552 dom_string_unref(attr
);
1554 svgtiny_parse_paint_attributes(line
, &state
);
1555 svgtiny_parse_transform_attributes(line
, &state
);
1557 p
= malloc(7 * sizeof p
[0]);
1559 svgtiny_cleanup_state_local(&state
);
1560 return svgtiny_OUT_OF_MEMORY
;
1563 p
[0] = svgtiny_PATH_MOVE
;
1566 p
[3] = svgtiny_PATH_LINE
;
1569 p
[6] = svgtiny_PATH_CLOSE
;
1571 err
= svgtiny_add_path(p
, 7, &state
);
1573 svgtiny_cleanup_state_local(&state
);
1580 * Parse a <polyline> or <polygon> element node.
1582 * http://www.w3.org/TR/SVG11/shapes#PolylineElement
1583 * http://www.w3.org/TR/SVG11/shapes#PolygonElement
1586 svgtiny_code
svgtiny_parse_poly(dom_element
*poly
,
1587 struct svgtiny_parse_state state
, bool polygon
)
1590 dom_string
*points_str
;
1596 svgtiny_setup_state_local(&state
);
1598 svgtiny_parse_paint_attributes(poly
, &state
);
1599 svgtiny_parse_transform_attributes(poly
, &state
);
1601 exc
= dom_element_get_attribute(poly
, state
.interned_points
,
1603 if (exc
!= DOM_NO_ERR
) {
1604 svgtiny_cleanup_state_local(&state
);
1605 return svgtiny_LIBDOM_ERROR
;
1608 if (points_str
== NULL
) {
1609 state
.diagram
->error_line
= -1; /* poly->line; */
1610 state
.diagram
->error_message
=
1611 "polyline/polygon: missing points attribute";
1612 svgtiny_cleanup_state_local(&state
);
1613 return svgtiny_SVG_ERROR
;
1616 s
= points
= strndup(dom_string_data(points_str
),
1617 dom_string_byte_length(points_str
));
1618 dom_string_unref(points_str
);
1619 /* read points attribute */
1621 svgtiny_cleanup_state_local(&state
);
1622 return svgtiny_OUT_OF_MEMORY
;
1624 /* allocate space for path: it will never have more elements than s */
1625 p
= malloc(sizeof p
[0] * strlen(s
));
1628 svgtiny_cleanup_state_local(&state
);
1629 return svgtiny_OUT_OF_MEMORY
;
1632 /* parse s and build path */
1633 for (i
= 0; s
[i
]; i
++)
1641 if (sscanf(s
, "%f %f %n", &x
, &y
, &n
) == 2) {
1643 p
[i
++] = svgtiny_PATH_MOVE
;
1645 p
[i
++] = svgtiny_PATH_LINE
;
1654 p
[i
++] = svgtiny_PATH_CLOSE
;
1658 err
= svgtiny_add_path(p
, i
, &state
);
1660 svgtiny_cleanup_state_local(&state
);
1667 * Parse a <text> or <tspan> element node.
1670 svgtiny_code
svgtiny_parse_text(dom_element
*text
,
1671 struct svgtiny_parse_state state
)
1673 float x
, y
, width
, height
;
1678 svgtiny_setup_state_local(&state
);
1680 svgtiny_parse_position_attributes(text
, state
,
1681 &x
, &y
, &width
, &height
);
1682 svgtiny_parse_font_attributes(text
, &state
);
1683 svgtiny_parse_transform_attributes(text
, &state
);
1685 px
= state
.ctm
.a
* x
+ state
.ctm
.c
* y
+ state
.ctm
.e
;
1686 py
= state
.ctm
.b
* x
+ state
.ctm
.d
* y
+ state
.ctm
.f
;
1687 /* state.ctm.e = px - state.origin_x; */
1688 /* state.ctm.f = py - state.origin_y; */
1690 /*struct css_style style = state.style;
1691 style.font_size.value.length.value *= state.ctm.a;*/
1693 exc
= dom_node_get_first_child(text
, &child
);
1694 if (exc
!= DOM_NO_ERR
) {
1695 return svgtiny_LIBDOM_ERROR
;
1696 svgtiny_cleanup_state_local(&state
);
1698 while (child
!= NULL
) {
1700 dom_node_type nodetype
;
1701 svgtiny_code code
= svgtiny_OK
;
1703 exc
= dom_node_get_node_type(child
, &nodetype
);
1704 if (exc
!= DOM_NO_ERR
) {
1705 dom_node_unref(child
);
1706 svgtiny_cleanup_state_local(&state
);
1707 return svgtiny_LIBDOM_ERROR
;
1709 if (nodetype
== DOM_ELEMENT_NODE
) {
1710 dom_string
*nodename
;
1711 exc
= dom_node_get_node_name(child
, &nodename
);
1712 if (exc
!= DOM_NO_ERR
) {
1713 dom_node_unref(child
);
1714 svgtiny_cleanup_state_local(&state
);
1715 return svgtiny_LIBDOM_ERROR
;
1717 if (dom_string_caseless_isequal(nodename
,
1718 state
.interned_tspan
))
1719 code
= svgtiny_parse_text((dom_element
*)child
,
1721 dom_string_unref(nodename
);
1722 } else if (nodetype
== DOM_TEXT_NODE
) {
1723 struct svgtiny_shape
*shape
= svgtiny_add_shape(&state
);
1724 dom_string
*content
;
1725 if (shape
== NULL
) {
1726 dom_node_unref(child
);
1727 svgtiny_cleanup_state_local(&state
);
1728 return svgtiny_OUT_OF_MEMORY
;
1730 exc
= dom_text_get_whole_text(child
, &content
);
1731 if (exc
!= DOM_NO_ERR
) {
1732 dom_node_unref(child
);
1733 svgtiny_cleanup_state_local(&state
);
1734 return svgtiny_LIBDOM_ERROR
;
1736 if (content
!= NULL
) {
1737 shape
->text
= strndup(dom_string_data(content
),
1738 dom_string_byte_length(content
));
1739 dom_string_unref(content
);
1741 shape
->text
= strdup("");
1745 state
.diagram
->shape_count
++;
1748 if (code
!= svgtiny_OK
) {
1749 dom_node_unref(child
);
1750 svgtiny_cleanup_state_local(&state
);
1753 exc
= dom_node_get_next_sibling(child
, &next
);
1754 dom_node_unref(child
);
1755 if (exc
!= DOM_NO_ERR
) {
1756 svgtiny_cleanup_state_local(&state
);
1757 return svgtiny_LIBDOM_ERROR
;
1762 svgtiny_cleanup_state_local(&state
);
1769 * Parse x, y, width, and height attributes, if present.
1772 void svgtiny_parse_position_attributes(dom_element
*node
,
1773 const struct svgtiny_parse_state state
,
1774 float *x
, float *y
, float *width
, float *height
)
1781 *width
= state
.viewport_width
;
1782 *height
= state
.viewport_height
;
1784 exc
= dom_element_get_attribute(node
, state
.interned_x
, &attr
);
1785 if (exc
== DOM_NO_ERR
&& attr
!= NULL
) {
1786 *x
= svgtiny_parse_length(attr
, state
.viewport_width
, state
);
1787 dom_string_unref(attr
);
1790 exc
= dom_element_get_attribute(node
, state
.interned_y
, &attr
);
1791 if (exc
== DOM_NO_ERR
&& attr
!= NULL
) {
1792 *y
= svgtiny_parse_length(attr
, state
.viewport_height
, state
);
1793 dom_string_unref(attr
);
1796 exc
= dom_element_get_attribute(node
, state
.interned_width
, &attr
);
1797 if (exc
== DOM_NO_ERR
&& attr
!= NULL
) {
1798 *width
= svgtiny_parse_length(attr
, state
.viewport_width
,
1800 dom_string_unref(attr
);
1803 exc
= dom_element_get_attribute(node
, state
.interned_height
, &attr
);
1804 if (exc
== DOM_NO_ERR
&& attr
!= NULL
) {
1805 *height
= svgtiny_parse_length(attr
, state
.viewport_height
,
1807 dom_string_unref(attr
);
1813 * Parse a length as a number of pixels.
1816 static float _svgtiny_parse_length(const char *s
, int viewport_size
,
1817 const struct svgtiny_parse_state state
)
1819 int num_length
= strspn(s
, "0123456789+-.");
1820 const char *unit
= s
+ num_length
;
1821 float n
= atof((const char *) s
);
1822 float font_size
= 20; /*css_len2px(&state.style.font_size.value.length, 0);*/
1828 } else if (unit
[0] == '%') {
1829 return n
/ 100.0 * viewport_size
;
1830 } else if (unit
[0] == 'e' && unit
[1] == 'm') {
1831 return n
* font_size
;
1832 } else if (unit
[0] == 'e' && unit
[1] == 'x') {
1833 return n
/ 2.0 * font_size
;
1834 } else if (unit
[0] == 'p' && unit
[1] == 'x') {
1836 } else if (unit
[0] == 'p' && unit
[1] == 't') {
1838 } else if (unit
[0] == 'p' && unit
[1] == 'c') {
1840 } else if (unit
[0] == 'm' && unit
[1] == 'm') {
1841 return n
* 3.543307;
1842 } else if (unit
[0] == 'c' && unit
[1] == 'm') {
1843 return n
* 35.43307;
1844 } else if (unit
[0] == 'i' && unit
[1] == 'n') {
1851 float svgtiny_parse_length(dom_string
*s
, int viewport_size
,
1852 const struct svgtiny_parse_state state
)
1854 char *ss
= strndup(dom_string_data(s
), dom_string_byte_length(s
));
1855 float ret
= _svgtiny_parse_length(ss
, viewport_size
, state
);
1861 * Parse paint attributes, if present.
1864 void svgtiny_parse_paint_attributes(dom_element
*node
,
1865 struct svgtiny_parse_state
*state
)
1870 exc
= dom_element_get_attribute(node
, state
->interned_fill
, &attr
);
1871 if (exc
== DOM_NO_ERR
&& attr
!= NULL
) {
1872 svgtiny_parse_color(attr
, &state
->fill
, &state
->fill_grad
, state
);
1873 dom_string_unref(attr
);
1876 exc
= dom_element_get_attribute(node
, state
->interned_stroke
, &attr
);
1877 if (exc
== DOM_NO_ERR
&& attr
!= NULL
) {
1878 svgtiny_parse_color(attr
, &state
->stroke
, &state
->stroke_grad
, state
);
1879 dom_string_unref(attr
);
1882 exc
= dom_element_get_attribute(node
, state
->interned_stroke_width
, &attr
);
1883 if (exc
== DOM_NO_ERR
&& attr
!= NULL
) {
1884 state
->stroke_width
= svgtiny_parse_length(attr
,
1885 state
->viewport_width
, *state
);
1886 dom_string_unref(attr
);
1889 exc
= dom_element_get_attribute(node
, state
->interned_style
, &attr
);
1890 if (exc
== DOM_NO_ERR
&& attr
!= NULL
) {
1891 char *style
= strndup(dom_string_data(attr
),
1892 dom_string_byte_length(attr
));
1895 if ((s
= strstr(style
, "fill:"))) {
1899 value
= strndup(s
, strcspn(s
, "; "));
1900 _svgtiny_parse_color(value
, &state
->fill
, &state
->fill_grad
, state
);
1903 if ((s
= strstr(style
, "stroke:"))) {
1907 value
= strndup(s
, strcspn(s
, "; "));
1908 _svgtiny_parse_color(value
, &state
->stroke
, &state
->stroke_grad
, state
);
1911 if ((s
= strstr(style
, "stroke-width:"))) {
1915 value
= strndup(s
, strcspn(s
, "; "));
1916 state
->stroke_width
= _svgtiny_parse_length(value
,
1917 state
->viewport_width
, *state
);
1921 dom_string_unref(attr
);
1930 static void _svgtiny_parse_color(const char *s
, svgtiny_colour
*c
,
1931 struct svgtiny_parse_state_gradient
*grad
,
1932 struct svgtiny_parse_state
*state
)
1934 unsigned int r
, g
, b
;
1936 size_t len
= strlen(s
);
1937 char *id
= 0, *rparen
;
1939 if (len
== 4 && s
[0] == '#') {
1940 if (sscanf(s
+ 1, "%1x%1x%1x", &r
, &g
, &b
) == 3)
1941 *c
= svgtiny_RGB(r
| r
<< 4, g
| g
<< 4, b
| b
<< 4);
1943 } else if (len
== 7 && s
[0] == '#') {
1944 if (sscanf(s
+ 1, "%2x%2x%2x", &r
, &g
, &b
) == 3)
1945 *c
= svgtiny_RGB(r
, g
, b
);
1947 } else if (10 <= len
&& s
[0] == 'r' && s
[1] == 'g' && s
[2] == 'b' &&
1948 s
[3] == '(' && s
[len
- 1] == ')') {
1949 if (sscanf(s
+ 4, "%u,%u,%u", &r
, &g
, &b
) == 3)
1950 *c
= svgtiny_RGB(r
, g
, b
);
1951 else if (sscanf(s
+ 4, "%f%%,%f%%,%f%%", &rf
, &gf
, &bf
) == 3) {
1955 *c
= svgtiny_RGB(r
, g
, b
);
1958 } else if (len
== 4 && strcmp(s
, "none") == 0) {
1959 *c
= svgtiny_TRANSPARENT
;
1961 } else if (5 < len
&& s
[0] == 'u' && s
[1] == 'r' && s
[2] == 'l' &&
1964 *c
= svgtiny_RGB(0, 0, 0);
1965 } else if (s
[4] == '#') {
1969 rparen
= strchr(id
, ')');
1972 svgtiny_find_gradient(id
, grad
, state
);
1974 if (grad
->linear_gradient_stop_count
== 0)
1975 *c
= svgtiny_TRANSPARENT
;
1976 else if (grad
->linear_gradient_stop_count
== 1)
1977 *c
= grad
->gradient_stop
[0].color
;
1979 *c
= svgtiny_LINEAR_GRADIENT
;
1983 const struct svgtiny_named_color
*named_color
;
1984 named_color
= svgtiny_color_lookup(s
, strlen(s
));
1986 *c
= named_color
->color
;
1990 void svgtiny_parse_color(dom_string
*s
, svgtiny_colour
*c
,
1991 struct svgtiny_parse_state_gradient
*grad
,
1992 struct svgtiny_parse_state
*state
)
1995 _svgtiny_parse_color(dom_string_data(s
), c
, grad
, state
);
1996 dom_string_unref(s
);
2000 * Parse font attributes, if present.
2003 void svgtiny_parse_font_attributes(dom_element
*node
,
2004 struct svgtiny_parse_state
*state
)
2006 /* TODO: Implement this, it never used to be */
2009 #ifdef WRITTEN_THIS_PROPERLY
2010 const xmlAttr
*attr
;
2014 for (attr
= node
->properties
; attr
; attr
= attr
->next
) {
2015 if (strcmp((const char *) attr
->name
, "font-size") == 0) {
2016 /*if (css_parse_length(
2017 (const char *) attr->children->content,
2018 &state->style.font_size.value.length,
2020 state->style.font_size.size =
2021 CSS_FONT_SIZE_LENGTH;
2030 * Parse transform attributes, if present.
2032 * http://www.w3.org/TR/SVG11/coords#TransformAttribute
2035 void svgtiny_parse_transform_attributes(dom_element
*node
,
2036 struct svgtiny_parse_state
*state
)
2042 exc
= dom_element_get_attribute(node
, state
->interned_transform
,
2044 if (exc
== DOM_NO_ERR
&& attr
!= NULL
) {
2045 transform
= strndup(dom_string_data(attr
),
2046 dom_string_byte_length(attr
));
2047 svgtiny_parse_transform(transform
, &state
->ctm
.a
, &state
->ctm
.b
,
2048 &state
->ctm
.c
, &state
->ctm
.d
,
2049 &state
->ctm
.e
, &state
->ctm
.f
);
2051 dom_string_unref(attr
);
2057 * Parse a transform string.
2060 void svgtiny_parse_transform(char *s
, float *ma
, float *mb
,
2061 float *mc
, float *md
, float *me
, float *mf
)
2063 float a
, b
, c
, d
, e
, f
;
2064 float za
, zb
, zc
, zd
, ze
, zf
;
2069 for (i
= 0; s
[i
]; i
++)
2078 if ((sscanf(s
, " matrix (%f %f %f %f %f %f ) %n",
2079 &a
, &b
, &c
, &d
, &e
, &f
, &n
) == 6) && (n
> 0))
2081 else if ((sscanf(s
, " translate (%f %f ) %n",
2082 &e
, &f
, &n
) == 2) && (n
> 0))
2084 else if ((sscanf(s
, " translate (%f ) %n",
2085 &e
, &n
) == 1) && (n
> 0))
2087 else if ((sscanf(s
, " scale (%f %f ) %n",
2088 &a
, &d
, &n
) == 2) && (n
> 0))
2090 else if ((sscanf(s
, " scale (%f ) %n",
2091 &a
, &n
) == 1) && (n
> 0))
2093 else if ((sscanf(s
, " rotate (%f %f %f ) %n",
2094 &angle
, &x
, &y
, &n
) == 3) && (n
> 0)) {
2095 angle
= angle
/ 180 * M_PI
;
2100 e
= -x
* cos(angle
) + y
* sin(angle
) + x
;
2101 f
= -x
* sin(angle
) - y
* cos(angle
) + y
;
2102 } else if ((sscanf(s
, " rotate (%f ) %n",
2103 &angle
, &n
) == 1) && (n
> 0)) {
2104 angle
= angle
/ 180 * M_PI
;
2109 } else if ((sscanf(s
, " skewX (%f ) %n",
2110 &angle
, &n
) == 1) && (n
> 0)) {
2111 angle
= angle
/ 180 * M_PI
;
2113 } else if ((sscanf(s
, " skewY (%f ) %n",
2114 &angle
, &n
) == 1) && (n
> 0)) {
2115 angle
= angle
/ 180 * M_PI
;
2119 za
= *ma
* a
+ *mc
* b
;
2120 zb
= *mb
* a
+ *md
* b
;
2121 zc
= *ma
* c
+ *mc
* d
;
2122 zd
= *mb
* c
+ *md
* d
;
2123 ze
= *ma
* e
+ *mc
* f
+ *me
;
2124 zf
= *mb
* e
+ *md
* f
+ *mf
;
2137 * Add a path to the svgtiny_diagram.
2140 svgtiny_code
svgtiny_add_path(float *p
, unsigned int n
,
2141 struct svgtiny_parse_state
*state
)
2143 struct svgtiny_shape
*shape
;
2145 if (state
->fill
== svgtiny_LINEAR_GRADIENT
)
2146 return svgtiny_add_path_linear_gradient(p
, n
, state
);
2148 svgtiny_transform_path(p
, n
, state
);
2150 shape
= svgtiny_add_shape(state
);
2153 return svgtiny_OUT_OF_MEMORY
;
2156 shape
->path_length
= n
;
2157 state
->diagram
->shape_count
++;
2164 * Add a svgtiny_shape to the svgtiny_diagram.
2167 struct svgtiny_shape
*svgtiny_add_shape(struct svgtiny_parse_state
*state
)
2169 struct svgtiny_shape
*shape
= realloc(state
->diagram
->shape
,
2170 (state
->diagram
->shape_count
+ 1) *
2171 sizeof (state
->diagram
->shape
[0]));
2174 state
->diagram
->shape
= shape
;
2176 shape
+= state
->diagram
->shape_count
;
2178 shape
->path_length
= 0;
2180 shape
->fill
= state
->fill
;
2181 shape
->stroke
= state
->stroke
;
2182 shape
->stroke_width
= lroundf((float) state
->stroke_width
*
2183 (state
->ctm
.a
+ state
->ctm
.d
) / 2.0);
2184 if (0 < state
->stroke_width
&& shape
->stroke_width
== 0)
2185 shape
->stroke_width
= 1;
2192 * Apply the current transformation matrix to a path.
2195 void svgtiny_transform_path(float *p
, unsigned int n
,
2196 struct svgtiny_parse_state
*state
)
2200 for (j
= 0; j
!= n
; ) {
2201 unsigned int points
= 0;
2203 switch ((int) p
[j
]) {
2204 case svgtiny_PATH_MOVE
:
2205 case svgtiny_PATH_LINE
:
2208 case svgtiny_PATH_CLOSE
:
2211 case svgtiny_PATH_BEZIER
:
2218 for (k
= 0; k
!= points
; k
++) {
2219 float x0
= p
[j
], y0
= p
[j
+ 1];
2220 float x
= state
->ctm
.a
* x0
+ state
->ctm
.c
* y0
+
2222 float y
= state
->ctm
.b
* x0
+ state
->ctm
.d
* y0
+
2233 * Free all memory used by a diagram.
2236 void svgtiny_free(struct svgtiny_diagram
*svg
)
2241 for (i
= 0; i
!= svg
->shape_count
; i
++) {
2242 free(svg
->shape
[i
].path
);
2243 free(svg
->shape
[i
].text
);
2251 #ifndef HAVE_STRNDUP
2252 char *svgtiny_strndup(const char *s
, size_t n
)
2257 for (len
= 0; len
!= n
&& s
[len
]; len
++)
2260 s2
= malloc(len
+ 1);