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_gfxline(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);