#include "project.h" struct Xfc_struct; static void (*Xfc_default_constructor)(struct Xfc_struct *); static void (*Xfc_default_destructor)(struct Xfc_struct *); OBJECT(Xfc, int r; int g; int b; int n; ); static CONSTRUCTOR(Xfc)=GObj_DefaultConstructor; static DESTRUCTOR(Xfc)=GObj_DefaultDestructor; struct Private_struct; static void (*Private_default_constructor)(struct Private_struct *); static void (*Private_default_destructor)(struct Private_struct *); OBJECT(Private, Xfc_list xfcs; int maxcol; FILE *tmpfile; FILE *file; ); static void private_constructor(Private); static void private_destructor(Private); static CONSTRUCTOR(Private)=private_constructor; static DESTRUCTOR(Private)=private_destructor; static void private_constructor(Private p) { char name[1024]; p->xfcs=NEW_LIST(Xfc); p->tmpfile=fopen(tmpnam(name),"w+"); unlink(name); } #define XFIG_BUFSIZ 1024 static void private_destructor(Private p) { Xfc c; char buf[XFIG_BUFSIZ]; int i; fflush(p->tmpfile); rewind(p->tmpfile); printf("JWG: Writing xfig colors\n"); c=p->xfcs->head; while (c) { fprintf(p->file,"0 %d #%02x%02x%02x\n",c->n,c->r,c->g,c->b); c=c->next; } GObj_Delete(p->xfcs); printf("JWG: Writing xfig file\n"); while ((i=fread(buf,1,XFIG_BUFSIZ,p->tmpfile))>0) { fwrite(buf,1,i,p->file); } fclose(p->tmpfile); printf("JWG: xfig done\n"); } INTERNAL void xfig_init_private(Handle h) { Private p; p=NEW(Private); p->maxcol=32; p->file=h->file; h->private=(void *) p; fprintf(h->file,"#FIG 3.1\n"); fprintf(h->file,"Portrait\nFlush Left\nInches\n1200 2\n"); } static int xfig_sortout_color(Handle h,int r,int g,int b) { Private p=(Private) h->private; Xfc c; c=p->xfcs->head; while (c) { if ((c->r==r) && (c->g==g) && (c->b==b)) return(c->n); c=c->next; } if (p->maxcol==512) { printf("JWG: Xfig colors exhusted: this kludge needs fixing...\n"); return(32); } c=NEW(Xfc); c->r=r; c->g=g; c->b=b; c->n=(p->maxcol++); GObj_InsertTail(p->xfcs,c); return(c->n); } INTERNAL void xfig_do_line(Handle h,Jwgline *line) { Private p=(Private) h->private; int i,c; int r,g,b; int w; int x,y; int n=line->npts; extern double sqrt(double); extern double fabs(double); r=(int) (h->pen.color.r*255.0); g=(int) (h->pen.color.g*255.0); b=(int) (h->pen.color.b*255.0); c=xfig_sortout_color(h,r,g,b); if (h->pen.width==0.0) { w=1; } else { w=(int) ((h->pen.width)/15.0); if (w<1) w=1; } fprintf(p->tmpfile,"2 1 0 %d %d 0 0 0 -1 0.0 1 1 -1 0 0 %d\n",w,c,n); for (i=0;idata[i].x; y=(int) line->data[i].y; fprintf(p->tmpfile,"%d %d ",x,y); } fprintf(p->tmpfile,"\n"); } INTERNAL void xfig_init_xform(Handle h) { h->transform.xc=0.0; h->transform.yc=14422.8; h->transform.a=10.2; h->transform.d=-10.2; h->transform.b=0.0; h->transform.c=0.0; } INTERNAL void xfig_do_poly(Handle h,Jwgline *line) { int i,c; int r,g,b; int w; int x,y; int n=line->npts; Private p=(Private) h->private; r=(int) (h->brush.color.r*255.0); g=(int) (h->brush.color.g*255.0); b=(int) (h->brush.color.b*255.0); c=xfig_sortout_color(h,r,g,b); printf("COL FOR POLY %d\n",c); fprintf(p->tmpfile,"2 3 0 0 0 %d 0 0 20 0.0 1 1 -1 0 0 %d\n",c,n+1); for (i=0;idata[i].x; y=(int) line->data[i].y; fprintf(p->tmpfile,"%d %d ",x,y); } x=(int) line->data->x; y=(int) line->data->y; fprintf(p->tmpfile,"%d %d",x,y); fprintf(p->tmpfile,"\n"); } INTERNAL void xfig_do_text(Handle h,Jwgpos pos,char *str) { int c,r,g,b; int x,y; double atan2(double,double); Transform t=h->transform; Private p=(Private) h->private; int height; float angle; x=(int) pos.x; y=(int) pos.y; height=(int) (h->ink.height/16.66); r=(int) (h->ink.color.r*255.0); g=(int) (h->ink.color.g*255.0); b=(int) (h->ink.color.b*255.0); c=xfig_sortout_color(h,r,g,b); if (t.det>0) { printf("JWG: Text cannot be written backwards in an xfig file\n"); return; } angle=-atan2(t.b,t.a); fprintf(p->tmpfile,"4 0 %d 0 0 %d %d %f 4 1 1 %d %d %s\\001\n",c,0, height, angle,x,y,str); return; }