aboutsummaryrefslogtreecommitdiffstats
path: root/src/cgm.c
diff options
context:
space:
mode:
Diffstat (limited to 'src/cgm.c')
-rw-r--r--src/cgm.c231
1 files changed, 231 insertions, 0 deletions
diff --git a/src/cgm.c b/src/cgm.c
new file mode 100644
index 0000000..18bd762
--- /dev/null
+++ b/src/cgm.c
@@ -0,0 +1,231 @@
+#include "project.h"
+#include "cd/cd.h"
+BEGIN_OBJECT(CGMc)
+ int r,g,b,n;
+END_OBJECT(CGMc)
+
+CREATOR(CGMc)=Obj_DefaultCreator;
+DESTRUCTOR(CGMc)=Obj_DefaultDestructor;
+
+BEGIN_OBJECT(Private)
+ CGMc_list cgmcs;
+ int maxcol;
+ cdImagePtr im;
+ FILE *file;
+END_OBJECT(Private)
+
+
+int cgm_sortout_color(Private p,int r,int g,int b)
+{
+CGMc c;
+
+c=p->cgmcs->head;
+
+while (c)
+{
+if ((c->r==r) && (c->g==g) && (c->b==b)) return(c->n);
+c=c->next;
+}
+
+
+c=CREATE_OBJ(CGMc);
+c->r=r;
+c->g=g;
+c->b=b;
+c->n=cdImageColorAllocate(p->im,r,g,b);
+
+Obj_Insert(p->cgmcs,c,ObjInsertHead);
+
+
+return(c->n);
+}
+
+static void private_constructor(Private p)
+{
+char name[1024];
+
+p->cgmcs=CREATE_LIST(CGMc);
+/* We need a square image otherwise nasty things happen to
+lines!*/
+p->im=cdImageCreate(7100,7100);
+}
+
+static void private_destructor(Private p)
+{
+CGMc c;
+int i;
+
+printf("JWG: Writing cgm file \n");
+
+Obj_Delete(p->cgmcs);
+
+cdImageCgm(p->im,p->file);
+cdImageDestroy(p->im);
+
+free(p);
+}
+
+
+CREATOR(Private)=private_constructor;
+DESTRUCTOR(Private)=private_destructor;
+
+
+void cgm_init_private(Handle h)
+{
+Private p;
+
+p=CREATE_OBJ(Private);
+p->file=h->file;
+
+h->private=(void *) p;
+
+}
+
+
+
+void cgm_do_line(Handle h,Jwgline *line)
+{
+Private p=(Private) h->private;
+int i,c;
+int r,g,b;
+int n=line->npts;
+int w;
+cdPointPtr ptr,base;
+
+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=cgm_sortout_color(p,r,g,b);
+
+
+if (h->pen.width==0.0) {
+ w=1;
+} else {
+ w=(int) (h->pen.width/7.10);
+ if (w<1) w=1;
+}
+
+cdSetLineWidth(p->im,w);
+cdSetLineType(p->im,1);
+cdSetLineColor(p->im,c);
+
+ptr=base=(cdPointPtr) malloc(sizeof(struct cdPointStruct) * n);
+
+for (i=0;i<n;++i) {
+
+ptr->x=(int) line->data[i].x;
+ptr->y=(int) line->data[i].y;
+ptr++;
+}
+
+cdPolyLine(p->im,base,n);
+
+free(base);
+
+
+}
+
+void cgm_init_xform(Handle h)
+{
+/* this should map 1000,1414.21 to an A4 page */
+
+h->transform.xc=0.0;
+h->transform.yc=0.0;
+h->transform.a=5.0;
+h->transform.d=5.0;
+
+h->transform.b=0.0;
+h->transform.c=0.0;
+
+
+}
+
+void cgm_do_poly(Handle h,Jwgline *line)
+{
+int i,c;
+int r,g,b;
+int n=line->npts;
+Private p=(Private) h->private;
+cdPointPtr ptr,base;
+
+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=cgm_sortout_color(p,r,g,b);
+
+cdSetFillStyle(p->im,1);
+cdSetFillColor(p->im,c);
+cdSetEdgeVis(p->im,0);
+
+ptr=base=(cdPointPtr) malloc(sizeof(struct cdPointStruct) * n);
+
+for (i=0;i<n;++i) {
+
+ptr->x=(int) line->data[i].x;
+ptr->y=(int) line->data[i].y;
+ptr++;
+}
+
+cdPolygon(p->im,base,n);
+
+free(base);
+
+}
+
+
+void cgm_do_text(Handle h,Jwgpos pos,char *str)
+{
+int color,r,g,b;
+Transform t=h->transform;
+Private p=(Private) h->private;
+float s;
+int height;
+
+height=(int) h->ink.height;
+
+r=(int) (h->ink.color.r*255.0);
+g=(int) (h->ink.color.g*255.0);
+b=(int) (h->ink.color.b*255.0);
+color=cgm_sortout_color(p,r,g,b);
+
+if (t.det<0) {
+ printf("JWG: Text cannot be written backwards in a CGM file\n");
+ return;
+}
+
+{
+float a,b,c,d;
+
+a=fabs(t.a);
+b=fabs(t.b);
+c=fabs(t.c);
+d=fabs(t.d);
+
+s=(a>b) ? a: ((b>c) ? b: ((c>d) ? c: d));
+
+s=16384.0/s;
+
+
+}
+
+{
+int a,b,c,d;
+
+a=(int) (s*t.a);
+b=(int) (s*t.b);
+c=(int) (s*t.c);
+d=(int) (s*t.d);
+
+printf("JWG: Text vectors are up=(%d,%d) right=(%d,%d)\n",c,d,a,b);
+cdSetTextOrient(p->im, c, d, a, b);
+}
+
+cdSetTextHeight(p->im,height);
+
+cdSetTextFont(p->im,5);
+cdSetTextColor(p->im,color);
+
+cdText(p->im,(int) pos.x,(int) pos.y,str);
+
+return;
+}