4 #include "../gfxdevice.h"
8 /* factor that determines into how many line fragments a spline is converted */
9 #define SUBFRACTION (2.4)
11 static edge_t*edge_new(int x1, int y1, int x2, int y2)
13 edge_t*s = rfx_calloc(sizeof(edge_t));
21 static inline void gfxpoly_add_edge(gfxpoly_t*poly, double _x1, double _y1, double _x2, double _y2)
23 /* we clamp to 31 bit instead of 32 bit because we use
24 a (x1-x2) shortcut when comparing coordinates
26 if(_x1 < -0x40000000) _x1 = -0x40000000;
27 if(_x1 > 0x3fffffff) _x1 = 0x3fffffff;
28 if(_y1 < -0x40000000) _y1 = -0x40000000;
29 if(_y1 > 0x3fffffff) _y1 = 0x3fffffff;
30 if(_x2 < -0x40000000) _x2 = -0x40000000;
31 if(_x2 > 0x3fffffff) _x2 = 0x3fffffff;
32 if(_y2 < -0x40000000) _y2 = -0x40000000;
33 if(_y2 > 0x3fffffff) _y2 = 0x3fffffff;
40 if(x1!=x2 || y1!=y2) {
41 edge_t*s = edge_new(x1, y1, x2, y2);
42 s->next = poly->edges;
47 gfxpoly_t* gfxpoly_from_gfxline(gfxline_t*line, double gridsize)
49 gfxpoly_t*p = gfxpoly_new(gridsize);
51 double z = 1.0 / gridsize;
53 double lastx=0, lasty=0;
54 assert(!line || line[0].type == gfx_moveTo);
58 if(line->type == gfx_moveTo) {
59 } else if(line->type == gfx_lineTo) {
60 gfxpoly_add_edge(p, lastx, lasty, x, y);
61 } else if(line->type == gfx_splineTo) {
62 int parts = (int)(sqrt(fabs(line->x-2*line->sx+lastx) +
63 fabs(line->y-2*line->sy+lasty))*SUBFRACTION);
65 double stepsize = 1.0/parts;
67 for(i=0;i<parts;i++) {
68 double t = (double)i*stepsize;
69 double sx = (line->x*t*t + 2*line->sx*t*(1-t) + x*(1-t)*(1-t))*z;
70 double sy = (line->y*t*t + 2*line->sy*t*(1-t) + y*(1-t)*(1-t))*z;
71 gfxpoly_add_edge(p, lastx, lasty, sx, sy);
75 gfxpoly_add_edge(p, lastx, lasty, x, y);
86 typedef struct _gfxstroke {
92 typedef struct _gfxcompactpoly {
95 gfxstroke_t strokes[0];
98 static char* readline(FILE*fi)
102 int l = fread(&c, 1, 1, fi);
113 int l = fread(&c, 1, 1, fi);
114 if(!l || c==10 || c==13) {
120 gfxpoly_t* gfxpoly_from_file(const char*filename, double gridsize)
122 gfxpoly_t*p = gfxpoly_new(gridsize);
124 double z = 1.0 / gridsize;
126 FILE*fi = fopen(filename, "rb");
133 double lastx=0,lasty=0;
135 char*line = readline(fi);
140 if(sscanf(line, "%lf %lf %s", &x, &y, &s) == 3) {
143 if(s && !strcmp(s,"moveto")) {
145 } else if(s && !strcmp(s,"lineto")) {
146 gfxpoly_add_edge(p, lastx, lasty, x, y);
149 printf("invalid command: %s\n", s);
153 } else if(sscanf(line, "%% gridsize %lf", &g) == 1) {
160 printf("loaded %d points from %s (gridsize %f)\n", count, filename, g);
162 printf("loaded %d points from %s\n", count, filename);