4 #include "../gfxdevice.h"
9 /* factor that determines into how many line fragments a spline is converted */
10 #define SUBFRACTION (2.4)
12 static inline int32_t convert_coord(double x, double z)
14 /* we clamp to 31 bit instead of 32 bit because we use
15 a (x1-x2) shortcut when comparing coordinates
18 if(x < -0x40000000) x = -0x40000000;
19 if(x > 0x3fffffff) x = 0x3fffffff;
23 static void convert_gfxline(gfxline_t*line, polywriter_t*w, double gridsize)
25 assert(!line || line[0].type == gfx_moveTo);
26 double lastx=0,lasty=0;
27 double z = 1.0 / gridsize;
29 if(line->type == gfx_moveTo) {
30 if(line->next && line->next->type != gfx_moveTo && (line->x!=lastx || line->y!=lasty)) {
31 w->moveto(w, convert_coord(line->x,z), convert_coord(line->y,z));
33 } else if(line->type == gfx_lineTo) {
34 w->lineto(w, convert_coord(line->x,z), convert_coord(line->y,z));
35 } else if(line->type == gfx_splineTo) {
36 int parts = (int)(sqrt(fabs(line->x-2*line->sx+lastx) +
37 fabs(line->y-2*line->sy+lasty))*SUBFRACTION);
39 double stepsize = 1.0/parts;
41 for(i=0;i<parts;i++) {
42 double t = (double)i*stepsize;
43 double sx = (line->x*t*t + 2*line->sx*t*(1-t) + lastx*(1-t)*(1-t));
44 double sy = (line->y*t*t + 2*line->sy*t*(1-t) + lasty*(1-t)*(1-t));
45 w->lineto(w, convert_coord(sx,z), convert_coord(sy,z));
47 w->lineto(w, convert_coord(line->x,z), convert_coord(line->y,z));
55 static char* readline(FILE*fi)
59 int l = fread(&c, 1, 1, fi);
70 int l = fread(&c, 1, 1, fi);
71 if(!l || c==10 || c==13) {
77 static void convert_file(const char*filename, polywriter_t*w, double gridsize)
79 FILE*fi = fopen(filename, "rb");
83 double z = 1.0 / gridsize;
86 double lastx=0,lasty=0;
88 char*line = readline(fi);
93 if(sscanf(line, "%lf %lf %s", &x, &y, &s) == 3) {
94 if(s && !strcmp(s,"moveto")) {
95 w->moveto(w, convert_coord(x,z), convert_coord(y,z));
97 } else if(s && !strcmp(s,"lineto")) {
98 w->lineto(w, convert_coord(x,z), convert_coord(y,z));
101 fprintf(stderr, "invalid command: %s\n", s);
103 } else if(sscanf(line, "%% gridsize %lf", &g) == 1) {
106 w->setgridsize(w, g);
112 fprintf(stderr, "loaded %d points from %s (gridsize %f)\n", count, filename, g);
114 fprintf(stderr, "loaded %d points from %s\n", count, filename);
118 typedef struct _compactpoly {
128 void finish_segment(compactpoly_t*data)
130 if(data->num_points <= 1)
132 point_t*p = malloc(sizeof(point_t)*data->num_points);
133 gfxpolystroke_t*s = rfx_calloc(sizeof(gfxpolystroke_t));
134 s->next = data->poly->strokes;
135 data->poly->strokes = s;
136 s->num_points = s->points_size = data->num_points;
139 assert(data->dir != DIR_UNKNOWN);
140 if(data->dir == DIR_UP) {
142 int s = data->num_points;
143 for(t=0;t<data->num_points;t++) {
144 p[--s] = data->points[t];
147 memcpy(p, data->points, sizeof(point_t)*data->num_points);
151 for(t=0;t<data->num_points-1;t++) {
152 assert(p[t].y<=p[t+1].y);
156 static void compactmoveto(polywriter_t*w, int32_t x, int32_t y)
158 compactpoly_t*data = (compactpoly_t*)w->internal;
162 if(p.x != data->last.x || p.y != data->last.y) {
167 static void compactlineto(polywriter_t*w, int32_t x, int32_t y)
169 compactpoly_t*data = (compactpoly_t*)w->internal;
173 if(p.x == data->last.x && p.y == data->last.y)
176 if(p.y < data->last.y && data->dir != DIR_UP ||
177 p.y > data->last.y && data->dir != DIR_DOWN ||
179 finish_segment(data);
180 data->dir = p.y > data->last.y ? DIR_DOWN : DIR_UP;
181 data->points[0] = data->last;
182 data->num_points = 1;
186 if(data->points_size == data->num_points) {
187 data->points_size <<= 1;
188 assert(data->points_size > data->num_points);
189 data->points = rfx_realloc(data->points, sizeof(point_t)*data->points_size);
191 data->points[data->num_points++] = p;
194 static void compactsetgridsize(polywriter_t*w, double gridsize)
196 compactpoly_t*d = (compactpoly_t*)w->internal;
197 d->poly->gridsize = gridsize;
199 /*static int compare_stroke(const void*_s1, const void*_s2)
201 gfxpolystroke_t*s1 = (gfxpolystroke_t*)_s1;
202 gfxpolystroke_t*s2 = (gfxpolystroke_t*)_s2;
203 return s1->points[0].y - s2->points[0].y;
205 static void*compactfinish(polywriter_t*w)
207 compactpoly_t*data = (compactpoly_t*)w->internal;
208 finish_segment(data);
209 //qsort(data->poly->strokes, data->poly->num_strokes, sizeof(gfxpolystroke_t), compare_stroke);
211 gfxpoly_t*poly = data->poly;
212 free(w->internal);w->internal = 0;
215 void gfxpolywriter_init(polywriter_t*w)
217 w->moveto = compactmoveto;
218 w->lineto = compactlineto;
219 w->setgridsize = compactsetgridsize;
220 w->finish = compactfinish;
221 compactpoly_t*data = w->internal = rfx_calloc(sizeof(compactpoly_t));
222 data->poly = rfx_calloc(sizeof(gfxpoly_t));
223 data->poly->gridsize = 1.0;
224 data->last.x = data->last.y = 0;
225 data->num_points = 0;
226 data->points_size = 16;
228 data->dir = DIR_UNKNOWN;
229 data->points = (point_t*)rfx_alloc(sizeof(point_t)*data->points_size);
230 data->poly->strokes = 0;
233 gfxpoly_t* gfxpoly_from_fill(gfxline_t*line, double gridsize)
236 gfxpolywriter_init(&writer);
237 writer.setgridsize(&writer, gridsize);
238 convert_gfxline(line, &writer, gridsize);
239 return (gfxpoly_t*)writer.finish(&writer);
241 gfxpoly_t* gfxpoly_from_file(const char*filename, double gridsize)
244 gfxpolywriter_init(&writer);
245 writer.setgridsize(&writer, gridsize);
246 convert_file(filename, &writer, gridsize);
247 return (gfxpoly_t*)writer.finish(&writer);
249 void gfxpoly_destroy(gfxpoly_t*poly)
252 gfxpolystroke_t*stroke = poly->strokes;
254 gfxpolystroke_t*next = stroke->next;
255 free(stroke->points);
262 typedef struct _polydraw_internal
265 int32_t lastx, lasty;
269 } polydraw_internal_t;
271 static void polydraw_moveTo(gfxdrawer_t*d, gfxcoord_t _x, gfxcoord_t _y)
273 polydraw_internal_t*i = (polydraw_internal_t*)d->internal;
274 int32_t x = convert_coord(_x, i->z);
275 int32_t y = convert_coord(_y, i->z);
276 if(i->lastx != x || i->lasty != y) {
277 i->writer.moveto(&i->writer, x, y);
285 static void polydraw_lineTo(gfxdrawer_t*d, gfxcoord_t _x, gfxcoord_t _y)
287 polydraw_internal_t*i = (polydraw_internal_t*)d->internal;
289 polydraw_moveTo(d, _x, _y);
292 int32_t x = convert_coord(_x, i->z);
293 int32_t y = convert_coord(_y, i->z);
294 if(i->lastx != x || i->lasty != y) {
295 i->writer.lineto(&i->writer, x, y);
303 static void polydraw_splineTo(gfxdrawer_t*d, gfxcoord_t sx, gfxcoord_t sy, gfxcoord_t x, gfxcoord_t y)
305 polydraw_internal_t*i = (polydraw_internal_t*)d->internal;
307 polydraw_moveTo(d, x, y);
310 double c = fabs(x-2*sx+i->lx) + fabs(y-2*sy+i->ly);
311 int parts = (int)(sqrt(c)*SUBFRACTION);
312 if(!parts) parts = 1;
315 for(t=0;t<parts;t++) {
316 nx = convert_coord((double)(t*t*x + 2*t*(parts-t)*sx + (parts-t)*(parts-t)*i->lx)/(double)(parts*parts), i->z);
317 ny = convert_coord((double)(t*t*y + 2*t*(parts-t)*sy + (parts-t)*(parts-t)*i->ly)/(double)(parts*parts), i->z);
318 if(nx != i->lastx || ny != i->lasty) {
319 i->writer.lineto(&i->writer, nx, ny);
320 i->lastx = nx; i->lasty = ny;
323 nx = convert_coord(x,i->z);
324 ny = convert_coord(y,i->z);
325 if(nx != i->lastx || ny != i->lasty) {
326 i->writer.lineto(&i->writer, nx, ny);
334 static void* polydraw_result(gfxdrawer_t*d)
336 polydraw_internal_t*i = (polydraw_internal_t*)d->internal;
337 void*result = i->writer.finish(&i->writer);
339 memset(d, 0, sizeof(gfxdrawer_t));
343 void gfxdrawer_target_poly(gfxdrawer_t*d, double gridsize)
345 polydraw_internal_t*i = (polydraw_internal_t*)rfx_calloc(sizeof(polydraw_internal_t));
347 i->lastx = 0x7fffffff; // convert_coord can never return this value
348 i->lasty = 0x7fffffff;
349 d->moveTo = polydraw_moveTo;
350 d->lineTo = polydraw_lineTo;
351 d->splineTo = polydraw_splineTo;
352 d->result = polydraw_result;
353 gfxpolywriter_init(&i->writer);
354 i->writer.setgridsize(&i->writer, gridsize);
355 i->z = 1.0 / gridsize;
358 gfxline_t*gfxline_from_gfxpoly(gfxpoly_t*poly)
360 gfxpolystroke_t*stroke;
362 for(stroke=poly->strokes;stroke;stroke=stroke->next) {
363 assert(stroke->num_points);
364 count += stroke->num_points;
367 gfxline_t*l = malloc(sizeof(gfxline_t)*count);
369 /* TODO: it might make sense to concatenate strokes */
370 for(stroke=poly->strokes;stroke;stroke=stroke->next) {
372 for(t=0;t<stroke->num_points;t++) {
373 l[count+t].x = stroke->points[t].x * poly->gridsize;
374 l[count+t].y = stroke->points[t].y * poly->gridsize;
375 l[count+t].type = gfx_lineTo;
376 l[count+t].next = &l[count+t+1];
378 l[count].type = gfx_moveTo;
379 count+=stroke->num_points;