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