2 * This file is part of Libsvgtiny
3 * Licensed under the MIT License,
4 * http://opensource.org/licenses/mit-license.php
5 * Copyright 2008 James Bursa <james@semichrome.net>
8 #define _GNU_SOURCE /* for strndup */
13 #include "svgtiny_internal.h"
17 static svgtiny_code
svgtiny_parse_linear_gradient(xmlNode
*linear
,
18 struct svgtiny_parse_state
*state
);
19 static float svgtiny_parse_gradient_offset(const char *s
);
20 static void svgtiny_path_bbox(float *p
, unsigned int n
,
21 float *x0
, float *y0
, float *x1
, float *y1
);
22 static void svgtiny_invert_matrix(float *m
, float *inv
);
26 * Find a gradient by id and parse it.
29 void svgtiny_find_gradient(const char *id
, struct svgtiny_parse_state
*state
)
31 fprintf(stderr
, "svgtiny_find_gradient: id \"%s\"\n", id
);
33 state
->linear_gradient_stop_count
= 0;
34 state
->gradient_x1
= "0%";
35 state
->gradient_y1
= "0%";
36 state
->gradient_x2
= "100%";
37 state
->gradient_y2
= "0%";
38 state
->gradient_user_space_on_use
= false;
39 state
->gradient_transform
.a
= 1;
40 state
->gradient_transform
.b
= 0;
41 state
->gradient_transform
.c
= 0;
42 state
->gradient_transform
.d
= 1;
43 state
->gradient_transform
.e
= 0;
44 state
->gradient_transform
.f
= 0;
46 xmlNode
*gradient
= svgtiny_find_element_by_id(
47 (xmlNode
*) state
->document
, id
);
48 fprintf(stderr
, "gradient %p\n", (void *) gradient
);
50 fprintf(stderr
, "gradient \"%s\" not found\n", id
);
54 fprintf(stderr
, "gradient name \"%s\"\n", gradient
->name
);
55 if (strcmp((const char *) gradient
->name
, "linearGradient") == 0) {
56 svgtiny_parse_linear_gradient(gradient
, state
);
62 * Parse a <linearGradient> element node.
64 * http://www.w3.org/TR/SVG11/pservers#LinearGradients
67 svgtiny_code
svgtiny_parse_linear_gradient(xmlNode
*linear
,
68 struct svgtiny_parse_state
*state
)
70 xmlAttr
*href
= xmlHasProp(linear
, (const xmlChar
*) "href");
71 if (href
&& href
->children
->content
[0] == '#')
72 svgtiny_find_gradient((const char *) href
->children
->content
75 for (xmlAttr
*attr
= linear
->properties
; attr
; attr
= attr
->next
) {
76 const char *name
= (const char *) attr
->name
;
77 const char *content
= (const char *) attr
->children
->content
;
78 if (strcmp(name
, "x1") == 0)
79 state
->gradient_x1
= content
;
80 else if (strcmp(name
, "y1") == 0)
81 state
->gradient_y1
= content
;
82 else if (strcmp(name
, "x2") == 0)
83 state
->gradient_x2
= content
;
84 else if (strcmp(name
, "y2") == 0)
85 state
->gradient_y2
= content
;
86 else if (strcmp(name
, "gradientUnits") == 0)
87 state
->gradient_user_space_on_use
=
88 strcmp(content
, "userSpaceOnUse") == 0;
89 else if (strcmp(name
, "gradientTransform") == 0) {
90 float a
= 1, b
= 0, c
= 0, d
= 1, e
= 0, f
= 0;
91 char *s
= strdup(content
);
93 return svgtiny_OUT_OF_MEMORY
;
94 svgtiny_parse_transform(s
, &a
, &b
, &c
, &d
, &e
, &f
);
96 fprintf(stderr
, "transform %g %g %g %g %g %g\n",
98 state
->gradient_transform
.a
= a
;
99 state
->gradient_transform
.b
= b
;
100 state
->gradient_transform
.c
= c
;
101 state
->gradient_transform
.d
= d
;
102 state
->gradient_transform
.e
= e
;
103 state
->gradient_transform
.f
= f
;
108 for (xmlNode
*stop
= linear
->children
; stop
; stop
= stop
->next
) {
110 svgtiny_colour color
= svgtiny_TRANSPARENT
;
112 if (stop
->type
!= XML_ELEMENT_NODE
)
114 if (strcmp((const char *) stop
->name
, "stop") != 0)
117 for (xmlAttr
*attr
= stop
->properties
; attr
;
119 const char *name
= (const char *) attr
->name
;
120 const char *content
=
121 (const char *) attr
->children
->content
;
122 if (strcmp(name
, "offset") == 0)
123 offset
= svgtiny_parse_gradient_offset(content
);
124 else if (strcmp(name
, "stop-color") == 0)
125 svgtiny_parse_color(content
, &color
, state
);
126 else if (strcmp(name
, "style") == 0) {
129 if ((s
= strstr(content
, "stop-color:"))) {
133 value
= strndup(s
, strcspn(s
, "; "));
134 svgtiny_parse_color(value
, &color
,
141 if (offset
!= -1 && color
!= svgtiny_TRANSPARENT
) {
142 fprintf(stderr
, "stop %g %x\n", offset
, color
);
143 state
->gradient_stop
[i
].offset
= offset
;
144 state
->gradient_stop
[i
].color
= color
;
148 if (i
== svgtiny_MAX_STOPS
)
153 state
->linear_gradient_stop_count
= i
;
159 float svgtiny_parse_gradient_offset(const char *s
)
161 int num_length
= strspn(s
, "0123456789+-.");
162 const char *unit
= s
+ num_length
;
163 float n
= atof((const char *) s
);
167 else if (unit
[0] == '%')
181 * Add a path with a linear gradient fill to the svgtiny_diagram.
184 svgtiny_code
svgtiny_add_path_linear_gradient(float *p
, unsigned int n
,
185 struct svgtiny_parse_state
*state
)
187 /* determine object bounding box */
188 float object_x0
, object_y0
, object_x1
, object_y1
;
189 svgtiny_path_bbox(p
, n
, &object_x0
, &object_y0
, &object_x1
, &object_y1
);
190 #ifdef GRADIENT_DEBUG
191 fprintf(stderr
, "object bbox: (%g %g) (%g %g)\n",
192 object_x0
, object_y0
, object_x1
, object_y1
);
195 /* compute gradient vector */
196 fprintf(stderr
, "x1 %s, y1 %s, x2 %s, y2 %s\n",
197 state
->gradient_x1
, state
->gradient_y1
,
198 state
->gradient_x2
, state
->gradient_y2
);
199 float gradient_x0
, gradient_y0
, gradient_x1
, gradient_y1
,
200 gradient_dx
, gradient_dy
;
201 if (!state
->gradient_user_space_on_use
) {
202 gradient_x0
= object_x0
+
203 svgtiny_parse_length(state
->gradient_x1
,
204 object_x1
- object_x0
, *state
);
205 gradient_y0
= object_y0
+
206 svgtiny_parse_length(state
->gradient_y1
,
207 object_y1
- object_y0
, *state
);
208 gradient_x1
= object_x0
+
209 svgtiny_parse_length(state
->gradient_x2
,
210 object_x1
- object_x0
, *state
);
211 gradient_y1
= object_y0
+
212 svgtiny_parse_length(state
->gradient_y2
,
213 object_y1
- object_y0
, *state
);
215 gradient_x0
= svgtiny_parse_length(state
->gradient_x1
,
216 state
->viewport_width
, *state
);
217 gradient_y0
= svgtiny_parse_length(state
->gradient_y1
,
218 state
->viewport_height
, *state
);
219 gradient_x1
= svgtiny_parse_length(state
->gradient_x2
,
220 state
->viewport_width
, *state
);
221 gradient_y1
= svgtiny_parse_length(state
->gradient_y2
,
222 state
->viewport_height
, *state
);
224 gradient_dx
= gradient_x1
- gradient_x0
;
225 gradient_dy
= gradient_y1
- gradient_y0
;
226 #ifdef GRADIENT_DEBUG
227 fprintf(stderr
, "gradient vector: (%g %g) => (%g %g)\n",
228 gradient_x0
, gradient_y0
, gradient_x1
, gradient_y1
);
231 /* show theoretical gradient strips for debugging */
232 /*unsigned int strips = 10;
233 for (unsigned int z = 0; z != strips; z++) {
234 float f0, fd, strip_x0, strip_y0, strip_dx, strip_dy;
235 f0 = (float) z / (float) strips;
236 fd = (float) 1 / (float) strips;
237 strip_x0 = gradient_x0 + f0 * gradient_dx;
238 strip_y0 = gradient_y0 + f0 * gradient_dy;
239 strip_dx = fd * gradient_dx;
240 strip_dy = fd * gradient_dy;
241 fprintf(stderr, "strip %i vector: (%g %g) + (%g %g)\n",
242 z, strip_x0, strip_y0, strip_dx, strip_dy);
244 float *p = malloc(13 * sizeof p[0]);
246 return svgtiny_OUT_OF_MEMORY;
247 p[0] = svgtiny_PATH_MOVE;
248 p[1] = strip_x0 + (strip_dy * 3);
249 p[2] = strip_y0 - (strip_dx * 3);
250 p[3] = svgtiny_PATH_LINE;
251 p[4] = p[1] + strip_dx;
252 p[5] = p[2] + strip_dy;
253 p[6] = svgtiny_PATH_LINE;
254 p[7] = p[4] - (strip_dy * 6);
255 p[8] = p[5] + (strip_dx * 6);
256 p[9] = svgtiny_PATH_LINE;
257 p[10] = p[7] - strip_dx;
258 p[11] = p[8] - strip_dy;
259 p[12] = svgtiny_PATH_CLOSE;
260 svgtiny_transform_path(p, 13, state);
261 struct svgtiny_shape *shape = svgtiny_add_shape(state);
264 return svgtiny_OUT_OF_MEMORY;
267 shape->path_length = 13;
268 shape->fill = svgtiny_TRANSPARENT;
269 shape->stroke = svgtiny_RGB(0, 0xff, 0);
270 state->diagram->shape_count++;
273 /* invert gradient transform for applying to vertices */
275 svgtiny_invert_matrix(&state
->gradient_transform
.a
, trans
);
276 fprintf(stderr
, "inverse transform %g %g %g %g %g %g\n",
277 trans
[0], trans
[1], trans
[2], trans
[3],
280 /* compute points on the path for triangle vertices */
281 /* r, r0, r1 are distance along gradient vector */
282 unsigned int steps
= 10;
283 float x0
= 0, y0
= 0, x0_trans
, y0_trans
, r0
; /* segment start point */
284 float x1
, y1
, x1_trans
, y1_trans
, r1
; /* segment end point */
285 /* segment control points (beziers only) */
286 float c0x
= 0, c0y
= 0, c1x
= 0, c1y
= 0;
287 float gradient_norm_squared
= gradient_dx
* gradient_dx
+
288 gradient_dy
* gradient_dy
;
292 struct svgtiny_list
*pts
= svgtiny_list_create(
293 sizeof (struct grad_point
));
295 return svgtiny_OUT_OF_MEMORY
;
297 unsigned int min_pt
= 0;
298 for (unsigned int j
= 0; j
!= n
; ) {
299 int segment_type
= (int) p
[j
];
301 if (segment_type
== svgtiny_PATH_MOVE
) {
308 assert(segment_type
== svgtiny_PATH_CLOSE
||
309 segment_type
== svgtiny_PATH_LINE
||
310 segment_type
== svgtiny_PATH_BEZIER
);
312 /* start point (x0, y0) */
313 x0_trans
= trans
[0]*x0
+ trans
[2]*y0
+ trans
[4];
314 y0_trans
= trans
[1]*x0
+ trans
[3]*y0
+ trans
[5];
315 r0
= ((x0_trans
- gradient_x0
) * gradient_dx
+
316 (y0_trans
- gradient_y0
) * gradient_dy
) /
317 gradient_norm_squared
;
318 struct grad_point
*point
= svgtiny_list_push(pts
);
320 svgtiny_list_free(pts
);
321 return svgtiny_OUT_OF_MEMORY
;
328 min_pt
= svgtiny_list_size(pts
) - 1;
331 /* end point (x1, y1) */
332 if (segment_type
== svgtiny_PATH_LINE
) {
336 } else if (segment_type
== svgtiny_PATH_CLOSE
) {
340 } else /* svgtiny_PATH_BEZIER */ {
349 x1_trans
= trans
[0]*x1
+ trans
[2]*y1
+ trans
[4];
350 y1_trans
= trans
[1]*x1
+ trans
[3]*y1
+ trans
[5];
351 r1
= ((x1_trans
- gradient_x0
) * gradient_dx
+
352 (y1_trans
- gradient_y0
) * gradient_dy
) /
353 gradient_norm_squared
;
355 /* determine steps from change in r */
356 steps
= ceilf(fabsf(r1
- r0
) / 0.05);
359 fprintf(stderr
, "r0 %g, r1 %g, steps %i\n",
362 /* loop through intermediate points */
363 for (unsigned int z
= 1; z
!= steps
; z
++) {
364 float t
, x
, y
, x_trans
, y_trans
, r
;
365 t
= (float) z
/ (float) steps
;
366 if (segment_type
== svgtiny_PATH_BEZIER
) {
367 x
= (1-t
) * (1-t
) * (1-t
) * x0
+
368 3 * t
* (1-t
) * (1-t
) * c0x
+
369 3 * t
* t
* (1-t
) * c1x
+
371 y
= (1-t
) * (1-t
) * (1-t
) * y0
+
372 3 * t
* (1-t
) * (1-t
) * c0y
+
373 3 * t
* t
* (1-t
) * c1y
+
376 x
= (1-t
) * x0
+ t
* x1
;
377 y
= (1-t
) * y0
+ t
* y1
;
379 x_trans
= trans
[0]*x
+ trans
[2]*y
+ trans
[4];
380 y_trans
= trans
[1]*x
+ trans
[3]*y
+ trans
[5];
381 r
= ((x_trans
- gradient_x0
) * gradient_dx
+
382 (y_trans
- gradient_y0
) * gradient_dy
) /
383 gradient_norm_squared
;
384 fprintf(stderr
, "(%g %g [%g]) ", x
, y
, r
);
385 struct grad_point
*point
= svgtiny_list_push(pts
);
387 svgtiny_list_free(pts
);
388 return svgtiny_OUT_OF_MEMORY
;
395 min_pt
= svgtiny_list_size(pts
) - 1;
398 fprintf(stderr
, "\n");
400 /* next segment start point is this segment end point */
404 fprintf(stderr
, "pts size %i, min_pt %i, min_r %.3f\n",
405 svgtiny_list_size(pts
), min_pt
, min_r
);
407 /* render triangles */
408 unsigned int stop_count
= state
->linear_gradient_stop_count
;
409 assert(2 <= stop_count
);
410 unsigned int current_stop
= 0;
411 float last_stop_r
= 0;
412 float current_stop_r
= state
->gradient_stop
[0].offset
;
413 int red0
, green0
, blue0
, red1
, green1
, blue1
;
414 red0
= red1
= svgtiny_RED(state
->gradient_stop
[0].color
);
415 green0
= green1
= svgtiny_GREEN(state
->gradient_stop
[0].color
);
416 blue0
= blue1
= svgtiny_BLUE(state
->gradient_stop
[0].color
);
417 unsigned int t
, a
, b
;
419 a
= (min_pt
+ 1) % svgtiny_list_size(pts
);
420 b
= min_pt
== 0 ? svgtiny_list_size(pts
) - 1 : min_pt
- 1;
422 struct grad_point
*point_t
= svgtiny_list_get(pts
, t
);
423 struct grad_point
*point_a
= svgtiny_list_get(pts
, a
);
424 struct grad_point
*point_b
= svgtiny_list_get(pts
, b
);
425 float mean_r
= (point_t
->r
+ point_a
->r
+ point_b
->r
) / 3;
426 /*fprintf(stderr, "triangle: t %i %.3f a %i %.3f b %i %.3f "
428 t, pts[t].r, a, pts[a].r, b, pts[b].r,
430 while (current_stop
!= stop_count
&& current_stop_r
< mean_r
) {
432 if (current_stop
== stop_count
)
437 red1
= svgtiny_RED(state
->
438 gradient_stop
[current_stop
].color
);
439 green1
= svgtiny_GREEN(state
->
440 gradient_stop
[current_stop
].color
);
441 blue1
= svgtiny_BLUE(state
->
442 gradient_stop
[current_stop
].color
);
443 last_stop_r
= current_stop_r
;
444 current_stop_r
= state
->
445 gradient_stop
[current_stop
].offset
;
447 float *p
= malloc(10 * sizeof p
[0]);
449 return svgtiny_OUT_OF_MEMORY
;
450 p
[0] = svgtiny_PATH_MOVE
;
453 p
[3] = svgtiny_PATH_LINE
;
456 p
[6] = svgtiny_PATH_LINE
;
459 p
[9] = svgtiny_PATH_CLOSE
;
460 svgtiny_transform_path(p
, 10, state
);
461 struct svgtiny_shape
*shape
= svgtiny_add_shape(state
);
464 return svgtiny_OUT_OF_MEMORY
;
467 shape
->path_length
= 10;
468 /*shape->fill = svgtiny_TRANSPARENT;*/
469 if (current_stop
== 0)
470 shape
->fill
= state
->gradient_stop
[0].color
;
471 else if (current_stop
== stop_count
)
472 shape
->fill
= state
->
473 gradient_stop
[stop_count
- 1].color
;
475 float stop_r
= (mean_r
- last_stop_r
) /
476 (current_stop_r
- last_stop_r
);
477 shape
->fill
= svgtiny_RGB(
478 (int) ((1 - stop_r
) * red0
+ stop_r
* red1
),
479 (int) ((1 - stop_r
) * green0
+ stop_r
* green1
),
480 (int) ((1 - stop_r
) * blue0
+ stop_r
* blue1
));
482 shape
->stroke
= svgtiny_TRANSPARENT
;
483 #ifdef GRADIENT_DEBUG
484 shape
->stroke
= svgtiny_RGB(0, 0, 0xff);
486 state
->diagram
->shape_count
++;
487 if (point_a
->r
< point_b
->r
) {
489 a
= (a
+ 1) % svgtiny_list_size(pts
);
492 b
= b
== 0 ? svgtiny_list_size(pts
) - 1 : b
- 1;
496 /* render gradient vector for debugging */
497 #ifdef GRADIENT_DEBUG
499 float *p
= malloc(7 * sizeof p
[0]);
501 return svgtiny_OUT_OF_MEMORY
;
502 p
[0] = svgtiny_PATH_MOVE
;
505 p
[3] = svgtiny_PATH_LINE
;
508 p
[6] = svgtiny_PATH_CLOSE
;
509 svgtiny_transform_path(p
, 7, state
);
510 struct svgtiny_shape
*shape
= svgtiny_add_shape(state
);
513 return svgtiny_OUT_OF_MEMORY
;
516 shape
->path_length
= 7;
517 shape
->fill
= svgtiny_TRANSPARENT
;
518 shape
->stroke
= svgtiny_RGB(0xff, 0, 0);
519 state
->diagram
->shape_count
++;
523 /* render triangle vertices with r values for debugging */
524 #ifdef GRADIENT_DEBUG
525 for (unsigned int i
= 0; i
!= pts
->size
; i
++) {
526 struct grad_point
*point
= svgtiny_list_get(pts
, i
);
527 struct svgtiny_shape
*shape
= svgtiny_add_shape(state
);
529 return svgtiny_OUT_OF_MEMORY
;
530 char *text
= malloc(20);
532 return svgtiny_OUT_OF_MEMORY
;
533 sprintf(text
, "%i=%.3f", i
, point
->r
);
535 shape
->text_x
= state
->ctm
.a
* point
->x
+
536 state
->ctm
.c
* point
->y
+ state
->ctm
.e
;
537 shape
->text_y
= state
->ctm
.b
* point
->x
+
538 state
->ctm
.d
* point
->y
+ state
->ctm
.f
;
539 shape
->fill
= svgtiny_RGB(0, 0, 0);
540 shape
->stroke
= svgtiny_TRANSPARENT
;
541 state
->diagram
->shape_count
++;
545 /* plot actual path outline */
546 if (state
->stroke
!= svgtiny_TRANSPARENT
) {
547 svgtiny_transform_path(p
, n
, state
);
549 struct svgtiny_shape
*shape
= svgtiny_add_shape(state
);
552 return svgtiny_OUT_OF_MEMORY
;
555 shape
->path_length
= n
;
556 shape
->fill
= svgtiny_TRANSPARENT
;
557 state
->diagram
->shape_count
++;
562 svgtiny_list_free(pts
);
569 * Get the bounding box of path.
572 void svgtiny_path_bbox(float *p
, unsigned int n
,
573 float *x0
, float *y0
, float *x1
, float *y1
)
578 for (unsigned int j
= 0; j
!= n
; ) {
579 unsigned int points
= 0;
580 switch ((int) p
[j
]) {
581 case svgtiny_PATH_MOVE
:
582 case svgtiny_PATH_LINE
:
585 case svgtiny_PATH_CLOSE
:
588 case svgtiny_PATH_BEZIER
:
595 for (unsigned int k
= 0; k
!= points
; k
++) {
596 float x
= p
[j
], y
= p
[j
+ 1];
612 * Invert a transformation matrix.
614 void svgtiny_invert_matrix(float *m
, float *inv
)
616 float determinant
= m
[0]*m
[3] - m
[1]*m
[2];
617 inv
[0] = m
[3] / determinant
;
618 inv
[1] = -m
[1] / determinant
;
619 inv
[2] = -m
[2] / determinant
;
620 inv
[3] = m
[0] / determinant
;
621 inv
[4] = (m
[2]*m
[5] - m
[3]*m
[4]) / determinant
;
622 inv
[5] = (m
[1]*m
[4] - m
[0]*m
[5]) / determinant
;
627 * Find an element in the document by id.
630 xmlNode
*svgtiny_find_element_by_id(xmlNode
*node
, const char *id
)
635 for (child
= node
->children
; child
; child
= child
->next
) {
636 if (child
->type
!= XML_ELEMENT_NODE
)
638 xmlAttr
*attr
= xmlHasProp(child
, (const xmlChar
*) "id");
639 if (attr
&& strcmp(id
, (const char *) attr
->children
->content
)
642 found
= svgtiny_find_element_by_id(child
, id
);