4 #include "../gfxdevice.h"
9 /* factor that determines into how many line fragments a spline is converted */
10 #define SUBFRACTION (2.4)
12 static edge_t*edge_new(int x1, int y1, int x2, int y2)
14 edge_t*s = rfx_calloc(sizeof(edge_t));
22 static inline int32_t convert_coord(double x, double z)
24 /* we clamp to 31 bit instead of 32 bit because we use
25 a (x1-x2) shortcut when comparing coordinates
28 if(x < -0x40000000) x = -0x40000000;
29 if(x > 0x3fffffff) x = 0x3fffffff;
33 static void convert_gfxline(gfxline_t*line, polywriter_t*w, double gridsize)
35 assert(!line || line[0].type == gfx_moveTo);
36 double lastx=0,lasty=0;
37 double z = 1.0 / gridsize;
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));
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);
49 double stepsize = 1.0/parts;
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));
57 w->lineto(w, convert_coord(line->x,z), convert_coord(line->y,z));
65 static char* readline(FILE*fi)
69 int l = fread(&c, 1, 1, fi);
80 int l = fread(&c, 1, 1, fi);
81 if(!l || c==10 || c==13) {
87 static void convert_file(const char*filename, polywriter_t*w, double gridsize)
89 FILE*fi = fopen(filename, "rb");
93 double z = 1.0 / gridsize;
96 double lastx=0,lasty=0;
98 char*line = readline(fi);
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));
107 } else if(s && !strcmp(s,"lineto")) {
108 w->lineto(w, convert_coord(x,z), convert_coord(y,z));
111 fprintf(stderr, "invalid command: %s\n", s);
113 } else if(sscanf(line, "%% gridsize %lf", &g) == 1) {
116 w->setgridsize(w, g);
122 fprintf(stderr, "loaded %d points from %s (gridsize %f)\n", count, filename, g);
124 fprintf(stderr, "loaded %d points from %s\n", count, filename);
128 typedef struct _stdpoly {
133 static void stdmoveto(polywriter_t*w, int x, int y)
135 stdpoly_t*d = (stdpoly_t*)w->internal;
136 d->lastx = x;d->lasty = y;
138 static void stdlineto(polywriter_t*w, int x, int y)
140 stdpoly_t*d = (stdpoly_t*)w->internal;
145 if(x1!=x2 || y1!=y2) {
146 edge_t*s = edge_new(x1, y1, x2, y2);
147 s->next = d->poly->edges;
150 d->lastx = x;d->lasty = y;
152 static void stdsetgridsize(polywriter_t*w, double gridsize)
154 stdpoly_t*d = (stdpoly_t*)w->internal;
155 d->poly->gridsize = gridsize;
157 static void* stdfinish(polywriter_t*w)
159 stdpoly_t*d = (stdpoly_t*)w->internal;
160 gfxpoly_t*poly = d->poly;
161 free(w->internal);w->internal = 0;
164 void gfxpolywriter_init(polywriter_t*w)
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);
176 typedef struct _compactpoly {
177 gfxcompactpoly_t*poly;
187 void finish_segment(compactpoly_t*data)
189 if(data->num_points <= 1)
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);
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;
201 assert(data->dir != DIR_UNKNOWN);
202 if(data->dir == DIR_UP) {
204 int s = data->num_points;
205 for(t=0;t<data->num_points;t++) {
206 p[--s] = data->points[t];
209 memcpy(p, data->points, sizeof(point_t)*data->num_points);
213 for(t=0;t<data->num_points-1;t++) {
214 assert(p[t].y<=p[t+1].y);
217 data->poly->num_strokes++;
219 static void compactmoveto(polywriter_t*w, int x, int y)
221 compactpoly_t*data = (compactpoly_t*)w->internal;
225 if(p.x != data->last.x || p.y != data->last.y) {
230 static void compactlineto(polywriter_t*w, int x, int y)
232 compactpoly_t*data = (compactpoly_t*)w->internal;
236 if(p.x == data->last.x && p.y == data->last.y)
239 if(p.y < data->last.y && data->dir != DIR_UP ||
240 p.y > data->last.y && data->dir != DIR_DOWN ||
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;
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);
253 data->points[data->num_points++] = p;
256 static void compactsetgridsize(polywriter_t*w, double gridsize)
258 compactpoly_t*d = (compactpoly_t*)w->internal;
259 d->poly->gridsize = gridsize;
261 /*static int compare_stroke(const void*_s1, const void*_s2)
263 gfxpolystroke_t*s1 = (gfxpolystroke_t*)_s1;
264 gfxpolystroke_t*s2 = (gfxpolystroke_t*)_s2;
265 return s1->points[0].y - s2->points[0].y;
267 static void*compactfinish(polywriter_t*w)
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);
274 gfxcompactpoly_t*poly = data->poly;
275 free(w->internal);w->internal = 0;
278 void gfxcompactpolywriter_init(polywriter_t*w)
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;
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);
297 gfxpoly_t* gfxpoly_from_gfxline(gfxline_t*line, double gridsize)
300 gfxpolywriter_init(&w);
301 w.setgridsize(&w, gridsize);
302 convert_gfxline(line, &w, gridsize);
305 gfxpoly_t* gfxpoly_from_file(const char*filename, double gridsize)
308 gfxpolywriter_init(&w);
309 w.setgridsize(&w, gridsize);
310 convert_file(filename, &w, gridsize);
313 gfxcompactpoly_t* gfxcompactpoly_from_gfxline(gfxline_t*line, double gridsize)
316 gfxcompactpolywriter_init(&writer);
317 writer.setgridsize(&writer, gridsize);
318 convert_gfxline(line, &writer, gridsize);
319 return (gfxcompactpoly_t*)writer.finish(&writer);
321 gfxcompactpoly_t* gfxcompactpoly_from_file(const char*filename, double gridsize)
324 gfxcompactpolywriter_init(&writer);
325 writer.setgridsize(&writer, gridsize);
326 convert_file(filename, &writer, gridsize);
327 return (gfxcompactpoly_t*)writer.finish(&writer);
329 gfxpoly_t*gfxpoly_from_gfxcompactpoly(gfxcompactpoly_t*poly)
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];
340 if(stroke->dir == DIR_UP) {
341 e = edge_new(a.x,a.y,b.x,b.y);
343 e = edge_new(b.x,b.y,a.x,a.y);
345 e->style = stroke->fs;
346 e->next = poly2->edges;
352 void gfxcompactpoly_destroy(gfxcompactpoly_t*poly)
355 for(t=0;t<poly->num_strokes;t++) {
356 free(poly->strokes[t].points);
357 poly->strokes[t].points = 0;