Subversion Repositories wimsdev

Rev

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