Subversion Repositories wimsdev

Rev

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