Subversion Repositories wimsdev

Rev

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