X-Git-Url: http://git.asbjorn.biz/?p=swftools.git;a=blobdiff_plain;f=lib%2Fgfxpoly%2Fconvert.c;h=9fbe40d2620752e6f57201048bc1e1fda5878163;hp=9d8ecab32508ab806916ff7aab4880d2eb4a13b1;hb=d7367b3ec772ea163ebca6b7497639a0cb45c20c;hpb=be6d3cbb485c5f7e9dd6fe22bf54245c79b10b71 diff --git a/lib/gfxpoly/convert.c b/lib/gfxpoly/convert.c index 9d8ecab..9fbe40d 100644 --- a/lib/gfxpoly/convert.c +++ b/lib/gfxpoly/convert.c @@ -12,12 +12,13 @@ static inline int32_t convert_coord(double x, double z) { - /* we clamp to 31 bit instead of 32 bit because we use - a (x1-x2) shortcut when comparing coordinates + /* we clamp to 26 bit because: + a) we use a (x1-x2) shortcut when comparing coordinates + b) we need to be able to multiply two coordinates and store them in a double w/o loss of precision */ x *= z; - if(x < -0x40000000) x = -0x40000000; - if(x > 0x3fffffff) x = 0x3fffffff; + if(x < -0x2000000) x = -0x2000000; + if(x > 0x1ffffff) x = 0x1ffffff; return ceil(x); } @@ -91,11 +92,11 @@ static void convert_file(const char*filename, polywriter_t*w, double gridsize) break; double x,y; char s[256]; - if(sscanf(line, "%lf %lf %s", &x, &y, &s) == 3) { - if(s && !strcmp(s,"moveto")) { + if(sscanf(line, "%lf %lf %s", &x, &y, (char*)&s) == 3) { + if(!strcmp(s,"moveto")) { w->moveto(w, convert_coord(x,z), convert_coord(y,z)); count++; - } else if(s && !strcmp(s,"lineto")) { + } else if(!strcmp(s,"lineto")) { w->lineto(w, convert_coord(x,z), convert_coord(y,z)); count++; } else { @@ -132,6 +133,7 @@ void finish_segment(compactpoly_t*data) return; point_t*p = malloc(sizeof(point_t)*data->num_points); gfxpolystroke_t*s = rfx_calloc(sizeof(gfxpolystroke_t)); + s->fs = &edgestyle_default; s->next = data->poly->strokes; data->poly->strokes = s; s->num_points = s->points_size = data->num_points; @@ -165,20 +167,29 @@ static void compactmoveto(polywriter_t*w, int32_t x, int32_t y) } data->last = p; } + +static inline int direction(point_t p1, point_t p2) +{ + int diff = p1.y - p2.y; + if(diff) return diff; + return p1.x - p2.x; +} + static void compactlineto(polywriter_t*w, int32_t x, int32_t y) { compactpoly_t*data = (compactpoly_t*)w->internal; point_t p; p.x = x; p.y = y; - if(p.x == data->last.x && p.y == data->last.y) + + int diff = direction(p, data->last); + if(!diff) return; + segment_dir_t dir = diff<0?DIR_UP:DIR_DOWN; - if(p.y < data->last.y && data->dir != DIR_UP || - p.y > data->last.y && data->dir != DIR_DOWN || - data->new) { + if(dir!=data->dir || data->new) { finish_segment(data); - data->dir = p.y > data->last.y ? DIR_DOWN : DIR_UP; + data->dir = dir; data->points[0] = data->last; data->num_points = 1; } @@ -406,7 +417,7 @@ gfxline_t*gfxline_from_gfxpoly(gfxpoly_t*poly) } #endif -gfxline_t*gfxline_from_gfxpoly(gfxpoly_t*poly) +static gfxline_t*mkgfxline(gfxpoly_t*poly, char preserve_direction) { gfxpolystroke_t*stroke; int count = 0; @@ -415,6 +426,7 @@ gfxline_t*gfxline_from_gfxpoly(gfxpoly_t*poly) dict_t*d = dict_new2(&point_type); dict_t*todo = dict_new2(&ptr_type); gfxpolystroke_t*stroke_min= poly->strokes; + int32_t x_min=stroke_min->points[0].x; int32_t y_min=stroke_min->points[0].y; for(stroke=poly->strokes;stroke;stroke=stroke->next) { dict_put(todo, stroke, stroke); @@ -422,10 +434,15 @@ gfxline_t*gfxline_from_gfxpoly(gfxpoly_t*poly) count += stroke->num_points; if(stroke->dir == DIR_UP) { dict_put(d, &stroke->points[stroke->num_points-1], stroke); + if(!preserve_direction) + dict_put(d, &stroke->points[0], stroke); } else { dict_put(d, &stroke->points[0], stroke); + if(!preserve_direction) + dict_put(d, &stroke->points[stroke->num_points-1], stroke); } - if(stroke->points[0].y < y_min) { + if(stroke->points[0].y < y_min || + (stroke->points[0].y == y_min && stroke->points[0].x < x_min)) { y_min = stroke->points[0].y; stroke_min = stroke; } @@ -438,17 +455,29 @@ gfxline_t*gfxline_from_gfxpoly(gfxpoly_t*poly) point_t last = {INVALID_COORD, INVALID_COORD}; char should_connect = 0; while(stroke) { + if(stroke && !preserve_direction) { + char del1 = dict_del2(d, &stroke->points[0], stroke); + char del2 = dict_del2(d, &stroke->points[stroke->num_points-1], stroke); + assert(del1 && del2); + } assert(dict_contains(todo, stroke)); int t; int pos = 0; int incr = 1; - if(stroke->dir == DIR_UP) { - pos = stroke->num_points-1; - incr = -1; + if(preserve_direction) { + if(stroke->dir == DIR_UP) { + pos = stroke->num_points-1; + incr = -1; + } + } else { + // try to find matching point on either end. + // Prefer downward. + if(last.x == stroke->points[stroke->num_points-1].x && + last.y == stroke->points[stroke->num_points-1].y) { + pos = stroke->num_points-1; + incr = -1; + } } - /* TODO: keeping the up/down order intact increases the polygon size - considerably. If this is going to be an even/odd polygon, - we could ignore it and save some space */ if(last.x != stroke->points[pos].x || last.y != stroke->points[pos].y) { l[count].x = stroke->points[pos].x * poly->gridsize; l[count].y = stroke->points[pos].y * poly->gridsize; @@ -490,6 +519,16 @@ gfxline_t*gfxline_from_gfxpoly(gfxpoly_t*poly) return l; } +gfxline_t*gfxline_from_gfxpoly(gfxpoly_t*poly) +{ + return mkgfxline(poly, 0); +} + +gfxline_t*gfxline_from_gfxpoly_with_direction(gfxpoly_t*poly) +{ + return mkgfxline(poly, 1); +} + static windcontext_t onepolygon = {1}; gfxline_t* gfxpoly_circular_to_evenodd(gfxline_t*line, double gridsize) {