+gfxpoly_t* gfxpoly_from_file(const char*filename, double gridsize)
+{
+ gfxpoly_t*p = gfxpoly_new(gridsize);
+
+ double z = 1.0 / gridsize;
+
+ FILE*fi = fopen(filename, "rb");
+ if(!fi) {
+ perror(filename);
+ return 0;
+ }
+ int count = 0;
+ double g = 0;
+ double lastx=0,lasty=0;
+ while(1) {
+ char*line = readline(fi);
+ if(!line)
+ break;
+ double x,y;
+ char s[256];
+ if(sscanf(line, "%lf %lf %s", &x, &y, &s) == 3) {
+ x*=z;
+ y*=z;
+ if(s && !strcmp(s,"moveto")) {
+ count++;
+ } else if(s && !strcmp(s,"lineto")) {
+ gfxpoly_add_edge(p, lastx, lasty, x, y);
+ count++;
+ } else {
+ printf("invalid command: %s\n", s);
+ }
+ lastx = x;
+ lasty = y;
+ } else if(sscanf(line, "%% gridsize %lf", &g) == 1) {
+ p->gridsize = g;
+ }
+ free(line);
+ }
+ fclose(fi);
+ if(g) {
+ printf("loaded %d points from %s (gridsize %f)\n", count, filename, g);
+ } else {
+ printf("loaded %d points from %s\n", count, filename);
+ }
+ return p;
+}