Subversion Repositories wimsdev

Rev

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