Subversion Repositories wimsdev

Rev

Rev 18350 | Rev 18352 | Go to most recent revision | Blame | Compare with Previous | Last modification | View Log | RSS feed

  1. /*    Copyright (C) 1998-2003 XIAO, Gang of Universite de Nice - Sophia Antipolis
  2.  *
  3.  *  This program is free software; you can redistribute it and/or modify
  4.  *  it under the terms of the GNU General Public License as published by
  5.  *  the Free Software Foundation; either version 2 of the License, or
  6.  *  (at your option) any later version.
  7.  *
  8.  *  This program is distributed in the hope that it will be useful,
  9.  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
  10.  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  11.  *  GNU General Public License for more details.
  12.  *
  13.  *  You should have received a copy of the GNU General Public License
  14.  *  along with this program; if not, write to the Free Software
  15.  *  Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  16.  */
  17.  
  18. #include <errno.h>
  19. #include "flydraw.h"
  20. void myGdImageArc(gdImagePtr, double, double, double, double, double, double, int);
  21. const double DEG=M_PI/180;
  22.  
  23. /* Tikz things */
  24.  
  25. static char *tikz_color (int the_color)
  26. {
  27.   static char buf[50];
  28.   int r=gdImageRed(image,the_color);
  29.   int g=gdImageGreen(image,the_color);
  30.   int b=gdImageBlue(image,the_color);
  31.   sprintf(buf, "{rgb,255:red,%i;green,%i;blue,%i}",r,g,b);
  32.   return buf;
  33. }
  34.  
  35. static char *tikz_options (int the_color, int the_fill)
  36. {
  37.   static char buf[100];
  38.   if (the_color == gdBrushed)
  39.     the_color = tikz_brushColor;
  40.   sprintf (buf, "draw=%s,cap=round",tikz_color(the_color));
  41.   if (the_color != tikz_brushColor) strcat(buf, ",thin");
  42.   if (the_fill == -1 || the_fill == 2) strcat(buf, ",dashed");
  43.   if (the_fill == 1 || the_fill == 2)
  44.     sprintf(buf+strlen(buf), ",fill=%s", tikz_color(the_color));
  45.   return buf;
  46. }
  47.  
  48. #define flip(y) (sizey-1-(y))
  49.  
  50. /* bug in gdImageFillToBorder */
  51.  
  52. void patchgdImageFillToBorder (gdImagePtr im, int x, int y, int border, int color)
  53. {
  54.   if(x>=im->sx || x<0 || y>=im->sy || y<0) return;
  55.   gdImageFillToBorder(im,x,y,border,color);
  56. }
  57.  
  58. void patchgdImageFill (gdImagePtr im, int x, int y, int color)
  59. {
  60.   if(x>=im->sx || x<0 || y>=im->sy || y<0) return;
  61.   gdImageFill(im,x,y,color);
  62. }
  63.  
  64. #define DASHPIXELS 8
  65.  
  66. void myDashedLine(gdImagePtr im, int x1, int y1, int x2, int y2, int color){
  67.   /**
  68.    * emulates gdImageDashedLine with gdImageSetStyle and gdImageLine
  69.    * GK: As gdImageDashedLine is slightly broken in latest libgd libraries,
  70.    * GK: I implemented an emulation named "myDashedLine"
  71.    **/
  72.  
  73.   int styleDashed[DASHPIXELS],i;
  74.   for (i=0; i< DASHPIXELS/2; i++) styleDashed[i]=color;
  75.   for (; i< DASHPIXELS; i++) styleDashed[i]=gdTransparent;
  76.   gdImageSetStyle(im, styleDashed, DASHPIXELS);
  77.   gdImageLine(im, x1, y1, x2, y2, gdStyled);
  78. }
  79.  
  80.  
  81. /* File opening: with security */
  82. FILE *open4read(char *n)
  83. {
  84.   char *p, *p1, *p2, namebuf[2048];
  85.   int t;
  86.   FILE *f;
  87.   n=find_word_start(n);
  88.   if(*n==0) return NULL;
  89.   p=getenv("flydraw_filebase");
  90.   p1=n+strlen(n)-4;if(p1<n || strcasecmp(p1,".gif")!=0) t=1; else t=0;
  91.   if(p!=NULL && *p!=0) {
  92.     char pbuf[MAX_LINELEN+1];
  93.     snprintf(pbuf,sizeof(pbuf),"%s",p);
  94.     p=find_word_start(pbuf); if(strstr(p,"..")!=NULL) return NULL;
  95.     if(*n=='/' || strstr(n,"..")!=NULL) return NULL;
  96.     /* prohibit unusual file/dir names */
  97.     for(p1=p;*p1;p1++)
  98.       if(!isalnum(*p1) && !isspace(*p1) && strchr("~_-/.",*p1)==NULL)
  99.         return NULL;
  100.     for(p1=n;*p1;p1++)
  101.       if(!isalnum(*p1) && !isspace(*p1) && strchr("~_-/.",*p1)==NULL)
  102.         return NULL;
  103.     f=NULL;
  104.     for(p1=p; *p1; p1=find_word_start(p2)) {
  105.       p2=find_word_end(p1);
  106.       if(*p2) *p2++=0;
  107.       snprintf(namebuf,sizeof(namebuf),"%s/%s",p1,n);
  108.       f=fopen(namebuf,"r"); if(f!=NULL) goto imgtype;
  109.     }
  110.     p1=getenv("w_wims_session");
  111.     if(p1!=NULL && strncmp(n,"insert",6)==0) {
  112.       snprintf(namebuf,sizeof(namebuf),"../s2/%s/%s",p1,n);
  113.       f=fopen(namebuf,"r");
  114.     }
  115.   }
  116.   else {
  117.     snprintf(namebuf,sizeof(namebuf),"%s",n);
  118.     f=fopen(namebuf,"r");
  119.   }
  120.   imgtype:
  121.   if(t && f!=NULL) {
  122.     char tbuf[1024],sbuf[4096];
  123.     fclose(f); f=NULL;
  124.     p1=getenv("TMPDIR"); if(p1==NULL || *p1==0) p1=".";
  125.     snprintf(tbuf,sizeof(tbuf),"%s/drawfile_.gif",p1);
  126.     snprintf(sbuf,sizeof(sbuf),"convert %s %s",namebuf,tbuf);
  127.     if (system(sbuf)) fly_error("system_failed");
  128.       f=fopen(tbuf,"r");
  129.   }
  130.   return f;
  131. }
  132.  
  133. /* Does nothing; just a comment. */
  134. void obj_comment(objparm *pm)
  135. {
  136.   return;
  137. }
  138.  
  139. /* define image size */
  140. void obj_size(objparm *pm)
  141. {
  142.   sizex=rint(pm->pd[0]); sizey=rint(pm->pd[1]);
  143.   if(sizex<0 || sizey<0 || sizex>MAX_SIZE || sizey>MAX_SIZE) {
  144.     fly_error("bad_size"); return;
  145.   }
  146.   if(image!=NULL) {
  147.     fly_error("size_already_defined"); return;
  148.   }
  149.   image=gdImageCreate(sizex,sizey);
  150.   if(tikz_file) fprintf(tikz_file,"\\clip (0,0) rectangle (%i, %i);\n", sizex, sizey);
  151.   if(image==NULL) fly_error("image_creation_failure");
  152.   else {
  153.     color_white=gdImageColorAllocate(image,255,255,255);
  154.     color_black=gdImageColorAllocate(image,0,0,0);
  155.     color_bounder=gdImageColorAllocate(image,1,2,3);
  156.     color_frame=gdImageColorAllocate(image,254,254,254);
  157.   }
  158. }
  159.  
  160. /* new image */
  161. void obj_new(objparm *pm)
  162. {
  163.   if(image) {
  164.     gdImageDestroy(image);image=NULL;
  165.   }
  166.   if(pm->pcnt>=2) { obj_size(pm); gdImageRectangle(image,0,0,sizex-1,sizey-1,color_frame);}
  167.   else sizex=sizey=0;
  168.   saved=0;
  169. }
  170.  
  171. /* new image */
  172. void obj_existing(objparm *pm)
  173. {
  174.   FILE *inf;
  175.   char *pp;
  176.  
  177.   if(image) {
  178.     gdImageDestroy(image);image=NULL;
  179.   }
  180.   pp=find_word_start(pm->str);*find_word_end(pp)=0;
  181.   inf=open4read(pp);
  182.   if(inf==NULL) {
  183.     fly_error("file_not_exist"); return;
  184.   }
  185.   image=gdImageCreateFromGif(inf); fclose(inf);
  186.   if(image==NULL) {
  187.     fly_error("bad_gif"); return;
  188.   }
  189.   sizex=image->sx; sizey=image->sy;
  190.   saved=0;
  191. }
  192. /* tikzfile */
  193. void obj_tikzfile(objparm *pm)
  194. {
  195.   char *p;
  196.   if(tikz_file){
  197.     fprintf(tikz_file,"\\end{tikzpicture}\n");
  198.     fclose(tikz_file);
  199.   }
  200.   p=find_word_start(pm->str); *find_word_end(p)=0;
  201.   snprintf(tikzfilename,sizeof(tikzfilename),"%s",p);
  202.   tikz_file=fopen(tikzfilename,"w");
  203.   if(!tikz_file)
  204.     fly_error("tikz_nopen");
  205.   fprintf(tikz_file,"\\begin{tikzpicture}\[x=0.02cm, y=0.02cm]\n");
  206. }
  207.  
  208. /* segment */
  209. void obj_line(objparm *pm)
  210. {
  211.   scale(pm->pd,pm->p,2);
  212.   if(pm->fill==-1 || pm->fill==2 )
  213.     myDashedLine(image,pm->p[0],pm->p[1],pm->p[2],pm->p[3],pm->color[0]);
  214.   else
  215.     gdImageLine(image,pm->p[0],pm->p[1],pm->p[2],pm->p[3],pm->color[0]);
  216.   if (tikz_file)
  217.     fprintf(tikz_file, "\\draw\[%s] (%i, %i) -- (%i, %i);\n",
  218.       tikz_options(pm->color[0],pm->fill),pm->p[0],flip(pm->p[1]),pm->p[2],flip(pm->p[3]));
  219.   if(vimg_enable) vimg_line(scale_buf[0],scale_buf[1],scale_buf[2],scale_buf[3]);
  220. }
  221.  
  222. void _obj_arrow(objparm *pm, int twoside)
  223. {
  224.   int l,xx,yy;
  225.   gdPoint ii[3];
  226.   double dx,dy,length,dd[6];
  227.   scale(pm->pd,pm->p,2);
  228.   xx=ii[0].x=pm->p[2];yy=ii[0].y=pm->p[3];
  229.   l=pm->pd[4];if(l<0) l=0; if(l>200) l=200;
  230.   scale2(pm->pd[0]-pm->pd[2],pm->pd[1]-pm->pd[3],&dx,&dy);
  231.   length=sqrt(dx*dx+dy*dy);
  232.   if(length<3 || l<5) goto stem;
  233.   dd[0]=l*dx/length; dd[1]=l*dy/length;
  234.   #define fat 0.27
  235.   dd[2]=dd[0]+dd[1]*fat; dd[3]=dd[1]-dd[0]*fat;
  236.   dd[4]=dd[0]-dd[1]*fat; dd[5]=dd[1]+dd[0]*fat;
  237.   ii[1].x=rint(dd[2])+ii[0].x; ii[1].y=rint(dd[3])+ii[0].y;
  238.   ii[2].x=rint(dd[4])+ii[0].x; ii[2].y=rint(dd[5])+ii[0].y;
  239.   gdImageFilledPolygon(image, ii,3,pm->color[0]);
  240.   if(tikz_file)
  241.     fprintf(tikz_file, "\\draw\[%s] (%i, %i) -- (%i, %i) -- (%i, %i) -- cycle;\n",
  242.       tikz_options(pm->color[0],1),ii[0].x,flip(ii[0].y),ii[1].x,flip(ii[1].y),ii[2].x,flip(ii[2].y));
  243.   if(vimg_enable) vimg_polyline(scale_buf,3,1);
  244.   xx=rint(dd[0])+ii[0].x;yy=rint(dd[1])+ii[0].y;
  245.   if(twoside) {
  246.     ii[0].x=pm->p[0]; ii[0].y=pm->p[1];
  247.     ii[1].x=-rint(dd[2])+ii[0].x; ii[1].y=-rint(dd[3])+ii[0].y;
  248.     ii[2].x=-rint(dd[4])+ii[0].x; ii[2].y=-rint(dd[5])+ii[0].y;
  249.     gdImageFilledPolygon(image, ii,3,pm->color[0]);
  250.   if(tikz_file)
  251.     fprintf(tikz_file, "\\draw\[%s] (%i, %i) -- (%i, %i) -- (%i, %i) -- cycle;\n",
  252.       tikz_options(pm->color[0],1),ii[0].x,flip(ii[0].y),ii[1].x,flip(ii[1].y),ii[2].x,flip(ii[2].y));
  253.   }
  254.   stem: if(pm->fill==-1 || pm->fill==2)
  255.     myDashedLine(image,pm->p[0],pm->p[1],xx,yy,pm->color[0]);
  256.   else
  257.     gdImageLine(image,pm->p[0],pm->p[1],xx,yy,pm->color[0]);
  258.   if(tikz_file)
  259.     fprintf(tikz_file, "\\draw\[%s] (%i, %i) -- (%i, %i);\n",
  260.       tikz_options(pm->color[0],pm->fill),pm->p[0],flip(pm->p[1]),xx,flip(yy));
  261.   if(vimg_enable) vimg_line(scale_buf[0],scale_buf[1],scale_buf[2],scale_buf[3]);
  262. }
  263.  
  264. /* Arrow */
  265. void obj_arrow(objparm *pm)
  266. {
  267.   _obj_arrow(pm,0);
  268. }
  269.  
  270. /* 2-sided arrow */
  271. void obj_arrow2(objparm *pm)
  272. {
  273.    _obj_arrow(pm,1);
  274. }
  275.  
  276. void _obj_arrows(objparm *pm, int twoside)
  277. {
  278.   #define fat 0.27
  279.   int i,l,xx,yy;
  280.   gdPoint ii[3];
  281.   double dx,dy,length,dd[6];
  282.   l=pm->pd[0];if(l<0) l=0; if(l>200) l=200;
  283.   scale(pm->pd+1,pm->p+1,pm->pcnt/2);
  284.   for (i=1;i<pm->pcnt;i+=4){
  285.     xx=ii[0].x=pm->p[i+2];yy=ii[0].y=pm->p[i+3];
  286.     scale2(pm->pd[i]-pm->pd[i+2],pm->pd[i+1]-pm->pd[i+3],&dx,&dy);
  287.     length=sqrt(dx*dx+dy*dy);
  288.     if(length<3 || l<5) goto stem;
  289.     dd[0]=l*dx/length; dd[1]=l*dy/length;
  290.     dd[2]=dd[0]+dd[1]*fat; dd[3]=dd[1]-dd[0]*fat;
  291.     dd[4]=dd[0]-dd[1]*fat; dd[5]=dd[1]+dd[0]*fat;
  292.     ii[1].x=rint(dd[2])+ii[0].x; ii[1].y=rint(dd[3])+ii[0].y;
  293.     ii[2].x=rint(dd[4])+ii[0].x; ii[2].y=rint(dd[5])+ii[0].y;
  294.     gdImageFilledPolygon(image, ii,3,pm->color[0]);
  295.     if(tikz_file)
  296.   fprintf(tikz_file, "\\draw\[%s] (%i, %i) -- (%i, %i) -- (%i, %i) -- cycle;\n",
  297.         tikz_options(pm->color[0],1),ii[0].x,flip(ii[0].y),ii[1].x,flip(ii[1].y),ii[2].x,flip(ii[2].y));
  298.       xx=rint(dd[0])+ii[0].x;yy=rint(dd[1])+ii[0].y;
  299.       if(twoside) {
  300.   ii[0].x=pm->p[i]; ii[0].y=pm->p[i+1];
  301.   ii[1].x=-rint(dd[2])+ii[0].x; ii[1].y=-rint(dd[3])+ii[0].y;
  302.   ii[2].x=-rint(dd[4])+ii[0].x; ii[2].y=-rint(dd[5])+ii[0].y;
  303.   gdImageFilledPolygon(image, ii,3,pm->color[0]);
  304.   if(tikz_file)
  305.     fprintf(tikz_file, "\\draw\[%s] (%i, %i) -- (%i, %i) -- (%i, %i) -- cycle;\n",
  306.         tikz_options(pm->color[0],1),ii[0].x,flip(ii[0].y),ii[1].x,flip(ii[1].y),ii[2].x,flip(ii[2].y));
  307.       }
  308.     stem: if(pm->fill==-1 || pm->fill==2)
  309.   myDashedLine(image,pm->p[i],pm->p[i+1],xx,yy,pm->color[0]);
  310.       else
  311.   gdImageLine(image,pm->p[i],pm->p[i+1],xx,yy,pm->color[0]);
  312.       if(tikz_file)
  313.   fprintf(tikz_file, "\\draw\[%s] (%i, %i) -- (%i, %i);\n",
  314.     tikz_options(pm->color[0],pm->fill),pm->p[i],flip(pm->p[i+1]),xx,flip(yy));
  315.     }
  316. }
  317.  
  318. /* Arrows */
  319. void obj_arrows(objparm *pm)
  320. {
  321.   _obj_arrows(pm,0);
  322. }
  323.  
  324. /* 2-sided arrows */
  325. void obj_arrows2(objparm *pm)
  326. {
  327.    _obj_arrows(pm,1);
  328. }
  329.  
  330. /* horizontal line */
  331. void obj_hline(objparm *pm)
  332. {
  333.   scale(pm->pd,pm->p,1);
  334.   if(pm->fill==-1 || pm->fill==2)
  335.     myDashedLine(image,0,pm->p[1],sizex,pm->p[1],pm->color[0]);
  336.   else
  337.     gdImageLine(image,0,pm->p[1],sizex,pm->p[1],pm->color[0]);
  338.   if (tikz_file)
  339.     fprintf(tikz_file, "\\draw\[%s] (%i, %i) -- (%i, %i);\n",
  340.       tikz_options(pm->color[0],pm->fill),0,flip(pm->p[1]),sizex,flip(pm->p[1]));
  341. }
  342.  
  343. static void line_extend(int x1, int y1, int x2, int y2, int* xend, int *yend)
  344. {
  345.   double t1, t2, t3;
  346.   if (x2 != x1){t1 = (x2 > x1) ? sizex : 0; t1 = (t1 - x1)/(x2 - x1);}
  347.   else t1 = INFINITY;
  348.   if (y2 != y1){t2 = (y2 > y1) ? sizey : 0; t2 = (t2 - y1)/(y2 - y1);}
  349.   else t2 = INFINITY;
  350.   t3 = (t1 < t2) ? t1 : t2;
  351.   *xend = x1+t3*(x2-x1);
  352.   *yend = y1+t3*(y2-y1);
  353. }
  354.  
  355. /* halfline */
  356. void obj_halfline(objparm *pm)
  357. {
  358.   int xend,yend;
  359.   scale(pm->pd,pm->p,2);
  360.   line_extend(pm->p[0],pm->p[1],pm->p[2],pm->p[3],&xend,&yend);
  361.   if(pm->fill==-1 || pm->fill==2)
  362.     myDashedLine(image,pm->p[0],pm->p[1],xend,yend,pm->color[0]);
  363.   else
  364.     gdImageLine(image,pm->p[0],pm->p[1],xend,yend,pm->color[0]);
  365.   if (tikz_file)
  366.     fprintf(tikz_file, "\\draw\[%s] (%i, %i) -- (%i, %i);\n",
  367.       tikz_options(pm->color[0],pm->fill),
  368.       pm->p[0],flip(pm->p[1]),xend,flip(yend));
  369. }
  370.  
  371. void obj_fullline(objparm *pm)
  372. {
  373.   int xstart,ystart,xend,yend;
  374.   scale(pm->pd,pm->p,2);
  375.   line_extend(pm->p[0],pm->p[1],pm->p[2],pm->p[3],&xend,&yend);
  376.   line_extend(pm->p[2],pm->p[3],pm->p[0],pm->p[1],&xstart,&ystart);
  377.   if(pm->fill==-1 || pm->fill==2)
  378.     myDashedLine(image,xstart,ystart,xend,yend,pm->color[0]);
  379.   else
  380.     gdImageLine(image,xstart,ystart,xend,yend,pm->color[0]);
  381.   if (tikz_file)
  382.     fprintf(tikz_file, "\\draw\[%s] (%i, %i) -- (%i, %i);\n",
  383.       tikz_options(pm->color[0],pm->fill),xstart,flip(ystart),xend,flip(yend));
  384. }
  385.  
  386. void obj_fulllines(objparm *pm)
  387. {
  388.   int i,xstart,ystart,xend,yend;
  389.   scale(pm->pd,pm->p,pm->pcnt/2);
  390.   if (tikz_file)
  391.       fprintf(tikz_file, "\\draw\[%s]",tikz_options(pm->color[0],pm->fill));
  392.   for (i=0;i<pm->pcnt;i+=4){
  393.     line_extend(pm->p[i],pm->p[i+1],pm->p[i+2],pm->p[i+3],&xend,&yend);
  394.     line_extend(pm->p[i+2],pm->p[i+3],pm->p[i],pm->p[i+1],&xstart,&ystart);
  395.     if(pm->fill==-1 || pm->fill==2)
  396.       myDashedLine(image,xstart,ystart,xend,yend,pm->color[0]);
  397.     else
  398.       gdImageLine(image,xstart,ystart,xend,yend,pm->color[0]);
  399.     if (tikz_file)
  400.       fprintf(tikz_file, "(%i,%i)--(%i, %i)",xstart,flip(ystart),xend,flip(yend));
  401.   }
  402.   if (tikz_file)
  403.       fprintf(tikz_file, ";\n");
  404. }
  405.  
  406. /* vertical line */
  407. void obj_vline(objparm *pm)
  408. {
  409.   scale(pm->pd,pm->p,1);
  410.   if(pm->fill==-1 || pm->fill==2)
  411.     myDashedLine(image,pm->p[0],0,pm->p[0],sizey,pm->color[0]);
  412.   else
  413.     gdImageLine(image,pm->p[0],0,pm->p[0],sizey,pm->color[0]);
  414.   if (tikz_file)
  415.     fprintf(tikz_file, "\\draw\[%s] (%i, %i) -- (%i, %i);\n",
  416.       tikz_options(pm->color[0],pm->fill),pm->p[0],0,pm->p[0],sizey);
  417. }
  418.  
  419. /* dashed line */
  420. void obj_dline(objparm *pm)
  421. {
  422.   scale(pm->pd,pm->p,2);
  423.   myDashedLine(image,pm->p[0],pm->p[1],pm->p[2],pm->p[3], pm->color[0]);
  424.   if (tikz_file)
  425.     fprintf(tikz_file, "\\draw\[%s] (%i, %i) -- (%i, %i);\n",
  426.       tikz_options(pm->color[0],-1),
  427.       pm->p[0],flip(pm->p[1]),pm->p[2],flip(pm->p[3]));
  428. }
  429.  
  430. /* parallel lines.
  431.  * x1,y1,x2,y2,xv,yv,n,color */
  432. void obj_parallel(objparm *pm)
  433. {
  434.   int i, n, xi,yi;
  435.   double xv,yv;
  436.   n=pm->pd[6]; if(n<0) return; if(n>256) n=256;
  437.   scale(pm->pd,pm->p,3);
  438.   scale2(pm->pd[4],pm->pd[5],&xv,&yv);
  439.   if (tikz_file)
  440.     fprintf(tikz_file, "\\draw\[%s]", tikz_options(pm->color[0],pm->fill));
  441.   for(i=0;i<n;i++) {
  442.     xi=rint(i*xv); yi=rint(i*yv);
  443.     if(pm->fill==-1 || pm->fill==2)
  444.       myDashedLine(image,pm->p[0]+xi,pm->p[1]+yi,pm->p[2]+xi,pm->p[3]+yi,
  445.         pm->color[0]);
  446.     else
  447.       gdImageLine(image,pm->p[0]+xi,pm->p[1]+yi,pm->p[2]+xi,pm->p[3]+yi,
  448.         pm->color[0]);
  449.     if (tikz_file)
  450.       fprintf(tikz_file, "(%i,%i)--(%i, %i)",
  451.         pm->p[0]+xi,flip(pm->p[1]+yi),pm->p[2]+xi,flip(pm->p[3]+yi));
  452.     if(vimg_enable) vimg_line(scale_buf[0]+i*(scale_buf[4]-transx),
  453.       scale_buf[1]+i*(scale_buf[5]-transy),
  454.       scale_buf[2]+i*(scale_buf[4]-transx),
  455.       scale_buf[3]+i*(scale_buf[5]-transy));
  456.   }
  457.  if(tikz_file) fprintf(tikz_file,";\n");
  458. }
  459.  
  460. /* rectangle */
  461. void obj_rect(objparm *pm)
  462. {
  463.   int x1,y1,x2,y2;
  464.   scale(pm->pd,pm->p,2);
  465.   x1=min(pm->p[0],pm->p[2]); x2=max(pm->p[0],pm->p[2]);
  466.   y1=min(pm->p[1],pm->p[3]); y2=max(pm->p[1],pm->p[3]);
  467.   if(pm->fill!=0){
  468.     if(pm->fill==-1 || pm->fill==2){
  469.       myDashedLine(image,x1,y1,x1,y2,pm->color[0]);
  470.       myDashedLine(image,x1,y2,x2,y2,pm->color[0]);
  471.       myDashedLine(image,x2,y2,x2,y1,pm->color[0]);
  472.       myDashedLine(image,x2,y1,x1,y1,pm->color[0]);
  473.     }
  474.     if(pm->fill==1 || pm->fill==2)
  475.       gdImageFilledRectangle(image,x1,y1,x2,y2,pm->color[0]);
  476.   }
  477.   else
  478.     gdImageRectangle(image,x1,y1,x2,y2,pm->color[0]);
  479.   if(tikz_file)
  480.     fprintf(tikz_file,
  481.       "\\draw\[%s] (%i,%i) rectangle (%i,%i);\n",
  482.       tikz_options(pm->color[0],pm->fill),x1,flip(y1),x2,flip(y2));
  483.   if(vimg_enable) vimg_rect(scale_buf[0],scale_buf[1],scale_buf[2],scale_buf[3]);
  484. }
  485.  
  486. /* square */
  487. void obj_square(objparm *pm)
  488. {
  489.   int w,h;
  490.   scale(pm->pd,pm->p,1);
  491.   w=rint(pm->pd[2]); h=rint(pm->pd[2]);
  492.   if(pm->fill!=0){
  493.     if(pm->fill==-1 || pm->fill==2){
  494.       myDashedLine(image,pm->p[0],pm->p[1],pm->p[0]+w,pm->p[1],pm->color[0]);
  495.       myDashedLine(image,pm->p[0]+w,pm->p[1],pm->p[0]+w,pm->p[1]+h,pm->color[0]);
  496.       myDashedLine(image,pm->p[0]+w,pm->p[1]+h,pm->p[0],pm->p[1]+h,pm->color[0]);
  497.       myDashedLine(image,pm->p[0],pm->p[1]+h,pm->p[0],pm->p[1],pm->color[0]);
  498.     }
  499.     if(pm->fill==1 || pm->fill==2)
  500.     gdImageFilledRectangle(image,pm->p[0],pm->p[1],
  501.         pm->p[0]+w,pm->p[1]+h,pm->color[0]);
  502.   }
  503.   else
  504.     gdImageRectangle(image,pm->p[0],pm->p[1],
  505.                pm->p[0]+w,pm->p[1]+h,pm->color[0]);
  506.   if(tikz_file){
  507.     fprintf(tikz_file,
  508.       "\\draw\[%s] (%i,%i) rectangle (%i,%i);\n",
  509.       tikz_options(pm->color[0],pm->fill),pm->p[0],flip(pm->p[1]),
  510.       pm->p[0]+w,flip(pm->p[1]+h));
  511.   }
  512.   if(vimg_enable) vimg_rect(scale_buf[0],scale_buf[1],
  513.                       scale_buf[0]+pm->pd[2],scale_buf[1]+pm->pd[2]);
  514. }
  515.  
  516. /* triangle */
  517. void obj_triangle(objparm *pm)
  518. {
  519.   scale(pm->pd,pm->p,3);
  520.   int i,n=2;
  521.   if(pm->fill!=0){
  522.     if(pm->fill==-1 || pm->fill==2){
  523.       for(i=0;i<n;i+=1)
  524.         myDashedLine(image,pm->p[2*i],pm->p[2*i+1],pm->p[2*i+2],pm->p[2*i+3],pm->color[0]);
  525.       myDashedLine(image,pm->p[2*n],pm->p[2*n+1],pm->p[0],pm->p[1],pm->color[0]);
  526.     }
  527.     if(pm->fill==1 || pm->fill==2)
  528.       gdImageFilledPolygon(image,(gdPointPtr) pm->p,3,pm->color[0]);
  529.   }
  530.   else
  531.     gdImagePolygon(image,(gdPointPtr) pm->p,3,pm->color[0]);
  532.   if (tikz_file)
  533.     fprintf(tikz_file, "\\draw\[%s] (%i, %i) -- (%i, %i) -- (%i, %i) -- cycle;\n",
  534.       tikz_options(pm->color[0],pm->fill),pm->p[0],flip(pm->p[1]),pm->p[2],flip(pm->p[3]),pm->p[4],flip(pm->p[5]));
  535.   if(vimg_enable) vimg_polyline(scale_buf,3,1);
  536. }
  537.  
  538. /* polygon */
  539. void obj_poly(objparm *pm)
  540. {
  541.   int cnt,i;
  542.   cnt=(pm->pcnt)/2;
  543.   scale(pm->pd,pm->p,cnt);
  544.   if(pm->fill==1 || pm->fill==2)
  545.     gdImageFilledPolygon(image,(gdPointPtr) pm->p,cnt,pm->color[0]);
  546.   else
  547.     gdImagePolygon(image,(gdPointPtr) pm->p,cnt,pm->color[0]);
  548.   if(tikz_file){
  549.     fprintf(tikz_file,"\\draw\[%s] (%i,%i) --",tikz_options(pm->color[0],pm->fill),pm->p[0],flip(pm->p[1]));
  550.     for (i= 1; i<cnt; i++)
  551.         fprintf(tikz_file,"(%i,%i)--", pm->p[2*i],flip(pm->p[2*i+1]));
  552.     fprintf(tikz_file," cycle;\n");
  553.   }
  554.   if(vimg_enable) vimg_polyline(scale_buf,cnt,1);
  555. }
  556.  
  557. /* rays */
  558. void obj_rays(objparm *pm)
  559. {
  560.   int i, n;
  561.   n=(pm->pcnt)/2;
  562.   scale(pm->pd,pm->p,n);
  563.   if (tikz_file)
  564.     fprintf(tikz_file, "\\draw\[%s]",tikz_options(pm->color[0],pm->fill));
  565.   for(i=2;i<2*n;i+=2) {
  566.     if(pm->fill==-1 || pm->fill==2)
  567.       myDashedLine(image,pm->p[0],pm->p[1],pm->p[i],pm->p[i+1],pm->color[0]);
  568.     else
  569.       gdImageLine(image,pm->p[0],pm->p[1],pm->p[i],pm->p[i+1],pm->color[0]);
  570.     if (tikz_file)
  571.       fprintf(tikz_file, "(%i,%i) -- (%i,%i)",
  572.         pm->p[0],flip(pm->p[1]),pm->p[i],flip(pm->p[i+1]));
  573.     if(vimg_enable) vimg_line(scale_buf[0],scale_buf[1],
  574.                         scale_buf[i],scale_buf[i+1]);
  575.   }
  576.   if (tikz_file) fprintf(tikz_file,";\n");
  577. }
  578. /* crosshair */
  579. void obj_crosshair(objparm *pm)
  580. {
  581.   scale(pm->pd,pm->p,2);
  582.   gdImageLine(image,pm->p[0]+width2,pm->p[1]+width2,pm->p[0]-width2,pm->p[1]-width2,pm->color[0]);
  583.   gdImageLine(image,pm->p[0]-width2,pm->p[1]+width2,pm->p[0]+width2,pm->p[1]-width2,pm->color[0]);
  584.   if (tikz_file){
  585.     fprintf(tikz_file, "\\draw\[%s] (%i, %i) -- (%i, %i);\n",
  586.       tikz_options(pm->color[0],pm->fill),pm->p[0]+width2,flip(pm->p[1]+width2),pm->p[0]-width2,flip(pm->p[1]-width2));
  587.     fprintf(tikz_file, "\\draw\[%s] (%i, %i) -- (%i, %i);\n",
  588.       tikz_options(pm->color[0],pm->fill),pm->p[0]-width2,flip(pm->p[1]+width2),pm->p[0]+width2,flip(pm->p[1]-width2));
  589.   }
  590. }
  591. /* crosshairs */
  592.  
  593. void obj_crosshairs(objparm *pm)
  594. {
  595.   int i, n;
  596.   n=(pm->pcnt)/2;
  597.   scale(pm->pd,pm->p,n);
  598.   if (tikz_file)
  599.       fprintf(tikz_file, "\\draw\[%s]",tikz_options(pm->color[0],pm->fill));
  600.   for(i=0;i<2*n;i+=2) {
  601.     gdImageLine(image,pm->p[i]+width2,pm->p[i+1]+width2,pm->p[i]-width2,pm->p[i+1]-width2,pm->color[0]);
  602.     gdImageLine(image,pm->p[i]-width2,pm->p[i+1]+width2,pm->p[i]+width2,pm->p[i+1]-width2,pm->color[0]);
  603.     if (tikz_file){
  604.       fprintf(tikz_file, "(%i, %i) -- (%i, %i) ",
  605.         pm->p[i]+width2,flip(pm->p[i+1]+width2),pm->p[i]-width2,flip(pm->p[i+1]-width2));
  606.       fprintf(tikz_file, "(%i, %i) -- (%i, %i)",
  607.         pm->p[i]-width2,flip(pm->p[i+1]+width2),pm->p[i]+width2,flip(pm->p[i+1]-width2));
  608.     }
  609.   }
  610.   if (tikz_file) fprintf(tikz_file,";\n");
  611. }
  612.  
  613.  
  614. /* segments */
  615. void obj_lines(objparm *pm)
  616. {
  617.  int i, n;
  618.  n=(pm->pcnt)/2;
  619.  scale(pm->pd,pm->p,n);
  620.  if (tikz_file){
  621.    fprintf(tikz_file,"\\draw\[%s] (%i,%i)",
  622.      tikz_options(pm->color[0],pm->fill),pm->p[0],flip(pm->p[1]));
  623.    for(i=2;i<2*n;i+=2)
  624.      fprintf(tikz_file, "--(%i,%i)",pm->p[i],flip(pm->p[i+1]));
  625.    fprintf(tikz_file, ";\n");
  626.  }
  627.  for(i=2;i<2*n;i+=2){
  628.   if(pm->fill==-1 || pm->fill==2 )
  629.     myDashedLine(image,pm->p[i-2],pm->p[i-1],pm->p[i],pm->p[i+1],pm->color[0]);
  630.   else
  631.     gdImageLine(image,pm->p[i-2],pm->p[i-1],pm->p[i],pm->p[i+1],pm->color[0]);
  632.   if(vimg_enable) vimg_polyline(scale_buf,n,0);
  633.   }
  634. }
  635. /*segments from x1,y1 to x2,y2, x3,y3 to x4,y4, etc */
  636. void obj_segments(objparm *pm)
  637. {
  638.   int i, n;
  639.   n=(pm->pcnt)/2;
  640.   scale(pm->pd,pm->p,n);
  641.   if (tikz_file)
  642.     fprintf(tikz_file, "\\draw\[%s]",tikz_options(pm->color[0],pm->fill));
  643.   for(i=0;i<2*n;i+=4){
  644.     if(pm->fill==-1 || pm->fill==2)
  645.       myDashedLine(image,pm->p[i],pm->p[i+1],pm->p[i+2],pm->p[i+3],pm->color[0]);
  646.     else
  647.       gdImageLine(image,pm->p[i],pm->p[i+1],pm->p[i+2],pm->p[i+3],pm->color[0]);
  648.     if (tikz_file)
  649.       fprintf(tikz_file, "(%i,%i)--(%i,%i)",
  650.        pm->p[i],flip(pm->p[i+1]),pm->p[i+2],flip(pm->p[i+3]));
  651.   }
  652.   if(tikz_file) fprintf(tikz_file, ";\n");
  653. }
  654. /* for geodesics, return data for obj_hypgeods + a point on the arc
  655.   to be used for filling hyperbolic triangle*/
  656. int hypgeodaux(double *q, double* res)
  657. {
  658.   double r,cx,cy,a1,a2,a3,tmp,
  659.     nx = -q[0]*q[2]*q[2]+(q[0]*q[0]+q[1]*q[1]+1)*q[2]-q[0]*q[3]*q[3]-q[0],
  660.     ny = -q[1]*q[2]*q[2]-q[1]*q[3]*q[3]+(q[0]*q[0]+q[1]*q[1]+1)*q[3]-q[1],
  661.     dy = -2*q[1]*q[2]+2*q[0]*q[3];
  662.   if (dy*dy*1e4 < nx*nx+ny*ny){
  663.     res[5]=(q[0]+q[2])/2;
  664.     res[6]=(q[1]+q[3])/2;
  665.     return 0;}
  666.   cx = ny/dy; cy=-nx/dy;
  667.   a1 = atan2(q[1]-cy, q[0]-cx);
  668.   a2 = atan2(q[3]-cy, q[2]-cx);
  669.   if (fabs(a2-a1)>M_PI){if(a1<a2) a1+=2*M_PI; else a2+=2*M_PI;};
  670.   a3 = (a1+a2)/2;
  671.   if(a1>a2) {tmp=a1; a1=a2; a2=tmp;}
  672.   r = sqrt(cx*cx+cy*cy-1);
  673.   res[0]=cx;
  674.   res[1]=cy;
  675.   res[2]=r;
  676.   res[3]=a1;
  677.   res[4]=a2;
  678.   res[5]=cx+r*cos(a3);
  679.   res[6]=cy+r*sin(a3);
  680.   return 1;
  681. }
  682. /* hyperbolic geodesics from x1,y1 to x2,y2, x3,y3 to x4,y4 in Poincaré disk */
  683.  
  684. void obj_hypgeods(objparm *pm)
  685. {
  686.   int i, *qq=pm->p;
  687.   double rx,ry,res[7];
  688.   for (i = 0; i < pm->pcnt; i+=4)
  689.     if (hypgeodaux(pm->pd+i,res)){
  690.       scale2(res[2],res[2],&rx,&ry);
  691.       scale(res,qq,1);
  692.       myGdImageArc(image,qq[0],qq[1],rx,ry,res[3]/DEG,res[4]/DEG,pm->color[0]);
  693.     }
  694.     else {
  695.       scale(pm->pd+i,qq,2);
  696.       gdImageLine(image,qq[0],qq[1],qq[2],qq[3],pm->color[0]);
  697.     }
  698. }
  699. /* hyperbolique triangle, can be filled */
  700. void obj_hyptriangle(objparm *pm)
  701. {
  702.   double rx,ry,data[8],res[7];
  703.   int i, *qq=pm->p;
  704.   for(i=0; i<8; i++) data[i]=pm->pd[i%6];
  705.   for(i=0; i<3; i++)
  706.     if(hypgeodaux(data+2*i,res)) {
  707.       scale2(res[2],res[2],&rx,&ry);
  708.       scale(res,qq,1);
  709.       myGdImageArc(image,qq[0],qq[1],rx,ry,res[3]/DEG,res[4]/DEG,pm->color[0]);
  710.     }
  711.     else {
  712.       scale(data+2*i,qq,2);
  713.       gdImageLine(image,qq[0],qq[1],qq[2],qq[3],pm->color[0]);
  714.     }
  715.   if(pm->fill){
  716.     data[0]=res[5];
  717.     data[1]=res[6];
  718.     hypgeodaux(data,res);
  719.     scale(res+5,qq,1);
  720.     patchgdImageFill(image,qq[0],qq[1],pm->color[0]);
  721.   }
  722. }
  723. /* complete hyperbolic geodesic through two points */
  724. void obj_hyplines(objparm *pm)
  725. {
  726.   double rx,ry,res[7],*pd = pm->pd;
  727.   int i;
  728.   for (i=0; i < pm->pcnt; i+=4,pd+=4)
  729.     if (hypgeodaux(pd,res)) {
  730.       double alpha=atan(1/res[2]), beta = atan2(res[1],res[0]);
  731.       scale(res,pm->p,1);
  732.       scale2(res[2],res[2],&rx,&ry);
  733.       myGdImageArc(image,pm->p[0],pm->p[1],rx,ry,180+(beta-alpha)/DEG,180+(beta+alpha)/DEG,pm->color[0]);
  734.     }
  735.     else {
  736.       double gamma;
  737.       if (pd[1]*pd[1]+pd[0]*pd[0] > pd[2]*pd[2]+pd[3]*pd[3])
  738.         gamma = atan2(pd[1],pd[0]);
  739.       else
  740.         gamma = atan2(pd[3],pd[2]);
  741.       res[0]=cos(gamma);
  742.       res[1]=sin(gamma);
  743.       res[2]=-cos(gamma);
  744.       res[3]=-sin(gamma);
  745.       scale(res,pm->p,2);
  746.       gdImageLine(image,pm->p[0],pm->p[1],pm->p[2],pm->p[3],pm->color[0]);
  747.     }
  748. }
  749. /* segments */
  750. void obj_dlines(objparm *pm)
  751. {
  752.  int i, n;
  753.  n=(pm->pcnt)/2;
  754.  scale(pm->pd,pm->p,n);
  755.  if (tikz_file){
  756.    fprintf(tikz_file,"\\draw\[%s] (%i,%i)",
  757.      tikz_options(pm->color[0],pm->fill),pm->p[0],flip(pm->p[1]));
  758.    for(i=2;i<2*n;i+=2)
  759.      fprintf(tikz_file, "--(%i,%i)",pm->p[i],flip(pm->p[i+1]));
  760.    fprintf(tikz_file, ";\n");
  761.  }
  762.  for(i=2;i<2*n;i+=2)
  763.    myDashedLine(image,pm->p[i-2],pm->p[i-1],pm->p[i],pm->p[i+1],pm->color[0]);
  764.  if(vimg_enable) vimg_polyline(scale_buf,n,0);
  765. }
  766.  
  767. /* points */
  768. void obj_points(objparm *pm)
  769. {
  770.  int i, n;
  771.  n=(pm->pcnt)/2;
  772.  scale(pm->pd,pm->p,n);
  773.  for(i=0;i<2*n;i+=2) gdImageSetPixel(image,pm->p[i],pm->p[i+1],pm->color[0]);
  774.  if(tikz_file) {
  775.    fprintf(tikz_file, "\\draw\[%s]", tikz_options(pm->color[0],pm->fill));
  776.    for(i=0;i<2*n;i+=2)
  777.      fprintf(tikz_file,"(%i,%i)circle(1pt)",pm->p[i],flip(pm->p[i+1]));
  778.    fprintf(tikz_file,";\n");
  779.  }
  780. }
  781.  
  782. /* lattice.
  783.  * x0,y0,xv1,yv1,xv2,yv2,n1,n2,color */
  784. void obj_lattice(objparm *pm)
  785. {
  786.  int n1,n2,i1,i2,xi1,yi1,xi2,yi2;
  787.  double xv1,xv2,yv1,yv2;
  788.  n1=pm->pd[6];n2=pm->pd[7]; if(n1<0 || n2<0) return;
  789.  if(n1>256) n1=256;
  790.  if(n2>256) n2=256;
  791.  scale(pm->pd,pm->p,1);
  792.  scale2(pm->pd[2],pm->pd[3],&xv1,&yv1);
  793.  scale2(pm->pd[4],pm->pd[5],&xv2,&yv2);
  794.  if(tikz_file)
  795.    fprintf(tikz_file,"\\draw[%s]", tikz_options(pm->color[0],0));
  796.  for(i1=0;i1<n1;i1++) {
  797.    xi1=rint(i1*xv1)+pm->p[0]; yi1=rint(i1*yv1)+pm->p[1];
  798.    for(i2=0;i2<n2;i2++) {
  799.      xi2=i2*xv2+xi1;yi2=i2*yv2+yi1;
  800.      gdImageSetPixel(image,xi2,yi2,pm->color[0]);
  801.      if(tikz_file) fprintf(tikz_file,"(%i,%i)circle(1pt)",xi2,flip(yi2));
  802.    }
  803.  }
  804.  if(tikz_file) fprintf(tikz_file,";\n");
  805. }
  806.  
  807. /* arc */
  808. /*trouble with gdImageArc as the angles are of type int */
  809. void myGdImageArc(gdImagePtr im, double cx, double cy, double rx, double ry, double t1, double t2, int color)
  810. {
  811.   int i,nb;
  812.   double dt, r=rx>ry?rx:ry;
  813.   t2-=t1;
  814.   t2-=360*floor(t2/360);
  815.   if(t2==0) t2=360;
  816.   t1*=DEG;t2*=DEG;
  817.   nb = r*t2/3;
  818.   dt = t2/nb;
  819.   for (i=0; i<nb; ++i,t1+=dt)
  820.     gdImageLine(im,rint(cx+rx*cos(t1)),rint(cy+ry*sin(t1)),
  821.     rint(cx+rx*cos(t1+dt)),rint(cy+ry*sin(t1+dt)),color);
  822. }
  823.  
  824. void obj_arc(objparm *pm)
  825. {
  826.   scale(pm->pd,pm->p,1);
  827.   int rx=pm->pd[2]*xscale/2, ry=pm->pd[3]*yscale/2;
  828.   myGdImageArc(image, pm->p[0], pm->p[1], rx, ry, pm->pd[4],pm->pd[5],pm->color[0]);
  829.   if(tikz_file){
  830.     pm->pd[5]-=pm->pd[4];
  831.     pm->pd[5]-=360*floor(pm->pd[5]/360);
  832.     pm->pd[5]+=pm->pd[4];
  833.     fprintf(tikz_file,
  834. /*    "\\draw\[%s, domain=%f:%f] plot ({%i+%f*cos(\\x)}, {%i+%f*sin(\\x)});\n",*/
  835.       "\\draw \[%s] (%i,%i) arc (%f:%f:%i and %i);\n",
  836.         tikz_options(pm->color[0],pm->fill),
  837.         (int)rint(pm->p[0]+rx*cos(DEG*pm->pd[4])),
  838.         flip((int)rint(pm->p[1]+ry*sin(DEG*pm->pd[4]))),
  839.         pm->pd[4],pm->pd[5],rx,-ry);
  840.   }
  841.   /*FIXME echelle mauvaise*/
  842.   if(vimg_enable) vimg_arc(scale_buf[0],scale_buf[1],
  843.                      0.5*pm->pd[2],0.5*pm->pd[3],pm->pd[4],pm->pd[5]);
  844. }
  845.  
  846. void obj_angle(objparm *pm)
  847. {
  848. /* dans la doc: mettre les angles dans l'ordre ???? demander qu'ils soient entre 0 et 360 */
  849.   double dpts[6];
  850.   int pts[6], wx=rint(2*pm->pd[2]*xscale), wy=rint(2*pm->pd[2]*yscale);
  851.   dpts[0]=pm->pd[0];
  852.   dpts[1]=pm->pd[1];
  853.   dpts[2]=pm->pd[0]+pm->pd[2]*cos(DEG*pm->pd[3]);
  854.   dpts[3]=pm->pd[1]+pm->pd[2]*sin(DEG*pm->pd[3]);
  855.   dpts[4]=pm->pd[0]+pm->pd[2]*cos(DEG*pm->pd[4]);
  856.   dpts[5]=pm->pd[1]+pm->pd[2]*sin(DEG*pm->pd[4]);
  857.   scale(dpts, pts, 3);
  858.   gdImageLine(image,pts[0],pts[1],pts[2],pts[3],pm->color[0]);
  859.   gdImageLine(image,pts[0],pts[1],pts[4],pts[5],pm->color[0]);
  860.   gdImageArc(image,pts[0],pts[1],wx,wy,pm->pd[3],pm->pd[4],pm->color[0]);
  861.   if(tikz_file) {
  862.     fprintf(tikz_file, "\\draw\[%s, domain=%f:%f] plot ({%i+%f*cos(\\x)}, {%i+%f*sin(\\x)});\n",
  863.       tikz_options(pm->color[0],pm->fill),pm->pd[3],pm->pd[4],pts[0],wx*0.5,flip(pts[1]),wy*(-0.5));
  864.     fprintf(tikz_file, "\\draw\[%s] (%i, %i) -- (%i, %i) (%i, %i) -- (%i, %i);\n",
  865.         tikz_options(pm->color[0],pm->fill),pts[0],flip(pts[1]),pts[2],flip(pts[3]),pts[0],flip(pts[1]),pts[4],flip(pts[5]));
  866.   }
  867. }
  868. /* Ellipse: centre 0,1, width 2, hight 3, color 4,5,6 */
  869. void obj_ellipse(objparm *pm)
  870. {
  871.   scale(pm->pd,pm->p,1);
  872.   pm->p[2]=pm->pd[2]*xscale; pm->p[3]=pm->pd[3]*yscale;
  873.   if(pm->fill==1 || pm->fill==2) {
  874.     gdImageArc(image,pm->p[0],pm->p[1],pm->p[2],pm->p[3],0,360,
  875.              color_bounder);
  876.     patchgdImageFillToBorder(image,pm->p[0],pm->p[1],
  877.                     color_bounder,pm->color[0]);
  878.   }
  879.   gdImageArc(image,pm->p[0],pm->p[1],rint(pm->p[2]),rint(pm->p[3]),0,360,pm->color[0]);
  880.   if(tikz_file) fprintf(tikz_file,"\\draw\[%s] (%i,%i) ellipse (%i and %i);\n\n",
  881.     tikz_options(pm->color[0],pm->fill),pm->p[0],flip(pm->p[1]),(int) rint(0.5*pm->p[2]),(int) rint(0.5*pm->p[3]));
  882.   if(vimg_enable) vimg_ellipse(scale_buf[0],scale_buf[1],0.5*pm->pd[2],0.5*pm->pd[3]);
  883. }
  884.  
  885. /* Circle radius in pixels*/
  886. void obj_circle(objparm *pm)
  887. {
  888.   scale(pm->pd,pm->p,1);
  889.   pm->p[2]=rint(pm->pd[2]); pm->p[3]=rint(pm->pd[2]);
  890.   if(pm->fill==1 || pm->fill==2) {
  891.     gdImageArc(image,pm->p[0],pm->p[1],pm->p[2],pm->p[3],0,360,
  892.              color_bounder);
  893.     patchgdImageFillToBorder(image,pm->p[0],pm->p[1],
  894.                     color_bounder,pm->color[0]);
  895.   }
  896.   gdImageArc(image,pm->p[0],pm->p[1],pm->p[2],pm->p[3],0,360,pm->color[0]);
  897.   if(tikz_file) fprintf(tikz_file, "\\draw\[%s] (%i, %i) circle (%i);\n",
  898.       tikz_options(pm->color[0],pm->fill),pm->p[0],flip(pm->p[1]),(int) rint(pm->pd[2]/2.));
  899. }
  900. /* Circle, radius in xrange */
  901. void obj_circles(objparm *pm)
  902. {
  903.   int i, n;
  904.   n=(pm->pcnt)/3;
  905.   for (i=0; i<n; ++i) scale(pm->pd+3*i, pm->p+3*i, 1);
  906.   if (tikz_file) fprintf(tikz_file,"\\draw\[%s]",tikz_options(pm->color[0],pm->fill));
  907.   for(i=0;i<3*n;i+=3){
  908.     if(pm->fill==1 || pm->fill==2) {
  909.       gdImageArc(image,pm->p[i],pm->p[i+1],
  910.         rint(2*pm->pd[i+2]*xscale),rint(2*pm->pd[i+2]*xscale),0,360,
  911.           color_bounder);
  912.       patchgdImageFillToBorder(image,pm->p[i],pm->p[i+1],
  913.                     color_bounder,pm->color[0]);
  914.     }
  915.     gdImageArc(image,pm->p[i],pm->p[i+1],
  916.         rint(2*pm->pd[i+2]*xscale),rint(2*pm->pd[i+2]*xscale),0,360,pm->color[0]);
  917.     if (tikz_file){
  918.       fprintf(tikz_file, "(%i, %i) circle (%i and %i)",
  919.         pm->p[i],flip(pm->p[i+1]),(int) rint(pm->pd[i+2]*xscale),(int) rint(pm->pd[i+2]*xscale));
  920.     }
  921.   }
  922.   if (tikz_file) fprintf(tikz_file,";\n");
  923. }
  924. /* ellipse, width in xrange,height in yrange */
  925. void obj_ellipses(objparm *pm)
  926. {
  927.   int i, n;
  928.   n=(pm->pcnt)/4;
  929.   for (i=0; i<n; ++i) scale(pm->pd+4*i, pm->p+4*i, 1);
  930.   if (tikz_file) fprintf(tikz_file,"\\draw\[%s]",tikz_options(pm->color[0],pm->fill));
  931.   for(i=0;i<4*n;i+=4){
  932.     if(pm->fill==1 || pm->fill==2){
  933.       gdImageArc(image,pm->p[i],pm->p[i+1],
  934.         rint(pm->pd[i+2]*xscale),rint(pm->pd[i+2]*xscale),0,360,
  935.         color_bounder);
  936.       patchgdImageFillToBorder(image,pm->p[i],pm->p[i+1],
  937.         color_bounder,pm->color[0]);
  938.     } else
  939.       gdImageArc(image,pm->p[i],pm->p[i+1],rint(pm->pd[i+2]*xscale),rint(pm->pd[i+3]*yscale),0,360,pm->color[0]);
  940.     if (tikz_file){
  941.       fprintf(tikz_file, "(%i, %i) circle (%i and %i)",
  942.         pm->p[i],flip(pm->p[i+1]),(int) rint(pm->pd[i+2]*xscale/2),(int) rint(pm->pd[i+3]*yscale/2));
  943.     }
  944.   }
  945.   if (tikz_file) fprintf(tikz_file,";\n");
  946. }
  947. typedef enum tikz_fill_options {grid, dot, hatch, diamond} tfo;
  948. int compar(const void *a, const void *b) {return *(int *)b - *(int *)a;}
  949.  
  950. static void tikz_fill(int x, int y, int nx, int ny, int wall, int the_color, tfo opt)
  951. {
  952.   int numpix=sizex*sizey, top = 0, a, c, cy, lx=0, rx=0, seed, nt,ct=0;
  953.   int *stack = xmalloc(numpix*sizeof(int));
  954.   int *stack2 = xmalloc(numpix*sizeof(int));
  955.   if (nx==0) nx=1;
  956.   if (ny==0) ny=1;
  957.   nt = abs(nx*ny);
  958.   seed = gdImageGetPixel(image,x,y);
  959.   char *check = xmalloc(numpix);
  960.   while (numpix--) check[numpix]=0;
  961.   a=x+y*sizex;
  962.   check[a]=1;
  963.   stack[top++]=a;
  964.   while(top)
  965.     {
  966.       a = stack[--top];
  967.       x = a%sizex; y = a/sizex;
  968.       c = gdImageGetPixel(image,x,y);
  969.       if ((wall && (c == wall))||(!wall && c!=seed))
  970.         continue;
  971.       if (x>0 && !check[a-1]) {check[a-1]=1; stack[top++]=a-1;}
  972.       if (x+1<sizex && !check[a+1]) {check[a+1]=1; stack[top++]=a+1;}
  973.       if (y>0 && !check[a-sizex]) {check[a-sizex]=1; stack[top++]=a-sizex;}
  974.       if (y+1<sizey && !check[a+sizex]) {check[a+sizex]=1; stack[top++]=a+sizex;}
  975.       switch(opt)
  976.   {
  977.   case grid: if(((x%nx)!=(nx/2))&&((y%ny)!=(ny/2))) continue; break;
  978.   case dot: if(x%nx!=nx/2||y%ny!=ny/2) continue; break;
  979.   case hatch: if((ny*x-nx*y+nt)%nt) continue; break;
  980.   case diamond: if((ny*x+nx*y)%nt && (ny*x-nx*y+nt)%nt)continue;
  981.   default: break;
  982.   }
  983.       stack2[ct++]=a;
  984.     }
  985.   free(stack);
  986.   free(check);
  987.   if (ct==0) return;
  988.   fprintf(tikz_file,"\\draw\[%s]", tikz_options(the_color,1));
  989.   qsort(stack2, ct, sizeof(int), compar);
  990.   cy=-1;
  991.   while (ct--)
  992.     {
  993.       a = stack2[ct];
  994.       x = a%sizex; y = a/sizex;
  995.       if (y != cy || x != rx+1)
  996.       {
  997.   if (cy >= 0)
  998.     fprintf(tikz_file,
  999.       "(%.2f,%.2f)rectangle(%.2f,%.2f)",
  1000.       lx-0.0,flip(cy-0.0),rx+0.0,flip(cy+0.0));
  1001.   cy = y;
  1002.   lx = rx = x;
  1003.       }
  1004.       else
  1005.   rx++;
  1006.     }
  1007.   fprintf(tikz_file,
  1008.     "(%.2f,%.2f)rectangle(%.2f,%.2f);\n",
  1009.      lx-0.0,flip(cy-0.0),rx+0.0,flip(cy+0.0));
  1010.   free(stack2);
  1011. }
  1012. void obj_dashed(objparm *pm){ dashed=1;}
  1013. void obj_filled(objparm *pm){ filled=1;}
  1014. void obj_noreset(objparm *pm){ noreset=1;}
  1015. void obj_reset(objparm *pm){ noreset=0;dashed=0;filled=0;}
  1016.  
  1017. /* flood fill */
  1018. void obj_fill(objparm *pm)
  1019. {
  1020.   scale(pm->pd,pm->p,1);
  1021.   if (tikz_file) tikz_fill(pm->p[0],pm->p[1],1,1,0,pm->color[0],grid);
  1022.   patchgdImageFill(image,pm->p[0],pm->p[1],pm->color[0]);
  1023. }
  1024.  
  1025. /* flood fill to border*/
  1026. void obj_fillb(objparm *pm)
  1027. {
  1028.   scale(pm->pd,pm->p,1);
  1029.   if (tikz_file) tikz_fill(pm->p[0],pm->p[1],1,1,pm->color[0],pm->color[1],grid);
  1030.   patchgdImageFillToBorder(image,pm->p[0],pm->p[1],pm->color[0],pm->color[1]);
  1031. }
  1032.  
  1033. gdImagePtr himg;
  1034.  
  1035. int makehatchimage(int x, int y, int px, int py, int col)
  1036. {
  1037.   int c1,c2,r,g,b;
  1038.   gdImagePtr saveimg;
  1039.   himg=gdImageCreate(x,y);
  1040.   c1=gdImageGetPixel(image,px,py);
  1041.   r=gdImageRed(image,c1); g=gdImageGreen(image,c1); b=gdImageBlue(image,c1);
  1042.   if(r>=255) r--; else r++; if(g>=255) g--; else g++; if(b>=255) b--; else b++;
  1043.   c1=gdImageColorAllocate(himg,r,g,b);
  1044.   r=gdImageRed(image,col); g=gdImageGreen(image,col); b=gdImageBlue(image,col);
  1045.   c2=gdImageColorAllocate(himg,r,g,b);
  1046.   if(width>1) {
  1047.     savew=-1; saveimg=image;
  1048.     image=himg; c2=widthcolor(width,c2); image=saveimg;
  1049.     c2=gdBrushed; savew=-1;
  1050.   }
  1051.   return c2;
  1052. }
  1053.  
  1054. /* flood fill with hatching */
  1055. void obj_hatchfill(objparm *pm)
  1056. {
  1057.   int nx,ny,ax,ay, dir, c;
  1058.   scale(pm->pd,pm->p,1);
  1059.   nx=pm->pd[2]; ny=pm->pd[3]; ax=abs(nx); ay=abs(ny);
  1060.   if(nx==0 && ny==0) {fly_error("bad displacement vector"); return;}
  1061.   if((nx>0 && ny>0) || (nx<0 && ny<0)) dir=1; else dir=-1;
  1062.   if(ax==0) {ax=100; dir=2;}
  1063.   if(ay==0) {ay=100; dir=3;}
  1064.   c=makehatchimage(ax,ay,pm->p[0],pm->p[1],pm->color[0]);
  1065.   switch(dir) {
  1066.     case -1: {
  1067.       gdImageLine(himg,0,ay-1,ax-1,0,c);
  1068.       if(width>1) {
  1069.         gdImageLine(himg,-ax,ay-1,-1,0,c);
  1070.         gdImageLine(himg,ax,ay-1,2*ax-1,0,c);
  1071.         gdImageLine(himg,0,-1,ax-1,-ay,c);
  1072.         gdImageLine(himg,0,2*ay-1,ax-1,ay,c);
  1073.       }
  1074.       break;
  1075.     }
  1076.     case 1: {
  1077.       gdImageLine(himg,0,0,ax-1,ay-1,c);
  1078.       if(width>1) {
  1079.         gdImageLine(himg,-ax,0,-1,ay-1,c);
  1080.         gdImageLine(himg,ax,0,2*ax-1,ay-1,c);
  1081.         gdImageLine(himg,0,-ay,ax-1,-1,c);
  1082.         gdImageLine(himg,0,ay,ax-1,2*ay-1,c);
  1083.       }
  1084.       break;
  1085.     }
  1086.     case 2: gdImageLine(himg,0,ay/2,ax-1,ay/2,c); break;
  1087.     case 3: gdImageLine(himg,ax/2,0,ax/2,ay-1,c); break;
  1088.   }
  1089.   if (tikz_file) tikz_fill(pm->p[0],pm->p[1],nx,ny,0,pm->color[0],hatch);
  1090.   gdImageSetTile(image,himg);
  1091.   patchgdImageFill(image,pm->p[0],pm->p[1],gdTiled);
  1092.   gdImageDestroy(himg);
  1093.   if(tiled) gdImageSetTile(image,tileimg);
  1094. }
  1095.  
  1096. /* flood fill with grid */
  1097. void obj_gridfill(objparm *pm)
  1098. {
  1099.   int nx,ny, c;
  1100.   scale(pm->pd,pm->p,1);
  1101.   nx=pm->pd[2]; ny=pm->pd[3]; nx=abs(nx); ny=abs(ny);
  1102.   if(nx==0 && ny==0) {fly_error("bad grid size"); return;}
  1103.   c=makehatchimage(nx,ny,pm->p[0],pm->p[1],pm->color[0]);
  1104.   if (tikz_file) tikz_fill(pm->p[0],pm->p[1],nx,ny,0,pm->color[0],grid);
  1105.   gdImageLine(himg,0,ny/2,nx-1,ny/2,c); gdImageLine(himg,nx/2,0,nx/2,ny-1,c);
  1106.   gdImageSetTile(image,himg);
  1107.   patchgdImageFill(image,pm->p[0],pm->p[1],gdTiled);
  1108.   gdImageDestroy(himg);
  1109.   if(tiled) gdImageSetTile(image,tileimg);
  1110. }
  1111.  
  1112. /* flood fill with double hatching */
  1113. void obj_diafill(objparm *pm)
  1114. {
  1115.   int nx,ny, c;
  1116.   scale(pm->pd,pm->p,1);
  1117.   nx=pm->pd[2]; ny=pm->pd[3]; nx=abs(nx); ny=abs(ny);
  1118.   if(nx==0 && ny==0) {fly_error("bad grid size"); return;}
  1119.   c=makehatchimage(nx,ny,pm->p[0],pm->p[1],pm->color[0]);
  1120.   if (tikz_file) tikz_fill(pm->p[0],pm->p[1],nx,ny,0,pm->color[0],diamond);
  1121.   gdImageLine(himg,0,0,nx-1,ny-1,c); gdImageLine(himg,0,ny-1,nx-1,0,c);
  1122.   gdImageSetTile(image,himg);
  1123.   patchgdImageFill(image,pm->p[0],pm->p[1],gdTiled);
  1124.   gdImageDestroy(himg);
  1125.   if(tiled) gdImageSetTile(image,tileimg);
  1126. }
  1127.  
  1128. /* flood fill with dots */
  1129. void obj_dotfill(objparm *pm)
  1130. {
  1131.   int nx,ny, c;
  1132.   scale(pm->pd,pm->p,1);
  1133.   nx=pm->pd[2]; ny=pm->pd[3]; nx=abs(nx); ny=abs(ny);
  1134.   if(nx==0 && ny==0) {fly_error("bad grid size"); return;}
  1135.   c=makehatchimage(nx,ny,pm->p[0],pm->p[1],pm->color[0]);
  1136.   if (tikz_file) tikz_fill(pm->p[0],pm->p[1],nx,ny,0,pm->color[0],dot);
  1137.   gdImageSetPixel(himg,nx/2,ny/2,c);
  1138.   gdImageSetTile(image,himg);
  1139.   patchgdImageFill(image,pm->p[0],pm->p[1],gdTiled);
  1140.   gdImageDestroy(himg);
  1141.   if(tiled) gdImageSetTile(image,tileimg);
  1142. }
  1143.  
  1144. struct {
  1145.   char *name;
  1146.   gdFontPtr *fpt;
  1147.   char *ftikz;
  1148. } fonttab[]={
  1149.   {"tiny",  &gdFontTiny,"0.3"},
  1150.   {"small", &gdFontSmall,"0.5"},
  1151.   {"medium",&gdFontMediumBold,"0.7"},
  1152.   {"large", &gdFontLarge,"0.9"},
  1153.   {"giant", &gdFontGiant,"1"},
  1154.   {"huge",  &gdFontGiant,"1"}
  1155. };
  1156.  
  1157. #define fonttab_no (sizeof(fonttab)/sizeof(fonttab[0]))
  1158.  
  1159. /* string */
  1160. void obj_string(objparm *pm)
  1161. {
  1162.   char *pp, *pe, *p2;
  1163.   int i;
  1164.   pp=pm->str; pe=strchr(pp,','); if(pe==NULL) {
  1165.     fly_error("too_few_parms"); return;
  1166.   }
  1167.   *pe++=0; pp=find_word_start(pp); *find_word_end(pp)=0;
  1168.   pe=find_word_start(pe); strip_trailing_spaces(pe);
  1169.   if(*pp) {
  1170.     for(i=0;i<fonttab_no && strcmp(pp,fonttab[i].name)!=0; i++);
  1171.     if(i>=fonttab_no) i=1;
  1172.   }
  1173.   else i=1;
  1174.   scale(pm->pd,pm->p,1);
  1175.   if(*pe=='"') {
  1176.     p2=strchr(pe+1,'"');
  1177.     if(p2 && *(p2+1)==0) {*p2=0; pe++;}
  1178.   }
  1179.   if(pm->fill){
  1180.     gdImageStringUp(image,*(fonttab[i].fpt),pm->p[0],pm->p[1], (unsigned char*) pe,
  1181.         pm->color[0]);
  1182.     if(tikz_file) fprintf(tikz_file,"\\draw (%i,%i) node[scale=%s,rotate=90,color=%s,anchor=north west,inner sep=0pt] {\\(%s\\)};\n",
  1183.       pm->p[0],flip(pm->p[1]),fonttab[i].ftikz,tikz_color(pm->color[0]),(unsigned char*) pe);
  1184.   }
  1185.   else {
  1186.     gdImageString(image,*(fonttab[i].fpt),pm->p[0],pm->p[1], (unsigned char*) pe,
  1187.         pm->color[0]);
  1188.     if(tikz_file) fprintf(tikz_file,"\\draw (%i,%i) node[scale=%s,color=%s,anchor=north west,inner sep=0pt] {\\(%s\\)};\n",
  1189.       pm->p[0],flip(pm->p[1]),fonttab[i].ftikz,tikz_color(pm->color[0]),(unsigned char*) pe);
  1190.   }
  1191. }
  1192. /*FIXME; giant does not exist in tikz and ... samething as huge */
  1193. /* point */
  1194. void obj_point(objparm *pm)
  1195. {
  1196.   scale(pm->pd,pm->p,1);
  1197.   gdImageSetPixel(image,pm->p[0],pm->p[1],pm->color[0]);
  1198.   if(tikz_file) fprintf(tikz_file,"\\draw[%s] (%i,%i) circle (1pt);\n",
  1199.     tikz_options(pm->color[0],0),pm->p[0],flip(pm->p[1]));
  1200. }
  1201.  
  1202. /* copy an image file */
  1203. void obj_copy(objparm *pm)
  1204. {
  1205.   char *pp;
  1206.   FILE *inf;
  1207.   gdImagePtr insimg;
  1208.  
  1209.   pp=find_word_start(pm->str);*find_word_end(pp)=0;
  1210.   inf=open4read(pp);
  1211.   if(inf==NULL) {
  1212.     fly_error("file_does_not_exist"); return;
  1213.   }
  1214.   insimg=gdImageCreateFromGif(inf); fclose(inf);
  1215.   if(insimg==NULL) {
  1216.     fly_error("bad_gif"); return;
  1217.   }
  1218.   scale(pm->pd,pm->p,1);
  1219.   if(pm->pd[2]<0 && pm->pd[3]<0 && pm->pd[4]<0 && pm->pd[5]<0)
  1220.     gdImageCopy(image,insimg,pm->p[0],pm->p[1],0,0,
  1221.             insimg->sx,insimg->sy);
  1222.   else
  1223.     gdImageCopy(image,insimg,pm->p[0],pm->p[1],pm->pd[2],pm->pd[3],
  1224.             pm->pd[4]-pm->pd[2],pm->pd[5]-pm->pd[3]);
  1225.   if(tikz_file) fprintf(tikz_file,
  1226.     "\\node[anchor=north west,inner sep=0pt] at (%i,%i) {\\includegraphics\[width=.05\\linewidth]{%s}};\n",
  1227.       pm->p[0],flip(pm->p[1]),pm->str);
  1228.     gdImageDestroy(insimg);
  1229. /*FIXME position non correcte; on ne peut pas utiliser une image gif */
  1230. }
  1231.  
  1232. /* copy an image file, with resizing */
  1233. void obj_copyresize(objparm *pm)
  1234. {
  1235.   char *pp;
  1236.   FILE *inf;
  1237.   gdImagePtr insimg;
  1238.  
  1239.   pp=find_word_start(pm->str);*find_word_end(pp)=0;
  1240.   inf=open4read(pp);
  1241.   if(inf==NULL) {
  1242.     fly_error("file_not_found"); return;
  1243.   }
  1244.   insimg=gdImageCreateFromGif(inf); fclose(inf);
  1245.   if(insimg==NULL) {
  1246.     fly_error("bad_gif"); return;
  1247.   }
  1248.   scale(pm->pd+4,pm->p+4,2);
  1249.   if(pm->pd[0]<0 && pm->pd[1]<0 && pm->pd[2]<0 && pm->pd[3]<0)
  1250.     gdImageCopyResized(image,insimg,pm->p[4],pm->p[5],0,0,
  1251.                  pm->p[6]-pm->p[4]+1,pm->p[7]-pm->p[5]+1,
  1252.                  insimg->sx,insimg->sy);
  1253.   else
  1254.     gdImageCopyResized(image,insimg,pm->p[4],pm->p[5],pm->pd[0],pm->pd[1],
  1255.                  pm->p[6]-pm->p[4]+1,pm->p[7]-pm->p[5]+1,
  1256.                  pm->pd[2]-pm->pd[0]+1,pm->pd[3]-pm->pd[1]+1);
  1257.   gdImageDestroy(insimg);
  1258. }
  1259.  
  1260. /* set brush or tile */
  1261. void obj_setbrush(objparm *pm)
  1262. {
  1263.   char *pp;
  1264.   FILE *inf;
  1265.   gdImagePtr insimg;
  1266.  
  1267.   pp=find_word_start(pm->str); *find_word_end(pp)=0;
  1268.   inf=open4read(pp); if(inf==NULL) {
  1269.     fly_error("file_not_found"); return;
  1270.   }
  1271.   insimg=gdImageCreateFromGif(inf); fclose(inf);
  1272.   if(insimg==NULL) {
  1273.     fly_error("bad_gif"); return;
  1274.   }
  1275.   if(pm->fill) {
  1276.     gdImageSetTile(image,insimg); tiled=1; tileimg=insimg;
  1277.   }
  1278.   else {
  1279.     gdImageSetBrush(image,insimg);brushed=1; brushimg=insimg;
  1280.   }
  1281. }
  1282.  
  1283. /* kill brush */
  1284. void obj_killbrush(objparm *pm)
  1285. {
  1286.   if(brushimg) gdImageDestroy(brushimg);
  1287.   brushed=0; brushimg=NULL;
  1288. }
  1289.  
  1290. /* kill tile */
  1291. void obj_killtile(objparm *pm)
  1292. {
  1293.   if(tileimg) gdImageDestroy(tileimg);
  1294.   tiled=0; tileimg=NULL;
  1295. }
  1296.  
  1297. /* set style */
  1298. void obj_setstyle(objparm *pm)
  1299. {
  1300.   int i,t;
  1301.   t=pm->pcnt/3; if(t<=0) {
  1302.     fly_error("too_few_parms"); return;
  1303.   }
  1304.   for(i=0;i<t;i++) {
  1305.     if(pm->pd[3*i]<0 || pm->pd[3*i+1]<0 || pm->pd[3*i+2]<0)
  1306.       pm->p[i]=gdTransparent;
  1307.     else
  1308.       pm->p[i]=getcolor(pm->pd[3*i],pm->pd[3*i+1],pm->pd[3*i+2]);
  1309.   }
  1310.   gdImageSetStyle(image,pm->p,t); styled=1;
  1311. }
  1312.  
  1313. /* kill style */
  1314. void obj_killstyle(objparm *pm)
  1315. {
  1316.   styled=0;
  1317. }
  1318.  
  1319. /* set transparent */
  1320. void obj_transp(objparm *pm)
  1321. {
  1322.   gdImageColorTransparent(image,pm->color[0]);
  1323. }
  1324.  
  1325. /* set interlace */
  1326. void obj_interlace(objparm *pm)
  1327. {
  1328.   gdImageInterlace(image,1);
  1329. }
  1330.  
  1331. /* set linewidth */
  1332. void obj_linewidth(objparm *pm)
  1333. {
  1334.   if(pm->pd[0]<1 || pm->pd[0]>255) fly_error("bad_parms");
  1335.   width=pm->pd[0];
  1336.   if(tikz_file) fprintf(tikz_file,"\\pgfsetlinewidth{%fpt}\n",width*0.7);
  1337. }
  1338.  
  1339. /* set crosshairsize */
  1340. void obj_crosshairsize(objparm *pm)
  1341. {
  1342.   if(pm->pd[0]<1 || pm->pd[0]>255) fly_error("bad_parms");
  1343.   else width2=pm->pd[0];
  1344. }
  1345.  
  1346. /* set x range */
  1347. void obj_xrange(objparm *pm)
  1348. {
  1349.   double dd;
  1350.   dd=pm->pd[1]-pm->pd[0];
  1351.   xstart=pm->pd[0]; xscale=sizex/dd;
  1352. }
  1353.  
  1354. /* set y range */
  1355. void obj_yrange(objparm *pm)
  1356. {
  1357.   double dd;
  1358.   dd=pm->pd[1]-pm->pd[0];
  1359.   ystart=pm->pd[1]; yscale=-sizey/dd;
  1360. }
  1361.  
  1362. /* set x et y range */
  1363. void obj_range(objparm *pm)
  1364. {
  1365.   double d1,d2;
  1366.   d1=pm->pd[1]-pm->pd[0]; d2=pm->pd[3]-pm->pd[2];
  1367.   xstart=pm->pd[0]; xscale=(sizex-1)/d1;
  1368.   ystart=pm->pd[3]; yscale=-(sizey-1)/d2;
  1369. }
  1370.  
  1371. /* set t range */
  1372. void obj_trange(objparm *pm)
  1373. {
  1374.   /*double dd;
  1375.   dd=pm->pd[1]-pm->pd[0];*/
  1376.   tstart=pm->pd[0]; tend=pm->pd[1];
  1377.   tranged=1;
  1378. }
  1379.  
  1380. /* set tstep (plotting step) */
  1381. void obj_tstep(objparm *pm)
  1382. {
  1383.   int dd;
  1384.   dd=pm->pd[0];
  1385.   if(dd<3) {
  1386.     fly_error("bad_step"); return;
  1387.   }
  1388.   if(dd>2000) dd=2000;
  1389.   tstep=dd;
  1390. }
  1391.  
  1392. /* set plotjump (plot jump break threashold) */
  1393. void obj_plotjump(objparm *pm)
  1394. {
  1395.   int dd;
  1396.   dd=pm->pd[0];
  1397.   if(dd<3) dd=3;
  1398.   if(dd>MAX_SIZE) dd=MAX_SIZE;
  1399.   plotjump=dd;
  1400. }
  1401.  
  1402. /* plot a curve, either parametric or explicit */
  1403. void _obj_plot(objparm *pm,int dash)
  1404. {
  1405.   int i,j,n,dist,xx,yy,varpos;
  1406.   char p1[MAX_LINELEN+1], p2[MAX_LINELEN+1];
  1407.   char *varn, *pp;
  1408.   double dc[2],t,v;
  1409.   int ic[2],oc[2];
  1410.  
  1411.   n=itemnum(pm->str);
  1412.   if(n<1) {
  1413.     fly_error("bad_parms"); return;
  1414.   }
  1415.   fnd_item(pm->str,1,p1); fnd_item(pm->str,2,p2);
  1416.   if(n==1 && !tranged) v=sizex/xscale/tstep;
  1417.   else v=(tend-tstart)/tstep;
  1418.   if(n==1) varn="x"; else varn="t";
  1419.   for(pp=varchr(p1,varn); pp; pp=varchr(pp,varn)) {
  1420.     string_modify(p1,pp,pp+strlen(varn),EV_T);
  1421.     pp+=strlen(EV_T);
  1422.   }
  1423.   for(pp=varchr(p2,varn); pp; pp=varchr(pp,varn)) {
  1424.     string_modify(p2,pp,pp+strlen(varn),EV_T);
  1425.       pp+=strlen(EV_T);
  1426.   }
  1427.   varpos=eval_getpos(EV_T);
  1428.   if(varpos<0) return; /* unknown error */
  1429.   evalue_compile(p1); evalue_compile(p2);
  1430.   if(vimg_enable) vimg_plotstart();
  1431.   if (tikz_file) fprintf(tikz_file,"\%\%begin plot\n\\draw\[%s]",
  1432.     tikz_options(pm->color[0],-dash));
  1433.   for(i=j=0;i<=tstep;i++) {
  1434.     if(n==1) {
  1435.       if(tranged) t=tstart+i*v; else t=xstart+i*v;
  1436.       eval_setval(varpos,t); dc[0]=t; dc[1]=strevalue(p1);
  1437.     }
  1438.     else {
  1439.       t=tstart+i*v; eval_setval(varpos,t);
  1440.       dc[0]=strevalue(p1); dc[1]=strevalue(p2);
  1441.     }
  1442.     if(!isfinite(dc[0]) || !isfinite(dc[1])) ic[0]=ic[1]=-BOUND;
  1443.     else scale(dc,ic,1);
  1444.     if(vimg_enable) vimg_plot1 (scale_buf[0],scale_buf[1]);
  1445.     if(j==0) {
  1446.       gdImageSetPixel(image,ic[0],ic[1],pm->color[0]); j++;
  1447.       if (tikz_file)
  1448.         fprintf(tikz_file,"(%i,%i)rectangle(%i,%i)",
  1449.           ic[0],flip(ic[1]),ic[0]+1,flip(ic[1]+1));
  1450.     }
  1451.     else {
  1452.       xx=ic[0]-oc[0]; yy=ic[1]-oc[1];
  1453.       dist=sqrt(xx*xx+yy*yy);
  1454.       if(dist>0){
  1455.         if(dist>plotjump || dist==1) {
  1456.           gdImageSetPixel(image,ic[0],ic[1],pm->color[0]);
  1457.           if (tikz_file)
  1458.             fprintf(tikz_file,"(%i,%i)rectangle(%i,%i)",
  1459.               ic[0],flip(ic[1]),ic[0]+1,flip(ic[1]+1));
  1460.         }
  1461.         else {
  1462.           if (dash==1)
  1463.             myDashedLine(image,oc[0],oc[1],ic[0],ic[1],pm->color[0]);
  1464.           else
  1465.             gdImageLine(image,oc[0],oc[1],ic[0],ic[1],pm->color[0]);
  1466.           if (tikz_file)
  1467.             fprintf(tikz_file, "(%i,%i)--(%i,%i)",
  1468.                 oc[0],flip(oc[1]),ic[0],flip(ic[1]));
  1469.         }
  1470.       }
  1471.     }
  1472.     memmove(oc,ic,sizeof(oc));
  1473.   }
  1474.   if (tikz_file) fprintf(tikz_file,";\n\%\%end plot\n");
  1475.   if(vimg_enable) vimg_plotend();
  1476. }
  1477.  
  1478. void obj_plot(objparm *pm) { _obj_plot( pm,0) ;}
  1479. void obj_dplot(objparm *pm) { _obj_plot( pm,1) ; }
  1480.  
  1481. /* set levelcurve granularity */
  1482. void obj_levelstep(objparm *pm)
  1483. {
  1484.   int dd;
  1485.   dd=pm->pd[0];
  1486.   if(dd<1) return;
  1487.   if(dd>16) dd=16;
  1488.   lstep=dd;
  1489. }
  1490.  
  1491. /* level curve */
  1492. void obj_levelcurve(objparm *pm)
  1493. {
  1494.   char fn[MAX_LINELEN+1],tc[MAX_LINELEN+1];
  1495.   int n,i;
  1496.   double d;
  1497.   leveldata *ld;
  1498.  
  1499.   ld=xmalloc(sizeof(leveldata)+16);
  1500.   ld->xname="x"; ld->yname="y";
  1501.   ld->xsize=sizex; ld->ysize=sizey; ld->datacnt=0;
  1502.   ld->xrange[0]=xstart; ld->xrange[1]=sizex/xscale+xstart;
  1503.   ld->yrange[0]=sizey/yscale+ystart; ld->yrange[1]=ystart;
  1504.   ld->grain=lstep;
  1505.   fnd_item(pm->str,1,fn); ld->fn=fn;
  1506.   n=itemnum(pm->str);
  1507.   if(n<=1) {ld->levelcnt=1; ld->levels[0]=0;}
  1508.   else {
  1509.     if(n>LEVEL_LIM+1) n=LEVEL_LIM+1;
  1510.     for(i=0;i<n-1;i++) {
  1511.       fnd_item(pm->str,i+2,tc);
  1512.       d=strevalue(tc);
  1513.       if(isfinite(d)) ld->levels[i]=d; else ld->levels[i]=0;
  1514.     }
  1515.     ld->levelcnt=n-1;
  1516.   }
  1517.   levelcurve(ld);
  1518.   if (tikz_file)
  1519.     fprintf(tikz_file,"\%\%begin levelcurve\n\\draw\[%s]"
  1520.       ,tikz_options(pm->color[0],1));
  1521.   for(i=0;i<ld->datacnt;i++) {
  1522.     gdImageSetPixel(image,ld->xdata[i],ld->ydata[i],pm->color[0]);
  1523.     if (tikz_file)
  1524.       fprintf(tikz_file,
  1525.         "(%i,%i)rectangle(%i,%i)",
  1526.         ld->xdata[i],flip(ld->ydata[i]),ld->xdata[i]+1,flip(ld->ydata[i]+1));
  1527.   }
  1528.   if (tikz_file) fprintf(tikz_file,";\n\%\%end levelcurve\n");
  1529.   free(ld);
  1530. }
  1531.  
  1532. /* set animation step */
  1533. void obj_animstep(objparm *pm)
  1534. {
  1535.   animstep=pm->pd[0];
  1536.   setvar("animstep",pm->pd[0]);
  1537. }
  1538.  
  1539. /* Starts a line counting */
  1540. void obj_linecount(objparm *pm)
  1541. {
  1542.   linecnt=pm->pd[0];
  1543. }
  1544.  
  1545. /* marks end of execution */
  1546. void obj_end(objparm *pm)
  1547. {
  1548.   fly_error("successful_end_of_execution");
  1549. }
  1550.  
  1551. /* output */
  1552. void obj_output(objparm *pm)
  1553. {
  1554.   char *p, namebuf[1024];
  1555.   p=find_word_start(pm->str); *find_word_end(p)=0;
  1556.   snprintf(namebuf,sizeof(namebuf),"%s",imagefilename);
  1557.   snprintf(imagefilename,sizeof(imagefilename),"%s",p);
  1558.   output();
  1559.   snprintf(imagefilename,sizeof(imagefilename),"%s",namebuf);
  1560. }
  1561.  
  1562. /* vimgfile */
  1563. void obj_vimgfile(objparm *pm)
  1564. {
  1565.   char *p;
  1566.   p=find_word_start(pm->str); *find_word_end(p)=0;
  1567.   snprintf(vimgfilename,sizeof(vimgfilename),"%s",p);
  1568.   if(vimg_ready) vimg_close();
  1569. }
  1570.  
  1571. /* vimg enable/disable */
  1572. void obj_vimg(objparm *pm)
  1573. {
  1574.   vimg_enable=pm->pd[0];
  1575.   if(vimg_enable>0 && vimg_ready==0) vimg_init();
  1576. }
  1577.  
  1578. /* Set affine transformation */
  1579. void obj_affine(objparm *pm)
  1580. {
  1581.   int i;
  1582.   for(i=0;i<4;i++) matrix[i]=pm->pd[i];
  1583.   transx=pm->pd[4]; transy=pm->pd[5];
  1584.   transform=1;
  1585. }
  1586.  
  1587. /* Set affine transformation */
  1588. void obj_rotation(objparm *pm)
  1589. {
  1590.   double r;
  1591.   r=M_PI*pm->pd[0]/180;
  1592.   matrix[0]=matrix[3]=cos(r);
  1593.   matrix[1]=-sin(r); matrix[2]=sin(r);
  1594.   transform=1;
  1595. }
  1596.  
  1597. /* Set linear transformation */
  1598. void obj_linear(objparm *pm)
  1599. {
  1600.   int i;
  1601.   for(i=0;i<4;i++) matrix[i]=pm->pd[i];
  1602.   transform=1;
  1603. }
  1604.  
  1605. /* Set translation transformation */
  1606. void obj_translation(objparm *pm)
  1607. {
  1608.   transx=pm->pd[0]; transy=pm->pd[1];
  1609. }
  1610.  
  1611. /* kill affine transformation */
  1612. void obj_killaffine(objparm *pm)
  1613. {
  1614.   matrix[0]=matrix[3]=1;
  1615.   matrix[1]=matrix[2]=transx=transy=0;
  1616.   transform=0;
  1617. }
  1618.  
  1619. /* kill affine transformation */
  1620. void obj_killlinear(objparm *pm)
  1621. {
  1622.   matrix[0]=matrix[3]=1;
  1623.   matrix[1]=matrix[2]=0;
  1624.   transform=0;
  1625. }
  1626.  
  1627. /* kill affine transformation */
  1628. void obj_killtranslation(objparm *pm)
  1629. {
  1630.   transx=transy=0;
  1631. }
  1632.  
  1633. /***** Les modifs de Jean-Christophe Leger Fev 2006 *****/
  1634.  
  1635. /* arguments: un numero de matrice entre 1 et JC_NB_MATRICES, une liste de 4 nombres reels pour la matrice */
  1636. void obj_setmatrix(objparm *pm)
  1637. {
  1638.   int nummatrix = (int) (pm->pd[0]);
  1639.   if((nummatrix < 1) ||(nummatrix>JC_NB_MATRICES)) {
  1640.     fly_error("bad_matrix_number");
  1641.     return;
  1642.   }
  1643.   matrices_pavage[nummatrix][0] = pm->pd[1];
  1644.   matrices_pavage[nummatrix][1] = pm->pd[2];
  1645.   matrices_pavage[nummatrix][2] = pm->pd[3];
  1646.   matrices_pavage[nummatrix][3] = pm->pd[4];
  1647. }
  1648.  
  1649. /* arguments: un numero de matrice entre 1 et JC_NB_MATRICES */
  1650. void obj_resetmatrix(objparm *pm)
  1651. {
  1652.   int nummatrix = (int) (pm->pd[0]);
  1653.   if((nummatrix < 1) ||(nummatrix>JC_NB_MATRICES)) {
  1654.     fly_error("bad_matrix_number");
  1655.     return;
  1656.   }
  1657.   matrices_pavage[nummatrix][0] = 1;
  1658.   matrices_pavage[nummatrix][1] = 0;
  1659.   matrices_pavage[nummatrix][2] = 0;
  1660.   matrices_pavage[nummatrix][3] = 1;
  1661.  
  1662. }
  1663. /* arguments: un numero de vecteur entre 1 et JC_NB_MATRICES, une liste de 2 nombres reels pour le vecteur */
  1664. void obj_setvector(objparm *pm)
  1665. {
  1666.   int nummatrix = (int) (pm->pd[0]);
  1667.   if((nummatrix < 1) ||(nummatrix>JC_NB_MATRICES)) {
  1668.     fly_error("bad_vector_number");
  1669.     return;
  1670.   }
  1671.   vecteurs_pavage[nummatrix][0] = pm->pd[1];
  1672.   vecteurs_pavage[nummatrix][1] = pm->pd[2];
  1673. }
  1674.  
  1675. /* arguments: un numero de matrice entre 1 et JC_NB_MATRICES */
  1676. void obj_resetvector(objparm *pm)
  1677. {
  1678.   int nummatrix = (int) (pm->pd[0]);
  1679.   if((nummatrix < 1) ||(nummatrix>JC_NB_MATRICES)) {
  1680.     fly_error("bad_vector_number");
  1681.     return;
  1682.   }
  1683.   vecteurs_pavage[nummatrix][0] = 0;
  1684.   vecteurs_pavage[nummatrix][1] = 0;
  1685. }
  1686.  
  1687. /* arguments: un numero de vecteur entre 1 et JC_NB_MATRICES, une liste de 6 nombres reels pour la matrice et le vecteur */
  1688. void obj_settransform(objparm *pm)
  1689. {
  1690.   int nummatrix = (int) (pm->pd[0]);
  1691.   if((nummatrix < 1) ||(nummatrix>JC_NB_MATRICES)) {
  1692.     fly_error("bad_vector_number");
  1693.     return;
  1694.   }
  1695.   matrices_pavage[nummatrix][0] = pm->pd[1];
  1696.   matrices_pavage[nummatrix][1] = pm->pd[2];
  1697.   matrices_pavage[nummatrix][2] = pm->pd[3];
  1698.   matrices_pavage[nummatrix][3] = pm->pd[4];
  1699.   vecteurs_pavage[nummatrix][0] = pm->pd[5];
  1700.   vecteurs_pavage[nummatrix][1] = pm->pd[6];
  1701. }
  1702.  
  1703.  
  1704. /* arguments: un numero de matrice entre 1 et JC_NB_MATRICES */
  1705. void obj_resettransform(objparm *pm)
  1706. {
  1707.   int nummatrix = (int) (pm->pd[0]);
  1708.   if((nummatrix < 1) ||(nummatrix>JC_NB_MATRICES)) {
  1709.     fly_error("bad_vector_number");
  1710.     return;
  1711.   }
  1712.   vecteurs_pavage[nummatrix][0] = 0;
  1713.   vecteurs_pavage[nummatrix][1] = 0;
  1714.   matrices_pavage[nummatrix][0] = 1;
  1715.   matrices_pavage[nummatrix][1] = 0;
  1716.   matrices_pavage[nummatrix][2] = 0;
  1717.   matrices_pavage[nummatrix][3] = 1;
  1718. }
  1719.  
  1720. /* arguments: une liste de 6 nombres reels pour les coordonnees de l'origine et des vecteurs de base */
  1721. void obj_setparallelogram(objparm *pm)
  1722. {
  1723.   int i;
  1724.   for(i=0;i<6;i++)  parallogram_fonda[i]=pm->pd[i];
  1725. }
  1726.  
  1727. /* arguments :aucun */
  1728. void obj_resetparallelogram(objparm *pm)
  1729. {
  1730.   parallogram_fonda[0]=0;
  1731.   parallogram_fonda[1]=0;
  1732.   parallogram_fonda[2]=100;
  1733.   parallogram_fonda[3]=0;
  1734.   parallogram_fonda[4]=0;
  1735.   parallogram_fonda[5]=100;
  1736. }
  1737.  
  1738. /* argument : la liste des numeros des matrices a faire agir, le nom du fichier de l'image a inclure */
  1739. void obj_multicopy(objparm *pm)
  1740. {
  1741.   char *pp,*pe,*cptr;
  1742.   FILE *inf;
  1743.   gdImagePtr insimg;
  1744.   int i,j;
  1745.   int imax,jmax;
  1746.   int c; /* la couleur reperee */
  1747.   int mat;
  1748.   int nummatrices[JC_NB_MATRICES];
  1749.   double pt[2]; /* les coordonnees math. du point repere dans le parallelogramme */
  1750.   double ptr[2]; /* les coordonnees math. du point transforme */
  1751.   int pti[2]; /* les coordonnees img du point a placer sur le canevas */
  1752.   int t;
  1753.   /* gestion palette de couleur de l'image a inserer */
  1754.   int colorMap[gdMaxColors];
  1755.   int comptmat=0; /* combien de matrices en tout ? */
  1756.  
  1757.   for (i=0; (i<gdMaxColors); i++) {
  1758.     colorMap[i] = (-1);
  1759.   }
  1760.  
  1761.   /* Gestion des parametres la liste est censee finir avec le nom du fichier */
  1762.   t=pm->pcnt-1; /* il faut enlever le nom de fichier ! c'est le nombre de parametres en plus */
  1763.   pp=find_word_start(pm->str);
  1764.   /* visiblement find_word_start n'arrive pas a trouver le dernier mot dans un contexte ou le nombre de parameters est variable
  1765.      * on cherche donc l'emplacement de la derniere virgule:
  1766.      * il y en a une car t>0, on retrouve ensuite le debut de mot
  1767.      */
  1768.   for(pe=pp;*pe!=0;pe++);/* trouve la fin de la chaine */
  1769.   if(t>0){
  1770.     for(cptr = pe; cptr > pp && *cptr != ','; cptr--);
  1771.     pp=find_word_start(cptr+1);
  1772.   }
  1773.   pe=find_word_end(pp);*pe=0; /* strip junk final */
  1774.   //      printf("pp contient %s\n",pp);
  1775.  
  1776.  
  1777.   inf=open4read(pp);
  1778.   if(inf==NULL) {
  1779.     fly_error("file_not_exist"); return;
  1780.   }
  1781.   insimg=gdImageCreateFromGif(inf); fclose(inf);
  1782.   if(insimg==NULL) {
  1783.     fly_error("bad_gif"); return;
  1784.   }
  1785.  
  1786.   /* On recupere les numeros des matrices/vecteurs a faire agir,
  1787.     * s'il n'y en a pas, on les fait toutes agir
  1788.   */
  1789.   for(i=0;i<t && i< JC_NB_MATRICES;i++) {
  1790.     if(pm->pd[i]>=1 && pm->pd[i]<=JC_NB_MATRICES){
  1791.       nummatrices[comptmat] = pm->pd[i];
  1792.       comptmat++;
  1793.     }
  1794.   }
  1795.   if(t<=0){
  1796.     for(i=0;i<JC_NB_MATRICES;i++) {
  1797.       nummatrices[i] = i+1;
  1798.     }
  1799.     comptmat=JC_NB_MATRICES;
  1800.   }
  1801.  
  1802.  
  1803.   imax = gdImageSX(insimg);
  1804.   jmax = gdImageSY(insimg);
  1805.  
  1806.   for(i=0;i<imax;i++){
  1807.     for(j=0;j<jmax;j++){
  1808.       int nc;
  1809.       c=gdImageGetPixel(insimg,i,j);
  1810.       /* Le code suivant est une copie du code dans gdImageCopy  Added 7/24/95: support transparent copies */
  1811.       if (gdImageGetTransparent(insimg) != c) {
  1812.         /* Have we established a mapping for this color? */
  1813.         if (colorMap[c] == (-1)) {
  1814.           /* If it's the same image, mapping is trivial, dans notre cas ca n'arrive jamais... */
  1815.           if (image == insimg) {
  1816.             nc = c;
  1817.           } else {
  1818.             /* First look for an exact match */
  1819.             nc = gdImageColorExact(image,
  1820.                    insimg->red[c], insimg->green[c],
  1821.                            insimg->blue[c]);
  1822.           }
  1823.           if (nc == (-1)) {
  1824.           /* No, so try to allocate it */
  1825.             nc = gdImageColorAllocate(image,
  1826.                              insimg->red[c], insimg->green[c],
  1827.                              insimg->blue[c]);
  1828.              /* If we're out of colors, go for the closest color */
  1829.             if (nc == (-1)) {
  1830.               nc = gdImageColorClosest(image,
  1831.                               insimg->red[c], insimg->green[c],
  1832.                               insimg->blue[c]);
  1833.             }
  1834.           }
  1835.           colorMap[c] = nc;
  1836.         }
  1837.  
  1838.         pt[0]= i*(parallogram_fonda[2])/(imax-1)+(jmax-j)*(parallogram_fonda[4])/(jmax-1)+parallogram_fonda[0];
  1839.         pt[1]= i*(parallogram_fonda[3])/(imax-1)+(jmax-j)*(parallogram_fonda[5])/(jmax-1)+parallogram_fonda[1];
  1840.         for(mat=0;mat<comptmat;mat++){
  1841.           double *matricecourante = matrices_pavage[nummatrices[mat]];
  1842.           double *vecteurcourant = vecteurs_pavage[nummatrices[mat]];
  1843.           ptr[0] = matricecourante[0]*pt[0]+matricecourante[1]*pt[1]+vecteurcourant[0];
  1844.           ptr[1] = matricecourante[2]*pt[0]+matricecourante[3]*pt[1]+vecteurcourant[1];
  1845.           scale(ptr,pti,1);
  1846.           gdImageSetPixel(image,pti[0],pti[1],colorMap[c]);
  1847.         }
  1848.       }
  1849.     }
  1850.   }
  1851.   gdImageDestroy(insimg);
  1852. }
  1853.  
  1854. /**** Fin modifs JC Fev 06 ******/
  1855.  
  1856. /* get color from value */
  1857. int calc_color(char *p, struct objtab *o)
  1858. {
  1859.   int k, cc[3];
  1860.   char buf[MAX_LINELEN+1];
  1861.   for(k=0;k<3;k++) {
  1862.     fnd_item(p,k+1,buf);
  1863.     cc[k]=strevalue(buf);
  1864.   }
  1865.   collapse_item(p,3);
  1866.   if(cc[0]==-1 && cc[1]==-1 && cc[2]==-255) {
  1867.  
  1868.     if(brushed && o->subst&1) return gdBrushed;
  1869.     else return color_black;
  1870.   }
  1871.   if(cc[0]==-1 && cc[1]==-255 && cc[2]==-1) {
  1872.     if(styled && o->subst&2)  return gdStyled;
  1873.     else return color_black;
  1874.   }
  1875.   if(cc[0]==-255 && cc[1]==-1 && cc[2]==-1) {
  1876.     if(tiled && o->fill_tag==1)  return gdTiled;
  1877.     else return color_black;
  1878.   }
  1879.   return getcolor(cc[0],cc[1],cc[2]);
  1880. }
  1881.  
  1882. /* Routine to parse parameters */
  1883. int parse_parms(char *p,objparm *pm,struct objtab *o)
  1884. {
  1885.   char buf[MAX_LINELEN+1];
  1886.   char *p1, *p2;
  1887.   int j,t,c,c1,c2;
  1888.  
  1889.   c=o->color_pos;c1=c2=0;
  1890.   pm->color[0]=pm->color[1]=0;
  1891.   if(c>0) c1=c;
  1892.   if(c<0) c2=-c;
  1893.   c=c1+c2;
  1894.   t=itemnum(p);if(t<o->required_parms+3*c) return -1;
  1895.   if(c1>0 && t>o->required_parms+3*c) t=o->required_parms+3*c;
  1896.   pm->pcnt=t-3*c;
  1897.   if(pm->pcnt>MAX_PARMS) pm->pcnt=MAX_PARMS;
  1898.   if(c2>0) {
  1899.     for(j=0;j<2 && j<c2; j++) pm->color[j]=calc_color(p,o);
  1900.   }
  1901.   snprintf(buf,sizeof(buf),"%s",p);
  1902.   for(j=0, p1=buf; j<pm->pcnt; j++, p1=p2) {
  1903.     p2=find_item_end(p1); if(*p2) *p2++=0;
  1904.     p1=find_word_start(p1);
  1905.     if(*p1) pm->pd[j]=strevalue(p1); else pm->pd[j]=0;
  1906.     if(!isfinite(pm->pd[j])) {
  1907.         if(j<o->required_parms) return -1;
  1908.         else {pm->pd[j]=0;break;}
  1909.     }
  1910.   }
  1911.   collapse_item(p,o->required_parms);
  1912.   if(c1>0) {
  1913.     for(j=0;j<c1 && j<2; j++) pm->color[j]=calc_color(p,o);
  1914.   }
  1915.   if(width>1 && o->subst&1 && pm->color[0]!=gdBrushed
  1916.       && pm->color[0]!=gdStyled && pm->color[0]!=gdTiled) {
  1917.     pm->color[1]=pm->color[0];
  1918.     pm->color[0]=widthcolor(width,pm->color[0]);
  1919.   }
  1920.   pm->fill=o->fill_tag;
  1921.   if(o->required_parms!=0){
  1922.     if(dashed) {
  1923.       if(pm->fill==1) pm->fill=2;
  1924.       if(pm->fill==0) pm->fill=-1;
  1925.        if(noreset==0) dashed=0;
  1926.     }
  1927.     if(filled) {
  1928.       if(pm->fill==-1) pm->fill=2;
  1929.       if(pm->fill==0) pm->fill=1;
  1930.       if(noreset==0) filled=0;
  1931.     }
  1932.   }
  1933.   ovlstrcpy(pm->str,p); return 0;
  1934. }
  1935.  
  1936. /* Create the struct objparm pm corresponding to the objparm p
  1937.  * Execute a command. Returns 0 if OK.
  1938.  */
  1939. int obj_main(char *p)
  1940. {
  1941.   int i;
  1942.   char *pp,*name,*pt;
  1943.   char tbuf2[MAX_LINELEN+1];
  1944.   struct objparm pm;
  1945.  
  1946.   p=find_word_start(p);
  1947.   if(*p==exec_prefix_char || *p==0) return 0; /* comment */
  1948.   name=p;
  1949.   pp=find_name_end(p);
  1950.   pt=find_word_start(pp); if(*pt=='=') *pt=' ';
  1951.   if(*pt==':' && *(pt+1)=='=') *pt=*(pt+1)=' ';
  1952.   pp=find_word_end(p);
  1953.   if(*pp!=0) {
  1954.     *(pp++)=0; pp=find_word_start(pp);
  1955.   }
  1956.   if(strlen(p)==1 || (strlen(p)==2 && isdigit(*(p+1)))) {
  1957.     setvar(p,strevalue(pp)); return 0;
  1958.   }
  1959.   /* search the number of the object */
  1960.   i=search_list(objtab,obj_no,sizeof(objtab[0]),name);
  1961.   if(i<0) {
  1962.     fly_error("bad_cmd"); return 1;
  1963.   }
  1964.   if(image==NULL && (objtab[i].color_pos || objtab[i].required_parms>2)) {
  1965.     fly_error("image_not_defined"); return 1;
  1966.   }
  1967.   ovlstrcpy(tbuf2,pp);
  1968.   if(objtab[i].color_pos || objtab[i].routine==obj_setstyle) {
  1969.     substit(tbuf2);
  1970.   }
  1971.   if(parse_parms(tbuf2,&pm,objtab+i)!=0) fly_error("bad_parms");
  1972.   else objtab[i].routine(&pm);
  1973.   return 0;
  1974. }
  1975.