removed annoying bfchar warnings
[swftools.git] / lib / gfxpoly / convert.c
1 #include <stdlib.h>
2 #include <math.h>
3 #include <string.h>
4 #include "../gfxdevice.h"
5 #include "../mem.h"
6 #include "poly.h"
7 #include "convert.h"
8 #include "wind.h"
9
10 /* factor that determines into how many line fragments a spline is converted */
11 #define SUBFRACTION (2.4)
12
13 static inline int32_t convert_coord(double x, double z)
14 {
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
18     */
19     x *= z;
20     if(x < -0x2000000) x = -0x2000000;
21     if(x >  0x1ffffff) x =  0x1ffffff;
22     return ceil(x);
23 }
24
25 static void convert_gfxline(gfxline_t*line, polywriter_t*w, double gridsize)
26 {
27     assert(!line || line[0].type == gfx_moveTo);
28     double lastx=0,lasty=0;
29     double z = 1.0 / gridsize;
30     while(line) {
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));
34             }
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);
40             if(!parts) parts = 1;
41             double stepsize = 1.0/parts;
42             int i;
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));
48             }
49             w->lineto(w, convert_coord(line->x,z), convert_coord(line->y,z));
50         }
51         lastx = line->x;
52         lasty = line->y;
53         line = line->next;
54     }
55 }
56
57 static char* readline(FILE*fi)
58 {
59     char c;
60     while(1) {
61         int l = fread(&c, 1, 1, fi);
62         if(!l)
63             return 0;
64         if(c!=10 || c!=13)
65             break;
66     }
67     char line[256];
68     int pos = 0;
69     while(1) {
70         line[pos++] = c;
71         line[pos] = 0;
72         int l = fread(&c, 1, 1, fi);
73         if(!l || c==10 || c==13) {
74             return strdup(line);
75         }
76     }
77 }
78
79 static void convert_file(const char*filename, polywriter_t*w, double gridsize)
80 {
81     FILE*fi = fopen(filename, "rb");
82     if(!fi) {
83         perror(filename);
84     }
85     double z = 1.0 / gridsize;
86     int count = 0;
87     double g = 0;
88     double lastx=0,lasty=0;
89     while(1) {
90         char*line = readline(fi);
91         if(!line)
92             break;
93         double x,y;
94         char s[256];
95         if(sscanf(line, "%lf %lf %s", &x, &y, (char*)&s) == 3) {
96             if(s && !strcmp(s,"moveto")) {
97                 w->moveto(w, convert_coord(x,z), convert_coord(y,z));
98                 count++;
99             } else if(s && !strcmp(s,"lineto")) {
100                 w->lineto(w, convert_coord(x,z), convert_coord(y,z));
101                 count++;
102             } else {
103                 fprintf(stderr, "invalid command: %s\n", s);
104             }
105         } else if(sscanf(line, "%% gridsize %lf", &g) == 1) {
106             gridsize = g;
107             z = 1.0 / gridsize;
108             w->setgridsize(w, g);
109         }
110         free(line);
111     }
112     fclose(fi);
113     if(g) {
114         fprintf(stderr, "loaded %d points from %s (gridsize %f)\n", count, filename, g);
115     } else {
116         fprintf(stderr, "loaded %d points from %s\n", count, filename);
117     }
118 }
119
120 typedef struct _compactpoly {
121     gfxpoly_t*poly;
122     point_t last;
123     point_t*points;
124     int num_points;
125     int points_size;
126     segment_dir_t dir;
127     char new;
128 } compactpoly_t;
129
130 void finish_segment(compactpoly_t*data)
131 {
132     if(data->num_points <= 1)
133         return;
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;
139     s->dir = data->dir;
140     s->points = p;
141     assert(data->dir != DIR_UNKNOWN);
142     if(data->dir == DIR_UP) {
143         int t;
144         int s = data->num_points;
145         for(t=0;t<data->num_points;t++) {
146             p[--s] = data->points[t];
147         }
148     } else {
149         memcpy(p, data->points, sizeof(point_t)*data->num_points);
150     }
151 #ifdef CHECKS
152     int t;
153     for(t=0;t<data->num_points-1;t++) {
154         assert(p[t].y<=p[t+1].y);
155     }
156 #endif
157 }
158 static void compactmoveto(polywriter_t*w, int32_t x, int32_t y)
159 {
160     compactpoly_t*data = (compactpoly_t*)w->internal;
161     point_t p;
162     p.x = x;
163     p.y = y;
164     if(p.x != data->last.x || p.y != data->last.y) {
165         data->new = 1;
166     }
167     data->last = p;
168 }
169 static void compactlineto(polywriter_t*w, int32_t x, int32_t y)
170 {
171     compactpoly_t*data = (compactpoly_t*)w->internal;
172     point_t p;
173     p.x = x;
174     p.y = y;
175     if(p.x == data->last.x && p.y == data->last.y)
176         return;
177
178     if((p.y < data->last.y && data->dir != DIR_UP) ||
179        (p.y > data->last.y && data->dir != DIR_DOWN) || data->new) {
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;
184     }
185     data->new = 0;
186
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);
191     }
192     data->points[data->num_points++] = p;
193     data->last = p;
194 }
195 static void compactsetgridsize(polywriter_t*w, double gridsize)
196 {
197     compactpoly_t*d = (compactpoly_t*)w->internal;
198     d->poly->gridsize = gridsize;
199 }
200 /*static int compare_stroke(const void*_s1, const void*_s2)
201 {
202     gfxpolystroke_t*s1 = (gfxpolystroke_t*)_s1;
203     gfxpolystroke_t*s2 = (gfxpolystroke_t*)_s2;
204     return s1->points[0].y - s2->points[0].y;
205 }*/
206 static void*compactfinish(polywriter_t*w)
207 {
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);
211     free(data->points);
212     gfxpoly_t*poly = data->poly;
213     free(w->internal);w->internal = 0;
214     return (void*)poly;
215 }
216 void gfxpolywriter_init(polywriter_t*w)
217 {
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;
228     data->new = 1;
229     data->dir = DIR_UNKNOWN;
230     data->points = (point_t*)rfx_alloc(sizeof(point_t)*data->points_size);
231     data->poly->strokes = 0;
232 }
233
234 gfxpoly_t* gfxpoly_from_fill(gfxline_t*line, double gridsize)
235 {
236     polywriter_t writer;
237     gfxpolywriter_init(&writer);
238     writer.setgridsize(&writer, gridsize);
239     convert_gfxline(line, &writer, gridsize);
240     return (gfxpoly_t*)writer.finish(&writer);
241 }
242 gfxpoly_t* gfxpoly_from_file(const char*filename, double gridsize)
243 {
244     polywriter_t writer;
245     gfxpolywriter_init(&writer);
246     writer.setgridsize(&writer, gridsize);
247     convert_file(filename, &writer, gridsize);
248     return (gfxpoly_t*)writer.finish(&writer);
249 }
250 void gfxpoly_destroy(gfxpoly_t*poly)
251 {
252     int t;
253     gfxpolystroke_t*stroke = poly->strokes;
254     while(stroke) {
255         gfxpolystroke_t*next = stroke->next;
256         free(stroke->points);
257         free(stroke);
258         stroke = next;
259     }
260     free(poly);
261 }
262
263 typedef struct _polydraw_internal
264 {
265     double lx, ly;
266     int32_t lastx, lasty;
267     int32_t x0, y0;
268     double z;
269     char last;
270     polywriter_t writer;
271 } polydraw_internal_t;
272
273 static void polydraw_moveTo(gfxdrawer_t*d, gfxcoord_t _x, gfxcoord_t _y)
274 {
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);
280     }
281     i->lx = _x;
282     i->ly = _y;
283     i->x0 = x;
284     i->y0 = y;
285     i->lastx = x;
286     i->lasty = y;
287     i->last = 1;
288 }
289 static void polydraw_lineTo(gfxdrawer_t*d, gfxcoord_t _x, gfxcoord_t _y)
290 {
291     polydraw_internal_t*i = (polydraw_internal_t*)d->internal;
292     if(!i->last) {
293         polydraw_moveTo(d, _x, _y);
294         return;
295     }
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);
300     }
301     i->lx = _x;
302     i->ly = _y;
303     i->lastx = x;
304     i->lasty = y;
305     i->last = 1;
306 }
307 static void polydraw_splineTo(gfxdrawer_t*d, gfxcoord_t sx, gfxcoord_t sy, gfxcoord_t x, gfxcoord_t y)
308 {
309     polydraw_internal_t*i = (polydraw_internal_t*)d->internal;
310     if(!i->last) {
311         polydraw_moveTo(d, x, y);
312         return;
313     }
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;
317     int t;
318     int32_t nx,ny;
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;
325         }
326     }
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);
331     }
332     i->lx = x;
333     i->ly = y;
334     i->lastx = nx; 
335     i->lasty = ny;
336     i->last = 1;
337 }
338 static void polydraw_close(gfxdrawer_t*d)
339 {
340     polydraw_internal_t*i = (polydraw_internal_t*)d->internal;
341     assert(!(i->last && (i->x0 == INVALID_COORD || i->y0 == INVALID_COORD)));
342     if(!i->last)
343         return;
344     if(i->lastx != i->x0 || i->lasty != i->y0) {
345         i->writer.lineto(&i->writer, i->x0, i->y0);
346         i->lastx = i->x0;
347         i->lasty = i->y0;
348     }
349     i->last = 0;
350     i->x0 = INVALID_COORD;
351     i->y0 = INVALID_COORD;
352 }
353 static void* polydraw_result(gfxdrawer_t*d)
354 {
355     polydraw_internal_t*i = (polydraw_internal_t*)d->internal;
356     void*result = i->writer.finish(&i->writer);
357     rfx_free(i);
358     memset(d, 0, sizeof(gfxdrawer_t));
359     return result;
360 }
361
362 void gfxdrawer_target_poly(gfxdrawer_t*d, double gridsize)
363 {
364     polydraw_internal_t*i = (polydraw_internal_t*)rfx_calloc(sizeof(polydraw_internal_t));
365     d->internal = i;
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;
378 }
379
380 #if 0
381 gfxline_t*gfxline_from_gfxpoly(gfxpoly_t*poly)
382 {
383     gfxpolystroke_t*stroke;
384     int count = 0;
385     for(stroke=poly->strokes;stroke;stroke=stroke->next) {
386         assert(stroke->num_points);
387         count += stroke->num_points;
388     }
389     if(!count) return 0;
390     gfxline_t*l = malloc(sizeof(gfxline_t)*count);
391     count = 0;
392     /* TODO: it might make sense to concatenate strokes */
393     for(stroke=poly->strokes;stroke;stroke=stroke->next) {
394         int t;
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];
400         }
401         l[count].type = gfx_moveTo;
402         count+=stroke->num_points;
403     }
404     l[count-1].next = 0;
405     return l;
406 }
407 #endif
408
409 static gfxline_t*mkgfxline(gfxpoly_t*poly, char preserve_direction)
410 {
411     gfxpolystroke_t*stroke;
412     int count = 0;
413     if(!poly->strokes)
414         return 0;
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 x_min=stroke_min->points[0].x;
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);
426             if(!preserve_direction)
427                 dict_put(d, &stroke->points[0], stroke);
428         } else {
429             dict_put(d, &stroke->points[0], stroke);
430             if(!preserve_direction)
431                 dict_put(d, &stroke->points[stroke->num_points-1], stroke);
432         }
433         if(stroke->points[0].y < y_min ||
434            (stroke->points[0].y == y_min && stroke->points[0].x < x_min)) {
435             y_min = stroke->points[0].y;
436             stroke_min = stroke;
437         }
438     }
439     gfxpolystroke_t*next_todo = poly->strokes;
440     gfxline_t*l = malloc(sizeof(gfxline_t)*count);
441     count = 0;
442     stroke = stroke_min;
443     
444     point_t last = {INVALID_COORD, INVALID_COORD};
445     char should_connect = 0;
446     while(stroke) {
447         if(stroke && !preserve_direction) {
448             char del1 = dict_del2(d, &stroke->points[0], stroke);
449             char del2 = dict_del2(d, &stroke->points[stroke->num_points-1], stroke);
450             assert(del1 && del2);
451         }
452         assert(dict_contains(todo, stroke));
453         int t;
454         int pos = 0;
455         int incr = 1;
456         if(preserve_direction) {
457             if(stroke->dir == DIR_UP) {
458                 pos = stroke->num_points-1;
459                 incr = -1;
460             }
461         } else {
462             // try to find matching point on either end.
463             // Prefer downward.
464             if(last.x == stroke->points[stroke->num_points-1].x &&
465                last.y == stroke->points[stroke->num_points-1].y) {
466                 pos = stroke->num_points-1;
467                 incr = -1;
468             }
469         }
470         if(last.x != stroke->points[pos].x || last.y != stroke->points[pos].y) {
471             l[count].x = stroke->points[pos].x * poly->gridsize;
472             l[count].y = stroke->points[pos].y * poly->gridsize;
473             l[count].type = gfx_moveTo;
474             l[count].next = &l[count+1];
475             count++;
476             assert(!should_connect);
477         }
478         pos += incr;
479         for(t=1;t<stroke->num_points;t++) {
480             l[count].x = stroke->points[pos].x * poly->gridsize;
481             l[count].y = stroke->points[pos].y * poly->gridsize;
482             l[count].type = gfx_lineTo;
483             l[count].next = &l[count+1];
484             count++;
485             pos += incr;
486         }
487         last = stroke->points[pos-incr];
488         char del = dict_del(todo, stroke);
489         assert(del);
490         assert(!dict_contains(todo, stroke));
491
492         /* try to find a poly which starts at the point we drew last */
493         stroke = dict_lookup(d, &last);
494         should_connect = 1;
495         while(!dict_contains(todo, stroke)) {
496             should_connect = 0;
497             stroke = next_todo;
498             if(!next_todo) {
499                 stroke = 0;
500                 break;
501             }
502             next_todo = next_todo->next;
503         }
504     }
505     l[count-1].next = 0;
506     dict_destroy(todo);
507     dict_destroy(d);
508     return l;
509 }
510
511 gfxline_t*gfxline_from_gfxpoly(gfxpoly_t*poly)
512 {
513     return mkgfxline(poly, 0);
514 }
515
516 gfxline_t*gfxline_from_gfxpoly_updown(gfxpoly_t*poly)
517 {
518     return mkgfxline(poly, 1);
519 }
520
521 static windcontext_t onepolygon = {1};
522 gfxline_t* gfxpoly_circular_to_evenodd(gfxline_t*line, double gridsize)
523 {
524     gfxpoly_t*poly = gfxpoly_from_fill(line, gridsize);
525     gfxpoly_t*poly2 = gfxpoly_process(poly, 0, &windrule_circular, &onepolygon);
526     gfxline_t*line2 = gfxline_from_gfxpoly(poly2);
527     gfxpoly_destroy(poly);
528     gfxpoly_destroy(poly2);
529     return line2;
530 }
531
532 gfxpoly_t* gfxpoly_createbox(double x1, double y1,double x2, double y2, double gridsize)
533 {
534     gfxline_t* line = gfxline_makerectangle(x1, y1, x2, y2);
535     gfxpoly_t* poly = gfxpoly_from_fill(line, gridsize);
536     gfxline_free(line);
537     return poly;
538 }
539