Subversion Repositories wimsdev

Rev

Rev 18318 | Rev 18325 | 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, int, int, int, int, 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. /* hyperbolic geodesics from x1,y1 to x2,y2, x3,y3 to x4,y4 in Poincaré disk */
  655.  
  656. void obj_hypgeods(objparm *pm)
  657. {
  658.  int i, *qq = pm->p;
  659.  for (i = 0; i < pm->pcnt; i+=4){
  660.    double r, *q=pm->pd + i,
  661.    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],
  662.    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],
  663.    dy = -2*q[1]*q[2]+2*q[0]*q[3];
  664.    if (dy*dy * 1e8 < nx*nx+ny*ny){
  665.      scale(q,qq,2);
  666.      gdImageLine(image,qq[0],qq[1],qq[2],qq[3],pm->color[0]);
  667.    }
  668.     else {
  669.       double rx,ry;
  670.       double dc[2];int ic[2];
  671.       double
  672.         cx = ny/dy, cy=-nx/dy,
  673.         a1 = atan2(q[1]-cy, q[0]-cx)/DEG,
  674.         a2 = atan2(q[3]-cy, q[2]-cx)/DEG;
  675.       if (fabs(a2-a1)>180){if(a1<a2) a1+=360; else a2+=360;};
  676.       r = sqrt(cx*cx+cy*cy-1);
  677.       scale2(r,r,&rx,&ry);
  678.       dc[0]=cx;dc[1]=cy;
  679.       scale(dc,ic,1);
  680.       if(a1<a2) myGdImageArc(image,ic[0],ic[1],rx,ry,a1,a2,pm->color[0]);
  681.       else myGdImageArc(image,ic[0],ic[1],rx,ry,a2,a1,pm->color[0]);
  682.     }
  683.   }
  684. }
  685. /* segments */
  686. void obj_dlines(objparm *pm)
  687. {
  688.  int i, n;
  689.  n=(pm->pcnt)/2;
  690.  scale(pm->pd,pm->p,n);
  691.  if (tikz_file){
  692.    fprintf(tikz_file,"\\draw\[%s] (%i,%i)",
  693.      tikz_options(pm->color[0],pm->fill),pm->p[0],flip(pm->p[1]));
  694.    for(i=2;i<2*n;i+=2)
  695.      fprintf(tikz_file, "--(%i,%i)",pm->p[i],flip(pm->p[i+1]));
  696.    fprintf(tikz_file, ";\n");
  697.  }
  698.  for(i=2;i<2*n;i+=2)
  699.    myDashedLine(image,pm->p[i-2],pm->p[i-1],pm->p[i],pm->p[i+1],pm->color[0]);
  700.  if(vimg_enable) vimg_polyline(scale_buf,n,0);
  701. }
  702.  
  703. /* points */
  704. void obj_points(objparm *pm)
  705. {
  706.  int i, n;
  707.  n=(pm->pcnt)/2;
  708.  scale(pm->pd,pm->p,n);
  709.  for(i=0;i<2*n;i+=2) gdImageSetPixel(image,pm->p[i],pm->p[i+1],pm->color[0]);
  710.  if(tikz_file) {
  711.    fprintf(tikz_file, "\\draw\[%s]", tikz_options(pm->color[0],pm->fill));
  712.    for(i=0;i<2*n;i+=2)
  713.      fprintf(tikz_file,"(%i,%i)circle(1pt)",pm->p[i],flip(pm->p[i+1]));
  714.    fprintf(tikz_file,";\n");
  715.  }
  716. }
  717.  
  718. /* lattice.
  719.  * x0,y0,xv1,yv1,xv2,yv2,n1,n2,color */
  720. void obj_lattice(objparm *pm)
  721. {
  722.  int n1,n2,i1,i2,xi1,yi1,xi2,yi2;
  723.  double xv1,xv2,yv1,yv2;
  724.  n1=pm->pd[6];n2=pm->pd[7]; if(n1<0 || n2<0) return;
  725.  if(n1>256) n1=256;
  726.  if(n2>256) n2=256;
  727.  scale(pm->pd,pm->p,1);
  728.  scale2(pm->pd[2],pm->pd[3],&xv1,&yv1);
  729.  scale2(pm->pd[4],pm->pd[5],&xv2,&yv2);
  730.  if(tikz_file)
  731.    fprintf(tikz_file,"\\draw[%s]", tikz_options(pm->color[0],0));
  732.  for(i1=0;i1<n1;i1++) {
  733.    xi1=rint(i1*xv1)+pm->p[0]; yi1=rint(i1*yv1)+pm->p[1];
  734.    for(i2=0;i2<n2;i2++) {
  735.      xi2=i2*xv2+xi1;yi2=i2*yv2+yi1;
  736.      gdImageSetPixel(image,xi2,yi2,pm->color[0]);
  737.      if(tikz_file) fprintf(tikz_file,"(%i,%i)circle(1pt)",xi2,flip(yi2));
  738.    }
  739.  }
  740.  if(tikz_file) fprintf(tikz_file,";\n");
  741. }
  742.  
  743. /* arc */
  744. /*trouble with gdImageArc as the angles are of type int */
  745. void myGdImageArc(gdImagePtr im, int cx, int cy, int rx, int ry, double t1, double t2, int color)
  746. {
  747.   int i,nb;
  748.   double dt, r=rx>ry?rx:ry;
  749.   t2-=t1;
  750.   t2-=360*floor(t2/360);
  751.   if(t2<1) t2=360;
  752.   t1*=DEG;t2*=DEG;
  753.   nb = r*t2/3;
  754.   dt = t2/nb;
  755.   for (i=0; i<nb; ++i,t1+=dt)
  756.     gdImageLine(im,cx+rx*cos(t1),cy+ry*sin(t1),
  757.     cx+rx*cos(t1+dt),cy+ry*sin(t1+dt),color);
  758. }
  759.  
  760. void obj_arc(objparm *pm)
  761. {
  762.   scale(pm->pd,pm->p,1);
  763.   int rx=pm->pd[2]*xscale/2, ry=pm->pd[3]*yscale/2;
  764.   myGdImageArc(image, pm->p[0], pm->p[1], rx, ry, pm->pd[4],pm->pd[5],pm->color[0]);
  765.   if(tikz_file){
  766.     pm->pd[5]-=pm->pd[4];
  767.     pm->pd[5]-=360*floor(pm->pd[5]/360);
  768.     pm->pd[5]+=pm->pd[4];
  769.     fprintf(tikz_file,
  770. /*    "\\draw\[%s, domain=%f:%f] plot ({%i+%f*cos(\\x)}, {%i+%f*sin(\\x)});\n",*/
  771.       "\\draw \[%s] (%i,%i) arc (%f:%f:%i and %i);\n",
  772.         tikz_options(pm->color[0],pm->fill),
  773.         (int)rint(pm->p[0]+rx*cos(DEG*pm->pd[4])),
  774.         flip((int)rint(pm->p[1]+ry*sin(DEG*pm->pd[4]))),
  775.         pm->pd[4],pm->pd[5],rx,-ry);
  776.   }
  777.   /*FIXME echelle mauvaise*/
  778.   if(vimg_enable) vimg_arc(scale_buf[0],scale_buf[1],
  779.                      0.5*pm->pd[2],0.5*pm->pd[3],pm->pd[4],pm->pd[5]);
  780. }
  781.  
  782. void obj_angle(objparm *pm)
  783. {
  784. /* dans la doc: mettre les angles dans l'ordre ???? demander qu'ils soient entre 0 et 360 */
  785.   double dpts[6];
  786.   int pts[6], wx=rint(2*pm->pd[2]*xscale), wy=rint(2*pm->pd[2]*yscale);
  787.   dpts[0]=pm->pd[0];
  788.   dpts[1]=pm->pd[1];
  789.   dpts[2]=pm->pd[0]+pm->pd[2]*cos(DEG*pm->pd[3]);
  790.   dpts[3]=pm->pd[1]+pm->pd[2]*sin(DEG*pm->pd[3]);
  791.   dpts[4]=pm->pd[0]+pm->pd[2]*cos(DEG*pm->pd[4]);
  792.   dpts[5]=pm->pd[1]+pm->pd[2]*sin(DEG*pm->pd[4]);
  793.   scale(dpts, pts, 3);
  794.   gdImageLine(image,pts[0],pts[1],pts[2],pts[3],pm->color[0]);
  795.   gdImageLine(image,pts[0],pts[1],pts[4],pts[5],pm->color[0]);
  796.   gdImageArc(image,pts[0],pts[1],wx,wy,pm->pd[3],pm->pd[4],pm->color[0]);
  797.   if(tikz_file) {
  798.     fprintf(tikz_file, "\\draw\[%s, domain=%f:%f] plot ({%i+%f*cos(\\x)}, {%i+%f*sin(\\x)});\n",
  799.       tikz_options(pm->color[0],pm->fill),pm->pd[3],pm->pd[4],pts[0],wx*0.5,flip(pts[1]),wy*(-0.5));
  800.     fprintf(tikz_file, "\\draw\[%s] (%i, %i) -- (%i, %i) (%i, %i) -- (%i, %i);\n",
  801.         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]));
  802.   }
  803. }
  804. /* Ellipse: centre 0,1, width 2, hight 3, color 4,5,6 */
  805. void obj_ellipse(objparm *pm)
  806. {
  807.   scale(pm->pd,pm->p,1);
  808.   pm->p[2]=pm->pd[2]*xscale; pm->p[3]=pm->pd[3]*yscale;
  809.   if(pm->fill==1 || pm->fill==2) {
  810.     gdImageArc(image,pm->p[0],pm->p[1],pm->p[2],pm->p[3],0,360,
  811.              color_bounder);
  812.     patchgdImageFillToBorder(image,pm->p[0],pm->p[1],
  813.                     color_bounder,pm->color[0]);
  814.   }
  815.   gdImageArc(image,pm->p[0],pm->p[1],rint(pm->p[2]),rint(pm->p[3]),0,360,pm->color[0]);
  816.   if(tikz_file) fprintf(tikz_file,"\\draw\[%s] (%i,%i) ellipse (%i and %i);\n\n",
  817.     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]));
  818.   if(vimg_enable) vimg_ellipse(scale_buf[0],scale_buf[1],0.5*pm->pd[2],0.5*pm->pd[3]);
  819. }
  820.  
  821. /* Circle radius in pixels*/
  822. void obj_circle(objparm *pm)
  823. {
  824.   scale(pm->pd,pm->p,1);
  825.   pm->p[2]=rint(pm->pd[2]); pm->p[3]=rint(pm->pd[2]);
  826.   if(pm->fill==1 || pm->fill==2) {
  827.     gdImageArc(image,pm->p[0],pm->p[1],pm->p[2],pm->p[3],0,360,
  828.              color_bounder);
  829.     patchgdImageFillToBorder(image,pm->p[0],pm->p[1],
  830.                     color_bounder,pm->color[0]);
  831.   }
  832.   gdImageArc(image,pm->p[0],pm->p[1],pm->p[2],pm->p[3],0,360,pm->color[0]);
  833.   if(tikz_file) fprintf(tikz_file, "\\draw\[%s] (%i, %i) circle (%i);\n",
  834.       tikz_options(pm->color[0],pm->fill),pm->p[0],flip(pm->p[1]),(int) rint(pm->pd[2]/2.));
  835. }
  836. /* Circle, radius in xrange */
  837. void obj_circles(objparm *pm)
  838. {
  839.   int i, n;
  840.   n=(pm->pcnt)/3;
  841.   for (i=0; i<n; ++i) scale(pm->pd+3*i, pm->p+3*i, 1);
  842.   if (tikz_file) fprintf(tikz_file,"\\draw\[%s]",tikz_options(pm->color[0],pm->fill));
  843.   for(i=0;i<3*n;i+=3){
  844.     if(pm->fill==1 || pm->fill==2) {
  845.       gdImageArc(image,pm->p[i],pm->p[i+1],
  846.         rint(2*pm->pd[i+2]*xscale),rint(2*pm->pd[i+2]*xscale),0,360,
  847.           color_bounder);
  848.       patchgdImageFillToBorder(image,pm->p[i],pm->p[i+1],
  849.                     color_bounder,pm->color[0]);
  850.     }
  851.     gdImageArc(image,pm->p[i],pm->p[i+1],
  852.         rint(2*pm->pd[i+2]*xscale),rint(2*pm->pd[i+2]*xscale),0,360,pm->color[0]);
  853.     if (tikz_file){
  854.       fprintf(tikz_file, "(%i, %i) circle (%i and %i)",
  855.         pm->p[i],flip(pm->p[i+1]),(int) rint(pm->pd[i+2]*xscale),(int) rint(pm->pd[i+2]*xscale));
  856.     }
  857.   }
  858.   if (tikz_file) fprintf(tikz_file,";\n");
  859. }
  860. /* ellipse, width in xrange,height in yrange */
  861. void obj_ellipses(objparm *pm)
  862. {
  863.   int i, n;
  864.   n=(pm->pcnt)/4;
  865.   for (i=0; i<n; ++i) scale(pm->pd+4*i, pm->p+4*i, 1);
  866.   if (tikz_file) fprintf(tikz_file,"\\draw\[%s]",tikz_options(pm->color[0],pm->fill));
  867.   for(i=0;i<4*n;i+=4){
  868.     if(pm->fill==1 || pm->fill==2){
  869.       gdImageArc(image,pm->p[i],pm->p[i+1],
  870.         rint(pm->pd[i+2]*xscale),rint(pm->pd[i+2]*xscale),0,360,
  871.         color_bounder);
  872.       patchgdImageFillToBorder(image,pm->p[i],pm->p[i+1],
  873.         color_bounder,pm->color[0]);
  874.     } else
  875.       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]);
  876.     if (tikz_file){
  877.       fprintf(tikz_file, "(%i, %i) circle (%i and %i)",
  878.         pm->p[i],flip(pm->p[i+1]),(int) rint(pm->pd[i+2]*xscale/2),(int) rint(pm->pd[i+3]*yscale/2));
  879.     }
  880.   }
  881.   if (tikz_file) fprintf(tikz_file,";\n");
  882. }
  883. typedef enum tikz_fill_options {grid, dot, hatch, diamond} tfo;
  884. int compar(const void *a, const void *b) {return *(int *)b - *(int *)a;}
  885.  
  886. static void tikz_fill(int x, int y, int nx, int ny, int wall, int the_color, tfo opt)
  887. {
  888.   int numpix=sizex*sizey, top = 0, a, c, cy, lx=0, rx=0, seed, nt,ct=0;
  889.   int *stack = xmalloc(numpix*sizeof(int));
  890.   int *stack2 = xmalloc(numpix*sizeof(int));
  891.   if (nx==0) nx=1;
  892.   if (ny==0) ny=1;
  893.   nt = abs(nx*ny);
  894.   seed = gdImageGetPixel(image,x,y);
  895.   char *check = xmalloc(numpix);
  896.   while (numpix--) check[numpix]=0;
  897.   a=x+y*sizex;
  898.   check[a]=1;
  899.   stack[top++]=a;
  900.   while(top)
  901.     {
  902.       a = stack[--top];
  903.       x = a%sizex; y = a/sizex;
  904.       c = gdImageGetPixel(image,x,y);
  905.       if ((wall && (c == wall))||(!wall && c!=seed))
  906.         continue;
  907.       if (x>0 && !check[a-1]) {check[a-1]=1; stack[top++]=a-1;}
  908.       if (x+1<sizex && !check[a+1]) {check[a+1]=1; stack[top++]=a+1;}
  909.       if (y>0 && !check[a-sizex]) {check[a-sizex]=1; stack[top++]=a-sizex;}
  910.       if (y+1<sizey && !check[a+sizex]) {check[a+sizex]=1; stack[top++]=a+sizex;}
  911.       switch(opt)
  912.   {
  913.   case grid: if(((x%nx)!=(nx/2))&&((y%ny)!=(ny/2))) continue; break;
  914.   case dot: if(x%nx!=nx/2||y%ny!=ny/2) continue; break;
  915.   case hatch: if((ny*x-nx*y+nt)%nt) continue; break;
  916.   case diamond: if((ny*x+nx*y)%nt && (ny*x-nx*y+nt)%nt)continue;
  917.   default: break;
  918.   }
  919.       stack2[ct++]=a;
  920.     }
  921.   free(stack);
  922.   free(check);
  923.   if (ct==0) return;
  924.   fprintf(tikz_file,"\\draw\[%s]", tikz_options(the_color,1));
  925.   qsort(stack2, ct, sizeof(int), compar);
  926.   cy=-1;
  927.   while (ct--)
  928.     {
  929.       a = stack2[ct];
  930.       x = a%sizex; y = a/sizex;
  931.       if (y != cy || x != rx+1)
  932.       {
  933.   if (cy >= 0)
  934.     fprintf(tikz_file,
  935.       "(%.2f,%.2f)rectangle(%.2f,%.2f)",
  936.       lx-0.0,flip(cy-0.0),rx+0.0,flip(cy+0.0));
  937.   cy = y;
  938.   lx = rx = x;
  939.       }
  940.       else
  941.   rx++;
  942.     }
  943.   fprintf(tikz_file,
  944.     "(%.2f,%.2f)rectangle(%.2f,%.2f);\n",
  945.      lx-0.0,flip(cy-0.0),rx+0.0,flip(cy+0.0));
  946.   free(stack2);
  947. }
  948. void obj_dashed(objparm *pm){ dashed=1;}
  949. void obj_filled(objparm *pm){ filled=1;}
  950. void obj_noreset(objparm *pm){ noreset=1;}
  951. void obj_reset(objparm *pm){ noreset=0;dashed=0;filled=0;}
  952.  
  953. /* flood fill */
  954. void obj_fill(objparm *pm)
  955. {
  956.   scale(pm->pd,pm->p,1);
  957.   if (tikz_file) tikz_fill(pm->p[0],pm->p[1],1,1,0,pm->color[0],grid);
  958.   patchgdImageFill(image,pm->p[0],pm->p[1],pm->color[0]);
  959. }
  960.  
  961. /* flood fill to border*/
  962. void obj_fillb(objparm *pm)
  963. {
  964.   scale(pm->pd,pm->p,1);
  965.   if (tikz_file) tikz_fill(pm->p[0],pm->p[1],1,1,pm->color[0],pm->color[1],grid);
  966.   patchgdImageFillToBorder(image,pm->p[0],pm->p[1],pm->color[0],pm->color[1]);
  967. }
  968.  
  969. gdImagePtr himg;
  970.  
  971. int makehatchimage(int x, int y, int px, int py, int col)
  972. {
  973.   int c1,c2,r,g,b;
  974.   gdImagePtr saveimg;
  975.   himg=gdImageCreate(x,y);
  976.   c1=gdImageGetPixel(image,px,py);
  977.   r=gdImageRed(image,c1); g=gdImageGreen(image,c1); b=gdImageBlue(image,c1);
  978.   if(r>=255) r--; else r++; if(g>=255) g--; else g++; if(b>=255) b--; else b++;
  979.   c1=gdImageColorAllocate(himg,r,g,b);
  980.   r=gdImageRed(image,col); g=gdImageGreen(image,col); b=gdImageBlue(image,col);
  981.   c2=gdImageColorAllocate(himg,r,g,b);
  982.   if(width>1) {
  983.     savew=-1; saveimg=image;
  984.     image=himg; c2=widthcolor(width,c2); image=saveimg;
  985.     c2=gdBrushed; savew=-1;
  986.   }
  987.   return c2;
  988. }
  989.  
  990. /* flood fill with hatching */
  991. void obj_hatchfill(objparm *pm)
  992. {
  993.   int nx,ny,ax,ay, dir, c;
  994.   scale(pm->pd,pm->p,1);
  995.   nx=pm->pd[2]; ny=pm->pd[3]; ax=abs(nx); ay=abs(ny);
  996.   if(nx==0 && ny==0) {fly_error("bad displacement vector"); return;}
  997.   if((nx>0 && ny>0) || (nx<0 && ny<0)) dir=1; else dir=-1;
  998.   if(ax==0) {ax=100; dir=2;}
  999.   if(ay==0) {ay=100; dir=3;}
  1000.   c=makehatchimage(ax,ay,pm->p[0],pm->p[1],pm->color[0]);
  1001.   switch(dir) {
  1002.     case -1: {
  1003.       gdImageLine(himg,0,ay-1,ax-1,0,c);
  1004.       if(width>1) {
  1005.         gdImageLine(himg,-ax,ay-1,-1,0,c);
  1006.         gdImageLine(himg,ax,ay-1,2*ax-1,0,c);
  1007.         gdImageLine(himg,0,-1,ax-1,-ay,c);
  1008.         gdImageLine(himg,0,2*ay-1,ax-1,ay,c);
  1009.       }
  1010.       break;
  1011.     }
  1012.     case 1: {
  1013.       gdImageLine(himg,0,0,ax-1,ay-1,c);
  1014.       if(width>1) {
  1015.         gdImageLine(himg,-ax,0,-1,ay-1,c);
  1016.         gdImageLine(himg,ax,0,2*ax-1,ay-1,c);
  1017.         gdImageLine(himg,0,-ay,ax-1,-1,c);
  1018.         gdImageLine(himg,0,ay,ax-1,2*ay-1,c);
  1019.       }
  1020.       break;
  1021.     }
  1022.     case 2: gdImageLine(himg,0,ay/2,ax-1,ay/2,c); break;
  1023.     case 3: gdImageLine(himg,ax/2,0,ax/2,ay-1,c); break;
  1024.   }
  1025.   if (tikz_file) tikz_fill(pm->p[0],pm->p[1],nx,ny,0,pm->color[0],hatch);
  1026.   gdImageSetTile(image,himg);
  1027.   patchgdImageFill(image,pm->p[0],pm->p[1],gdTiled);
  1028.   gdImageDestroy(himg);
  1029.   if(tiled) gdImageSetTile(image,tileimg);
  1030. }
  1031.  
  1032. /* flood fill with grid */
  1033. void obj_gridfill(objparm *pm)
  1034. {
  1035.   int nx,ny, c;
  1036.   scale(pm->pd,pm->p,1);
  1037.   nx=pm->pd[2]; ny=pm->pd[3]; nx=abs(nx); ny=abs(ny);
  1038.   if(nx==0 && ny==0) {fly_error("bad grid size"); return;}
  1039.   c=makehatchimage(nx,ny,pm->p[0],pm->p[1],pm->color[0]);
  1040.   if (tikz_file) tikz_fill(pm->p[0],pm->p[1],nx,ny,0,pm->color[0],grid);
  1041.   gdImageLine(himg,0,ny/2,nx-1,ny/2,c); gdImageLine(himg,nx/2,0,nx/2,ny-1,c);
  1042.   gdImageSetTile(image,himg);
  1043.   patchgdImageFill(image,pm->p[0],pm->p[1],gdTiled);
  1044.   gdImageDestroy(himg);
  1045.   if(tiled) gdImageSetTile(image,tileimg);
  1046. }
  1047.  
  1048. /* flood fill with double hatching */
  1049. void obj_diafill(objparm *pm)
  1050. {
  1051.   int nx,ny, c;
  1052.   scale(pm->pd,pm->p,1);
  1053.   nx=pm->pd[2]; ny=pm->pd[3]; nx=abs(nx); ny=abs(ny);
  1054.   if(nx==0 && ny==0) {fly_error("bad grid size"); return;}
  1055.   c=makehatchimage(nx,ny,pm->p[0],pm->p[1],pm->color[0]);
  1056.   if (tikz_file) tikz_fill(pm->p[0],pm->p[1],nx,ny,0,pm->color[0],diamond);
  1057.   gdImageLine(himg,0,0,nx-1,ny-1,c); gdImageLine(himg,0,ny-1,nx-1,0,c);
  1058.   gdImageSetTile(image,himg);
  1059.   patchgdImageFill(image,pm->p[0],pm->p[1],gdTiled);
  1060.   gdImageDestroy(himg);
  1061.   if(tiled) gdImageSetTile(image,tileimg);
  1062. }
  1063.  
  1064. /* flood fill with dots */
  1065. void obj_dotfill(objparm *pm)
  1066. {
  1067.   int nx,ny, c;
  1068.   scale(pm->pd,pm->p,1);
  1069.   nx=pm->pd[2]; ny=pm->pd[3]; nx=abs(nx); ny=abs(ny);
  1070.   if(nx==0 && ny==0) {fly_error("bad grid size"); return;}
  1071.   c=makehatchimage(nx,ny,pm->p[0],pm->p[1],pm->color[0]);
  1072.   if (tikz_file) tikz_fill(pm->p[0],pm->p[1],nx,ny,0,pm->color[0],dot);
  1073.   gdImageSetPixel(himg,nx/2,ny/2,c);
  1074.   gdImageSetTile(image,himg);
  1075.   patchgdImageFill(image,pm->p[0],pm->p[1],gdTiled);
  1076.   gdImageDestroy(himg);
  1077.   if(tiled) gdImageSetTile(image,tileimg);
  1078. }
  1079.  
  1080. struct {
  1081.   char *name;
  1082.   gdFontPtr *fpt;
  1083.   char *ftikz;
  1084. } fonttab[]={
  1085.   {"tiny",  &gdFontTiny,"0.3"},
  1086.   {"small", &gdFontSmall,"0.5"},
  1087.   {"medium",&gdFontMediumBold,"0.7"},
  1088.   {"large", &gdFontLarge,"0.9"},
  1089.   {"giant", &gdFontGiant,"1"},
  1090.   {"huge",  &gdFontGiant,"1"}
  1091. };
  1092.  
  1093. #define fonttab_no (sizeof(fonttab)/sizeof(fonttab[0]))
  1094.  
  1095. /* string */
  1096. void obj_string(objparm *pm)
  1097. {
  1098.   char *pp, *pe, *p2;
  1099.   int i;
  1100.   pp=pm->str; pe=strchr(pp,','); if(pe==NULL) {
  1101.     fly_error("too_few_parms"); return;
  1102.   }
  1103.   *pe++=0; pp=find_word_start(pp); *find_word_end(pp)=0;
  1104.   pe=find_word_start(pe); strip_trailing_spaces(pe);
  1105.   if(*pp) {
  1106.     for(i=0;i<fonttab_no && strcmp(pp,fonttab[i].name)!=0; i++);
  1107.     if(i>=fonttab_no) i=1;
  1108.   }
  1109.   else i=1;
  1110.   scale(pm->pd,pm->p,1);
  1111.   if(*pe=='"') {
  1112.     p2=strchr(pe+1,'"');
  1113.     if(p2 && *(p2+1)==0) {*p2=0; pe++;}
  1114.   }
  1115.   if(pm->fill){
  1116.     gdImageStringUp(image,*(fonttab[i].fpt),pm->p[0],pm->p[1], (unsigned char*) pe,
  1117.         pm->color[0]);
  1118.     if(tikz_file) fprintf(tikz_file,"\\draw (%i,%i) node[scale=%s,rotate=90,color=%s,anchor=north west,inner sep=0pt] {\\(%s\\)};\n",
  1119.       pm->p[0],flip(pm->p[1]),fonttab[i].ftikz,tikz_color(pm->color[0]),(unsigned char*) pe);
  1120.   }
  1121.   else {
  1122.     gdImageString(image,*(fonttab[i].fpt),pm->p[0],pm->p[1], (unsigned char*) pe,
  1123.         pm->color[0]);
  1124.     if(tikz_file) fprintf(tikz_file,"\\draw (%i,%i) node[scale=%s,color=%s,anchor=north west,inner sep=0pt] {\\(%s\\)};\n",
  1125.       pm->p[0],flip(pm->p[1]),fonttab[i].ftikz,tikz_color(pm->color[0]),(unsigned char*) pe);
  1126.   }
  1127. }
  1128. /*FIXME; giant does not exist in tikz and ... samething as huge */
  1129. /* point */
  1130. void obj_point(objparm *pm)
  1131. {
  1132.   scale(pm->pd,pm->p,1);
  1133.   gdImageSetPixel(image,pm->p[0],pm->p[1],pm->color[0]);
  1134.   if(tikz_file) fprintf(tikz_file,"\\draw[%s] (%i,%i) circle (1pt);\n",
  1135.     tikz_options(pm->color[0],0),pm->p[0],flip(pm->p[1]));
  1136. }
  1137.  
  1138. /* copy an image file */
  1139. void obj_copy(objparm *pm)
  1140. {
  1141.   char *pp;
  1142.   FILE *inf;
  1143.   gdImagePtr insimg;
  1144.  
  1145.   pp=find_word_start(pm->str);*find_word_end(pp)=0;
  1146.   inf=open4read(pp);
  1147.   if(inf==NULL) {
  1148.     fly_error("file_does_not_exist"); return;
  1149.   }
  1150.   insimg=gdImageCreateFromGif(inf); fclose(inf);
  1151.   if(insimg==NULL) {
  1152.     fly_error("bad_gif"); return;
  1153.   }
  1154.   scale(pm->pd,pm->p,1);
  1155.   if(pm->pd[2]<0 && pm->pd[3]<0 && pm->pd[4]<0 && pm->pd[5]<0)
  1156.     gdImageCopy(image,insimg,pm->p[0],pm->p[1],0,0,
  1157.             insimg->sx,insimg->sy);
  1158.   else
  1159.     gdImageCopy(image,insimg,pm->p[0],pm->p[1],pm->pd[2],pm->pd[3],
  1160.             pm->pd[4]-pm->pd[2],pm->pd[5]-pm->pd[3]);
  1161.   if(tikz_file) fprintf(tikz_file,
  1162.     "\\node[anchor=north west,inner sep=0pt] at (%i,%i) {\\includegraphics\[width=.05\\linewidth]{%s}};\n",
  1163.       pm->p[0],flip(pm->p[1]),pm->str);
  1164.     gdImageDestroy(insimg);
  1165. /*FIXME position non correcte; on ne peut pas utiliser une image gif */
  1166. }
  1167.  
  1168. /* copy an image file, with resizing */
  1169. void obj_copyresize(objparm *pm)
  1170. {
  1171.   char *pp;
  1172.   FILE *inf;
  1173.   gdImagePtr insimg;
  1174.  
  1175.   pp=find_word_start(pm->str);*find_word_end(pp)=0;
  1176.   inf=open4read(pp);
  1177.   if(inf==NULL) {
  1178.     fly_error("file_not_found"); return;
  1179.   }
  1180.   insimg=gdImageCreateFromGif(inf); fclose(inf);
  1181.   if(insimg==NULL) {
  1182.     fly_error("bad_gif"); return;
  1183.   }
  1184.   scale(pm->pd+4,pm->p+4,2);
  1185.   if(pm->pd[0]<0 && pm->pd[1]<0 && pm->pd[2]<0 && pm->pd[3]<0)
  1186.     gdImageCopyResized(image,insimg,pm->p[4],pm->p[5],0,0,
  1187.                  pm->p[6]-pm->p[4]+1,pm->p[7]-pm->p[5]+1,
  1188.                  insimg->sx,insimg->sy);
  1189.   else
  1190.     gdImageCopyResized(image,insimg,pm->p[4],pm->p[5],pm->pd[0],pm->pd[1],
  1191.                  pm->p[6]-pm->p[4]+1,pm->p[7]-pm->p[5]+1,
  1192.                  pm->pd[2]-pm->pd[0]+1,pm->pd[3]-pm->pd[1]+1);
  1193.   gdImageDestroy(insimg);
  1194. }
  1195.  
  1196. /* set brush or tile */
  1197. void obj_setbrush(objparm *pm)
  1198. {
  1199.   char *pp;
  1200.   FILE *inf;
  1201.   gdImagePtr insimg;
  1202.  
  1203.   pp=find_word_start(pm->str); *find_word_end(pp)=0;
  1204.   inf=open4read(pp); if(inf==NULL) {
  1205.     fly_error("file_not_found"); return;
  1206.   }
  1207.   insimg=gdImageCreateFromGif(inf); fclose(inf);
  1208.   if(insimg==NULL) {
  1209.     fly_error("bad_gif"); return;
  1210.   }
  1211.   if(pm->fill) {
  1212.     gdImageSetTile(image,insimg); tiled=1; tileimg=insimg;
  1213.   }
  1214.   else {
  1215.     gdImageSetBrush(image,insimg);brushed=1; brushimg=insimg;
  1216.   }
  1217. }
  1218.  
  1219. /* kill brush */
  1220. void obj_killbrush(objparm *pm)
  1221. {
  1222.   if(brushimg) gdImageDestroy(brushimg);
  1223.   brushed=0; brushimg=NULL;
  1224. }
  1225.  
  1226. /* kill tile */
  1227. void obj_killtile(objparm *pm)
  1228. {
  1229.   if(tileimg) gdImageDestroy(tileimg);
  1230.   tiled=0; tileimg=NULL;
  1231. }
  1232.  
  1233. /* set style */
  1234. void obj_setstyle(objparm *pm)
  1235. {
  1236.   int i,t;
  1237.   t=pm->pcnt/3; if(t<=0) {
  1238.     fly_error("too_few_parms"); return;
  1239.   }
  1240.   for(i=0;i<t;i++) {
  1241.     if(pm->pd[3*i]<0 || pm->pd[3*i+1]<0 || pm->pd[3*i+2]<0)
  1242.       pm->p[i]=gdTransparent;
  1243.     else
  1244.       pm->p[i]=getcolor(pm->pd[3*i],pm->pd[3*i+1],pm->pd[3*i+2]);
  1245.   }
  1246.   gdImageSetStyle(image,pm->p,t); styled=1;
  1247. }
  1248.  
  1249. /* kill style */
  1250. void obj_killstyle(objparm *pm)
  1251. {
  1252.   styled=0;
  1253. }
  1254.  
  1255. /* set transparent */
  1256. void obj_transp(objparm *pm)
  1257. {
  1258.   gdImageColorTransparent(image,pm->color[0]);
  1259. }
  1260.  
  1261. /* set interlace */
  1262. void obj_interlace(objparm *pm)
  1263. {
  1264.   gdImageInterlace(image,1);
  1265. }
  1266.  
  1267. /* set linewidth */
  1268. void obj_linewidth(objparm *pm)
  1269. {
  1270.   if(pm->pd[0]<1 || pm->pd[0]>255) fly_error("bad_parms");
  1271.   width=pm->pd[0];
  1272.   if(tikz_file) fprintf(tikz_file,"\\pgfsetlinewidth{%fpt}\n",width*0.7);
  1273. }
  1274.  
  1275. /* set crosshairsize */
  1276. void obj_crosshairsize(objparm *pm)
  1277. {
  1278.   if(pm->pd[0]<1 || pm->pd[0]>255) fly_error("bad_parms");
  1279.   else width2=pm->pd[0];
  1280. }
  1281.  
  1282. /* set x range */
  1283. void obj_xrange(objparm *pm)
  1284. {
  1285.   double dd;
  1286.   dd=pm->pd[1]-pm->pd[0];
  1287.   xstart=pm->pd[0]; xscale=sizex/dd;
  1288. }
  1289.  
  1290. /* set y range */
  1291. void obj_yrange(objparm *pm)
  1292. {
  1293.   double dd;
  1294.   dd=pm->pd[1]-pm->pd[0];
  1295.   ystart=pm->pd[1]; yscale=-sizey/dd;
  1296. }
  1297.  
  1298. /* set x et y range */
  1299. void obj_range(objparm *pm)
  1300. {
  1301.   double d1,d2;
  1302.   d1=pm->pd[1]-pm->pd[0]; d2=pm->pd[3]-pm->pd[2];
  1303.   xstart=pm->pd[0]; xscale=(sizex-1)/d1;
  1304.   ystart=pm->pd[3]; yscale=-(sizey-1)/d2;
  1305. }
  1306.  
  1307. /* set t range */
  1308. void obj_trange(objparm *pm)
  1309. {
  1310.   /*double dd;
  1311.   dd=pm->pd[1]-pm->pd[0];*/
  1312.   tstart=pm->pd[0]; tend=pm->pd[1];
  1313.   tranged=1;
  1314. }
  1315.  
  1316. /* set tstep (plotting step) */
  1317. void obj_tstep(objparm *pm)
  1318. {
  1319.   int dd;
  1320.   dd=pm->pd[0];
  1321.   if(dd<3) {
  1322.     fly_error("bad_step"); return;
  1323.   }
  1324.   if(dd>2000) dd=2000;
  1325.   tstep=dd;
  1326. }
  1327.  
  1328. /* set plotjump (plot jump break threashold) */
  1329. void obj_plotjump(objparm *pm)
  1330. {
  1331.   int dd;
  1332.   dd=pm->pd[0];
  1333.   if(dd<3) dd=3;
  1334.   if(dd>MAX_SIZE) dd=MAX_SIZE;
  1335.   plotjump=dd;
  1336. }
  1337.  
  1338. /* plot a curve, either parametric or explicit */
  1339. void _obj_plot(objparm *pm,int dash)
  1340. {
  1341.   int i,j,n,dist,xx,yy,varpos;
  1342.   char p1[MAX_LINELEN+1], p2[MAX_LINELEN+1];
  1343.   char *varn, *pp;
  1344.   double dc[2],t,v;
  1345.   int ic[2],oc[2];
  1346.  
  1347.   n=itemnum(pm->str);
  1348.   if(n<1) {
  1349.     fly_error("bad_parms"); return;
  1350.   }
  1351.   fnd_item(pm->str,1,p1); fnd_item(pm->str,2,p2);
  1352.   if(n==1 && !tranged) v=sizex/xscale/tstep;
  1353.   else v=(tend-tstart)/tstep;
  1354.   if(n==1) varn="x"; else varn="t";
  1355.   for(pp=varchr(p1,varn); pp; pp=varchr(pp,varn)) {
  1356.     string_modify(p1,pp,pp+strlen(varn),EV_T);
  1357.     pp+=strlen(EV_T);
  1358.   }
  1359.   for(pp=varchr(p2,varn); pp; pp=varchr(pp,varn)) {
  1360.     string_modify(p2,pp,pp+strlen(varn),EV_T);
  1361.       pp+=strlen(EV_T);
  1362.   }
  1363.   varpos=eval_getpos(EV_T);
  1364.   if(varpos<0) return; /* unknown error */
  1365.   evalue_compile(p1); evalue_compile(p2);
  1366.   if(vimg_enable) vimg_plotstart();
  1367.   if (tikz_file) fprintf(tikz_file,"\%\%begin plot\n\\draw\[%s]",
  1368.     tikz_options(pm->color[0],-dash));
  1369.   for(i=j=0;i<=tstep;i++) {
  1370.     if(n==1) {
  1371.       if(tranged) t=tstart+i*v; else t=xstart+i*v;
  1372.       eval_setval(varpos,t); dc[0]=t; dc[1]=strevalue(p1);
  1373.     }
  1374.     else {
  1375.       t=tstart+i*v; eval_setval(varpos,t);
  1376.       dc[0]=strevalue(p1); dc[1]=strevalue(p2);
  1377.     }
  1378.     if(!isfinite(dc[0]) || !isfinite(dc[1])) ic[0]=ic[1]=-BOUND;
  1379.     else scale(dc,ic,1);
  1380.     if(vimg_enable) vimg_plot1 (scale_buf[0],scale_buf[1]);
  1381.     if(j==0) {
  1382.       gdImageSetPixel(image,ic[0],ic[1],pm->color[0]); j++;
  1383.       if (tikz_file)
  1384.         fprintf(tikz_file,"(%i,%i)rectangle(%i,%i)",
  1385.           ic[0],flip(ic[1]),ic[0]+1,flip(ic[1]+1));
  1386.     }
  1387.     else {
  1388.       xx=ic[0]-oc[0]; yy=ic[1]-oc[1];
  1389.       dist=sqrt(xx*xx+yy*yy);
  1390.       if(dist>0){
  1391.         if(dist>plotjump || dist==1) {
  1392.           gdImageSetPixel(image,ic[0],ic[1],pm->color[0]);
  1393.           if (tikz_file)
  1394.             fprintf(tikz_file,"(%i,%i)rectangle(%i,%i)",
  1395.               ic[0],flip(ic[1]),ic[0]+1,flip(ic[1]+1));
  1396.         }
  1397.         else {
  1398.           if (dash==1)
  1399.             myDashedLine(image,oc[0],oc[1],ic[0],ic[1],pm->color[0]);
  1400.           else
  1401.             gdImageLine(image,oc[0],oc[1],ic[0],ic[1],pm->color[0]);
  1402.           if (tikz_file)
  1403.             fprintf(tikz_file, "(%i,%i)--(%i,%i)",
  1404.                 oc[0],flip(oc[1]),ic[0],flip(ic[1]));
  1405.         }
  1406.       }
  1407.     }
  1408.     memmove(oc,ic,sizeof(oc));
  1409.   }
  1410.   if (tikz_file) fprintf(tikz_file,";\n\%\%end plot\n");
  1411.   if(vimg_enable) vimg_plotend();
  1412. }
  1413.  
  1414. void obj_plot(objparm *pm) { _obj_plot( pm,0) ;}
  1415. void obj_dplot(objparm *pm) { _obj_plot( pm,1) ; }
  1416.  
  1417. /* set levelcurve granularity */
  1418. void obj_levelstep(objparm *pm)
  1419. {
  1420.   int dd;
  1421.   dd=pm->pd[0];
  1422.   if(dd<1) return;
  1423.   if(dd>16) dd=16;
  1424.   lstep=dd;
  1425. }
  1426.  
  1427. /* level curve */
  1428. void obj_levelcurve(objparm *pm)
  1429. {
  1430.   char fn[MAX_LINELEN+1],tc[MAX_LINELEN+1];
  1431.   int n,i;
  1432.   double d;
  1433.   leveldata *ld;
  1434.  
  1435.   ld=xmalloc(sizeof(leveldata)+16);
  1436.   ld->xname="x"; ld->yname="y";
  1437.   ld->xsize=sizex; ld->ysize=sizey; ld->datacnt=0;
  1438.   ld->xrange[0]=xstart; ld->xrange[1]=sizex/xscale+xstart;
  1439.   ld->yrange[0]=sizey/yscale+ystart; ld->yrange[1]=ystart;
  1440.   ld->grain=lstep;
  1441.   fnd_item(pm->str,1,fn); ld->fn=fn;
  1442.   n=itemnum(pm->str);
  1443.   if(n<=1) {ld->levelcnt=1; ld->levels[0]=0;}
  1444.   else {
  1445.     if(n>LEVEL_LIM+1) n=LEVEL_LIM+1;
  1446.     for(i=0;i<n-1;i++) {
  1447.       fnd_item(pm->str,i+2,tc);
  1448.       d=strevalue(tc);
  1449.       if(isfinite(d)) ld->levels[i]=d; else ld->levels[i]=0;
  1450.     }
  1451.     ld->levelcnt=n-1;
  1452.   }
  1453.   levelcurve(ld);
  1454.   if (tikz_file)
  1455.     fprintf(tikz_file,"\%\%begin levelcurve\n\\draw\[%s]"
  1456.       ,tikz_options(pm->color[0],1));
  1457.   for(i=0;i<ld->datacnt;i++) {
  1458.     gdImageSetPixel(image,ld->xdata[i],ld->ydata[i],pm->color[0]);
  1459.     if (tikz_file)
  1460.       fprintf(tikz_file,
  1461.         "(%i,%i)rectangle(%i,%i)",
  1462.         ld->xdata[i],flip(ld->ydata[i]),ld->xdata[i]+1,flip(ld->ydata[i]+1));
  1463.   }
  1464.   if (tikz_file) fprintf(tikz_file,";\n\%\%end levelcurve\n");
  1465.   free(ld);
  1466. }
  1467.  
  1468. /* set animation step */
  1469. void obj_animstep(objparm *pm)
  1470. {
  1471.   animstep=pm->pd[0];
  1472.   setvar("animstep",pm->pd[0]);
  1473. }
  1474.  
  1475. /* Starts a line counting */
  1476. void obj_linecount(objparm *pm)
  1477. {
  1478.   linecnt=pm->pd[0];
  1479. }
  1480.  
  1481. /* marks end of execution */
  1482. void obj_end(objparm *pm)
  1483. {
  1484.   fly_error("successful_end_of_execution");
  1485. }
  1486.  
  1487. /* output */
  1488. void obj_output(objparm *pm)
  1489. {
  1490.   char *p, namebuf[1024];
  1491.   p=find_word_start(pm->str); *find_word_end(p)=0;
  1492.   snprintf(namebuf,sizeof(namebuf),"%s",imagefilename);
  1493.   snprintf(imagefilename,sizeof(imagefilename),"%s",p);
  1494.   output();
  1495.   snprintf(imagefilename,sizeof(imagefilename),"%s",namebuf);
  1496. }
  1497.  
  1498. /* vimgfile */
  1499. void obj_vimgfile(objparm *pm)
  1500. {
  1501.   char *p;
  1502.   p=find_word_start(pm->str); *find_word_end(p)=0;
  1503.   snprintf(vimgfilename,sizeof(vimgfilename),"%s",p);
  1504.   if(vimg_ready) vimg_close();
  1505. }
  1506.  
  1507. /* vimg enable/disable */
  1508. void obj_vimg(objparm *pm)
  1509. {
  1510.   vimg_enable=pm->pd[0];
  1511.   if(vimg_enable>0 && vimg_ready==0) vimg_init();
  1512. }
  1513.  
  1514. /* Set affine transformation */
  1515. void obj_affine(objparm *pm)
  1516. {
  1517.   int i;
  1518.   for(i=0;i<4;i++) matrix[i]=pm->pd[i];
  1519.   transx=pm->pd[4]; transy=pm->pd[5];
  1520.   transform=1;
  1521. }
  1522.  
  1523. /* Set affine transformation */
  1524. void obj_rotation(objparm *pm)
  1525. {
  1526.   double r;
  1527.   r=M_PI*pm->pd[0]/180;
  1528.   matrix[0]=matrix[3]=cos(r);
  1529.   matrix[1]=-sin(r); matrix[2]=sin(r);
  1530.   transform=1;
  1531. }
  1532.  
  1533. /* Set linear transformation */
  1534. void obj_linear(objparm *pm)
  1535. {
  1536.   int i;
  1537.   for(i=0;i<4;i++) matrix[i]=pm->pd[i];
  1538.   transform=1;
  1539. }
  1540.  
  1541. /* Set translation transformation */
  1542. void obj_translation(objparm *pm)
  1543. {
  1544.   transx=pm->pd[0]; transy=pm->pd[1];
  1545. }
  1546.  
  1547. /* kill affine transformation */
  1548. void obj_killaffine(objparm *pm)
  1549. {
  1550.   matrix[0]=matrix[3]=1;
  1551.   matrix[1]=matrix[2]=transx=transy=0;
  1552.   transform=0;
  1553. }
  1554.  
  1555. /* kill affine transformation */
  1556. void obj_killlinear(objparm *pm)
  1557. {
  1558.   matrix[0]=matrix[3]=1;
  1559.   matrix[1]=matrix[2]=0;
  1560.   transform=0;
  1561. }
  1562.  
  1563. /* kill affine transformation */
  1564. void obj_killtranslation(objparm *pm)
  1565. {
  1566.   transx=transy=0;
  1567. }
  1568.  
  1569. /***** Les modifs de Jean-Christophe Leger Fev 2006 *****/
  1570.  
  1571. /* arguments: un numero de matrice entre 1 et JC_NB_MATRICES, une liste de 4 nombres reels pour la matrice */
  1572. void obj_setmatrix(objparm *pm)
  1573. {
  1574.   int nummatrix = (int) (pm->pd[0]);
  1575.   if((nummatrix < 1) ||(nummatrix>JC_NB_MATRICES)) {
  1576.     fly_error("bad_matrix_number");
  1577.     return;
  1578.   }
  1579.   matrices_pavage[nummatrix][0] = pm->pd[1];
  1580.   matrices_pavage[nummatrix][1] = pm->pd[2];
  1581.   matrices_pavage[nummatrix][2] = pm->pd[3];
  1582.   matrices_pavage[nummatrix][3] = pm->pd[4];
  1583. }
  1584.  
  1585. /* arguments: un numero de matrice entre 1 et JC_NB_MATRICES */
  1586. void obj_resetmatrix(objparm *pm)
  1587. {
  1588.   int nummatrix = (int) (pm->pd[0]);
  1589.   if((nummatrix < 1) ||(nummatrix>JC_NB_MATRICES)) {
  1590.     fly_error("bad_matrix_number");
  1591.     return;
  1592.   }
  1593.   matrices_pavage[nummatrix][0] = 1;
  1594.   matrices_pavage[nummatrix][1] = 0;
  1595.   matrices_pavage[nummatrix][2] = 0;
  1596.   matrices_pavage[nummatrix][3] = 1;
  1597.  
  1598. }
  1599. /* arguments: un numero de vecteur entre 1 et JC_NB_MATRICES, une liste de 2 nombres reels pour le vecteur */
  1600. void obj_setvector(objparm *pm)
  1601. {
  1602.   int nummatrix = (int) (pm->pd[0]);
  1603.   if((nummatrix < 1) ||(nummatrix>JC_NB_MATRICES)) {
  1604.     fly_error("bad_vector_number");
  1605.     return;
  1606.   }
  1607.   vecteurs_pavage[nummatrix][0] = pm->pd[1];
  1608.   vecteurs_pavage[nummatrix][1] = pm->pd[2];
  1609. }
  1610.  
  1611. /* arguments: un numero de matrice entre 1 et JC_NB_MATRICES */
  1612. void obj_resetvector(objparm *pm)
  1613. {
  1614.   int nummatrix = (int) (pm->pd[0]);
  1615.   if((nummatrix < 1) ||(nummatrix>JC_NB_MATRICES)) {
  1616.     fly_error("bad_vector_number");
  1617.     return;
  1618.   }
  1619.   vecteurs_pavage[nummatrix][0] = 0;
  1620.   vecteurs_pavage[nummatrix][1] = 0;
  1621. }
  1622.  
  1623. /* arguments: un numero de vecteur entre 1 et JC_NB_MATRICES, une liste de 6 nombres reels pour la matrice et le vecteur */
  1624. void obj_settransform(objparm *pm)
  1625. {
  1626.   int nummatrix = (int) (pm->pd[0]);
  1627.   if((nummatrix < 1) ||(nummatrix>JC_NB_MATRICES)) {
  1628.     fly_error("bad_vector_number");
  1629.     return;
  1630.   }
  1631.   matrices_pavage[nummatrix][0] = pm->pd[1];
  1632.   matrices_pavage[nummatrix][1] = pm->pd[2];
  1633.   matrices_pavage[nummatrix][2] = pm->pd[3];
  1634.   matrices_pavage[nummatrix][3] = pm->pd[4];
  1635.   vecteurs_pavage[nummatrix][0] = pm->pd[5];
  1636.   vecteurs_pavage[nummatrix][1] = pm->pd[6];
  1637. }
  1638.  
  1639.  
  1640. /* arguments: un numero de matrice entre 1 et JC_NB_MATRICES */
  1641. void obj_resettransform(objparm *pm)
  1642. {
  1643.   int nummatrix = (int) (pm->pd[0]);
  1644.   if((nummatrix < 1) ||(nummatrix>JC_NB_MATRICES)) {
  1645.     fly_error("bad_vector_number");
  1646.     return;
  1647.   }
  1648.   vecteurs_pavage[nummatrix][0] = 0;
  1649.   vecteurs_pavage[nummatrix][1] = 0;
  1650.   matrices_pavage[nummatrix][0] = 1;
  1651.   matrices_pavage[nummatrix][1] = 0;
  1652.   matrices_pavage[nummatrix][2] = 0;
  1653.   matrices_pavage[nummatrix][3] = 1;
  1654. }
  1655.  
  1656. /* arguments: une liste de 6 nombres reels pour les coordonnees de l'origine et des vecteurs de base */
  1657. void obj_setparallelogram(objparm *pm)
  1658. {
  1659.   int i;
  1660.   for(i=0;i<6;i++)  parallogram_fonda[i]=pm->pd[i];
  1661. }
  1662.  
  1663. /* arguments :aucun */
  1664. void obj_resetparallelogram(objparm *pm)
  1665. {
  1666.   parallogram_fonda[0]=0;
  1667.   parallogram_fonda[1]=0;
  1668.   parallogram_fonda[2]=100;
  1669.   parallogram_fonda[3]=0;
  1670.   parallogram_fonda[4]=0;
  1671.   parallogram_fonda[5]=100;
  1672. }
  1673.  
  1674. /* argument : la liste des numeros des matrices a faire agir, le nom du fichier de l'image a inclure */
  1675. void obj_multicopy(objparm *pm)
  1676. {
  1677.   char *pp,*pe,*cptr;
  1678.   FILE *inf;
  1679.   gdImagePtr insimg;
  1680.   int i,j;
  1681.   int imax,jmax;
  1682.   int c; /* la couleur reperee */
  1683.   int mat;
  1684.   int nummatrices[JC_NB_MATRICES];
  1685.   double pt[2]; /* les coordonnees math. du point repere dans le parallelogramme */
  1686.   double ptr[2]; /* les coordonnees math. du point transforme */
  1687.   int pti[2]; /* les coordonnees img du point a placer sur le canevas */
  1688.   int t;
  1689.   /* gestion palette de couleur de l'image a inserer */
  1690.   int colorMap[gdMaxColors];
  1691.   int comptmat=0; /* combien de matrices en tout ? */
  1692.  
  1693.   for (i=0; (i<gdMaxColors); i++) {
  1694.     colorMap[i] = (-1);
  1695.   }
  1696.  
  1697.   /* Gestion des parametres la liste est censee finir avec le nom du fichier */
  1698.   t=pm->pcnt-1; /* il faut enlever le nom de fichier ! c'est le nombre de parametres en plus */
  1699.   pp=find_word_start(pm->str);
  1700.   /* visiblement find_word_start n'arrive pas a trouver le dernier mot dans un contexte ou le nombre de parameters est variable
  1701.      * on cherche donc l'emplacement de la derniere virgule:
  1702.      * il y en a une car t>0, on retrouve ensuite le debut de mot
  1703.      */
  1704.   for(pe=pp;*pe!=0;pe++);/* trouve la fin de la chaine */
  1705.   if(t>0){
  1706.     for(cptr = pe; cptr > pp && *cptr != ','; cptr--);
  1707.     pp=find_word_start(cptr+1);
  1708.   }
  1709.   pe=find_word_end(pp);*pe=0; /* strip junk final */
  1710.   //      printf("pp contient %s\n",pp);
  1711.  
  1712.  
  1713.   inf=open4read(pp);
  1714.   if(inf==NULL) {
  1715.     fly_error("file_not_exist"); return;
  1716.   }
  1717.   insimg=gdImageCreateFromGif(inf); fclose(inf);
  1718.   if(insimg==NULL) {
  1719.     fly_error("bad_gif"); return;
  1720.   }
  1721.  
  1722.   /* On recupere les numeros des matrices/vecteurs a faire agir,
  1723.     * s'il n'y en a pas, on les fait toutes agir
  1724.   */
  1725.   for(i=0;i<t && i< JC_NB_MATRICES;i++) {
  1726.     if(pm->pd[i]>=1 && pm->pd[i]<=JC_NB_MATRICES){
  1727.       nummatrices[comptmat] = pm->pd[i];
  1728.       comptmat++;
  1729.     }
  1730.   }
  1731.   if(t<=0){
  1732.     for(i=0;i<JC_NB_MATRICES;i++) {
  1733.       nummatrices[i] = i+1;
  1734.     }
  1735.     comptmat=JC_NB_MATRICES;
  1736.   }
  1737.  
  1738.  
  1739.   imax = gdImageSX(insimg);
  1740.   jmax = gdImageSY(insimg);
  1741.  
  1742.   for(i=0;i<imax;i++){
  1743.     for(j=0;j<jmax;j++){
  1744.       int nc;
  1745.       c=gdImageGetPixel(insimg,i,j);
  1746.       /* Le code suivant est une copie du code dans gdImageCopy  Added 7/24/95: support transparent copies */
  1747.       if (gdImageGetTransparent(insimg) != c) {
  1748.         /* Have we established a mapping for this color? */
  1749.         if (colorMap[c] == (-1)) {
  1750.           /* If it's the same image, mapping is trivial, dans notre cas ca n'arrive jamais... */
  1751.           if (image == insimg) {
  1752.             nc = c;
  1753.           } else {
  1754.             /* First look for an exact match */
  1755.             nc = gdImageColorExact(image,
  1756.                    insimg->red[c], insimg->green[c],
  1757.                            insimg->blue[c]);
  1758.           }
  1759.           if (nc == (-1)) {
  1760.           /* No, so try to allocate it */
  1761.             nc = gdImageColorAllocate(image,
  1762.                              insimg->red[c], insimg->green[c],
  1763.                              insimg->blue[c]);
  1764.              /* If we're out of colors, go for the closest color */
  1765.             if (nc == (-1)) {
  1766.               nc = gdImageColorClosest(image,
  1767.                               insimg->red[c], insimg->green[c],
  1768.                               insimg->blue[c]);
  1769.             }
  1770.           }
  1771.           colorMap[c] = nc;
  1772.         }
  1773.  
  1774.         pt[0]= i*(parallogram_fonda[2])/(imax-1)+(jmax-j)*(parallogram_fonda[4])/(jmax-1)+parallogram_fonda[0];
  1775.         pt[1]= i*(parallogram_fonda[3])/(imax-1)+(jmax-j)*(parallogram_fonda[5])/(jmax-1)+parallogram_fonda[1];
  1776.         for(mat=0;mat<comptmat;mat++){
  1777.           double *matricecourante = matrices_pavage[nummatrices[mat]];
  1778.           double *vecteurcourant = vecteurs_pavage[nummatrices[mat]];
  1779.           ptr[0] = matricecourante[0]*pt[0]+matricecourante[1]*pt[1]+vecteurcourant[0];
  1780.           ptr[1] = matricecourante[2]*pt[0]+matricecourante[3]*pt[1]+vecteurcourant[1];
  1781.           scale(ptr,pti,1);
  1782.           gdImageSetPixel(image,pti[0],pti[1],colorMap[c]);
  1783.         }
  1784.       }
  1785.     }
  1786.   }
  1787.   gdImageDestroy(insimg);
  1788. }
  1789.  
  1790. /**** Fin modifs JC Fev 06 ******/
  1791.  
  1792. /* get color from value */
  1793. int calc_color(char *p, struct objtab *o)
  1794. {
  1795.   int k, cc[3];
  1796.   char buf[MAX_LINELEN+1];
  1797.   for(k=0;k<3;k++) {
  1798.     fnd_item(p,k+1,buf);
  1799.     cc[k]=strevalue(buf);
  1800.   }
  1801.   collapse_item(p,3);
  1802.   if(cc[0]==-1 && cc[1]==-1 && cc[2]==-255) {
  1803.  
  1804.     if(brushed && o->subst&1) return gdBrushed;
  1805.     else return color_black;
  1806.   }
  1807.   if(cc[0]==-1 && cc[1]==-255 && cc[2]==-1) {
  1808.     if(styled && o->subst&2)  return gdStyled;
  1809.     else return color_black;
  1810.   }
  1811.   if(cc[0]==-255 && cc[1]==-1 && cc[2]==-1) {
  1812.     if(tiled && o->fill_tag==1)  return gdTiled;
  1813.     else return color_black;
  1814.   }
  1815.   return getcolor(cc[0],cc[1],cc[2]);
  1816. }
  1817.  
  1818. /* Routine to parse parameters */
  1819. int parse_parms(char *p,objparm *pm,struct objtab *o)
  1820. {
  1821.   char buf[MAX_LINELEN+1];
  1822.   char *p1, *p2;
  1823.   int j,t,c,c1,c2;
  1824.  
  1825.   c=o->color_pos;c1=c2=0;
  1826.   pm->color[0]=pm->color[1]=0;
  1827.   if(c>0) c1=c;
  1828.   if(c<0) c2=-c;
  1829.   c=c1+c2;
  1830.   t=itemnum(p);if(t<o->required_parms+3*c) return -1;
  1831.   if(c1>0 && t>o->required_parms+3*c) t=o->required_parms+3*c;
  1832.   pm->pcnt=t-3*c;
  1833.   if(pm->pcnt>MAX_PARMS) pm->pcnt=MAX_PARMS;
  1834.   if(c2>0) {
  1835.     for(j=0;j<2 && j<c2; j++) pm->color[j]=calc_color(p,o);
  1836.   }
  1837.   snprintf(buf,sizeof(buf),"%s",p);
  1838.   for(j=0, p1=buf; j<pm->pcnt; j++, p1=p2) {
  1839.     p2=find_item_end(p1); if(*p2) *p2++=0;
  1840.     p1=find_word_start(p1);
  1841.     if(*p1) pm->pd[j]=strevalue(p1); else pm->pd[j]=0;
  1842.     if(!isfinite(pm->pd[j])) {
  1843.         if(j<o->required_parms) return -1;
  1844.         else {pm->pd[j]=0;break;}
  1845.     }
  1846.   }
  1847.   collapse_item(p,o->required_parms);
  1848.   if(c1>0) {
  1849.     for(j=0;j<c1 && j<2; j++) pm->color[j]=calc_color(p,o);
  1850.   }
  1851.   if(width>1 && o->subst&1 && pm->color[0]!=gdBrushed
  1852.       && pm->color[0]!=gdStyled && pm->color[0]!=gdTiled) {
  1853.     pm->color[1]=pm->color[0];
  1854.     pm->color[0]=widthcolor(width,pm->color[0]);
  1855.   }
  1856.   pm->fill=o->fill_tag;
  1857.   if(o->required_parms!=0){
  1858.     if(dashed) {
  1859.       if(pm->fill==1) pm->fill=2;
  1860.       if(pm->fill==0) pm->fill=-1;
  1861.        if(noreset==0) dashed=0;
  1862.     }
  1863.     if(filled) {
  1864.       if(pm->fill==-1) pm->fill=2;
  1865.       if(pm->fill==0) pm->fill=1;
  1866.       if(noreset==0) filled=0;
  1867.     }
  1868.   }
  1869.   ovlstrcpy(pm->str,p); return 0;
  1870. }
  1871.  
  1872. /* Create the struct objparm pm corresponding to the objparm p
  1873.  * Execute a command. Returns 0 if OK.
  1874.  */
  1875. int obj_main(char *p)
  1876. {
  1877.   int i;
  1878.   char *pp,*name,*pt;
  1879.   char tbuf2[MAX_LINELEN+1];
  1880.   struct objparm pm;
  1881.  
  1882.   p=find_word_start(p);
  1883.   if(*p==exec_prefix_char || *p==0) return 0; /* comment */
  1884.   name=p;
  1885.   pp=find_name_end(p);
  1886.   pt=find_word_start(pp); if(*pt=='=') *pt=' ';
  1887.   if(*pt==':' && *(pt+1)=='=') *pt=*(pt+1)=' ';
  1888.   pp=find_word_end(p);
  1889.   if(*pp!=0) {
  1890.     *(pp++)=0; pp=find_word_start(pp);
  1891.   }
  1892.   if(strlen(p)==1 || (strlen(p)==2 && isdigit(*(p+1)))) {
  1893.     setvar(p,strevalue(pp)); return 0;
  1894.   }
  1895.   /* search the number of the object */
  1896.   i=search_list(objtab,obj_no,sizeof(objtab[0]),name);
  1897.   if(i<0) {
  1898.     fly_error("bad_cmd"); return 1;
  1899.   }
  1900.   if(image==NULL && (objtab[i].color_pos || objtab[i].required_parms>2)) {
  1901.     fly_error("image_not_defined"); return 1;
  1902.   }
  1903.   ovlstrcpy(tbuf2,pp);
  1904.   if(objtab[i].color_pos || objtab[i].routine==obj_setstyle) {
  1905.     substit(tbuf2);
  1906.   }
  1907.   if(parse_parms(tbuf2,&pm,objtab+i)!=0) fly_error("bad_parms");
  1908.   else objtab[i].routine(&pm);
  1909.   return 0;
  1910. }
  1911.