Subversion Repositories wimsdev

Rev

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