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 svgtiny_code
svgtiny_add_path(float *p
, unsigned int n
,
75 struct svgtiny_parse_state
*state
);
76 static void _svgtiny_parse_color(const char *s
, svgtiny_colour
*c
,
77 struct svgtiny_parse_state_gradient
*grad
,
78 struct svgtiny_parse_state
*state
);
81 * rotate midpoint vector
84 rotate_midpoint_vector(float ax
, float ay
,
87 double *x_out
, double *y_out
)
89 double dx2
; /* midpoint x coordinate */
90 double dy2
; /* midpoint y coordinate */
91 double cosangle
; /* cosine of rotation angle */
92 double sinangle
; /* sine of rotation angle */
94 /* compute the sin and cos of the angle */
95 cosangle
= cos(radangle
);
96 sinangle
= sin(radangle
);
98 /* compute the midpoint between start and end points */
99 dx2
= (ax
- bx
) / 2.0;
100 dy2
= (ay
- by
) / 2.0;
102 /* rotate vector to remove angle */
103 *x_out
= ((cosangle
* dx2
) + (sinangle
* dy2
));
104 *y_out
= ((-sinangle
* dx2
) + (cosangle
* dy2
));
109 * ensure the arc radii are large enough and scale as appropriate
111 * the radii need to be large enough if they are not they must be
112 * adjusted. This allows for elimination of differences between
113 * implementations especialy with rounding.
116 ensure_radii_scale(double x1_sq
, double y1_sq
,
117 float *rx
, float *ry
,
118 double *rx_sq
, double *ry_sq
)
123 /* set radii square values */
124 (*rx_sq
) = (*rx
) * (*rx
);
125 (*ry_sq
) = (*ry
) * (*ry
);
127 radiisum
= (x1_sq
/ (*rx_sq
)) + (y1_sq
/ (*ry_sq
));
128 if (radiisum
> 0.99999) {
129 /* need to scale radii */
130 radiiscale
= sqrt(radiisum
) * 1.00001;
131 *rx
= (float)(radiiscale
* (*rx
));
132 *ry
= (float)(radiiscale
* (*ry
));
133 /* update squares too */
134 (*rx_sq
) = (*rx
) * (*rx
);
135 (*ry_sq
) = (*ry
) * (*ry
);
141 * compute the transformed centre point
144 compute_transformed_centre_point(double sign
, float rx
, float ry
,
145 double rx_sq
, double ry_sq
,
146 double x1
, double y1
,
147 double x1_sq
, double y1_sq
,
148 double *cx1
, double *cy1
)
152 sq
= ((rx_sq
* ry_sq
) - (rx_sq
* y1_sq
) - (ry_sq
* x1_sq
)) /
153 ((rx_sq
* y1_sq
) + (ry_sq
* x1_sq
));
154 sq
= (sq
< 0) ? 0 : sq
;
156 coef
= (sign
* sqrt(sq
));
158 *cx1
= coef
* ((rx
* y1
) / ry
);
159 *cy1
= coef
* -((ry
* x1
) / rx
);
164 * compute untransformed centre point
166 * \param ax The first point x coordinate
167 * \param ay The first point y coordinate
168 * \param bx The second point x coordinate
169 * \param ay The second point y coordinate
172 compute_centre_point(float ax
, float ay
,
174 double cx1
, double cy1
,
176 double *x_out
, double *y_out
)
180 double cosangle
; /* cosine of rotation angle */
181 double sinangle
; /* sine of rotation angle */
183 /* compute the sin and cos of the angle */
184 cosangle
= cos(radangle
);
185 sinangle
= sin(radangle
);
187 sx2
= (ax
+ bx
) / 2.0;
188 sy2
= (ay
+ by
) / 2.0;
190 *x_out
= sx2
+ (cosangle
* cx1
- sinangle
* cy1
);
191 *y_out
= sy2
+ (sinangle
* cx1
+ cosangle
* cy1
);
196 * compute the angle start and extent
199 compute_angle_start_extent(float rx
, float ry
,
200 double x1
, double y1
,
201 double cx1
, double cy1
,
202 double *start
, double *extent
)
213 * Angle betwen two vectors is +/- acos( u.v / len(u) * len(v))
215 * '.' is the dot product.
216 * +/- is calculated from the sign of the cross product (u x v)
219 ux
= (x1
- cx1
) / rx
;
220 uy
= (y1
- cy1
) / ry
;
221 vx
= (-x1
- cx1
) / rx
;
222 vy
= (-y1
- cy1
) / ry
;
224 /* compute the start angle */
225 /* The angle between (ux, uy) and the 0 angle */
227 /* len(u) * len(1,0) == len(u) */
228 n
= sqrt((ux
* ux
) + (uy
* uy
));
229 /* u.v == (ux,uy).(1,0) == (1 * ux) + (0 * uy) == ux */
231 /* u x v == (1 * uy - ux * 0) == uy */
232 sign
= (uy
< 0) ? -1.0 : 1.0;
233 /* (p >= n) so safe */
234 *start
= sign
* acos(p
/ n
);
236 /* compute the extent angle */
237 n
= sqrt(((ux
* ux
) + (uy
* uy
)) * ((vx
* vx
) + (vy
* vy
)));
238 p
= (ux
* vx
) + (uy
* vy
);
239 sign
= ((ux
* vy
) - (uy
* vx
) < 0) ? -1.0f
: 1.0f
;
241 /* arc cos must operate between -1 and 1 */
244 *extent
= sign
* M_PI
;
245 } else if (actmp
> 1.0) {
248 *extent
= sign
* acos(actmp
);
254 * converts a circle centered unit circle arc to a series of bezier curves
256 * Each bezier is stored as six values of three pairs of coordinates
258 * The beziers are stored without their start point as that is assumed
259 * to be the preceding elements end point.
261 * \param start The start angle of the arc (in radians)
262 * \param extent The size of the arc (in radians)
263 * \param bzpt The array to store the bezier values in
264 * \return The number of bezier segments output (max 4)
267 circle_arc_to_bezier(double start
, double extent
, double *bzpt
)
277 bzsegments
= (int) ceil(fabs(extent
) / M_PI_2
);
278 increment
= extent
/ bzsegments
;
279 controllen
= 4.0 / 3.0 * sin(increment
/ 2.0) / (1.0 + cos(increment
/ 2.0));
281 for (segment
= 0; segment
< bzsegments
; segment
++) {
282 /* first control point */
283 angle
= start
+ (segment
* increment
);
286 bzpt
[pos
++] = dx
- controllen
* dy
;
287 bzpt
[pos
++] = dy
+ controllen
* dx
;
288 /* second control point */
292 bzpt
[pos
++] = dx
+ controllen
* dy
;
293 bzpt
[pos
++] = dy
- controllen
* dx
;
304 * transform coordinate list
306 * perform a scale, rotate and translate on list of coordinates
312 * homogeneous transforms
320 * | cos(an) -sin(an) 0 |
321 * R = | sin(an) cos(an) 0 |
324 * {{cos(a), -sin(a) 0}, {sin(a), cos(a),0}, {0,0,1}}
331 * note order is significat here and the combined matrix is
334 * | cos(an) -sin(an) cx |
335 * T.R = | sin(an) cos(an) cy |
338 * | rx * cos(an) ry * -sin(an) cx |
339 * T.R.S = | rx * sin(an) ry * cos(an) cy |
342 * {{Cos[a], -Sin[a], c}, {Sin[a], Cos[a], d}, {0, 0, 1}} . {{r, 0, 0}, {0, s, 0}, {0, 0, 1}}
354 * x2 = cx + (rx * x1 * cos(a)) + (ry * y1 * -1 * sin(a))
355 * y2 = cy + (ry * y1 * cos(a)) + (rx * x1 * sin(a))
358 * \param rx X scaling to apply
359 * \param ry Y scaling to apply
360 * \param radangle rotation to apply (in radians)
361 * \param cx X translation to apply
362 * \param cy Y translation to apply
363 * \param points The size of the bzpoints array
364 * \param bzpoints an array of x,y values to apply the transform to
367 scale_rotate_translate_points(double rx
, double ry
,
369 double cx
, double cy
,
374 double cosangle
; /* cosine of rotation angle */
375 double sinangle
; /* sine of rotation angle */
376 double rxcosangle
, rxsinangle
, rycosangle
, rynsinangle
;
379 /* compute the sin and cos of the angle */
380 cosangle
= cos(radangle
);
381 sinangle
= sin(radangle
);
383 rxcosangle
= rx
* cosangle
;
384 rxsinangle
= rx
* sinangle
;
385 rycosangle
= ry
* cosangle
;
386 rynsinangle
= ry
* -1 * sinangle
;
388 for (pnt
= 0; pnt
< pntsize
; pnt
+=2) {
389 x2
= cx
+ (points
[pnt
] * rxcosangle
) + (points
[pnt
+ 1] * rynsinangle
);
390 y2
= cy
+ (points
[pnt
+ 1] * rycosangle
) + (points
[pnt
] * rxsinangle
);
392 points
[pnt
+ 1] = y2
;
398 * convert an svg path arc to a bezier curve
400 * This function perfoms a transform on the nine arc parameters
401 * (coordinate pairs for start and end together with the radii of the
402 * elipse, the rotation angle and which of the four arcs to draw)
403 * which generates the parameters (coordinate pairs for start,
404 * end and their control points) for a set of up to four bezier curves.
406 * Obviously the start and end coordinates are not altered between
407 * representations so the aim is to calculate the coordinate pairs for
408 * the bezier control points.
410 * \param bzpoints the array to fill with bezier curves
411 * \return the number of bezier segments generated or -1 for a line
414 svgarc_to_bezier(float start_x
,
425 double radangle
; /* normalised elipsis rotation angle in radians */
426 double rx_sq
; /* x radius squared */
427 double ry_sq
; /* y radius squared */
428 double x1
, y1
; /* rotated midpoint vector */
429 double x1_sq
, y1_sq
; /* x1 vector squared */
430 double cx1
,cy1
; /* transformed circle center */
431 double cx
,cy
; /* circle center */
432 double start
, extent
;
435 if ((start_x
== end_x
) && (start_y
== end_y
)) {
437 * if the start and end coordinates are the same the
438 * svg spec says this is equivalent to having no segment
444 if ((rx
== 0) || (ry
== 0)) {
446 * if either radii is zero the specified behaviour is a line
451 /* obtain the absolute values of the radii */
455 /* convert normalised angle to radians */
456 radangle
= degToRad(fmod(angle
, 360.0));
459 /* x1,x2 is the midpoint vector rotated to remove the arc angle */
460 rotate_midpoint_vector(start_x
, start_y
, end_x
, end_y
, radangle
, &x1
, &y1
);
463 /* get squared x1 values */
467 /* ensure radii are correctly scaled */
468 ensure_radii_scale(x1_sq
, y1_sq
, &rx
, &ry
, &rx_sq
, &ry_sq
);
470 /* compute the transformed centre point */
471 compute_transformed_centre_point(largearc
== sweep
?-1:1,
479 /* get the untransformed centre point */
480 compute_centre_point(start_x
, start_y
,
487 /* compute anglestart and extent */
488 compute_angle_start_extent(rx
,ry
,
493 /* extent of 0 is a straight line */
498 /* take account of sweep */
499 if (!sweep
&& extent
> 0) {
501 } else if (sweep
&& extent
< 0) {
505 /* normalise start and extent */
506 extent
= fmod(extent
, TAU
);
507 start
= fmod(start
, TAU
);
509 /* convert the arc to unit circle bezier curves */
510 bzsegments
= circle_arc_to_bezier(start
, extent
, bzpoints
);
512 /* transform the bezier curves */
513 scale_rotate_translate_points(rx
, ry
,
524 * Call this to ref the strings in a gradient state.
526 static void svgtiny_grad_string_ref(struct svgtiny_parse_state_gradient
*grad
)
528 if (grad
->gradient_x1
!= NULL
) {
529 dom_string_ref(grad
->gradient_x1
);
531 if (grad
->gradient_y1
!= NULL
) {
532 dom_string_ref(grad
->gradient_y1
);
534 if (grad
->gradient_x2
!= NULL
) {
535 dom_string_ref(grad
->gradient_x2
);
537 if (grad
->gradient_y2
!= NULL
) {
538 dom_string_ref(grad
->gradient_y2
);
543 * Call this to clean up the strings in a gradient state.
545 static void svgtiny_grad_string_cleanup(
546 struct svgtiny_parse_state_gradient
*grad
)
548 if (grad
->gradient_x1
!= NULL
) {
549 dom_string_unref(grad
->gradient_x1
);
550 grad
->gradient_x1
= NULL
;
552 if (grad
->gradient_y1
!= NULL
) {
553 dom_string_unref(grad
->gradient_y1
);
554 grad
->gradient_y1
= NULL
;
556 if (grad
->gradient_x2
!= NULL
) {
557 dom_string_unref(grad
->gradient_x2
);
558 grad
->gradient_x2
= NULL
;
560 if (grad
->gradient_y2
!= NULL
) {
561 dom_string_unref(grad
->gradient_y2
);
562 grad
->gradient_y2
= NULL
;
567 * Set the local externally-stored parts of a parse state.
568 * Call this in functions that made a new state on the stack.
569 * Doesn't make own copy of global state, such as the interned string list.
571 static void svgtiny_setup_state_local(struct svgtiny_parse_state
*state
)
573 svgtiny_grad_string_ref(&(state
->fill_grad
));
574 svgtiny_grad_string_ref(&(state
->stroke_grad
));
578 * Cleanup the local externally-stored parts of a parse state.
579 * Call this in functions that made a new state on the stack.
580 * Doesn't cleanup global state, such as the interned string list.
582 static void svgtiny_cleanup_state_local(struct svgtiny_parse_state
*state
)
584 svgtiny_grad_string_cleanup(&(state
->fill_grad
));
585 svgtiny_grad_string_cleanup(&(state
->stroke_grad
));
590 * Create a new svgtiny_diagram structure.
593 struct svgtiny_diagram
*svgtiny_create(void)
595 struct svgtiny_diagram
*diagram
;
597 diagram
= calloc(sizeof(*diagram
), 1);
606 static void ignore_msg(uint32_t severity
, void *ctx
, const char *msg
, ...)
614 * Parse a block of memory into a svgtiny_diagram.
617 svgtiny_code
svgtiny_parse(struct svgtiny_diagram
*diagram
,
618 const char *buffer
, size_t size
, const char *url
,
619 int viewport_width
, int viewport_height
)
622 dom_document
*document
;
624 dom_xml_parser
*parser
;
627 dom_string
*svg_name
;
628 lwc_string
*svg_name_lwc
;
629 struct svgtiny_parse_state state
;
630 float x
, y
, width
, height
;
639 parser
= dom_xml_parser_create(NULL
, NULL
,
640 ignore_msg
, NULL
, &document
);
643 return svgtiny_LIBDOM_ERROR
;
645 err
= dom_xml_parser_parse_chunk(parser
, (uint8_t *)buffer
, size
);
646 if (err
!= DOM_XML_OK
) {
647 dom_node_unref(document
);
648 dom_xml_parser_destroy(parser
);
649 return svgtiny_LIBDOM_ERROR
;
652 err
= dom_xml_parser_completed(parser
);
653 if (err
!= DOM_XML_OK
) {
654 dom_node_unref(document
);
655 dom_xml_parser_destroy(parser
);
656 return svgtiny_LIBDOM_ERROR
;
659 /* We're done parsing, drop the parser.
660 * We now own the document entirely.
662 dom_xml_parser_destroy(parser
);
664 /* find root <svg> element */
665 exc
= dom_document_get_document_element(document
, &svg
);
666 if (exc
!= DOM_NO_ERR
) {
667 dom_node_unref(document
);
668 return svgtiny_LIBDOM_ERROR
;
671 /* no root svg element */
672 dom_node_unref(document
);
673 return svgtiny_SVG_ERROR
;
676 exc
= dom_node_get_node_name(svg
, &svg_name
);
677 if (exc
!= DOM_NO_ERR
) {
679 dom_node_unref(document
);
680 return svgtiny_LIBDOM_ERROR
;
682 if (lwc_intern_string("svg", 3 /* SLEN("svg") */,
683 &svg_name_lwc
) != lwc_error_ok
) {
684 dom_string_unref(svg_name
);
686 dom_node_unref(document
);
687 return svgtiny_LIBDOM_ERROR
;
689 if (!dom_string_caseless_lwc_isequal(svg_name
, svg_name_lwc
)) {
690 lwc_string_unref(svg_name_lwc
);
691 dom_string_unref(svg_name
);
693 dom_node_unref(document
);
694 return svgtiny_NOT_SVG
;
697 lwc_string_unref(svg_name_lwc
);
698 dom_string_unref(svg_name
);
700 /* get graphic dimensions */
701 memset(&state
, 0, sizeof(state
));
702 state
.diagram
= diagram
;
703 state
.document
= document
;
704 state
.viewport_width
= viewport_width
;
705 state
.viewport_height
= viewport_height
;
708 /* Initialize CSS context */
709 if (state
.select_ctx
== NULL
) {
710 css_code
= css_select_ctx_create(&state
.select_ctx
);
711 if (css_code
!= CSS_OK
) {
713 dom_node_unref(document
);
714 return svgtiny_LIBCSS_ERROR
;
718 #define SVGTINY_STRING_ACTION2(s,n) \
719 if (dom_string_create_interned((const uint8_t *) #n, \
720 strlen(#n), &state.interned_##s) \
722 code = svgtiny_LIBDOM_ERROR; \
725 #include "svgtiny_strings.h"
726 #undef SVGTINY_STRING_ACTION2
728 /* Intern SVG's xmlns separately because it's an lwc_string
729 * and not a dom_string. We initialize its pointer to NULL
730 * because the "cleanup:" test to see if it needs to be free'd
731 * looks for NULL. Returning a LIBDOM_ERROR on failure is not
732 * perfect but it's the closest of the available options. */
733 state
.interned_svg_xmlns
= NULL
;
734 if (lwc_intern_string("http://www.w3.org/2000/svg",
736 &state
.interned_svg_xmlns
) != lwc_error_ok
) {
737 code
= svgtiny_LIBDOM_ERROR
;
741 svgtiny_parse_position_attributes(svg
, state
, &x
, &y
, &width
, &height
);
742 diagram
->width
= width
;
743 diagram
->height
= height
;
745 /* set up parsing state */
746 state
.viewport_width
= width
;
747 state
.viewport_height
= height
;
748 state
.ctm
.a
= 1; /*(float) viewport_width / (float) width;*/
751 state
.ctm
.d
= 1; /*(float) viewport_height / (float) height;*/
752 state
.ctm
.e
= 0; /*x;*/
753 state
.ctm
.f
= 0; /*y;*/
754 state
.fill
= 0x000000;
755 state
.stroke
= svgtiny_TRANSPARENT
;
756 state
.stroke_width
= 1;
759 code
= svgtiny_preparse_styles(svg
, state
);
760 if (code
== svgtiny_OK
) {
761 code
= svgtiny_parse_svg(svg
, state
);
765 dom_node_unref(document
);
766 css_code
= css_select_ctx_destroy(state
.select_ctx
);
767 if (css_code
!= CSS_OK
) {
768 code
= svgtiny_LIBCSS_ERROR
;
772 svgtiny_cleanup_state_local(&state
);
773 #define SVGTINY_STRING_ACTION2(s,n) \
774 if (state.interned_##s != NULL) \
775 dom_string_unref(state.interned_##s);
776 #include "svgtiny_strings.h"
777 #undef SVGTINY_STRING_ACTION2
779 if (state
.interned_svg_xmlns
!= NULL
) {
780 lwc_string_unref(state
.interned_svg_xmlns
);
788 * Parse a single <style> element, appending the result to the CSS
789 * select context within the given parser state.
791 svgtiny_code
svgtiny_parse_style_element(dom_element
*style
,
792 struct svgtiny_parse_state state
)
794 css_stylesheet
*sheet
;
798 code
= svgtiny_create_stylesheet(&sheet
, false);
799 if (code
!= CSS_OK
) {
800 return svgtiny_LIBCSS_ERROR
;
803 /* Parse the style element's "media" attribute if it has
804 one. We don't do anything with it right now. */
805 dom_string
*media_attr
;
806 exc
= dom_element_get_attribute(style
, state
.interned_media
,
808 if (exc
!= DOM_NO_ERR
) {
809 css_stylesheet_destroy(sheet
);
810 return svgtiny_LIBDOM_ERROR
;
814 /* Here's where we'd actually change the media type if
815 we were going to use it */
816 dom_string_unref(media_attr
);
820 dom_node_get_text_content(style
, &data
);
822 /* Empty stylesheet? That's fine. */
823 css_stylesheet_destroy(sheet
);
827 code
= css_stylesheet_append_data(sheet
,
828 (uint8_t *)dom_string_data(data
),
829 dom_string_byte_length(data
));
830 if (code
!= CSS_OK
&& code
!= CSS_NEEDDATA
) {
831 dom_string_unref(data
);
832 css_stylesheet_destroy(sheet
);
833 return svgtiny_LIBCSS_ERROR
;
836 code
= css_stylesheet_data_done(sheet
);
837 if (code
!= CSS_OK
) {
838 dom_string_unref(data
);
839 css_stylesheet_destroy(sheet
);
840 return svgtiny_LIBCSS_ERROR
;
843 code
= css_select_ctx_append_sheet(state
.select_ctx
,
847 if (code
!= CSS_OK
) {
848 dom_string_unref(data
);
849 return svgtiny_LIBCSS_ERROR
;
852 dom_string_unref(data
);
858 * Parse the contents of an inline style and return (a pointer to) the
859 * corresponding stylesheet for use with css_select_style(). Returns
860 * NULL if anything goes wrong.
862 css_stylesheet
*svgtiny_parse_style_inline(const uint8_t *data
,
865 css_stylesheet
*sheet
;
868 code
= svgtiny_create_stylesheet(&sheet
, true);
869 if (code
!= CSS_OK
) {
873 code
= css_stylesheet_append_data(sheet
, data
, len
);
874 if (code
!= CSS_OK
&& code
!= CSS_NEEDDATA
) {
875 css_stylesheet_destroy(sheet
);
879 code
= css_stylesheet_data_done(sheet
);
880 if (code
!= CSS_OK
) {
881 css_stylesheet_destroy(sheet
);
889 * Parse all <style> elements within a root <svg> element. This
890 * should be called before svgtiny_parse_svg() because that function
891 * makes a single pass through the document and we'd like all style
892 * information to be available during that pass. Specifically, we'd
893 * like a <style> sheet at the end of the document to affect the
894 * rendering of elements at its beginning.
896 * The element-parsing inner loop here is essentially the same as
897 * that within svgtiny_parse_svg().
899 svgtiny_code
svgtiny_preparse_styles(dom_element
*svg
,
900 struct svgtiny_parse_state state
)
905 exc
= dom_node_get_first_child(svg
, (dom_node
**) (void *) &child
);
906 if (exc
!= DOM_NO_ERR
) {
907 return svgtiny_LIBDOM_ERROR
;
909 while (child
!= NULL
) {
911 dom_node_type nodetype
;
912 svgtiny_code code
= svgtiny_OK
;
914 exc
= dom_node_get_node_type(child
, &nodetype
);
915 if (exc
!= DOM_NO_ERR
) {
916 dom_node_unref(child
);
917 return svgtiny_LIBDOM_ERROR
;
919 if (nodetype
== DOM_ELEMENT_NODE
) {
920 dom_string
*nodename
;
921 exc
= dom_node_get_node_name(child
, &nodename
);
922 if (exc
!= DOM_NO_ERR
) {
923 dom_node_unref(child
);
924 return svgtiny_LIBDOM_ERROR
;
927 if (dom_string_caseless_isequal(state
.interned_style
,
929 /* We have a <style> element, parse it */
930 code
= svgtiny_parse_style_element(child
,
935 dom_string_unref(nodename
);
937 if (code
!= svgtiny_OK
) {
938 dom_node_unref(child
);
941 exc
= dom_node_get_next_sibling(child
,
942 (dom_node
**) (void *) &next
);
943 dom_node_unref(child
);
944 if (exc
!= DOM_NO_ERR
) {
945 return svgtiny_LIBDOM_ERROR
;
954 * Parse a <svg> or <g> element node.
957 svgtiny_code
svgtiny_parse_svg(dom_element
*svg
,
958 struct svgtiny_parse_state state
)
960 float x
, y
, width
, height
;
961 dom_string
*view_box
;
965 svgtiny_setup_state_local(&state
);
967 svgtiny_parse_position_attributes(svg
, state
, &x
, &y
, &width
, &height
);
968 svgtiny_parse_paint_attributes(svg
, &state
);
969 svgtiny_parse_font_attributes(svg
, &state
);
971 exc
= dom_element_get_attribute(svg
, state
.interned_viewBox
,
973 if (exc
!= DOM_NO_ERR
) {
974 svgtiny_cleanup_state_local(&state
);
975 return svgtiny_LIBDOM_ERROR
;
979 char *s
= strndup(dom_string_data(view_box
),
980 dom_string_byte_length(view_box
));
981 float min_x
, min_y
, vwidth
, vheight
;
982 if (sscanf(s
, "%f,%f,%f,%f",
983 &min_x
, &min_y
, &vwidth
, &vheight
) == 4 ||
984 sscanf(s
, "%f %f %f %f",
985 &min_x
, &min_y
, &vwidth
, &vheight
) == 4) {
986 state
.ctm
.a
= (float) state
.viewport_width
/ vwidth
;
987 state
.ctm
.d
= (float) state
.viewport_height
/ vheight
;
988 state
.ctm
.e
+= -min_x
* state
.ctm
.a
;
989 state
.ctm
.f
+= -min_y
* state
.ctm
.d
;
992 dom_string_unref(view_box
);
995 svgtiny_parse_transform_attributes(svg
, &state
);
997 exc
= dom_node_get_first_child(svg
, (dom_node
**) (void *) &child
);
998 if (exc
!= DOM_NO_ERR
) {
999 svgtiny_cleanup_state_local(&state
);
1000 return svgtiny_LIBDOM_ERROR
;
1002 while (child
!= NULL
) {
1004 dom_node_type nodetype
;
1005 svgtiny_code code
= svgtiny_OK
;
1007 exc
= dom_node_get_node_type(child
, &nodetype
);
1008 if (exc
!= DOM_NO_ERR
) {
1009 dom_node_unref(child
);
1010 return svgtiny_LIBDOM_ERROR
;
1012 if (nodetype
== DOM_ELEMENT_NODE
) {
1013 dom_string
*nodename
;
1014 exc
= dom_node_get_node_name(child
, &nodename
);
1015 if (exc
!= DOM_NO_ERR
) {
1016 dom_node_unref(child
);
1017 svgtiny_cleanup_state_local(&state
);
1018 return svgtiny_LIBDOM_ERROR
;
1020 if (dom_string_caseless_isequal(state
.interned_svg
,
1022 code
= svgtiny_parse_svg(child
, state
);
1023 else if (dom_string_caseless_isequal(state
.interned_g
,
1025 code
= svgtiny_parse_svg(child
, state
);
1026 else if (dom_string_caseless_isequal(state
.interned_a
,
1028 code
= svgtiny_parse_svg(child
, state
);
1029 else if (dom_string_caseless_isequal(state
.interned_path
,
1031 code
= svgtiny_parse_path(child
, state
);
1032 else if (dom_string_caseless_isequal(state
.interned_rect
,
1034 code
= svgtiny_parse_rect(child
, state
);
1035 else if (dom_string_caseless_isequal(state
.interned_circle
,
1037 code
= svgtiny_parse_circle(child
, state
);
1038 else if (dom_string_caseless_isequal(state
.interned_ellipse
,
1040 code
= svgtiny_parse_ellipse(child
, state
);
1041 else if (dom_string_caseless_isequal(state
.interned_line
,
1043 code
= svgtiny_parse_line(child
, state
);
1044 else if (dom_string_caseless_isequal(state
.interned_polyline
,
1046 code
= svgtiny_parse_poly(child
, state
, false);
1047 else if (dom_string_caseless_isequal(state
.interned_polygon
,
1049 code
= svgtiny_parse_poly(child
, state
, true);
1050 else if (dom_string_caseless_isequal(state
.interned_text
,
1052 code
= svgtiny_parse_text(child
, state
);
1053 dom_string_unref(nodename
);
1055 if (code
!= svgtiny_OK
) {
1056 dom_node_unref(child
);
1057 svgtiny_cleanup_state_local(&state
);
1060 exc
= dom_node_get_next_sibling(child
,
1061 (dom_node
**) (void *) &next
);
1062 dom_node_unref(child
);
1063 if (exc
!= DOM_NO_ERR
) {
1064 svgtiny_cleanup_state_local(&state
);
1065 return svgtiny_LIBDOM_ERROR
;
1070 svgtiny_cleanup_state_local(&state
);
1077 * Parse a <path> element node.
1079 * http://www.w3.org/TR/SVG11/paths#PathElement
1082 svgtiny_code
svgtiny_parse_path(dom_element
*path
,
1083 struct svgtiny_parse_state state
)
1086 dom_string
*path_d_str
;
1089 float *p
; /* path elemets */
1090 unsigned int palloc
; /* number of path elements allocated */
1092 float last_x
= 0, last_y
= 0;
1093 float last_cubic_x
= 0, last_cubic_y
= 0;
1094 float last_quad_x
= 0, last_quad_y
= 0;
1095 float subpath_first_x
= 0, subpath_first_y
= 0;
1097 svgtiny_setup_state_local(&state
);
1099 svgtiny_parse_paint_attributes(path
, &state
);
1100 svgtiny_parse_transform_attributes(path
, &state
);
1102 /* read d attribute */
1103 exc
= dom_element_get_attribute(path
, state
.interned_d
, &path_d_str
);
1104 if (exc
!= DOM_NO_ERR
) {
1105 state
.diagram
->error_line
= -1; /* path->line; */
1106 state
.diagram
->error_message
= "path: error retrieving d attribute";
1107 svgtiny_cleanup_state_local(&state
);
1108 return svgtiny_SVG_ERROR
;
1111 if (path_d_str
== NULL
) {
1112 state
.diagram
->error_line
= -1; /* path->line; */
1113 state
.diagram
->error_message
= "path: missing d attribute";
1114 svgtiny_cleanup_state_local(&state
);
1115 return svgtiny_SVG_ERROR
;
1118 /* empty path is permitted it just disables the path */
1119 palloc
= dom_string_byte_length(path_d_str
);
1121 dom_string_unref(path_d_str
);
1122 svgtiny_cleanup_state_local(&state
);
1126 /* local copy of the path data allowing in-place modification */
1127 s
= path_d
= strndup(dom_string_data(path_d_str
), palloc
);
1128 dom_string_unref(path_d_str
);
1130 svgtiny_cleanup_state_local(&state
);
1131 return svgtiny_OUT_OF_MEMORY
;
1134 /* ensure path element allocation is sensibly bounded */
1137 } else if (palloc
> 64) {
1138 palloc
= palloc
/ 2;
1141 /* allocate initial space for path elements */
1142 p
= malloc(sizeof p
[0] * palloc
);
1145 svgtiny_cleanup_state_local(&state
);
1146 return svgtiny_OUT_OF_MEMORY
;
1149 /* parse d and build path */
1150 for (i
= 0; s
[i
]; i
++)
1157 float x
, y
, x1
, y1
, x2
, y2
, rx
, ry
, rotation
, large_arc
, sweep
;
1160 /* Ensure there is sufficient space for path elements */
1161 #define ALLOC_PATH_ELEMENTS(NUM_ELEMENTS) \
1163 if ((palloc - i) < NUM_ELEMENTS) { \
1165 palloc = (palloc * 2) + (palloc / 2); \
1166 tp = realloc(p, sizeof p[0] * palloc); \
1170 svgtiny_cleanup_state_local(&state); \
1171 return svgtiny_OUT_OF_MEMORY; \
1178 /* moveto (M, m), lineto (L, l) (2 arguments) */
1179 if (sscanf(s
, " %1[MmLl] %f %f %n", command
, &x
, &y
, &n
) == 3) {
1180 /*LOG(("moveto or lineto"));*/
1181 if (*command
== 'M' || *command
== 'm')
1182 plot_command
= svgtiny_PATH_MOVE
;
1184 plot_command
= svgtiny_PATH_LINE
;
1186 ALLOC_PATH_ELEMENTS(3);
1187 p
[i
++] = plot_command
;
1188 if ('a' <= *command
) {
1192 if (plot_command
== svgtiny_PATH_MOVE
) {
1193 subpath_first_x
= x
;
1194 subpath_first_y
= y
;
1196 p
[i
++] = last_cubic_x
= last_quad_x
= last_x
1198 p
[i
++] = last_cubic_y
= last_quad_y
= last_y
1201 plot_command
= svgtiny_PATH_LINE
;
1202 } while (sscanf(s
, "%f %f %n", &x
, &y
, &n
) == 2);
1204 /* closepath (Z, z) (no arguments) */
1205 } else if (sscanf(s
, " %1[Zz] %n", command
, &n
) == 1) {
1206 /*LOG(("closepath"));*/
1207 ALLOC_PATH_ELEMENTS(1);
1209 p
[i
++] = svgtiny_PATH_CLOSE
;
1211 last_cubic_x
= last_quad_x
= last_x
= subpath_first_x
;
1212 last_cubic_y
= last_quad_y
= last_y
= subpath_first_y
;
1214 /* horizontal lineto (H, h) (1 argument) */
1215 } else if (sscanf(s
, " %1[Hh] %f %n", command
, &x
, &n
) == 2) {
1216 /*LOG(("horizontal lineto"));*/
1218 ALLOC_PATH_ELEMENTS(3);
1220 p
[i
++] = svgtiny_PATH_LINE
;
1221 if (*command
== 'h')
1223 p
[i
++] = last_cubic_x
= last_quad_x
= last_x
1225 p
[i
++] = last_cubic_y
= last_quad_y
= last_y
;
1227 } while (sscanf(s
, "%f %n", &x
, &n
) == 1);
1229 /* vertical lineto (V, v) (1 argument) */
1230 } else if (sscanf(s
, " %1[Vv] %f %n", command
, &y
, &n
) == 2) {
1231 /*LOG(("vertical lineto"));*/
1233 ALLOC_PATH_ELEMENTS(3);
1235 p
[i
++] = svgtiny_PATH_LINE
;
1236 if (*command
== 'v')
1238 p
[i
++] = last_cubic_x
= last_quad_x
= last_x
;
1239 p
[i
++] = last_cubic_y
= last_quad_y
= last_y
1242 } while (sscanf(s
, "%f %n", &y
, &n
) == 1);
1244 /* curveto (C, c) (6 arguments) */
1245 } else if (sscanf(s
, " %1[Cc] %f %f %f %f %f %f %n", command
,
1246 &x1
, &y1
, &x2
, &y2
, &x
, &y
, &n
) == 7) {
1247 /*LOG(("curveto"));*/
1249 ALLOC_PATH_ELEMENTS(7);
1251 p
[i
++] = svgtiny_PATH_BEZIER
;
1252 if (*command
== 'c') {
1262 p
[i
++] = last_cubic_x
= x2
;
1263 p
[i
++] = last_cubic_y
= y2
;
1264 p
[i
++] = last_quad_x
= last_x
= x
;
1265 p
[i
++] = last_quad_y
= last_y
= y
;
1267 } while (sscanf(s
, "%f %f %f %f %f %f %n",
1268 &x1
, &y1
, &x2
, &y2
, &x
, &y
, &n
) == 6);
1270 /* shorthand/smooth curveto (S, s) (4 arguments) */
1271 } else if (sscanf(s
, " %1[Ss] %f %f %f %f %n", command
,
1272 &x2
, &y2
, &x
, &y
, &n
) == 5) {
1273 /*LOG(("shorthand/smooth curveto"));*/
1275 ALLOC_PATH_ELEMENTS(7);
1277 p
[i
++] = svgtiny_PATH_BEZIER
;
1278 x1
= last_x
+ (last_x
- last_cubic_x
);
1279 y1
= last_y
+ (last_y
- last_cubic_y
);
1280 if (*command
== 's') {
1288 p
[i
++] = last_cubic_x
= x2
;
1289 p
[i
++] = last_cubic_y
= y2
;
1290 p
[i
++] = last_quad_x
= last_x
= x
;
1291 p
[i
++] = last_quad_y
= last_y
= y
;
1293 } while (sscanf(s
, "%f %f %f %f %n",
1294 &x2
, &y2
, &x
, &y
, &n
) == 4);
1296 /* quadratic Bezier curveto (Q, q) (4 arguments) */
1297 } else if (sscanf(s
, " %1[Qq] %f %f %f %f %n", command
,
1298 &x1
, &y1
, &x
, &y
, &n
) == 5) {
1299 /*LOG(("quadratic Bezier curveto"));*/
1301 ALLOC_PATH_ELEMENTS(7);
1303 p
[i
++] = svgtiny_PATH_BEZIER
;
1306 if (*command
== 'q') {
1312 p
[i
++] = 1./3 * last_x
+ 2./3 * x1
;
1313 p
[i
++] = 1./3 * last_y
+ 2./3 * y1
;
1314 p
[i
++] = 2./3 * x1
+ 1./3 * x
;
1315 p
[i
++] = 2./3 * y1
+ 1./3 * y
;
1316 p
[i
++] = last_cubic_x
= last_x
= x
;
1317 p
[i
++] = last_cubic_y
= last_y
= y
;
1319 } while (sscanf(s
, "%f %f %f %f %n",
1320 &x1
, &y1
, &x
, &y
, &n
) == 4);
1322 /* shorthand/smooth quadratic Bezier curveto (T, t)
1324 } else if (sscanf(s
, " %1[Tt] %f %f %n", command
,
1326 /*LOG(("shorthand/smooth quadratic Bezier curveto"));*/
1328 ALLOC_PATH_ELEMENTS(7);
1330 p
[i
++] = svgtiny_PATH_BEZIER
;
1331 x1
= last_x
+ (last_x
- last_quad_x
);
1332 y1
= last_y
+ (last_y
- last_quad_y
);
1335 if (*command
== 't') {
1341 p
[i
++] = 1./3 * last_x
+ 2./3 * x1
;
1342 p
[i
++] = 1./3 * last_y
+ 2./3 * y1
;
1343 p
[i
++] = 2./3 * x1
+ 1./3 * x
;
1344 p
[i
++] = 2./3 * y1
+ 1./3 * y
;
1345 p
[i
++] = last_cubic_x
= last_x
= x
;
1346 p
[i
++] = last_cubic_y
= last_y
= y
;
1348 } while (sscanf(s
, "%f %f %n",
1351 /* elliptical arc (A, a) (7 arguments) */
1352 } else if (sscanf(s
, " %1[Aa] %f %f %f %f %f %f %f %n", command
,
1353 &rx
, &ry
, &rotation
, &large_arc
, &sweep
,
1357 double bzpoints
[6*4]; /* allow for up to four bezier segments per arc */
1359 if (*command
== 'a') {
1364 bzsegments
= svgarc_to_bezier(last_x
, last_y
,
1371 if (bzsegments
== -1) {
1373 ALLOC_PATH_ELEMENTS(3);
1374 p
[i
++] = svgtiny_PATH_LINE
;
1377 } else if (bzsegments
> 0) {
1379 ALLOC_PATH_ELEMENTS((unsigned int)bzsegments
* 7);
1380 for (bzpnt
= 0;bzpnt
< (bzsegments
* 6); bzpnt
+=6) {
1381 p
[i
++] = svgtiny_PATH_BEZIER
;
1382 p
[i
++] = bzpoints
[bzpnt
];
1383 p
[i
++] = bzpoints
[bzpnt
+1];
1384 p
[i
++] = bzpoints
[bzpnt
+2];
1385 p
[i
++] = bzpoints
[bzpnt
+3];
1386 p
[i
++] = bzpoints
[bzpnt
+4];
1387 p
[i
++] = bzpoints
[bzpnt
+5];
1390 if (bzsegments
!= 0) {
1391 last_cubic_x
= last_quad_x
= last_x
= p
[i
-2];
1392 last_cubic_y
= last_quad_y
= last_y
= p
[i
-1];
1397 } while (sscanf(s
, "%f %f %f %f %f %f %f %n",
1398 &rx
, &ry
, &rotation
, &large_arc
, &sweep
,
1402 /* fprintf(stderr, "parse failed at \"%s\"\n", s); */
1410 /* no real segments in path */
1412 svgtiny_cleanup_state_local(&state
);
1416 /* resize path element array to not be over allocated */
1420 /* try the resize, if it fails just continue to use the old
1423 tp
= realloc(p
, sizeof p
[0] * i
);
1429 err
= svgtiny_add_path(p
, i
, &state
);
1431 svgtiny_cleanup_state_local(&state
);
1438 * Parse a <rect> element node.
1440 * http://www.w3.org/TR/SVG11/shapes#RectElement
1443 svgtiny_code
svgtiny_parse_rect(dom_element
*rect
,
1444 struct svgtiny_parse_state state
)
1447 float x
, y
, width
, height
;
1450 svgtiny_setup_state_local(&state
);
1452 svgtiny_parse_position_attributes(rect
, state
,
1453 &x
, &y
, &width
, &height
);
1454 svgtiny_parse_paint_attributes(rect
, &state
);
1455 svgtiny_parse_transform_attributes(rect
, &state
);
1457 p
= malloc(13 * sizeof p
[0]);
1459 svgtiny_cleanup_state_local(&state
);
1460 return svgtiny_OUT_OF_MEMORY
;
1463 p
[0] = svgtiny_PATH_MOVE
;
1466 p
[3] = svgtiny_PATH_LINE
;
1469 p
[6] = svgtiny_PATH_LINE
;
1472 p
[9] = svgtiny_PATH_LINE
;
1475 p
[12] = svgtiny_PATH_CLOSE
;
1477 err
= svgtiny_add_path(p
, 13, &state
);
1479 svgtiny_cleanup_state_local(&state
);
1486 * Parse a <circle> element node.
1489 svgtiny_code
svgtiny_parse_circle(dom_element
*circle
,
1490 struct svgtiny_parse_state state
)
1493 float x
= 0, y
= 0, r
= -1;
1498 svgtiny_setup_state_local(&state
);
1500 exc
= dom_element_get_attribute(circle
, state
.interned_cx
, &attr
);
1501 if (exc
!= DOM_NO_ERR
) {
1502 svgtiny_cleanup_state_local(&state
);
1503 return svgtiny_LIBDOM_ERROR
;
1506 x
= svgtiny_parse_length(attr
, state
.viewport_width
, state
);
1508 dom_string_unref(attr
);
1510 exc
= dom_element_get_attribute(circle
, state
.interned_cy
, &attr
);
1511 if (exc
!= DOM_NO_ERR
) {
1512 svgtiny_cleanup_state_local(&state
);
1513 return svgtiny_LIBDOM_ERROR
;
1516 y
= svgtiny_parse_length(attr
, state
.viewport_height
, state
);
1518 dom_string_unref(attr
);
1520 exc
= dom_element_get_attribute(circle
, state
.interned_r
, &attr
);
1521 if (exc
!= DOM_NO_ERR
) {
1522 svgtiny_cleanup_state_local(&state
);
1523 return svgtiny_LIBDOM_ERROR
;
1526 r
= svgtiny_parse_length(attr
, state
.viewport_width
, state
);
1528 dom_string_unref(attr
);
1530 svgtiny_parse_paint_attributes(circle
, &state
);
1531 svgtiny_parse_transform_attributes(circle
, &state
);
1534 state
.diagram
->error_line
= -1; /* circle->line; */
1535 state
.diagram
->error_message
= "circle: r missing or negative";
1536 svgtiny_cleanup_state_local(&state
);
1537 return svgtiny_SVG_ERROR
;
1540 svgtiny_cleanup_state_local(&state
);
1544 p
= malloc(32 * sizeof p
[0]);
1546 svgtiny_cleanup_state_local(&state
);
1547 return svgtiny_OUT_OF_MEMORY
;
1550 p
[0] = svgtiny_PATH_MOVE
;
1553 p
[3] = svgtiny_PATH_BEZIER
;
1555 p
[5] = y
+ r
* KAPPA
;
1556 p
[6] = x
+ r
* KAPPA
;
1560 p
[10] = svgtiny_PATH_BEZIER
;
1561 p
[11] = x
- r
* KAPPA
;
1564 p
[14] = y
+ r
* KAPPA
;
1567 p
[17] = svgtiny_PATH_BEZIER
;
1569 p
[19] = y
- r
* KAPPA
;
1570 p
[20] = x
- r
* KAPPA
;
1574 p
[24] = svgtiny_PATH_BEZIER
;
1575 p
[25] = x
+ r
* KAPPA
;
1578 p
[28] = y
- r
* KAPPA
;
1581 p
[31] = svgtiny_PATH_CLOSE
;
1583 err
= svgtiny_add_path(p
, 32, &state
);
1585 svgtiny_cleanup_state_local(&state
);
1592 * Parse an <ellipse> element node.
1595 svgtiny_code
svgtiny_parse_ellipse(dom_element
*ellipse
,
1596 struct svgtiny_parse_state state
)
1599 float x
= 0, y
= 0, rx
= -1, ry
= -1;
1604 svgtiny_setup_state_local(&state
);
1606 exc
= dom_element_get_attribute(ellipse
, state
.interned_cx
, &attr
);
1607 if (exc
!= DOM_NO_ERR
) {
1608 svgtiny_cleanup_state_local(&state
);
1609 return svgtiny_LIBDOM_ERROR
;
1612 x
= svgtiny_parse_length(attr
, state
.viewport_width
, state
);
1614 dom_string_unref(attr
);
1616 exc
= dom_element_get_attribute(ellipse
, state
.interned_cy
, &attr
);
1617 if (exc
!= DOM_NO_ERR
) {
1618 svgtiny_cleanup_state_local(&state
);
1619 return svgtiny_LIBDOM_ERROR
;
1622 y
= svgtiny_parse_length(attr
, state
.viewport_height
, state
);
1624 dom_string_unref(attr
);
1626 exc
= dom_element_get_attribute(ellipse
, state
.interned_rx
, &attr
);
1627 if (exc
!= DOM_NO_ERR
) {
1628 svgtiny_cleanup_state_local(&state
);
1629 return svgtiny_LIBDOM_ERROR
;
1632 rx
= svgtiny_parse_length(attr
, state
.viewport_width
, state
);
1634 dom_string_unref(attr
);
1636 exc
= dom_element_get_attribute(ellipse
, state
.interned_ry
, &attr
);
1637 if (exc
!= DOM_NO_ERR
) {
1638 svgtiny_cleanup_state_local(&state
);
1639 return svgtiny_LIBDOM_ERROR
;
1642 ry
= svgtiny_parse_length(attr
, state
.viewport_width
, state
);
1644 dom_string_unref(attr
);
1646 svgtiny_parse_paint_attributes(ellipse
, &state
);
1647 svgtiny_parse_transform_attributes(ellipse
, &state
);
1649 if (rx
< 0 || ry
< 0) {
1650 state
.diagram
->error_line
= -1; /* ellipse->line; */
1651 state
.diagram
->error_message
= "ellipse: rx or ry missing "
1653 svgtiny_cleanup_state_local(&state
);
1654 return svgtiny_SVG_ERROR
;
1656 if (rx
== 0 || ry
== 0) {
1657 svgtiny_cleanup_state_local(&state
);
1661 p
= malloc(32 * sizeof p
[0]);
1663 svgtiny_cleanup_state_local(&state
);
1664 return svgtiny_OUT_OF_MEMORY
;
1667 p
[0] = svgtiny_PATH_MOVE
;
1670 p
[3] = svgtiny_PATH_BEZIER
;
1672 p
[5] = y
+ ry
* KAPPA
;
1673 p
[6] = x
+ rx
* KAPPA
;
1677 p
[10] = svgtiny_PATH_BEZIER
;
1678 p
[11] = x
- rx
* KAPPA
;
1681 p
[14] = y
+ ry
* KAPPA
;
1684 p
[17] = svgtiny_PATH_BEZIER
;
1686 p
[19] = y
- ry
* KAPPA
;
1687 p
[20] = x
- rx
* KAPPA
;
1691 p
[24] = svgtiny_PATH_BEZIER
;
1692 p
[25] = x
+ rx
* KAPPA
;
1695 p
[28] = y
- ry
* KAPPA
;
1698 p
[31] = svgtiny_PATH_CLOSE
;
1700 err
= svgtiny_add_path(p
, 32, &state
);
1702 svgtiny_cleanup_state_local(&state
);
1709 * Parse a <line> element node.
1712 svgtiny_code
svgtiny_parse_line(dom_element
*line
,
1713 struct svgtiny_parse_state state
)
1716 float x1
= 0, y1
= 0, x2
= 0, y2
= 0;
1721 svgtiny_setup_state_local(&state
);
1723 exc
= dom_element_get_attribute(line
, state
.interned_x1
, &attr
);
1724 if (exc
!= DOM_NO_ERR
) {
1725 svgtiny_cleanup_state_local(&state
);
1726 return svgtiny_LIBDOM_ERROR
;
1729 x1
= svgtiny_parse_length(attr
, state
.viewport_width
, state
);
1731 dom_string_unref(attr
);
1733 exc
= dom_element_get_attribute(line
, state
.interned_y1
, &attr
);
1734 if (exc
!= DOM_NO_ERR
) {
1735 svgtiny_cleanup_state_local(&state
);
1736 return svgtiny_LIBDOM_ERROR
;
1739 y1
= svgtiny_parse_length(attr
, state
.viewport_height
, state
);
1741 dom_string_unref(attr
);
1743 exc
= dom_element_get_attribute(line
, state
.interned_x2
, &attr
);
1744 if (exc
!= DOM_NO_ERR
) {
1745 svgtiny_cleanup_state_local(&state
);
1746 return svgtiny_LIBDOM_ERROR
;
1749 x2
= svgtiny_parse_length(attr
, state
.viewport_width
, state
);
1751 dom_string_unref(attr
);
1753 exc
= dom_element_get_attribute(line
, state
.interned_y2
, &attr
);
1754 if (exc
!= DOM_NO_ERR
) {
1755 svgtiny_cleanup_state_local(&state
);
1756 return svgtiny_LIBDOM_ERROR
;
1759 y2
= svgtiny_parse_length(attr
, state
.viewport_height
, state
);
1761 dom_string_unref(attr
);
1763 svgtiny_parse_paint_attributes(line
, &state
);
1764 svgtiny_parse_transform_attributes(line
, &state
);
1766 p
= malloc(7 * sizeof p
[0]);
1768 svgtiny_cleanup_state_local(&state
);
1769 return svgtiny_OUT_OF_MEMORY
;
1772 p
[0] = svgtiny_PATH_MOVE
;
1775 p
[3] = svgtiny_PATH_LINE
;
1778 p
[6] = svgtiny_PATH_CLOSE
;
1780 err
= svgtiny_add_path(p
, 7, &state
);
1782 svgtiny_cleanup_state_local(&state
);
1789 * Parse a <polyline> or <polygon> element node.
1791 * http://www.w3.org/TR/SVG11/shapes#PolylineElement
1792 * http://www.w3.org/TR/SVG11/shapes#PolygonElement
1795 svgtiny_code
svgtiny_parse_poly(dom_element
*poly
,
1796 struct svgtiny_parse_state state
, bool polygon
)
1799 dom_string
*points_str
;
1805 svgtiny_setup_state_local(&state
);
1807 svgtiny_parse_paint_attributes(poly
, &state
);
1808 svgtiny_parse_transform_attributes(poly
, &state
);
1810 exc
= dom_element_get_attribute(poly
, state
.interned_points
,
1812 if (exc
!= DOM_NO_ERR
) {
1813 svgtiny_cleanup_state_local(&state
);
1814 return svgtiny_LIBDOM_ERROR
;
1817 if (points_str
== NULL
) {
1818 state
.diagram
->error_line
= -1; /* poly->line; */
1819 state
.diagram
->error_message
=
1820 "polyline/polygon: missing points attribute";
1821 svgtiny_cleanup_state_local(&state
);
1822 return svgtiny_SVG_ERROR
;
1825 s
= points
= strndup(dom_string_data(points_str
),
1826 dom_string_byte_length(points_str
));
1827 dom_string_unref(points_str
);
1828 /* read points attribute */
1830 svgtiny_cleanup_state_local(&state
);
1831 return svgtiny_OUT_OF_MEMORY
;
1833 /* allocate space for path: it will never have more elements than s */
1834 p
= malloc(sizeof p
[0] * strlen(s
));
1837 svgtiny_cleanup_state_local(&state
);
1838 return svgtiny_OUT_OF_MEMORY
;
1841 /* parse s and build path */
1842 for (i
= 0; s
[i
]; i
++)
1850 if (sscanf(s
, "%f %f %n", &x
, &y
, &n
) == 2) {
1852 p
[i
++] = svgtiny_PATH_MOVE
;
1854 p
[i
++] = svgtiny_PATH_LINE
;
1863 p
[i
++] = svgtiny_PATH_CLOSE
;
1867 err
= svgtiny_add_path(p
, i
, &state
);
1869 svgtiny_cleanup_state_local(&state
);
1876 * Parse a <text> or <tspan> element node.
1879 svgtiny_code
svgtiny_parse_text(dom_element
*text
,
1880 struct svgtiny_parse_state state
)
1882 float x
, y
, width
, height
;
1887 svgtiny_setup_state_local(&state
);
1889 svgtiny_parse_position_attributes(text
, state
,
1890 &x
, &y
, &width
, &height
);
1891 svgtiny_parse_font_attributes(text
, &state
);
1892 svgtiny_parse_transform_attributes(text
, &state
);
1894 px
= state
.ctm
.a
* x
+ state
.ctm
.c
* y
+ state
.ctm
.e
;
1895 py
= state
.ctm
.b
* x
+ state
.ctm
.d
* y
+ state
.ctm
.f
;
1896 /* state.ctm.e = px - state.origin_x; */
1897 /* state.ctm.f = py - state.origin_y; */
1899 exc
= dom_node_get_first_child(text
, &child
);
1900 if (exc
!= DOM_NO_ERR
) {
1901 return svgtiny_LIBDOM_ERROR
;
1902 svgtiny_cleanup_state_local(&state
);
1904 while (child
!= NULL
) {
1906 dom_node_type nodetype
;
1907 svgtiny_code code
= svgtiny_OK
;
1909 exc
= dom_node_get_node_type(child
, &nodetype
);
1910 if (exc
!= DOM_NO_ERR
) {
1911 dom_node_unref(child
);
1912 svgtiny_cleanup_state_local(&state
);
1913 return svgtiny_LIBDOM_ERROR
;
1915 if (nodetype
== DOM_ELEMENT_NODE
) {
1916 dom_string
*nodename
;
1917 exc
= dom_node_get_node_name(child
, &nodename
);
1918 if (exc
!= DOM_NO_ERR
) {
1919 dom_node_unref(child
);
1920 svgtiny_cleanup_state_local(&state
);
1921 return svgtiny_LIBDOM_ERROR
;
1923 if (dom_string_caseless_isequal(nodename
,
1924 state
.interned_tspan
))
1925 code
= svgtiny_parse_text((dom_element
*)child
,
1927 dom_string_unref(nodename
);
1928 } else if (nodetype
== DOM_TEXT_NODE
) {
1929 struct svgtiny_shape
*shape
= svgtiny_add_shape(&state
);
1930 dom_string
*content
;
1931 if (shape
== NULL
) {
1932 dom_node_unref(child
);
1933 svgtiny_cleanup_state_local(&state
);
1934 return svgtiny_OUT_OF_MEMORY
;
1936 exc
= dom_text_get_whole_text(child
, &content
);
1937 if (exc
!= DOM_NO_ERR
) {
1938 dom_node_unref(child
);
1939 svgtiny_cleanup_state_local(&state
);
1940 return svgtiny_LIBDOM_ERROR
;
1942 if (content
!= NULL
) {
1943 shape
->text
= strndup(dom_string_data(content
),
1944 dom_string_byte_length(content
));
1945 dom_string_unref(content
);
1947 shape
->text
= strdup("");
1951 state
.diagram
->shape_count
++;
1954 if (code
!= svgtiny_OK
) {
1955 dom_node_unref(child
);
1956 svgtiny_cleanup_state_local(&state
);
1959 exc
= dom_node_get_next_sibling(child
, &next
);
1960 dom_node_unref(child
);
1961 if (exc
!= DOM_NO_ERR
) {
1962 svgtiny_cleanup_state_local(&state
);
1963 return svgtiny_LIBDOM_ERROR
;
1968 svgtiny_cleanup_state_local(&state
);
1975 * Parse x, y, width, and height attributes, if present.
1978 void svgtiny_parse_position_attributes(dom_element
*node
,
1979 const struct svgtiny_parse_state state
,
1980 float *x
, float *y
, float *width
, float *height
)
1987 *width
= state
.viewport_width
;
1988 *height
= state
.viewport_height
;
1990 exc
= dom_element_get_attribute(node
, state
.interned_x
, &attr
);
1991 if (exc
== DOM_NO_ERR
&& attr
!= NULL
) {
1992 *x
= svgtiny_parse_length(attr
, state
.viewport_width
, state
);
1993 dom_string_unref(attr
);
1996 exc
= dom_element_get_attribute(node
, state
.interned_y
, &attr
);
1997 if (exc
== DOM_NO_ERR
&& attr
!= NULL
) {
1998 *y
= svgtiny_parse_length(attr
, state
.viewport_height
, state
);
1999 dom_string_unref(attr
);
2002 exc
= dom_element_get_attribute(node
, state
.interned_width
, &attr
);
2003 if (exc
== DOM_NO_ERR
&& attr
!= NULL
) {
2004 *width
= svgtiny_parse_length(attr
, state
.viewport_width
,
2006 dom_string_unref(attr
);
2009 exc
= dom_element_get_attribute(node
, state
.interned_height
, &attr
);
2010 if (exc
== DOM_NO_ERR
&& attr
!= NULL
) {
2011 *height
= svgtiny_parse_length(attr
, state
.viewport_height
,
2013 dom_string_unref(attr
);
2019 * Parse a length as a number of pixels.
2022 static float _svgtiny_parse_length(const char *s
, int viewport_size
,
2023 const struct svgtiny_parse_state state
)
2025 int num_length
= strspn(s
, "0123456789+-.");
2026 const char *unit
= s
+ num_length
;
2027 float n
= atof((const char *) s
);
2028 float font_size
= 20;
2034 } else if (unit
[0] == '%') {
2035 return n
/ 100.0 * viewport_size
;
2036 } else if (unit
[0] == 'e' && unit
[1] == 'm') {
2037 return n
* font_size
;
2038 } else if (unit
[0] == 'e' && unit
[1] == 'x') {
2039 return n
/ 2.0 * font_size
;
2040 } else if (unit
[0] == 'p' && unit
[1] == 'x') {
2042 } else if (unit
[0] == 'p' && unit
[1] == 't') {
2044 } else if (unit
[0] == 'p' && unit
[1] == 'c') {
2046 } else if (unit
[0] == 'm' && unit
[1] == 'm') {
2047 return n
* 3.543307;
2048 } else if (unit
[0] == 'c' && unit
[1] == 'm') {
2049 return n
* 35.43307;
2050 } else if (unit
[0] == 'i' && unit
[1] == 'n') {
2057 float svgtiny_parse_length(dom_string
*s
, int viewport_size
,
2058 const struct svgtiny_parse_state state
)
2060 char *ss
= strndup(dom_string_data(s
), dom_string_byte_length(s
));
2061 float ret
= _svgtiny_parse_length(ss
, viewport_size
, state
);
2067 * Parse paint attributes, if present.
2070 void svgtiny_parse_paint_attributes(dom_element
*node
,
2071 struct svgtiny_parse_state
*state
)
2076 /* We store the result of svgtiny_parse_style_inline() in
2077 * inline_sheet, and that function returns NULL on error; in
2078 * particular you do not need to css_stylesheet_destroy() the
2079 * result if it is NULL. We initialize inline_sheet to NULL to
2080 * retain the same semantics. */
2081 css_stylesheet
*inline_sheet
= NULL
;
2083 exc
= dom_element_get_attribute(node
, state
->interned_fill
, &attr
);
2084 if (exc
== DOM_NO_ERR
&& attr
!= NULL
) {
2085 svgtiny_parse_color(attr
, &state
->fill
, &state
->fill_grad
, state
);
2086 dom_string_unref(attr
);
2089 exc
= dom_element_get_attribute(node
, state
->interned_stroke
, &attr
);
2090 if (exc
== DOM_NO_ERR
&& attr
!= NULL
) {
2091 svgtiny_parse_color(attr
, &state
->stroke
, &state
->stroke_grad
, state
);
2092 dom_string_unref(attr
);
2095 exc
= dom_element_get_attribute(node
, state
->interned_stroke_width
, &attr
);
2096 if (exc
== DOM_NO_ERR
&& attr
!= NULL
) {
2097 state
->stroke_width
= svgtiny_parse_length(attr
,
2098 state
->viewport_width
, *state
);
2099 dom_string_unref(attr
);
2102 exc
= dom_element_get_attribute(node
, state
->interned_style
, &attr
);
2103 if (exc
== DOM_NO_ERR
&& attr
!= NULL
) {
2104 /* First parse the style attribute into a libcss stylesheet
2105 in case any of its properties are known to libcss. */
2106 inline_sheet
= svgtiny_parse_style_inline(
2107 (uint8_t *)dom_string_data(attr
),
2108 dom_string_byte_length(attr
));
2110 /* Parse any other properties "by hand" until they can
2111 be supported in libcss. */
2112 char *style
= strndup(dom_string_data(attr
),
2113 dom_string_byte_length(attr
));
2116 if ((s
= strstr(style
, "fill:"))) {
2120 value
= strndup(s
, strcspn(s
, "; "));
2121 _svgtiny_parse_color(value
, &state
->fill
, &state
->fill_grad
, state
);
2124 if ((s
= strstr(style
, "stroke:"))) {
2128 value
= strndup(s
, strcspn(s
, "; "));
2129 _svgtiny_parse_color(value
, &state
->stroke
, &state
->stroke_grad
, state
);
2132 if ((s
= strstr(style
, "stroke-width:"))) {
2136 value
= strndup(s
, strcspn(s
, "; "));
2137 state
->stroke_width
= _svgtiny_parse_length(value
,
2138 state
->viewport_width
, *state
);
2142 dom_string_unref(attr
);
2145 if (inline_sheet
!= NULL
) {
2146 css_stylesheet_destroy(inline_sheet
);
2155 static void _svgtiny_parse_color(const char *s
, svgtiny_colour
*c
,
2156 struct svgtiny_parse_state_gradient
*grad
,
2157 struct svgtiny_parse_state
*state
)
2159 unsigned int r
, g
, b
;
2161 size_t len
= strlen(s
);
2162 char *id
= 0, *rparen
;
2164 if (len
== 4 && s
[0] == '#') {
2165 if (sscanf(s
+ 1, "%1x%1x%1x", &r
, &g
, &b
) == 3)
2166 *c
= svgtiny_RGB(r
| r
<< 4, g
| g
<< 4, b
| b
<< 4);
2168 } else if (len
== 7 && s
[0] == '#') {
2169 if (sscanf(s
+ 1, "%2x%2x%2x", &r
, &g
, &b
) == 3)
2170 *c
= svgtiny_RGB(r
, g
, b
);
2172 } else if (10 <= len
&& s
[0] == 'r' && s
[1] == 'g' && s
[2] == 'b' &&
2173 s
[3] == '(' && s
[len
- 1] == ')') {
2174 if (sscanf(s
+ 4, "%u,%u,%u", &r
, &g
, &b
) == 3)
2175 *c
= svgtiny_RGB(r
, g
, b
);
2176 else if (sscanf(s
+ 4, "%f%%,%f%%,%f%%", &rf
, &gf
, &bf
) == 3) {
2180 *c
= svgtiny_RGB(r
, g
, b
);
2183 } else if (len
== 4 && strcmp(s
, "none") == 0) {
2184 *c
= svgtiny_TRANSPARENT
;
2186 } else if (5 < len
&& s
[0] == 'u' && s
[1] == 'r' && s
[2] == 'l' &&
2189 *c
= svgtiny_RGB(0, 0, 0);
2190 } else if (s
[4] == '#') {
2194 rparen
= strchr(id
, ')');
2197 svgtiny_find_gradient(id
, grad
, state
);
2199 if (grad
->linear_gradient_stop_count
== 0)
2200 *c
= svgtiny_TRANSPARENT
;
2201 else if (grad
->linear_gradient_stop_count
== 1)
2202 *c
= grad
->gradient_stop
[0].color
;
2204 *c
= svgtiny_LINEAR_GRADIENT
;
2208 const struct svgtiny_named_color
*named_color
;
2209 named_color
= svgtiny_color_lookup(s
, strlen(s
));
2211 *c
= named_color
->color
;
2215 void svgtiny_parse_color(dom_string
*s
, svgtiny_colour
*c
,
2216 struct svgtiny_parse_state_gradient
*grad
,
2217 struct svgtiny_parse_state
*state
)
2220 _svgtiny_parse_color(dom_string_data(s
), c
, grad
, state
);
2221 dom_string_unref(s
);
2225 * Parse font attributes, if present.
2228 void svgtiny_parse_font_attributes(dom_element
*node
,
2229 struct svgtiny_parse_state
*state
)
2231 /* TODO: Implement this, it never used to be */
2234 #ifdef WRITTEN_THIS_PROPERLY
2235 const xmlAttr
*attr
;
2239 for (attr
= node
->properties
; attr
; attr
= attr
->next
) {
2240 if (strcmp((const char *) attr
->name
, "font-size") == 0) {
2249 * Parse transform attributes, if present.
2251 * http://www.w3.org/TR/SVG11/coords#TransformAttribute
2254 void svgtiny_parse_transform_attributes(dom_element
*node
,
2255 struct svgtiny_parse_state
*state
)
2261 exc
= dom_element_get_attribute(node
, state
->interned_transform
,
2263 if (exc
== DOM_NO_ERR
&& attr
!= NULL
) {
2264 transform
= strndup(dom_string_data(attr
),
2265 dom_string_byte_length(attr
));
2266 svgtiny_parse_transform(transform
, &state
->ctm
.a
, &state
->ctm
.b
,
2267 &state
->ctm
.c
, &state
->ctm
.d
,
2268 &state
->ctm
.e
, &state
->ctm
.f
);
2270 dom_string_unref(attr
);
2276 * Parse a transform string.
2279 void svgtiny_parse_transform(char *s
, float *ma
, float *mb
,
2280 float *mc
, float *md
, float *me
, float *mf
)
2282 float a
, b
, c
, d
, e
, f
;
2283 float za
, zb
, zc
, zd
, ze
, zf
;
2288 for (i
= 0; s
[i
]; i
++)
2297 if ((sscanf(s
, " matrix (%f %f %f %f %f %f ) %n",
2298 &a
, &b
, &c
, &d
, &e
, &f
, &n
) == 6) && (n
> 0))
2300 else if ((sscanf(s
, " translate (%f %f ) %n",
2301 &e
, &f
, &n
) == 2) && (n
> 0))
2303 else if ((sscanf(s
, " translate (%f ) %n",
2304 &e
, &n
) == 1) && (n
> 0))
2306 else if ((sscanf(s
, " scale (%f %f ) %n",
2307 &a
, &d
, &n
) == 2) && (n
> 0))
2309 else if ((sscanf(s
, " scale (%f ) %n",
2310 &a
, &n
) == 1) && (n
> 0))
2312 else if ((sscanf(s
, " rotate (%f %f %f ) %n",
2313 &angle
, &x
, &y
, &n
) == 3) && (n
> 0)) {
2314 angle
= angle
/ 180 * M_PI
;
2319 e
= -x
* cos(angle
) + y
* sin(angle
) + x
;
2320 f
= -x
* sin(angle
) - y
* cos(angle
) + y
;
2321 } else if ((sscanf(s
, " rotate (%f ) %n",
2322 &angle
, &n
) == 1) && (n
> 0)) {
2323 angle
= angle
/ 180 * M_PI
;
2328 } else if ((sscanf(s
, " skewX (%f ) %n",
2329 &angle
, &n
) == 1) && (n
> 0)) {
2330 angle
= angle
/ 180 * M_PI
;
2332 } else if ((sscanf(s
, " skewY (%f ) %n",
2333 &angle
, &n
) == 1) && (n
> 0)) {
2334 angle
= angle
/ 180 * M_PI
;
2338 za
= *ma
* a
+ *mc
* b
;
2339 zb
= *mb
* a
+ *md
* b
;
2340 zc
= *ma
* c
+ *mc
* d
;
2341 zd
= *mb
* c
+ *md
* d
;
2342 ze
= *ma
* e
+ *mc
* f
+ *me
;
2343 zf
= *mb
* e
+ *md
* f
+ *mf
;
2356 * Add a path to the svgtiny_diagram.
2359 svgtiny_code
svgtiny_add_path(float *p
, unsigned int n
,
2360 struct svgtiny_parse_state
*state
)
2362 struct svgtiny_shape
*shape
;
2364 if (state
->fill
== svgtiny_LINEAR_GRADIENT
)
2365 return svgtiny_add_path_linear_gradient(p
, n
, state
);
2367 svgtiny_transform_path(p
, n
, state
);
2369 shape
= svgtiny_add_shape(state
);
2372 return svgtiny_OUT_OF_MEMORY
;
2375 shape
->path_length
= n
;
2376 state
->diagram
->shape_count
++;
2383 * Add a svgtiny_shape to the svgtiny_diagram.
2386 struct svgtiny_shape
*svgtiny_add_shape(struct svgtiny_parse_state
*state
)
2388 struct svgtiny_shape
*shape
= realloc(state
->diagram
->shape
,
2389 (state
->diagram
->shape_count
+ 1) *
2390 sizeof (state
->diagram
->shape
[0]));
2393 state
->diagram
->shape
= shape
;
2395 shape
+= state
->diagram
->shape_count
;
2397 shape
->path_length
= 0;
2399 shape
->fill
= state
->fill
;
2400 shape
->stroke
= state
->stroke
;
2401 shape
->stroke_width
= lroundf((float) state
->stroke_width
*
2402 (state
->ctm
.a
+ state
->ctm
.d
) / 2.0);
2403 if (0 < state
->stroke_width
&& shape
->stroke_width
== 0)
2404 shape
->stroke_width
= 1;
2411 * Apply the current transformation matrix to a path.
2414 void svgtiny_transform_path(float *p
, unsigned int n
,
2415 struct svgtiny_parse_state
*state
)
2419 for (j
= 0; j
!= n
; ) {
2420 unsigned int points
= 0;
2422 switch ((int) p
[j
]) {
2423 case svgtiny_PATH_MOVE
:
2424 case svgtiny_PATH_LINE
:
2427 case svgtiny_PATH_CLOSE
:
2430 case svgtiny_PATH_BEZIER
:
2437 for (k
= 0; k
!= points
; k
++) {
2438 float x0
= p
[j
], y0
= p
[j
+ 1];
2439 float x
= state
->ctm
.a
* x0
+ state
->ctm
.c
* y0
+
2441 float y
= state
->ctm
.b
* x0
+ state
->ctm
.d
* y0
+
2452 * Free all memory used by a diagram.
2455 void svgtiny_free(struct svgtiny_diagram
*svg
)
2460 for (i
= 0; i
!= svg
->shape_count
; i
++) {
2461 free(svg
->shape
[i
].path
);
2462 free(svg
->shape
[i
].text
);
2470 #ifndef HAVE_STRNDUP
2471 char *svgtiny_strndup(const char *s
, size_t n
)
2476 for (len
= 0; len
!= n
&& s
[len
]; len
++)
2479 s2
= malloc(len
+ 1);