fixed format warnings
[swftools.git] / lib / gfxpoly / convert.c
index c6e365d..f67699e 100644 (file)
 
 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,7 +92,7 @@ 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(sscanf(line, "%lf %lf %s", &x, &y, (char*)&s) == 3) {
             if(s && !strcmp(s,"moveto")) {
                w->moveto(w, convert_coord(x,z), convert_coord(y,z));
                 count++;
@@ -174,9 +175,8 @@ static void compactlineto(polywriter_t*w, int32_t x, int32_t y)
     if(p.x == data->last.x && p.y == data->last.y)
        return;
 
-    if(p.y < data->last.y && data->dir != DIR_UP ||
-       p.y > data->last.y && data->dir != DIR_DOWN || 
-       data->new) {
+    if((p.y < data->last.y && data->dir != DIR_UP) ||
+       (p.y > data->last.y && data->dir != DIR_DOWN) || data->new) {
        finish_segment(data);
        data->dir = p.y > data->last.y ? DIR_DOWN : DIR_UP;
        data->points[0] = data->last;
@@ -264,6 +264,7 @@ typedef struct _polydraw_internal
 {
     double lx, ly;
     int32_t lastx, lasty;
+    int32_t x0, y0;
     double z;
     char last;
     polywriter_t writer;
@@ -279,6 +280,8 @@ static void polydraw_moveTo(gfxdrawer_t*d, gfxcoord_t _x, gfxcoord_t _y)
     }
     i->lx = _x;
     i->ly = _y;
+    i->x0 = x;
+    i->y0 = y;
     i->lastx = x;
     i->lasty = y;
     i->last = 1;
@@ -332,6 +335,21 @@ static void polydraw_splineTo(gfxdrawer_t*d, gfxcoord_t sx, gfxcoord_t sy, gfxco
     i->lasty = ny;
     i->last = 1;
 }
+static void polydraw_close(gfxdrawer_t*d)
+{
+    polydraw_internal_t*i = (polydraw_internal_t*)d->internal;
+    assert(!(i->last && (i->x0 == INVALID_COORD || i->y0 == INVALID_COORD)));
+    if(!i->last)
+       return;
+    if(i->lastx != i->x0 || i->lasty != i->y0) {
+       i->writer.lineto(&i->writer, i->x0, i->y0);
+       i->lastx = i->x0;
+       i->lasty = i->y0;
+    }
+    i->last = 0;
+    i->x0 = INVALID_COORD;
+    i->y0 = INVALID_COORD;
+}
 static void* polydraw_result(gfxdrawer_t*d)
 {
     polydraw_internal_t*i = (polydraw_internal_t*)d->internal;
@@ -347,9 +365,12 @@ void gfxdrawer_target_poly(gfxdrawer_t*d, double gridsize)
     d->internal = i;
     i->lastx = INVALID_COORD; // convert_coord can never return this value
     i->lasty = INVALID_COORD;
+    i->x0 = INVALID_COORD;
+    i->y0 = INVALID_COORD;
     d->moveTo = polydraw_moveTo;
     d->lineTo = polydraw_lineTo;
     d->splineTo = polydraw_splineTo;
+    d->close = polydraw_close;
     d->result = polydraw_result;
     gfxpolywriter_init(&i->writer);
     i->writer.setgridsize(&i->writer, gridsize);
@@ -385,7 +406,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;
@@ -393,29 +414,58 @@ gfxline_t*gfxline_from_gfxpoly(gfxpoly_t*poly)
        return 0;
     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);
        assert(stroke->num_points>1);
        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 ||
+          (stroke->points[0].y == y_min && stroke->points[0].x < x_min)) {
+           y_min = stroke->points[0].y;
+           stroke_min = stroke;
        }
     }
     gfxpolystroke_t*next_todo = poly->strokes;
     gfxline_t*l = malloc(sizeof(gfxline_t)*count);
     count = 0;
-    stroke = poly->strokes;
+    stroke = stroke_min;
+    
     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;
+           }
        }
        if(last.x != stroke->points[pos].x || last.y != stroke->points[pos].y) {
            l[count].x = stroke->points[pos].x * poly->gridsize;
@@ -423,6 +473,7 @@ gfxline_t*gfxline_from_gfxpoly(gfxpoly_t*poly)
            l[count].type = gfx_moveTo;
            l[count].next = &l[count+1];
            count++;
+           assert(!should_connect);
        }
        pos += incr;
        for(t=1;t<stroke->num_points;t++) {
@@ -440,7 +491,9 @@ gfxline_t*gfxline_from_gfxpoly(gfxpoly_t*poly)
 
        /* try to find a poly which starts at the point we drew last */
        stroke = dict_lookup(d, &last);
+       should_connect = 1;
        while(!dict_contains(todo, stroke)) {
+           should_connect = 0;
            stroke = next_todo;
            if(!next_todo) {
                stroke = 0;
@@ -455,6 +508,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_updown(gfxpoly_t*poly)
+{
+    return mkgfxline(poly, 1);
+}
+
 static windcontext_t onepolygon = {1};
 gfxline_t* gfxpoly_circular_to_evenodd(gfxline_t*line, double gridsize)
 {