Back to index

wims  3.65+svn20090927
lines.c
Go to the documentation of this file.
00001 /*    Copyright (C) 1998-2003 XIAO, Gang of Universite de Nice - Sophia Antipolis
00002  *
00003  *  This program is free software; you can redistribute it and/or modify
00004  *  it under the terms of the GNU General Public License as published by
00005  *  the Free Software Foundation; either version 2 of the License, or
00006  *  (at your option) any later version.
00007  *
00008  *  This program is distributed in the hope that it will be useful,
00009  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
00010  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00011  *  GNU General Public License for more details.
00012  *
00013  *  You should have received a copy of the GNU General Public License
00014  *  along with this program; if not, write to the Free Software
00015  *  Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
00016  */
00017        /* line input / output / translation routines
00018         * and error routines */
00019 
00020 void error(char *p)
00021 {
00022     fprintf(stderr,"%s %d\n",p,linecnt);
00023 }
00024 
00025        /* Get a line in a stored working file. 
00026         * Buffer length is always MAX_LINELEN. */
00027 int ggetline(char buf[])
00028 {
00029     int c;
00030     int i;
00031     for(i=0,c=getchar();i<MAX_LINELEN;i++,c=getchar()) {
00032        if(c=='\n' || c==EOF) break;
00033        buf[i]=c;
00034     }
00035     buf[i]=0; 
00036     if(linecnt!=-100000) linecnt++;
00037     return c;
00038 }
00039 
00040 double getvar(char *p)
00041 {
00042     int i;
00043     for(i=0;i<varcnt && strcmp(p,vartab[i].name)!=0;i++);
00044     if(i<varcnt) return vartab[i].value;
00045     else return 0;
00046 }
00047 
00048 void setvar(char *p, double v)
00049 {
00050     int i;
00051     if((strlen(p)>2 && strcmp(p,"animstep")!=0) || !isalpha(*p)) return;
00052     for(i=0;i<varcnt && strcmp(p,vartab[i].name)!=0;i++);
00053     if(i<varcnt) {vartab[i].value=v; return;}
00054     else {
00055        if(varcnt>=MAX_VARS || varnameptr>=varnamebuf+sizeof(varnamebuf)-1) return;
00056        strcpy(varnameptr,p);
00057        vartab[varcnt].name=varnameptr; vartab[varcnt].value=v;
00058        varnameptr+=strlen(varnameptr)+1; (varcnt)++;
00059     }
00060 }
00061 
00062        /* Points to the end of a name */
00063 char *find_name_end(char *p)
00064 {
00065     int i;
00066     for(i=0;isalnum(*p) && i<MAX_LINELEN; p++,i++);
00067     return p;
00068 }
00069 
00070        /* Find the beginning of a name */
00071 char *find_name_start(char *p)
00072 {
00073     int i;
00074     for(i=0; !isalpha(*p) && i<MAX_LINELEN; p++,i++);
00075     return p;
00076 }
00077 
00078 void collapse_item(char *p, int n)
00079 {
00080     int i;
00081     char *pp;
00082     if(n<1) return;
00083     for(i=1,pp=strchr(p,','); i<n && pp!=NULL; i++,pp=strchr(pp+1,','));
00084     if(pp==NULL) *p=0;
00085     else strcpy(p,pp+1);
00086 }
00087 
00088 int getcolor(int r, int g, int b)
00089 {
00090     int col;
00091     if(r>255) r=255; if(r<0) r=0;
00092     if(g>255) g=255; if(g<0) g=0;
00093     if(b>255) b=255; if(b<0) b=0;
00094     col=gdImageColorExact(image, r, g, b);
00095     if(col==-1) col=gdImageColorAllocate(image,r,g,b);
00096     return col;
00097 }
00098 
00099 int widthcolor(int w, int color)
00100 {
00101     int bg,fg,sh,e;
00102            /* already allocated */
00103     if(wimg!=NULL && savew==w && wcolor==color) goto end;
00104     if(wimg!=NULL) gdImageDestroy(wimg);
00105     wimg=gdImageCreate(w,w);
00106     if(wimg==NULL) {
00107        error("width_creation_failure"); return color;
00108     }
00109     bg=gdImageColorAllocate(wimg,255,255,255);
00110     gdImageColorTransparent(wimg,bg);
00111     fg=gdImageColorAllocate(wimg,gdImageRed(image,color),
00112                          gdImageGreen(image,color),
00113                          gdImageBlue(image,color));
00114     e=w-1;sh=e/3;
00115     switch(w) {
00116        case 2: {
00117            gdImageFill(wimg,0,0,fg); break;
00118        }
00119        case 3: {
00120            gdImageFill(wimg,0,0,fg); 
00121            gdImageSetPixel(wimg,2,0,bg);gdImageSetPixel(wimg,2,2,bg);
00122            break;
00123        }
00124        case 4:
00125        case 5:
00126        case 6:
00127        case 7: 
00128        case 8:
00129        case 9:
00130        case 10:
00131        case 11:
00132        case 12: {
00133            int pl[]={0,sh, sh,0, e-sh,0, e,sh,
00134                 e,e-sh, e-sh,e, sh,e, 0,e-sh};
00135            gdImageFilledPolygon(wimg,(gdPointPtr) pl,8,fg);
00136            break;
00137        }
00138        default: {
00139            gdImageArc(wimg,w/2,w/2,w,w,0,360,fg);
00140            gdImageFillToBorder(wimg,w/2,w/2,fg,fg);
00141            break;
00142        }
00143     }
00144     savew=w; wcolor=color;
00145     end: gdImageSetBrush(image,wimg); return gdBrushed;
00146 }
00147 
00148        /* scale coordinates, x then y */
00149 void scale(double dbuf[], int ibuf[], int cnt)
00150 {
00151     int i; double x,y;
00152     for(i=0;i<cnt*2 && i<MAX_PARMS;i+=2) {
00153        if(transform) {
00154            x=dbuf[i]*matrix[0]+dbuf[i+1]*matrix[1];
00155            y=dbuf[i]*matrix[2]+dbuf[i+1]*matrix[3];
00156        }
00157        else {x=dbuf[i]; y=dbuf[i+1];}
00158        scale_buf[i]=x+transx; scale_buf[i+1]=y+transy;
00159        ibuf[i]=rint((x-xstart+transx)*xscale);
00160        ibuf[i+1]=rint((y-ystart+transy)*yscale);
00161        if(ibuf[i]<-BOUND) ibuf[i]=-BOUND;if(ibuf[i]>BOUND) ibuf[i]=BOUND;
00162        if(ibuf[i+1]<-BOUND) ibuf[i+1]=-BOUND;if(ibuf[i+1]>BOUND) ibuf[i+1]=BOUND;
00163     }
00164 }
00165 
00166        /* scale without displacement */
00167 void scale2(double xin, double yin, double *xout, double *yout)
00168 {
00169     if(transform) {
00170        *xout=xin*matrix[0]+yin*matrix[1];
00171        *yout=xin*matrix[2]+yin*matrix[3];
00172     }
00173     else {
00174        *xout=xin; *yout=yin;
00175     }
00176     *xout*=xscale; *yout*=yscale;
00177 }
00178