replaced libart with new polygon code
[swftools.git] / lib / gfxpoly / convert.c
index 213cdad..3d9763b 100644 (file)
@@ -5,20 +5,11 @@
 #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)
 
-static edge_t*edge_new(int x1, int y1, int x2, int y2)
-{
-    edge_t*s = rfx_calloc(sizeof(edge_t));
-    s->a.x = x1;
-    s->a.y = y1;
-    s->b.x = x2;
-    s->b.y = y2;
-    return s;
-}
-
 static inline int32_t convert_coord(double x, double z)
 {
     /* we clamp to 31 bit instead of 32 bit because we use
@@ -125,58 +116,9 @@ static void convert_file(const char*filename, polywriter_t*w, double gridsize)
     }
 }
 
-typedef struct _stdpoly {
-    gfxpoly_t*poly;
-    double lastx,lasty;
-} stdpoly_t;
-
-static void stdmoveto(polywriter_t*w, int x, int y)
-{
-    stdpoly_t*d = (stdpoly_t*)w->internal;
-    d->lastx = x;d->lasty = y;
-}
-static void stdlineto(polywriter_t*w, int x, int y)
-{
-    stdpoly_t*d = (stdpoly_t*)w->internal;
-    int x1 = d->lastx;
-    int y1 = d->lasty;
-    int x2 = x;
-    int y2 = y;
-    if(x1!=x2 || y1!=y2) {
-        edge_t*s = edge_new(x1, y1, x2, y2);
-        s->next = d->poly->edges;
-        d->poly->edges = s;
-    }
-    d->lastx = x;d->lasty = y;
-}
-static void stdsetgridsize(polywriter_t*w, double gridsize)
-{
-    stdpoly_t*d = (stdpoly_t*)w->internal;
-    d->poly->gridsize = gridsize;
-}
-static void* stdfinish(polywriter_t*w)
-{
-    stdpoly_t*d = (stdpoly_t*)w->internal;
-    gfxpoly_t*poly =  d->poly;
-    free(w->internal);w->internal = 0;
-    return poly;
-}
-void gfxpolywriter_init(polywriter_t*w)
-{
-    w->moveto = stdmoveto;
-    w->lineto = stdlineto;
-    w->setgridsize = stdsetgridsize;
-    w->finish = stdfinish;
-    stdpoly_t*data = w->internal = malloc(sizeof(stdpoly_t));
-    data->poly = gfxpoly_new(1.0);
-    data->lastx = 0;
-    data->lasty = 0;
-}
-
 typedef struct _compactpoly {
-    gfxcompactpoly_t*poly;
+    gfxpoly_t*poly;
     point_t last;
-    int strokes_size;
     point_t*points;
     int num_points;
     int points_size;
@@ -188,14 +130,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);
@@ -214,9 +153,8 @@ 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, int x, int y)
+static void compactmoveto(polywriter_t*w, int32_t x, int32_t y)
 {
     compactpoly_t*data = (compactpoly_t*)w->internal;
     point_t p;
@@ -227,7 +165,7 @@ static void compactmoveto(polywriter_t*w, int x, int y)
     }
     data->last = p;
 }
-static void compactlineto(polywriter_t*w, int x, int y)
+static void compactlineto(polywriter_t*w, int32_t x, int32_t y)
 {
     compactpoly_t*data = (compactpoly_t*)w->internal;
     point_t p;
@@ -244,6 +182,7 @@ static void compactlineto(polywriter_t*w, int x, int y)
        data->points[0] = data->last;
        data->num_points = 1;
     }
+    data->new = 0;
 
     if(data->points_size == data->num_points) {
        data->points_size <<= 1;
@@ -268,95 +207,198 @@ 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);
-    gfxcompactpoly_t*poly = data->poly;
+    gfxpoly_t*poly = data->poly;
     free(w->internal);w->internal = 0;
     return (void*)poly;
 }
-void gfxcompactpolywriter_init(polywriter_t*w)
+void gfxpolywriter_init(polywriter_t*w)
 {
     w->moveto = compactmoveto;
     w->lineto = compactlineto;
     w->setgridsize = compactsetgridsize;
     w->finish = compactfinish;
     compactpoly_t*data = w->internal = rfx_calloc(sizeof(compactpoly_t));
-    data->poly = rfx_calloc(sizeof(gfxcompactpoly_t));
+    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)
-{
-    polywriter_t w;
-    gfxpolywriter_init(&w);
-    w.setgridsize(&w, gridsize);
-    convert_gfxline(line, &w, gridsize);
-    return w.finish(&w);
-}
-gfxpoly_t* gfxpoly_from_file(const char*filename, double gridsize)
-{
-    polywriter_t w;
-    gfxpolywriter_init(&w);
-    w.setgridsize(&w, gridsize);
-    convert_file(filename, &w, gridsize);
-    return w.finish(&w);
-}
-gfxcompactpoly_t* gfxcompactpoly_from_gfxline(gfxline_t*line, double gridsize)
+gfxpoly_t* gfxpoly_from_fill(gfxline_t*line, double gridsize)
 {
     polywriter_t writer;
-    gfxcompactpolywriter_init(&writer);
+    gfxpolywriter_init(&writer);
     writer.setgridsize(&writer, gridsize);
     convert_gfxline(line, &writer, gridsize);
-    return (gfxcompactpoly_t*)writer.finish(&writer);
+    return (gfxpoly_t*)writer.finish(&writer);
 }
-gfxcompactpoly_t* gfxcompactpoly_from_file(const char*filename, double gridsize)
+gfxpoly_t* gfxpoly_from_file(const char*filename, double gridsize)
 {
     polywriter_t writer;
-    gfxcompactpolywriter_init(&writer);
+    gfxpolywriter_init(&writer);
     writer.setgridsize(&writer, gridsize);
     convert_file(filename, &writer, gridsize);
-    return (gfxcompactpoly_t*)writer.finish(&writer);
+    return (gfxpoly_t*)writer.finish(&writer);
 }
-gfxpoly_t*gfxpoly_from_gfxcompactpoly(gfxcompactpoly_t*poly)
+void gfxpoly_destroy(gfxpoly_t*poly)
 {
-    int s,t;
-    int pass;
-    gfxpoly_t*poly2 = gfxpoly_new(poly->gridsize);
-    for(t=0;t<poly->num_strokes;t++) {
-       gfxpolystroke_t*stroke = &poly->strokes[t];
-       for(s=0;s<stroke->num_points-1;s++) {
-           point_t a = stroke->points[s];
-           point_t b = stroke->points[s+1];
-           edge_t*e = 0;
-           if(stroke->dir == DIR_UP) {
-               e = edge_new(a.x,a.y,b.x,b.y);
-           } else {
-               e = edge_new(b.x,b.y,a.x,a.y);
-           }
-           e->style = stroke->fs;
-           e->next = poly2->edges;
-           poly2->edges = e;
-       }
+    int t;
+    gfxpolystroke_t*stroke = poly->strokes;
+    while(stroke) {
+       gfxpolystroke_t*next = stroke->next;
+       free(stroke->points);
+       free(stroke);
+       stroke = next;
     }
-    return poly2;
+    free(poly);
 }
-void gfxcompactpoly_destroy(gfxcompactpoly_t*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;
-    for(t=0;t<poly->num_strokes;t++) {
-       free(poly->strokes[t].points);
-       poly->strokes[t].points = 0;
+    int32_t nx,ny;
+    for(t=0;t<parts;t++) {
+       nx = convert_coord((double)(t*t*x + 2*t*(parts-t)*sx + (parts-t)*(parts-t)*i->lx)/(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;
+       }
     }
-    free(poly->strokes);
-    free(poly);
+    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;t<stroke->num_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;
 }