]> gitweb.michael.orlitzky.com - libsvgtiny.git/blob - src/svgtiny_gradient.c
Explicitly check if r0 or r1 are NaN, as if they are, on x86 the function evaluates...
[libsvgtiny.git] / src / svgtiny_gradient.c
1 /*
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>
6 */
7
8 #include <assert.h>
9 #include <math.h>
10 #include <string.h>
11 #include <stdio.h>
12 #include <stdlib.h>
13
14 #include "svgtiny.h"
15 #include "svgtiny_internal.h"
16
17 #undef GRADIENT_DEBUG
18
19 static svgtiny_code svgtiny_parse_linear_gradient(dom_element *linear,
20 struct svgtiny_parse_state *state);
21 static float svgtiny_parse_gradient_offset(const char *s);
22 static void svgtiny_path_bbox(float *p, unsigned int n,
23 float *x0, float *y0, float *x1, float *y1);
24 static void svgtiny_invert_matrix(float *m, float *inv);
25
26
27 /**
28 * Find a gradient by id and parse it.
29 */
30
31 void svgtiny_find_gradient(const char *id, struct svgtiny_parse_state *state)
32 {
33 dom_element *gradient;
34 dom_string *id_str;
35 dom_exception exc;
36
37 fprintf(stderr, "svgtiny_find_gradient: id \"%s\"\n", id);
38
39 state->linear_gradient_stop_count = 0;
40 if (state->gradient_x1 != NULL)
41 dom_string_unref(state->gradient_x1);
42 if (state->gradient_y1 != NULL)
43 dom_string_unref(state->gradient_y1);
44 if (state->gradient_x2 != NULL)
45 dom_string_unref(state->gradient_x2);
46 if (state->gradient_y2 != NULL)
47 dom_string_unref(state->gradient_y2);
48 state->gradient_x1 = dom_string_ref(state->interned_zero_percent);
49 state->gradient_y1 = dom_string_ref(state->interned_zero_percent);
50 state->gradient_x2 = dom_string_ref(state->interned_hundred_percent);
51 state->gradient_y2 = dom_string_ref(state->interned_zero_percent);
52 state->gradient_user_space_on_use = false;
53 state->gradient_transform.a = 1;
54 state->gradient_transform.b = 0;
55 state->gradient_transform.c = 0;
56 state->gradient_transform.d = 1;
57 state->gradient_transform.e = 0;
58 state->gradient_transform.f = 0;
59
60 exc = dom_string_create_interned((const uint8_t *) id, strlen(id),
61 &id_str);
62 if (exc != DOM_NO_ERR)
63 return;
64
65 exc = dom_document_get_element_by_id(state->document, id_str,
66 &gradient);
67 dom_string_unref(id_str);
68 if (exc != DOM_NO_ERR)
69 return;
70
71 if (gradient == NULL) {
72 fprintf(stderr, "gradient \"%s\" not found\n", id);
73 return;
74 }
75
76 exc = dom_node_get_node_name(gradient, &id_str);
77 if (exc != DOM_NO_ERR) {
78 dom_node_unref(gradient);
79 return;
80 }
81
82 if (dom_string_isequal(id_str, state->interned_linearGradient))
83 svgtiny_parse_linear_gradient(gradient, state);
84
85 dom_string_unref(id_str);
86 dom_node_unref(gradient);
87 }
88
89
90 /**
91 * Parse a <linearGradient> element node.
92 *
93 * http://www.w3.org/TR/SVG11/pservers#LinearGradients
94 */
95
96 svgtiny_code svgtiny_parse_linear_gradient(dom_element *linear,
97 struct svgtiny_parse_state *state)
98 {
99 unsigned int i = 0;
100 dom_string *attr;
101 dom_exception exc;
102 dom_nodelist *stops;
103
104 exc = dom_element_get_attribute(linear, state->interned_href, &attr);
105 if (exc == DOM_NO_ERR && attr != NULL) {
106 if (dom_string_data(attr)[0] == (uint8_t) '#') {
107 char *s = strndup(dom_string_data(attr) + 1,
108 dom_string_byte_length(attr) - 1);
109 svgtiny_find_gradient(s, state);
110 free(s);
111 }
112 dom_string_unref(attr);
113 }
114
115 exc = dom_element_get_attribute(linear, state->interned_x1, &attr);
116 if (exc == DOM_NO_ERR && attr != NULL) {
117 dom_string_unref(state->gradient_x1);
118 state->gradient_x1 = attr;
119 attr = NULL;
120 }
121
122 exc = dom_element_get_attribute(linear, state->interned_y1, &attr);
123 if (exc == DOM_NO_ERR && attr != NULL) {
124 dom_string_unref(state->gradient_y1);
125 state->gradient_y1 = attr;
126 attr = NULL;
127 }
128
129 exc = dom_element_get_attribute(linear, state->interned_x2, &attr);
130 if (exc == DOM_NO_ERR && attr != NULL) {
131 dom_string_unref(state->gradient_x2);
132 state->gradient_x2 = attr;
133 attr = NULL;
134 }
135
136 exc = dom_element_get_attribute(linear, state->interned_y2, &attr);
137 if (exc == DOM_NO_ERR && attr != NULL) {
138 dom_string_unref(state->gradient_y2);
139 state->gradient_y2 = attr;
140 attr = NULL;
141 }
142
143 exc = dom_element_get_attribute(linear, state->interned_gradientUnits,
144 &attr);
145 if (exc == DOM_NO_ERR && attr != NULL) {
146 state->gradient_user_space_on_use =
147 dom_string_isequal(attr,
148 state->interned_userSpaceOnUse);
149 dom_string_unref(attr);
150 }
151
152 exc = dom_element_get_attribute(linear,
153 state->interned_gradientTransform,
154 &attr);
155 if (exc == DOM_NO_ERR && attr != NULL) {
156 float a = 1, b = 0, c = 0, d = 1, e = 0, f = 0;
157 char *s = strndup(dom_string_data(attr),
158 dom_string_byte_length(attr));
159 if (s == NULL) {
160 dom_string_unref(attr);
161 return svgtiny_OUT_OF_MEMORY;
162 }
163 svgtiny_parse_transform(s, &a, &b, &c, &d, &e, &f);
164 free(s);
165 fprintf(stderr, "transform %g %g %g %g %g %g\n",
166 a, b, c, d, e, f);
167 state->gradient_transform.a = a;
168 state->gradient_transform.b = b;
169 state->gradient_transform.c = c;
170 state->gradient_transform.d = d;
171 state->gradient_transform.e = e;
172 state->gradient_transform.f = f;
173 dom_string_unref(attr);
174 }
175
176 exc = dom_element_get_elements_by_tag_name(linear,
177 state->interned_stop,
178 &stops);
179 if (exc == DOM_NO_ERR && stops != NULL) {
180 uint32_t listlen, stopnr;
181 exc = dom_nodelist_get_length(stops, &listlen);
182 if (exc != DOM_NO_ERR) {
183 dom_nodelist_unref(stops);
184 goto no_more_stops;
185 }
186
187 for (stopnr = 0; stopnr < listlen; ++stopnr) {
188 dom_element *stop;
189 float offset = -1;
190 svgtiny_colour color = svgtiny_TRANSPARENT;
191 exc = dom_nodelist_item(stops, stopnr,
192 (dom_node **) (void *) &stop);
193 if (exc != DOM_NO_ERR)
194 continue;
195 exc = dom_element_get_attribute(stop,
196 state->interned_offset,
197 &attr);
198 if (exc == DOM_NO_ERR && attr != NULL) {
199 char *s = strndup(dom_string_data(attr),
200 dom_string_byte_length(attr));
201 offset = svgtiny_parse_gradient_offset(s);
202 free(s);
203 dom_string_unref(attr);
204 }
205 exc = dom_element_get_attribute(stop,
206 state->interned_stop_color,
207 &attr);
208 if (exc == DOM_NO_ERR && attr != NULL) {
209 svgtiny_parse_color(attr, &color, state);
210 dom_string_unref(attr);
211 }
212 exc = dom_element_get_attribute(stop,
213 state->interned_style,
214 &attr);
215 if (exc == DOM_NO_ERR && attr != NULL) {
216 char *content = strndup(dom_string_data(attr),
217 dom_string_byte_length(attr));
218 const char *s;
219 dom_string *value;
220 if ((s = strstr(content, "stop-color:"))) {
221 s += 11;
222 while (*s == ' ')
223 s++;
224 exc = dom_string_create_interned(
225 (const uint8_t *) s,
226 strcspn(s, "; "),
227 &value);
228 if (exc != DOM_NO_ERR &&
229 value != NULL) {
230 svgtiny_parse_color(value,
231 &color,
232 state);
233 dom_string_unref(value);
234 }
235 }
236 free(content);
237 dom_string_unref(attr);
238 }
239 if (offset != -1 && color != svgtiny_TRANSPARENT) {
240 fprintf(stderr, "stop %g %x\n", offset, color);
241 state->gradient_stop[i].offset = offset;
242 state->gradient_stop[i].color = color;
243 i++;
244 }
245 dom_node_unref(stop);
246 if (i == svgtiny_MAX_STOPS)
247 break;
248 }
249
250 dom_nodelist_unref(stops);
251 }
252 no_more_stops:
253 if (i > 0)
254 state->linear_gradient_stop_count = i;
255
256 return svgtiny_OK;
257 }
258
259
260 float svgtiny_parse_gradient_offset(const char *s)
261 {
262 int num_length = strspn(s, "0123456789+-.");
263 const char *unit = s + num_length;
264 float n = atof((const char *) s);
265
266 if (unit[0] == 0)
267 ;
268 else if (unit[0] == '%')
269 n /= 100.0;
270 else
271 return -1;
272
273 if (n < 0)
274 n = 0;
275 if (1 < n)
276 n = 1;
277 return n;
278 }
279
280
281 /**
282 * Add a path with a linear gradient fill to the svgtiny_diagram.
283 */
284
285 svgtiny_code svgtiny_add_path_linear_gradient(float *p, unsigned int n,
286 struct svgtiny_parse_state *state)
287 {
288 struct grad_point {
289 float x, y, r;
290 };
291 float object_x0, object_y0, object_x1, object_y1;
292 float gradient_x0, gradient_y0, gradient_x1, gradient_y1,
293 gradient_dx, gradient_dy;
294 float trans[6];
295 unsigned int steps = 10;
296 float x0 = 0, y0 = 0, x0_trans, y0_trans, r0; /* segment start point */
297 float x1, y1, x1_trans, y1_trans, r1; /* segment end point */
298 /* segment control points (beziers only) */
299 float c0x = 0, c0y = 0, c1x = 0, c1y = 0;
300 float gradient_norm_squared;
301 struct svgtiny_list *pts;
302 float min_r = 1000;
303 unsigned int min_pt = 0;
304 unsigned int j;
305 unsigned int stop_count;
306 unsigned int current_stop;
307 float last_stop_r;
308 float current_stop_r;
309 int red0, green0, blue0, red1, green1, blue1;
310 unsigned int t, a, b;
311
312 /* determine object bounding box */
313 svgtiny_path_bbox(p, n, &object_x0, &object_y0, &object_x1, &object_y1);
314 #ifdef GRADIENT_DEBUG
315 fprintf(stderr, "object bbox: (%g %g) (%g %g)\n",
316 object_x0, object_y0, object_x1, object_y1);
317 #endif
318
319 if (!state->gradient_user_space_on_use) {
320 gradient_x0 = object_x0 +
321 svgtiny_parse_length(state->gradient_x1,
322 object_x1 - object_x0, *state);
323 gradient_y0 = object_y0 +
324 svgtiny_parse_length(state->gradient_y1,
325 object_y1 - object_y0, *state);
326 gradient_x1 = object_x0 +
327 svgtiny_parse_length(state->gradient_x2,
328 object_x1 - object_x0, *state);
329 gradient_y1 = object_y0 +
330 svgtiny_parse_length(state->gradient_y2,
331 object_y1 - object_y0, *state);
332 } else {
333 gradient_x0 = svgtiny_parse_length(state->gradient_x1,
334 state->viewport_width, *state);
335 gradient_y0 = svgtiny_parse_length(state->gradient_y1,
336 state->viewport_height, *state);
337 gradient_x1 = svgtiny_parse_length(state->gradient_x2,
338 state->viewport_width, *state);
339 gradient_y1 = svgtiny_parse_length(state->gradient_y2,
340 state->viewport_height, *state);
341 }
342 gradient_dx = gradient_x1 - gradient_x0;
343 gradient_dy = gradient_y1 - gradient_y0;
344 #ifdef GRADIENT_DEBUG
345 fprintf(stderr, "gradient vector: (%g %g) => (%g %g)\n",
346 gradient_x0, gradient_y0, gradient_x1, gradient_y1);
347 #endif
348
349 /* show theoretical gradient strips for debugging */
350 /*unsigned int strips = 10;
351 for (unsigned int z = 0; z != strips; z++) {
352 float f0, fd, strip_x0, strip_y0, strip_dx, strip_dy;
353 f0 = (float) z / (float) strips;
354 fd = (float) 1 / (float) strips;
355 strip_x0 = gradient_x0 + f0 * gradient_dx;
356 strip_y0 = gradient_y0 + f0 * gradient_dy;
357 strip_dx = fd * gradient_dx;
358 strip_dy = fd * gradient_dy;
359 fprintf(stderr, "strip %i vector: (%g %g) + (%g %g)\n",
360 z, strip_x0, strip_y0, strip_dx, strip_dy);
361
362 float *p = malloc(13 * sizeof p[0]);
363 if (!p)
364 return svgtiny_OUT_OF_MEMORY;
365 p[0] = svgtiny_PATH_MOVE;
366 p[1] = strip_x0 + (strip_dy * 3);
367 p[2] = strip_y0 - (strip_dx * 3);
368 p[3] = svgtiny_PATH_LINE;
369 p[4] = p[1] + strip_dx;
370 p[5] = p[2] + strip_dy;
371 p[6] = svgtiny_PATH_LINE;
372 p[7] = p[4] - (strip_dy * 6);
373 p[8] = p[5] + (strip_dx * 6);
374 p[9] = svgtiny_PATH_LINE;
375 p[10] = p[7] - strip_dx;
376 p[11] = p[8] - strip_dy;
377 p[12] = svgtiny_PATH_CLOSE;
378 svgtiny_transform_path(p, 13, state);
379 struct svgtiny_shape *shape = svgtiny_add_shape(state);
380 if (!shape) {
381 free(p);
382 return svgtiny_OUT_OF_MEMORY;
383 }
384 shape->path = p;
385 shape->path_length = 13;
386 shape->fill = svgtiny_TRANSPARENT;
387 shape->stroke = svgtiny_RGB(0, 0xff, 0);
388 state->diagram->shape_count++;
389 }*/
390
391 /* invert gradient transform for applying to vertices */
392 svgtiny_invert_matrix(&state->gradient_transform.a, trans);
393 fprintf(stderr, "inverse transform %g %g %g %g %g %g\n",
394 trans[0], trans[1], trans[2], trans[3],
395 trans[4], trans[5]);
396
397 /* compute points on the path for triangle vertices */
398 /* r, r0, r1 are distance along gradient vector */
399 gradient_norm_squared = gradient_dx * gradient_dx +
400 gradient_dy * gradient_dy;
401 pts = svgtiny_list_create(
402 sizeof (struct grad_point));
403 if (!pts)
404 return svgtiny_OUT_OF_MEMORY;
405 for (j = 0; j != n; ) {
406 int segment_type = (int) p[j];
407 struct grad_point *point;
408 unsigned int z;
409
410 if (segment_type == svgtiny_PATH_MOVE) {
411 x0 = p[j + 1];
412 y0 = p[j + 2];
413 j += 3;
414 continue;
415 }
416
417 assert(segment_type == svgtiny_PATH_CLOSE ||
418 segment_type == svgtiny_PATH_LINE ||
419 segment_type == svgtiny_PATH_BEZIER);
420
421 /* start point (x0, y0) */
422 x0_trans = trans[0]*x0 + trans[2]*y0 + trans[4];
423 y0_trans = trans[1]*x0 + trans[3]*y0 + trans[5];
424 r0 = ((x0_trans - gradient_x0) * gradient_dx +
425 (y0_trans - gradient_y0) * gradient_dy) /
426 gradient_norm_squared;
427 point = svgtiny_list_push(pts);
428 if (!point) {
429 svgtiny_list_free(pts);
430 return svgtiny_OUT_OF_MEMORY;
431 }
432 point->x = x0;
433 point->y = y0;
434 point->r = r0;
435 if (r0 < min_r) {
436 min_r = r0;
437 min_pt = svgtiny_list_size(pts) - 1;
438 }
439
440 /* end point (x1, y1) */
441 if (segment_type == svgtiny_PATH_LINE) {
442 x1 = p[j + 1];
443 y1 = p[j + 2];
444 j += 3;
445 } else if (segment_type == svgtiny_PATH_CLOSE) {
446 x1 = p[1];
447 y1 = p[2];
448 j++;
449 } else /* svgtiny_PATH_BEZIER */ {
450 c0x = p[j + 1];
451 c0y = p[j + 2];
452 c1x = p[j + 3];
453 c1y = p[j + 4];
454 x1 = p[j + 5];
455 y1 = p[j + 6];
456 j += 7;
457 }
458 x1_trans = trans[0]*x1 + trans[2]*y1 + trans[4];
459 y1_trans = trans[1]*x1 + trans[3]*y1 + trans[5];
460 r1 = ((x1_trans - gradient_x0) * gradient_dx +
461 (y1_trans - gradient_y0) * gradient_dy) /
462 gradient_norm_squared;
463
464 /* determine steps from change in r */
465
466 if(isnan(r0) || isnan(r1)) {
467 steps = 1;
468 } else {
469 steps = ceilf(fabsf(r1 - r0) / 0.05);
470 }
471
472 if (steps == 0)
473 steps = 1;
474 fprintf(stderr, "r0 %g, r1 %g, steps %i\n",
475 r0, r1, steps);
476
477 /* loop through intermediate points */
478 for (z = 1; z != steps; z++) {
479 float t, x, y, x_trans, y_trans, r;
480 struct grad_point *point;
481 t = (float) z / (float) steps;
482 if (segment_type == svgtiny_PATH_BEZIER) {
483 x = (1-t) * (1-t) * (1-t) * x0 +
484 3 * t * (1-t) * (1-t) * c0x +
485 3 * t * t * (1-t) * c1x +
486 t * t * t * x1;
487 y = (1-t) * (1-t) * (1-t) * y0 +
488 3 * t * (1-t) * (1-t) * c0y +
489 3 * t * t * (1-t) * c1y +
490 t * t * t * y1;
491 } else {
492 x = (1-t) * x0 + t * x1;
493 y = (1-t) * y0 + t * y1;
494 }
495 x_trans = trans[0]*x + trans[2]*y + trans[4];
496 y_trans = trans[1]*x + trans[3]*y + trans[5];
497 r = ((x_trans - gradient_x0) * gradient_dx +
498 (y_trans - gradient_y0) * gradient_dy) /
499 gradient_norm_squared;
500 fprintf(stderr, "(%g %g [%g]) ", x, y, r);
501 point = svgtiny_list_push(pts);
502 if (!point) {
503 svgtiny_list_free(pts);
504 return svgtiny_OUT_OF_MEMORY;
505 }
506 point->x = x;
507 point->y = y;
508 point->r = r;
509 if (r < min_r) {
510 min_r = r;
511 min_pt = svgtiny_list_size(pts) - 1;
512 }
513 }
514 fprintf(stderr, "\n");
515
516 /* next segment start point is this segment end point */
517 x0 = x1;
518 y0 = y1;
519 }
520 fprintf(stderr, "pts size %i, min_pt %i, min_r %.3f\n",
521 svgtiny_list_size(pts), min_pt, min_r);
522
523 /* render triangles */
524 stop_count = state->linear_gradient_stop_count;
525 assert(2 <= stop_count);
526 current_stop = 0;
527 last_stop_r = 0;
528 current_stop_r = state->gradient_stop[0].offset;
529 red0 = red1 = svgtiny_RED(state->gradient_stop[0].color);
530 green0 = green1 = svgtiny_GREEN(state->gradient_stop[0].color);
531 blue0 = blue1 = svgtiny_BLUE(state->gradient_stop[0].color);
532 t = min_pt;
533 a = (min_pt + 1) % svgtiny_list_size(pts);
534 b = min_pt == 0 ? svgtiny_list_size(pts) - 1 : min_pt - 1;
535 while (a != b) {
536 struct grad_point *point_t = svgtiny_list_get(pts, t);
537 struct grad_point *point_a = svgtiny_list_get(pts, a);
538 struct grad_point *point_b = svgtiny_list_get(pts, b);
539 float mean_r = (point_t->r + point_a->r + point_b->r) / 3;
540 float *p;
541 struct svgtiny_shape *shape;
542 /*fprintf(stderr, "triangle: t %i %.3f a %i %.3f b %i %.3f "
543 "mean_r %.3f\n",
544 t, pts[t].r, a, pts[a].r, b, pts[b].r,
545 mean_r);*/
546 while (current_stop != stop_count && current_stop_r < mean_r) {
547 current_stop++;
548 if (current_stop == stop_count)
549 break;
550 red0 = red1;
551 green0 = green1;
552 blue0 = blue1;
553 red1 = svgtiny_RED(state->
554 gradient_stop[current_stop].color);
555 green1 = svgtiny_GREEN(state->
556 gradient_stop[current_stop].color);
557 blue1 = svgtiny_BLUE(state->
558 gradient_stop[current_stop].color);
559 last_stop_r = current_stop_r;
560 current_stop_r = state->
561 gradient_stop[current_stop].offset;
562 }
563 p = malloc(10 * sizeof p[0]);
564 if (!p)
565 return svgtiny_OUT_OF_MEMORY;
566 p[0] = svgtiny_PATH_MOVE;
567 p[1] = point_t->x;
568 p[2] = point_t->y;
569 p[3] = svgtiny_PATH_LINE;
570 p[4] = point_a->x;
571 p[5] = point_a->y;
572 p[6] = svgtiny_PATH_LINE;
573 p[7] = point_b->x;
574 p[8] = point_b->y;
575 p[9] = svgtiny_PATH_CLOSE;
576 svgtiny_transform_path(p, 10, state);
577 shape = svgtiny_add_shape(state);
578 if (!shape) {
579 free(p);
580 return svgtiny_OUT_OF_MEMORY;
581 }
582 shape->path = p;
583 shape->path_length = 10;
584 /*shape->fill = svgtiny_TRANSPARENT;*/
585 if (current_stop == 0)
586 shape->fill = state->gradient_stop[0].color;
587 else if (current_stop == stop_count)
588 shape->fill = state->
589 gradient_stop[stop_count - 1].color;
590 else {
591 float stop_r = (mean_r - last_stop_r) /
592 (current_stop_r - last_stop_r);
593 shape->fill = svgtiny_RGB(
594 (int) ((1 - stop_r) * red0 + stop_r * red1),
595 (int) ((1 - stop_r) * green0 + stop_r * green1),
596 (int) ((1 - stop_r) * blue0 + stop_r * blue1));
597 }
598 shape->stroke = svgtiny_TRANSPARENT;
599 #ifdef GRADIENT_DEBUG
600 shape->stroke = svgtiny_RGB(0, 0, 0xff);
601 #endif
602 state->diagram->shape_count++;
603 if (point_a->r < point_b->r) {
604 t = a;
605 a = (a + 1) % svgtiny_list_size(pts);
606 } else {
607 t = b;
608 b = b == 0 ? svgtiny_list_size(pts) - 1 : b - 1;
609 }
610 }
611
612 /* render gradient vector for debugging */
613 #ifdef GRADIENT_DEBUG
614 {
615 float *p = malloc(7 * sizeof p[0]);
616 if (!p)
617 return svgtiny_OUT_OF_MEMORY;
618 p[0] = svgtiny_PATH_MOVE;
619 p[1] = gradient_x0;
620 p[2] = gradient_y0;
621 p[3] = svgtiny_PATH_LINE;
622 p[4] = gradient_x1;
623 p[5] = gradient_y1;
624 p[6] = svgtiny_PATH_CLOSE;
625 svgtiny_transform_path(p, 7, state);
626 struct svgtiny_shape *shape = svgtiny_add_shape(state);
627 if (!shape) {
628 free(p);
629 return svgtiny_OUT_OF_MEMORY;
630 }
631 shape->path = p;
632 shape->path_length = 7;
633 shape->fill = svgtiny_TRANSPARENT;
634 shape->stroke = svgtiny_RGB(0xff, 0, 0);
635 state->diagram->shape_count++;
636 }
637 #endif
638
639 /* render triangle vertices with r values for debugging */
640 #ifdef GRADIENT_DEBUG
641 for (unsigned int i = 0; i != pts->size; i++) {
642 struct grad_point *point = svgtiny_list_get(pts, i);
643 struct svgtiny_shape *shape = svgtiny_add_shape(state);
644 if (!shape)
645 return svgtiny_OUT_OF_MEMORY;
646 char *text = malloc(20);
647 if (!text)
648 return svgtiny_OUT_OF_MEMORY;
649 sprintf(text, "%i=%.3f", i, point->r);
650 shape->text = text;
651 shape->text_x = state->ctm.a * point->x +
652 state->ctm.c * point->y + state->ctm.e;
653 shape->text_y = state->ctm.b * point->x +
654 state->ctm.d * point->y + state->ctm.f;
655 shape->fill = svgtiny_RGB(0, 0, 0);
656 shape->stroke = svgtiny_TRANSPARENT;
657 state->diagram->shape_count++;
658 }
659 #endif
660
661 /* plot actual path outline */
662 if (state->stroke != svgtiny_TRANSPARENT) {
663 struct svgtiny_shape *shape;
664 svgtiny_transform_path(p, n, state);
665
666 shape = svgtiny_add_shape(state);
667 if (!shape) {
668 free(p);
669 return svgtiny_OUT_OF_MEMORY;
670 }
671 shape->path = p;
672 shape->path_length = n;
673 shape->fill = svgtiny_TRANSPARENT;
674 state->diagram->shape_count++;
675 } else {
676 free(p);
677 }
678
679 svgtiny_list_free(pts);
680
681 return svgtiny_OK;
682 }
683
684
685 /**
686 * Get the bounding box of path.
687 */
688
689 void svgtiny_path_bbox(float *p, unsigned int n,
690 float *x0, float *y0, float *x1, float *y1)
691 {
692 unsigned int j;
693
694 *x0 = *x1 = p[1];
695 *y0 = *y1 = p[2];
696
697 for (j = 0; j != n; ) {
698 unsigned int points = 0;
699 unsigned int k;
700 switch ((int) p[j]) {
701 case svgtiny_PATH_MOVE:
702 case svgtiny_PATH_LINE:
703 points = 1;
704 break;
705 case svgtiny_PATH_CLOSE:
706 points = 0;
707 break;
708 case svgtiny_PATH_BEZIER:
709 points = 3;
710 break;
711 default:
712 assert(0);
713 }
714 j++;
715 for (k = 0; k != points; k++) {
716 float x = p[j], y = p[j + 1];
717 if (x < *x0)
718 *x0 = x;
719 else if (*x1 < x)
720 *x1 = x;
721 if (y < *y0)
722 *y0 = y;
723 else if (*y1 < y)
724 *y1 = y;
725 j += 2;
726 }
727 }
728 }
729
730
731 /**
732 * Invert a transformation matrix.
733 */
734 void svgtiny_invert_matrix(float *m, float *inv)
735 {
736 float determinant = m[0]*m[3] - m[1]*m[2];
737 inv[0] = m[3] / determinant;
738 inv[1] = -m[1] / determinant;
739 inv[2] = -m[2] / determinant;
740 inv[3] = m[0] / determinant;
741 inv[4] = (m[2]*m[5] - m[3]*m[4]) / determinant;
742 inv[5] = (m[1]*m[4] - m[0]*m[5]) / determinant;
743 }
744