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