+ 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;
+ 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 = 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);
+ i->z = 1.0 / gridsize;
+}
+
+#if 0
+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;
+}
+#endif
+
+static gfxline_t*mkgfxline(gfxpoly_t*poly, char preserve_direction)
+{
+ gfxpolystroke_t*stroke;
+ int count = 0;
+ if(!poly->strokes)
+ 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 = 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(preserve_direction) {