draw8.c

0000/**********************************
0001
0002 X Font
0003
0004**********************************/
0005
0006#include <stdlib.h>
0007#include <string.h>
0008#include "draw6.h"
0009#include "draw7.h"
0010#include "draw8.h"
0011#include <X11/Xlib.h>
0012#include <X11/Xutil.h>
0013
0014int initial=0;
0015Display *d=NULL;
0016XFontStruct *xfs;
0017XFontStruct *xfsa[5]={NULL,NULL,NULL,NULL,NULL};
0018int fdirec=0; /* direction of string in tenths of degree */
0019int spos=0; /* string position 0:left 1:right 2:center */
0020Pixmap px;
0021GC gc;
0022int widthX,heightX;
0023PixMap *p;
0024
0025
0026int DrawStr(char *c)
0027{
0028 int i,j,k,n,m;
0029 XCharStruct cs; 
0030 Pixmap ptemp;
0031 GC gctemp;
0032 XImage *imgtemp;
0033 unsigned long l;
0034 int wmin,wmax,hmin,hmax;
0035
0036 XTextExtents(xfs,c,strlen(c),&i,&j,&k,&cs);
0037 n=cs.width;
0038 ptemp=XCreatePixmap(d,RootWindow(d,0),n,j+k,XDefaultDepth(d,0));
0039 gctemp=XCreateGC(d,ptemp,0,0);
0040 XSetForeground(d,gctemp,WhitePixel(d,0));
0041 XFillRectangle(d,ptemp,gctemp,0,0,n,j+k);
0042 XSetForeground(d,gctemp,BlackPixel(d,0));
0043 XSetFont(d,gctemp,xfs->fid);
0044 XDrawString(d,ptemp,gctemp,0,j,c,strlen(c));
0045 XSync(d,True);
0046 imgtemp=XGetImage(d,ptemp,0,0,n,j+k,0xffffff,ZPixmap);
0047
0048 if(fdirec==0){ /* horizontal */
0049  p=pcreate(0,n,j+k); /* 0 : 1-bit B&W (0=black, 1=white) */
0050  hmin=0;
0051  hmax=j+k;
0052  wmin=0;
0053  wmax=n;
0054  if(spos!=0) p->refx=n/spos; /* horiz ref position */
0055  else p->refx=0;
0056  p->refy=j; /* vert ref position */
0057  for(j=hmin;j<hmax;j++)
0058   for(i=wmin;i<wmax;i++){
0059    l=XGetPixel(imgtemp,i,j);
0060    if(l==0) putpix(p,i,j,0,0,0);
0061    else putpix(p,i,j,255,255,255);
0062   }
0063 }
0064 else{ /* vertical */
0065  p=pcreate(0,j+k,n); /* 0 : 1-bit B&W (0=black, 1=white) */
0066  hmin=0;
0067  hmax=n;
0068  wmin=0;
0069  wmax=j+k;
0070  if(spos!=0) p->refy=n-n/spos; /* vert ref position */
0071  else p->refy=n;
0072  p->refx=j; /* horiz ref position */
0073  for(j=wmin;j<wmax;j++)
0074   for(i=hmin;i<hmax;i++){
0075    l=XGetPixel(imgtemp,i,j);
0076    if(l==0) putpix(p,j,hmax-1-i,0,0,0);
0077    else putpix(p,j,hmax-1-i,255,255,255);
0078   }
0079 }
0080 XDestroyImage(imgtemp);
0081 XFreeGC(d,gctemp);
0082 XFreePixmap(d,ptemp);
0083 j=MakePixmap(0,wmax,hmax);
0084 XferPixmap(j,p);
0085 PastePixmap(j,0,0,2);
0086 DestroyPixmap(j);
0087 pdestroy(p);
0088 return 0;
0089}
0090
0091int DrawStrSize(char *c,int *wid,int *asc,int *des)
0092{
0093 int i,j,k;
0094 XCharStruct cs; 
0095
0096 XTextExtents(xfs,c,strlen(c),&i,&j,&k,&cs);
0097 *wid=cs.width;
0098 *asc=j;
0099 *des=k;
0100 return 0;
0101}
0102
0103int SetFont(int font,int direc,int mode)
0104{
0105 char f[60];
0106 char ast[]="*";
0107 char *type[]={"illegal","courier","helvetica","century","symbol","times",
0108  "utopia","lucida","lucidabright","lucidatypewriter","charter"};
0109 int sizn=15;
0110 int siz[]={8,10,11,12,14,15,17,18,19,20,24,25,26,33,34};
0111 char *sizc[]={"-8-","-10-","-11-","-12-","-14-","-15-","-17-","-18-",
0112               "-19-","-20-","-24-","-25-","-26-","-33-","-34-"};
0113 char *face[]={"medium","bold"};
0114 char *ang[]={"r-","i-","o-"};
0115 int i,j,k,l,m,n;
0116 if(initial==0){
0117  d=XOpenDisplay("localhost:0");
0118  initial=1;
0119  if(d==NULL){
0120   /* error processing due */
0121   return 1;
0122  }
0123 }
0124 k=font/100;
0125 n=font-k*100;
0126 l=mode/100;
0127 m=mode-l*100;
0128 strcpy(f,ast);
0129 strcat(f,type[k]);
0130 strcat(f,ast);
0131 strcat(f,face[l]);
0132 strcat(f,ast);
0133 strcat(f,ang[m]);
0134 strcat(f,ast);
0135 for(i=0;i<sizn;i++) if(siz[i]==n) break;
0136 strcat(f,sizc[i]);
0137 strcat(f,ast);
0138 fdirec=direc;
0139 spos=fdirec;
0140 fdirec=fdirec/10; /* angle in thenth of a degree */
0141 spos=spos-fdirec*10; /* 0:left 1:right 2:middle */
0142 xfs=XLoadQueryFont(d,f);
0143 if(xfs==NULL){
0144  /* error processing due */
0145  return 1;
0146 }
0147 return 0;
0148}
0149
0150int SetFontName(char *f,int direc)
0151{
0152 if(initial==0){
0153  d=XOpenDisplay("localhost:0");
0154  initial=1;
0155  if(d==NULL){
0156   /* error processing due */
0157   return 1;
0158  }
0159 }
0160 fdirec=direc;
0161 spos=fdirec;
0162 fdirec=fdirec/10; /* angle in thenth of a degree */
0163 spos=spos-fdirec*10; /* 0:left 1:right 2:middle */
0164 xfs=XLoadQueryFont(d,f);
0165 if(xfs==NULL){
0166  /* error processing due */
0167  return 1;
0168 }
0169 return 0;
0170}
0171
0172int BeginX(int width,int height)
0173{
0174 widthX=width;
0175 heightX=height;
0176 d=XOpenDisplay("localhost:0");
0177 px=XCreatePixmap(d,RootWindow(d,0),width,height,XDefaultDepth(d,0));
0178 gc=XCreateGC(d,px,0,0);
0179 XSetForeground(d,gc,WhitePixel(d,0));
0180 XFillRectangle(d,px,gc,0,0,width,height);
0181 XSync(d,True);
0182 return 0;
0183}
0184
0185/*************************************************************
0186 ang1 start of the arc relative to x-axis in deg*64
0187 ang2 extent of the arc relative to the start point in deg*64
0188*************************************************************/
0189int DrawArc(int x,int y,int wid,int hei,int ang1,int ang2)
0190{
0191 XDrawArc(d,px,gc,x,y,wid,hei,ang1,ang2);
0192 return 0;
0193}
0194
0195int DrawImageString(int x,int y,char *s)
0196{
0197 XDrawImageString(d,px,gc,x,y,s,strlen(s));
0198 return 0;
0199}
0200
0201int DrawImageString16(int x,int y,char *s)
0202{
0203 XDrawImageString16(d,px,gc,x,y,(XChar2b*)s,strlen(s)/2);
0204 return 0;
0205}
0206
0207int DrawLine(int x1,int y1,int x2,int y2)
0208{
0209 XDrawLine(d,px,gc,x1,y1,x2,y2);
0210 return 0;
0211}
0212
0213/*************************************************
0214mode (CoordinateMode)
0215 0: CoordModeOrigin	(relative to the origin)
0216 1: CoordModePrevious (relative to previous point)
0217***************************************************/
0218
0219int DrawLines(int *poin,int npoints,int mode)
0220{
0221 XPoint *points;
0222 int i;
0223 points=(XPoint *)malloc(sizeof(XPoint)*npoints);
0224 for(i=0;i<npoints;i++){
0225  points[i].x=poin[2*i];
0226  points[i].y=poin[2*i+1];
0227 }
0228 XDrawLines(d,px,gc,points,npoints,mode);
0229 return 0;
0230}
0231
0232int DrawPoint(int x,int y)
0233{
0234 XDrawPoint(d,px,gc,x,y);
0235 return 0;
0236}
0237
0238int DrawRectangle(int x,int y,int wid,int hei)
0239{
0240 XDrawRectangle(d,px,gc,x,y,wid,hei);
0241 return 0;
0242}
0243
0244int Draw_String(int x,int y,char *s)
0245{
0246 XDrawString(d,px,gc,x,y,s,strlen(s));
0247 return 0;
0248}
0249
0250int DrawString16(int x,int y,char *s)
0251{
0252 XDrawString16(d,px,gc,x,y,(XChar2b*)s,strlen(s)/2);
0253 return 0;
0254}
0255
0256int FillArc(int x,int y,int wid,int hei,int ang1,int ang2)
0257{
0258 XFillArc(d,px,gc,x,y,wid,hei,ang1,ang2);
0259 return 0;
0260}
0261
0262/*************************************************************
0263shape
0264 0: Complex (paths may intersect)
0265 1: Nonconvex (no paths intersect, but not convex )
0266 2: Convex (wholly convex)
0267
0268mode (CoordinateMode)
0269 0: CoordModeOrigin	(relative to the origin)
0270 1: CoordModePrevious (relative to previous point)
0271*************************************************************/
0272int FillPolygon(int *poin,int npoints,int shape,int mode)
0273{
0274 XPoint *points;
0275 int i;
0276 points=(XPoint *)malloc(sizeof(XPoint)*npoints);
0277 for(i=0;i<npoints;i++){
0278  points[i].x=poin[2*i];
0279  points[i].y=poin[2*i+1];
0280 }
0281 XFillPolygon(d,px,gc,points,npoints,shape,mode);
0282 return 0;
0283}
0284
0285int FillRectangle(int x,int y,int width,int height)
0286{
0287 XFillRectangle(d,px,gc,x,y,width,height);
0288 return 0;
0289}
0290
0291int FreeFont(int fn)
0292{
0293 XFreeFont(d,xfsa[fn]);
0294 xfsa[fn]=NULL;
0295 return 0;
0296}
0297
0298int LoadQueryFont(char *name)
0299{
0300 int i;
0301 for(i=0;i<5;i++) if(xfsa[i]==NULL) break;
0302 if(i==5) return -1; /* no room */
0303 xfsa[i]=XLoadQueryFont(d,name);
0304 if(xfsa[i]==NULL) return -2; /* no font */
0305 return i;
0306}
0307
0308/**********************************
0309 r,g,b : 0 - 100
0310**********************************/
0311int SetBackground(int r,int g,int b)
0312{
0313 Colormap cmap;
0314 XColor inout;
0315
0316 cmap=DefaultColormap(d,0);
0317 inout.red=((0xff*r)/100)*0xff;
0318 inout.green=((0xff*g)/100)*0xff;
0319 inout.blue=((0xff*b)/100)*0xff;
0320 XAllocColor(d,cmap,&inout);
0321 XSetBackground(d,gc,inout.pixel);
0322 return 0;
0323}
0324
0325int Set_Font(int i)
0326{
0327 XSetFont(d,gc,xfsa[i]->fid);
0328 return 0;
0329}
0330
0331/**********************************
0332 r,g,b : 0 - 100
0333**********************************/
0334int SetForeground(int r,int g,int b)
0335{
0336 Colormap cmap;
0337 XColor inout;
0338
0339 cmap=DefaultColormap(d,0);
0340 inout.red=((0xff*r)/100)*0xff;
0341 inout.green=((0xff*g)/100)*0xff;
0342 inout.blue=((0xff*b)/100)*0xff;
0343 XAllocColor(d,cmap,&inout);
0344 XSetForeground(d,gc,inout.pixel);
0345 return 0;
0346}
0347
0348/**********************************************************
0349
0350 lineStyle LineSolid:0 LineOnOffDash:1 LineDoubleDash:2
0351 capStyle CapNotLast:0 CapButt:1 CapRound:2 CapProjecting:3
0352 joinStyle JoinMiter:0 JoinRound:1 JoinBevel:2
0353
0354**********************************************************/
0355int SetLineAttributes(int w,int lineStyle,int capStyle,int joinStyle)
0356{
0357 XSetLineAttributes(d,gc,w,lineStyle,capStyle,joinStyle);
0358 return 0;
0359}
0360
0361int TextWidth(int fn,char *str)
0362{
0363 int i;
0364 i=XTextWidth(xfsa[fn],str,strlen(str));
0365 return i;
0366}
0367
0368int PasteX(int x,int y)
0369{
0370 int i,j;
0371 XImage *imgtemp;
0372 unsigned long l;
0373
0374 XSync(d,True);
0375 imgtemp=XGetImage(d,px,0,0,widthX,heightX,0xffffff,ZPixmap);
0376 p=pcreate(3,widthX,heightX); /* 16-bit color */
0377 p->refx=0;
0378 p->refy=0;
0379 for(j=0;j<heightX;j++)
0380  for(i=0;i<widthX;i++){
0381   l=XGetPixel(imgtemp,i,j);
0382   putpix16(p,i,j,l);
0383  }
0384 XDestroyImage(imgtemp);
0385 XFreeGC(d,gc);
0386 XFreePixmap(d,px);
0387 j=MakePixmap(3,widthX,heightX);
0388 XferPixmap(j,p);
0389 PastePixmap(j,0,0,0);
0390 DestroyPixmap(j);
0391 pdestroy(p);
0392 return 0;
0393}
0394