draw4.c
0000/*****************************************************
0001 draw4.c Xlib functions - no-interactive version
0002******************************************************/
0003#include <stdio.h>
0004#include <stdlib.h>
0005#include <math.h>
0006#include "draw1.h"
0007#include "draw4.h"
0008#include <sys/types.h>
0009#include <sys/ipc.h>
0010#include <sys/shm.h>
0011#include <sys/sem.h>
0012#include <X11/Xlib.h>
0013#include <string.h>
0014
0015#if 0
0016extern int semid; /* semaphore identifier */
0017extern int shmid; /* shared memory segment id */
0018extern int semno; /* semaphore number for this process */
0019extern char *shmbuf; /* shared memory address */
0020extern struct sembuf sb;
0021#endif
0022
0023extern char *outbuf; // output buffer in shared memory
0024extern int outptr; // current pointer in outbuf
0025extern int outmax; // buffer capacity
0026
0027int wid,hei; /* used in the real number drawing */
0028Display *d;
0029Pixmap p,ptemp;
0030GC gc,gctemp;
0031XFontStruct *xfs;
0032XImage *img;
0033XImage *imgtemp;
0034int i,j,k,m,n,o,r,g,b;
0035unsigned long l;
0036int xcur,ycur;
0037XPoint *xp;
0038int nxp=-1;
0039int outmin=0;
0040int wmin,wmax,hmin,hmax;
0041XCharStruct cs;
0042int fdirec=0; /* direction of string in tenths of degree */
0043int spos=0; /* string position 0:left 1:right 2:center */
0044XRectangle rec;
0045XGCValues val;
0046
0047int MakeViewX(int width,int height)
0048{
0049
0050 wid=width;
0051 hei=height;
0052// d=XOpenDisplay( NULL );
0053 d=XOpenDisplay("localhost:0");
0054 if(d==NULL){
0055 return 1;
0056 }
0057 p=XCreatePixmap(d,RootWindow(d,0),wid,hei,XDefaultDepth(d,0));
0058 gc=XCreateGC(d,p,0,0);
0059 XSetForeground(d,gc,WhitePixel(d,0));
0060 XFillRectangle(d,p,gc,0,0,wid,hei);
0061 xcur=0;
0062 ycur=0;
0063}
0064
0065int GifPngOutX(void)
0066{
0067 XSync(d,True);
0068 img=XGetImage(d,p,0,0,wid,hei,0xffffff,ZPixmap);
0069 if(outmin){
0070 hmin=hei;
0071 hmax=0;
0072 wmin=wid;
0073 wmax=0;
0074 for(j=0;j<hei;j++)
0075 for(i=0;i<wid;i++){
0076 l=XGetPixel(img,i,j);
0077 if(l!=0xffff){
0078 if(hmin>j) hmin=j;
0079 if(hmax<j) hmax=j;
0080 if(wmin>i) wmin=i;
0081 if(wmax<i) wmax=i;
0082 }
0083 }
0084 hmax++;
0085 wmax++;
0086 if(hmax-hmin<2||wmax-wmin<2){
0087 hmin=0;
0088 hmax=hei;
0089 wmin=0;
0090 wmax=wid;
0091 }
0092 }
0093 else{
0094 hmin=0;
0095 hmax=hei;
0096 wmin=0;
0097 wmax=wid;
0098 }
0099 MakeView(wmax-wmin,hmax-hmin);
0100 for(j=hmin;j<hmax;j++)
0101 for(i=wmin;i<wmax;i++){
0102 l=XGetPixel(img,i,j);
0103 r=(l>>11)&0x1f;
0104 g=(l>>5)&0x3f;
0105 b=l&0x1f;
0106 SetColor((100*r)/0x1f,(100*g)/0x3f,(100*b)/0x1f);
0107 MoveTo(i-wmin,j-hmin);
0108 LineTo(i-wmin+1,j-hmin);
0109 }
0110 XDestroyImage(img);
0111 XFreeGC(d,gc);
0112 XFreePixmap(d,p);
0113 XCloseDisplay(d);
0114 return 0;
0115}
0116
0117int PngOutX(void)
0118{
0119 GifPngOutX();
0120 PngOut();
0121 return 0;
0122}
0123
0124int GifOutX(void)
0125{
0126 GifPngOutX();
0127 GifOut();
0128 return 0;
0129}
0130
0131int OutMin(void)
0132{
0133 outmin=1;
0134 return 0;
0135}
0136
0137
0138/***********************************
0139specify rgb in 0xrrggbb
0140************************************/
0141unsigned long nearColor(Display *d,unsigned long rgb)
0142{
0143 Colormap cmap;
0144 XColor inout;
0145
0146 cmap=DefaultColormap(d,0);
0147 inout.red=((rgb>>16)&0xff)*0xff;
0148 inout.green=((rgb>>8)&0xff)*0xff;
0149 inout.blue=(rgb&0xff)*0xff;
0150 XAllocColor(d,cmap,&inout);
0151 return inout.pixel;
0152}
0153
0154int SetColorX(int r,int g,int b)
0155{
0156 unsigned long l;
0157
0158 l=(0xff*r)/100;
0159 l=(l<<8)+(0xff*g)/100;
0160 l=(l<<8)+(0xff*b)/100;
0161 XSetForeground(d,gc,nearColor(d,l));
0162 return 0;
0163}
0164
0165int MoveToX(int x,int y)
0166{
0167 if(nxp==-1){
0168 xcur=x;
0169 ycur=y;
0170 }
0171 else{
0172 xp[nxp].x=x;
0173 xp[nxp].y=y;
0174 nxp++;
0175 }
0176 return 0;
0177}
0178
0179int DrawStringX(char *c)
0180{
0181// if(fdirec!=0||spos!=0){
0182 XTextExtents(xfs,c,strlen(c)
0183 ,&i,&j,&k,&cs);
0184 n=cs.width;
0185// }
0186 if(fdirec==0){
0187 if(spos!=0) m=n/spos;
0188 else m=0;
0189 XDrawString(d,p,gc,xcur-m,ycur,c,
0190 strlen(c));
0191 xcur+=n;
0192 }
0193 else{ /* vertical */
0194 ptemp=XCreatePixmap(d,RootWindow(d,0),n,j+k,XDefaultDepth(d,0));
0195 gctemp=XCreateGC(d,ptemp,0,0);
0196 XSetForeground(d,gctemp,WhitePixel(d,0));
0197 XFillRectangle(d,ptemp,gctemp,0,0,n,j+k);
0198 XSetForeground(d,gctemp,BlackPixel(d,0));
0199 XSetFont(d,gctemp,xfs->fid);
0200 XDrawString(d,ptemp,gctemp,0,j,c,
0201 strlen(c));
0202 XSync(d,True);
0203 imgtemp=XGetImage(d,ptemp,0,0,n,j+k,0xffffff,ZPixmap);
0204 hmin=0;
0205 hmax=j+k;
0206 wmin=0;
0207 wmax=n;
0208 o=j;
0209 if(spos!=0) m=n/spos;
0210 else m=0;
0211 for(j=hmin;j<hmax;j++)
0212 for(i=wmin;i<wmax;i++){
0213 l=XGetPixel(imgtemp,i,j);
0214 if(l==0) XDrawPoint(d,p,gc,xcur-o+j,ycur-i+m);
0215 }
0216 ycur+=n;
0217 XDestroyImage(imgtemp);
0218 XFreeGC(d,gctemp);
0219 XFreePixmap(d,ptemp);
0220 }
0221 return 0;
0222}
0223
0224int FontInfoX(int font,int direc,int mode)
0225{
0226// FontX("-b&h-lucida-bold-i-normal-sans-34-240-100-100-p-215-iso8859-1");
0227 char f[60];
0228 char ast[]="*";
0229 char *type[]={"illegal","courier","helvetica","century","symbol","times",
0230 "utopia","lucida","lucidabright","lucidatypewriter","charter"};
0231 int sizn=15;
0232 int siz[]={8,10,11,12,14,15,17,18,19,20,24,25,26,33,34};
0233 char *sizc[]={"-8-","-10-","-11-","-12-","-14-","-15-","-17-","-18-",
0234 "-19-","-20-","-24-","-25-","-26-","-33-","-34-"};
0235 char *face[]={"medium","bold"};
0236 char *ang[]={"r-","i-","o-"};
0237 int i,j,k,l,m,n;
0238
0239 k=font/100;
0240 n=font-k*100;
0241 l=mode/100;
0242 m=mode-l*100;
0243 strcpy(f,ast);
0244 strcat(f,type[k]);
0245 strcat(f,ast);
0246 strcat(f,face[l]);
0247 strcat(f,ast);
0248 strcat(f,ang[m]);
0249 strcat(f,ast);
0250 for(i=0;i<sizn;i++) if(siz[i]==n) break;
0251 strcat(f,sizc[i]);
0252 strcat(f,ast);
0253 xfs=XLoadQueryFont(d,f);
0254 if(xfs==NULL) return 1;
0255 XSetFont(d,gc,xfs->fid);
0256 fdirec=direc;
0257 spos=fdirec;
0258 fdirec=fdirec/10;
0259 spos=spos-fdirec*10;
0260 return 0;
0261}
0262
0263int FontX(char *c)
0264{
0265 xfs=XLoadQueryFont(d,c);
0266 if(xfs==NULL) return 1;
0267 XSetFont(d,gc,xfs->fid);
0268 return 0;
0269}
0270
0271int PointX(int x,int y)
0272{
0273 XDrawPoint(d,p,gc,x,y);
0274 return 0;
0275}
0276
0277int LineToX(int x,int y)
0278{
0279 if(nxp==-1){
0280 XDrawLine(d,p,gc,xcur,ycur,x,y);
0281 xcur=x;
0282 ycur=y;
0283 }
0284 else{
0285 xp[nxp].x=x;
0286 xp[nxp].y=y;
0287 nxp++;
0288 }
0289 return 0;
0290}
0291
0292int LineX(int x1,int y1,int x2,int y2)
0293{
0294 XDrawLine(d,p,gc,x1,y1,x2,y2);
0295 return 0;
0296}
0297
0298int FrameRectX(int x,int y,int wid,int hei)
0299{
0300 XDrawRectangle(d,p,gc,x,y,wid,hei);
0301 return 0;
0302}
0303
0304int FrameOvalX(int x,int y,int wid,int hei)
0305{
0306 XDrawArc(d,p,gc,x,y,wid,hei,0,360*64);
0307 return 0;
0308}
0309
0310int FillRectX(int x,int y,int wid,int hei)
0311{
0312 XFillRectangle(d,p,gc,x,y,wid,hei);
0313 return 0;
0314}
0315
0316int FillOvalX(int x,int y,int wid,int hei)
0317{
0318 XFillArc(d,p,gc,x,y,wid,hei,0,360*64);
0319 return 0;
0320}
0321
0322int OpenPolyX()
0323{
0324 xp=(XPoint *)malloc(sizeof(XPoint)*1000);
0325 nxp=0;
0326 return 0;
0327}
0328
0329int ClosePolyX()
0330{
0331 XFillPolygon(d,p,gc,xp,nxp,Complex,CoordModeOrigin);
0332 XSync(d,True);
0333 free(xp);
0334 nxp=-1;
0335 return 0;
0336}
0337
0338int LineWidthX(int w)
0339{
0340 XSetLineAttributes(d,gc,w,LineSolid,CapButt,JoinMiter);
0341 return 0;
0342}
0343
0344int TextExtentsX(char *s,int *asc,int *desc,int *wid)
0345{
0346 XTextExtents(xfs,s,strlen(s),&i,asc,desc,&cs);
0347 *wid=cs.width;
0348 return 0;
0349}
0350
0351int SetClipRectX(int x,int y,int wid,int hei)
0352{
0353 rec.x=x;
0354 rec.y=y;
0355 rec.width=wid;
0356 rec.height=hei;
0357 XSetClipRectangles(d,gc,0,0,&rec,1,Unsorted);
0358 return 0;
0359}
0360
0361int ClrClipRectX()
0362{
0363 val.clip_mask=None;
0364 XChangeGC(d,gc,GCClipMask,&val);
0365 return 0;
0366}
0367