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