213cdad24a546444c6d59436b847b1332bd90496
[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
9 /* factor that determines into how many line fragments a spline is converted */
10 #define SUBFRACTION (2.4)
11
12 static edge_t*edge_new(int x1, int y1, int x2, int y2)
13 {
14     edge_t*s = rfx_calloc(sizeof(edge_t));
15     s->a.x = x1;
16     s->a.y = y1;
17     s->b.x = x2;
18     s->b.y = y2;
19     return s;
20 }
21
22 static inline int32_t convert_coord(double x, double z)
23 {
24     /* we clamp to 31 bit instead of 32 bit because we use
25        a (x1-x2) shortcut when comparing coordinates
26     */
27     x *= z;
28     if(x < -0x40000000) x = -0x40000000;
29     if(x >  0x3fffffff) x =  0x3fffffff;
30     return ceil(x);
31 }
32
33 static void convert_gfxline(gfxline_t*line, polywriter_t*w, double gridsize)
34 {
35     assert(!line || line[0].type == gfx_moveTo);
36     double lastx=0,lasty=0;
37     double z = 1.0 / gridsize;
38     while(line) {
39         if(line->type == gfx_moveTo) {
40             if(line->next && line->next->type != gfx_moveTo && (line->x!=lastx || line->y!=lasty)) {
41                 w->moveto(w, convert_coord(line->x,z), convert_coord(line->y,z));
42             }
43         } else if(line->type == gfx_lineTo) {
44             w->lineto(w, convert_coord(line->x,z), convert_coord(line->y,z));
45         } else if(line->type == gfx_splineTo) {
46             int parts = (int)(sqrt(fabs(line->x-2*line->sx+lastx) + 
47                                    fabs(line->y-2*line->sy+lasty))*SUBFRACTION);
48             if(!parts) parts = 1;
49             double stepsize = 1.0/parts;
50             int i;
51             for(i=0;i<parts;i++) {
52                 double t = (double)i*stepsize;
53                 double sx = (line->x*t*t + 2*line->sx*t*(1-t) + lastx*(1-t)*(1-t));
54                 double sy = (line->y*t*t + 2*line->sy*t*(1-t) + lasty*(1-t)*(1-t));
55                 w->lineto(w, convert_coord(sx,z), convert_coord(sy,z));
56             }
57             w->lineto(w, convert_coord(line->x,z), convert_coord(line->y,z));
58         }
59         lastx = line->x;
60         lasty = line->y;
61         line = line->next;
62     }
63 }
64
65 static char* readline(FILE*fi)
66 {
67     char c;
68     while(1) {
69         int l = fread(&c, 1, 1, fi);
70         if(!l)
71             return 0;
72         if(c!=10 || c!=13)
73             break;
74     }
75     char line[256];
76     int pos = 0;
77     while(1) {
78         line[pos++] = c;
79         line[pos] = 0;
80         int l = fread(&c, 1, 1, fi);
81         if(!l || c==10 || c==13) {
82             return strdup(line);
83         }
84     }
85 }
86
87 static void convert_file(const char*filename, polywriter_t*w, double gridsize)
88 {
89     FILE*fi = fopen(filename, "rb");
90     if(!fi) {
91         perror(filename);
92     }
93     double z = 1.0 / gridsize;
94     int count = 0;
95     double g = 0;
96     double lastx=0,lasty=0;
97     while(1) {
98         char*line = readline(fi);
99         if(!line)
100             break;
101         double x,y;
102         char s[256];
103         if(sscanf(line, "%lf %lf %s", &x, &y, &s) == 3) {
104             if(s && !strcmp(s,"moveto")) {
105                 w->moveto(w, convert_coord(x,z), convert_coord(y,z));
106                 count++;
107             } else if(s && !strcmp(s,"lineto")) {
108                 w->lineto(w, convert_coord(x,z), convert_coord(y,z));
109                 count++;
110             } else {
111                 fprintf(stderr, "invalid command: %s\n", s);
112             }
113         } else if(sscanf(line, "%% gridsize %lf", &g) == 1) {
114             gridsize = g;
115             z = 1.0 / gridsize;
116             w->setgridsize(w, g);
117         }
118         free(line);
119     }
120     fclose(fi);
121     if(g) {
122         fprintf(stderr, "loaded %d points from %s (gridsize %f)\n", count, filename, g);
123     } else {
124         fprintf(stderr, "loaded %d points from %s\n", count, filename);
125     }
126 }
127
128 typedef struct _stdpoly {
129     gfxpoly_t*poly;
130     double lastx,lasty;
131 } stdpoly_t;
132
133 static void stdmoveto(polywriter_t*w, int x, int y)
134 {
135     stdpoly_t*d = (stdpoly_t*)w->internal;
136     d->lastx = x;d->lasty = y;
137 }
138 static void stdlineto(polywriter_t*w, int x, int y)
139 {
140     stdpoly_t*d = (stdpoly_t*)w->internal;
141     int x1 = d->lastx;
142     int y1 = d->lasty;
143     int x2 = x;
144     int y2 = y;
145     if(x1!=x2 || y1!=y2) {
146         edge_t*s = edge_new(x1, y1, x2, y2);
147         s->next = d->poly->edges;
148         d->poly->edges = s;
149     }
150     d->lastx = x;d->lasty = y;
151 }
152 static void stdsetgridsize(polywriter_t*w, double gridsize)
153 {
154     stdpoly_t*d = (stdpoly_t*)w->internal;
155     d->poly->gridsize = gridsize;
156 }
157 static void* stdfinish(polywriter_t*w)
158 {
159     stdpoly_t*d = (stdpoly_t*)w->internal;
160     gfxpoly_t*poly =  d->poly;
161     free(w->internal);w->internal = 0;
162     return poly;
163 }
164 void gfxpolywriter_init(polywriter_t*w)
165 {
166     w->moveto = stdmoveto;
167     w->lineto = stdlineto;
168     w->setgridsize = stdsetgridsize;
169     w->finish = stdfinish;
170     stdpoly_t*data = w->internal = malloc(sizeof(stdpoly_t));
171     data->poly = gfxpoly_new(1.0);
172     data->lastx = 0;
173     data->lasty = 0;
174 }
175
176 typedef struct _compactpoly {
177     gfxcompactpoly_t*poly;
178     point_t last;
179     int strokes_size;
180     point_t*points;
181     int num_points;
182     int points_size;
183     segment_dir_t dir;
184     char new;
185 } compactpoly_t;
186
187 void finish_segment(compactpoly_t*data)
188 {
189     if(data->num_points <= 1)
190         return;
191     if(data->poly->num_strokes == data->strokes_size) {
192         data->strokes_size <<= 1;
193         assert(data->strokes_size > data->poly->num_strokes);
194         data->poly->strokes = rfx_realloc(data->poly->strokes, sizeof(gfxpolystroke_t)*data->strokes_size);
195     }
196     point_t*p = malloc(sizeof(point_t)*data->num_points);
197     gfxpolystroke_t*s = &data->poly->strokes[data->poly->num_strokes];
198     s->num_points = data->num_points;
199     s->dir = data->dir;
200     s->points = p;
201     assert(data->dir != DIR_UNKNOWN);
202     if(data->dir == DIR_UP) {
203         int t;
204         int s = data->num_points;
205         for(t=0;t<data->num_points;t++) {
206             p[--s] = data->points[t];
207         }
208     } else {
209         memcpy(p, data->points, sizeof(point_t)*data->num_points);
210     }
211 #ifdef CHECKS
212     int t;
213     for(t=0;t<data->num_points-1;t++) {
214         assert(p[t].y<=p[t+1].y);
215     }
216 #endif
217     data->poly->num_strokes++;
218 }
219 static void compactmoveto(polywriter_t*w, int x, int y)
220 {
221     compactpoly_t*data = (compactpoly_t*)w->internal;
222     point_t p;
223     p.x = x;
224     p.y = y;
225     if(p.x != data->last.x || p.y != data->last.y) {
226         data->new = 1;
227     }
228     data->last = p;
229 }
230 static void compactlineto(polywriter_t*w, int x, int y)
231 {
232     compactpoly_t*data = (compactpoly_t*)w->internal;
233     point_t p;
234     p.x = x;
235     p.y = y;
236     if(p.x == data->last.x && p.y == data->last.y)
237         return;
238
239     if(p.y < data->last.y && data->dir != DIR_UP ||
240        p.y > data->last.y && data->dir != DIR_DOWN || 
241        data->new) {
242         finish_segment(data);
243         data->dir = p.y > data->last.y ? DIR_DOWN : DIR_UP;
244         data->points[0] = data->last;
245         data->num_points = 1;
246     }
247
248     if(data->points_size == data->num_points) {
249         data->points_size <<= 1;
250         assert(data->points_size > data->num_points);
251         data->points = rfx_realloc(data->points, sizeof(point_t)*data->points_size);
252     }
253     data->points[data->num_points++] = p;
254     data->last = p;
255 }
256 static void compactsetgridsize(polywriter_t*w, double gridsize)
257 {
258     compactpoly_t*d = (compactpoly_t*)w->internal;
259     d->poly->gridsize = gridsize;
260 }
261 /*static int compare_stroke(const void*_s1, const void*_s2)
262 {
263     gfxpolystroke_t*s1 = (gfxpolystroke_t*)_s1;
264     gfxpolystroke_t*s2 = (gfxpolystroke_t*)_s2;
265     return s1->points[0].y - s2->points[0].y;
266 }*/
267 static void*compactfinish(polywriter_t*w)
268 {
269     compactpoly_t*data = (compactpoly_t*)w->internal;
270     finish_segment(data);
271     data->poly->strokes = (gfxpolystroke_t*)rfx_realloc(data->poly->strokes, sizeof(gfxpolystroke_t)*data->poly->num_strokes);
272     //qsort(data->poly->strokes, data->poly->num_strokes, sizeof(gfxpolystroke_t), compare_stroke);
273     free(data->points);
274     gfxcompactpoly_t*poly = data->poly;
275     free(w->internal);w->internal = 0;
276     return (void*)poly;
277 }
278 void gfxcompactpolywriter_init(polywriter_t*w)
279 {
280     w->moveto = compactmoveto;
281     w->lineto = compactlineto;
282     w->setgridsize = compactsetgridsize;
283     w->finish = compactfinish;
284     compactpoly_t*data = w->internal = rfx_calloc(sizeof(compactpoly_t));
285     data->poly = rfx_calloc(sizeof(gfxcompactpoly_t));
286     data->poly->gridsize = 1.0;
287     data->last.x = data->last.y = 0;
288     data->strokes_size = 16;
289     data->num_points = 0;
290     data->points_size = 16;
291     data->new = 1;
292     data->dir = DIR_UNKNOWN;
293     data->points = (point_t*)rfx_alloc(sizeof(point_t)*data->points_size);
294     data->poly->strokes = (gfxpolystroke_t*)rfx_alloc(sizeof(gfxpolystroke_t)*data->strokes_size);
295 }
296
297 gfxpoly_t* gfxpoly_from_gfxline(gfxline_t*line, double gridsize)
298 {
299     polywriter_t w;
300     gfxpolywriter_init(&w);
301     w.setgridsize(&w, gridsize);
302     convert_gfxline(line, &w, gridsize);
303     return w.finish(&w);
304 }
305 gfxpoly_t* gfxpoly_from_file(const char*filename, double gridsize)
306 {
307     polywriter_t w;
308     gfxpolywriter_init(&w);
309     w.setgridsize(&w, gridsize);
310     convert_file(filename, &w, gridsize);
311     return w.finish(&w);
312 }
313 gfxcompactpoly_t* gfxcompactpoly_from_gfxline(gfxline_t*line, double gridsize)
314 {
315     polywriter_t writer;
316     gfxcompactpolywriter_init(&writer);
317     writer.setgridsize(&writer, gridsize);
318     convert_gfxline(line, &writer, gridsize);
319     return (gfxcompactpoly_t*)writer.finish(&writer);
320 }
321 gfxcompactpoly_t* gfxcompactpoly_from_file(const char*filename, double gridsize)
322 {
323     polywriter_t writer;
324     gfxcompactpolywriter_init(&writer);
325     writer.setgridsize(&writer, gridsize);
326     convert_file(filename, &writer, gridsize);
327     return (gfxcompactpoly_t*)writer.finish(&writer);
328 }
329 gfxpoly_t*gfxpoly_from_gfxcompactpoly(gfxcompactpoly_t*poly)
330 {
331     int s,t;
332     int pass;
333     gfxpoly_t*poly2 = gfxpoly_new(poly->gridsize);
334     for(t=0;t<poly->num_strokes;t++) {
335         gfxpolystroke_t*stroke = &poly->strokes[t];
336         for(s=0;s<stroke->num_points-1;s++) {
337             point_t a = stroke->points[s];
338             point_t b = stroke->points[s+1];
339             edge_t*e = 0;
340             if(stroke->dir == DIR_UP) {
341                 e = edge_new(a.x,a.y,b.x,b.y);
342             } else {
343                 e = edge_new(b.x,b.y,a.x,a.y);
344             }
345             e->style = stroke->fs;
346             e->next = poly2->edges;
347             poly2->edges = e;
348         }
349     }
350     return poly2;
351 }
352 void gfxcompactpoly_destroy(gfxcompactpoly_t*poly)
353 {
354     int t;
355     for(t=0;t<poly->num_strokes;t++) {
356         free(poly->strokes[t].points);
357         poly->strokes[t].points = 0;
358     }
359     free(poly->strokes);
360     free(poly);
361 }
362