X-Git-Url: http://git.asbjorn.biz/?a=blobdiff_plain;f=lib%2Fgfxpoly%2Fconvert.c;h=0fc2b7fca981e4cd16d5b71b270a214cba8563a4;hb=1ec6bb9a0eb63847e814b987014b2f1afcb7def6;hp=7cf23295b0a24bbecd4fcc51b9f384931fd7267b;hpb=8d76501168e44398feb36ae4d378178a676d8f2c;p=swftools.git diff --git a/lib/gfxpoly/convert.c b/lib/gfxpoly/convert.c index 7cf2329..0fc2b7f 100644 --- a/lib/gfxpoly/convert.c +++ b/lib/gfxpoly/convert.c @@ -118,7 +118,6 @@ static void convert_file(const char*filename, polywriter_t*w, double gridsize) typedef struct _compactpoly { gfxpoly_t*poly; point_t last; - int strokes_size; point_t*points; int num_points; int points_size; @@ -130,14 +129,11 @@ void finish_segment(compactpoly_t*data) { if(data->num_points <= 1) return; - if(data->poly->num_strokes == data->strokes_size) { - data->strokes_size <<= 1; - assert(data->strokes_size > data->poly->num_strokes); - data->poly->strokes = rfx_realloc(data->poly->strokes, sizeof(gfxpolystroke_t)*data->strokes_size); - } point_t*p = malloc(sizeof(point_t)*data->num_points); - gfxpolystroke_t*s = &data->poly->strokes[data->poly->num_strokes]; - s->num_points = data->num_points; + gfxpolystroke_t*s = rfx_calloc(sizeof(gfxpolystroke_t)); + s->next = data->poly->strokes; + data->poly->strokes = s; + s->num_points = s->points_size = data->num_points; s->dir = data->dir; s->points = p; assert(data->dir != DIR_UNKNOWN); @@ -156,7 +152,6 @@ void finish_segment(compactpoly_t*data) assert(p[t].y<=p[t+1].y); } #endif - data->poly->num_strokes++; } static void compactmoveto(polywriter_t*w, int32_t x, int32_t y) { @@ -186,6 +181,7 @@ static void compactlineto(polywriter_t*w, int32_t x, int32_t y) data->points[0] = data->last; data->num_points = 1; } + data->new = 0; if(data->points_size == data->num_points) { data->points_size <<= 1; @@ -210,7 +206,6 @@ static void*compactfinish(polywriter_t*w) { compactpoly_t*data = (compactpoly_t*)w->internal; finish_segment(data); - data->poly->strokes = (gfxpolystroke_t*)rfx_realloc(data->poly->strokes, sizeof(gfxpolystroke_t)*data->poly->num_strokes); //qsort(data->poly->strokes, data->poly->num_strokes, sizeof(gfxpolystroke_t), compare_stroke); free(data->points); gfxpoly_t*poly = data->poly; @@ -227,13 +222,12 @@ void gfxpolywriter_init(polywriter_t*w) data->poly = rfx_calloc(sizeof(gfxpoly_t)); data->poly->gridsize = 1.0; data->last.x = data->last.y = 0; - data->strokes_size = 16; data->num_points = 0; data->points_size = 16; data->new = 1; data->dir = DIR_UNKNOWN; data->points = (point_t*)rfx_alloc(sizeof(point_t)*data->points_size); - data->poly->strokes = (gfxpolystroke_t*)rfx_alloc(sizeof(gfxpolystroke_t)*data->strokes_size); + data->poly->strokes = 0; } gfxpoly_t* gfxpoly_from_gfxline(gfxline_t*line, double gridsize) @@ -255,11 +249,89 @@ gfxpoly_t* gfxpoly_from_file(const char*filename, double gridsize) void gfxpoly_destroy(gfxpoly_t*poly) { int t; - for(t=0;tnum_strokes;t++) { - free(poly->strokes[t].points); - poly->strokes[t].points = 0; + gfxpolystroke_t*stroke = poly->strokes; + while(stroke) { + gfxpolystroke_t*next = stroke->next; + free(stroke->points); + free(stroke); + stroke = next; } - free(poly->strokes); free(poly); } +typedef struct _polydraw_internal +{ + int32_t lastx, lasty; + double z; + polywriter_t writer; +} polydraw_internal_t; + +static void polydraw_moveTo(gfxdrawer_t*d, gfxcoord_t _x, gfxcoord_t _y) +{ + polydraw_internal_t*i = (polydraw_internal_t*)d->internal; + int32_t x = convert_coord(_x, i->z); + int32_t y = convert_coord(_y, i->z); + if(i->lastx != x || i->lasty != y) { + i->writer.moveto(&i->writer, x, y); + } + i->lastx = x; + i->lasty = y; +} +static void polydraw_lineTo(gfxdrawer_t*d, gfxcoord_t _x, gfxcoord_t _y) +{ + polydraw_internal_t*i = (polydraw_internal_t*)d->internal; + int32_t x = convert_coord(_x, i->z); + int32_t y = convert_coord(_y, i->z); + if(i->lastx != x || i->lasty != y) { + i->writer.lineto(&i->writer, x, y); + } + i->lastx = x; + i->lasty = y; +} +static void polydraw_splineTo(gfxdrawer_t*d, gfxcoord_t sx, gfxcoord_t sy, gfxcoord_t x, gfxcoord_t y) +{ + polydraw_internal_t*i = (polydraw_internal_t*)d->internal; + double c = fabs(x-2*sx+i->lastx) + fabs(y-2*sy+i->lasty); + int parts = (int)(sqrt(c)*SUBFRACTION); + if(!parts) parts = 1; + int t; + int32_t nx,ny; + for(t=0;tlastx)/(double)(parts*parts), i->z); + ny = convert_coord((double)(t*t*y + 2*t*(parts-t)*sy + (parts-t)*(parts-t)*i->lasty)/(double)(parts*parts), i->z); + if(nx != i->lastx || ny != i->lasty) { + i->writer.lineto(&i->writer, nx, ny); + i->lastx = nx; i->lasty = ny; + } + } + nx = convert_coord(x,i->z); + ny = convert_coord(y,i->z); + if(nx != i->lastx || ny != i->lasty) { + i->writer.lineto(&i->writer, nx, ny); + i->lastx = nx; i->lasty = ny; + } +} +static void* polydraw_result(gfxdrawer_t*d) +{ + polydraw_internal_t*i = (polydraw_internal_t*)d->internal; + void*result = i->writer.finish(&i->writer); + rfx_free(i); + memset(d, 0, sizeof(gfxdrawer_t)); + return result; +} + +void gfxdrawer_target_poly(gfxdrawer_t*d, double gridsize) +{ + polydraw_internal_t*i = (polydraw_internal_t*)rfx_calloc(sizeof(polydraw_internal_t)); + d->internal = i; + i->lastx = 0x7fffffff; // convert_coord can never return this value + i->lasty = 0x7fffffff; + d->moveTo = polydraw_moveTo; + d->lineTo = polydraw_lineTo; + d->splineTo = polydraw_splineTo; + d->result = polydraw_result; + gfxpolywriter_init(&i->writer); + i->writer.setgridsize(&i->writer, gridsize); + i->z = 1.0 / gridsize; +} +