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 css_stylesheet
*svgtiny_parse_style_inline(const uint8_t *data
,
47 static svgtiny_code
svgtiny_preparse_styles(dom_element
*svg
,
48 struct svgtiny_parse_state state
);
49 static svgtiny_code
svgtiny_parse_svg(dom_element
*svg
,
50 struct svgtiny_parse_state state
);
51 static svgtiny_code
svgtiny_parse_path(dom_element
*path
,
52 struct svgtiny_parse_state state
);
53 static svgtiny_code
svgtiny_parse_rect(dom_element
*rect
,
54 struct svgtiny_parse_state state
);
55 static svgtiny_code
svgtiny_parse_circle(dom_element
*circle
,
56 struct svgtiny_parse_state state
);
57 static svgtiny_code
svgtiny_parse_ellipse(dom_element
*ellipse
,
58 struct svgtiny_parse_state state
);
59 static svgtiny_code
svgtiny_parse_line(dom_element
*line
,
60 struct svgtiny_parse_state state
);
61 static svgtiny_code
svgtiny_parse_poly(dom_element
*poly
,
62 struct svgtiny_parse_state state
, bool polygon
);
63 static svgtiny_code
svgtiny_parse_text(dom_element
*text
,
64 struct svgtiny_parse_state state
);
65 static void svgtiny_parse_position_attributes(dom_element
*node
,
66 const struct svgtiny_parse_state state
,
67 float *x
, float *y
, float *width
, float *height
);
68 static void svgtiny_parse_paint_attributes(dom_element
*node
,
69 struct svgtiny_parse_state
*state
);
70 static void svgtiny_parse_font_attributes(dom_element
*node
,
71 struct svgtiny_parse_state
*state
);
72 static void svgtiny_parse_transform_attributes(dom_element
*node
,
73 struct svgtiny_parse_state
*state
);
74 static void svgtiny_parse_styles(dom_element
*node
,
75 struct svgtiny_parse_state
*state
);
76 static svgtiny_code
svgtiny_add_path(float *p
, unsigned int n
,
77 struct svgtiny_parse_state
*state
);
78 static void _svgtiny_parse_color(const char *s
, svgtiny_colour
*c
,
79 struct svgtiny_parse_state_gradient
*grad
,
80 struct svgtiny_parse_state
*state
);
83 * rotate midpoint vector
86 rotate_midpoint_vector(float ax
, float ay
,
89 double *x_out
, double *y_out
)
91 double dx2
; /* midpoint x coordinate */
92 double dy2
; /* midpoint y coordinate */
93 double cosangle
; /* cosine of rotation angle */
94 double sinangle
; /* sine of rotation angle */
96 /* compute the sin and cos of the angle */
97 cosangle
= cos(radangle
);
98 sinangle
= sin(radangle
);
100 /* compute the midpoint between start and end points */
101 dx2
= (ax
- bx
) / 2.0;
102 dy2
= (ay
- by
) / 2.0;
104 /* rotate vector to remove angle */
105 *x_out
= ((cosangle
* dx2
) + (sinangle
* dy2
));
106 *y_out
= ((-sinangle
* dx2
) + (cosangle
* dy2
));
111 * ensure the arc radii are large enough and scale as appropriate
113 * the radii need to be large enough if they are not they must be
114 * adjusted. This allows for elimination of differences between
115 * implementations especialy with rounding.
118 ensure_radii_scale(double x1_sq
, double y1_sq
,
119 float *rx
, float *ry
,
120 double *rx_sq
, double *ry_sq
)
125 /* set radii square values */
126 (*rx_sq
) = (*rx
) * (*rx
);
127 (*ry_sq
) = (*ry
) * (*ry
);
129 radiisum
= (x1_sq
/ (*rx_sq
)) + (y1_sq
/ (*ry_sq
));
130 if (radiisum
> 0.99999) {
131 /* need to scale radii */
132 radiiscale
= sqrt(radiisum
) * 1.00001;
133 *rx
= (float)(radiiscale
* (*rx
));
134 *ry
= (float)(radiiscale
* (*ry
));
135 /* update squares too */
136 (*rx_sq
) = (*rx
) * (*rx
);
137 (*ry_sq
) = (*ry
) * (*ry
);
143 * compute the transformed centre point
146 compute_transformed_centre_point(double sign
, float rx
, float ry
,
147 double rx_sq
, double ry_sq
,
148 double x1
, double y1
,
149 double x1_sq
, double y1_sq
,
150 double *cx1
, double *cy1
)
154 sq
= ((rx_sq
* ry_sq
) - (rx_sq
* y1_sq
) - (ry_sq
* x1_sq
)) /
155 ((rx_sq
* y1_sq
) + (ry_sq
* x1_sq
));
156 sq
= (sq
< 0) ? 0 : sq
;
158 coef
= (sign
* sqrt(sq
));
160 *cx1
= coef
* ((rx
* y1
) / ry
);
161 *cy1
= coef
* -((ry
* x1
) / rx
);
166 * compute untransformed centre point
168 * \param ax The first point x coordinate
169 * \param ay The first point y coordinate
170 * \param bx The second point x coordinate
171 * \param ay The second point y coordinate
174 compute_centre_point(float ax
, float ay
,
176 double cx1
, double cy1
,
178 double *x_out
, double *y_out
)
182 double cosangle
; /* cosine of rotation angle */
183 double sinangle
; /* sine of rotation angle */
185 /* compute the sin and cos of the angle */
186 cosangle
= cos(radangle
);
187 sinangle
= sin(radangle
);
189 sx2
= (ax
+ bx
) / 2.0;
190 sy2
= (ay
+ by
) / 2.0;
192 *x_out
= sx2
+ (cosangle
* cx1
- sinangle
* cy1
);
193 *y_out
= sy2
+ (sinangle
* cx1
+ cosangle
* cy1
);
198 * compute the angle start and extent
201 compute_angle_start_extent(float rx
, float ry
,
202 double x1
, double y1
,
203 double cx1
, double cy1
,
204 double *start
, double *extent
)
215 * Angle betwen two vectors is +/- acos( u.v / len(u) * len(v))
217 * '.' is the dot product.
218 * +/- is calculated from the sign of the cross product (u x v)
221 ux
= (x1
- cx1
) / rx
;
222 uy
= (y1
- cy1
) / ry
;
223 vx
= (-x1
- cx1
) / rx
;
224 vy
= (-y1
- cy1
) / ry
;
226 /* compute the start angle */
227 /* The angle between (ux, uy) and the 0 angle */
229 /* len(u) * len(1,0) == len(u) */
230 n
= sqrt((ux
* ux
) + (uy
* uy
));
231 /* u.v == (ux,uy).(1,0) == (1 * ux) + (0 * uy) == ux */
233 /* u x v == (1 * uy - ux * 0) == uy */
234 sign
= (uy
< 0) ? -1.0 : 1.0;
235 /* (p >= n) so safe */
236 *start
= sign
* acos(p
/ n
);
238 /* compute the extent angle */
239 n
= sqrt(((ux
* ux
) + (uy
* uy
)) * ((vx
* vx
) + (vy
* vy
)));
240 p
= (ux
* vx
) + (uy
* vy
);
241 sign
= ((ux
* vy
) - (uy
* vx
) < 0) ? -1.0f
: 1.0f
;
243 /* arc cos must operate between -1 and 1 */
246 *extent
= sign
* M_PI
;
247 } else if (actmp
> 1.0) {
250 *extent
= sign
* acos(actmp
);
256 * converts a circle centered unit circle arc to a series of bezier curves
258 * Each bezier is stored as six values of three pairs of coordinates
260 * The beziers are stored without their start point as that is assumed
261 * to be the preceding elements end point.
263 * \param start The start angle of the arc (in radians)
264 * \param extent The size of the arc (in radians)
265 * \param bzpt The array to store the bezier values in
266 * \return The number of bezier segments output (max 4)
269 circle_arc_to_bezier(double start
, double extent
, double *bzpt
)
279 bzsegments
= (int) ceil(fabs(extent
) / M_PI_2
);
280 increment
= extent
/ bzsegments
;
281 controllen
= 4.0 / 3.0 * sin(increment
/ 2.0) / (1.0 + cos(increment
/ 2.0));
283 for (segment
= 0; segment
< bzsegments
; segment
++) {
284 /* first control point */
285 angle
= start
+ (segment
* increment
);
288 bzpt
[pos
++] = dx
- controllen
* dy
;
289 bzpt
[pos
++] = dy
+ controllen
* dx
;
290 /* second control point */
294 bzpt
[pos
++] = dx
+ controllen
* dy
;
295 bzpt
[pos
++] = dy
- controllen
* dx
;
306 * transform coordinate list
308 * perform a scale, rotate and translate on list of coordinates
314 * homogeneous transforms
322 * | cos(an) -sin(an) 0 |
323 * R = | sin(an) cos(an) 0 |
326 * {{cos(a), -sin(a) 0}, {sin(a), cos(a),0}, {0,0,1}}
333 * note order is significat here and the combined matrix is
336 * | cos(an) -sin(an) cx |
337 * T.R = | sin(an) cos(an) cy |
340 * | rx * cos(an) ry * -sin(an) cx |
341 * T.R.S = | rx * sin(an) ry * cos(an) cy |
344 * {{Cos[a], -Sin[a], c}, {Sin[a], Cos[a], d}, {0, 0, 1}} . {{r, 0, 0}, {0, s, 0}, {0, 0, 1}}
356 * x2 = cx + (rx * x1 * cos(a)) + (ry * y1 * -1 * sin(a))
357 * y2 = cy + (ry * y1 * cos(a)) + (rx * x1 * sin(a))
360 * \param rx X scaling to apply
361 * \param ry Y scaling to apply
362 * \param radangle rotation to apply (in radians)
363 * \param cx X translation to apply
364 * \param cy Y translation to apply
365 * \param points The size of the bzpoints array
366 * \param bzpoints an array of x,y values to apply the transform to
369 scale_rotate_translate_points(double rx
, double ry
,
371 double cx
, double cy
,
376 double cosangle
; /* cosine of rotation angle */
377 double sinangle
; /* sine of rotation angle */
378 double rxcosangle
, rxsinangle
, rycosangle
, rynsinangle
;
381 /* compute the sin and cos of the angle */
382 cosangle
= cos(radangle
);
383 sinangle
= sin(radangle
);
385 rxcosangle
= rx
* cosangle
;
386 rxsinangle
= rx
* sinangle
;
387 rycosangle
= ry
* cosangle
;
388 rynsinangle
= ry
* -1 * sinangle
;
390 for (pnt
= 0; pnt
< pntsize
; pnt
+=2) {
391 x2
= cx
+ (points
[pnt
] * rxcosangle
) + (points
[pnt
+ 1] * rynsinangle
);
392 y2
= cy
+ (points
[pnt
+ 1] * rycosangle
) + (points
[pnt
] * rxsinangle
);
394 points
[pnt
+ 1] = y2
;
400 * convert an svg path arc to a bezier curve
402 * This function perfoms a transform on the nine arc parameters
403 * (coordinate pairs for start and end together with the radii of the
404 * elipse, the rotation angle and which of the four arcs to draw)
405 * which generates the parameters (coordinate pairs for start,
406 * end and their control points) for a set of up to four bezier curves.
408 * Obviously the start and end coordinates are not altered between
409 * representations so the aim is to calculate the coordinate pairs for
410 * the bezier control points.
412 * \param bzpoints the array to fill with bezier curves
413 * \return the number of bezier segments generated or -1 for a line
416 svgarc_to_bezier(float start_x
,
427 double radangle
; /* normalised elipsis rotation angle in radians */
428 double rx_sq
; /* x radius squared */
429 double ry_sq
; /* y radius squared */
430 double x1
, y1
; /* rotated midpoint vector */
431 double x1_sq
, y1_sq
; /* x1 vector squared */
432 double cx1
,cy1
; /* transformed circle center */
433 double cx
,cy
; /* circle center */
434 double start
, extent
;
437 if ((start_x
== end_x
) && (start_y
== end_y
)) {
439 * if the start and end coordinates are the same the
440 * svg spec says this is equivalent to having no segment
446 if ((rx
== 0) || (ry
== 0)) {
448 * if either radii is zero the specified behaviour is a line
453 /* obtain the absolute values of the radii */
457 /* convert normalised angle to radians */
458 radangle
= degToRad(fmod(angle
, 360.0));
461 /* x1,x2 is the midpoint vector rotated to remove the arc angle */
462 rotate_midpoint_vector(start_x
, start_y
, end_x
, end_y
, radangle
, &x1
, &y1
);
465 /* get squared x1 values */
469 /* ensure radii are correctly scaled */
470 ensure_radii_scale(x1_sq
, y1_sq
, &rx
, &ry
, &rx_sq
, &ry_sq
);
472 /* compute the transformed centre point */
473 compute_transformed_centre_point(largearc
== sweep
?-1:1,
481 /* get the untransformed centre point */
482 compute_centre_point(start_x
, start_y
,
489 /* compute anglestart and extent */
490 compute_angle_start_extent(rx
,ry
,
495 /* extent of 0 is a straight line */
500 /* take account of sweep */
501 if (!sweep
&& extent
> 0) {
503 } else if (sweep
&& extent
< 0) {
507 /* normalise start and extent */
508 extent
= fmod(extent
, TAU
);
509 start
= fmod(start
, TAU
);
511 /* convert the arc to unit circle bezier curves */
512 bzsegments
= circle_arc_to_bezier(start
, extent
, bzpoints
);
514 /* transform the bezier curves */
515 scale_rotate_translate_points(rx
, ry
,
526 * Call this to ref the strings in a gradient state.
528 static void svgtiny_grad_string_ref(struct svgtiny_parse_state_gradient
*grad
)
530 if (grad
->gradient_x1
!= NULL
) {
531 dom_string_ref(grad
->gradient_x1
);
533 if (grad
->gradient_y1
!= NULL
) {
534 dom_string_ref(grad
->gradient_y1
);
536 if (grad
->gradient_x2
!= NULL
) {
537 dom_string_ref(grad
->gradient_x2
);
539 if (grad
->gradient_y2
!= NULL
) {
540 dom_string_ref(grad
->gradient_y2
);
545 * Call this to clean up the strings in a gradient state.
547 static void svgtiny_grad_string_cleanup(
548 struct svgtiny_parse_state_gradient
*grad
)
550 if (grad
->gradient_x1
!= NULL
) {
551 dom_string_unref(grad
->gradient_x1
);
552 grad
->gradient_x1
= NULL
;
554 if (grad
->gradient_y1
!= NULL
) {
555 dom_string_unref(grad
->gradient_y1
);
556 grad
->gradient_y1
= NULL
;
558 if (grad
->gradient_x2
!= NULL
) {
559 dom_string_unref(grad
->gradient_x2
);
560 grad
->gradient_x2
= NULL
;
562 if (grad
->gradient_y2
!= NULL
) {
563 dom_string_unref(grad
->gradient_y2
);
564 grad
->gradient_y2
= NULL
;
569 * Set the local externally-stored parts of a parse state.
570 * Call this in functions that made a new state on the stack.
571 * Doesn't make own copy of global state, such as the interned string list.
573 static void svgtiny_setup_state_local(struct svgtiny_parse_state
*state
)
575 svgtiny_grad_string_ref(&(state
->fill_grad
));
576 svgtiny_grad_string_ref(&(state
->stroke_grad
));
580 * Cleanup the local externally-stored parts of a parse state.
581 * Call this in functions that made a new state on the stack.
582 * Doesn't cleanup global state, such as the interned string list.
584 static void svgtiny_cleanup_state_local(struct svgtiny_parse_state
*state
)
586 svgtiny_grad_string_cleanup(&(state
->fill_grad
));
587 svgtiny_grad_string_cleanup(&(state
->stroke_grad
));
592 * Create a new svgtiny_diagram structure.
595 struct svgtiny_diagram
*svgtiny_create(void)
597 struct svgtiny_diagram
*diagram
;
599 diagram
= calloc(sizeof(*diagram
), 1);
608 static void ignore_msg(uint32_t severity
, void *ctx
, const char *msg
, ...)
616 * Parse a block of memory into a svgtiny_diagram.
619 svgtiny_code
svgtiny_parse(struct svgtiny_diagram
*diagram
,
620 const char *buffer
, size_t size
, const char *url
,
621 int viewport_width
, int viewport_height
)
624 dom_document
*document
;
626 dom_xml_parser
*parser
;
629 dom_string
*svg_name
;
630 lwc_string
*svg_name_lwc
;
631 struct svgtiny_parse_state state
= {
632 /* Initialize the unit context here because it has a
633 * const member and doing it any other way subverts
634 * the type system. The magic numbers below were taken
635 * from the libcss example program without much
636 * thought, because at the moment we don't support any
637 * properties with units. */
639 .font_size_default
= FLTTOFIX(16.0),
640 .font_size_minimum
= FLTTOFIX(6.0),
641 .device_dpi
= FLTTOFIX(96.0),
647 float x
, y
, width
, height
;
656 parser
= dom_xml_parser_create(NULL
, NULL
,
657 ignore_msg
, NULL
, &document
);
660 return svgtiny_LIBDOM_ERROR
;
662 err
= dom_xml_parser_parse_chunk(parser
, (uint8_t *)buffer
, size
);
663 if (err
!= DOM_XML_OK
) {
664 dom_node_unref(document
);
665 dom_xml_parser_destroy(parser
);
666 return svgtiny_LIBDOM_ERROR
;
669 err
= dom_xml_parser_completed(parser
);
670 if (err
!= DOM_XML_OK
) {
671 dom_node_unref(document
);
672 dom_xml_parser_destroy(parser
);
673 return svgtiny_LIBDOM_ERROR
;
676 /* We're done parsing, drop the parser.
677 * We now own the document entirely.
679 dom_xml_parser_destroy(parser
);
681 /* find root <svg> element */
682 exc
= dom_document_get_document_element(document
, &svg
);
683 if (exc
!= DOM_NO_ERR
) {
684 dom_node_unref(document
);
685 return svgtiny_LIBDOM_ERROR
;
688 /* no root svg element */
689 dom_node_unref(document
);
690 return svgtiny_SVG_ERROR
;
693 exc
= dom_node_get_node_name(svg
, &svg_name
);
694 if (exc
!= DOM_NO_ERR
) {
696 dom_node_unref(document
);
697 return svgtiny_LIBDOM_ERROR
;
699 if (lwc_intern_string("svg", 3 /* SLEN("svg") */,
700 &svg_name_lwc
) != lwc_error_ok
) {
701 dom_string_unref(svg_name
);
703 dom_node_unref(document
);
704 return svgtiny_LIBDOM_ERROR
;
706 if (!dom_string_caseless_lwc_isequal(svg_name
, svg_name_lwc
)) {
707 lwc_string_unref(svg_name_lwc
);
708 dom_string_unref(svg_name
);
710 dom_node_unref(document
);
711 return svgtiny_NOT_SVG
;
714 lwc_string_unref(svg_name_lwc
);
715 dom_string_unref(svg_name
);
717 /* initialize the state struct with zeros */
718 memset(&state
, 0, sizeof(state
));
720 /* get graphic dimensions */
721 state
.diagram
= diagram
;
722 state
.document
= document
;
723 state
.viewport_width
= viewport_width
;
724 state
.viewport_height
= viewport_height
;
726 /* Initialize CSS context */
727 css_code
= css_select_ctx_create(&state
.select_ctx
);
728 if (css_code
!= CSS_OK
) {
730 dom_node_unref(document
);
731 return svgtiny_LIBCSS_ERROR
;
734 /* ...and the unit context, whose other fields were
735 * initialized along with the parser state itself */
736 state
.unit_ctx
.viewport_width
= FLTTOFIX(viewport_width
);
737 state
.unit_ctx
.viewport_height
= FLTTOFIX(viewport_height
);
739 #define SVGTINY_STRING_ACTION2(s,n) \
740 if (dom_string_create_interned((const uint8_t *) #n, \
741 strlen(#n), &state.interned_##s) \
743 code = svgtiny_LIBDOM_ERROR; \
746 #include "svgtiny_strings.h"
747 #undef SVGTINY_STRING_ACTION2
749 /* Intern SVG's xmlns separately because it's an lwc_string
750 * and not a dom_string. We initialize its pointer to NULL
751 * because the "cleanup:" test to see if it needs to be free'd
752 * looks for NULL. Returning a LIBDOM_ERROR on failure is not
753 * perfect but it's the closest of the available options. */
754 state
.interned_svg_xmlns
= NULL
;
755 if (lwc_intern_string("http://www.w3.org/2000/svg",
757 &state
.interned_svg_xmlns
) != lwc_error_ok
) {
758 code
= svgtiny_LIBDOM_ERROR
;
762 svgtiny_parse_position_attributes(svg
, state
, &x
, &y
, &width
, &height
);
763 diagram
->width
= width
;
764 diagram
->height
= height
;
766 /* set up parsing state */
767 state
.viewport_width
= width
;
768 state
.viewport_height
= height
;
769 state
.ctm
.a
= 1; /*(float) viewport_width / (float) width;*/
772 state
.ctm
.d
= 1; /*(float) viewport_height / (float) height;*/
773 state
.ctm
.e
= 0; /*x;*/
774 state
.ctm
.f
= 0; /*y;*/
775 state
.fill
= 0x000000;
776 state
.stroke
= svgtiny_TRANSPARENT
;
777 state
.stroke_width
= 1;
780 code
= svgtiny_preparse_styles(svg
, state
);
781 if (code
== svgtiny_OK
) {
782 code
= svgtiny_parse_svg(svg
, state
);
786 dom_node_unref(document
);
787 css_code
= css_select_ctx_destroy(state
.select_ctx
);
788 if (css_code
!= CSS_OK
) {
789 code
= svgtiny_LIBCSS_ERROR
;
793 svgtiny_cleanup_state_local(&state
);
794 #define SVGTINY_STRING_ACTION2(s,n) \
795 if (state.interned_##s != NULL) \
796 dom_string_unref(state.interned_##s);
797 #include "svgtiny_strings.h"
798 #undef SVGTINY_STRING_ACTION2
800 if (state
.interned_svg_xmlns
!= NULL
) {
801 lwc_string_unref(state
.interned_svg_xmlns
);
809 * Parse a single <style> element, appending the result to the CSS
810 * select context within the given parser state.
812 svgtiny_code
svgtiny_parse_style_element(dom_element
*style
,
813 struct svgtiny_parse_state state
)
815 css_stylesheet
*sheet
;
819 code
= svgtiny_create_stylesheet(&sheet
, false);
820 if (code
!= CSS_OK
) {
821 return svgtiny_LIBCSS_ERROR
;
824 /* Parse the style element's "media" attribute if it has
825 one. We don't do anything with it right now. */
826 dom_string
*media_attr
;
827 exc
= dom_element_get_attribute(style
, state
.interned_media
,
829 if (exc
!= DOM_NO_ERR
) {
830 css_stylesheet_destroy(sheet
);
831 return svgtiny_LIBDOM_ERROR
;
835 /* Here's where we'd actually change the media type if
836 we were going to use it */
837 dom_string_unref(media_attr
);
841 dom_node_get_text_content(style
, &data
);
843 /* Empty stylesheet? That's fine. */
844 css_stylesheet_destroy(sheet
);
848 code
= css_stylesheet_append_data(sheet
,
849 (uint8_t *)dom_string_data(data
),
850 dom_string_byte_length(data
));
851 if (code
!= CSS_OK
&& code
!= CSS_NEEDDATA
) {
852 dom_string_unref(data
);
853 css_stylesheet_destroy(sheet
);
854 return svgtiny_LIBCSS_ERROR
;
857 code
= css_stylesheet_data_done(sheet
);
858 if (code
!= CSS_OK
) {
859 dom_string_unref(data
);
860 css_stylesheet_destroy(sheet
);
861 return svgtiny_LIBCSS_ERROR
;
864 code
= css_select_ctx_append_sheet(state
.select_ctx
,
868 if (code
!= CSS_OK
) {
869 dom_string_unref(data
);
870 return svgtiny_LIBCSS_ERROR
;
873 dom_string_unref(data
);
879 * Parse the contents of an inline style and return (a pointer to) the
880 * corresponding stylesheet for use with css_select_style(). Returns
881 * NULL if anything goes wrong.
883 css_stylesheet
*svgtiny_parse_style_inline(const uint8_t *data
,
886 css_stylesheet
*sheet
;
889 code
= svgtiny_create_stylesheet(&sheet
, true);
890 if (code
!= CSS_OK
) {
894 code
= css_stylesheet_append_data(sheet
, data
, len
);
895 if (code
!= CSS_OK
&& code
!= CSS_NEEDDATA
) {
896 css_stylesheet_destroy(sheet
);
900 code
= css_stylesheet_data_done(sheet
);
901 if (code
!= CSS_OK
) {
902 css_stylesheet_destroy(sheet
);
910 * Parse all <style> elements within a root <svg> element. This
911 * should be called before svgtiny_parse_svg() because that function
912 * makes a single pass through the document and we'd like all style
913 * information to be available during that pass. Specifically, we'd
914 * like a <style> sheet at the end of the document to affect the
915 * rendering of elements at its beginning.
917 * The element-parsing inner loop here is essentially the same as
918 * that within svgtiny_parse_svg().
920 svgtiny_code
svgtiny_preparse_styles(dom_element
*svg
,
921 struct svgtiny_parse_state state
)
926 exc
= dom_node_get_first_child(svg
, (dom_node
**) (void *) &child
);
927 if (exc
!= DOM_NO_ERR
) {
928 return svgtiny_LIBDOM_ERROR
;
930 while (child
!= NULL
) {
932 dom_node_type nodetype
;
933 svgtiny_code code
= svgtiny_OK
;
935 exc
= dom_node_get_node_type(child
, &nodetype
);
936 if (exc
!= DOM_NO_ERR
) {
937 dom_node_unref(child
);
938 return svgtiny_LIBDOM_ERROR
;
940 if (nodetype
== DOM_ELEMENT_NODE
) {
941 dom_string
*nodename
;
942 exc
= dom_node_get_node_name(child
, &nodename
);
943 if (exc
!= DOM_NO_ERR
) {
944 dom_node_unref(child
);
945 return svgtiny_LIBDOM_ERROR
;
948 if (dom_string_caseless_isequal(state
.interned_style
,
950 /* We have a <style> element, parse it */
951 code
= svgtiny_parse_style_element(child
,
956 dom_string_unref(nodename
);
958 if (code
!= svgtiny_OK
) {
959 dom_node_unref(child
);
962 exc
= dom_node_get_next_sibling(child
,
963 (dom_node
**) (void *) &next
);
964 dom_node_unref(child
);
965 if (exc
!= DOM_NO_ERR
) {
966 return svgtiny_LIBDOM_ERROR
;
975 * Parse <svg>, <g>, and <a> element nodes.
978 svgtiny_code
svgtiny_parse_svg(dom_element
*svg
,
979 struct svgtiny_parse_state state
)
981 float x
, y
, width
, height
;
982 dom_string
*view_box
;
986 svgtiny_setup_state_local(&state
);
988 svgtiny_parse_position_attributes(svg
, state
, &x
, &y
, &width
, &height
);
989 svgtiny_parse_paint_attributes(svg
, &state
);
990 svgtiny_parse_font_attributes(svg
, &state
);
991 svgtiny_parse_styles(svg
, &state
);
993 exc
= dom_element_get_attribute(svg
, state
.interned_viewBox
,
995 if (exc
!= DOM_NO_ERR
) {
996 svgtiny_cleanup_state_local(&state
);
997 return svgtiny_LIBDOM_ERROR
;
1001 char *s
= strndup(dom_string_data(view_box
),
1002 dom_string_byte_length(view_box
));
1003 float min_x
, min_y
, vwidth
, vheight
;
1004 if (sscanf(s
, "%f,%f,%f,%f",
1005 &min_x
, &min_y
, &vwidth
, &vheight
) == 4 ||
1006 sscanf(s
, "%f %f %f %f",
1007 &min_x
, &min_y
, &vwidth
, &vheight
) == 4) {
1008 state
.ctm
.a
= (float) state
.viewport_width
/ vwidth
;
1009 state
.ctm
.d
= (float) state
.viewport_height
/ vheight
;
1010 state
.ctm
.e
+= -min_x
* state
.ctm
.a
;
1011 state
.ctm
.f
+= -min_y
* state
.ctm
.d
;
1014 dom_string_unref(view_box
);
1017 svgtiny_parse_transform_attributes(svg
, &state
);
1019 exc
= dom_node_get_first_child(svg
, (dom_node
**) (void *) &child
);
1020 if (exc
!= DOM_NO_ERR
) {
1021 svgtiny_cleanup_state_local(&state
);
1022 return svgtiny_LIBDOM_ERROR
;
1024 while (child
!= NULL
) {
1026 dom_node_type nodetype
;
1027 svgtiny_code code
= svgtiny_OK
;
1029 exc
= dom_node_get_node_type(child
, &nodetype
);
1030 if (exc
!= DOM_NO_ERR
) {
1031 dom_node_unref(child
);
1032 return svgtiny_LIBDOM_ERROR
;
1034 if (nodetype
== DOM_ELEMENT_NODE
) {
1035 dom_string
*nodename
;
1036 exc
= dom_node_get_node_name(child
, &nodename
);
1037 if (exc
!= DOM_NO_ERR
) {
1038 dom_node_unref(child
);
1039 svgtiny_cleanup_state_local(&state
);
1040 return svgtiny_LIBDOM_ERROR
;
1042 if (dom_string_caseless_isequal(state
.interned_svg
,
1044 code
= svgtiny_parse_svg(child
, state
);
1045 else if (dom_string_caseless_isequal(state
.interned_g
,
1047 code
= svgtiny_parse_svg(child
, state
);
1048 else if (dom_string_caseless_isequal(state
.interned_a
,
1050 code
= svgtiny_parse_svg(child
, state
);
1051 else if (dom_string_caseless_isequal(state
.interned_path
,
1053 code
= svgtiny_parse_path(child
, state
);
1054 else if (dom_string_caseless_isequal(state
.interned_rect
,
1056 code
= svgtiny_parse_rect(child
, state
);
1057 else if (dom_string_caseless_isequal(state
.interned_circle
,
1059 code
= svgtiny_parse_circle(child
, state
);
1060 else if (dom_string_caseless_isequal(state
.interned_ellipse
,
1062 code
= svgtiny_parse_ellipse(child
, state
);
1063 else if (dom_string_caseless_isequal(state
.interned_line
,
1065 code
= svgtiny_parse_line(child
, state
);
1066 else if (dom_string_caseless_isequal(state
.interned_polyline
,
1068 code
= svgtiny_parse_poly(child
, state
, false);
1069 else if (dom_string_caseless_isequal(state
.interned_polygon
,
1071 code
= svgtiny_parse_poly(child
, state
, true);
1072 else if (dom_string_caseless_isequal(state
.interned_text
,
1074 code
= svgtiny_parse_text(child
, state
);
1075 dom_string_unref(nodename
);
1077 if (code
!= svgtiny_OK
) {
1078 dom_node_unref(child
);
1079 svgtiny_cleanup_state_local(&state
);
1082 exc
= dom_node_get_next_sibling(child
,
1083 (dom_node
**) (void *) &next
);
1084 dom_node_unref(child
);
1085 if (exc
!= DOM_NO_ERR
) {
1086 svgtiny_cleanup_state_local(&state
);
1087 return svgtiny_LIBDOM_ERROR
;
1092 svgtiny_cleanup_state_local(&state
);
1099 * Parse a <path> element node.
1101 * http://www.w3.org/TR/SVG11/paths#PathElement
1104 svgtiny_code
svgtiny_parse_path(dom_element
*path
,
1105 struct svgtiny_parse_state state
)
1108 dom_string
*path_d_str
;
1111 float *p
; /* path elemets */
1112 unsigned int palloc
; /* number of path elements allocated */
1114 float last_x
= 0, last_y
= 0;
1115 float last_cubic_x
= 0, last_cubic_y
= 0;
1116 float last_quad_x
= 0, last_quad_y
= 0;
1117 float subpath_first_x
= 0, subpath_first_y
= 0;
1119 svgtiny_setup_state_local(&state
);
1121 svgtiny_parse_paint_attributes(path
, &state
);
1122 svgtiny_parse_transform_attributes(path
, &state
);
1123 svgtiny_parse_styles(path
, &state
);
1125 /* read d attribute */
1126 exc
= dom_element_get_attribute(path
, state
.interned_d
, &path_d_str
);
1127 if (exc
!= DOM_NO_ERR
) {
1128 state
.diagram
->error_line
= -1; /* path->line; */
1129 state
.diagram
->error_message
= "path: error retrieving d attribute";
1130 svgtiny_cleanup_state_local(&state
);
1131 return svgtiny_SVG_ERROR
;
1134 if (path_d_str
== NULL
) {
1135 state
.diagram
->error_line
= -1; /* path->line; */
1136 state
.diagram
->error_message
= "path: missing d attribute";
1137 svgtiny_cleanup_state_local(&state
);
1138 return svgtiny_SVG_ERROR
;
1141 /* empty path is permitted it just disables the path */
1142 palloc
= dom_string_byte_length(path_d_str
);
1144 dom_string_unref(path_d_str
);
1145 svgtiny_cleanup_state_local(&state
);
1149 /* local copy of the path data allowing in-place modification */
1150 s
= path_d
= strndup(dom_string_data(path_d_str
), palloc
);
1151 dom_string_unref(path_d_str
);
1153 svgtiny_cleanup_state_local(&state
);
1154 return svgtiny_OUT_OF_MEMORY
;
1157 /* ensure path element allocation is sensibly bounded */
1160 } else if (palloc
> 64) {
1161 palloc
= palloc
/ 2;
1164 /* allocate initial space for path elements */
1165 p
= malloc(sizeof p
[0] * palloc
);
1168 svgtiny_cleanup_state_local(&state
);
1169 return svgtiny_OUT_OF_MEMORY
;
1172 /* parse d and build path */
1173 for (i
= 0; s
[i
]; i
++)
1180 float x
, y
, x1
, y1
, x2
, y2
, rx
, ry
, rotation
, large_arc
, sweep
;
1183 /* Ensure there is sufficient space for path elements */
1184 #define ALLOC_PATH_ELEMENTS(NUM_ELEMENTS) \
1186 if ((palloc - i) < NUM_ELEMENTS) { \
1188 palloc = (palloc * 2) + (palloc / 2); \
1189 tp = realloc(p, sizeof p[0] * palloc); \
1193 svgtiny_cleanup_state_local(&state); \
1194 return svgtiny_OUT_OF_MEMORY; \
1201 /* moveto (M, m), lineto (L, l) (2 arguments) */
1202 if (sscanf(s
, " %1[MmLl] %f %f %n", command
, &x
, &y
, &n
) == 3) {
1203 /*LOG(("moveto or lineto"));*/
1204 if (*command
== 'M' || *command
== 'm')
1205 plot_command
= svgtiny_PATH_MOVE
;
1207 plot_command
= svgtiny_PATH_LINE
;
1209 ALLOC_PATH_ELEMENTS(3);
1210 p
[i
++] = plot_command
;
1211 if ('a' <= *command
) {
1215 if (plot_command
== svgtiny_PATH_MOVE
) {
1216 subpath_first_x
= x
;
1217 subpath_first_y
= y
;
1219 p
[i
++] = last_cubic_x
= last_quad_x
= last_x
1221 p
[i
++] = last_cubic_y
= last_quad_y
= last_y
1224 plot_command
= svgtiny_PATH_LINE
;
1225 } while (sscanf(s
, "%f %f %n", &x
, &y
, &n
) == 2);
1227 /* closepath (Z, z) (no arguments) */
1228 } else if (sscanf(s
, " %1[Zz] %n", command
, &n
) == 1) {
1229 /*LOG(("closepath"));*/
1230 ALLOC_PATH_ELEMENTS(1);
1232 p
[i
++] = svgtiny_PATH_CLOSE
;
1234 last_cubic_x
= last_quad_x
= last_x
= subpath_first_x
;
1235 last_cubic_y
= last_quad_y
= last_y
= subpath_first_y
;
1237 /* horizontal lineto (H, h) (1 argument) */
1238 } else if (sscanf(s
, " %1[Hh] %f %n", command
, &x
, &n
) == 2) {
1239 /*LOG(("horizontal lineto"));*/
1241 ALLOC_PATH_ELEMENTS(3);
1243 p
[i
++] = svgtiny_PATH_LINE
;
1244 if (*command
== 'h')
1246 p
[i
++] = last_cubic_x
= last_quad_x
= last_x
1248 p
[i
++] = last_cubic_y
= last_quad_y
= last_y
;
1250 } while (sscanf(s
, "%f %n", &x
, &n
) == 1);
1252 /* vertical lineto (V, v) (1 argument) */
1253 } else if (sscanf(s
, " %1[Vv] %f %n", command
, &y
, &n
) == 2) {
1254 /*LOG(("vertical lineto"));*/
1256 ALLOC_PATH_ELEMENTS(3);
1258 p
[i
++] = svgtiny_PATH_LINE
;
1259 if (*command
== 'v')
1261 p
[i
++] = last_cubic_x
= last_quad_x
= last_x
;
1262 p
[i
++] = last_cubic_y
= last_quad_y
= last_y
1265 } while (sscanf(s
, "%f %n", &y
, &n
) == 1);
1267 /* curveto (C, c) (6 arguments) */
1268 } else if (sscanf(s
, " %1[Cc] %f %f %f %f %f %f %n", command
,
1269 &x1
, &y1
, &x2
, &y2
, &x
, &y
, &n
) == 7) {
1270 /*LOG(("curveto"));*/
1272 ALLOC_PATH_ELEMENTS(7);
1274 p
[i
++] = svgtiny_PATH_BEZIER
;
1275 if (*command
== 'c') {
1285 p
[i
++] = last_cubic_x
= x2
;
1286 p
[i
++] = last_cubic_y
= y2
;
1287 p
[i
++] = last_quad_x
= last_x
= x
;
1288 p
[i
++] = last_quad_y
= last_y
= y
;
1290 } while (sscanf(s
, "%f %f %f %f %f %f %n",
1291 &x1
, &y1
, &x2
, &y2
, &x
, &y
, &n
) == 6);
1293 /* shorthand/smooth curveto (S, s) (4 arguments) */
1294 } else if (sscanf(s
, " %1[Ss] %f %f %f %f %n", command
,
1295 &x2
, &y2
, &x
, &y
, &n
) == 5) {
1296 /*LOG(("shorthand/smooth curveto"));*/
1298 ALLOC_PATH_ELEMENTS(7);
1300 p
[i
++] = svgtiny_PATH_BEZIER
;
1301 x1
= last_x
+ (last_x
- last_cubic_x
);
1302 y1
= last_y
+ (last_y
- last_cubic_y
);
1303 if (*command
== 's') {
1311 p
[i
++] = last_cubic_x
= x2
;
1312 p
[i
++] = last_cubic_y
= y2
;
1313 p
[i
++] = last_quad_x
= last_x
= x
;
1314 p
[i
++] = last_quad_y
= last_y
= y
;
1316 } while (sscanf(s
, "%f %f %f %f %n",
1317 &x2
, &y2
, &x
, &y
, &n
) == 4);
1319 /* quadratic Bezier curveto (Q, q) (4 arguments) */
1320 } else if (sscanf(s
, " %1[Qq] %f %f %f %f %n", command
,
1321 &x1
, &y1
, &x
, &y
, &n
) == 5) {
1322 /*LOG(("quadratic Bezier curveto"));*/
1324 ALLOC_PATH_ELEMENTS(7);
1326 p
[i
++] = svgtiny_PATH_BEZIER
;
1329 if (*command
== 'q') {
1335 p
[i
++] = 1./3 * last_x
+ 2./3 * x1
;
1336 p
[i
++] = 1./3 * last_y
+ 2./3 * y1
;
1337 p
[i
++] = 2./3 * x1
+ 1./3 * x
;
1338 p
[i
++] = 2./3 * y1
+ 1./3 * y
;
1339 p
[i
++] = last_cubic_x
= last_x
= x
;
1340 p
[i
++] = last_cubic_y
= last_y
= y
;
1342 } while (sscanf(s
, "%f %f %f %f %n",
1343 &x1
, &y1
, &x
, &y
, &n
) == 4);
1345 /* shorthand/smooth quadratic Bezier curveto (T, t)
1347 } else if (sscanf(s
, " %1[Tt] %f %f %n", command
,
1349 /*LOG(("shorthand/smooth quadratic Bezier curveto"));*/
1351 ALLOC_PATH_ELEMENTS(7);
1353 p
[i
++] = svgtiny_PATH_BEZIER
;
1354 x1
= last_x
+ (last_x
- last_quad_x
);
1355 y1
= last_y
+ (last_y
- last_quad_y
);
1358 if (*command
== 't') {
1364 p
[i
++] = 1./3 * last_x
+ 2./3 * x1
;
1365 p
[i
++] = 1./3 * last_y
+ 2./3 * y1
;
1366 p
[i
++] = 2./3 * x1
+ 1./3 * x
;
1367 p
[i
++] = 2./3 * y1
+ 1./3 * y
;
1368 p
[i
++] = last_cubic_x
= last_x
= x
;
1369 p
[i
++] = last_cubic_y
= last_y
= y
;
1371 } while (sscanf(s
, "%f %f %n",
1374 /* elliptical arc (A, a) (7 arguments) */
1375 } else if (sscanf(s
, " %1[Aa] %f %f %f %f %f %f %f %n", command
,
1376 &rx
, &ry
, &rotation
, &large_arc
, &sweep
,
1380 double bzpoints
[6*4]; /* allow for up to four bezier segments per arc */
1382 if (*command
== 'a') {
1387 bzsegments
= svgarc_to_bezier(last_x
, last_y
,
1394 if (bzsegments
== -1) {
1396 ALLOC_PATH_ELEMENTS(3);
1397 p
[i
++] = svgtiny_PATH_LINE
;
1400 } else if (bzsegments
> 0) {
1402 ALLOC_PATH_ELEMENTS((unsigned int)bzsegments
* 7);
1403 for (bzpnt
= 0;bzpnt
< (bzsegments
* 6); bzpnt
+=6) {
1404 p
[i
++] = svgtiny_PATH_BEZIER
;
1405 p
[i
++] = bzpoints
[bzpnt
];
1406 p
[i
++] = bzpoints
[bzpnt
+1];
1407 p
[i
++] = bzpoints
[bzpnt
+2];
1408 p
[i
++] = bzpoints
[bzpnt
+3];
1409 p
[i
++] = bzpoints
[bzpnt
+4];
1410 p
[i
++] = bzpoints
[bzpnt
+5];
1413 if (bzsegments
!= 0) {
1414 last_cubic_x
= last_quad_x
= last_x
= p
[i
-2];
1415 last_cubic_y
= last_quad_y
= last_y
= p
[i
-1];
1420 } while (sscanf(s
, "%f %f %f %f %f %f %f %n",
1421 &rx
, &ry
, &rotation
, &large_arc
, &sweep
,
1425 /* fprintf(stderr, "parse failed at \"%s\"\n", s); */
1433 /* no real segments in path */
1435 svgtiny_cleanup_state_local(&state
);
1439 /* resize path element array to not be over allocated */
1443 /* try the resize, if it fails just continue to use the old
1446 tp
= realloc(p
, sizeof p
[0] * i
);
1452 err
= svgtiny_add_path(p
, i
, &state
);
1454 svgtiny_cleanup_state_local(&state
);
1461 * Parse a <rect> element node.
1463 * http://www.w3.org/TR/SVG11/shapes#RectElement
1466 svgtiny_code
svgtiny_parse_rect(dom_element
*rect
,
1467 struct svgtiny_parse_state state
)
1470 float x
, y
, width
, height
;
1473 svgtiny_setup_state_local(&state
);
1475 svgtiny_parse_position_attributes(rect
, state
,
1476 &x
, &y
, &width
, &height
);
1477 svgtiny_parse_paint_attributes(rect
, &state
);
1478 svgtiny_parse_transform_attributes(rect
, &state
);
1479 svgtiny_parse_styles(rect
, &state
);
1481 p
= malloc(13 * sizeof p
[0]);
1483 svgtiny_cleanup_state_local(&state
);
1484 return svgtiny_OUT_OF_MEMORY
;
1487 p
[0] = svgtiny_PATH_MOVE
;
1490 p
[3] = svgtiny_PATH_LINE
;
1493 p
[6] = svgtiny_PATH_LINE
;
1496 p
[9] = svgtiny_PATH_LINE
;
1499 p
[12] = svgtiny_PATH_CLOSE
;
1501 err
= svgtiny_add_path(p
, 13, &state
);
1503 svgtiny_cleanup_state_local(&state
);
1510 * Parse a <circle> element node.
1513 svgtiny_code
svgtiny_parse_circle(dom_element
*circle
,
1514 struct svgtiny_parse_state state
)
1517 float x
= 0, y
= 0, r
= -1;
1522 svgtiny_setup_state_local(&state
);
1524 exc
= dom_element_get_attribute(circle
, state
.interned_cx
, &attr
);
1525 if (exc
!= DOM_NO_ERR
) {
1526 svgtiny_cleanup_state_local(&state
);
1527 return svgtiny_LIBDOM_ERROR
;
1530 x
= svgtiny_parse_length(attr
, state
.viewport_width
, state
);
1532 dom_string_unref(attr
);
1534 exc
= dom_element_get_attribute(circle
, state
.interned_cy
, &attr
);
1535 if (exc
!= DOM_NO_ERR
) {
1536 svgtiny_cleanup_state_local(&state
);
1537 return svgtiny_LIBDOM_ERROR
;
1540 y
= svgtiny_parse_length(attr
, state
.viewport_height
, state
);
1542 dom_string_unref(attr
);
1544 exc
= dom_element_get_attribute(circle
, state
.interned_r
, &attr
);
1545 if (exc
!= DOM_NO_ERR
) {
1546 svgtiny_cleanup_state_local(&state
);
1547 return svgtiny_LIBDOM_ERROR
;
1550 r
= svgtiny_parse_length(attr
, state
.viewport_width
, state
);
1552 dom_string_unref(attr
);
1554 svgtiny_parse_paint_attributes(circle
, &state
);
1555 svgtiny_parse_transform_attributes(circle
, &state
);
1556 svgtiny_parse_styles(circle
, &state
);
1559 state
.diagram
->error_line
= -1; /* circle->line; */
1560 state
.diagram
->error_message
= "circle: r missing or negative";
1561 svgtiny_cleanup_state_local(&state
);
1562 return svgtiny_SVG_ERROR
;
1565 svgtiny_cleanup_state_local(&state
);
1569 p
= malloc(32 * sizeof p
[0]);
1571 svgtiny_cleanup_state_local(&state
);
1572 return svgtiny_OUT_OF_MEMORY
;
1575 p
[0] = svgtiny_PATH_MOVE
;
1578 p
[3] = svgtiny_PATH_BEZIER
;
1580 p
[5] = y
+ r
* KAPPA
;
1581 p
[6] = x
+ r
* KAPPA
;
1585 p
[10] = svgtiny_PATH_BEZIER
;
1586 p
[11] = x
- r
* KAPPA
;
1589 p
[14] = y
+ r
* KAPPA
;
1592 p
[17] = svgtiny_PATH_BEZIER
;
1594 p
[19] = y
- r
* KAPPA
;
1595 p
[20] = x
- r
* KAPPA
;
1599 p
[24] = svgtiny_PATH_BEZIER
;
1600 p
[25] = x
+ r
* KAPPA
;
1603 p
[28] = y
- r
* KAPPA
;
1606 p
[31] = svgtiny_PATH_CLOSE
;
1608 err
= svgtiny_add_path(p
, 32, &state
);
1610 svgtiny_cleanup_state_local(&state
);
1617 * Parse an <ellipse> element node.
1620 svgtiny_code
svgtiny_parse_ellipse(dom_element
*ellipse
,
1621 struct svgtiny_parse_state state
)
1624 float x
= 0, y
= 0, rx
= -1, ry
= -1;
1629 svgtiny_setup_state_local(&state
);
1631 exc
= dom_element_get_attribute(ellipse
, state
.interned_cx
, &attr
);
1632 if (exc
!= DOM_NO_ERR
) {
1633 svgtiny_cleanup_state_local(&state
);
1634 return svgtiny_LIBDOM_ERROR
;
1637 x
= svgtiny_parse_length(attr
, state
.viewport_width
, state
);
1639 dom_string_unref(attr
);
1641 exc
= dom_element_get_attribute(ellipse
, state
.interned_cy
, &attr
);
1642 if (exc
!= DOM_NO_ERR
) {
1643 svgtiny_cleanup_state_local(&state
);
1644 return svgtiny_LIBDOM_ERROR
;
1647 y
= svgtiny_parse_length(attr
, state
.viewport_height
, state
);
1649 dom_string_unref(attr
);
1651 exc
= dom_element_get_attribute(ellipse
, state
.interned_rx
, &attr
);
1652 if (exc
!= DOM_NO_ERR
) {
1653 svgtiny_cleanup_state_local(&state
);
1654 return svgtiny_LIBDOM_ERROR
;
1657 rx
= svgtiny_parse_length(attr
, state
.viewport_width
, state
);
1659 dom_string_unref(attr
);
1661 exc
= dom_element_get_attribute(ellipse
, state
.interned_ry
, &attr
);
1662 if (exc
!= DOM_NO_ERR
) {
1663 svgtiny_cleanup_state_local(&state
);
1664 return svgtiny_LIBDOM_ERROR
;
1667 ry
= svgtiny_parse_length(attr
, state
.viewport_width
, state
);
1669 dom_string_unref(attr
);
1671 svgtiny_parse_paint_attributes(ellipse
, &state
);
1672 svgtiny_parse_transform_attributes(ellipse
, &state
);
1673 svgtiny_parse_styles(ellipse
, &state
);
1675 if (rx
< 0 || ry
< 0) {
1676 state
.diagram
->error_line
= -1; /* ellipse->line; */
1677 state
.diagram
->error_message
= "ellipse: rx or ry missing "
1679 svgtiny_cleanup_state_local(&state
);
1680 return svgtiny_SVG_ERROR
;
1682 if (rx
== 0 || ry
== 0) {
1683 svgtiny_cleanup_state_local(&state
);
1687 p
= malloc(32 * sizeof p
[0]);
1689 svgtiny_cleanup_state_local(&state
);
1690 return svgtiny_OUT_OF_MEMORY
;
1693 p
[0] = svgtiny_PATH_MOVE
;
1696 p
[3] = svgtiny_PATH_BEZIER
;
1698 p
[5] = y
+ ry
* KAPPA
;
1699 p
[6] = x
+ rx
* KAPPA
;
1703 p
[10] = svgtiny_PATH_BEZIER
;
1704 p
[11] = x
- rx
* KAPPA
;
1707 p
[14] = y
+ ry
* KAPPA
;
1710 p
[17] = svgtiny_PATH_BEZIER
;
1712 p
[19] = y
- ry
* KAPPA
;
1713 p
[20] = x
- rx
* KAPPA
;
1717 p
[24] = svgtiny_PATH_BEZIER
;
1718 p
[25] = x
+ rx
* KAPPA
;
1721 p
[28] = y
- ry
* KAPPA
;
1724 p
[31] = svgtiny_PATH_CLOSE
;
1726 err
= svgtiny_add_path(p
, 32, &state
);
1728 svgtiny_cleanup_state_local(&state
);
1735 * Parse a <line> element node.
1738 svgtiny_code
svgtiny_parse_line(dom_element
*line
,
1739 struct svgtiny_parse_state state
)
1742 float x1
= 0, y1
= 0, x2
= 0, y2
= 0;
1747 svgtiny_setup_state_local(&state
);
1749 exc
= dom_element_get_attribute(line
, state
.interned_x1
, &attr
);
1750 if (exc
!= DOM_NO_ERR
) {
1751 svgtiny_cleanup_state_local(&state
);
1752 return svgtiny_LIBDOM_ERROR
;
1755 x1
= svgtiny_parse_length(attr
, state
.viewport_width
, state
);
1757 dom_string_unref(attr
);
1759 exc
= dom_element_get_attribute(line
, state
.interned_y1
, &attr
);
1760 if (exc
!= DOM_NO_ERR
) {
1761 svgtiny_cleanup_state_local(&state
);
1762 return svgtiny_LIBDOM_ERROR
;
1765 y1
= svgtiny_parse_length(attr
, state
.viewport_height
, state
);
1767 dom_string_unref(attr
);
1769 exc
= dom_element_get_attribute(line
, state
.interned_x2
, &attr
);
1770 if (exc
!= DOM_NO_ERR
) {
1771 svgtiny_cleanup_state_local(&state
);
1772 return svgtiny_LIBDOM_ERROR
;
1775 x2
= svgtiny_parse_length(attr
, state
.viewport_width
, state
);
1777 dom_string_unref(attr
);
1779 exc
= dom_element_get_attribute(line
, state
.interned_y2
, &attr
);
1780 if (exc
!= DOM_NO_ERR
) {
1781 svgtiny_cleanup_state_local(&state
);
1782 return svgtiny_LIBDOM_ERROR
;
1785 y2
= svgtiny_parse_length(attr
, state
.viewport_height
, state
);
1787 dom_string_unref(attr
);
1789 svgtiny_parse_paint_attributes(line
, &state
);
1790 svgtiny_parse_transform_attributes(line
, &state
);
1791 svgtiny_parse_styles(line
, &state
);
1793 p
= malloc(7 * sizeof p
[0]);
1795 svgtiny_cleanup_state_local(&state
);
1796 return svgtiny_OUT_OF_MEMORY
;
1799 p
[0] = svgtiny_PATH_MOVE
;
1802 p
[3] = svgtiny_PATH_LINE
;
1805 p
[6] = svgtiny_PATH_CLOSE
;
1807 err
= svgtiny_add_path(p
, 7, &state
);
1809 svgtiny_cleanup_state_local(&state
);
1816 * Parse a <polyline> or <polygon> element node.
1818 * http://www.w3.org/TR/SVG11/shapes#PolylineElement
1819 * http://www.w3.org/TR/SVG11/shapes#PolygonElement
1822 svgtiny_code
svgtiny_parse_poly(dom_element
*poly
,
1823 struct svgtiny_parse_state state
, bool polygon
)
1826 dom_string
*points_str
;
1832 svgtiny_setup_state_local(&state
);
1834 svgtiny_parse_paint_attributes(poly
, &state
);
1835 svgtiny_parse_transform_attributes(poly
, &state
);
1836 svgtiny_parse_styles(poly
, &state
);
1838 exc
= dom_element_get_attribute(poly
, state
.interned_points
,
1840 if (exc
!= DOM_NO_ERR
) {
1841 svgtiny_cleanup_state_local(&state
);
1842 return svgtiny_LIBDOM_ERROR
;
1845 if (points_str
== NULL
) {
1846 state
.diagram
->error_line
= -1; /* poly->line; */
1847 state
.diagram
->error_message
=
1848 "polyline/polygon: missing points attribute";
1849 svgtiny_cleanup_state_local(&state
);
1850 return svgtiny_SVG_ERROR
;
1853 s
= points
= strndup(dom_string_data(points_str
),
1854 dom_string_byte_length(points_str
));
1855 dom_string_unref(points_str
);
1856 /* read points attribute */
1858 svgtiny_cleanup_state_local(&state
);
1859 return svgtiny_OUT_OF_MEMORY
;
1861 /* allocate space for path: it will never have more elements than s */
1862 p
= malloc(sizeof p
[0] * strlen(s
));
1865 svgtiny_cleanup_state_local(&state
);
1866 return svgtiny_OUT_OF_MEMORY
;
1869 /* parse s and build path */
1870 for (i
= 0; s
[i
]; i
++)
1878 if (sscanf(s
, "%f %f %n", &x
, &y
, &n
) == 2) {
1880 p
[i
++] = svgtiny_PATH_MOVE
;
1882 p
[i
++] = svgtiny_PATH_LINE
;
1891 p
[i
++] = svgtiny_PATH_CLOSE
;
1895 err
= svgtiny_add_path(p
, i
, &state
);
1897 svgtiny_cleanup_state_local(&state
);
1904 * Parse a <text> or <tspan> element node.
1907 svgtiny_code
svgtiny_parse_text(dom_element
*text
,
1908 struct svgtiny_parse_state state
)
1910 float x
, y
, width
, height
;
1915 svgtiny_setup_state_local(&state
);
1917 svgtiny_parse_position_attributes(text
, state
,
1918 &x
, &y
, &width
, &height
);
1919 svgtiny_parse_font_attributes(text
, &state
);
1920 svgtiny_parse_transform_attributes(text
, &state
);
1922 px
= state
.ctm
.a
* x
+ state
.ctm
.c
* y
+ state
.ctm
.e
;
1923 py
= state
.ctm
.b
* x
+ state
.ctm
.d
* y
+ state
.ctm
.f
;
1924 /* state.ctm.e = px - state.origin_x; */
1925 /* state.ctm.f = py - state.origin_y; */
1927 exc
= dom_node_get_first_child(text
, &child
);
1928 if (exc
!= DOM_NO_ERR
) {
1929 return svgtiny_LIBDOM_ERROR
;
1930 svgtiny_cleanup_state_local(&state
);
1932 while (child
!= NULL
) {
1934 dom_node_type nodetype
;
1935 svgtiny_code code
= svgtiny_OK
;
1937 exc
= dom_node_get_node_type(child
, &nodetype
);
1938 if (exc
!= DOM_NO_ERR
) {
1939 dom_node_unref(child
);
1940 svgtiny_cleanup_state_local(&state
);
1941 return svgtiny_LIBDOM_ERROR
;
1943 if (nodetype
== DOM_ELEMENT_NODE
) {
1944 dom_string
*nodename
;
1945 exc
= dom_node_get_node_name(child
, &nodename
);
1946 if (exc
!= DOM_NO_ERR
) {
1947 dom_node_unref(child
);
1948 svgtiny_cleanup_state_local(&state
);
1949 return svgtiny_LIBDOM_ERROR
;
1951 if (dom_string_caseless_isequal(nodename
,
1952 state
.interned_tspan
))
1953 code
= svgtiny_parse_text((dom_element
*)child
,
1955 dom_string_unref(nodename
);
1956 } else if (nodetype
== DOM_TEXT_NODE
) {
1957 struct svgtiny_shape
*shape
= svgtiny_add_shape(&state
);
1958 dom_string
*content
;
1959 if (shape
== NULL
) {
1960 dom_node_unref(child
);
1961 svgtiny_cleanup_state_local(&state
);
1962 return svgtiny_OUT_OF_MEMORY
;
1964 exc
= dom_text_get_whole_text(child
, &content
);
1965 if (exc
!= DOM_NO_ERR
) {
1966 dom_node_unref(child
);
1967 svgtiny_cleanup_state_local(&state
);
1968 return svgtiny_LIBDOM_ERROR
;
1970 if (content
!= NULL
) {
1971 shape
->text
= strndup(dom_string_data(content
),
1972 dom_string_byte_length(content
));
1973 dom_string_unref(content
);
1975 shape
->text
= strdup("");
1979 state
.diagram
->shape_count
++;
1982 if (code
!= svgtiny_OK
) {
1983 dom_node_unref(child
);
1984 svgtiny_cleanup_state_local(&state
);
1987 exc
= dom_node_get_next_sibling(child
, &next
);
1988 dom_node_unref(child
);
1989 if (exc
!= DOM_NO_ERR
) {
1990 svgtiny_cleanup_state_local(&state
);
1991 return svgtiny_LIBDOM_ERROR
;
1996 svgtiny_cleanup_state_local(&state
);
2003 * Parse x, y, width, and height attributes, if present.
2006 void svgtiny_parse_position_attributes(dom_element
*node
,
2007 const struct svgtiny_parse_state state
,
2008 float *x
, float *y
, float *width
, float *height
)
2015 *width
= state
.viewport_width
;
2016 *height
= state
.viewport_height
;
2018 exc
= dom_element_get_attribute(node
, state
.interned_x
, &attr
);
2019 if (exc
== DOM_NO_ERR
&& attr
!= NULL
) {
2020 *x
= svgtiny_parse_length(attr
, state
.viewport_width
, state
);
2021 dom_string_unref(attr
);
2024 exc
= dom_element_get_attribute(node
, state
.interned_y
, &attr
);
2025 if (exc
== DOM_NO_ERR
&& attr
!= NULL
) {
2026 *y
= svgtiny_parse_length(attr
, state
.viewport_height
, state
);
2027 dom_string_unref(attr
);
2030 exc
= dom_element_get_attribute(node
, state
.interned_width
, &attr
);
2031 if (exc
== DOM_NO_ERR
&& attr
!= NULL
) {
2032 *width
= svgtiny_parse_length(attr
, state
.viewport_width
,
2034 dom_string_unref(attr
);
2037 exc
= dom_element_get_attribute(node
, state
.interned_height
, &attr
);
2038 if (exc
== DOM_NO_ERR
&& attr
!= NULL
) {
2039 *height
= svgtiny_parse_length(attr
, state
.viewport_height
,
2041 dom_string_unref(attr
);
2047 * Parse a length as a number of pixels.
2050 static float _svgtiny_parse_length(const char *s
, int viewport_size
,
2051 const struct svgtiny_parse_state state
)
2053 int num_length
= strspn(s
, "0123456789+-.");
2054 const char *unit
= s
+ num_length
;
2055 float n
= atof((const char *) s
);
2056 float font_size
= 20;
2062 } else if (unit
[0] == '%') {
2063 return n
/ 100.0 * viewport_size
;
2064 } else if (unit
[0] == 'e' && unit
[1] == 'm') {
2065 return n
* font_size
;
2066 } else if (unit
[0] == 'e' && unit
[1] == 'x') {
2067 return n
/ 2.0 * font_size
;
2068 } else if (unit
[0] == 'p' && unit
[1] == 'x') {
2070 } else if (unit
[0] == 'p' && unit
[1] == 't') {
2072 } else if (unit
[0] == 'p' && unit
[1] == 'c') {
2074 } else if (unit
[0] == 'm' && unit
[1] == 'm') {
2075 return n
* 3.543307;
2076 } else if (unit
[0] == 'c' && unit
[1] == 'm') {
2077 return n
* 35.43307;
2078 } else if (unit
[0] == 'i' && unit
[1] == 'n') {
2085 float svgtiny_parse_length(dom_string
*s
, int viewport_size
,
2086 const struct svgtiny_parse_state state
)
2088 char *ss
= strndup(dom_string_data(s
), dom_string_byte_length(s
));
2089 float ret
= _svgtiny_parse_length(ss
, viewport_size
, state
);
2095 * Parse paint attributes, if present.
2098 void svgtiny_parse_paint_attributes(dom_element
*node
,
2099 struct svgtiny_parse_state
*state
)
2104 exc
= dom_element_get_attribute(node
, state
->interned_fill
, &attr
);
2105 if (exc
== DOM_NO_ERR
&& attr
!= NULL
) {
2106 svgtiny_parse_color(attr
, &state
->fill
, &state
->fill_grad
, state
);
2107 dom_string_unref(attr
);
2110 exc
= dom_element_get_attribute(node
, state
->interned_stroke
, &attr
);
2111 if (exc
== DOM_NO_ERR
&& attr
!= NULL
) {
2112 svgtiny_parse_color(attr
, &state
->stroke
, &state
->stroke_grad
, state
);
2113 dom_string_unref(attr
);
2116 exc
= dom_element_get_attribute(node
, state
->interned_stroke_width
, &attr
);
2117 if (exc
== DOM_NO_ERR
&& attr
!= NULL
) {
2118 state
->stroke_width
= svgtiny_parse_length(attr
,
2119 state
->viewport_width
, *state
);
2120 dom_string_unref(attr
);
2123 exc
= dom_element_get_attribute(node
, state
->interned_style
, &attr
);
2124 if (exc
== DOM_NO_ERR
&& attr
!= NULL
) {
2125 /* Parse a few properties "by hand" until they can
2126 be supported in libcss. */
2127 char *style
= strndup(dom_string_data(attr
),
2128 dom_string_byte_length(attr
));
2131 if ((s
= strstr(style
, "fill:"))) {
2135 value
= strndup(s
, strcspn(s
, "; "));
2136 _svgtiny_parse_color(value
, &state
->fill
, &state
->fill_grad
, state
);
2139 if ((s
= strstr(style
, "stroke:"))) {
2143 value
= strndup(s
, strcspn(s
, "; "));
2144 _svgtiny_parse_color(value
, &state
->stroke
, &state
->stroke_grad
, state
);
2147 if ((s
= strstr(style
, "stroke-width:"))) {
2151 value
= strndup(s
, strcspn(s
, "; "));
2152 state
->stroke_width
= _svgtiny_parse_length(value
,
2153 state
->viewport_width
, *state
);
2157 dom_string_unref(attr
);
2166 static void _svgtiny_parse_color(const char *s
, svgtiny_colour
*c
,
2167 struct svgtiny_parse_state_gradient
*grad
,
2168 struct svgtiny_parse_state
*state
)
2170 unsigned int r
, g
, b
;
2172 size_t len
= strlen(s
);
2173 char *id
= 0, *rparen
;
2175 if (len
== 4 && s
[0] == '#') {
2176 if (sscanf(s
+ 1, "%1x%1x%1x", &r
, &g
, &b
) == 3)
2177 *c
= svgtiny_RGB(r
| r
<< 4, g
| g
<< 4, b
| b
<< 4);
2179 } else if (len
== 7 && s
[0] == '#') {
2180 if (sscanf(s
+ 1, "%2x%2x%2x", &r
, &g
, &b
) == 3)
2181 *c
= svgtiny_RGB(r
, g
, b
);
2183 } else if (10 <= len
&& s
[0] == 'r' && s
[1] == 'g' && s
[2] == 'b' &&
2184 s
[3] == '(' && s
[len
- 1] == ')') {
2185 if (sscanf(s
+ 4, "%u,%u,%u", &r
, &g
, &b
) == 3)
2186 *c
= svgtiny_RGB(r
, g
, b
);
2187 else if (sscanf(s
+ 4, "%f%%,%f%%,%f%%", &rf
, &gf
, &bf
) == 3) {
2191 *c
= svgtiny_RGB(r
, g
, b
);
2194 } else if (len
== 4 && strcmp(s
, "none") == 0) {
2195 *c
= svgtiny_TRANSPARENT
;
2197 } else if (5 < len
&& s
[0] == 'u' && s
[1] == 'r' && s
[2] == 'l' &&
2200 *c
= svgtiny_RGB(0, 0, 0);
2201 } else if (s
[4] == '#') {
2205 rparen
= strchr(id
, ')');
2208 svgtiny_find_gradient(id
, grad
, state
);
2210 if (grad
->linear_gradient_stop_count
== 0)
2211 *c
= svgtiny_TRANSPARENT
;
2212 else if (grad
->linear_gradient_stop_count
== 1)
2213 *c
= grad
->gradient_stop
[0].color
;
2215 *c
= svgtiny_LINEAR_GRADIENT
;
2219 const struct svgtiny_named_color
*named_color
;
2220 named_color
= svgtiny_color_lookup(s
, strlen(s
));
2222 *c
= named_color
->color
;
2226 void svgtiny_parse_color(dom_string
*s
, svgtiny_colour
*c
,
2227 struct svgtiny_parse_state_gradient
*grad
,
2228 struct svgtiny_parse_state
*state
)
2231 _svgtiny_parse_color(dom_string_data(s
), c
, grad
, state
);
2232 dom_string_unref(s
);
2236 * Parse font attributes, if present.
2239 void svgtiny_parse_font_attributes(dom_element
*node
,
2240 struct svgtiny_parse_state
*state
)
2242 /* TODO: Implement this, it never used to be */
2245 #ifdef WRITTEN_THIS_PROPERLY
2246 const xmlAttr
*attr
;
2250 for (attr
= node
->properties
; attr
; attr
= attr
->next
) {
2251 if (strcmp((const char *) attr
->name
, "font-size") == 0) {
2260 * Parse transform attributes, if present.
2262 * http://www.w3.org/TR/SVG11/coords#TransformAttribute
2265 void svgtiny_parse_transform_attributes(dom_element
*node
,
2266 struct svgtiny_parse_state
*state
)
2272 exc
= dom_element_get_attribute(node
, state
->interned_transform
,
2274 if (exc
== DOM_NO_ERR
&& attr
!= NULL
) {
2275 transform
= strndup(dom_string_data(attr
),
2276 dom_string_byte_length(attr
));
2277 svgtiny_parse_transform(transform
, &state
->ctm
.a
, &state
->ctm
.b
,
2278 &state
->ctm
.c
, &state
->ctm
.d
,
2279 &state
->ctm
.e
, &state
->ctm
.f
);
2281 dom_string_unref(attr
);
2286 * Parse element styles.
2288 * First we parse any inline "style" attributes. We then compose the
2289 * element's style with any parent styles. Finally, we compute any
2290 * styles that we support and set the corresponding fields in the
2293 void svgtiny_parse_styles(dom_element
*node
,
2294 struct svgtiny_parse_state
*state
)
2297 uint8_t fill_opacity_type
;
2298 css_fixed fill_opacity
;
2299 uint8_t stroke_opacity_type
;
2300 css_fixed stroke_opacity
;
2302 /* We store the result of svgtiny_parse_style_inline() in
2303 * inline_sheet, and that function returns NULL on error; in
2304 * particular you do not need to css_stylesheet_destroy() the
2305 * result if it is NULL, and css_stylesheet_destroy() checks
2307 css_stylesheet
*inline_sheet
= NULL
;
2309 /* Initialize this to NULL for the same reason: so that we can
2310 * safely destroy it later even if we never populated it. */
2311 css_select_results
*styles
= NULL
;
2317 exc
= dom_element_get_attribute(node
, state
->interned_style
, &attr
);
2318 if (exc
!= DOM_NO_ERR
) {
2322 inline_sheet
= svgtiny_parse_style_inline(
2323 (uint8_t *)dom_string_data(attr
),
2324 dom_string_byte_length(attr
));
2325 dom_string_unref(attr
);
2328 struct dom_element
*parent
;
2329 dom_element_parent_node(node
, &parent
);
2330 if (parent
== NULL
) {
2331 /* This is the root <svg> node, skip it.
2333 * While initialising its selection state, libcss sets its
2334 * node_data->bloom pointer using css__get_parent_bloom().
2335 * But if there is no parent, that function returns,
2337 * static css_bloom empty_bloom[CSS_BLOOM_SIZE];
2339 * A problem later arises because when libcss FINALISES its
2340 * selection state, it frees node_data->bloom! That obviously
2341 * won't work then node has no parent, i.e. if it's the root
2344 css_stylesheet_destroy(inline_sheet
);
2348 /* We only needed to know if it was NULL */
2349 dom_node_unref(parent
);
2352 code
= svgtiny_select_style(state
, node
, inline_sheet
, &styles
);
2353 css_stylesheet_destroy(inline_sheet
);
2354 if (code
!= CSS_OK
) {
2358 fill_opacity_type
= css_computed_fill_opacity(
2359 styles
->styles
[CSS_PSEUDO_ELEMENT_NONE
],
2361 stroke_opacity_type
= css_computed_stroke_opacity(
2362 styles
->styles
[CSS_PSEUDO_ELEMENT_NONE
],
2364 css_select_results_destroy(styles
);
2366 if (fill_opacity_type
== CSS_FILL_OPACITY_SET
) {
2367 state
->fill_opacity
= FIXTOFLT(fill_opacity
);
2369 if (stroke_opacity_type
== CSS_STROKE_OPACITY_SET
) {
2370 state
->stroke_opacity
= FIXTOFLT(stroke_opacity
);
2375 * Parse a transform string.
2378 void svgtiny_parse_transform(char *s
, float *ma
, float *mb
,
2379 float *mc
, float *md
, float *me
, float *mf
)
2381 float a
, b
, c
, d
, e
, f
;
2382 float za
, zb
, zc
, zd
, ze
, zf
;
2387 for (i
= 0; s
[i
]; i
++)
2396 if ((sscanf(s
, " matrix (%f %f %f %f %f %f ) %n",
2397 &a
, &b
, &c
, &d
, &e
, &f
, &n
) == 6) && (n
> 0))
2399 else if ((sscanf(s
, " translate (%f %f ) %n",
2400 &e
, &f
, &n
) == 2) && (n
> 0))
2402 else if ((sscanf(s
, " translate (%f ) %n",
2403 &e
, &n
) == 1) && (n
> 0))
2405 else if ((sscanf(s
, " scale (%f %f ) %n",
2406 &a
, &d
, &n
) == 2) && (n
> 0))
2408 else if ((sscanf(s
, " scale (%f ) %n",
2409 &a
, &n
) == 1) && (n
> 0))
2411 else if ((sscanf(s
, " rotate (%f %f %f ) %n",
2412 &angle
, &x
, &y
, &n
) == 3) && (n
> 0)) {
2413 angle
= angle
/ 180 * M_PI
;
2418 e
= -x
* cos(angle
) + y
* sin(angle
) + x
;
2419 f
= -x
* sin(angle
) - y
* cos(angle
) + y
;
2420 } else if ((sscanf(s
, " rotate (%f ) %n",
2421 &angle
, &n
) == 1) && (n
> 0)) {
2422 angle
= angle
/ 180 * M_PI
;
2427 } else if ((sscanf(s
, " skewX (%f ) %n",
2428 &angle
, &n
) == 1) && (n
> 0)) {
2429 angle
= angle
/ 180 * M_PI
;
2431 } else if ((sscanf(s
, " skewY (%f ) %n",
2432 &angle
, &n
) == 1) && (n
> 0)) {
2433 angle
= angle
/ 180 * M_PI
;
2437 za
= *ma
* a
+ *mc
* b
;
2438 zb
= *mb
* a
+ *md
* b
;
2439 zc
= *ma
* c
+ *mc
* d
;
2440 zd
= *mb
* c
+ *md
* d
;
2441 ze
= *ma
* e
+ *mc
* f
+ *me
;
2442 zf
= *mb
* e
+ *md
* f
+ *mf
;
2455 * Add a path to the svgtiny_diagram.
2458 svgtiny_code
svgtiny_add_path(float *p
, unsigned int n
,
2459 struct svgtiny_parse_state
*state
)
2461 struct svgtiny_shape
*shape
;
2463 if (state
->fill
== svgtiny_LINEAR_GRADIENT
)
2464 return svgtiny_add_path_linear_gradient(p
, n
, state
);
2466 svgtiny_transform_path(p
, n
, state
);
2468 shape
= svgtiny_add_shape(state
);
2471 return svgtiny_OUT_OF_MEMORY
;
2474 shape
->path_length
= n
;
2475 state
->diagram
->shape_count
++;
2482 * Add a svgtiny_shape to the svgtiny_diagram.
2485 struct svgtiny_shape
*svgtiny_add_shape(struct svgtiny_parse_state
*state
)
2487 struct svgtiny_shape
*shape
= realloc(state
->diagram
->shape
,
2488 (state
->diagram
->shape_count
+ 1) *
2489 sizeof (state
->diagram
->shape
[0]));
2492 state
->diagram
->shape
= shape
;
2494 shape
+= state
->diagram
->shape_count
;
2496 shape
->path_length
= 0;
2498 shape
->fill
= state
->fill
;
2499 shape
->stroke
= state
->stroke
;
2500 shape
->fill_opacity
= state
->fill_opacity
;
2501 shape
->stroke_opacity
= state
->stroke_opacity
;
2502 shape
->stroke_width
= lroundf((float) state
->stroke_width
*
2503 (state
->ctm
.a
+ state
->ctm
.d
) / 2.0);
2504 if (0 < state
->stroke_width
&& shape
->stroke_width
== 0)
2505 shape
->stroke_width
= 1;
2512 * Apply the current transformation matrix to a path.
2515 void svgtiny_transform_path(float *p
, unsigned int n
,
2516 struct svgtiny_parse_state
*state
)
2520 for (j
= 0; j
!= n
; ) {
2521 unsigned int points
= 0;
2523 switch ((int) p
[j
]) {
2524 case svgtiny_PATH_MOVE
:
2525 case svgtiny_PATH_LINE
:
2528 case svgtiny_PATH_CLOSE
:
2531 case svgtiny_PATH_BEZIER
:
2538 for (k
= 0; k
!= points
; k
++) {
2539 float x0
= p
[j
], y0
= p
[j
+ 1];
2540 float x
= state
->ctm
.a
* x0
+ state
->ctm
.c
* y0
+
2542 float y
= state
->ctm
.b
* x0
+ state
->ctm
.d
* y0
+
2553 * Free all memory used by a diagram.
2556 void svgtiny_free(struct svgtiny_diagram
*svg
)
2561 for (i
= 0; i
!= svg
->shape_count
; i
++) {
2562 free(svg
->shape
[i
].path
);
2563 free(svg
->shape
[i
].text
);
2571 #ifndef HAVE_STRNDUP
2572 char *svgtiny_strndup(const char *s
, size_t n
)
2577 for (len
= 0; len
!= n
&& s
[len
]; len
++)
2580 s2
= malloc(len
+ 1);