#include <assert.h>
#include <math.h>
#include <string.h>
+#include <stdio.h>
+#include <stdlib.h>
+
#include "svgtiny.h"
#include "svgtiny_internal.h"
#undef GRADIENT_DEBUG
-static svgtiny_code svgtiny_parse_linear_gradient(xmlNode *linear,
+static svgtiny_code svgtiny_parse_linear_gradient(dom_element *linear,
+ struct svgtiny_parse_state_gradient *grad,
struct svgtiny_parse_state *state);
static float svgtiny_parse_gradient_offset(const char *s);
static void svgtiny_path_bbox(float *p, unsigned int n,
* Find a gradient by id and parse it.
*/
-void svgtiny_find_gradient(const char *id, struct svgtiny_parse_state *state)
+void svgtiny_find_gradient(const char *id,
+ struct svgtiny_parse_state_gradient *grad,
+ struct svgtiny_parse_state *state)
{
- xmlNode *gradient;
+ dom_element *gradient;
+ dom_string *id_str, *name;
+ dom_exception exc;
+ #ifdef GRADIENT_DEBUG
fprintf(stderr, "svgtiny_find_gradient: id \"%s\"\n", id);
+ #endif
- state->linear_gradient_stop_count = 0;
- state->gradient_x1 = "0%";
- state->gradient_y1 = "0%";
- state->gradient_x2 = "100%";
- state->gradient_y2 = "0%";
- state->gradient_user_space_on_use = false;
- state->gradient_transform.a = 1;
- state->gradient_transform.b = 0;
- state->gradient_transform.c = 0;
- state->gradient_transform.d = 1;
- state->gradient_transform.e = 0;
- state->gradient_transform.f = 0;
-
- gradient = svgtiny_find_element_by_id(
- (xmlNode *) state->document, id);
- fprintf(stderr, "gradient %p\n", (void *) gradient);
- if (!gradient) {
+ grad->linear_gradient_stop_count = 0;
+ if (grad->gradient_x1 != NULL)
+ dom_string_unref(grad->gradient_x1);
+ if (grad->gradient_y1 != NULL)
+ dom_string_unref(grad->gradient_y1);
+ if (grad->gradient_x2 != NULL)
+ dom_string_unref(grad->gradient_x2);
+ if (grad->gradient_y2 != NULL)
+ dom_string_unref(grad->gradient_y2);
+ grad->gradient_x1 = dom_string_ref(state->interned_zero_percent);
+ grad->gradient_y1 = dom_string_ref(state->interned_zero_percent);
+ grad->gradient_x2 = dom_string_ref(state->interned_hundred_percent);
+ grad->gradient_y2 = dom_string_ref(state->interned_zero_percent);
+ grad->gradient_user_space_on_use = false;
+ grad->gradient_transform.a = 1;
+ grad->gradient_transform.b = 0;
+ grad->gradient_transform.c = 0;
+ grad->gradient_transform.d = 1;
+ grad->gradient_transform.e = 0;
+ grad->gradient_transform.f = 0;
+
+ exc = dom_string_create_interned((const uint8_t *) id,
+ strlen(id), &id_str);
+ if (exc != DOM_NO_ERR)
+ return;
+
+ exc = dom_document_get_element_by_id(state->document, id_str,
+ &gradient);
+ dom_string_unref(id_str);
+ if (exc != DOM_NO_ERR || gradient == NULL) {
+ #ifdef GRADIENT_DEBUG
fprintf(stderr, "gradient \"%s\" not found\n", id);
+ #endif
return;
}
-
- fprintf(stderr, "gradient name \"%s\"\n", gradient->name);
- if (strcmp((const char *) gradient->name, "linearGradient") == 0) {
- svgtiny_parse_linear_gradient(gradient, state);
+
+ exc = dom_node_get_node_name(gradient, &name);
+ if (exc != DOM_NO_ERR) {
+ dom_node_unref(gradient);
+ return;
}
+
+ if (dom_string_isequal(name, state->interned_linearGradient))
+ svgtiny_parse_linear_gradient(gradient, grad, state);
+
+ dom_node_unref(gradient);
+ dom_string_unref(name);
+
+ #ifdef GRADIENT_DEBUG
+ fprintf(stderr, "linear_gradient_stop_count %i\n",
+ grad->linear_gradient_stop_count);
+ #endif
}
* http://www.w3.org/TR/SVG11/pservers#LinearGradients
*/
-svgtiny_code svgtiny_parse_linear_gradient(xmlNode *linear,
+svgtiny_code svgtiny_parse_linear_gradient(dom_element *linear,
+ struct svgtiny_parse_state_gradient *grad,
struct svgtiny_parse_state *state)
{
unsigned int i = 0;
- xmlNode *stop;
- xmlAttr *attr;
- xmlAttr *href = xmlHasProp(linear, (const xmlChar *) "href");
- if (href && href->children->content[0] == '#')
- svgtiny_find_gradient((const char *) href->children->content
- + 1, state);
-
- for (attr = linear->properties; attr; attr = attr->next) {
- const char *name = (const char *) attr->name;
- const char *content = (const char *) attr->children->content;
- if (strcmp(name, "x1") == 0)
- state->gradient_x1 = content;
- else if (strcmp(name, "y1") == 0)
- state->gradient_y1 = content;
- else if (strcmp(name, "x2") == 0)
- state->gradient_x2 = content;
- else if (strcmp(name, "y2") == 0)
- state->gradient_y2 = content;
- else if (strcmp(name, "gradientUnits") == 0)
- state->gradient_user_space_on_use =
- strcmp(content, "userSpaceOnUse") == 0;
- else if (strcmp(name, "gradientTransform") == 0) {
- float a = 1, b = 0, c = 0, d = 1, e = 0, f = 0;
- char *s = strdup(content);
- if (!s)
- return svgtiny_OUT_OF_MEMORY;
- svgtiny_parse_transform(s, &a, &b, &c, &d, &e, &f);
+ dom_string *attr;
+ dom_exception exc;
+ dom_nodelist *stops;
+
+ exc = dom_element_get_attribute(linear, state->interned_href, &attr);
+ if (exc == DOM_NO_ERR && attr != NULL) {
+ if (dom_string_data(attr)[0] == (uint8_t) '#') {
+ char *s = strndup(dom_string_data(attr) + 1,
+ dom_string_byte_length(attr) - 1);
+ svgtiny_find_gradient(s, grad, state);
free(s);
- fprintf(stderr, "transform %g %g %g %g %g %g\n",
- a, b, c, d, e, f);
- state->gradient_transform.a = a;
- state->gradient_transform.b = b;
- state->gradient_transform.c = c;
- state->gradient_transform.d = d;
- state->gradient_transform.e = e;
- state->gradient_transform.f = f;
}
- }
+ dom_string_unref(attr);
+ }
- for (stop = linear->children; stop; stop = stop->next) {
- float offset = -1;
- svgtiny_colour color = svgtiny_TRANSPARENT;
+ exc = dom_element_get_attribute(linear, state->interned_x1, &attr);
+ if (exc == DOM_NO_ERR && attr != NULL) {
+ dom_string_unref(grad->gradient_x1);
+ grad->gradient_x1 = attr;
+ attr = NULL;
+ }
- if (stop->type != XML_ELEMENT_NODE)
- continue;
- if (strcmp((const char *) stop->name, "stop") != 0)
- continue;
+ exc = dom_element_get_attribute(linear, state->interned_y1, &attr);
+ if (exc == DOM_NO_ERR && attr != NULL) {
+ dom_string_unref(grad->gradient_y1);
+ grad->gradient_y1 = attr;
+ attr = NULL;
+ }
- for (attr = stop->properties; attr;
- attr = attr->next) {
- const char *name = (const char *) attr->name;
- const char *content =
- (const char *) attr->children->content;
- if (strcmp(name, "offset") == 0)
- offset = svgtiny_parse_gradient_offset(content);
- else if (strcmp(name, "stop-color") == 0)
- svgtiny_parse_color(content, &color, state);
- else if (strcmp(name, "style") == 0) {
+ exc = dom_element_get_attribute(linear, state->interned_x2, &attr);
+ if (exc == DOM_NO_ERR && attr != NULL) {
+ dom_string_unref(grad->gradient_x2);
+ grad->gradient_x2 = attr;
+ attr = NULL;
+ }
+
+ exc = dom_element_get_attribute(linear, state->interned_y2, &attr);
+ if (exc == DOM_NO_ERR && attr != NULL) {
+ dom_string_unref(grad->gradient_y2);
+ grad->gradient_y2 = attr;
+ attr = NULL;
+ }
+
+ exc = dom_element_get_attribute(linear, state->interned_gradientUnits,
+ &attr);
+ if (exc == DOM_NO_ERR && attr != NULL) {
+ grad->gradient_user_space_on_use =
+ dom_string_isequal(attr,
+ state->interned_userSpaceOnUse);
+ dom_string_unref(attr);
+ }
+
+ exc = dom_element_get_attribute(linear,
+ state->interned_gradientTransform,
+ &attr);
+ if (exc == DOM_NO_ERR && attr != NULL) {
+ float a = 1, b = 0, c = 0, d = 1, e = 0, f = 0;
+ char *s = strndup(dom_string_data(attr),
+ dom_string_byte_length(attr));
+ if (s == NULL) {
+ dom_string_unref(attr);
+ return svgtiny_OUT_OF_MEMORY;
+ }
+ svgtiny_parse_transform(s, &a, &b, &c, &d, &e, &f);
+ free(s);
+ #ifdef GRADIENT_DEBUG
+ fprintf(stderr, "transform %g %g %g %g %g %g\n",
+ a, b, c, d, e, f);
+ #endif
+ grad->gradient_transform.a = a;
+ grad->gradient_transform.b = b;
+ grad->gradient_transform.c = c;
+ grad->gradient_transform.d = d;
+ grad->gradient_transform.e = e;
+ grad->gradient_transform.f = f;
+ dom_string_unref(attr);
+ }
+
+ exc = dom_element_get_elements_by_tag_name(linear,
+ state->interned_stop,
+ &stops);
+ if (exc == DOM_NO_ERR && stops != NULL) {
+ uint32_t listlen, stopnr;
+ exc = dom_nodelist_get_length(stops, &listlen);
+ if (exc != DOM_NO_ERR) {
+ dom_nodelist_unref(stops);
+ goto no_more_stops;
+ }
+
+ for (stopnr = 0; stopnr < listlen; ++stopnr) {
+ dom_element *stop;
+ float offset = -1;
+ svgtiny_colour color = svgtiny_TRANSPARENT;
+ exc = dom_nodelist_item(stops, stopnr,
+ (dom_node **) (void *) &stop);
+ if (exc != DOM_NO_ERR)
+ continue;
+ exc = dom_element_get_attribute(stop,
+ state->interned_offset,
+ &attr);
+ if (exc == DOM_NO_ERR && attr != NULL) {
+ char *s = strndup(dom_string_data(attr),
+ dom_string_byte_length(attr));
+ offset = svgtiny_parse_gradient_offset(s);
+ free(s);
+ dom_string_unref(attr);
+ }
+ exc = dom_element_get_attribute(stop,
+ state->interned_stop_color,
+ &attr);
+ if (exc == DOM_NO_ERR && attr != NULL) {
+ svgtiny_parse_color(attr, &color, NULL, state);
+ dom_string_unref(attr);
+ }
+ exc = dom_element_get_attribute(stop,
+ state->interned_style,
+ &attr);
+ if (exc == DOM_NO_ERR && attr != NULL) {
+ char *content = strndup(dom_string_data(attr),
+ dom_string_byte_length(attr));
const char *s;
- char *value;
+ dom_string *value;
if ((s = strstr(content, "stop-color:"))) {
s += 11;
while (*s == ' ')
s++;
- value = strndup(s, strcspn(s, "; "));
- svgtiny_parse_color(value, &color,
- state);
- free(value);
+ exc = dom_string_create_interned(
+ (const uint8_t *) s,
+ strcspn(s, "; "),
+ &value);
+ if (exc == DOM_NO_ERR &&
+ value != NULL) {
+ svgtiny_parse_color(value,
+ &color,
+ NULL,
+ state);
+ dom_string_unref(value);
+ }
}
+ free(content);
+ dom_string_unref(attr);
}
+ if (offset != -1 && color != svgtiny_TRANSPARENT) {
+ #ifdef GRADIENT_DEBUG
+ fprintf(stderr, "stop %g %x\n", offset, color);
+ #endif
+ grad->gradient_stop[i].offset = offset;
+ grad->gradient_stop[i].color = color;
+ i++;
+ }
+ dom_node_unref(stop);
+ if (i == svgtiny_MAX_STOPS)
+ break;
}
-
- if (offset != -1 && color != svgtiny_TRANSPARENT) {
- fprintf(stderr, "stop %g %x\n", offset, color);
- state->gradient_stop[i].offset = offset;
- state->gradient_stop[i].color = color;
- i++;
- }
-
- if (i == svgtiny_MAX_STOPS)
- break;
+
+ dom_nodelist_unref(stops);
}
-
- if (i)
- state->linear_gradient_stop_count = i;
+no_more_stops:
+ if (i > 0)
+ grad->linear_gradient_stop_count = i;
return svgtiny_OK;
}
float current_stop_r;
int red0, green0, blue0, red1, green1, blue1;
unsigned int t, a, b;
+ struct svgtiny_parse_state_gradient *grad;
+
+ assert(state->fill == svgtiny_LINEAR_GRADIENT);
+ grad = &state->fill_grad;
/* determine object bounding box */
svgtiny_path_bbox(p, n, &object_x0, &object_y0, &object_x1, &object_y1);
object_x0, object_y0, object_x1, object_y1);
#endif
- /* compute gradient vector */
- fprintf(stderr, "x1 %s, y1 %s, x2 %s, y2 %s\n",
- state->gradient_x1, state->gradient_y1,
- state->gradient_x2, state->gradient_y2);
- if (!state->gradient_user_space_on_use) {
+ if (!grad->gradient_user_space_on_use) {
gradient_x0 = object_x0 +
- svgtiny_parse_length(state->gradient_x1,
+ svgtiny_parse_length(grad->gradient_x1,
object_x1 - object_x0, *state);
gradient_y0 = object_y0 +
- svgtiny_parse_length(state->gradient_y1,
+ svgtiny_parse_length(grad->gradient_y1,
object_y1 - object_y0, *state);
gradient_x1 = object_x0 +
- svgtiny_parse_length(state->gradient_x2,
+ svgtiny_parse_length(grad->gradient_x2,
object_x1 - object_x0, *state);
gradient_y1 = object_y0 +
- svgtiny_parse_length(state->gradient_y2,
+ svgtiny_parse_length(grad->gradient_y2,
object_y1 - object_y0, *state);
} else {
- gradient_x0 = svgtiny_parse_length(state->gradient_x1,
+ gradient_x0 = svgtiny_parse_length(grad->gradient_x1,
state->viewport_width, *state);
- gradient_y0 = svgtiny_parse_length(state->gradient_y1,
+ gradient_y0 = svgtiny_parse_length(grad->gradient_y1,
state->viewport_height, *state);
- gradient_x1 = svgtiny_parse_length(state->gradient_x2,
+ gradient_x1 = svgtiny_parse_length(grad->gradient_x2,
state->viewport_width, *state);
- gradient_y1 = svgtiny_parse_length(state->gradient_y2,
+ gradient_y1 = svgtiny_parse_length(grad->gradient_y2,
state->viewport_height, *state);
}
gradient_dx = gradient_x1 - gradient_x0;
}*/
/* invert gradient transform for applying to vertices */
- svgtiny_invert_matrix(&state->gradient_transform.a, trans);
+ svgtiny_invert_matrix(&grad->gradient_transform.a, trans);
+ #ifdef GRADIENT_DEBUG
fprintf(stderr, "inverse transform %g %g %g %g %g %g\n",
trans[0], trans[1], trans[2], trans[3],
trans[4], trans[5]);
+ #endif
/* compute points on the path for triangle vertices */
/* r, r0, r1 are distance along gradient vector */
gradient_norm_squared = gradient_dx * gradient_dx +
gradient_dy * gradient_dy;
- pts = svgtiny_list_create(
- sizeof (struct grad_point));
- if (!pts)
+ pts = svgtiny_list_create(sizeof (struct grad_point));
+ if (!pts) {
+ free(p);
return svgtiny_OUT_OF_MEMORY;
+ }
for (j = 0; j != n; ) {
int segment_type = (int) p[j];
struct grad_point *point;
gradient_norm_squared;
point = svgtiny_list_push(pts);
if (!point) {
+ free(p);
svgtiny_list_free(pts);
return svgtiny_OUT_OF_MEMORY;
}
gradient_norm_squared;
/* determine steps from change in r */
- steps = ceilf(fabsf(r1 - r0) / 0.05);
+
+ if(isnan(r0) || isnan(r1)) {
+ steps = 1;
+ } else {
+ steps = ceilf(fabsf(r1 - r0) / 0.05);
+ }
+
if (steps == 0)
steps = 1;
+ #ifdef GRADIENT_DEBUG
fprintf(stderr, "r0 %g, r1 %g, steps %i\n",
r0, r1, steps);
+ #endif
/* loop through intermediate points */
for (z = 1; z != steps; z++) {
r = ((x_trans - gradient_x0) * gradient_dx +
(y_trans - gradient_y0) * gradient_dy) /
gradient_norm_squared;
+ #ifdef GRADIENT_DEBUG
fprintf(stderr, "(%g %g [%g]) ", x, y, r);
+ #endif
point = svgtiny_list_push(pts);
if (!point) {
+ free(p);
svgtiny_list_free(pts);
return svgtiny_OUT_OF_MEMORY;
}
min_pt = svgtiny_list_size(pts) - 1;
}
}
+ #ifdef GRADIENT_DEBUG
fprintf(stderr, "\n");
+ #endif
/* next segment start point is this segment end point */
x0 = x1;
y0 = y1;
}
+ #ifdef GRADIENT_DEBUG
fprintf(stderr, "pts size %i, min_pt %i, min_r %.3f\n",
svgtiny_list_size(pts), min_pt, min_r);
+ #endif
+
+ /* There must be at least a single point for the gradient */
+ if (svgtiny_list_size(pts) == 0) {
+ svgtiny_list_free(pts);
+ free(p);
+ return svgtiny_OK;
+ }
+
/* render triangles */
- stop_count = state->linear_gradient_stop_count;
+ stop_count = grad->linear_gradient_stop_count;
assert(2 <= stop_count);
current_stop = 0;
last_stop_r = 0;
- current_stop_r = state->gradient_stop[0].offset;
- red0 = red1 = svgtiny_RED(state->gradient_stop[0].color);
- green0 = green1 = svgtiny_GREEN(state->gradient_stop[0].color);
- blue0 = blue1 = svgtiny_BLUE(state->gradient_stop[0].color);
+ current_stop_r = grad->gradient_stop[0].offset;
+ red0 = red1 = svgtiny_RED(grad->gradient_stop[0].color);
+ green0 = green1 = svgtiny_GREEN(grad->gradient_stop[0].color);
+ blue0 = blue1 = svgtiny_BLUE(grad->gradient_stop[0].color);
t = min_pt;
a = (min_pt + 1) % svgtiny_list_size(pts);
b = min_pt == 0 ? svgtiny_list_size(pts) - 1 : min_pt - 1;
red0 = red1;
green0 = green1;
blue0 = blue1;
- red1 = svgtiny_RED(state->
+ red1 = svgtiny_RED(grad->
gradient_stop[current_stop].color);
- green1 = svgtiny_GREEN(state->
+ green1 = svgtiny_GREEN(grad->
gradient_stop[current_stop].color);
- blue1 = svgtiny_BLUE(state->
+ blue1 = svgtiny_BLUE(grad->
gradient_stop[current_stop].color);
last_stop_r = current_stop_r;
- current_stop_r = state->
+ current_stop_r = grad->
gradient_stop[current_stop].offset;
}
p = malloc(10 * sizeof p[0]);
shape->path_length = 10;
/*shape->fill = svgtiny_TRANSPARENT;*/
if (current_stop == 0)
- shape->fill = state->gradient_stop[0].color;
+ shape->fill = grad->gradient_stop[0].color;
else if (current_stop == stop_count)
- shape->fill = state->
- gradient_stop[stop_count - 1].color;
+ shape->fill = grad->gradient_stop[stop_count - 1].color;
else {
float stop_r = (mean_r - last_stop_r) /
(current_stop_r - last_stop_r);
/* render triangle vertices with r values for debugging */
#ifdef GRADIENT_DEBUG
- for (unsigned int i = 0; i != pts->size; i++) {
+ for (unsigned int i = 0; i != svgtiny_list_size(pts); i++) {
struct grad_point *point = svgtiny_list_get(pts, i);
struct svgtiny_shape *shape = svgtiny_add_shape(state);
if (!shape)
inv[5] = (m[1]*m[4] - m[0]*m[5]) / determinant;
}
-
-/**
- * Find an element in the document by id.
- */
-
-xmlNode *svgtiny_find_element_by_id(xmlNode *node, const char *id)
-{
- xmlNode *child;
- xmlNode *found;
-
- for (child = node->children; child; child = child->next) {
- xmlAttr *attr;
- if (child->type != XML_ELEMENT_NODE)
- continue;
- attr = xmlHasProp(child, (const xmlChar *) "id");
- if (attr && strcmp(id, (const char *) attr->children->content)
- == 0)
- return child;
- found = svgtiny_find_element_by_id(child, id);
- if (found)
- return found;
- }
-
- return 0;
-}
-