5 #include "../gfxdevice.h"
8 static edge_t*edge_new(int x1, int y1, int x2, int y2)
10 edge_t*s = malloc(sizeof(edge_t));
19 static inline void gfxpoly_add_edge(gfxpoly_t*poly, double _x1, double _y1, double _x2, double _y2)
25 if(x1!=x2 || y1!=y2) {
26 edge_t*s = edge_new(x1, y1, x2, y2);
27 s->next = poly->edges;
32 gfxpoly_t* gfxpoly_fillToPoly(gfxline_t*line, double gridsize)
34 gfxpoly_t*p = gfxpoly_new(gridsize);
36 /* factor that determines into how many line fragments a spline is converted */
37 double subfraction = 2.4;//0.3
39 double z = 1.0 / gridsize;
41 double lastx=0, lasty=0;
42 assert(!line || line[0].type == gfx_moveTo);
46 if(line->type == gfx_moveTo) {
47 } else if(line->type == gfx_lineTo) {
48 gfxpoly_add_edge(p, lastx, lasty, x, y);
49 } else if(line->type == gfx_splineTo) {
50 int parts = (int)(sqrt(fabs(line->x-2*line->sx+lastx) +
51 fabs(line->y-2*line->sy+lasty))*subfraction);
53 double stepsize = 1.0/parts;
55 for(i=0;i<parts;i++) {
56 double t = (double)i*stepsize;
57 double sx = (line->x*t*t + 2*line->sx*t*(1-t) + x*(1-t)*(1-t))*z;
58 double sy = (line->y*t*t + 2*line->sy*t*(1-t) + y*(1-t)*(1-t))*z;
59 gfxpoly_add_edge(p, lastx, lasty, sx, sy);
63 gfxpoly_add_edge(p, lastx, lasty, x, y);
74 static char* readline(FILE*fi)
78 int l = fread(&c, 1, 1, fi);
89 int l = fread(&c, 1, 1, fi);
90 if(!l || c==10 || c==13) {
96 gfxpoly_t* gfxpoly_from_file(const char*filename, double gridsize)
98 gfxpoly_t*p = gfxpoly_new(gridsize);
100 double z = 1.0 / gridsize;
102 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);
132 printf("loaded %d points from %s\n", count, filename);