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>
15 #include "svgtiny_internal.h"
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
);
28 * Find a gradient by id and parse it.
31 void svgtiny_find_gradient(const char *id
, struct svgtiny_parse_state
*state
)
33 dom_element
*gradient
;
37 fprintf(stderr
, "svgtiny_find_gradient: id \"%s\"\n", id
);
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;
60 exc
= dom_string_create_interned((const uint8_t *) id
, strlen(id
),
62 if (exc
!= DOM_NO_ERR
)
65 exc
= dom_document_get_element_by_id(state
->document
, id_str
,
67 dom_string_unref(id_str
);
68 if (exc
!= DOM_NO_ERR
)
71 if (gradient
== NULL
) {
72 fprintf(stderr
, "gradient \"%s\" not found\n", id
);
76 exc
= dom_node_get_node_name(gradient
, &id_str
);
77 if (exc
!= DOM_NO_ERR
) {
78 dom_node_unref(gradient
);
82 if (dom_string_isequal(id_str
, state
->interned_linearGradient
))
83 svgtiny_parse_linear_gradient(gradient
, state
);
85 dom_string_unref(id_str
);
86 dom_node_unref(gradient
);
91 * Parse a <linearGradient> element node.
93 * http://www.w3.org/TR/SVG11/pservers#LinearGradients
96 svgtiny_code
svgtiny_parse_linear_gradient(dom_element
*linear
,
97 struct svgtiny_parse_state
*state
)
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_byte_length(attr
) - 1);
109 svgtiny_find_gradient(s
, state
);
112 dom_string_unref(attr
);
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
;
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
;
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
;
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
;
143 exc
= dom_element_get_attribute(linear
, state
->interned_gradientUnits
,
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
);
152 exc
= dom_element_get_attribute(linear
,
153 state
->interned_gradientTransform
,
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_byte_length(attr
));
160 dom_string_unref(attr
);
161 return svgtiny_OUT_OF_MEMORY
;
163 svgtiny_parse_transform(s
, &a
, &b
, &c
, &d
, &e
, &f
);
165 fprintf(stderr
, "transform %g %g %g %g %g %g\n",
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
);
176 exc
= dom_element_get_elements_by_tag_name(linear
,
177 state
->interned_stop
,
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
);
187 for (stopnr
= 0; stopnr
< listlen
; ++stopnr
) {
190 svgtiny_colour color
= svgtiny_TRANSPARENT
;
191 exc
= dom_nodelist_item(stops
, stopnr
,
192 (dom_node
**) (void *) &stop
);
193 if (exc
!= DOM_NO_ERR
)
195 exc
= dom_element_get_attribute(stop
,
196 state
->interned_offset
,
198 if (exc
== DOM_NO_ERR
&& attr
!= NULL
) {
199 char *s
= strndup(dom_string_data(attr
),
200 dom_string_byte_length(attr
));
201 offset
= svgtiny_parse_gradient_offset(s
);
203 dom_string_unref(attr
);
205 exc
= dom_element_get_attribute(stop
,
206 state
->interned_stop_color
,
208 if (exc
== DOM_NO_ERR
&& attr
!= NULL
) {
209 svgtiny_parse_color(attr
, &color
, state
);
210 dom_string_unref(attr
);
212 exc
= dom_element_get_attribute(stop
,
213 state
->interned_style
,
215 if (exc
== DOM_NO_ERR
&& attr
!= NULL
) {
216 char *content
= strndup(dom_string_data(attr
),
217 dom_string_byte_length(attr
));
220 if ((s
= strstr(content
, "stop-color:"))) {
224 exc
= dom_string_create_interned(
228 if (exc
!= DOM_NO_ERR
&&
230 svgtiny_parse_color(value
,
233 dom_string_unref(value
);
237 dom_string_unref(attr
);
239 if (offset
!= -1 && color
!= svgtiny_TRANSPARENT
) {
240 fprintf(stderr
, "stop %g %x\n", offset
, color
);
241 state
->gradient_stop
[i
].offset
= offset
;
242 state
->gradient_stop
[i
].color
= color
;
245 dom_node_unref(stop
);
246 if (i
== svgtiny_MAX_STOPS
)
250 dom_nodelist_unref(stops
);
254 state
->linear_gradient_stop_count
= i
;
260 float svgtiny_parse_gradient_offset(const char *s
)
262 int num_length
= strspn(s
, "0123456789+-.");
263 const char *unit
= s
+ num_length
;
264 float n
= atof((const char *) s
);
268 else if (unit
[0] == '%')
282 * Add a path with a linear gradient fill to the svgtiny_diagram.
285 svgtiny_code
svgtiny_add_path_linear_gradient(float *p
, unsigned int n
,
286 struct svgtiny_parse_state
*state
)
291 float object_x0
, object_y0
, object_x1
, object_y1
;
292 float gradient_x0
, gradient_y0
, gradient_x1
, gradient_y1
,
293 gradient_dx
, gradient_dy
;
295 unsigned int steps
= 10;
296 float x0
= 0, y0
= 0, x0_trans
, y0_trans
, r0
; /* segment start point */
297 float x1
, y1
, x1_trans
, y1_trans
, r1
; /* segment end point */
298 /* segment control points (beziers only) */
299 float c0x
= 0, c0y
= 0, c1x
= 0, c1y
= 0;
300 float gradient_norm_squared
;
301 struct svgtiny_list
*pts
;
303 unsigned int min_pt
= 0;
305 unsigned int stop_count
;
306 unsigned int current_stop
;
308 float current_stop_r
;
309 int red0
, green0
, blue0
, red1
, green1
, blue1
;
310 unsigned int t
, a
, b
;
312 /* determine object bounding box */
313 svgtiny_path_bbox(p
, n
, &object_x0
, &object_y0
, &object_x1
, &object_y1
);
314 #ifdef GRADIENT_DEBUG
315 fprintf(stderr
, "object bbox: (%g %g) (%g %g)\n",
316 object_x0
, object_y0
, object_x1
, object_y1
);
319 if (!state
->gradient_user_space_on_use
) {
320 gradient_x0
= object_x0
+
321 svgtiny_parse_length(state
->gradient_x1
,
322 object_x1
- object_x0
, *state
);
323 gradient_y0
= object_y0
+
324 svgtiny_parse_length(state
->gradient_y1
,
325 object_y1
- object_y0
, *state
);
326 gradient_x1
= object_x0
+
327 svgtiny_parse_length(state
->gradient_x2
,
328 object_x1
- object_x0
, *state
);
329 gradient_y1
= object_y0
+
330 svgtiny_parse_length(state
->gradient_y2
,
331 object_y1
- object_y0
, *state
);
333 gradient_x0
= svgtiny_parse_length(state
->gradient_x1
,
334 state
->viewport_width
, *state
);
335 gradient_y0
= svgtiny_parse_length(state
->gradient_y1
,
336 state
->viewport_height
, *state
);
337 gradient_x1
= svgtiny_parse_length(state
->gradient_x2
,
338 state
->viewport_width
, *state
);
339 gradient_y1
= svgtiny_parse_length(state
->gradient_y2
,
340 state
->viewport_height
, *state
);
342 gradient_dx
= gradient_x1
- gradient_x0
;
343 gradient_dy
= gradient_y1
- gradient_y0
;
344 #ifdef GRADIENT_DEBUG
345 fprintf(stderr
, "gradient vector: (%g %g) => (%g %g)\n",
346 gradient_x0
, gradient_y0
, gradient_x1
, gradient_y1
);
349 /* show theoretical gradient strips for debugging */
350 /*unsigned int strips = 10;
351 for (unsigned int z = 0; z != strips; z++) {
352 float f0, fd, strip_x0, strip_y0, strip_dx, strip_dy;
353 f0 = (float) z / (float) strips;
354 fd = (float) 1 / (float) strips;
355 strip_x0 = gradient_x0 + f0 * gradient_dx;
356 strip_y0 = gradient_y0 + f0 * gradient_dy;
357 strip_dx = fd * gradient_dx;
358 strip_dy = fd * gradient_dy;
359 fprintf(stderr, "strip %i vector: (%g %g) + (%g %g)\n",
360 z, strip_x0, strip_y0, strip_dx, strip_dy);
362 float *p = malloc(13 * sizeof p[0]);
364 return svgtiny_OUT_OF_MEMORY;
365 p[0] = svgtiny_PATH_MOVE;
366 p[1] = strip_x0 + (strip_dy * 3);
367 p[2] = strip_y0 - (strip_dx * 3);
368 p[3] = svgtiny_PATH_LINE;
369 p[4] = p[1] + strip_dx;
370 p[5] = p[2] + strip_dy;
371 p[6] = svgtiny_PATH_LINE;
372 p[7] = p[4] - (strip_dy * 6);
373 p[8] = p[5] + (strip_dx * 6);
374 p[9] = svgtiny_PATH_LINE;
375 p[10] = p[7] - strip_dx;
376 p[11] = p[8] - strip_dy;
377 p[12] = svgtiny_PATH_CLOSE;
378 svgtiny_transform_path(p, 13, state);
379 struct svgtiny_shape *shape = svgtiny_add_shape(state);
382 return svgtiny_OUT_OF_MEMORY;
385 shape->path_length = 13;
386 shape->fill = svgtiny_TRANSPARENT;
387 shape->stroke = svgtiny_RGB(0, 0xff, 0);
388 state->diagram->shape_count++;
391 /* invert gradient transform for applying to vertices */
392 svgtiny_invert_matrix(&state
->gradient_transform
.a
, trans
);
393 fprintf(stderr
, "inverse transform %g %g %g %g %g %g\n",
394 trans
[0], trans
[1], trans
[2], trans
[3],
397 /* compute points on the path for triangle vertices */
398 /* r, r0, r1 are distance along gradient vector */
399 gradient_norm_squared
= gradient_dx
* gradient_dx
+
400 gradient_dy
* gradient_dy
;
401 pts
= svgtiny_list_create(
402 sizeof (struct grad_point
));
404 return svgtiny_OUT_OF_MEMORY
;
405 for (j
= 0; j
!= n
; ) {
406 int segment_type
= (int) p
[j
];
407 struct grad_point
*point
;
410 if (segment_type
== svgtiny_PATH_MOVE
) {
417 assert(segment_type
== svgtiny_PATH_CLOSE
||
418 segment_type
== svgtiny_PATH_LINE
||
419 segment_type
== svgtiny_PATH_BEZIER
);
421 /* start point (x0, y0) */
422 x0_trans
= trans
[0]*x0
+ trans
[2]*y0
+ trans
[4];
423 y0_trans
= trans
[1]*x0
+ trans
[3]*y0
+ trans
[5];
424 r0
= ((x0_trans
- gradient_x0
) * gradient_dx
+
425 (y0_trans
- gradient_y0
) * gradient_dy
) /
426 gradient_norm_squared
;
427 point
= svgtiny_list_push(pts
);
429 svgtiny_list_free(pts
);
430 return svgtiny_OUT_OF_MEMORY
;
437 min_pt
= svgtiny_list_size(pts
) - 1;
440 /* end point (x1, y1) */
441 if (segment_type
== svgtiny_PATH_LINE
) {
445 } else if (segment_type
== svgtiny_PATH_CLOSE
) {
449 } else /* svgtiny_PATH_BEZIER */ {
458 x1_trans
= trans
[0]*x1
+ trans
[2]*y1
+ trans
[4];
459 y1_trans
= trans
[1]*x1
+ trans
[3]*y1
+ trans
[5];
460 r1
= ((x1_trans
- gradient_x0
) * gradient_dx
+
461 (y1_trans
- gradient_y0
) * gradient_dy
) /
462 gradient_norm_squared
;
464 /* determine steps from change in r */
465 steps
= ceilf(fabsf(r1
- r0
) / 0.05);
468 fprintf(stderr
, "r0 %g, r1 %g, steps %i\n",
471 /* loop through intermediate points */
472 for (z
= 1; z
!= steps
; z
++) {
473 float t
, x
, y
, x_trans
, y_trans
, r
;
474 struct grad_point
*point
;
475 t
= (float) z
/ (float) steps
;
476 if (segment_type
== svgtiny_PATH_BEZIER
) {
477 x
= (1-t
) * (1-t
) * (1-t
) * x0
+
478 3 * t
* (1-t
) * (1-t
) * c0x
+
479 3 * t
* t
* (1-t
) * c1x
+
481 y
= (1-t
) * (1-t
) * (1-t
) * y0
+
482 3 * t
* (1-t
) * (1-t
) * c0y
+
483 3 * t
* t
* (1-t
) * c1y
+
486 x
= (1-t
) * x0
+ t
* x1
;
487 y
= (1-t
) * y0
+ t
* y1
;
489 x_trans
= trans
[0]*x
+ trans
[2]*y
+ trans
[4];
490 y_trans
= trans
[1]*x
+ trans
[3]*y
+ trans
[5];
491 r
= ((x_trans
- gradient_x0
) * gradient_dx
+
492 (y_trans
- gradient_y0
) * gradient_dy
) /
493 gradient_norm_squared
;
494 fprintf(stderr
, "(%g %g [%g]) ", x
, y
, r
);
495 point
= svgtiny_list_push(pts
);
497 svgtiny_list_free(pts
);
498 return svgtiny_OUT_OF_MEMORY
;
505 min_pt
= svgtiny_list_size(pts
) - 1;
508 fprintf(stderr
, "\n");
510 /* next segment start point is this segment end point */
514 fprintf(stderr
, "pts size %i, min_pt %i, min_r %.3f\n",
515 svgtiny_list_size(pts
), min_pt
, min_r
);
517 /* render triangles */
518 stop_count
= state
->linear_gradient_stop_count
;
519 assert(2 <= stop_count
);
522 current_stop_r
= state
->gradient_stop
[0].offset
;
523 red0
= red1
= svgtiny_RED(state
->gradient_stop
[0].color
);
524 green0
= green1
= svgtiny_GREEN(state
->gradient_stop
[0].color
);
525 blue0
= blue1
= svgtiny_BLUE(state
->gradient_stop
[0].color
);
527 a
= (min_pt
+ 1) % svgtiny_list_size(pts
);
528 b
= min_pt
== 0 ? svgtiny_list_size(pts
) - 1 : min_pt
- 1;
530 struct grad_point
*point_t
= svgtiny_list_get(pts
, t
);
531 struct grad_point
*point_a
= svgtiny_list_get(pts
, a
);
532 struct grad_point
*point_b
= svgtiny_list_get(pts
, b
);
533 float mean_r
= (point_t
->r
+ point_a
->r
+ point_b
->r
) / 3;
535 struct svgtiny_shape
*shape
;
536 /*fprintf(stderr, "triangle: t %i %.3f a %i %.3f b %i %.3f "
538 t, pts[t].r, a, pts[a].r, b, pts[b].r,
540 while (current_stop
!= stop_count
&& current_stop_r
< mean_r
) {
542 if (current_stop
== stop_count
)
547 red1
= svgtiny_RED(state
->
548 gradient_stop
[current_stop
].color
);
549 green1
= svgtiny_GREEN(state
->
550 gradient_stop
[current_stop
].color
);
551 blue1
= svgtiny_BLUE(state
->
552 gradient_stop
[current_stop
].color
);
553 last_stop_r
= current_stop_r
;
554 current_stop_r
= state
->
555 gradient_stop
[current_stop
].offset
;
557 p
= malloc(10 * sizeof p
[0]);
559 return svgtiny_OUT_OF_MEMORY
;
560 p
[0] = svgtiny_PATH_MOVE
;
563 p
[3] = svgtiny_PATH_LINE
;
566 p
[6] = svgtiny_PATH_LINE
;
569 p
[9] = svgtiny_PATH_CLOSE
;
570 svgtiny_transform_path(p
, 10, state
);
571 shape
= svgtiny_add_shape(state
);
574 return svgtiny_OUT_OF_MEMORY
;
577 shape
->path_length
= 10;
578 /*shape->fill = svgtiny_TRANSPARENT;*/
579 if (current_stop
== 0)
580 shape
->fill
= state
->gradient_stop
[0].color
;
581 else if (current_stop
== stop_count
)
582 shape
->fill
= state
->
583 gradient_stop
[stop_count
- 1].color
;
585 float stop_r
= (mean_r
- last_stop_r
) /
586 (current_stop_r
- last_stop_r
);
587 shape
->fill
= svgtiny_RGB(
588 (int) ((1 - stop_r
) * red0
+ stop_r
* red1
),
589 (int) ((1 - stop_r
) * green0
+ stop_r
* green1
),
590 (int) ((1 - stop_r
) * blue0
+ stop_r
* blue1
));
592 shape
->stroke
= svgtiny_TRANSPARENT
;
593 #ifdef GRADIENT_DEBUG
594 shape
->stroke
= svgtiny_RGB(0, 0, 0xff);
596 state
->diagram
->shape_count
++;
597 if (point_a
->r
< point_b
->r
) {
599 a
= (a
+ 1) % svgtiny_list_size(pts
);
602 b
= b
== 0 ? svgtiny_list_size(pts
) - 1 : b
- 1;
606 /* render gradient vector for debugging */
607 #ifdef GRADIENT_DEBUG
609 float *p
= malloc(7 * sizeof p
[0]);
611 return svgtiny_OUT_OF_MEMORY
;
612 p
[0] = svgtiny_PATH_MOVE
;
615 p
[3] = svgtiny_PATH_LINE
;
618 p
[6] = svgtiny_PATH_CLOSE
;
619 svgtiny_transform_path(p
, 7, state
);
620 struct svgtiny_shape
*shape
= svgtiny_add_shape(state
);
623 return svgtiny_OUT_OF_MEMORY
;
626 shape
->path_length
= 7;
627 shape
->fill
= svgtiny_TRANSPARENT
;
628 shape
->stroke
= svgtiny_RGB(0xff, 0, 0);
629 state
->diagram
->shape_count
++;
633 /* render triangle vertices with r values for debugging */
634 #ifdef GRADIENT_DEBUG
635 for (unsigned int i
= 0; i
!= pts
->size
; i
++) {
636 struct grad_point
*point
= svgtiny_list_get(pts
, i
);
637 struct svgtiny_shape
*shape
= svgtiny_add_shape(state
);
639 return svgtiny_OUT_OF_MEMORY
;
640 char *text
= malloc(20);
642 return svgtiny_OUT_OF_MEMORY
;
643 sprintf(text
, "%i=%.3f", i
, point
->r
);
645 shape
->text_x
= state
->ctm
.a
* point
->x
+
646 state
->ctm
.c
* point
->y
+ state
->ctm
.e
;
647 shape
->text_y
= state
->ctm
.b
* point
->x
+
648 state
->ctm
.d
* point
->y
+ state
->ctm
.f
;
649 shape
->fill
= svgtiny_RGB(0, 0, 0);
650 shape
->stroke
= svgtiny_TRANSPARENT
;
651 state
->diagram
->shape_count
++;
655 /* plot actual path outline */
656 if (state
->stroke
!= svgtiny_TRANSPARENT
) {
657 struct svgtiny_shape
*shape
;
658 svgtiny_transform_path(p
, n
, state
);
660 shape
= svgtiny_add_shape(state
);
663 return svgtiny_OUT_OF_MEMORY
;
666 shape
->path_length
= n
;
667 shape
->fill
= svgtiny_TRANSPARENT
;
668 state
->diagram
->shape_count
++;
673 svgtiny_list_free(pts
);
680 * Get the bounding box of path.
683 void svgtiny_path_bbox(float *p
, unsigned int n
,
684 float *x0
, float *y0
, float *x1
, float *y1
)
691 for (j
= 0; j
!= n
; ) {
692 unsigned int points
= 0;
694 switch ((int) p
[j
]) {
695 case svgtiny_PATH_MOVE
:
696 case svgtiny_PATH_LINE
:
699 case svgtiny_PATH_CLOSE
:
702 case svgtiny_PATH_BEZIER
:
709 for (k
= 0; k
!= points
; k
++) {
710 float x
= p
[j
], y
= p
[j
+ 1];
726 * Invert a transformation matrix.
728 void svgtiny_invert_matrix(float *m
, float *inv
)
730 float determinant
= m
[0]*m
[3] - m
[1]*m
[2];
731 inv
[0] = m
[3] / determinant
;
732 inv
[1] = -m
[1] / determinant
;
733 inv
[2] = -m
[2] / determinant
;
734 inv
[3] = m
[0] / determinant
;
735 inv
[4] = (m
[2]*m
[5] - m
[3]*m
[4]) / determinant
;
736 inv
[5] = (m
[1]*m
[4] - m
[0]*m
[5]) / determinant
;