X-Git-Url: http://gitweb.michael.orlitzky.com/?a=blobdiff_plain;f=src%2Fsvgtiny.c;h=bd9aeaa89486ddc873249744feed390d78042801;hb=f293a45e808b444b5a3b8b989b76fdc20566d3c9;hp=6215b04adb316b6baa971ac13d6ba5771fdc4b38;hpb=b314f3b54da7892d9f6994ba99082b92b76cf316;p=libsvgtiny.git diff --git a/src/svgtiny.c b/src/svgtiny.c index 6215b04..bd9aeaa 100644 --- a/src/svgtiny.c +++ b/src/svgtiny.c @@ -188,6 +188,12 @@ svgtiny_code svgtiny_parse(struct svgtiny_diagram *diagram, dom_node_unref(document); return svgtiny_LIBDOM_ERROR; } + if (svg == NULL) { + /* no root svg element */ + dom_node_unref(document); + return svgtiny_SVG_ERROR; + } + exc = dom_node_get_node_name(svg, &svg_name); if (exc != DOM_NO_ERR) { dom_node_unref(svg); @@ -473,18 +479,21 @@ svgtiny_code svgtiny_parse_path(dom_element *path, int n; /* Ensure there is sufficient space for path elements */ - if ((palloc - i) < 7) { - float *tp; - palloc = (palloc * 2) + (palloc / 2); - tp = realloc(p, sizeof p[0] * palloc); - if (tp == NULL) { - free(p); - free(path_d); - svgtiny_cleanup_state_local(&state); - return svgtiny_OUT_OF_MEMORY; - } - p = tp; - } +#define ALLOC_PATH_ELEMENTS(NUM_ELEMENTS) \ + do { \ + if ((palloc - i) < NUM_ELEMENTS) { \ + float *tp; \ + palloc = (palloc * 2) + (palloc / 2); \ + tp = realloc(p, sizeof p[0] * palloc); \ + if (tp == NULL) { \ + free(p); \ + free(path_d); \ + svgtiny_cleanup_state_local(&state); \ + return svgtiny_OUT_OF_MEMORY; \ + } \ + p = tp; \ + } \ + } while(0) /* moveto (M, m), lineto (L, l) (2 arguments) */ @@ -495,6 +504,7 @@ svgtiny_code svgtiny_parse_path(dom_element *path, else plot_command = svgtiny_PATH_LINE; do { + ALLOC_PATH_ELEMENTS(3); p[i++] = plot_command; if ('a' <= *command) { x += last_x; @@ -515,6 +525,8 @@ svgtiny_code svgtiny_parse_path(dom_element *path, /* closepath (Z, z) (no arguments) */ } else if (sscanf(s, " %1[Zz] %n", command, &n) == 1) { /*LOG(("closepath"));*/ + ALLOC_PATH_ELEMENTS(1); + p[i++] = svgtiny_PATH_CLOSE; s += n; last_cubic_x = last_quad_x = last_x = subpath_first_x; @@ -524,6 +536,8 @@ svgtiny_code svgtiny_parse_path(dom_element *path, } else if (sscanf(s, " %1[Hh] %f %n", command, &x, &n) == 2) { /*LOG(("horizontal lineto"));*/ do { + ALLOC_PATH_ELEMENTS(3); + p[i++] = svgtiny_PATH_LINE; if (*command == 'h') x += last_x; @@ -537,6 +551,8 @@ svgtiny_code svgtiny_parse_path(dom_element *path, } else if (sscanf(s, " %1[Vv] %f %n", command, &y, &n) == 2) { /*LOG(("vertical lineto"));*/ do { + ALLOC_PATH_ELEMENTS(3); + p[i++] = svgtiny_PATH_LINE; if (*command == 'v') y += last_y; @@ -551,6 +567,8 @@ svgtiny_code svgtiny_parse_path(dom_element *path, &x1, &y1, &x2, &y2, &x, &y, &n) == 7) { /*LOG(("curveto"));*/ do { + ALLOC_PATH_ELEMENTS(7); + p[i++] = svgtiny_PATH_BEZIER; if (*command == 'c') { x1 += last_x; @@ -575,6 +593,8 @@ svgtiny_code svgtiny_parse_path(dom_element *path, &x2, &y2, &x, &y, &n) == 5) { /*LOG(("shorthand/smooth curveto"));*/ do { + ALLOC_PATH_ELEMENTS(7); + p[i++] = svgtiny_PATH_BEZIER; x1 = last_x + (last_x - last_cubic_x); y1 = last_y + (last_y - last_cubic_y); @@ -599,6 +619,8 @@ svgtiny_code svgtiny_parse_path(dom_element *path, &x1, &y1, &x, &y, &n) == 5) { /*LOG(("quadratic Bezier curveto"));*/ do { + ALLOC_PATH_ELEMENTS(7); + p[i++] = svgtiny_PATH_BEZIER; last_quad_x = x1; last_quad_y = y1; @@ -624,6 +646,8 @@ svgtiny_code svgtiny_parse_path(dom_element *path, &x, &y, &n) == 3) { /*LOG(("shorthand/smooth quadratic Bezier curveto"));*/ do { + ALLOC_PATH_ELEMENTS(7); + p[i++] = svgtiny_PATH_BEZIER; x1 = last_x + (last_x - last_quad_x); y1 = last_y + (last_y - last_quad_y); @@ -650,6 +674,8 @@ svgtiny_code svgtiny_parse_path(dom_element *path, &rx, &ry, &rotation, &large_arc, &sweep, &x, &y, &n) == 8) { do { + ALLOC_PATH_ELEMENTS(3); + p[i++] = svgtiny_PATH_LINE; if (*command == 'a') { x += last_x; @@ -681,15 +707,15 @@ svgtiny_code svgtiny_parse_path(dom_element *path, /* resize path element array to not be over allocated */ if (palloc != i) { - float *tp; - - /* try the resize, if it fails just continue to use the old - * allocation - */ - tp = realloc(p, sizeof p[0] * i); - if (tp != NULL) { - p = tp; - } + float *tp; + + /* try the resize, if it fails just continue to use the old + * allocation + */ + tp = realloc(p, sizeof p[0] * i); + if (tp != NULL) { + p = tp; + } } err = svgtiny_add_path(p, i, &state);