+}
+
+static char* readline(FILE*fi)
+{
+ char c;
+ while(1) {
+ int l = fread(&c, 1, 1, fi);
+ if(!l)
+ return 0;
+ if(c!=10 || c!=13)
+ break;
+ }
+ char line[256];
+ int pos = 0;
+ while(1) {
+ line[pos++] = c;
+ line[pos] = 0;
+ int l = fread(&c, 1, 1, fi);
+ if(!l || c==10 || c==13) {
+ return strdup(line);
+ }
+ }
+}
+
+static void convert_file(const char*filename, polywriter_t*w, double gridsize)
+{
+ FILE*fi = fopen(filename, "rb");
+ if(!fi) {
+ perror(filename);
+ }
+ double z = 1.0 / gridsize;
+ int count = 0;
+ double g = 0;
+ double lastx=0,lasty=0;
+ while(1) {
+ char*line = readline(fi);
+ if(!line)
+ break;
+ double x,y;
+ char s[256];
+ if(sscanf(line, "%lf %lf %s", &x, &y, &s) == 3) {
+ if(s && !strcmp(s,"moveto")) {
+ w->moveto(w, convert_coord(x,z), convert_coord(y,z));
+ count++;
+ } else if(s && !strcmp(s,"lineto")) {
+ w->lineto(w, convert_coord(x,z), convert_coord(y,z));
+ count++;
+ } else {
+ fprintf(stderr, "invalid command: %s\n", s);
+ }
+ } else if(sscanf(line, "%% gridsize %lf", &g) == 1) {
+ gridsize = g;
+ z = 1.0 / gridsize;
+ w->setgridsize(w, g);
+ }
+ free(line);
+ }
+ fclose(fi);
+ if(g) {
+ fprintf(stderr, "loaded %d points from %s (gridsize %f)\n", count, filename, g);
+ } else {
+ fprintf(stderr, "loaded %d points from %s\n", count, filename);
+ }
+}
+
+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;
+ point_t last;
+ int strokes_size;
+ 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;
+ 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;
+ 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
+ data->poly->num_strokes++;
+}
+static void compactmoveto(polywriter_t*w, int x, int 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, int x, int 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;
+ }