]> gitweb.michael.orlitzky.com - libsvgtiny.git/blob - src/svgtiny_gradient.c
Fix to use list_size accessor.
[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(
414 sizeof (struct grad_point));
415 if (!pts)
416 return svgtiny_OUT_OF_MEMORY;
417 for (j = 0; j != n; ) {
418 int segment_type = (int) p[j];
419 struct grad_point *point;
420 unsigned int z;
421
422 if (segment_type == svgtiny_PATH_MOVE) {
423 x0 = p[j + 1];
424 y0 = p[j + 2];
425 j += 3;
426 continue;
427 }
428
429 assert(segment_type == svgtiny_PATH_CLOSE ||
430 segment_type == svgtiny_PATH_LINE ||
431 segment_type == svgtiny_PATH_BEZIER);
432
433 /* start point (x0, y0) */
434 x0_trans = trans[0]*x0 + trans[2]*y0 + trans[4];
435 y0_trans = trans[1]*x0 + trans[3]*y0 + trans[5];
436 r0 = ((x0_trans - gradient_x0) * gradient_dx +
437 (y0_trans - gradient_y0) * gradient_dy) /
438 gradient_norm_squared;
439 point = svgtiny_list_push(pts);
440 if (!point) {
441 svgtiny_list_free(pts);
442 return svgtiny_OUT_OF_MEMORY;
443 }
444 point->x = x0;
445 point->y = y0;
446 point->r = r0;
447 if (r0 < min_r) {
448 min_r = r0;
449 min_pt = svgtiny_list_size(pts) - 1;
450 }
451
452 /* end point (x1, y1) */
453 if (segment_type == svgtiny_PATH_LINE) {
454 x1 = p[j + 1];
455 y1 = p[j + 2];
456 j += 3;
457 } else if (segment_type == svgtiny_PATH_CLOSE) {
458 x1 = p[1];
459 y1 = p[2];
460 j++;
461 } else /* svgtiny_PATH_BEZIER */ {
462 c0x = p[j + 1];
463 c0y = p[j + 2];
464 c1x = p[j + 3];
465 c1y = p[j + 4];
466 x1 = p[j + 5];
467 y1 = p[j + 6];
468 j += 7;
469 }
470 x1_trans = trans[0]*x1 + trans[2]*y1 + trans[4];
471 y1_trans = trans[1]*x1 + trans[3]*y1 + trans[5];
472 r1 = ((x1_trans - gradient_x0) * gradient_dx +
473 (y1_trans - gradient_y0) * gradient_dy) /
474 gradient_norm_squared;
475
476 /* determine steps from change in r */
477
478 if(isnan(r0) || isnan(r1)) {
479 steps = 1;
480 } else {
481 steps = ceilf(fabsf(r1 - r0) / 0.05);
482 }
483
484 if (steps == 0)
485 steps = 1;
486 #ifdef GRADIENT_DEBUG
487 fprintf(stderr, "r0 %g, r1 %g, steps %i\n",
488 r0, r1, steps);
489 #endif
490
491 /* loop through intermediate points */
492 for (z = 1; z != steps; z++) {
493 float t, x, y, x_trans, y_trans, r;
494 struct grad_point *point;
495 t = (float) z / (float) steps;
496 if (segment_type == svgtiny_PATH_BEZIER) {
497 x = (1-t) * (1-t) * (1-t) * x0 +
498 3 * t * (1-t) * (1-t) * c0x +
499 3 * t * t * (1-t) * c1x +
500 t * t * t * x1;
501 y = (1-t) * (1-t) * (1-t) * y0 +
502 3 * t * (1-t) * (1-t) * c0y +
503 3 * t * t * (1-t) * c1y +
504 t * t * t * y1;
505 } else {
506 x = (1-t) * x0 + t * x1;
507 y = (1-t) * y0 + t * y1;
508 }
509 x_trans = trans[0]*x + trans[2]*y + trans[4];
510 y_trans = trans[1]*x + trans[3]*y + trans[5];
511 r = ((x_trans - gradient_x0) * gradient_dx +
512 (y_trans - gradient_y0) * gradient_dy) /
513 gradient_norm_squared;
514 #ifdef GRADIENT_DEBUG
515 fprintf(stderr, "(%g %g [%g]) ", x, y, r);
516 #endif
517 point = svgtiny_list_push(pts);
518 if (!point) {
519 svgtiny_list_free(pts);
520 return svgtiny_OUT_OF_MEMORY;
521 }
522 point->x = x;
523 point->y = y;
524 point->r = r;
525 if (r < min_r) {
526 min_r = r;
527 min_pt = svgtiny_list_size(pts) - 1;
528 }
529 }
530 #ifdef GRADIENT_DEBUG
531 fprintf(stderr, "\n");
532 #endif
533
534 /* next segment start point is this segment end point */
535 x0 = x1;
536 y0 = y1;
537 }
538 #ifdef GRADIENT_DEBUG
539 fprintf(stderr, "pts size %i, min_pt %i, min_r %.3f\n",
540 svgtiny_list_size(pts), min_pt, min_r);
541 #endif
542
543 /* render triangles */
544 stop_count = state->linear_gradient_stop_count;
545 assert(2 <= stop_count);
546 current_stop = 0;
547 last_stop_r = 0;
548 current_stop_r = state->gradient_stop[0].offset;
549 red0 = red1 = svgtiny_RED(state->gradient_stop[0].color);
550 green0 = green1 = svgtiny_GREEN(state->gradient_stop[0].color);
551 blue0 = blue1 = svgtiny_BLUE(state->gradient_stop[0].color);
552 t = min_pt;
553 a = (min_pt + 1) % svgtiny_list_size(pts);
554 b = min_pt == 0 ? svgtiny_list_size(pts) - 1 : min_pt - 1;
555 while (a != b) {
556 struct grad_point *point_t = svgtiny_list_get(pts, t);
557 struct grad_point *point_a = svgtiny_list_get(pts, a);
558 struct grad_point *point_b = svgtiny_list_get(pts, b);
559 float mean_r = (point_t->r + point_a->r + point_b->r) / 3;
560 float *p;
561 struct svgtiny_shape *shape;
562 /*fprintf(stderr, "triangle: t %i %.3f a %i %.3f b %i %.3f "
563 "mean_r %.3f\n",
564 t, pts[t].r, a, pts[a].r, b, pts[b].r,
565 mean_r);*/
566 while (current_stop != stop_count && current_stop_r < mean_r) {
567 current_stop++;
568 if (current_stop == stop_count)
569 break;
570 red0 = red1;
571 green0 = green1;
572 blue0 = blue1;
573 red1 = svgtiny_RED(state->
574 gradient_stop[current_stop].color);
575 green1 = svgtiny_GREEN(state->
576 gradient_stop[current_stop].color);
577 blue1 = svgtiny_BLUE(state->
578 gradient_stop[current_stop].color);
579 last_stop_r = current_stop_r;
580 current_stop_r = state->
581 gradient_stop[current_stop].offset;
582 }
583 p = malloc(10 * sizeof p[0]);
584 if (!p)
585 return svgtiny_OUT_OF_MEMORY;
586 p[0] = svgtiny_PATH_MOVE;
587 p[1] = point_t->x;
588 p[2] = point_t->y;
589 p[3] = svgtiny_PATH_LINE;
590 p[4] = point_a->x;
591 p[5] = point_a->y;
592 p[6] = svgtiny_PATH_LINE;
593 p[7] = point_b->x;
594 p[8] = point_b->y;
595 p[9] = svgtiny_PATH_CLOSE;
596 svgtiny_transform_path(p, 10, state);
597 shape = svgtiny_add_shape(state);
598 if (!shape) {
599 free(p);
600 return svgtiny_OUT_OF_MEMORY;
601 }
602 shape->path = p;
603 shape->path_length = 10;
604 /*shape->fill = svgtiny_TRANSPARENT;*/
605 if (current_stop == 0)
606 shape->fill = state->gradient_stop[0].color;
607 else if (current_stop == stop_count)
608 shape->fill = state->
609 gradient_stop[stop_count - 1].color;
610 else {
611 float stop_r = (mean_r - last_stop_r) /
612 (current_stop_r - last_stop_r);
613 shape->fill = svgtiny_RGB(
614 (int) ((1 - stop_r) * red0 + stop_r * red1),
615 (int) ((1 - stop_r) * green0 + stop_r * green1),
616 (int) ((1 - stop_r) * blue0 + stop_r * blue1));
617 }
618 shape->stroke = svgtiny_TRANSPARENT;
619 #ifdef GRADIENT_DEBUG
620 shape->stroke = svgtiny_RGB(0, 0, 0xff);
621 #endif
622 state->diagram->shape_count++;
623 if (point_a->r < point_b->r) {
624 t = a;
625 a = (a + 1) % svgtiny_list_size(pts);
626 } else {
627 t = b;
628 b = b == 0 ? svgtiny_list_size(pts) - 1 : b - 1;
629 }
630 }
631
632 /* render gradient vector for debugging */
633 #ifdef GRADIENT_DEBUG
634 {
635 float *p = malloc(7 * sizeof p[0]);
636 if (!p)
637 return svgtiny_OUT_OF_MEMORY;
638 p[0] = svgtiny_PATH_MOVE;
639 p[1] = gradient_x0;
640 p[2] = gradient_y0;
641 p[3] = svgtiny_PATH_LINE;
642 p[4] = gradient_x1;
643 p[5] = gradient_y1;
644 p[6] = svgtiny_PATH_CLOSE;
645 svgtiny_transform_path(p, 7, state);
646 struct svgtiny_shape *shape = svgtiny_add_shape(state);
647 if (!shape) {
648 free(p);
649 return svgtiny_OUT_OF_MEMORY;
650 }
651 shape->path = p;
652 shape->path_length = 7;
653 shape->fill = svgtiny_TRANSPARENT;
654 shape->stroke = svgtiny_RGB(0xff, 0, 0);
655 state->diagram->shape_count++;
656 }
657 #endif
658
659 /* render triangle vertices with r values for debugging */
660 #ifdef GRADIENT_DEBUG
661 for (unsigned int i = 0; i != svgtiny_list_size(pts); i++) {
662 struct grad_point *point = svgtiny_list_get(pts, i);
663 struct svgtiny_shape *shape = svgtiny_add_shape(state);
664 if (!shape)
665 return svgtiny_OUT_OF_MEMORY;
666 char *text = malloc(20);
667 if (!text)
668 return svgtiny_OUT_OF_MEMORY;
669 sprintf(text, "%i=%.3f", i, point->r);
670 shape->text = text;
671 shape->text_x = state->ctm.a * point->x +
672 state->ctm.c * point->y + state->ctm.e;
673 shape->text_y = state->ctm.b * point->x +
674 state->ctm.d * point->y + state->ctm.f;
675 shape->fill = svgtiny_RGB(0, 0, 0);
676 shape->stroke = svgtiny_TRANSPARENT;
677 state->diagram->shape_count++;
678 }
679 #endif
680
681 /* plot actual path outline */
682 if (state->stroke != svgtiny_TRANSPARENT) {
683 struct svgtiny_shape *shape;
684 svgtiny_transform_path(p, n, state);
685
686 shape = svgtiny_add_shape(state);
687 if (!shape) {
688 free(p);
689 return svgtiny_OUT_OF_MEMORY;
690 }
691 shape->path = p;
692 shape->path_length = n;
693 shape->fill = svgtiny_TRANSPARENT;
694 state->diagram->shape_count++;
695 } else {
696 free(p);
697 }
698
699 svgtiny_list_free(pts);
700
701 return svgtiny_OK;
702 }
703
704
705 /**
706 * Get the bounding box of path.
707 */
708
709 void svgtiny_path_bbox(float *p, unsigned int n,
710 float *x0, float *y0, float *x1, float *y1)
711 {
712 unsigned int j;
713
714 *x0 = *x1 = p[1];
715 *y0 = *y1 = p[2];
716
717 for (j = 0; j != n; ) {
718 unsigned int points = 0;
719 unsigned int k;
720 switch ((int) p[j]) {
721 case svgtiny_PATH_MOVE:
722 case svgtiny_PATH_LINE:
723 points = 1;
724 break;
725 case svgtiny_PATH_CLOSE:
726 points = 0;
727 break;
728 case svgtiny_PATH_BEZIER:
729 points = 3;
730 break;
731 default:
732 assert(0);
733 }
734 j++;
735 for (k = 0; k != points; k++) {
736 float x = p[j], y = p[j + 1];
737 if (x < *x0)
738 *x0 = x;
739 else if (*x1 < x)
740 *x1 = x;
741 if (y < *y0)
742 *y0 = y;
743 else if (*y1 < y)
744 *y1 = y;
745 j += 2;
746 }
747 }
748 }
749
750
751 /**
752 * Invert a transformation matrix.
753 */
754 void svgtiny_invert_matrix(float *m, float *inv)
755 {
756 float determinant = m[0]*m[3] - m[1]*m[2];
757 inv[0] = m[3] / determinant;
758 inv[1] = -m[1] / determinant;
759 inv[2] = -m[2] / determinant;
760 inv[3] = m[0] / determinant;
761 inv[4] = (m[2]*m[5] - m[3]*m[4]) / determinant;
762 inv[5] = (m[1]*m[4] - m[0]*m[5]) / determinant;
763 }
764