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>
12 #include "svgtiny_internal.h"
16 static svgtiny_code
svgtiny_parse_linear_gradient(xmlNode
*linear
,
17 struct svgtiny_parse_state
*state
);
18 static float svgtiny_parse_gradient_offset(const char *s
);
19 static void svgtiny_path_bbox(float *p
, unsigned int n
,
20 float *x0
, float *y0
, float *x1
, float *y1
);
21 static void svgtiny_invert_matrix(float *m
, float *inv
);
25 * Find a gradient by id and parse it.
28 void svgtiny_find_gradient(const char *id
, struct svgtiny_parse_state
*state
)
32 fprintf(stderr
, "svgtiny_find_gradient: id \"%s\"\n", id
);
34 state
->linear_gradient_stop_count
= 0;
35 state
->gradient_x1
= "0%";
36 state
->gradient_y1
= "0%";
37 state
->gradient_x2
= "100%";
38 state
->gradient_y2
= "0%";
39 state
->gradient_user_space_on_use
= false;
40 state
->gradient_transform
.a
= 1;
41 state
->gradient_transform
.b
= 0;
42 state
->gradient_transform
.c
= 0;
43 state
->gradient_transform
.d
= 1;
44 state
->gradient_transform
.e
= 0;
45 state
->gradient_transform
.f
= 0;
47 gradient
= svgtiny_find_element_by_id(
48 (xmlNode
*) state
->document
, id
);
49 fprintf(stderr
, "gradient %p\n", (void *) gradient
);
51 fprintf(stderr
, "gradient \"%s\" not found\n", id
);
55 fprintf(stderr
, "gradient name \"%s\"\n", gradient
->name
);
56 if (strcmp((const char *) gradient
->name
, "linearGradient") == 0) {
57 svgtiny_parse_linear_gradient(gradient
, state
);
63 * Parse a <linearGradient> element node.
65 * http://www.w3.org/TR/SVG11/pservers#LinearGradients
68 svgtiny_code
svgtiny_parse_linear_gradient(xmlNode
*linear
,
69 struct svgtiny_parse_state
*state
)
74 xmlAttr
*href
= xmlHasProp(linear
, (const xmlChar
*) "href");
75 if (href
&& href
->children
->content
[0] == '#')
76 svgtiny_find_gradient((const char *) href
->children
->content
79 for (attr
= linear
->properties
; attr
; attr
= attr
->next
) {
80 const char *name
= (const char *) attr
->name
;
81 const char *content
= (const char *) attr
->children
->content
;
82 if (strcmp(name
, "x1") == 0)
83 state
->gradient_x1
= content
;
84 else if (strcmp(name
, "y1") == 0)
85 state
->gradient_y1
= content
;
86 else if (strcmp(name
, "x2") == 0)
87 state
->gradient_x2
= content
;
88 else if (strcmp(name
, "y2") == 0)
89 state
->gradient_y2
= content
;
90 else if (strcmp(name
, "gradientUnits") == 0)
91 state
->gradient_user_space_on_use
=
92 strcmp(content
, "userSpaceOnUse") == 0;
93 else if (strcmp(name
, "gradientTransform") == 0) {
94 float a
= 1, b
= 0, c
= 0, d
= 1, e
= 0, f
= 0;
95 char *s
= strdup(content
);
97 return svgtiny_OUT_OF_MEMORY
;
98 svgtiny_parse_transform(s
, &a
, &b
, &c
, &d
, &e
, &f
);
100 fprintf(stderr
, "transform %g %g %g %g %g %g\n",
102 state
->gradient_transform
.a
= a
;
103 state
->gradient_transform
.b
= b
;
104 state
->gradient_transform
.c
= c
;
105 state
->gradient_transform
.d
= d
;
106 state
->gradient_transform
.e
= e
;
107 state
->gradient_transform
.f
= f
;
111 for (stop
= linear
->children
; stop
; stop
= stop
->next
) {
113 svgtiny_colour color
= svgtiny_TRANSPARENT
;
115 if (stop
->type
!= XML_ELEMENT_NODE
)
117 if (strcmp((const char *) stop
->name
, "stop") != 0)
120 for (attr
= stop
->properties
; attr
;
122 const char *name
= (const char *) attr
->name
;
123 const char *content
=
124 (const char *) attr
->children
->content
;
125 if (strcmp(name
, "offset") == 0)
126 offset
= svgtiny_parse_gradient_offset(content
);
127 else if (strcmp(name
, "stop-color") == 0)
128 svgtiny_parse_color(content
, &color
, state
);
129 else if (strcmp(name
, "style") == 0) {
132 if ((s
= strstr(content
, "stop-color:"))) {
136 value
= strndup(s
, strcspn(s
, "; "));
137 svgtiny_parse_color(value
, &color
,
144 if (offset
!= -1 && color
!= svgtiny_TRANSPARENT
) {
145 fprintf(stderr
, "stop %g %x\n", offset
, color
);
146 state
->gradient_stop
[i
].offset
= offset
;
147 state
->gradient_stop
[i
].color
= color
;
151 if (i
== svgtiny_MAX_STOPS
)
156 state
->linear_gradient_stop_count
= i
;
162 float svgtiny_parse_gradient_offset(const char *s
)
164 int num_length
= strspn(s
, "0123456789+-.");
165 const char *unit
= s
+ num_length
;
166 float n
= atof((const char *) s
);
170 else if (unit
[0] == '%')
184 * Add a path with a linear gradient fill to the svgtiny_diagram.
187 svgtiny_code
svgtiny_add_path_linear_gradient(float *p
, unsigned int n
,
188 struct svgtiny_parse_state
*state
)
193 float object_x0
, object_y0
, object_x1
, object_y1
;
194 float gradient_x0
, gradient_y0
, gradient_x1
, gradient_y1
,
195 gradient_dx
, gradient_dy
;
197 unsigned int steps
= 10;
198 float x0
= 0, y0
= 0, x0_trans
, y0_trans
, r0
; /* segment start point */
199 float x1
, y1
, x1_trans
, y1_trans
, r1
; /* segment end point */
200 /* segment control points (beziers only) */
201 float c0x
= 0, c0y
= 0, c1x
= 0, c1y
= 0;
202 float gradient_norm_squared
;
203 struct svgtiny_list
*pts
;
205 unsigned int min_pt
= 0;
207 unsigned int stop_count
;
208 unsigned int current_stop
;
210 float current_stop_r
;
211 int red0
, green0
, blue0
, red1
, green1
, blue1
;
212 unsigned int t
, a
, b
;
214 /* determine object bounding box */
215 svgtiny_path_bbox(p
, n
, &object_x0
, &object_y0
, &object_x1
, &object_y1
);
216 #ifdef GRADIENT_DEBUG
217 fprintf(stderr
, "object bbox: (%g %g) (%g %g)\n",
218 object_x0
, object_y0
, object_x1
, object_y1
);
221 /* compute gradient vector */
222 fprintf(stderr
, "x1 %s, y1 %s, x2 %s, y2 %s\n",
223 state
->gradient_x1
, state
->gradient_y1
,
224 state
->gradient_x2
, state
->gradient_y2
);
225 if (!state
->gradient_user_space_on_use
) {
226 gradient_x0
= object_x0
+
227 svgtiny_parse_length(state
->gradient_x1
,
228 object_x1
- object_x0
, *state
);
229 gradient_y0
= object_y0
+
230 svgtiny_parse_length(state
->gradient_y1
,
231 object_y1
- object_y0
, *state
);
232 gradient_x1
= object_x0
+
233 svgtiny_parse_length(state
->gradient_x2
,
234 object_x1
- object_x0
, *state
);
235 gradient_y1
= object_y0
+
236 svgtiny_parse_length(state
->gradient_y2
,
237 object_y1
- object_y0
, *state
);
239 gradient_x0
= svgtiny_parse_length(state
->gradient_x1
,
240 state
->viewport_width
, *state
);
241 gradient_y0
= svgtiny_parse_length(state
->gradient_y1
,
242 state
->viewport_height
, *state
);
243 gradient_x1
= svgtiny_parse_length(state
->gradient_x2
,
244 state
->viewport_width
, *state
);
245 gradient_y1
= svgtiny_parse_length(state
->gradient_y2
,
246 state
->viewport_height
, *state
);
248 gradient_dx
= gradient_x1
- gradient_x0
;
249 gradient_dy
= gradient_y1
- gradient_y0
;
250 #ifdef GRADIENT_DEBUG
251 fprintf(stderr
, "gradient vector: (%g %g) => (%g %g)\n",
252 gradient_x0
, gradient_y0
, gradient_x1
, gradient_y1
);
255 /* show theoretical gradient strips for debugging */
256 /*unsigned int strips = 10;
257 for (unsigned int z = 0; z != strips; z++) {
258 float f0, fd, strip_x0, strip_y0, strip_dx, strip_dy;
259 f0 = (float) z / (float) strips;
260 fd = (float) 1 / (float) strips;
261 strip_x0 = gradient_x0 + f0 * gradient_dx;
262 strip_y0 = gradient_y0 + f0 * gradient_dy;
263 strip_dx = fd * gradient_dx;
264 strip_dy = fd * gradient_dy;
265 fprintf(stderr, "strip %i vector: (%g %g) + (%g %g)\n",
266 z, strip_x0, strip_y0, strip_dx, strip_dy);
268 float *p = malloc(13 * sizeof p[0]);
270 return svgtiny_OUT_OF_MEMORY;
271 p[0] = svgtiny_PATH_MOVE;
272 p[1] = strip_x0 + (strip_dy * 3);
273 p[2] = strip_y0 - (strip_dx * 3);
274 p[3] = svgtiny_PATH_LINE;
275 p[4] = p[1] + strip_dx;
276 p[5] = p[2] + strip_dy;
277 p[6] = svgtiny_PATH_LINE;
278 p[7] = p[4] - (strip_dy * 6);
279 p[8] = p[5] + (strip_dx * 6);
280 p[9] = svgtiny_PATH_LINE;
281 p[10] = p[7] - strip_dx;
282 p[11] = p[8] - strip_dy;
283 p[12] = svgtiny_PATH_CLOSE;
284 svgtiny_transform_path(p, 13, state);
285 struct svgtiny_shape *shape = svgtiny_add_shape(state);
288 return svgtiny_OUT_OF_MEMORY;
291 shape->path_length = 13;
292 shape->fill = svgtiny_TRANSPARENT;
293 shape->stroke = svgtiny_RGB(0, 0xff, 0);
294 state->diagram->shape_count++;
297 /* invert gradient transform for applying to vertices */
298 svgtiny_invert_matrix(&state
->gradient_transform
.a
, trans
);
299 fprintf(stderr
, "inverse transform %g %g %g %g %g %g\n",
300 trans
[0], trans
[1], trans
[2], trans
[3],
303 /* compute points on the path for triangle vertices */
304 /* r, r0, r1 are distance along gradient vector */
305 gradient_norm_squared
= gradient_dx
* gradient_dx
+
306 gradient_dy
* gradient_dy
;
307 pts
= svgtiny_list_create(
308 sizeof (struct grad_point
));
310 return svgtiny_OUT_OF_MEMORY
;
311 for (j
= 0; j
!= n
; ) {
312 int segment_type
= (int) p
[j
];
313 struct grad_point
*point
;
316 if (segment_type
== svgtiny_PATH_MOVE
) {
323 assert(segment_type
== svgtiny_PATH_CLOSE
||
324 segment_type
== svgtiny_PATH_LINE
||
325 segment_type
== svgtiny_PATH_BEZIER
);
327 /* start point (x0, y0) */
328 x0_trans
= trans
[0]*x0
+ trans
[2]*y0
+ trans
[4];
329 y0_trans
= trans
[1]*x0
+ trans
[3]*y0
+ trans
[5];
330 r0
= ((x0_trans
- gradient_x0
) * gradient_dx
+
331 (y0_trans
- gradient_y0
) * gradient_dy
) /
332 gradient_norm_squared
;
333 point
= svgtiny_list_push(pts
);
335 svgtiny_list_free(pts
);
336 return svgtiny_OUT_OF_MEMORY
;
343 min_pt
= svgtiny_list_size(pts
) - 1;
346 /* end point (x1, y1) */
347 if (segment_type
== svgtiny_PATH_LINE
) {
351 } else if (segment_type
== svgtiny_PATH_CLOSE
) {
355 } else /* svgtiny_PATH_BEZIER */ {
364 x1_trans
= trans
[0]*x1
+ trans
[2]*y1
+ trans
[4];
365 y1_trans
= trans
[1]*x1
+ trans
[3]*y1
+ trans
[5];
366 r1
= ((x1_trans
- gradient_x0
) * gradient_dx
+
367 (y1_trans
- gradient_y0
) * gradient_dy
) /
368 gradient_norm_squared
;
370 /* determine steps from change in r */
371 steps
= ceilf(fabsf(r1
- r0
) / 0.05);
374 fprintf(stderr
, "r0 %g, r1 %g, steps %i\n",
377 /* loop through intermediate points */
378 for (z
= 1; z
!= steps
; z
++) {
379 float t
, x
, y
, x_trans
, y_trans
, r
;
380 struct grad_point
*point
;
381 t
= (float) z
/ (float) steps
;
382 if (segment_type
== svgtiny_PATH_BEZIER
) {
383 x
= (1-t
) * (1-t
) * (1-t
) * x0
+
384 3 * t
* (1-t
) * (1-t
) * c0x
+
385 3 * t
* t
* (1-t
) * c1x
+
387 y
= (1-t
) * (1-t
) * (1-t
) * y0
+
388 3 * t
* (1-t
) * (1-t
) * c0y
+
389 3 * t
* t
* (1-t
) * c1y
+
392 x
= (1-t
) * x0
+ t
* x1
;
393 y
= (1-t
) * y0
+ t
* y1
;
395 x_trans
= trans
[0]*x
+ trans
[2]*y
+ trans
[4];
396 y_trans
= trans
[1]*x
+ trans
[3]*y
+ trans
[5];
397 r
= ((x_trans
- gradient_x0
) * gradient_dx
+
398 (y_trans
- gradient_y0
) * gradient_dy
) /
399 gradient_norm_squared
;
400 fprintf(stderr
, "(%g %g [%g]) ", x
, y
, r
);
401 point
= svgtiny_list_push(pts
);
403 svgtiny_list_free(pts
);
404 return svgtiny_OUT_OF_MEMORY
;
411 min_pt
= svgtiny_list_size(pts
) - 1;
414 fprintf(stderr
, "\n");
416 /* next segment start point is this segment end point */
420 fprintf(stderr
, "pts size %i, min_pt %i, min_r %.3f\n",
421 svgtiny_list_size(pts
), min_pt
, min_r
);
423 /* render triangles */
424 stop_count
= state
->linear_gradient_stop_count
;
425 assert(2 <= stop_count
);
428 current_stop_r
= state
->gradient_stop
[0].offset
;
429 red0
= red1
= svgtiny_RED(state
->gradient_stop
[0].color
);
430 green0
= green1
= svgtiny_GREEN(state
->gradient_stop
[0].color
);
431 blue0
= blue1
= svgtiny_BLUE(state
->gradient_stop
[0].color
);
433 a
= (min_pt
+ 1) % svgtiny_list_size(pts
);
434 b
= min_pt
== 0 ? svgtiny_list_size(pts
) - 1 : min_pt
- 1;
436 struct grad_point
*point_t
= svgtiny_list_get(pts
, t
);
437 struct grad_point
*point_a
= svgtiny_list_get(pts
, a
);
438 struct grad_point
*point_b
= svgtiny_list_get(pts
, b
);
439 float mean_r
= (point_t
->r
+ point_a
->r
+ point_b
->r
) / 3;
441 struct svgtiny_shape
*shape
;
442 /*fprintf(stderr, "triangle: t %i %.3f a %i %.3f b %i %.3f "
444 t, pts[t].r, a, pts[a].r, b, pts[b].r,
446 while (current_stop
!= stop_count
&& current_stop_r
< mean_r
) {
448 if (current_stop
== stop_count
)
453 red1
= svgtiny_RED(state
->
454 gradient_stop
[current_stop
].color
);
455 green1
= svgtiny_GREEN(state
->
456 gradient_stop
[current_stop
].color
);
457 blue1
= svgtiny_BLUE(state
->
458 gradient_stop
[current_stop
].color
);
459 last_stop_r
= current_stop_r
;
460 current_stop_r
= state
->
461 gradient_stop
[current_stop
].offset
;
463 p
= malloc(10 * sizeof p
[0]);
465 return svgtiny_OUT_OF_MEMORY
;
466 p
[0] = svgtiny_PATH_MOVE
;
469 p
[3] = svgtiny_PATH_LINE
;
472 p
[6] = svgtiny_PATH_LINE
;
475 p
[9] = svgtiny_PATH_CLOSE
;
476 svgtiny_transform_path(p
, 10, state
);
477 shape
= svgtiny_add_shape(state
);
480 return svgtiny_OUT_OF_MEMORY
;
483 shape
->path_length
= 10;
484 /*shape->fill = svgtiny_TRANSPARENT;*/
485 if (current_stop
== 0)
486 shape
->fill
= state
->gradient_stop
[0].color
;
487 else if (current_stop
== stop_count
)
488 shape
->fill
= state
->
489 gradient_stop
[stop_count
- 1].color
;
491 float stop_r
= (mean_r
- last_stop_r
) /
492 (current_stop_r
- last_stop_r
);
493 shape
->fill
= svgtiny_RGB(
494 (int) ((1 - stop_r
) * red0
+ stop_r
* red1
),
495 (int) ((1 - stop_r
) * green0
+ stop_r
* green1
),
496 (int) ((1 - stop_r
) * blue0
+ stop_r
* blue1
));
498 shape
->stroke
= svgtiny_TRANSPARENT
;
499 #ifdef GRADIENT_DEBUG
500 shape
->stroke
= svgtiny_RGB(0, 0, 0xff);
502 state
->diagram
->shape_count
++;
503 if (point_a
->r
< point_b
->r
) {
505 a
= (a
+ 1) % svgtiny_list_size(pts
);
508 b
= b
== 0 ? svgtiny_list_size(pts
) - 1 : b
- 1;
512 /* render gradient vector for debugging */
513 #ifdef GRADIENT_DEBUG
515 float *p
= malloc(7 * sizeof p
[0]);
517 return svgtiny_OUT_OF_MEMORY
;
518 p
[0] = svgtiny_PATH_MOVE
;
521 p
[3] = svgtiny_PATH_LINE
;
524 p
[6] = svgtiny_PATH_CLOSE
;
525 svgtiny_transform_path(p
, 7, state
);
526 struct svgtiny_shape
*shape
= svgtiny_add_shape(state
);
529 return svgtiny_OUT_OF_MEMORY
;
532 shape
->path_length
= 7;
533 shape
->fill
= svgtiny_TRANSPARENT
;
534 shape
->stroke
= svgtiny_RGB(0xff, 0, 0);
535 state
->diagram
->shape_count
++;
539 /* render triangle vertices with r values for debugging */
540 #ifdef GRADIENT_DEBUG
541 for (unsigned int i
= 0; i
!= pts
->size
; i
++) {
542 struct grad_point
*point
= svgtiny_list_get(pts
, i
);
543 struct svgtiny_shape
*shape
= svgtiny_add_shape(state
);
545 return svgtiny_OUT_OF_MEMORY
;
546 char *text
= malloc(20);
548 return svgtiny_OUT_OF_MEMORY
;
549 sprintf(text
, "%i=%.3f", i
, point
->r
);
551 shape
->text_x
= state
->ctm
.a
* point
->x
+
552 state
->ctm
.c
* point
->y
+ state
->ctm
.e
;
553 shape
->text_y
= state
->ctm
.b
* point
->x
+
554 state
->ctm
.d
* point
->y
+ state
->ctm
.f
;
555 shape
->fill
= svgtiny_RGB(0, 0, 0);
556 shape
->stroke
= svgtiny_TRANSPARENT
;
557 state
->diagram
->shape_count
++;
561 /* plot actual path outline */
562 if (state
->stroke
!= svgtiny_TRANSPARENT
) {
563 struct svgtiny_shape
*shape
;
564 svgtiny_transform_path(p
, n
, state
);
566 shape
= svgtiny_add_shape(state
);
569 return svgtiny_OUT_OF_MEMORY
;
572 shape
->path_length
= n
;
573 shape
->fill
= svgtiny_TRANSPARENT
;
574 state
->diagram
->shape_count
++;
579 svgtiny_list_free(pts
);
586 * Get the bounding box of path.
589 void svgtiny_path_bbox(float *p
, unsigned int n
,
590 float *x0
, float *y0
, float *x1
, float *y1
)
597 for (j
= 0; j
!= n
; ) {
598 unsigned int points
= 0;
600 switch ((int) p
[j
]) {
601 case svgtiny_PATH_MOVE
:
602 case svgtiny_PATH_LINE
:
605 case svgtiny_PATH_CLOSE
:
608 case svgtiny_PATH_BEZIER
:
615 for (k
= 0; k
!= points
; k
++) {
616 float x
= p
[j
], y
= p
[j
+ 1];
632 * Invert a transformation matrix.
634 void svgtiny_invert_matrix(float *m
, float *inv
)
636 float determinant
= m
[0]*m
[3] - m
[1]*m
[2];
637 inv
[0] = m
[3] / determinant
;
638 inv
[1] = -m
[1] / determinant
;
639 inv
[2] = -m
[2] / determinant
;
640 inv
[3] = m
[0] / determinant
;
641 inv
[4] = (m
[2]*m
[5] - m
[3]*m
[4]) / determinant
;
642 inv
[5] = (m
[1]*m
[4] - m
[0]*m
[5]) / determinant
;
647 * Find an element in the document by id.
650 xmlNode
*svgtiny_find_element_by_id(xmlNode
*node
, const char *id
)
655 for (child
= node
->children
; child
; child
= child
->next
) {
657 if (child
->type
!= XML_ELEMENT_NODE
)
659 attr
= xmlHasProp(child
, (const xmlChar
*) "id");
660 if (attr
&& strcmp(id
, (const char *) attr
->children
->content
)
663 found
= svgtiny_find_element_by_id(child
, id
);