Subversion Repositories wimsdev

Rev

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