+ fprintf(stderr, "loaded %d points from %s\n", count, filename);
+ }
+}
+
+typedef struct _compactpoly {
+ gfxpoly_t*poly;
+ point_t last;
+ point_t*points;
+ int num_points;
+ int points_size;
+ segment_dir_t dir;
+ char new;
+} compactpoly_t;
+
+void finish_segment(compactpoly_t*data)
+{
+ if(data->num_points <= 1)
+ return;
+ point_t*p = malloc(sizeof(point_t)*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);
+ if(data->dir == DIR_UP) {
+ int t;
+ int s = data->num_points;
+ for(t=0;t<data->num_points;t++) {
+ p[--s] = data->points[t];
+ }
+ } else {
+ memcpy(p, data->points, sizeof(point_t)*data->num_points);
+ }
+#ifdef CHECKS
+ int t;
+ for(t=0;t<data->num_points-1;t++) {
+ assert(p[t].y<=p[t+1].y);
+ }
+#endif
+}
+static void compactmoveto(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) {
+ data->new = 1;
+ }
+ data->last = p;
+}
+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)
+ return;
+
+ 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;
+ data->num_points = 1;
+ }
+ data->new = 0;
+
+ if(data->points_size == data->num_points) {
+ data->points_size <<= 1;
+ assert(data->points_size > data->num_points);
+ data->points = rfx_realloc(data->points, sizeof(point_t)*data->points_size);
+ }
+ data->points[data->num_points++] = p;
+ data->last = p;
+}
+static void compactsetgridsize(polywriter_t*w, double gridsize)
+{
+ compactpoly_t*d = (compactpoly_t*)w->internal;
+ d->poly->gridsize = gridsize;
+}
+/*static int compare_stroke(const void*_s1, const void*_s2)
+{
+ gfxpolystroke_t*s1 = (gfxpolystroke_t*)_s1;
+ gfxpolystroke_t*s2 = (gfxpolystroke_t*)_s2;
+ return s1->points[0].y - s2->points[0].y;
+}*/
+static void*compactfinish(polywriter_t*w)
+{
+ compactpoly_t*data = (compactpoly_t*)w->internal;
+ finish_segment(data);
+ //qsort(data->poly->strokes, data->poly->num_strokes, sizeof(gfxpolystroke_t), compare_stroke);
+ free(data->points);
+ gfxpoly_t*poly = data->poly;
+ free(w->internal);w->internal = 0;
+ return (void*)poly;
+}
+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(gfxpoly_t));
+ data->poly->gridsize = 1.0;
+ data->last.x = data->last.y = 0;
+ 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 = 0;
+}
+
+gfxpoly_t* gfxpoly_from_fill(gfxline_t*line, double gridsize)
+{
+ polywriter_t writer;
+ gfxpolywriter_init(&writer);
+ writer.setgridsize(&writer, gridsize);
+ convert_gfxline(line, &writer, gridsize);
+ return (gfxpoly_t*)writer.finish(&writer);
+}
+gfxpoly_t* gfxpoly_from_file(const char*filename, double gridsize)
+{
+ polywriter_t writer;
+ gfxpolywriter_init(&writer);
+ writer.setgridsize(&writer, gridsize);
+ convert_file(filename, &writer, gridsize);
+ return (gfxpoly_t*)writer.finish(&writer);
+}
+void gfxpoly_destroy(gfxpoly_t*poly)
+{
+ int t;
+ gfxpolystroke_t*stroke = poly->strokes;
+ while(stroke) {
+ gfxpolystroke_t*next = stroke->next;
+ free(stroke->points);
+ free(stroke);
+ stroke = next;
+ }
+ free(poly);
+}
+
+typedef struct _polydraw_internal
+{
+ double lx, ly;
+ int32_t lastx, lasty;
+ int32_t x0, y0;
+ 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->x0 = x;
+ i->y0 = 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;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;
+ }
+ }
+ 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);