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>
20 #include <libcss/libcss.h>
23 #include "svgtiny_internal.h"
25 /* Source file generated by `gperf`. */
26 #include "autogenerated_colors.c"
28 #define TAU 6.28318530717958647692
31 #define M_PI 3.14159265358979323846
35 #define M_PI_2 1.57079632679489661923
38 #define KAPPA 0.5522847498
40 #define degToRad(angleInDegrees) ((angleInDegrees) * M_PI / 180.0)
41 #define radToDeg(angleInRadians) ((angleInRadians) * 180.0 / M_PI)
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
);
79 * rotate midpoint vector
82 rotate_midpoint_vector(float ax
, float ay
,
85 double *x_out
, double *y_out
)
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 */
92 /* compute the sin and cos of the angle */
93 cosangle
= cos(radangle
);
94 sinangle
= sin(radangle
);
96 /* compute the midpoint between start and end points */
97 dx2
= (ax
- bx
) / 2.0;
98 dy2
= (ay
- by
) / 2.0;
100 /* rotate vector to remove angle */
101 *x_out
= ((cosangle
* dx2
) + (sinangle
* dy2
));
102 *y_out
= ((-sinangle
* dx2
) + (cosangle
* dy2
));
107 * ensure the arc radii are large enough and scale as appropriate
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.
114 ensure_radii_scale(double x1_sq
, double y1_sq
,
115 float *rx
, float *ry
,
116 double *rx_sq
, double *ry_sq
)
121 /* set radii square values */
122 (*rx_sq
) = (*rx
) * (*rx
);
123 (*ry_sq
) = (*ry
) * (*ry
);
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
);
139 * compute the transformed centre point
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
)
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
;
154 coef
= (sign
* sqrt(sq
));
156 *cx1
= coef
* ((rx
* y1
) / ry
);
157 *cy1
= coef
* -((ry
* x1
) / rx
);
162 * compute untransformed centre point
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
170 compute_centre_point(float ax
, float ay
,
172 double cx1
, double cy1
,
174 double *x_out
, double *y_out
)
178 double cosangle
; /* cosine of rotation angle */
179 double sinangle
; /* sine of rotation angle */
181 /* compute the sin and cos of the angle */
182 cosangle
= cos(radangle
);
183 sinangle
= sin(radangle
);
185 sx2
= (ax
+ bx
) / 2.0;
186 sy2
= (ay
+ by
) / 2.0;
188 *x_out
= sx2
+ (cosangle
* cx1
- sinangle
* cy1
);
189 *y_out
= sy2
+ (sinangle
* cx1
+ cosangle
* cy1
);
194 * compute the angle start and extent
197 compute_angle_start_extent(float rx
, float ry
,
198 double x1
, double y1
,
199 double cx1
, double cy1
,
200 double *start
, double *extent
)
211 * Angle betwen two vectors is +/- acos( u.v / len(u) * len(v))
213 * '.' is the dot product.
214 * +/- is calculated from the sign of the cross product (u x v)
217 ux
= (x1
- cx1
) / rx
;
218 uy
= (y1
- cy1
) / ry
;
219 vx
= (-x1
- cx1
) / rx
;
220 vy
= (-y1
- cy1
) / ry
;
222 /* compute the start angle */
223 /* The angle between (ux, uy) and the 0 angle */
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 */
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
);
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
;
239 /* arc cos must operate between -1 and 1 */
242 *extent
= sign
* M_PI
;
243 } else if (actmp
> 1.0) {
246 *extent
= sign
* acos(actmp
);
252 * converts a circle centered unit circle arc to a series of bezier curves
254 * Each bezier is stored as six values of three pairs of coordinates
256 * The beziers are stored without their start point as that is assumed
257 * to be the preceding elements end point.
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)
265 circle_arc_to_bezier(double start
, double extent
, double *bzpt
)
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));
279 for (segment
= 0; segment
< bzsegments
; segment
++) {
280 /* first control point */
281 angle
= start
+ (segment
* increment
);
284 bzpt
[pos
++] = dx
- controllen
* dy
;
285 bzpt
[pos
++] = dy
+ controllen
* dx
;
286 /* second control point */
290 bzpt
[pos
++] = dx
+ controllen
* dy
;
291 bzpt
[pos
++] = dy
- controllen
* dx
;
302 * transform coordinate list
304 * perform a scale, rotate and translate on list of coordinates
310 * homogeneous transforms
318 * | cos(an) -sin(an) 0 |
319 * R = | sin(an) cos(an) 0 |
322 * {{cos(a), -sin(a) 0}, {sin(a), cos(a),0}, {0,0,1}}
329 * note order is significat here and the combined matrix is
332 * | cos(an) -sin(an) cx |
333 * T.R = | sin(an) cos(an) cy |
336 * | rx * cos(an) ry * -sin(an) cx |
337 * T.R.S = | rx * sin(an) ry * cos(an) cy |
340 * {{Cos[a], -Sin[a], c}, {Sin[a], Cos[a], d}, {0, 0, 1}} . {{r, 0, 0}, {0, s, 0}, {0, 0, 1}}
352 * x2 = cx + (rx * x1 * cos(a)) + (ry * y1 * -1 * sin(a))
353 * y2 = cy + (ry * y1 * cos(a)) + (rx * x1 * sin(a))
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
365 scale_rotate_translate_points(double rx
, double ry
,
367 double cx
, double cy
,
372 double cosangle
; /* cosine of rotation angle */
373 double sinangle
; /* sine of rotation angle */
374 double rxcosangle
, rxsinangle
, rycosangle
, rynsinangle
;
377 /* compute the sin and cos of the angle */
378 cosangle
= cos(radangle
);
379 sinangle
= sin(radangle
);
381 rxcosangle
= rx
* cosangle
;
382 rxsinangle
= rx
* sinangle
;
383 rycosangle
= ry
* cosangle
;
384 rynsinangle
= ry
* -1 * sinangle
;
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
);
390 points
[pnt
+ 1] = y2
;
396 * convert an svg path arc to a bezier curve
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.
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.
408 * \param bzpoints the array to fill with bezier curves
409 * \return the number of bezier segments generated or -1 for a line
412 svgarc_to_bezier(float start_x
,
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
;
433 if ((start_x
== end_x
) && (start_y
== end_y
)) {
435 * if the start and end coordinates are the same the
436 * svg spec says this is equivalent to having no segment
442 if ((rx
== 0) || (ry
== 0)) {
444 * if either radii is zero the specified behaviour is a line
449 /* obtain the absolute values of the radii */
453 /* convert normalised angle to radians */
454 radangle
= degToRad(fmod(angle
, 360.0));
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
);
461 /* get squared x1 values */
465 /* ensure radii are correctly scaled */
466 ensure_radii_scale(x1_sq
, y1_sq
, &rx
, &ry
, &rx_sq
, &ry_sq
);
468 /* compute the transformed centre point */
469 compute_transformed_centre_point(largearc
== sweep
?-1:1,
477 /* get the untransformed centre point */
478 compute_centre_point(start_x
, start_y
,
485 /* compute anglestart and extent */
486 compute_angle_start_extent(rx
,ry
,
491 /* extent of 0 is a straight line */
496 /* take account of sweep */
497 if (!sweep
&& extent
> 0) {
499 } else if (sweep
&& extent
< 0) {
503 /* normalise start and extent */
504 extent
= fmod(extent
, TAU
);
505 start
= fmod(start
, TAU
);
507 /* convert the arc to unit circle bezier curves */
508 bzsegments
= circle_arc_to_bezier(start
, extent
, bzpoints
);
510 /* transform the bezier curves */
511 scale_rotate_translate_points(rx
, ry
,
522 * Call this to ref the strings in a gradient state.
524 static void svgtiny_grad_string_ref(struct svgtiny_parse_state_gradient
*grad
)
526 if (grad
->gradient_x1
!= NULL
) {
527 dom_string_ref(grad
->gradient_x1
);
529 if (grad
->gradient_y1
!= NULL
) {
530 dom_string_ref(grad
->gradient_y1
);
532 if (grad
->gradient_x2
!= NULL
) {
533 dom_string_ref(grad
->gradient_x2
);
535 if (grad
->gradient_y2
!= NULL
) {
536 dom_string_ref(grad
->gradient_y2
);
541 * Call this to clean up the strings in a gradient state.
543 static void svgtiny_grad_string_cleanup(
544 struct svgtiny_parse_state_gradient
*grad
)
546 if (grad
->gradient_x1
!= NULL
) {
547 dom_string_unref(grad
->gradient_x1
);
548 grad
->gradient_x1
= NULL
;
550 if (grad
->gradient_y1
!= NULL
) {
551 dom_string_unref(grad
->gradient_y1
);
552 grad
->gradient_y1
= NULL
;
554 if (grad
->gradient_x2
!= NULL
) {
555 dom_string_unref(grad
->gradient_x2
);
556 grad
->gradient_x2
= NULL
;
558 if (grad
->gradient_y2
!= NULL
) {
559 dom_string_unref(grad
->gradient_y2
);
560 grad
->gradient_y2
= NULL
;
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.
569 static void svgtiny_setup_state_local(struct svgtiny_parse_state
*state
)
571 svgtiny_grad_string_ref(&(state
->fill_grad
));
572 svgtiny_grad_string_ref(&(state
->stroke_grad
));
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.
580 static void svgtiny_cleanup_state_local(struct svgtiny_parse_state
*state
)
582 svgtiny_grad_string_cleanup(&(state
->fill_grad
));
583 svgtiny_grad_string_cleanup(&(state
->stroke_grad
));
588 * Create a new svgtiny_diagram structure.
591 struct svgtiny_diagram
*svgtiny_create(void)
593 struct svgtiny_diagram
*diagram
;
595 diagram
= calloc(sizeof(*diagram
), 1);
604 static void ignore_msg(uint32_t severity
, void *ctx
, const char *msg
, ...)
612 * Parse a block of memory into a svgtiny_diagram.
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
)
620 dom_document
*document
;
622 dom_xml_parser
*parser
;
625 dom_string
*svg_name
;
626 lwc_string
*svg_name_lwc
;
627 struct svgtiny_parse_state state
;
628 float x
, y
, width
, height
;
637 parser
= dom_xml_parser_create(NULL
, NULL
,
638 ignore_msg
, NULL
, &document
);
641 return svgtiny_LIBDOM_ERROR
;
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
;
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
;
657 /* We're done parsing, drop the parser.
658 * We now own the document entirely.
660 dom_xml_parser_destroy(parser
);
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
;
669 /* no root svg element */
670 dom_node_unref(document
);
671 return svgtiny_SVG_ERROR
;
674 exc
= dom_node_get_node_name(svg
, &svg_name
);
675 if (exc
!= DOM_NO_ERR
) {
677 dom_node_unref(document
);
678 return svgtiny_LIBDOM_ERROR
;
680 if (lwc_intern_string("svg", 3 /* SLEN("svg") */,
681 &svg_name_lwc
) != lwc_error_ok
) {
682 dom_string_unref(svg_name
);
684 dom_node_unref(document
);
685 return svgtiny_LIBDOM_ERROR
;
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
);
691 dom_node_unref(document
);
692 return svgtiny_NOT_SVG
;
695 lwc_string_unref(svg_name_lwc
);
696 dom_string_unref(svg_name
);
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
;
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
) {
711 dom_node_unref(document
);
712 return svgtiny_LIBCSS_ERROR
;
716 #define SVGTINY_STRING_ACTION2(s,n) \
717 if (dom_string_create_interned((const uint8_t *) #n, \
718 strlen(#n), &state.interned_##s) \
720 code = svgtiny_LIBDOM_ERROR; \
723 #include "svgtiny_strings.h"
724 #undef SVGTINY_STRING_ACTION2
726 svgtiny_parse_position_attributes(svg
, state
, &x
, &y
, &width
, &height
);
727 diagram
->width
= width
;
728 diagram
->height
= height
;
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;*/
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;
744 code
= svgtiny_preparse_styles(svg
, state
);
745 if (code
== svgtiny_OK
) {
746 code
= svgtiny_parse_svg(svg
, state
);
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
;
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
768 * Parse a single <style> element, appending the result to the CSS
769 * select context within the given parser state.
771 svgtiny_code
svgtiny_parse_style_element(dom_element
*style
,
772 struct svgtiny_parse_state state
)
774 css_stylesheet
*sheet
;
778 code
= svgtiny_create_stylesheet(&sheet
, false);
779 if (code
!= CSS_OK
) {
780 return svgtiny_LIBCSS_ERROR
;
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
,
788 if (exc
!= DOM_NO_ERR
) {
789 css_stylesheet_destroy(sheet
);
790 return svgtiny_LIBDOM_ERROR
;
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
);
800 dom_node_get_text_content(style
, &data
);
802 /* Empty stylesheet? That's fine. */
803 css_stylesheet_destroy(sheet
);
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
;
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
;
823 code
= css_select_ctx_append_sheet(state
.select_ctx
,
827 if (code
!= CSS_OK
) {
828 dom_string_unref(data
);
829 return svgtiny_LIBCSS_ERROR
;
832 dom_string_unref(data
);
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.
845 * The element-parsing inner loop here is essentially the same as
846 * that within svgtiny_parse_svg().
848 svgtiny_code
svgtiny_preparse_styles(dom_element
*svg
,
849 struct svgtiny_parse_state state
)
854 exc
= dom_node_get_first_child(svg
, (dom_node
**) (void *) &child
);
855 if (exc
!= DOM_NO_ERR
) {
856 return svgtiny_LIBDOM_ERROR
;
858 while (child
!= NULL
) {
860 dom_node_type nodetype
;
861 svgtiny_code code
= svgtiny_OK
;
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
;
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
;
876 if (dom_string_caseless_isequal(state
.interned_style
,
878 /* We have a <style> element, parse it */
879 code
= svgtiny_parse_style_element(child
,
884 dom_string_unref(nodename
);
886 if (code
!= svgtiny_OK
) {
887 dom_node_unref(child
);
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
;
903 * Parse a <svg> or <g> element node.
906 svgtiny_code
svgtiny_parse_svg(dom_element
*svg
,
907 struct svgtiny_parse_state state
)
909 float x
, y
, width
, height
;
910 dom_string
*view_box
;
914 svgtiny_setup_state_local(&state
);
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
);
920 exc
= dom_element_get_attribute(svg
, state
.interned_viewBox
,
922 if (exc
!= DOM_NO_ERR
) {
923 svgtiny_cleanup_state_local(&state
);
924 return svgtiny_LIBDOM_ERROR
;
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
;
941 dom_string_unref(view_box
);
944 svgtiny_parse_transform_attributes(svg
, &state
);
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
;
951 while (child
!= NULL
) {
953 dom_node_type nodetype
;
954 svgtiny_code code
= svgtiny_OK
;
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
;
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
;
969 if (dom_string_caseless_isequal(state
.interned_svg
,
971 code
= svgtiny_parse_svg(child
, state
);
972 else if (dom_string_caseless_isequal(state
.interned_g
,
974 code
= svgtiny_parse_svg(child
, state
);
975 else if (dom_string_caseless_isequal(state
.interned_a
,
977 code
= svgtiny_parse_svg(child
, state
);
978 else if (dom_string_caseless_isequal(state
.interned_path
,
980 code
= svgtiny_parse_path(child
, state
);
981 else if (dom_string_caseless_isequal(state
.interned_rect
,
983 code
= svgtiny_parse_rect(child
, state
);
984 else if (dom_string_caseless_isequal(state
.interned_circle
,
986 code
= svgtiny_parse_circle(child
, state
);
987 else if (dom_string_caseless_isequal(state
.interned_ellipse
,
989 code
= svgtiny_parse_ellipse(child
, state
);
990 else if (dom_string_caseless_isequal(state
.interned_line
,
992 code
= svgtiny_parse_line(child
, state
);
993 else if (dom_string_caseless_isequal(state
.interned_polyline
,
995 code
= svgtiny_parse_poly(child
, state
, false);
996 else if (dom_string_caseless_isequal(state
.interned_polygon
,
998 code
= svgtiny_parse_poly(child
, state
, true);
999 else if (dom_string_caseless_isequal(state
.interned_text
,
1001 code
= svgtiny_parse_text(child
, state
);
1002 dom_string_unref(nodename
);
1004 if (code
!= svgtiny_OK
) {
1005 dom_node_unref(child
);
1006 svgtiny_cleanup_state_local(&state
);
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
;
1019 svgtiny_cleanup_state_local(&state
);
1026 * Parse a <path> element node.
1028 * http://www.w3.org/TR/SVG11/paths#PathElement
1031 svgtiny_code
svgtiny_parse_path(dom_element
*path
,
1032 struct svgtiny_parse_state state
)
1035 dom_string
*path_d_str
;
1038 float *p
; /* path elemets */
1039 unsigned int palloc
; /* number of path elements allocated */
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;
1046 svgtiny_setup_state_local(&state
);
1048 svgtiny_parse_paint_attributes(path
, &state
);
1049 svgtiny_parse_transform_attributes(path
, &state
);
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
;
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
;
1067 /* empty path is permitted it just disables the path */
1068 palloc
= dom_string_byte_length(path_d_str
);
1070 dom_string_unref(path_d_str
);
1071 svgtiny_cleanup_state_local(&state
);
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
);
1079 svgtiny_cleanup_state_local(&state
);
1080 return svgtiny_OUT_OF_MEMORY
;
1083 /* ensure path element allocation is sensibly bounded */
1086 } else if (palloc
> 64) {
1087 palloc
= palloc
/ 2;
1090 /* allocate initial space for path elements */
1091 p
= malloc(sizeof p
[0] * palloc
);
1094 svgtiny_cleanup_state_local(&state
);
1095 return svgtiny_OUT_OF_MEMORY
;
1098 /* parse d and build path */
1099 for (i
= 0; s
[i
]; i
++)
1106 float x
, y
, x1
, y1
, x2
, y2
, rx
, ry
, rotation
, large_arc
, sweep
;
1109 /* Ensure there is sufficient space for path elements */
1110 #define ALLOC_PATH_ELEMENTS(NUM_ELEMENTS) \
1112 if ((palloc - i) < NUM_ELEMENTS) { \
1114 palloc = (palloc * 2) + (palloc / 2); \
1115 tp = realloc(p, sizeof p[0] * palloc); \
1119 svgtiny_cleanup_state_local(&state); \
1120 return svgtiny_OUT_OF_MEMORY; \
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
;
1133 plot_command
= svgtiny_PATH_LINE
;
1135 ALLOC_PATH_ELEMENTS(3);
1136 p
[i
++] = plot_command
;
1137 if ('a' <= *command
) {
1141 if (plot_command
== svgtiny_PATH_MOVE
) {
1142 subpath_first_x
= x
;
1143 subpath_first_y
= y
;
1145 p
[i
++] = last_cubic_x
= last_quad_x
= last_x
1147 p
[i
++] = last_cubic_y
= last_quad_y
= last_y
1150 plot_command
= svgtiny_PATH_LINE
;
1151 } while (sscanf(s
, "%f %f %n", &x
, &y
, &n
) == 2);
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);
1158 p
[i
++] = svgtiny_PATH_CLOSE
;
1160 last_cubic_x
= last_quad_x
= last_x
= subpath_first_x
;
1161 last_cubic_y
= last_quad_y
= last_y
= subpath_first_y
;
1163 /* horizontal lineto (H, h) (1 argument) */
1164 } else if (sscanf(s
, " %1[Hh] %f %n", command
, &x
, &n
) == 2) {
1165 /*LOG(("horizontal lineto"));*/
1167 ALLOC_PATH_ELEMENTS(3);
1169 p
[i
++] = svgtiny_PATH_LINE
;
1170 if (*command
== 'h')
1172 p
[i
++] = last_cubic_x
= last_quad_x
= last_x
1174 p
[i
++] = last_cubic_y
= last_quad_y
= last_y
;
1176 } while (sscanf(s
, "%f %n", &x
, &n
) == 1);
1178 /* vertical lineto (V, v) (1 argument) */
1179 } else if (sscanf(s
, " %1[Vv] %f %n", command
, &y
, &n
) == 2) {
1180 /*LOG(("vertical lineto"));*/
1182 ALLOC_PATH_ELEMENTS(3);
1184 p
[i
++] = svgtiny_PATH_LINE
;
1185 if (*command
== 'v')
1187 p
[i
++] = last_cubic_x
= last_quad_x
= last_x
;
1188 p
[i
++] = last_cubic_y
= last_quad_y
= last_y
1191 } while (sscanf(s
, "%f %n", &y
, &n
) == 1);
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"));*/
1198 ALLOC_PATH_ELEMENTS(7);
1200 p
[i
++] = svgtiny_PATH_BEZIER
;
1201 if (*command
== 'c') {
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
;
1216 } while (sscanf(s
, "%f %f %f %f %f %f %n",
1217 &x1
, &y1
, &x2
, &y2
, &x
, &y
, &n
) == 6);
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"));*/
1224 ALLOC_PATH_ELEMENTS(7);
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') {
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
;
1242 } while (sscanf(s
, "%f %f %f %f %n",
1243 &x2
, &y2
, &x
, &y
, &n
) == 4);
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"));*/
1250 ALLOC_PATH_ELEMENTS(7);
1252 p
[i
++] = svgtiny_PATH_BEZIER
;
1255 if (*command
== 'q') {
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
;
1268 } while (sscanf(s
, "%f %f %f %f %n",
1269 &x1
, &y1
, &x
, &y
, &n
) == 4);
1271 /* shorthand/smooth quadratic Bezier curveto (T, t)
1273 } else if (sscanf(s
, " %1[Tt] %f %f %n", command
,
1275 /*LOG(("shorthand/smooth quadratic Bezier curveto"));*/
1277 ALLOC_PATH_ELEMENTS(7);
1279 p
[i
++] = svgtiny_PATH_BEZIER
;
1280 x1
= last_x
+ (last_x
- last_quad_x
);
1281 y1
= last_y
+ (last_y
- last_quad_y
);
1284 if (*command
== 't') {
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
;
1297 } while (sscanf(s
, "%f %f %n",
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
,
1306 double bzpoints
[6*4]; /* allow for up to four bezier segments per arc */
1308 if (*command
== 'a') {
1313 bzsegments
= svgarc_to_bezier(last_x
, last_y
,
1320 if (bzsegments
== -1) {
1322 ALLOC_PATH_ELEMENTS(3);
1323 p
[i
++] = svgtiny_PATH_LINE
;
1326 } else if (bzsegments
> 0) {
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];
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];
1346 } while (sscanf(s
, "%f %f %f %f %f %f %f %n",
1347 &rx
, &ry
, &rotation
, &large_arc
, &sweep
,
1351 /* fprintf(stderr, "parse failed at \"%s\"\n", s); */
1359 /* no real segments in path */
1361 svgtiny_cleanup_state_local(&state
);
1365 /* resize path element array to not be over allocated */
1369 /* try the resize, if it fails just continue to use the old
1372 tp
= realloc(p
, sizeof p
[0] * i
);
1378 err
= svgtiny_add_path(p
, i
, &state
);
1380 svgtiny_cleanup_state_local(&state
);
1387 * Parse a <rect> element node.
1389 * http://www.w3.org/TR/SVG11/shapes#RectElement
1392 svgtiny_code
svgtiny_parse_rect(dom_element
*rect
,
1393 struct svgtiny_parse_state state
)
1396 float x
, y
, width
, height
;
1399 svgtiny_setup_state_local(&state
);
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
);
1406 p
= malloc(13 * sizeof p
[0]);
1408 svgtiny_cleanup_state_local(&state
);
1409 return svgtiny_OUT_OF_MEMORY
;
1412 p
[0] = svgtiny_PATH_MOVE
;
1415 p
[3] = svgtiny_PATH_LINE
;
1418 p
[6] = svgtiny_PATH_LINE
;
1421 p
[9] = svgtiny_PATH_LINE
;
1424 p
[12] = svgtiny_PATH_CLOSE
;
1426 err
= svgtiny_add_path(p
, 13, &state
);
1428 svgtiny_cleanup_state_local(&state
);
1435 * Parse a <circle> element node.
1438 svgtiny_code
svgtiny_parse_circle(dom_element
*circle
,
1439 struct svgtiny_parse_state state
)
1442 float x
= 0, y
= 0, r
= -1;
1447 svgtiny_setup_state_local(&state
);
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
;
1455 x
= svgtiny_parse_length(attr
, state
.viewport_width
, state
);
1457 dom_string_unref(attr
);
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
;
1465 y
= svgtiny_parse_length(attr
, state
.viewport_height
, state
);
1467 dom_string_unref(attr
);
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
;
1475 r
= svgtiny_parse_length(attr
, state
.viewport_width
, state
);
1477 dom_string_unref(attr
);
1479 svgtiny_parse_paint_attributes(circle
, &state
);
1480 svgtiny_parse_transform_attributes(circle
, &state
);
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
;
1489 svgtiny_cleanup_state_local(&state
);
1493 p
= malloc(32 * sizeof p
[0]);
1495 svgtiny_cleanup_state_local(&state
);
1496 return svgtiny_OUT_OF_MEMORY
;
1499 p
[0] = svgtiny_PATH_MOVE
;
1502 p
[3] = svgtiny_PATH_BEZIER
;
1504 p
[5] = y
+ r
* KAPPA
;
1505 p
[6] = x
+ r
* KAPPA
;
1509 p
[10] = svgtiny_PATH_BEZIER
;
1510 p
[11] = x
- r
* KAPPA
;
1513 p
[14] = y
+ r
* KAPPA
;
1516 p
[17] = svgtiny_PATH_BEZIER
;
1518 p
[19] = y
- r
* KAPPA
;
1519 p
[20] = x
- r
* KAPPA
;
1523 p
[24] = svgtiny_PATH_BEZIER
;
1524 p
[25] = x
+ r
* KAPPA
;
1527 p
[28] = y
- r
* KAPPA
;
1530 p
[31] = svgtiny_PATH_CLOSE
;
1532 err
= svgtiny_add_path(p
, 32, &state
);
1534 svgtiny_cleanup_state_local(&state
);
1541 * Parse an <ellipse> element node.
1544 svgtiny_code
svgtiny_parse_ellipse(dom_element
*ellipse
,
1545 struct svgtiny_parse_state state
)
1548 float x
= 0, y
= 0, rx
= -1, ry
= -1;
1553 svgtiny_setup_state_local(&state
);
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
;
1561 x
= svgtiny_parse_length(attr
, state
.viewport_width
, state
);
1563 dom_string_unref(attr
);
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
;
1571 y
= svgtiny_parse_length(attr
, state
.viewport_height
, state
);
1573 dom_string_unref(attr
);
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
;
1581 rx
= svgtiny_parse_length(attr
, state
.viewport_width
, state
);
1583 dom_string_unref(attr
);
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
;
1591 ry
= svgtiny_parse_length(attr
, state
.viewport_width
, state
);
1593 dom_string_unref(attr
);
1595 svgtiny_parse_paint_attributes(ellipse
, &state
);
1596 svgtiny_parse_transform_attributes(ellipse
, &state
);
1598 if (rx
< 0 || ry
< 0) {
1599 state
.diagram
->error_line
= -1; /* ellipse->line; */
1600 state
.diagram
->error_message
= "ellipse: rx or ry missing "
1602 svgtiny_cleanup_state_local(&state
);
1603 return svgtiny_SVG_ERROR
;
1605 if (rx
== 0 || ry
== 0) {
1606 svgtiny_cleanup_state_local(&state
);
1610 p
= malloc(32 * sizeof p
[0]);
1612 svgtiny_cleanup_state_local(&state
);
1613 return svgtiny_OUT_OF_MEMORY
;
1616 p
[0] = svgtiny_PATH_MOVE
;
1619 p
[3] = svgtiny_PATH_BEZIER
;
1621 p
[5] = y
+ ry
* KAPPA
;
1622 p
[6] = x
+ rx
* KAPPA
;
1626 p
[10] = svgtiny_PATH_BEZIER
;
1627 p
[11] = x
- rx
* KAPPA
;
1630 p
[14] = y
+ ry
* KAPPA
;
1633 p
[17] = svgtiny_PATH_BEZIER
;
1635 p
[19] = y
- ry
* KAPPA
;
1636 p
[20] = x
- rx
* KAPPA
;
1640 p
[24] = svgtiny_PATH_BEZIER
;
1641 p
[25] = x
+ rx
* KAPPA
;
1644 p
[28] = y
- ry
* KAPPA
;
1647 p
[31] = svgtiny_PATH_CLOSE
;
1649 err
= svgtiny_add_path(p
, 32, &state
);
1651 svgtiny_cleanup_state_local(&state
);
1658 * Parse a <line> element node.
1661 svgtiny_code
svgtiny_parse_line(dom_element
*line
,
1662 struct svgtiny_parse_state state
)
1665 float x1
= 0, y1
= 0, x2
= 0, y2
= 0;
1670 svgtiny_setup_state_local(&state
);
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
;
1678 x1
= svgtiny_parse_length(attr
, state
.viewport_width
, state
);
1680 dom_string_unref(attr
);
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
;
1688 y1
= svgtiny_parse_length(attr
, state
.viewport_height
, state
);
1690 dom_string_unref(attr
);
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
;
1698 x2
= svgtiny_parse_length(attr
, state
.viewport_width
, state
);
1700 dom_string_unref(attr
);
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
;
1708 y2
= svgtiny_parse_length(attr
, state
.viewport_height
, state
);
1710 dom_string_unref(attr
);
1712 svgtiny_parse_paint_attributes(line
, &state
);
1713 svgtiny_parse_transform_attributes(line
, &state
);
1715 p
= malloc(7 * sizeof p
[0]);
1717 svgtiny_cleanup_state_local(&state
);
1718 return svgtiny_OUT_OF_MEMORY
;
1721 p
[0] = svgtiny_PATH_MOVE
;
1724 p
[3] = svgtiny_PATH_LINE
;
1727 p
[6] = svgtiny_PATH_CLOSE
;
1729 err
= svgtiny_add_path(p
, 7, &state
);
1731 svgtiny_cleanup_state_local(&state
);
1738 * Parse a <polyline> or <polygon> element node.
1740 * http://www.w3.org/TR/SVG11/shapes#PolylineElement
1741 * http://www.w3.org/TR/SVG11/shapes#PolygonElement
1744 svgtiny_code
svgtiny_parse_poly(dom_element
*poly
,
1745 struct svgtiny_parse_state state
, bool polygon
)
1748 dom_string
*points_str
;
1754 svgtiny_setup_state_local(&state
);
1756 svgtiny_parse_paint_attributes(poly
, &state
);
1757 svgtiny_parse_transform_attributes(poly
, &state
);
1759 exc
= dom_element_get_attribute(poly
, state
.interned_points
,
1761 if (exc
!= DOM_NO_ERR
) {
1762 svgtiny_cleanup_state_local(&state
);
1763 return svgtiny_LIBDOM_ERROR
;
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
;
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 */
1779 svgtiny_cleanup_state_local(&state
);
1780 return svgtiny_OUT_OF_MEMORY
;
1782 /* allocate space for path: it will never have more elements than s */
1783 p
= malloc(sizeof p
[0] * strlen(s
));
1786 svgtiny_cleanup_state_local(&state
);
1787 return svgtiny_OUT_OF_MEMORY
;
1790 /* parse s and build path */
1791 for (i
= 0; s
[i
]; i
++)
1799 if (sscanf(s
, "%f %f %n", &x
, &y
, &n
) == 2) {
1801 p
[i
++] = svgtiny_PATH_MOVE
;
1803 p
[i
++] = svgtiny_PATH_LINE
;
1812 p
[i
++] = svgtiny_PATH_CLOSE
;
1816 err
= svgtiny_add_path(p
, i
, &state
);
1818 svgtiny_cleanup_state_local(&state
);
1825 * Parse a <text> or <tspan> element node.
1828 svgtiny_code
svgtiny_parse_text(dom_element
*text
,
1829 struct svgtiny_parse_state state
)
1831 float x
, y
, width
, height
;
1836 svgtiny_setup_state_local(&state
);
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
);
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; */
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
);
1853 while (child
!= NULL
) {
1855 dom_node_type nodetype
;
1856 svgtiny_code code
= svgtiny_OK
;
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
;
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
;
1872 if (dom_string_caseless_isequal(nodename
,
1873 state
.interned_tspan
))
1874 code
= svgtiny_parse_text((dom_element
*)child
,
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
;
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
;
1891 if (content
!= NULL
) {
1892 shape
->text
= strndup(dom_string_data(content
),
1893 dom_string_byte_length(content
));
1894 dom_string_unref(content
);
1896 shape
->text
= strdup("");
1900 state
.diagram
->shape_count
++;
1903 if (code
!= svgtiny_OK
) {
1904 dom_node_unref(child
);
1905 svgtiny_cleanup_state_local(&state
);
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
;
1917 svgtiny_cleanup_state_local(&state
);
1924 * Parse x, y, width, and height attributes, if present.
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
)
1936 *width
= state
.viewport_width
;
1937 *height
= state
.viewport_height
;
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
);
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
);
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
,
1955 dom_string_unref(attr
);
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
,
1962 dom_string_unref(attr
);
1968 * Parse a length as a number of pixels.
1971 static float _svgtiny_parse_length(const char *s
, int viewport_size
,
1972 const struct svgtiny_parse_state state
)
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;
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') {
1991 } else if (unit
[0] == 'p' && unit
[1] == 't') {
1993 } else if (unit
[0] == 'p' && unit
[1] == 'c') {
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') {
2006 float svgtiny_parse_length(dom_string
*s
, int viewport_size
,
2007 const struct svgtiny_parse_state state
)
2009 char *ss
= strndup(dom_string_data(s
), dom_string_byte_length(s
));
2010 float ret
= _svgtiny_parse_length(ss
, viewport_size
, state
);
2016 * Parse paint attributes, if present.
2019 void svgtiny_parse_paint_attributes(dom_element
*node
,
2020 struct svgtiny_parse_state
*state
)
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
);
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
);
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
);
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
));
2050 if ((s
= strstr(style
, "fill:"))) {
2054 value
= strndup(s
, strcspn(s
, "; "));
2055 _svgtiny_parse_color(value
, &state
->fill
, &state
->fill_grad
, state
);
2058 if ((s
= strstr(style
, "stroke:"))) {
2062 value
= strndup(s
, strcspn(s
, "; "));
2063 _svgtiny_parse_color(value
, &state
->stroke
, &state
->stroke_grad
, state
);
2066 if ((s
= strstr(style
, "stroke-width:"))) {
2070 value
= strndup(s
, strcspn(s
, "; "));
2071 state
->stroke_width
= _svgtiny_parse_length(value
,
2072 state
->viewport_width
, *state
);
2076 dom_string_unref(attr
);
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
)
2089 unsigned int r
, g
, b
;
2091 size_t len
= strlen(s
);
2092 char *id
= 0, *rparen
;
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);
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
);
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) {
2110 *c
= svgtiny_RGB(r
, g
, b
);
2113 } else if (len
== 4 && strcmp(s
, "none") == 0) {
2114 *c
= svgtiny_TRANSPARENT
;
2116 } else if (5 < len
&& s
[0] == 'u' && s
[1] == 'r' && s
[2] == 'l' &&
2119 *c
= svgtiny_RGB(0, 0, 0);
2120 } else if (s
[4] == '#') {
2124 rparen
= strchr(id
, ')');
2127 svgtiny_find_gradient(id
, grad
, state
);
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
;
2134 *c
= svgtiny_LINEAR_GRADIENT
;
2138 const struct svgtiny_named_color
*named_color
;
2139 named_color
= svgtiny_color_lookup(s
, strlen(s
));
2141 *c
= named_color
->color
;
2145 void svgtiny_parse_color(dom_string
*s
, svgtiny_colour
*c
,
2146 struct svgtiny_parse_state_gradient
*grad
,
2147 struct svgtiny_parse_state
*state
)
2150 _svgtiny_parse_color(dom_string_data(s
), c
, grad
, state
);
2151 dom_string_unref(s
);
2155 * Parse font attributes, if present.
2158 void svgtiny_parse_font_attributes(dom_element
*node
,
2159 struct svgtiny_parse_state
*state
)
2161 /* TODO: Implement this, it never used to be */
2164 #ifdef WRITTEN_THIS_PROPERLY
2165 const xmlAttr
*attr
;
2169 for (attr
= node
->properties
; attr
; attr
= attr
->next
) {
2170 if (strcmp((const char *) attr
->name
, "font-size") == 0) {
2179 * Parse transform attributes, if present.
2181 * http://www.w3.org/TR/SVG11/coords#TransformAttribute
2184 void svgtiny_parse_transform_attributes(dom_element
*node
,
2185 struct svgtiny_parse_state
*state
)
2191 exc
= dom_element_get_attribute(node
, state
->interned_transform
,
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
);
2200 dom_string_unref(attr
);
2206 * Parse a transform string.
2209 void svgtiny_parse_transform(char *s
, float *ma
, float *mb
,
2210 float *mc
, float *md
, float *me
, float *mf
)
2212 float a
, b
, c
, d
, e
, f
;
2213 float za
, zb
, zc
, zd
, ze
, zf
;
2218 for (i
= 0; s
[i
]; i
++)
2227 if ((sscanf(s
, " matrix (%f %f %f %f %f %f ) %n",
2228 &a
, &b
, &c
, &d
, &e
, &f
, &n
) == 6) && (n
> 0))
2230 else if ((sscanf(s
, " translate (%f %f ) %n",
2231 &e
, &f
, &n
) == 2) && (n
> 0))
2233 else if ((sscanf(s
, " translate (%f ) %n",
2234 &e
, &n
) == 1) && (n
> 0))
2236 else if ((sscanf(s
, " scale (%f %f ) %n",
2237 &a
, &d
, &n
) == 2) && (n
> 0))
2239 else if ((sscanf(s
, " scale (%f ) %n",
2240 &a
, &n
) == 1) && (n
> 0))
2242 else if ((sscanf(s
, " rotate (%f %f %f ) %n",
2243 &angle
, &x
, &y
, &n
) == 3) && (n
> 0)) {
2244 angle
= angle
/ 180 * M_PI
;
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
;
2258 } else if ((sscanf(s
, " skewX (%f ) %n",
2259 &angle
, &n
) == 1) && (n
> 0)) {
2260 angle
= angle
/ 180 * M_PI
;
2262 } else if ((sscanf(s
, " skewY (%f ) %n",
2263 &angle
, &n
) == 1) && (n
> 0)) {
2264 angle
= angle
/ 180 * M_PI
;
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
;
2286 * Add a path to the svgtiny_diagram.
2289 svgtiny_code
svgtiny_add_path(float *p
, unsigned int n
,
2290 struct svgtiny_parse_state
*state
)
2292 struct svgtiny_shape
*shape
;
2294 if (state
->fill
== svgtiny_LINEAR_GRADIENT
)
2295 return svgtiny_add_path_linear_gradient(p
, n
, state
);
2297 svgtiny_transform_path(p
, n
, state
);
2299 shape
= svgtiny_add_shape(state
);
2302 return svgtiny_OUT_OF_MEMORY
;
2305 shape
->path_length
= n
;
2306 state
->diagram
->shape_count
++;
2313 * Add a svgtiny_shape to the svgtiny_diagram.
2316 struct svgtiny_shape
*svgtiny_add_shape(struct svgtiny_parse_state
*state
)
2318 struct svgtiny_shape
*shape
= realloc(state
->diagram
->shape
,
2319 (state
->diagram
->shape_count
+ 1) *
2320 sizeof (state
->diagram
->shape
[0]));
2323 state
->diagram
->shape
= shape
;
2325 shape
+= state
->diagram
->shape_count
;
2327 shape
->path_length
= 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;
2341 * Apply the current transformation matrix to a path.
2344 void svgtiny_transform_path(float *p
, unsigned int n
,
2345 struct svgtiny_parse_state
*state
)
2349 for (j
= 0; j
!= n
; ) {
2350 unsigned int points
= 0;
2352 switch ((int) p
[j
]) {
2353 case svgtiny_PATH_MOVE
:
2354 case svgtiny_PATH_LINE
:
2357 case svgtiny_PATH_CLOSE
:
2360 case svgtiny_PATH_BEZIER
:
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
+
2371 float y
= state
->ctm
.b
* x0
+ state
->ctm
.d
* y0
+
2382 * Free all memory used by a diagram.
2385 void svgtiny_free(struct svgtiny_diagram
*svg
)
2390 for (i
= 0; i
!= svg
->shape_count
; i
++) {
2391 free(svg
->shape
[i
].path
);
2392 free(svg
->shape
[i
].text
);
2400 #ifndef HAVE_STRNDUP
2401 char *svgtiny_strndup(const char *s
, size_t n
)
2406 for (len
= 0; len
!= n
&& s
[len
]; len
++)
2409 s2
= malloc(len
+ 1);