X-Git-Url: http://git.asbjorn.biz/?a=blobdiff_plain;ds=sidebyside;f=lib%2Fgfxpoly%2Fconvert.c;h=3d9763bd99b0fdef0b0ab139e78765858931b80f;hb=e0a27e1bd83ff2d60745dd6030e0f5ef7ef97b82;hp=9cd560c9dbd0b12b269a686edb8bc1075c379f7c;hpb=ae2bbf404cbdf63152d22f1b0824468d04f8f8ce;p=swftools.git diff --git a/lib/gfxpoly/convert.c b/lib/gfxpoly/convert.c index 9cd560c..3d9763b 100644 --- a/lib/gfxpoly/convert.c +++ b/lib/gfxpoly/convert.c @@ -5,6 +5,7 @@ #include "../mem.h" #include "poly.h" #include "convert.h" +#include "wind.h" /* factor that determines into how many line fragments a spline is converted */ #define SUBFRACTION (2.4) @@ -133,7 +134,7 @@ void finish_segment(compactpoly_t*data) gfxpolystroke_t*s = rfx_calloc(sizeof(gfxpolystroke_t)); s->next = data->poly->strokes; data->poly->strokes = s; - s->num_points = data->num_points; + s->num_points = s->points_size = data->num_points; s->dir = data->dir; s->points = p; assert(data->dir != DIR_UNKNOWN); @@ -230,7 +231,7 @@ void gfxpolywriter_init(polywriter_t*w) data->poly->strokes = 0; } -gfxpoly_t* gfxpoly_from_gfxline(gfxline_t*line, double gridsize) +gfxpoly_t* gfxpoly_from_fill(gfxline_t*line, double gridsize) { polywriter_t writer; gfxpolywriter_init(&writer); @@ -259,3 +260,145 @@ void gfxpoly_destroy(gfxpoly_t*poly) free(poly); } +typedef struct _polydraw_internal +{ + double lx, ly; + int32_t lastx, lasty; + double z; + char last; + 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->lx = _x; + i->ly = _y; + i->lastx = x; + i->lasty = y; + i->last = 1; +} +static void polydraw_lineTo(gfxdrawer_t*d, gfxcoord_t _x, gfxcoord_t _y) +{ + polydraw_internal_t*i = (polydraw_internal_t*)d->internal; + if(!i->last) { + polydraw_moveTo(d, _x, _y); + return; + } + 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->lx = _x; + i->ly = _y; + i->lastx = x; + i->lasty = y; + i->last = 1; +} +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; + if(!i->last) { + polydraw_moveTo(d, x, y); + return; + } + double c = fabs(x-2*sx+i->lx) + fabs(y-2*sy+i->ly); + int parts = (int)(sqrt(c)*SUBFRACTION); + if(!parts) parts = 1; + int t; + int32_t nx,ny; + for(t=0;tlx)/(double)(parts*parts), i->z); + ny = convert_coord((double)(t*t*y + 2*t*(parts-t)*sy + (parts-t)*(parts-t)*i->ly)/(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->lx = x; + i->ly = y; + i->lastx = nx; + i->lasty = ny; + i->last = 1; +} +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; +} + +gfxline_t*gfxline_from_gfxpoly(gfxpoly_t*poly) +{ + gfxpolystroke_t*stroke; + int count = 0; + for(stroke=poly->strokes;stroke;stroke=stroke->next) { + assert(stroke->num_points); + count += stroke->num_points; + } + if(!count) return 0; + gfxline_t*l = malloc(sizeof(gfxline_t)*count); + count = 0; + /* TODO: it might make sense to concatenate strokes */ + for(stroke=poly->strokes;stroke;stroke=stroke->next) { + int t; + for(t=0;tnum_points;t++) { + l[count+t].x = stroke->points[t].x * poly->gridsize; + l[count+t].y = stroke->points[t].y * poly->gridsize; + l[count+t].type = gfx_lineTo; + l[count+t].next = &l[count+t+1]; + } + l[count].type = gfx_moveTo; + count+=stroke->num_points; + } + l[count-1].next = 0; + return l; +} + +static windcontext_t onepolygon = {1}; +gfxline_t* gfxpoly_circular_to_evenodd(gfxline_t*line, double gridsize) +{ + gfxpoly_t*poly = gfxpoly_from_fill(line, gridsize); + gfxpoly_t*poly2 = gfxpoly_process(poly, 0, &windrule_circular, &onepolygon); + gfxline_t*line2 = gfxline_from_gfxpoly(poly2); + gfxpoly_destroy(poly); + gfxpoly_destroy(poly2); + return line2; +} + +gfxpoly_t* gfxpoly_createbox(double x1, double y1,double x2, double y2, double gridsize) +{ + gfxline_t* line = gfxline_makerectangle(x1, y1, x2, y2); + gfxpoly_t* poly = gfxpoly_from_fill(line, gridsize); + gfxline_free(line); + return poly; +} +