Subversion Repositories wimsdev

Rev

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

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