4 #include "../gfxdevice.h"
10 /* factor that determines into how many line fragments a spline is converted */
11 #define SUBFRACTION (2.4)
13 static inline int32_t convert_coord(double x, double z)
15 /* we clamp to 31 bit instead of 32 bit because we use
16 a (x1-x2) shortcut when comparing coordinates
19 if(x < -0x40000000) x = -0x40000000;
20 if(x > 0x3fffffff) x = 0x3fffffff;
24 static void convert_gfxline(gfxline_t*line, polywriter_t*w, double gridsize)
26 assert(!line || line[0].type == gfx_moveTo);
27 double lastx=0,lasty=0;
28 double z = 1.0 / gridsize;
30 if(line->type == gfx_moveTo) {
31 if(line->next && line->next->type != gfx_moveTo && (line->x!=lastx || line->y!=lasty)) {
32 w->moveto(w, convert_coord(line->x,z), convert_coord(line->y,z));
34 } else if(line->type == gfx_lineTo) {
35 w->lineto(w, convert_coord(line->x,z), convert_coord(line->y,z));
36 } else if(line->type == gfx_splineTo) {
37 int parts = (int)(sqrt(fabs(line->x-2*line->sx+lastx) +
38 fabs(line->y-2*line->sy+lasty))*SUBFRACTION);
40 double stepsize = 1.0/parts;
42 for(i=0;i<parts;i++) {
43 double t = (double)i*stepsize;
44 double sx = (line->x*t*t + 2*line->sx*t*(1-t) + lastx*(1-t)*(1-t));
45 double sy = (line->y*t*t + 2*line->sy*t*(1-t) + lasty*(1-t)*(1-t));
46 w->lineto(w, convert_coord(sx,z), convert_coord(sy,z));
48 w->lineto(w, convert_coord(line->x,z), convert_coord(line->y,z));
56 static char* readline(FILE*fi)
60 int l = fread(&c, 1, 1, fi);
71 int l = fread(&c, 1, 1, fi);
72 if(!l || c==10 || c==13) {
78 static void convert_file(const char*filename, polywriter_t*w, double gridsize)
80 FILE*fi = fopen(filename, "rb");
84 double z = 1.0 / gridsize;
87 double lastx=0,lasty=0;
89 char*line = readline(fi);
94 if(sscanf(line, "%lf %lf %s", &x, &y, &s) == 3) {
95 if(s && !strcmp(s,"moveto")) {
96 w->moveto(w, convert_coord(x,z), convert_coord(y,z));
98 } else if(s && !strcmp(s,"lineto")) {
99 w->lineto(w, convert_coord(x,z), convert_coord(y,z));
102 fprintf(stderr, "invalid command: %s\n", s);
104 } else if(sscanf(line, "%% gridsize %lf", &g) == 1) {
107 w->setgridsize(w, g);
113 fprintf(stderr, "loaded %d points from %s (gridsize %f)\n", count, filename, g);
115 fprintf(stderr, "loaded %d points from %s\n", count, filename);
119 typedef struct _compactpoly {
129 void finish_segment(compactpoly_t*data)
131 if(data->num_points <= 1)
133 point_t*p = malloc(sizeof(point_t)*data->num_points);
134 gfxpolystroke_t*s = rfx_calloc(sizeof(gfxpolystroke_t));
135 s->next = data->poly->strokes;
136 data->poly->strokes = s;
137 s->num_points = s->points_size = data->num_points;
140 assert(data->dir != DIR_UNKNOWN);
141 if(data->dir == DIR_UP) {
143 int s = data->num_points;
144 for(t=0;t<data->num_points;t++) {
145 p[--s] = data->points[t];
148 memcpy(p, data->points, sizeof(point_t)*data->num_points);
152 for(t=0;t<data->num_points-1;t++) {
153 assert(p[t].y<=p[t+1].y);
157 static void compactmoveto(polywriter_t*w, int32_t x, int32_t y)
159 compactpoly_t*data = (compactpoly_t*)w->internal;
163 if(p.x != data->last.x || p.y != data->last.y) {
168 static void compactlineto(polywriter_t*w, int32_t x, int32_t y)
170 compactpoly_t*data = (compactpoly_t*)w->internal;
174 if(p.x == data->last.x && p.y == data->last.y)
177 if(p.y < data->last.y && data->dir != DIR_UP ||
178 p.y > data->last.y && data->dir != DIR_DOWN ||
180 finish_segment(data);
181 data->dir = p.y > data->last.y ? DIR_DOWN : DIR_UP;
182 data->points[0] = data->last;
183 data->num_points = 1;
187 if(data->points_size == data->num_points) {
188 data->points_size <<= 1;
189 assert(data->points_size > data->num_points);
190 data->points = rfx_realloc(data->points, sizeof(point_t)*data->points_size);
192 data->points[data->num_points++] = p;
195 static void compactsetgridsize(polywriter_t*w, double gridsize)
197 compactpoly_t*d = (compactpoly_t*)w->internal;
198 d->poly->gridsize = gridsize;
200 /*static int compare_stroke(const void*_s1, const void*_s2)
202 gfxpolystroke_t*s1 = (gfxpolystroke_t*)_s1;
203 gfxpolystroke_t*s2 = (gfxpolystroke_t*)_s2;
204 return s1->points[0].y - s2->points[0].y;
206 static void*compactfinish(polywriter_t*w)
208 compactpoly_t*data = (compactpoly_t*)w->internal;
209 finish_segment(data);
210 //qsort(data->poly->strokes, data->poly->num_strokes, sizeof(gfxpolystroke_t), compare_stroke);
212 gfxpoly_t*poly = data->poly;
213 free(w->internal);w->internal = 0;
216 void gfxpolywriter_init(polywriter_t*w)
218 w->moveto = compactmoveto;
219 w->lineto = compactlineto;
220 w->setgridsize = compactsetgridsize;
221 w->finish = compactfinish;
222 compactpoly_t*data = w->internal = rfx_calloc(sizeof(compactpoly_t));
223 data->poly = rfx_calloc(sizeof(gfxpoly_t));
224 data->poly->gridsize = 1.0;
225 data->last.x = data->last.y = 0;
226 data->num_points = 0;
227 data->points_size = 16;
229 data->dir = DIR_UNKNOWN;
230 data->points = (point_t*)rfx_alloc(sizeof(point_t)*data->points_size);
231 data->poly->strokes = 0;
234 gfxpoly_t* gfxpoly_from_fill(gfxline_t*line, double gridsize)
237 gfxpolywriter_init(&writer);
238 writer.setgridsize(&writer, gridsize);
239 convert_gfxline(line, &writer, gridsize);
240 return (gfxpoly_t*)writer.finish(&writer);
242 gfxpoly_t* gfxpoly_from_file(const char*filename, double gridsize)
245 gfxpolywriter_init(&writer);
246 writer.setgridsize(&writer, gridsize);
247 convert_file(filename, &writer, gridsize);
248 return (gfxpoly_t*)writer.finish(&writer);
250 void gfxpoly_destroy(gfxpoly_t*poly)
253 gfxpolystroke_t*stroke = poly->strokes;
255 gfxpolystroke_t*next = stroke->next;
256 free(stroke->points);
263 typedef struct _polydraw_internal
266 int32_t lastx, lasty;
271 } polydraw_internal_t;
273 static void polydraw_moveTo(gfxdrawer_t*d, gfxcoord_t _x, gfxcoord_t _y)
275 polydraw_internal_t*i = (polydraw_internal_t*)d->internal;
276 int32_t x = convert_coord(_x, i->z);
277 int32_t y = convert_coord(_y, i->z);
278 if(i->lastx != x || i->lasty != y) {
279 i->writer.moveto(&i->writer, x, y);
289 static void polydraw_lineTo(gfxdrawer_t*d, gfxcoord_t _x, gfxcoord_t _y)
291 polydraw_internal_t*i = (polydraw_internal_t*)d->internal;
293 polydraw_moveTo(d, _x, _y);
296 int32_t x = convert_coord(_x, i->z);
297 int32_t y = convert_coord(_y, i->z);
298 if(i->lastx != x || i->lasty != y) {
299 i->writer.lineto(&i->writer, x, y);
307 static void polydraw_splineTo(gfxdrawer_t*d, gfxcoord_t sx, gfxcoord_t sy, gfxcoord_t x, gfxcoord_t y)
309 polydraw_internal_t*i = (polydraw_internal_t*)d->internal;
311 polydraw_moveTo(d, x, y);
314 double c = fabs(x-2*sx+i->lx) + fabs(y-2*sy+i->ly);
315 int parts = (int)(sqrt(c)*SUBFRACTION);
316 if(!parts) parts = 1;
319 for(t=0;t<parts;t++) {
320 nx = convert_coord((double)(t*t*x + 2*t*(parts-t)*sx + (parts-t)*(parts-t)*i->lx)/(double)(parts*parts), i->z);
321 ny = convert_coord((double)(t*t*y + 2*t*(parts-t)*sy + (parts-t)*(parts-t)*i->ly)/(double)(parts*parts), i->z);
322 if(nx != i->lastx || ny != i->lasty) {
323 i->writer.lineto(&i->writer, nx, ny);
324 i->lastx = nx; i->lasty = ny;
327 nx = convert_coord(x,i->z);
328 ny = convert_coord(y,i->z);
329 if(nx != i->lastx || ny != i->lasty) {
330 i->writer.lineto(&i->writer, nx, ny);
338 static void polydraw_close(gfxdrawer_t*d)
340 polydraw_internal_t*i = (polydraw_internal_t*)d->internal;
341 assert(!(i->last && (i->x0 == INVALID_COORD || i->y0 == INVALID_COORD)));
344 if(i->lastx != i->x0 || i->lasty != i->y0) {
345 i->writer.lineto(&i->writer, i->x0, i->y0);
350 i->x0 = INVALID_COORD;
351 i->y0 = INVALID_COORD;
353 static void* polydraw_result(gfxdrawer_t*d)
355 polydraw_internal_t*i = (polydraw_internal_t*)d->internal;
356 void*result = i->writer.finish(&i->writer);
358 memset(d, 0, sizeof(gfxdrawer_t));
362 void gfxdrawer_target_poly(gfxdrawer_t*d, double gridsize)
364 polydraw_internal_t*i = (polydraw_internal_t*)rfx_calloc(sizeof(polydraw_internal_t));
366 i->lastx = INVALID_COORD; // convert_coord can never return this value
367 i->lasty = INVALID_COORD;
368 i->x0 = INVALID_COORD;
369 i->y0 = INVALID_COORD;
370 d->moveTo = polydraw_moveTo;
371 d->lineTo = polydraw_lineTo;
372 d->splineTo = polydraw_splineTo;
373 d->close = polydraw_close;
374 d->result = polydraw_result;
375 gfxpolywriter_init(&i->writer);
376 i->writer.setgridsize(&i->writer, gridsize);
377 i->z = 1.0 / gridsize;
381 gfxline_t*gfxline_from_gfxpoly(gfxpoly_t*poly)
383 gfxpolystroke_t*stroke;
385 for(stroke=poly->strokes;stroke;stroke=stroke->next) {
386 assert(stroke->num_points);
387 count += stroke->num_points;
390 gfxline_t*l = malloc(sizeof(gfxline_t)*count);
392 /* TODO: it might make sense to concatenate strokes */
393 for(stroke=poly->strokes;stroke;stroke=stroke->next) {
395 for(t=0;t<stroke->num_points;t++) {
396 l[count+t].x = stroke->points[t].x * poly->gridsize;
397 l[count+t].y = stroke->points[t].y * poly->gridsize;
398 l[count+t].type = gfx_lineTo;
399 l[count+t].next = &l[count+t+1];
401 l[count].type = gfx_moveTo;
402 count+=stroke->num_points;
409 gfxline_t*gfxline_from_gfxpoly(gfxpoly_t*poly)
411 gfxpolystroke_t*stroke;
415 dict_t*d = dict_new2(&point_type);
416 dict_t*todo = dict_new2(&ptr_type);
417 gfxpolystroke_t*stroke_min= poly->strokes;
418 int32_t y_min=stroke_min->points[0].y;
419 for(stroke=poly->strokes;stroke;stroke=stroke->next) {
420 dict_put(todo, stroke, stroke);
421 assert(stroke->num_points>1);
422 count += stroke->num_points;
423 if(stroke->dir == DIR_UP) {
424 dict_put(d, &stroke->points[stroke->num_points-1], stroke);
426 dict_put(d, &stroke->points[0], stroke);
428 if(stroke->points[0].y < y_min) {
429 y_min = stroke->points[0].y;
433 gfxpolystroke_t*next_todo = poly->strokes;
434 gfxline_t*l = malloc(sizeof(gfxline_t)*count);
438 point_t last = {INVALID_COORD, INVALID_COORD};
439 char should_connect = 0;
441 assert(dict_contains(todo, stroke));
445 if(stroke->dir == DIR_UP) {
446 pos = stroke->num_points-1;
449 /* TODO: keeping the up/down order intact increases the polygon size
450 considerably. If this is going to be an even/odd polygon,
451 we could ignore it and save some space */
452 if(last.x != stroke->points[pos].x || last.y != stroke->points[pos].y) {
453 l[count].x = stroke->points[pos].x * poly->gridsize;
454 l[count].y = stroke->points[pos].y * poly->gridsize;
455 l[count].type = gfx_moveTo;
456 l[count].next = &l[count+1];
458 assert(!should_connect);
461 for(t=1;t<stroke->num_points;t++) {
462 l[count].x = stroke->points[pos].x * poly->gridsize;
463 l[count].y = stroke->points[pos].y * poly->gridsize;
464 l[count].type = gfx_lineTo;
465 l[count].next = &l[count+1];
469 last = stroke->points[pos-incr];
470 char del = dict_del(todo, stroke);
472 assert(!dict_contains(todo, stroke));
474 /* try to find a poly which starts at the point we drew last */
475 stroke = dict_lookup(d, &last);
477 while(!dict_contains(todo, stroke)) {
484 next_todo = next_todo->next;
493 static windcontext_t onepolygon = {1};
494 gfxline_t* gfxpoly_circular_to_evenodd(gfxline_t*line, double gridsize)
496 gfxpoly_t*poly = gfxpoly_from_fill(line, gridsize);
497 gfxpoly_t*poly2 = gfxpoly_process(poly, 0, &windrule_circular, &onepolygon);
498 gfxline_t*line2 = gfxline_from_gfxpoly(poly2);
499 gfxpoly_destroy(poly);
500 gfxpoly_destroy(poly2);
504 gfxpoly_t* gfxpoly_createbox(double x1, double y1,double x2, double y2, double gridsize)
506 gfxline_t* line = gfxline_makerectangle(x1, y1, x2, y2);
507 gfxpoly_t* poly = gfxpoly_from_fill(line, gridsize);