Subversion Repositories wimsdev

Rev

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