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 */
12 #include "svgtiny_internal.h"
14 #define GRADIENT_DEBUG
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
);
24 * Find a gradient by id and parse it.
27 void svgtiny_find_gradient(const char *id
, struct svgtiny_parse_state
*state
)
29 fprintf(stderr
, "svgtiny_find_gradient: id \"%s\"\n", id
);
31 state
->linear_gradient_stop_count
= 0;
33 xmlNode
*gradient
= svgtiny_find_element_by_id(
34 (xmlNode
*) state
->document
, id
);
35 fprintf(stderr
, "gradient %p\n", gradient
);
37 fprintf(stderr
, "gradient \"%s\" not found\n", id
);
41 fprintf(stderr
, "gradient name \"%s\"\n", gradient
->name
);
42 if (strcmp((const char *) gradient
->name
, "linearGradient") == 0) {
43 svgtiny_parse_linear_gradient(gradient
, state
);
49 * Parse a <linearGradient> element node.
51 * http://www.w3.org/TR/SVG11/pservers#LinearGradients
54 svgtiny_code
svgtiny_parse_linear_gradient(xmlNode
*linear
,
55 struct svgtiny_parse_state
*state
)
57 xmlAttr
*href
= xmlHasProp(linear
, (const xmlChar
*) "href");
58 if (href
&& href
->children
->content
[0] == '#')
59 svgtiny_find_gradient((const char *) href
->children
->content
63 for (xmlNode
*stop
= linear
->children
; stop
; stop
= stop
->next
) {
65 svgtiny_colour color
= svgtiny_TRANSPARENT
;
67 if (stop
->type
!= XML_ELEMENT_NODE
)
69 if (strcmp((const char *) stop
->name
, "stop") != 0)
72 for (xmlAttr
*attr
= stop
->properties
; attr
;
74 const char *name
= (const char *) attr
->name
;
76 (const char *) attr
->children
->content
;
77 if (strcmp(name
, "offset") == 0)
78 offset
= svgtiny_parse_gradient_offset(content
);
79 else if (strcmp(name
, "stop-color") == 0)
80 svgtiny_parse_color(content
, &color
, state
);
81 else if (strcmp(name
, "style") == 0) {
84 if ((s
= strstr(content
, "stop-color:"))) {
88 value
= strndup(s
, strcspn(s
, "; "));
89 svgtiny_parse_color(value
, &color
,
96 if (offset
!= -1 && color
!= svgtiny_TRANSPARENT
) {
97 fprintf(stderr
, "stop %g %x\n", offset
, color
);
98 state
->gradient_stop
[i
].offset
= offset
;
99 state
->gradient_stop
[i
].color
= color
;
103 if (i
== svgtiny_MAX_STOPS
)
108 state
->linear_gradient_stop_count
= i
;
114 float svgtiny_parse_gradient_offset(const char *s
)
116 int num_length
= strspn(s
, "0123456789+-.");
117 const char *unit
= s
+ num_length
;
118 float n
= atof((const char *) s
);
122 else if (unit
[0] == '%')
136 * Add a path with a linear gradient fill to the svgtiny_diagram.
139 svgtiny_code
svgtiny_add_path_linear_gradient(float *p
, unsigned int n
,
140 struct svgtiny_parse_state
*state
)
142 /* determine object bounding box */
143 float object_x0
, object_y0
, object_x1
, object_y1
;
144 svgtiny_path_bbox(p
, n
, &object_x0
, &object_y0
, &object_x1
, &object_y1
);
145 #ifdef GRADIENT_DEBUG
146 fprintf(stderr
, "object bbox: (%g %g) (%g %g)\n",
147 object_x0
, object_y0
, object_x1
, object_y1
);
150 /* compute gradient vector */
151 float gradient_x0
= 0, gradient_y0
= 0,
152 gradient_x1
= 1, gradient_y1
= 0.7,
153 gradient_dx
, gradient_dy
;
154 gradient_x0
= object_x0
+ gradient_x0
* (object_x1
- object_x0
);
155 gradient_y0
= object_y0
+ gradient_y0
* (object_y1
- object_y0
);
156 gradient_x1
= object_x0
+ gradient_x1
* (object_x1
- object_x0
);
157 gradient_y1
= object_y0
+ gradient_y1
* (object_y1
- object_y0
);
158 gradient_dx
= gradient_x1
- gradient_x0
;
159 gradient_dy
= gradient_y1
- gradient_y0
;
160 #ifdef GRADIENT_DEBUG
161 fprintf(stderr
, "gradient vector: (%g %g) => (%g %g)\n",
162 gradient_x0
, gradient_y0
, gradient_x1
, gradient_y1
);
165 /* show theoretical gradient strips for debugging */
166 /*unsigned int strips = 10;
167 for (unsigned int z = 0; z != strips; z++) {
168 float f0, fd, strip_x0, strip_y0, strip_dx, strip_dy;
169 f0 = (float) z / (float) strips;
170 fd = (float) 1 / (float) strips;
171 strip_x0 = gradient_x0 + f0 * gradient_dx;
172 strip_y0 = gradient_y0 + f0 * gradient_dy;
173 strip_dx = fd * gradient_dx;
174 strip_dy = fd * gradient_dy;
175 fprintf(stderr, "strip %i vector: (%g %g) + (%g %g)\n",
176 z, strip_x0, strip_y0, strip_dx, strip_dy);
178 float *p = malloc(13 * sizeof p[0]);
180 return svgtiny_OUT_OF_MEMORY;
181 p[0] = svgtiny_PATH_MOVE;
182 p[1] = strip_x0 + (strip_dy * 3);
183 p[2] = strip_y0 - (strip_dx * 3);
184 p[3] = svgtiny_PATH_LINE;
185 p[4] = p[1] + strip_dx;
186 p[5] = p[2] + strip_dy;
187 p[6] = svgtiny_PATH_LINE;
188 p[7] = p[4] - (strip_dy * 6);
189 p[8] = p[5] + (strip_dx * 6);
190 p[9] = svgtiny_PATH_LINE;
191 p[10] = p[7] - strip_dx;
192 p[11] = p[8] - strip_dy;
193 p[12] = svgtiny_PATH_CLOSE;
194 svgtiny_transform_path(p, 13, state);
195 struct svgtiny_shape *shape = svgtiny_add_shape(state);
198 return svgtiny_OUT_OF_MEMORY;
201 shape->path_length = 13;
202 shape->fill = svgtiny_TRANSPARENT;
203 shape->stroke = svgtiny_RGB(0, 0xff, 0);
204 state->diagram->shape_count++;
207 /* compute points on the path for triangle vertices */
208 unsigned int steps
= 10;
209 float x0
, y0
, x1
, y1
;
210 float gradient_norm_squared
= gradient_dx
* gradient_dx
+
211 gradient_dy
* gradient_dy
;
215 struct grad_point
*pts
= malloc(n
* steps
* sizeof pts
[0]);
217 return svgtiny_OUT_OF_MEMORY
;
218 unsigned int pts_count
= 0;
220 unsigned int min_pt
= 0;
221 for (unsigned int j
= 0; j
!= n
; ) {
222 switch ((int) p
[j
]) {
223 case svgtiny_PATH_MOVE
:
228 case svgtiny_PATH_LINE
:
229 case svgtiny_PATH_CLOSE
:
230 if (((int) p
[j
]) == svgtiny_PATH_LINE
) {
239 fprintf(stderr
, "line: ");
240 for (unsigned int z
= 0; z
!= steps
; z
++) {
242 f
= (float) z
/ (float) steps
;
243 x
= x0
+ f
* (x1
- x0
);
244 y
= y0
+ f
* (y1
- y0
);
245 r
= ((x
- gradient_x0
) * gradient_dx
+
246 (y
- gradient_y0
) * gradient_dy
) /
247 gradient_norm_squared
;
248 fprintf(stderr
, "(%g %g [%g]) ", x
, y
, r
);
249 pts
[pts_count
].x
= x
;
250 pts
[pts_count
].y
= y
;
251 pts
[pts_count
].r
= r
;
258 fprintf(stderr
, "\n");
262 case svgtiny_PATH_BEZIER
:
263 fprintf(stderr
, "bezier: ");
264 for (unsigned int z
= 0; z
!= steps
; z
++) {
266 t
= (float) z
/ (float) steps
;
267 x
= (1-t
) * (1-t
) * (1-t
) * x0
+
268 3 * t
* (1-t
) * (1-t
) * p
[j
+ 1] +
269 3 * t
* t
* (1-t
) * p
[j
+ 3] +
270 t
* t
* t
* p
[j
+ 5];
271 y
= (1-t
) * (1-t
) * (1-t
) * y0
+
272 3 * t
* (1-t
) * (1-t
) * p
[j
+ 2] +
273 3 * t
* t
* (1-t
) * p
[j
+ 4] +
274 t
* t
* t
* p
[j
+ 6];
275 r
= ((x
- gradient_x0
) * gradient_dx
+
276 (y
- gradient_y0
) * gradient_dy
) /
277 gradient_norm_squared
;
278 fprintf(stderr
, "(%g %g [%g]) ", x
, y
, r
);
279 pts
[pts_count
].x
= x
;
280 pts
[pts_count
].y
= y
;
281 pts
[pts_count
].r
= r
;
288 fprintf(stderr
, "\n");
297 fprintf(stderr
, "pts_count %i, min_pt %i, min_r %.3f\n",
298 pts_count
, min_pt
, min_r
);
300 unsigned int stop_count
= state
->linear_gradient_stop_count
;
301 assert(2 <= stop_count
);
302 unsigned int current_stop
= 0;
303 float last_stop_r
= 0;
304 float current_stop_r
= state
->gradient_stop
[0].offset
;
305 int red0
, green0
, blue0
, red1
, green1
, blue1
;
306 red0
= red1
= svgtiny_RED(state
->gradient_stop
[0].color
);
307 green0
= green1
= svgtiny_GREEN(state
->gradient_stop
[0].color
);
308 blue0
= blue1
= svgtiny_BLUE(state
->gradient_stop
[0].color
);
309 unsigned int t
, a
, b
;
311 a
= (min_pt
+ 1) % pts_count
;
312 b
= min_pt
== 0 ? pts_count
- 1 : min_pt
- 1;
314 float mean_r
= (pts
[t
].r
+ pts
[a
].r
+ pts
[b
].r
) / 3;
315 fprintf(stderr
, "triangle: t %i %.3f a %i %.3f b %i %.3f "
317 t
, pts
[t
].r
, a
, pts
[a
].r
, b
, pts
[b
].r
,
319 while (current_stop
!= stop_count
&& current_stop_r
< mean_r
) {
321 if (current_stop
== stop_count
)
326 red1
= svgtiny_RED(state
->
327 gradient_stop
[current_stop
].color
);
328 green1
= svgtiny_GREEN(state
->
329 gradient_stop
[current_stop
].color
);
330 blue1
= svgtiny_BLUE(state
->
331 gradient_stop
[current_stop
].color
);
332 last_stop_r
= current_stop_r
;
333 current_stop_r
= state
->
334 gradient_stop
[current_stop
].offset
;
336 float *p
= malloc(10 * sizeof p
[0]);
338 return svgtiny_OUT_OF_MEMORY
;
339 p
[0] = svgtiny_PATH_MOVE
;
342 p
[3] = svgtiny_PATH_LINE
;
345 p
[6] = svgtiny_PATH_LINE
;
348 p
[9] = svgtiny_PATH_CLOSE
;
349 svgtiny_transform_path(p
, 10, state
);
350 struct svgtiny_shape
*shape
= svgtiny_add_shape(state
);
353 return svgtiny_OUT_OF_MEMORY
;
356 shape
->path_length
= 10;
357 /*shape->fill = svgtiny_TRANSPARENT;*/
358 if (current_stop
== 0)
359 shape
->fill
= state
->gradient_stop
[0].color
;
360 else if (current_stop
== stop_count
)
361 shape
->fill
= state
->
362 gradient_stop
[stop_count
- 1].color
;
364 float stop_r
= (mean_r
- last_stop_r
) /
365 (current_stop_r
- last_stop_r
);
366 shape
->fill
= svgtiny_RGB(
367 (int) ((1 - stop_r
) * red0
+ stop_r
* red1
),
368 (int) ((1 - stop_r
) * green0
+ stop_r
* green1
),
369 (int) ((1 - stop_r
) * blue0
+ stop_r
* blue1
));
371 shape
->stroke
= svgtiny_TRANSPARENT
;
372 #ifdef GRADIENT_DEBUG
373 shape
->stroke
= svgtiny_RGB(0, 0, 0xff);
375 state
->diagram
->shape_count
++;
376 if (pts
[a
].r
< pts
[b
].r
) {
378 a
= (a
+ 1) % pts_count
;
381 b
= b
== 0 ? pts_count
- 1 : b
- 1;
385 /* render gradient vector for debugging */
386 #ifdef GRADIENT_DEBUG
388 float *p
= malloc(7 * sizeof p
[0]);
390 return svgtiny_OUT_OF_MEMORY
;
391 p
[0] = svgtiny_PATH_MOVE
;
394 p
[3] = svgtiny_PATH_LINE
;
397 p
[6] = svgtiny_PATH_CLOSE
;
398 svgtiny_transform_path(p
, 7, state
);
399 struct svgtiny_shape
*shape
= svgtiny_add_shape(state
);
402 return svgtiny_OUT_OF_MEMORY
;
405 shape
->path_length
= 7;
406 shape
->fill
= svgtiny_TRANSPARENT
;
407 shape
->stroke
= svgtiny_RGB(0xff, 0, 0);
408 state
->diagram
->shape_count
++;
412 /* render triangle vertices with r values for debugging */
413 #ifdef GRADIENT_DEBUG
414 for (unsigned int i
= 0; i
!= pts_count
; i
++) {
415 struct svgtiny_shape
*shape
= svgtiny_add_shape(state
);
417 return svgtiny_OUT_OF_MEMORY
;
418 char *text
= malloc(20);
420 return svgtiny_OUT_OF_MEMORY
;
421 sprintf(text
, "%i=%.3f", i
, pts
[i
].r
);
423 shape
->text_x
= state
->ctm
.a
* pts
[i
].x
+
424 state
->ctm
.c
* pts
[i
].y
+ state
->ctm
.e
;
425 shape
->text_y
= state
->ctm
.b
* pts
[i
].x
+
426 state
->ctm
.d
* pts
[i
].y
+ state
->ctm
.f
;
427 shape
->fill
= svgtiny_RGB(0, 0, 0);
428 state
->diagram
->shape_count
++;
432 /* plot actual path outline */
433 if (state
->stroke
!= svgtiny_TRANSPARENT
) {
434 svgtiny_transform_path(p
, n
, state
);
436 struct svgtiny_shape
*shape
= svgtiny_add_shape(state
);
439 return svgtiny_OUT_OF_MEMORY
;
442 shape
->path_length
= n
;
443 shape
->fill
= svgtiny_TRANSPARENT
;
444 state
->diagram
->shape_count
++;
452 * Get the bounding box of path.
455 void svgtiny_path_bbox(float *p
, unsigned int n
,
456 float *x0
, float *y0
, float *x1
, float *y1
)
461 for (unsigned int j
= 0; j
!= n
; ) {
462 unsigned int points
= 0;
463 switch ((int) p
[j
]) {
464 case svgtiny_PATH_MOVE
:
465 case svgtiny_PATH_LINE
:
468 case svgtiny_PATH_CLOSE
:
471 case svgtiny_PATH_BEZIER
:
478 for (unsigned int k
= 0; k
!= points
; k
++) {
479 float x
= p
[j
], y
= p
[j
+ 1];
495 * Find an element in the document by id.
498 xmlNode
*svgtiny_find_element_by_id(xmlNode
*node
, const char *id
)
503 for (child
= node
->children
; child
; child
= child
->next
) {
504 if (child
->type
!= XML_ELEMENT_NODE
)
506 xmlAttr
*attr
= xmlHasProp(child
, (const xmlChar
*) "id");
507 if (attr
&& strcmp(id
, (const char *) attr
->children
->content
)
510 found
= svgtiny_find_element_by_id(child
, id
);