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 26 bit because:
16 a) we use a (x1-x2) shortcut when comparing coordinates
17 b) we need to be able to multiply two coordinates and store them in a double w/o loss of precision
20 if(x < -0x2000000) x = -0x2000000;
21 if(x > 0x1ffffff) x = 0x1ffffff;
25 static void convert_gfxline(gfxline_t*line, polywriter_t*w, double gridsize)
27 assert(!line || line[0].type == gfx_moveTo);
28 double lastx=0,lasty=0;
29 double z = 1.0 / gridsize;
31 if(line->type == gfx_moveTo) {
32 if(line->next && line->next->type != gfx_moveTo && (line->x!=lastx || line->y!=lasty)) {
33 w->moveto(w, convert_coord(line->x,z), convert_coord(line->y,z));
35 } else if(line->type == gfx_lineTo) {
36 w->lineto(w, convert_coord(line->x,z), convert_coord(line->y,z));
37 } else if(line->type == gfx_splineTo) {
38 int parts = (int)(sqrt(fabs(line->x-2*line->sx+lastx) +
39 fabs(line->y-2*line->sy+lasty))*SUBFRACTION);
41 double stepsize = 1.0/parts;
43 for(i=0;i<parts;i++) {
44 double t = (double)i*stepsize;
45 double sx = (line->x*t*t + 2*line->sx*t*(1-t) + lastx*(1-t)*(1-t));
46 double sy = (line->y*t*t + 2*line->sy*t*(1-t) + lasty*(1-t)*(1-t));
47 w->lineto(w, convert_coord(sx,z), convert_coord(sy,z));
49 w->lineto(w, convert_coord(line->x,z), convert_coord(line->y,z));
57 static char* readline(FILE*fi)
61 int l = fread(&c, 1, 1, fi);
72 int l = fread(&c, 1, 1, fi);
73 if(!l || c==10 || c==13) {
79 static void convert_file(const char*filename, polywriter_t*w, double gridsize)
81 FILE*fi = fopen(filename, "rb");
85 double z = 1.0 / gridsize;
88 double lastx=0,lasty=0;
90 char*line = readline(fi);
95 if(sscanf(line, "%lf %lf %s", &x, &y, &s) == 3) {
96 if(s && !strcmp(s,"moveto")) {
97 w->moveto(w, convert_coord(x,z), convert_coord(y,z));
99 } else if(s && !strcmp(s,"lineto")) {
100 w->lineto(w, convert_coord(x,z), convert_coord(y,z));
103 fprintf(stderr, "invalid command: %s\n", s);
105 } else if(sscanf(line, "%% gridsize %lf", &g) == 1) {
108 w->setgridsize(w, g);
114 fprintf(stderr, "loaded %d points from %s (gridsize %f)\n", count, filename, g);
116 fprintf(stderr, "loaded %d points from %s\n", count, filename);
120 typedef struct _compactpoly {
130 void finish_segment(compactpoly_t*data)
132 if(data->num_points <= 1)
134 point_t*p = malloc(sizeof(point_t)*data->num_points);
135 gfxpolystroke_t*s = rfx_calloc(sizeof(gfxpolystroke_t));
136 s->next = data->poly->strokes;
137 data->poly->strokes = s;
138 s->num_points = s->points_size = data->num_points;
141 assert(data->dir != DIR_UNKNOWN);
142 if(data->dir == DIR_UP) {
144 int s = data->num_points;
145 for(t=0;t<data->num_points;t++) {
146 p[--s] = data->points[t];
149 memcpy(p, data->points, sizeof(point_t)*data->num_points);
153 for(t=0;t<data->num_points-1;t++) {
154 assert(p[t].y<=p[t+1].y);
158 static void compactmoveto(polywriter_t*w, int32_t x, int32_t y)
160 compactpoly_t*data = (compactpoly_t*)w->internal;
164 if(p.x != data->last.x || p.y != data->last.y) {
169 static void compactlineto(polywriter_t*w, int32_t x, int32_t y)
171 compactpoly_t*data = (compactpoly_t*)w->internal;
175 if(p.x == data->last.x && p.y == data->last.y)
178 if(p.y < data->last.y && data->dir != DIR_UP ||
179 p.y > data->last.y && data->dir != DIR_DOWN ||
181 finish_segment(data);
182 data->dir = p.y > data->last.y ? DIR_DOWN : DIR_UP;
183 data->points[0] = data->last;
184 data->num_points = 1;
188 if(data->points_size == data->num_points) {
189 data->points_size <<= 1;
190 assert(data->points_size > data->num_points);
191 data->points = rfx_realloc(data->points, sizeof(point_t)*data->points_size);
193 data->points[data->num_points++] = p;
196 static void compactsetgridsize(polywriter_t*w, double gridsize)
198 compactpoly_t*d = (compactpoly_t*)w->internal;
199 d->poly->gridsize = gridsize;
201 /*static int compare_stroke(const void*_s1, const void*_s2)
203 gfxpolystroke_t*s1 = (gfxpolystroke_t*)_s1;
204 gfxpolystroke_t*s2 = (gfxpolystroke_t*)_s2;
205 return s1->points[0].y - s2->points[0].y;
207 static void*compactfinish(polywriter_t*w)
209 compactpoly_t*data = (compactpoly_t*)w->internal;
210 finish_segment(data);
211 //qsort(data->poly->strokes, data->poly->num_strokes, sizeof(gfxpolystroke_t), compare_stroke);
213 gfxpoly_t*poly = data->poly;
214 free(w->internal);w->internal = 0;
217 void gfxpolywriter_init(polywriter_t*w)
219 w->moveto = compactmoveto;
220 w->lineto = compactlineto;
221 w->setgridsize = compactsetgridsize;
222 w->finish = compactfinish;
223 compactpoly_t*data = w->internal = rfx_calloc(sizeof(compactpoly_t));
224 data->poly = rfx_calloc(sizeof(gfxpoly_t));
225 data->poly->gridsize = 1.0;
226 data->last.x = data->last.y = 0;
227 data->num_points = 0;
228 data->points_size = 16;
230 data->dir = DIR_UNKNOWN;
231 data->points = (point_t*)rfx_alloc(sizeof(point_t)*data->points_size);
232 data->poly->strokes = 0;
235 gfxpoly_t* gfxpoly_from_fill(gfxline_t*line, double gridsize)
238 gfxpolywriter_init(&writer);
239 writer.setgridsize(&writer, gridsize);
240 convert_gfxline(line, &writer, gridsize);
241 return (gfxpoly_t*)writer.finish(&writer);
243 gfxpoly_t* gfxpoly_from_file(const char*filename, double gridsize)
246 gfxpolywriter_init(&writer);
247 writer.setgridsize(&writer, gridsize);
248 convert_file(filename, &writer, gridsize);
249 return (gfxpoly_t*)writer.finish(&writer);
251 void gfxpoly_destroy(gfxpoly_t*poly)
254 gfxpolystroke_t*stroke = poly->strokes;
256 gfxpolystroke_t*next = stroke->next;
257 free(stroke->points);
264 typedef struct _polydraw_internal
267 int32_t lastx, lasty;
272 } polydraw_internal_t;
274 static void polydraw_moveTo(gfxdrawer_t*d, gfxcoord_t _x, gfxcoord_t _y)
276 polydraw_internal_t*i = (polydraw_internal_t*)d->internal;
277 int32_t x = convert_coord(_x, i->z);
278 int32_t y = convert_coord(_y, i->z);
279 if(i->lastx != x || i->lasty != y) {
280 i->writer.moveto(&i->writer, x, y);
290 static void polydraw_lineTo(gfxdrawer_t*d, gfxcoord_t _x, gfxcoord_t _y)
292 polydraw_internal_t*i = (polydraw_internal_t*)d->internal;
294 polydraw_moveTo(d, _x, _y);
297 int32_t x = convert_coord(_x, i->z);
298 int32_t y = convert_coord(_y, i->z);
299 if(i->lastx != x || i->lasty != y) {
300 i->writer.lineto(&i->writer, x, y);
308 static void polydraw_splineTo(gfxdrawer_t*d, gfxcoord_t sx, gfxcoord_t sy, gfxcoord_t x, gfxcoord_t y)
310 polydraw_internal_t*i = (polydraw_internal_t*)d->internal;
312 polydraw_moveTo(d, x, y);
315 double c = fabs(x-2*sx+i->lx) + fabs(y-2*sy+i->ly);
316 int parts = (int)(sqrt(c)*SUBFRACTION);
317 if(!parts) parts = 1;
320 for(t=0;t<parts;t++) {
321 nx = convert_coord((double)(t*t*x + 2*t*(parts-t)*sx + (parts-t)*(parts-t)*i->lx)/(double)(parts*parts), i->z);
322 ny = convert_coord((double)(t*t*y + 2*t*(parts-t)*sy + (parts-t)*(parts-t)*i->ly)/(double)(parts*parts), i->z);
323 if(nx != i->lastx || ny != i->lasty) {
324 i->writer.lineto(&i->writer, nx, ny);
325 i->lastx = nx; i->lasty = ny;
328 nx = convert_coord(x,i->z);
329 ny = convert_coord(y,i->z);
330 if(nx != i->lastx || ny != i->lasty) {
331 i->writer.lineto(&i->writer, nx, ny);
339 static void polydraw_close(gfxdrawer_t*d)
341 polydraw_internal_t*i = (polydraw_internal_t*)d->internal;
342 assert(!(i->last && (i->x0 == INVALID_COORD || i->y0 == INVALID_COORD)));
345 if(i->lastx != i->x0 || i->lasty != i->y0) {
346 i->writer.lineto(&i->writer, i->x0, i->y0);
351 i->x0 = INVALID_COORD;
352 i->y0 = INVALID_COORD;
354 static void* polydraw_result(gfxdrawer_t*d)
356 polydraw_internal_t*i = (polydraw_internal_t*)d->internal;
357 void*result = i->writer.finish(&i->writer);
359 memset(d, 0, sizeof(gfxdrawer_t));
363 void gfxdrawer_target_poly(gfxdrawer_t*d, double gridsize)
365 polydraw_internal_t*i = (polydraw_internal_t*)rfx_calloc(sizeof(polydraw_internal_t));
367 i->lastx = INVALID_COORD; // convert_coord can never return this value
368 i->lasty = INVALID_COORD;
369 i->x0 = INVALID_COORD;
370 i->y0 = INVALID_COORD;
371 d->moveTo = polydraw_moveTo;
372 d->lineTo = polydraw_lineTo;
373 d->splineTo = polydraw_splineTo;
374 d->close = polydraw_close;
375 d->result = polydraw_result;
376 gfxpolywriter_init(&i->writer);
377 i->writer.setgridsize(&i->writer, gridsize);
378 i->z = 1.0 / gridsize;
382 gfxline_t*gfxline_from_gfxpoly(gfxpoly_t*poly)
384 gfxpolystroke_t*stroke;
386 for(stroke=poly->strokes;stroke;stroke=stroke->next) {
387 assert(stroke->num_points);
388 count += stroke->num_points;
391 gfxline_t*l = malloc(sizeof(gfxline_t)*count);
393 /* TODO: it might make sense to concatenate strokes */
394 for(stroke=poly->strokes;stroke;stroke=stroke->next) {
396 for(t=0;t<stroke->num_points;t++) {
397 l[count+t].x = stroke->points[t].x * poly->gridsize;
398 l[count+t].y = stroke->points[t].y * poly->gridsize;
399 l[count+t].type = gfx_lineTo;
400 l[count+t].next = &l[count+t+1];
402 l[count].type = gfx_moveTo;
403 count+=stroke->num_points;
410 gfxline_t*gfxline_from_gfxpoly(gfxpoly_t*poly)
412 gfxpolystroke_t*stroke;
416 dict_t*d = dict_new2(&point_type);
417 dict_t*todo = dict_new2(&ptr_type);
418 gfxpolystroke_t*stroke_min= poly->strokes;
419 int32_t y_min=stroke_min->points[0].y;
420 for(stroke=poly->strokes;stroke;stroke=stroke->next) {
421 dict_put(todo, stroke, stroke);
422 assert(stroke->num_points>1);
423 count += stroke->num_points;
424 if(stroke->dir == DIR_UP) {
425 dict_put(d, &stroke->points[stroke->num_points-1], stroke);
427 dict_put(d, &stroke->points[0], stroke);
429 if(stroke->points[0].y < y_min) {
430 y_min = stroke->points[0].y;
434 gfxpolystroke_t*next_todo = poly->strokes;
435 gfxline_t*l = malloc(sizeof(gfxline_t)*count);
439 point_t last = {INVALID_COORD, INVALID_COORD};
440 char should_connect = 0;
442 assert(dict_contains(todo, stroke));
446 if(stroke->dir == DIR_UP) {
447 pos = stroke->num_points-1;
450 /* TODO: keeping the up/down order intact increases the polygon size
451 considerably. If this is going to be an even/odd polygon,
452 we could ignore it and save some space */
453 if(last.x != stroke->points[pos].x || last.y != stroke->points[pos].y) {
454 l[count].x = stroke->points[pos].x * poly->gridsize;
455 l[count].y = stroke->points[pos].y * poly->gridsize;
456 l[count].type = gfx_moveTo;
457 l[count].next = &l[count+1];
459 assert(!should_connect);
462 for(t=1;t<stroke->num_points;t++) {
463 l[count].x = stroke->points[pos].x * poly->gridsize;
464 l[count].y = stroke->points[pos].y * poly->gridsize;
465 l[count].type = gfx_lineTo;
466 l[count].next = &l[count+1];
470 last = stroke->points[pos-incr];
471 char del = dict_del(todo, stroke);
473 assert(!dict_contains(todo, stroke));
475 /* try to find a poly which starts at the point we drew last */
476 stroke = dict_lookup(d, &last);
478 while(!dict_contains(todo, stroke)) {
485 next_todo = next_todo->next;
494 static windcontext_t onepolygon = {1};
495 gfxline_t* gfxpoly_circular_to_evenodd(gfxline_t*line, double gridsize)
497 gfxpoly_t*poly = gfxpoly_from_fill(line, gridsize);
498 gfxpoly_t*poly2 = gfxpoly_process(poly, 0, &windrule_circular, &onepolygon);
499 gfxline_t*line2 = gfxline_from_gfxpoly(poly2);
500 gfxpoly_destroy(poly);
501 gfxpoly_destroy(poly2);
505 gfxpoly_t* gfxpoly_createbox(double x1, double y1,double x2, double y2, double gridsize)
507 gfxline_t* line = gfxline_makerectangle(x1, y1, x2, y2);
508 gfxpoly_t* poly = gfxpoly_from_fill(line, gridsize);