]> gitweb.michael.orlitzky.com - libsvgtiny.git/blob - src/svgtiny_gradient.c
19f7f1f5cd0e048404cd409789fe722b69b2d191
[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_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_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, &stop);
192 if (exc != DOM_NO_ERR)
193 continue;
194 exc = dom_element_get_attribute(stop,
195 state->interned_offset,
196 &attr);
197 if (exc == DOM_NO_ERR && attr != NULL) {
198 char *s = strndup(dom_string_data(attr),
199 dom_string_length(attr));
200 offset = svgtiny_parse_gradient_offset(s);
201 free(s);
202 dom_string_unref(attr);
203 }
204 exc = dom_element_get_attribute(stop,
205 state->interned_stop_color,
206 &attr);
207 if (exc == DOM_NO_ERR && attr != NULL) {
208 svgtiny_parse_color(attr, &color, state);
209 dom_string_unref(attr);
210 }
211 exc = dom_element_get_attribute(stop,
212 state->interned_style,
213 &attr);
214 if (exc == DOM_NO_ERR && attr != NULL) {
215 char *content = strndup(dom_string_data(attr),
216 dom_string_length(attr));
217 const char *s;
218 dom_string *value;
219 if ((s = strstr(content, "stop-color:"))) {
220 s += 11;
221 while (*s == ' ')
222 s++;
223 exc = dom_string_create_interned(
224 (const uint8_t *) s,
225 strcspn(s, "; "),
226 &value);
227 if (exc != DOM_NO_ERR &&
228 value != NULL) {
229 svgtiny_parse_color(value,
230 &color,
231 state);
232 dom_string_unref(value);
233 }
234 }
235 free(content);
236 dom_string_unref(attr);
237 }
238 if (offset != -1 && color != svgtiny_TRANSPARENT) {
239 fprintf(stderr, "stop %g %x\n", offset, color);
240 state->gradient_stop[i].offset = offset;
241 state->gradient_stop[i].color = color;
242 i++;
243 }
244 dom_node_unref(stop);
245 if (i == svgtiny_MAX_STOPS)
246 break;
247 }
248
249 dom_nodelist_unref(stops);
250 }
251 no_more_stops:
252 if (i > 0)
253 state->linear_gradient_stop_count = i;
254
255 return svgtiny_OK;
256 }
257
258
259 float svgtiny_parse_gradient_offset(const char *s)
260 {
261 int num_length = strspn(s, "0123456789+-.");
262 const char *unit = s + num_length;
263 float n = atof((const char *) s);
264
265 if (unit[0] == 0)
266 ;
267 else if (unit[0] == '%')
268 n /= 100.0;
269 else
270 return -1;
271
272 if (n < 0)
273 n = 0;
274 if (1 < n)
275 n = 1;
276 return n;
277 }
278
279
280 /**
281 * Add a path with a linear gradient fill to the svgtiny_diagram.
282 */
283
284 svgtiny_code svgtiny_add_path_linear_gradient(float *p, unsigned int n,
285 struct svgtiny_parse_state *state)
286 {
287 struct grad_point {
288 float x, y, r;
289 };
290 float object_x0, object_y0, object_x1, object_y1;
291 float gradient_x0, gradient_y0, gradient_x1, gradient_y1,
292 gradient_dx, gradient_dy;
293 float trans[6];
294 unsigned int steps = 10;
295 float x0 = 0, y0 = 0, x0_trans, y0_trans, r0; /* segment start point */
296 float x1, y1, x1_trans, y1_trans, r1; /* segment end point */
297 /* segment control points (beziers only) */
298 float c0x = 0, c0y = 0, c1x = 0, c1y = 0;
299 float gradient_norm_squared;
300 struct svgtiny_list *pts;
301 float min_r = 1000;
302 unsigned int min_pt = 0;
303 unsigned int j;
304 unsigned int stop_count;
305 unsigned int current_stop;
306 float last_stop_r;
307 float current_stop_r;
308 int red0, green0, blue0, red1, green1, blue1;
309 unsigned int t, a, b;
310
311 /* determine object bounding box */
312 svgtiny_path_bbox(p, n, &object_x0, &object_y0, &object_x1, &object_y1);
313 #ifdef GRADIENT_DEBUG
314 fprintf(stderr, "object bbox: (%g %g) (%g %g)\n",
315 object_x0, object_y0, object_x1, object_y1);
316 #endif
317
318 /* compute gradient vector */
319 fprintf(stderr, "x1 %*s, y1 %*s, x2 %*s, y2 %*s\n",
320 dom_string_length(state->gradient_x1),
321 dom_string_data(state->gradient_x1),
322 dom_string_length(state->gradient_y1),
323 dom_string_data(state->gradient_y1),
324 dom_string_length(state->gradient_x2),
325 dom_string_data(state->gradient_x2),
326 dom_string_length(state->gradient_y2),
327 dom_string_data(state->gradient_y2));
328
329 if (!state->gradient_user_space_on_use) {
330 gradient_x0 = object_x0 +
331 svgtiny_parse_length(state->gradient_x1,
332 object_x1 - object_x0, *state);
333 gradient_y0 = object_y0 +
334 svgtiny_parse_length(state->gradient_y1,
335 object_y1 - object_y0, *state);
336 gradient_x1 = object_x0 +
337 svgtiny_parse_length(state->gradient_x2,
338 object_x1 - object_x0, *state);
339 gradient_y1 = object_y0 +
340 svgtiny_parse_length(state->gradient_y2,
341 object_y1 - object_y0, *state);
342 } else {
343 gradient_x0 = svgtiny_parse_length(state->gradient_x1,
344 state->viewport_width, *state);
345 gradient_y0 = svgtiny_parse_length(state->gradient_y1,
346 state->viewport_height, *state);
347 gradient_x1 = svgtiny_parse_length(state->gradient_x2,
348 state->viewport_width, *state);
349 gradient_y1 = svgtiny_parse_length(state->gradient_y2,
350 state->viewport_height, *state);
351 }
352 gradient_dx = gradient_x1 - gradient_x0;
353 gradient_dy = gradient_y1 - gradient_y0;
354 #ifdef GRADIENT_DEBUG
355 fprintf(stderr, "gradient vector: (%g %g) => (%g %g)\n",
356 gradient_x0, gradient_y0, gradient_x1, gradient_y1);
357 #endif
358
359 /* show theoretical gradient strips for debugging */
360 /*unsigned int strips = 10;
361 for (unsigned int z = 0; z != strips; z++) {
362 float f0, fd, strip_x0, strip_y0, strip_dx, strip_dy;
363 f0 = (float) z / (float) strips;
364 fd = (float) 1 / (float) strips;
365 strip_x0 = gradient_x0 + f0 * gradient_dx;
366 strip_y0 = gradient_y0 + f0 * gradient_dy;
367 strip_dx = fd * gradient_dx;
368 strip_dy = fd * gradient_dy;
369 fprintf(stderr, "strip %i vector: (%g %g) + (%g %g)\n",
370 z, strip_x0, strip_y0, strip_dx, strip_dy);
371
372 float *p = malloc(13 * sizeof p[0]);
373 if (!p)
374 return svgtiny_OUT_OF_MEMORY;
375 p[0] = svgtiny_PATH_MOVE;
376 p[1] = strip_x0 + (strip_dy * 3);
377 p[2] = strip_y0 - (strip_dx * 3);
378 p[3] = svgtiny_PATH_LINE;
379 p[4] = p[1] + strip_dx;
380 p[5] = p[2] + strip_dy;
381 p[6] = svgtiny_PATH_LINE;
382 p[7] = p[4] - (strip_dy * 6);
383 p[8] = p[5] + (strip_dx * 6);
384 p[9] = svgtiny_PATH_LINE;
385 p[10] = p[7] - strip_dx;
386 p[11] = p[8] - strip_dy;
387 p[12] = svgtiny_PATH_CLOSE;
388 svgtiny_transform_path(p, 13, state);
389 struct svgtiny_shape *shape = svgtiny_add_shape(state);
390 if (!shape) {
391 free(p);
392 return svgtiny_OUT_OF_MEMORY;
393 }
394 shape->path = p;
395 shape->path_length = 13;
396 shape->fill = svgtiny_TRANSPARENT;
397 shape->stroke = svgtiny_RGB(0, 0xff, 0);
398 state->diagram->shape_count++;
399 }*/
400
401 /* invert gradient transform for applying to vertices */
402 svgtiny_invert_matrix(&state->gradient_transform.a, trans);
403 fprintf(stderr, "inverse transform %g %g %g %g %g %g\n",
404 trans[0], trans[1], trans[2], trans[3],
405 trans[4], trans[5]);
406
407 /* compute points on the path for triangle vertices */
408 /* r, r0, r1 are distance along gradient vector */
409 gradient_norm_squared = gradient_dx * gradient_dx +
410 gradient_dy * gradient_dy;
411 pts = svgtiny_list_create(
412 sizeof (struct grad_point));
413 if (!pts)
414 return svgtiny_OUT_OF_MEMORY;
415 for (j = 0; j != n; ) {
416 int segment_type = (int) p[j];
417 struct grad_point *point;
418 unsigned int z;
419
420 if (segment_type == svgtiny_PATH_MOVE) {
421 x0 = p[j + 1];
422 y0 = p[j + 2];
423 j += 3;
424 continue;
425 }
426
427 assert(segment_type == svgtiny_PATH_CLOSE ||
428 segment_type == svgtiny_PATH_LINE ||
429 segment_type == svgtiny_PATH_BEZIER);
430
431 /* start point (x0, y0) */
432 x0_trans = trans[0]*x0 + trans[2]*y0 + trans[4];
433 y0_trans = trans[1]*x0 + trans[3]*y0 + trans[5];
434 r0 = ((x0_trans - gradient_x0) * gradient_dx +
435 (y0_trans - gradient_y0) * gradient_dy) /
436 gradient_norm_squared;
437 point = svgtiny_list_push(pts);
438 if (!point) {
439 svgtiny_list_free(pts);
440 return svgtiny_OUT_OF_MEMORY;
441 }
442 point->x = x0;
443 point->y = y0;
444 point->r = r0;
445 if (r0 < min_r) {
446 min_r = r0;
447 min_pt = svgtiny_list_size(pts) - 1;
448 }
449
450 /* end point (x1, y1) */
451 if (segment_type == svgtiny_PATH_LINE) {
452 x1 = p[j + 1];
453 y1 = p[j + 2];
454 j += 3;
455 } else if (segment_type == svgtiny_PATH_CLOSE) {
456 x1 = p[1];
457 y1 = p[2];
458 j++;
459 } else /* svgtiny_PATH_BEZIER */ {
460 c0x = p[j + 1];
461 c0y = p[j + 2];
462 c1x = p[j + 3];
463 c1y = p[j + 4];
464 x1 = p[j + 5];
465 y1 = p[j + 6];
466 j += 7;
467 }
468 x1_trans = trans[0]*x1 + trans[2]*y1 + trans[4];
469 y1_trans = trans[1]*x1 + trans[3]*y1 + trans[5];
470 r1 = ((x1_trans - gradient_x0) * gradient_dx +
471 (y1_trans - gradient_y0) * gradient_dy) /
472 gradient_norm_squared;
473
474 /* determine steps from change in r */
475 steps = ceilf(fabsf(r1 - r0) / 0.05);
476 if (steps == 0)
477 steps = 1;
478 fprintf(stderr, "r0 %g, r1 %g, steps %i\n",
479 r0, r1, steps);
480
481 /* loop through intermediate points */
482 for (z = 1; z != steps; z++) {
483 float t, x, y, x_trans, y_trans, r;
484 struct grad_point *point;
485 t = (float) z / (float) steps;
486 if (segment_type == svgtiny_PATH_BEZIER) {
487 x = (1-t) * (1-t) * (1-t) * x0 +
488 3 * t * (1-t) * (1-t) * c0x +
489 3 * t * t * (1-t) * c1x +
490 t * t * t * x1;
491 y = (1-t) * (1-t) * (1-t) * y0 +
492 3 * t * (1-t) * (1-t) * c0y +
493 3 * t * t * (1-t) * c1y +
494 t * t * t * y1;
495 } else {
496 x = (1-t) * x0 + t * x1;
497 y = (1-t) * y0 + t * y1;
498 }
499 x_trans = trans[0]*x + trans[2]*y + trans[4];
500 y_trans = trans[1]*x + trans[3]*y + trans[5];
501 r = ((x_trans - gradient_x0) * gradient_dx +
502 (y_trans - gradient_y0) * gradient_dy) /
503 gradient_norm_squared;
504 fprintf(stderr, "(%g %g [%g]) ", x, y, r);
505 point = svgtiny_list_push(pts);
506 if (!point) {
507 svgtiny_list_free(pts);
508 return svgtiny_OUT_OF_MEMORY;
509 }
510 point->x = x;
511 point->y = y;
512 point->r = r;
513 if (r < min_r) {
514 min_r = r;
515 min_pt = svgtiny_list_size(pts) - 1;
516 }
517 }
518 fprintf(stderr, "\n");
519
520 /* next segment start point is this segment end point */
521 x0 = x1;
522 y0 = y1;
523 }
524 fprintf(stderr, "pts size %i, min_pt %i, min_r %.3f\n",
525 svgtiny_list_size(pts), min_pt, min_r);
526
527 /* render triangles */
528 stop_count = state->linear_gradient_stop_count;
529 assert(2 <= stop_count);
530 current_stop = 0;
531 last_stop_r = 0;
532 current_stop_r = state->gradient_stop[0].offset;
533 red0 = red1 = svgtiny_RED(state->gradient_stop[0].color);
534 green0 = green1 = svgtiny_GREEN(state->gradient_stop[0].color);
535 blue0 = blue1 = svgtiny_BLUE(state->gradient_stop[0].color);
536 t = min_pt;
537 a = (min_pt + 1) % svgtiny_list_size(pts);
538 b = min_pt == 0 ? svgtiny_list_size(pts) - 1 : min_pt - 1;
539 while (a != b) {
540 struct grad_point *point_t = svgtiny_list_get(pts, t);
541 struct grad_point *point_a = svgtiny_list_get(pts, a);
542 struct grad_point *point_b = svgtiny_list_get(pts, b);
543 float mean_r = (point_t->r + point_a->r + point_b->r) / 3;
544 float *p;
545 struct svgtiny_shape *shape;
546 /*fprintf(stderr, "triangle: t %i %.3f a %i %.3f b %i %.3f "
547 "mean_r %.3f\n",
548 t, pts[t].r, a, pts[a].r, b, pts[b].r,
549 mean_r);*/
550 while (current_stop != stop_count && current_stop_r < mean_r) {
551 current_stop++;
552 if (current_stop == stop_count)
553 break;
554 red0 = red1;
555 green0 = green1;
556 blue0 = blue1;
557 red1 = svgtiny_RED(state->
558 gradient_stop[current_stop].color);
559 green1 = svgtiny_GREEN(state->
560 gradient_stop[current_stop].color);
561 blue1 = svgtiny_BLUE(state->
562 gradient_stop[current_stop].color);
563 last_stop_r = current_stop_r;
564 current_stop_r = state->
565 gradient_stop[current_stop].offset;
566 }
567 p = malloc(10 * sizeof p[0]);
568 if (!p)
569 return svgtiny_OUT_OF_MEMORY;
570 p[0] = svgtiny_PATH_MOVE;
571 p[1] = point_t->x;
572 p[2] = point_t->y;
573 p[3] = svgtiny_PATH_LINE;
574 p[4] = point_a->x;
575 p[5] = point_a->y;
576 p[6] = svgtiny_PATH_LINE;
577 p[7] = point_b->x;
578 p[8] = point_b->y;
579 p[9] = svgtiny_PATH_CLOSE;
580 svgtiny_transform_path(p, 10, state);
581 shape = svgtiny_add_shape(state);
582 if (!shape) {
583 free(p);
584 return svgtiny_OUT_OF_MEMORY;
585 }
586 shape->path = p;
587 shape->path_length = 10;
588 /*shape->fill = svgtiny_TRANSPARENT;*/
589 if (current_stop == 0)
590 shape->fill = state->gradient_stop[0].color;
591 else if (current_stop == stop_count)
592 shape->fill = state->
593 gradient_stop[stop_count - 1].color;
594 else {
595 float stop_r = (mean_r - last_stop_r) /
596 (current_stop_r - last_stop_r);
597 shape->fill = svgtiny_RGB(
598 (int) ((1 - stop_r) * red0 + stop_r * red1),
599 (int) ((1 - stop_r) * green0 + stop_r * green1),
600 (int) ((1 - stop_r) * blue0 + stop_r * blue1));
601 }
602 shape->stroke = svgtiny_TRANSPARENT;
603 #ifdef GRADIENT_DEBUG
604 shape->stroke = svgtiny_RGB(0, 0, 0xff);
605 #endif
606 state->diagram->shape_count++;
607 if (point_a->r < point_b->r) {
608 t = a;
609 a = (a + 1) % svgtiny_list_size(pts);
610 } else {
611 t = b;
612 b = b == 0 ? svgtiny_list_size(pts) - 1 : b - 1;
613 }
614 }
615
616 /* render gradient vector for debugging */
617 #ifdef GRADIENT_DEBUG
618 {
619 float *p = malloc(7 * sizeof p[0]);
620 if (!p)
621 return svgtiny_OUT_OF_MEMORY;
622 p[0] = svgtiny_PATH_MOVE;
623 p[1] = gradient_x0;
624 p[2] = gradient_y0;
625 p[3] = svgtiny_PATH_LINE;
626 p[4] = gradient_x1;
627 p[5] = gradient_y1;
628 p[6] = svgtiny_PATH_CLOSE;
629 svgtiny_transform_path(p, 7, state);
630 struct svgtiny_shape *shape = svgtiny_add_shape(state);
631 if (!shape) {
632 free(p);
633 return svgtiny_OUT_OF_MEMORY;
634 }
635 shape->path = p;
636 shape->path_length = 7;
637 shape->fill = svgtiny_TRANSPARENT;
638 shape->stroke = svgtiny_RGB(0xff, 0, 0);
639 state->diagram->shape_count++;
640 }
641 #endif
642
643 /* render triangle vertices with r values for debugging */
644 #ifdef GRADIENT_DEBUG
645 for (unsigned int i = 0; i != pts->size; i++) {
646 struct grad_point *point = svgtiny_list_get(pts, i);
647 struct svgtiny_shape *shape = svgtiny_add_shape(state);
648 if (!shape)
649 return svgtiny_OUT_OF_MEMORY;
650 char *text = malloc(20);
651 if (!text)
652 return svgtiny_OUT_OF_MEMORY;
653 sprintf(text, "%i=%.3f", i, point->r);
654 shape->text = text;
655 shape->text_x = state->ctm.a * point->x +
656 state->ctm.c * point->y + state->ctm.e;
657 shape->text_y = state->ctm.b * point->x +
658 state->ctm.d * point->y + state->ctm.f;
659 shape->fill = svgtiny_RGB(0, 0, 0);
660 shape->stroke = svgtiny_TRANSPARENT;
661 state->diagram->shape_count++;
662 }
663 #endif
664
665 /* plot actual path outline */
666 if (state->stroke != svgtiny_TRANSPARENT) {
667 struct svgtiny_shape *shape;
668 svgtiny_transform_path(p, n, state);
669
670 shape = svgtiny_add_shape(state);
671 if (!shape) {
672 free(p);
673 return svgtiny_OUT_OF_MEMORY;
674 }
675 shape->path = p;
676 shape->path_length = n;
677 shape->fill = svgtiny_TRANSPARENT;
678 state->diagram->shape_count++;
679 } else {
680 free(p);
681 }
682
683 svgtiny_list_free(pts);
684
685 return svgtiny_OK;
686 }
687
688
689 /**
690 * Get the bounding box of path.
691 */
692
693 void svgtiny_path_bbox(float *p, unsigned int n,
694 float *x0, float *y0, float *x1, float *y1)
695 {
696 unsigned int j;
697
698 *x0 = *x1 = p[1];
699 *y0 = *y1 = p[2];
700
701 for (j = 0; j != n; ) {
702 unsigned int points = 0;
703 unsigned int k;
704 switch ((int) p[j]) {
705 case svgtiny_PATH_MOVE:
706 case svgtiny_PATH_LINE:
707 points = 1;
708 break;
709 case svgtiny_PATH_CLOSE:
710 points = 0;
711 break;
712 case svgtiny_PATH_BEZIER:
713 points = 3;
714 break;
715 default:
716 assert(0);
717 }
718 j++;
719 for (k = 0; k != points; k++) {
720 float x = p[j], y = p[j + 1];
721 if (x < *x0)
722 *x0 = x;
723 else if (*x1 < x)
724 *x1 = x;
725 if (y < *y0)
726 *y0 = y;
727 else if (*y1 < y)
728 *y1 = y;
729 j += 2;
730 }
731 }
732 }
733
734
735 /**
736 * Invert a transformation matrix.
737 */
738 void svgtiny_invert_matrix(float *m, float *inv)
739 {
740 float determinant = m[0]*m[3] - m[1]*m[2];
741 inv[0] = m[3] / determinant;
742 inv[1] = -m[1] / determinant;
743 inv[2] = -m[2] / determinant;
744 inv[3] = m[0] / determinant;
745 inv[4] = (m[2]*m[5] - m[3]*m[4]) / determinant;
746 inv[5] = (m[1]*m[4] - m[0]*m[5]) / determinant;
747 }
748