4 #include "../gfxdevice.h"
8 static edge_t*edge_new(int x1, int y1, int x2, int y2)
10 edge_t*s = rfx_calloc(sizeof(edge_t));
18 static inline void gfxpoly_add_edge(gfxpoly_t*poly, double _x1, double _y1, double _x2, double _y2)
24 if(x1!=x2 || y1!=y2) {
25 edge_t*s = edge_new(x1, y1, x2, y2);
26 s->next = poly->edges;
31 gfxpoly_t* gfxpoly_from_gfxline(gfxline_t*line, double gridsize)
33 gfxpoly_t*p = gfxpoly_new(gridsize);
35 /* factor that determines into how many line fragments a spline is converted */
36 double subfraction = 2.4;//0.3
38 double z = 1.0 / gridsize;
40 double lastx=0, lasty=0;
41 assert(!line || line[0].type == gfx_moveTo);
45 if(line->type == gfx_moveTo) {
46 } else if(line->type == gfx_lineTo) {
47 gfxpoly_add_edge(p, lastx, lasty, x, y);
48 } else if(line->type == gfx_splineTo) {
49 int parts = (int)(sqrt(fabs(line->x-2*line->sx+lastx) +
50 fabs(line->y-2*line->sy+lasty))*subfraction);
52 double stepsize = 1.0/parts;
54 for(i=0;i<parts;i++) {
55 double t = (double)i*stepsize;
56 double sx = (line->x*t*t + 2*line->sx*t*(1-t) + x*(1-t)*(1-t))*z;
57 double sy = (line->y*t*t + 2*line->sy*t*(1-t) + y*(1-t)*(1-t))*z;
58 gfxpoly_add_edge(p, lastx, lasty, sx, sy);
62 gfxpoly_add_edge(p, lastx, lasty, x, y);
73 static char* readline(FILE*fi)
77 int l = fread(&c, 1, 1, fi);
88 int l = fread(&c, 1, 1, fi);
89 if(!l || c==10 || c==13) {
95 gfxpoly_t* gfxpoly_from_file(const char*filename, double gridsize)
97 gfxpoly_t*p = gfxpoly_new(gridsize);
99 double z = 1.0 / gridsize;
101 FILE*fi = fopen(filename, "rb");
108 double lastx=0,lasty=0;
110 char*line = readline(fi);
115 if(sscanf(line, "%lf %lf %s", &x, &y, &s) == 3) {
118 if(s && !strcmp(s,"moveto")) {
120 } else if(s && !strcmp(s,"lineto")) {
121 gfxpoly_add_edge(p, lastx, lasty, x, y);
124 printf("invalid command: %s\n", s);
128 } else if(sscanf(line, "%% gridsize %lf", &g) == 1) {
135 printf("loaded %d points from %s (gridsize %f)\n", count, filename, g);
137 printf("loaded %d points from %s\n", count, filename);