Index: libsvgtiny/svgtiny_gradient.c =================================================================== --- libsvgtiny/svgtiny_gradient.c (revision 7307) +++ libsvgtiny/svgtiny_gradient.c (working copy) @@ -28,6 +28,7 @@ void svgtiny_find_gradient(const char *id, struct svgtiny_parse_state *state) { + xmlNode *gradient; fprintf(stderr, "svgtiny_find_gradient: id \"%s\"\n", id); state->linear_gradient_stop_count = 0; @@ -43,7 +44,7 @@ state->gradient_transform.e = 0; state->gradient_transform.f = 0; - xmlNode *gradient = svgtiny_find_element_by_id( + gradient = svgtiny_find_element_by_id( (xmlNode *) state->document, id); fprintf(stderr, "gradient %p\n", gradient); if (!gradient) { @@ -68,11 +69,14 @@ struct svgtiny_parse_state *state) { xmlAttr *href = xmlHasProp(linear, (const xmlChar *) "href"); + xmlAttr *attr; + xmlNode *stop; + unsigned int i = 0; if (href && href->children->content[0] == '#') svgtiny_find_gradient((const char *) href->children->content + 1, state); - for (xmlAttr *attr = linear->properties; attr; attr = attr->next) { + 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) @@ -102,10 +106,9 @@ state->gradient_transform.e = e; state->gradient_transform.f = f; } - } + } - unsigned int i = 0; - for (xmlNode *stop = linear->children; stop; stop = stop->next) { + for (stop = linear->children; stop; stop = stop->next) { float offset = -1; svgtiny_colour color = svgtiny_TRANSPARENT; @@ -114,7 +117,7 @@ if (strcmp((const char *) stop->name, "stop") != 0) continue; - for (xmlAttr *attr = stop->properties; attr; + for (attr = stop->properties; attr; attr = attr->next) { const char *name = (const char *) attr->name; const char *content = @@ -184,8 +187,30 @@ svgtiny_code svgtiny_add_path_linear_gradient(float *p, unsigned int n, struct svgtiny_parse_state *state) { + float object_x0, object_y0, object_x1, object_y1; + float gradient_x0, gradient_y0, gradient_x1, gradient_y1, + gradient_dx, gradient_dy; + float trans[6]; + unsigned int steps; + float x0, y0, x0_trans, y0_trans, r0; /* segment start point */ + float x1, y1, x1_trans, y1_trans, r1; /* segment end point */ + float c0x, c0y, c1x, c1y; /* segment control points (beziers only) */ + float gradient_norm_squared; + struct grad_point { + float x, y, r; + }; + struct svgtiny_list *pts; + float min_r; + unsigned int min_pt; + unsigned int j; + unsigned int stop_count; + unsigned int current_stop; + float last_stop_r; + float current_stop_r; + int red0, green0, blue0, red1, green1, blue1; + unsigned int t, a, b; + /* determine object bounding box */ - float object_x0, object_y0, object_x1, object_y1; svgtiny_path_bbox(p, n, &object_x0, &object_y0, &object_x1, &object_y1); #ifdef GRADIENT_DEBUG fprintf(stderr, "object bbox: (%g %g) (%g %g)\n", @@ -196,8 +221,6 @@ fprintf(stderr, "x1 %s, y1 %s, x2 %s, y2 %s\n", state->gradient_x1, state->gradient_y1, state->gradient_x2, state->gradient_y2); - float gradient_x0, gradient_y0, gradient_x1, gradient_y1, - gradient_dx, gradient_dy; if (!state->gradient_user_space_on_use) { gradient_x0 = object_x0 + svgtiny_parse_length(state->gradient_x1, @@ -271,7 +294,6 @@ }*/ /* invert gradient transform for applying to vertices */ - float trans[6]; svgtiny_invert_matrix(&state->gradient_transform.a, trans); fprintf(stderr, "inverse transform %g %g %g %g %g %g\n", trans[0], trans[1], trans[2], trans[3], @@ -279,23 +301,19 @@ /* compute points on the path for triangle vertices */ /* r, r0, r1 are distance along gradient vector */ - unsigned int steps = 10; - float x0, y0, x0_trans, y0_trans, r0; /* segment start point */ - float x1, y1, x1_trans, y1_trans, r1; /* segment end point */ - float c0x, c0y, c1x, c1y; /* segment control points (beziers only) */ - float gradient_norm_squared = gradient_dx * gradient_dx + - gradient_dy * gradient_dy; - struct grad_point { - float x, y, r; - }; - struct svgtiny_list *pts = svgtiny_list_create( + steps = 10; + gradient_norm_squared = gradient_dx * gradient_dx + + gradient_dy * gradient_dy; + pts = svgtiny_list_create( sizeof (struct grad_point)); if (!pts) return svgtiny_OUT_OF_MEMORY; - float min_r = 1000; - unsigned int min_pt = 0; - for (unsigned int j = 0; j != n; ) { + min_r = 1000; + min_pt = 0; + for (j = 0; j != n; ) { int segment_type = (int) p[j]; + unsigned int z; + struct grad_point *point; if (segment_type == svgtiny_PATH_MOVE) { x0 = p[j + 1]; @@ -314,7 +332,7 @@ r0 = ((x0_trans - gradient_x0) * gradient_dx + (y0_trans - gradient_y0) * gradient_dy) / gradient_norm_squared; - struct grad_point *point = svgtiny_list_push(pts); + point = svgtiny_list_push(pts); if (!point) { svgtiny_list_free(pts); return svgtiny_OUT_OF_MEMORY; @@ -359,8 +377,9 @@ r0, r1, steps); /* loop through intermediate points */ - for (unsigned int z = 1; z != steps; z++) { + for (z = 1; z != steps; z++) { float t, x, y, x_trans, y_trans, r; + struct grad_point *point; t = (float) z / (float) steps; if (segment_type == svgtiny_PATH_BEZIER) { x = (1-t) * (1-t) * (1-t) * x0 + @@ -381,7 +400,7 @@ (y_trans - gradient_y0) * gradient_dy) / gradient_norm_squared; fprintf(stderr, "(%g %g [%g]) ", x, y, r); - struct grad_point *point = svgtiny_list_push(pts); + point = svgtiny_list_push(pts); if (!point) { svgtiny_list_free(pts); return svgtiny_OUT_OF_MEMORY; @@ -404,16 +423,14 @@ svgtiny_list_size(pts), min_pt, min_r); /* render triangles */ - unsigned int stop_count = state->linear_gradient_stop_count; + stop_count = state->linear_gradient_stop_count; assert(2 <= stop_count); - unsigned int current_stop = 0; - float last_stop_r = 0; - float current_stop_r = state->gradient_stop[0].offset; - int red0, green0, blue0, red1, green1, blue1; + 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); - unsigned int t, a, b; t = min_pt; a = (min_pt + 1) % svgtiny_list_size(pts); b = min_pt == 0 ? svgtiny_list_size(pts) - 1 : min_pt - 1; @@ -422,6 +439,8 @@ struct grad_point *point_a = svgtiny_list_get(pts, a); struct grad_point *point_b = svgtiny_list_get(pts, b); float mean_r = (point_t->r + point_a->r + point_b->r) / 3; + float *p; + struct svgtiny_shape *shape; /*fprintf(stderr, "triangle: t %i %.3f a %i %.3f b %i %.3f " "mean_r %.3f\n", t, pts[t].r, a, pts[a].r, b, pts[b].r, @@ -443,7 +462,7 @@ current_stop_r = state-> gradient_stop[current_stop].offset; } - float *p = malloc(10 * sizeof p[0]); + p = malloc(10 * sizeof p[0]); if (!p) return svgtiny_OUT_OF_MEMORY; p[0] = svgtiny_PATH_MOVE; @@ -457,7 +476,7 @@ p[8] = point_b->y; p[9] = svgtiny_PATH_CLOSE; svgtiny_transform_path(p, 10, state); - struct svgtiny_shape *shape = svgtiny_add_shape(state); + shape = svgtiny_add_shape(state); if (!shape) { free(p); return svgtiny_OUT_OF_MEMORY; @@ -543,9 +562,10 @@ /* plot actual path outline */ if (state->stroke != svgtiny_TRANSPARENT) { + struct svgtiny_shape *shape; svgtiny_transform_path(p, n, state); - struct svgtiny_shape *shape = svgtiny_add_shape(state); + shape = svgtiny_add_shape(state); if (!shape) { free(p); return svgtiny_OUT_OF_MEMORY; @@ -571,11 +591,13 @@ void svgtiny_path_bbox(float *p, unsigned int n, float *x0, float *y0, float *x1, float *y1) { + unsigned int j; *x0 = *x1 = p[1]; *y0 = *y1 = p[2]; - for (unsigned int j = 0; j != n; ) { + for (j = 0; j != n; ) { unsigned int points = 0; + unsigned int k; switch ((int) p[j]) { case svgtiny_PATH_MOVE: case svgtiny_PATH_LINE: @@ -591,7 +613,7 @@ assert(0); } j++; - for (unsigned int k = 0; k != points; k++) { + for (k = 0; k != points; k++) { float x = p[j], y = p[j + 1]; if (x < *x0) *x0 = x; @@ -632,9 +654,10 @@ xmlNode *found; for (child = node->children; child; child = child->next) { + xmlAttr *attr; if (child->type != XML_ELEMENT_NODE) continue; - xmlAttr *attr = xmlHasProp(child, (const xmlChar *) "id"); + attr = xmlHasProp(child, (const xmlChar *) "id"); if (attr && strcmp(id, (const char *) attr->children->content) == 0) return child; Index: libsvgtiny/svgtiny.c =================================================================== --- libsvgtiny/svgtiny.c (revision 7307) +++ libsvgtiny/svgtiny.c (working copy) @@ -143,13 +143,15 @@ struct svgtiny_parse_state state) { float x, y, width, height; + xmlAttr *view_box; + xmlNode *child; svgtiny_parse_position_attributes(svg, state, &x, &y, &width, &height); svgtiny_parse_paint_attributes(svg, &state); svgtiny_parse_font_attributes(svg, &state); /* parse viewBox */ - xmlAttr *view_box = xmlHasProp(svg, (const xmlChar *) "viewBox"); + view_box = xmlHasProp(svg, (const xmlChar *) "viewBox"); if (view_box) { const char *s = (const char *) view_box->children->content; float min_x, min_y, vwidth, vheight; @@ -166,7 +168,7 @@ svgtiny_parse_transform_attributes(svg, &state); - for (xmlNode *child = svg->children; child; child = child->next) { + for (child = svg->children; child; child = child->next) { svgtiny_code code = svgtiny_OK; if (child->type == XML_ELEMENT_NODE) { @@ -212,6 +214,11 @@ struct svgtiny_parse_state state) { char *s, *path_d; + float *p; + unsigned int i; + float last_x = 0, last_y = 0; + float last_cubic_x = 0, last_cubic_y = 0; + float last_quad_x = 0, last_quad_y = 0; svgtiny_parse_paint_attributes(path, &state); svgtiny_parse_transform_attributes(path, &state); @@ -225,18 +232,15 @@ } /* allocate space for path: it will never have more elements than d */ - float *p = malloc(sizeof p[0] * strlen(s)); + p = malloc(sizeof p[0] * strlen(s)); if (!p) return svgtiny_OUT_OF_MEMORY; /* parse d and build path */ - for (unsigned int i = 0; s[i]; i++) + for (i = 0; s[i]; i++) if (s[i] == ',') s[i] = ' '; - unsigned int i = 0; - float last_x = 0, last_y = 0; - float last_cubic_x = 0, last_cubic_y = 0; - float last_quad_x = 0, last_quad_y = 0; + i = 0; while (*s) { char command[2]; int plot_command; @@ -417,13 +421,14 @@ struct svgtiny_parse_state state) { float x, y, width, height; + float *p; svgtiny_parse_position_attributes(rect, state, &x, &y, &width, &height); svgtiny_parse_paint_attributes(rect, &state); svgtiny_parse_transform_attributes(rect, &state); - float *p = malloc(13 * sizeof p[0]); + p = malloc(13 * sizeof p[0]); if (!p) return svgtiny_OUT_OF_MEMORY; @@ -454,8 +459,10 @@ { float x = 0, y = 0, r = 0; const float kappa = 0.5522847498; + xmlAttr *attr; + float *p; - for (xmlAttr *attr = circle->properties; attr; attr = attr->next) { + for (attr = circle->properties; attr; attr = attr->next) { const char *name = (const char *) attr->name; const char *content = (const char *) attr->children->content; if (strcmp(name, "cx") == 0) @@ -471,7 +478,7 @@ svgtiny_parse_paint_attributes(circle, &state); svgtiny_parse_transform_attributes(circle, &state); - float *p = malloc(32 * sizeof p[0]); + p = malloc(32 * sizeof p[0]); if (!p) return svgtiny_OUT_OF_MEMORY; @@ -520,8 +527,10 @@ struct svgtiny_parse_state state) { float x1 = 0, y1 = 0, x2 = 0, y2 = 0; + xmlAttr *attr; + float *p; - for (xmlAttr *attr = line->properties; attr; attr = attr->next) { + for (attr = line->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) @@ -540,7 +549,7 @@ svgtiny_parse_paint_attributes(line, &state); svgtiny_parse_transform_attributes(line, &state); - float *p = malloc(7 * sizeof p[0]); + p = malloc(7 * sizeof p[0]); if (!p) return svgtiny_OUT_OF_MEMORY; @@ -567,6 +576,8 @@ struct svgtiny_parse_state state, bool polygon) { char *s, *points; + float *p; + unsigned int i; svgtiny_parse_paint_attributes(poly, &state); svgtiny_parse_transform_attributes(poly, &state); @@ -581,17 +592,17 @@ } /* allocate space for path: it will never have more elements than s */ - float *p = malloc(sizeof p[0] * strlen(s)); + p = malloc(sizeof p[0] * strlen(s)); if (!p) { xmlFree(points); return svgtiny_OUT_OF_MEMORY; } /* parse s and build path */ - for (unsigned int i = 0; s[i]; i++) + for (i = 0; s[i]; i++) if (s[i] == ',') s[i] = ' '; - unsigned int i = 0; + i = 0; while (*s) { float x, y; int n; @@ -625,21 +636,23 @@ struct svgtiny_parse_state state) { float x, y, width, height; + float px, py; + xmlNode *child; svgtiny_parse_position_attributes(text, state, &x, &y, &width, &height); svgtiny_parse_font_attributes(text, &state); svgtiny_parse_transform_attributes(text, &state); - float px = state.ctm.a * x + state.ctm.c * y + state.ctm.e; - float py = state.ctm.b * x + state.ctm.d * y + state.ctm.f; + px = state.ctm.a * x + state.ctm.c * y + state.ctm.e; + py = state.ctm.b * x + state.ctm.d * y + state.ctm.f; /* state.ctm.e = px - state.origin_x; */ /* state.ctm.f = py - state.origin_y; */ /*struct css_style style = state.style; style.font_size.value.length.value *= state.ctm.a;*/ - for (xmlNode *child = text->children; child; child = child->next) { + for (child = text->children; child; child = child->next) { svgtiny_code code = svgtiny_OK; if (child->type == XML_TEXT_NODE) { @@ -673,12 +686,13 @@ const struct svgtiny_parse_state state, float *x, float *y, float *width, float *height) { + xmlAttr *attr; *x = 0; *y = 0; *width = state.viewport_width; *height = state.viewport_height; - for (xmlAttr *attr = node->properties; attr; attr = attr->next) { + for (attr = node->properties; attr; attr = attr->next) { const char *name = (const char *) attr->name; const char *content = (const char *) attr->children->content; if (strcmp(name, "x") == 0) @@ -742,7 +756,8 @@ void svgtiny_parse_paint_attributes(const xmlNode *node, struct svgtiny_parse_state *state) { - for (const xmlAttr *attr = node->properties; attr; attr = attr->next) { + const xmlAttr *attr; + for (attr = node->properties; attr; attr = attr->next) { const char *name = (const char *) attr->name; const char *content = (const char *) attr->children->content; if (strcmp(name, "fill") == 0) @@ -856,7 +871,8 @@ void svgtiny_parse_font_attributes(const xmlNode *node, struct svgtiny_parse_state *state) { - for (const xmlAttr *attr = node->properties; attr; attr = attr->next) { + const xmlAttr *attr; + for (attr = node->properties; attr; attr = attr->next) { if (strcmp((const char *) attr->name, "font-size") == 0) { /*if (css_parse_length( (const char *) attr->children->content, @@ -903,8 +919,9 @@ float za, zb, zc, zd, ze, zf; float angle, x, y; int n; + unsigned int i; - for (unsigned int i = 0; s[i]; i++) + for (i = 0; s[i]; i++) if (s[i] == ',') s[i] = ' '; @@ -977,12 +994,14 @@ svgtiny_code svgtiny_add_path(float *p, unsigned int n, struct svgtiny_parse_state *state) { + struct svgtiny_shape *shape; + if (state->fill == svgtiny_LINEAR_GRADIENT) return svgtiny_add_path_linear_gradient(p, n, state); svgtiny_transform_path(p, n, state); - struct svgtiny_shape *shape = svgtiny_add_shape(state); + shape = svgtiny_add_shape(state); if (!shape) { free(p); return svgtiny_OUT_OF_MEMORY; @@ -1028,8 +1047,11 @@ void svgtiny_transform_path(float *p, unsigned int n, struct svgtiny_parse_state *state) { - for (unsigned int j = 0; j != n; ) { + unsigned int j; + + for (j = 0; j != n; ) { unsigned int points = 0; + unsigned int k; switch ((int) p[j]) { case svgtiny_PATH_MOVE: case svgtiny_PATH_LINE: @@ -1045,7 +1067,7 @@ assert(0); } j++; - for (unsigned int k = 0; k != points; k++) { + for (k = 0; k != points; k++) { float x0 = p[j], y0 = p[j + 1]; float x = state->ctm.a * x0 + state->ctm.c * y0 + state->ctm.e; @@ -1065,9 +1087,11 @@ void svgtiny_free(struct svgtiny_diagram *svg) { + unsigned int i; + assert(svg); - for (unsigned int i = 0; i != svg->shape_count; i++) { + for (i = 0; i != svg->shape_count; i++) { free(svg->shape[i].path); free(svg->shape[i].text); }