Subversion Repositories wimsdev

Rev

Rev 18301 | Rev 18317 | Go to most recent revision | Details | Compare with Previous | Last modification | View Log | RSS feed

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