+gfxline_t*gfxline_makecircle(double x,double y,double rx, double ry)
+{
+ double C1 = 0.2930;
+ double C2 = 0.4140;
+ double begin = 0.7070;
+ gfxline_t** line = (gfxline_t**)rfx_calloc(sizeof(gfxline_t*)*9);
+ int t;
+ for(t=0;t<9;t++) {
+ line[t] = rfx_calloc(sizeof(gfxline_t));
+ }
+ line[0]->type = gfx_moveTo;
+ line[0]->x = x+begin*rx;
+ line[0]->y = y+begin*ry;
+ for(t=1;t<9;t++) {
+ line[t-1]->next = line[t];
+ line[t]->type = gfx_splineTo;
+ }
+ line[8]->next = 0;
+#define R(nr,cx,cy,mx,my) \
+ line[nr]->sx = line[nr-1]->x + (cx); \
+ line[nr]->sy = line[nr-1]->y + (cy); \
+ line[nr]->x = line[nr]->sx + (mx); \
+ line[nr]->y = line[nr]->sy + (my);
+ R(1, -C1*rx, C1*ry, -C2*rx, 0);
+ R(2, -C2*rx, 0, -C1*rx, -C1*ry);
+ R(3, -C1*rx, -C1*ry, 0, -C2*ry);
+ R(4, 0, -C2*ry, C1*rx, -C1*ry);
+ R(5, C1*rx, -C1*ry, C2*rx, 0);
+ R(6, C2*rx, 0, C1*rx, C1*ry);
+ R(7, C1*rx, C1*ry, 0, C2*ry);
+ R(8, 0, C2*ry, -C1*rx, C1*ry);
+ gfxline_t*l = line[0];
+ free(line);
+ return l;
+}
+
+gfxbbox_t* gfxline_isrectangle(gfxline_t*_l)
+{
+ if(!_l)
+ return 0;
+
+ gfxline_t*l = gfxline_clone(_l);
+ gfxline_optimize(l);
+
+ double x1,x2,y1,y2;
+ int xc=0,yc=0;
+ char corners=0;
+
+ char prev=0;
+ char fail=0;
+ for(;l; l=l->next) {
+ double x = l->x;
+ double y = l->y;
+
+ char top=0,left=0;
+
+ if(xc==2 && x!=x1 && x!=x2) {fail=1;break;}
+ else if(xc>=1 && x==x1) {left=0;}
+ else if(xc==2 && x==x2) {left=1;}
+ else if(xc==1 && x!=x1) {x2 = x; xc=2; left=1;}
+ else if(xc==0) {x1 = x; xc=1;left=0;}
+ else {fprintf(stderr, "Internal error in rectangle detection\n");}
+
+ if(yc==2 && y!=y1 && y!=y2) {fail=1;break;}
+ else if(yc>=1 && y==y1) {top=0;}
+ else if(yc==2 && y==y2) {top=1;}
+ else if(yc==1 && y!=y1) {y2 = y; yc=2; top=1;}
+ else if(yc==0) {y1 = y; yc=1;top=0;}
+ else {fprintf(stderr, "Internal error in rectangle detection\n");}
+
+ char pos=top<<1|left;
+
+ if((pos^prev)==3) {
+ /* diagonal lines not allowed */
+ fail=1;break;
+ }
+ prev = pos;
+
+ /* no corner except the first one may be touched twice */
+ if(pos && (corners & 1<<pos)) {
+ fail=1;break;
+ }
+ /* mark which corners have been touched so far */
+ corners |= 1<<pos;
+ }
+ if(fail) {
+ gfxline_free(l);
+ return 0;
+ }
+
+ if(corners!=0x0f) return 0; // not all 4 corners reached
+
+ if(x2<x1) {double x = x2;x2=x1;x1=x;}
+ if(y2<y1) {double y = y2;y2=y1;y1=y;}
+
+ gfxbbox_t*r = malloc(sizeof(gfxbbox_t));
+ r->xmin = x1; r->ymin = y1;
+ r->xmax = x2; r->ymax = y2;
+ return r;
+}
+