5 Extension module for the rfxswf library.
\r
6 Part of the swftools package.
\r
8 Copyright (c) 2001 Rainer Böhme <rfxswf@reflex-studio.de>
\r
10 This file is distributed under the GPL, see file COPYING for details
\r
14 #define SF_MOVETO 0x01
\r
15 #define SF_FILL0 0x02
\r
16 #define SF_FILL1 0x04
\r
17 #define SF_LINE 0x08
\r
18 #define SF_NEWSTYLE 0x10
\r
20 #define FILL_SOLID 0x00
\r
21 #define FILL_LINEAR 0x10 // Gradient
\r
22 #define FILL_RADIAL 0x12
\r
23 #define FILL_TILED 0x40 // Bitmap
\r
24 #define FILL_CLIPPED 0x41
\r
26 void ShapeFree(LPSHAPE s)
\r
28 { if (s->linestyle.data) free(s->linestyle.data);
\r
29 s->linestyle.data = NULL;
\r
31 if (s->fillstyle.data) free(s->fillstyle.data);
\r
32 s->fillstyle.data = NULL;
\r
34 if (s->data) free(s->data);
\r
40 int NewShape(LPSHAPE * s)
\r
43 sh = (LPSHAPE)malloc(sizeof(SHAPE)); s[0] = sh;
\r
44 if (sh) memset(sh,0x00,sizeof(SHAPE));
\r
48 int GetSimpleShape(LPTAG t,LPSHAPE * s) // without Linestyle/Fillstyle Record
\r
54 if (FAILED(NewShape(s))) return -1;
\r
58 sh->bits.fill = (U16)GetBits(t,4);
\r
59 sh->bits.line = (U16)GetBits(t,4);
\r
60 bitl = 0; end = 0; pos = GetTagPos(t);
\r
63 { int edge = GetBits(t,1); bitl+=1;
\r
66 if (GetBits(t,1)) // Line
\r
67 { U16 nbits = GetBits(t,4)+2;
\r
70 if (GetBits(t,1)) // x/y Line
\r
76 { GetBits(t,nbits+1);
\r
81 { U16 nbits = GetBits(t,4)+2;
\r
93 { U16 flags = GetBits(t,5); bitl+=5;
\r
96 if (flags&SF_MOVETO)
\r
97 { U16 nbits = GetBits(t,5); bitl+=5;
\r
103 if (flags&SF_FILL0)
\r
104 { GetBits(t,sh->bits.fill);
\r
105 bitl+=sh->bits.fill;
\r
108 if (flags&SF_FILL1)
\r
109 { GetBits(t,sh->bits.fill);
\r
110 bitl+=sh->bits.fill;
\r
114 { GetBits(t,sh->bits.line);
\r
115 bitl+=sh->bits.line;
\r
118 if (flags&SF_NEWSTYLE)
\r
119 { fprintf(stderr,"Can't process extended styles in shape.\n");
\r
128 if (sh->data) free(sh->data);
\r
129 sh->data = (U8*)malloc(len);
\r
132 { sh->bitlen = bitl;
\r
133 GetBlock(t,sh->data,len);
\r
140 int SetSimpleShape(LPTAG t,LPSHAPE s) // without Linestyle/Fillstyle Record
\r
144 l = (s->bitlen+7)/8;
\r
147 { ResetBitcount(t);
\r
149 SetBits(t,s->bits.fill,4);
\r
150 SetBits(t,s->bits.line,4);
\r
151 SetBlock(t,s->data,l);
\r
158 int SetFillStyle(LPTAG t,LPFILLSTYLE f)
\r
159 { if ((!t)||(!f)) return -1;
\r
162 // no gradients yet!
\r
166 if (GetTagID(t)!=ST_DEFINESHAPE3) SetRGB(t,&f->color);
\r
167 else SetRGBA(t,&f->color);
\r
172 SetU16(t,f->id_bitmap);
\r
173 SetMatrix(t,&f->m);
\r
180 int SetLineStyle(LPTAG t,LPLINESTYLE l)
\r
181 { if ((!l)||(!t)) return -1;
\r
182 SetU16(t,l->width);
\r
184 if (GetTagID(t)!=ST_DEFINESHAPE3) SetRGB(t,&l->color);
\r
185 else SetRGBA(t,&l->color);
\r
190 int SetShapeStyleCount(LPTAG t,U16 n)
\r
202 int SetShapeStyles(LPTAG t,LPSHAPE s)
\r
207 l += SetShapeStyleCount(t,s->fillstyle.n);
\r
209 for (i=0;i<s->fillstyle.n;i++)
\r
210 l+=SetFillStyle(t,&s->fillstyle.data[i]);
\r
212 l += SetShapeStyleCount(t,s->linestyle.n);
\r
214 for (i=0;i<s->linestyle.n;i++)
\r
215 l+=SetLineStyle(t,&s->linestyle.data[i]);
\r
220 int ShapeCountBits(LPSHAPE s,U8 * fbits,U8 * lbits)
\r
221 { if (!s) return -1;
\r
223 s->bits.fill = CountBits(s->fillstyle.n,0);
\r
224 s->bits.line = CountBits(s->linestyle.n,0);
\r
226 if (fbits) fbits[0] = s->bits.fill;
\r
227 if (lbits) lbits[0] = s->bits.line;
\r
232 int SetShapeBits(LPTAG t,LPSHAPE s)
\r
233 { if ((!t)||(!s)) return -1;
\r
235 SetBits(t,s->bits.fill,4);
\r
236 SetBits(t,s->bits.line,4);
\r
240 int SetShapeHeader(LPTAG t,LPSHAPE s)
\r
242 res = SetShapeStyles(t,s);
\r
243 if (res>=0) res = ShapeCountBits(s,NULL,NULL);
\r
244 if (res>=0) res = SetShapeBits(t,s);
\r
248 int ShapeExport(int handle,LPSHAPE s) // without Linestyle/Fillstyle Record
\r
255 if (write(handle,s,sizeof(SHAPE))!=sizeof(SHAPE)) return -1;
\r
257 // Fillstyle, Linestyle ...
\r
260 { int ll = (s->bitlen+7)/8;
\r
263 if (write(handle,s->data,ll)!=ll) return -1;
\r
269 int ShapeImport(int handle,LPSHAPE * shape)
\r
272 if (handle<0) return -1;
\r
274 s = (LPSHAPE)malloc(sizeof(SHAPE)); shape[0] = s;
\r
277 if (read(handle,s,sizeof(SHAPE))!=sizeof(SHAPE))
\r
284 { int ll = (s->bitlen+7)/8;
\r
285 s->data = (U8*)malloc(ll);
\r
291 if (read(handle,s->data,ll)!=ll)
\r
302 int ShapeAddFillStyle(LPSHAPE s,U8 type,LPMATRIX m,LPRGBA color,U16 id_bitmap)
\r
312 def_c.r = def_c.g = def_c.b = 0;
\r
321 if (s->fillstyle.data)
\r
322 { LPFILLSTYLE new = (LPFILLSTYLE)realloc(s->fillstyle.data,(s->fillstyle.n+1)*sizeof(FILLSTYLE));
\r
323 if (!new) return -1;
\r
324 s->fillstyle.data = new;
\r
327 { s->fillstyle.data = (LPFILLSTYLE)malloc(sizeof(FILLSTYLE));
\r
328 s->fillstyle.n = 0;
\r
329 if (!s->fillstyle.data) return -1;
\r
332 // set fillstyle (no gradients yet!)
\r
334 s->fillstyle.data[s->fillstyle.n].type = type;
\r
335 s->fillstyle.data[s->fillstyle.n].id_bitmap = id_bitmap;
\r
336 memcpy(&s->fillstyle.data[s->fillstyle.n].m,m,sizeof(MATRIX));
\r
337 memcpy(&s->fillstyle.data[s->fillstyle.n].color,color,sizeof(RGBA));
\r
339 return (++s->fillstyle.n);
\r
342 int ShapeAddSolidFillStyle(LPSHAPE s,LPRGBA color)
\r
343 { return ShapeAddFillStyle(s,FILL_SOLID,NULL,color,0);
\r
346 int ShapeAddBitmapFillStyle(LPSHAPE s,LPMATRIX m,U16 id_bitmap,int clip)
\r
347 { return ShapeAddFillStyle(s,clip?FILL_CLIPPED:FILL_TILED,m,NULL,id_bitmap);
\r
350 int ShapeAddLineStyle(LPSHAPE s,U16 width,LPRGBA color)
\r
356 def.r = def.g = def.b = 0;
\r
358 if (s->linestyle.data)
\r
359 { LPLINESTYLE new = (LPLINESTYLE)realloc(s->linestyle.data,(s->linestyle.n+1)*sizeof(LINESTYLE));
\r
360 if (!new) return -1;
\r
361 s->linestyle.data = new;
\r
364 { s->linestyle.data = (LPLINESTYLE)malloc(sizeof(LINESTYLE));
\r
365 s->linestyle.n = 0;
\r
366 if (!s->linestyle.data) return -1;
\r
369 s->linestyle.data[s->linestyle.n].width = width;
\r
370 memcpy(&s->linestyle.data[s->linestyle.n].color,color,sizeof(RGBA));
\r
372 return (++s->linestyle.n);
\r
375 int ShapeSetMove(LPTAG t,LPSHAPE s,S32 x,S32 y)
\r
379 SetBits(t,SF_MOVETO,5);
\r
381 b = CountBits(x,0);
\r
382 b = CountBits(y,b);
\r
395 int ShapeSetStyle(LPTAG t,LPSHAPE s,U16 line,U16 fill0,U16 fill1)
\r
396 { if ((!t)||(!s)) return -1;
\r
399 SetBits(t,(line?SF_LINE:0)|(fill0?SF_FILL0:0)|(fill1?SF_FILL1:0),5);
\r
401 if (fill0) SetBits(t,fill0,s->bits.fill);
\r
402 if (fill1) SetBits(t,fill1,s->bits.fill);
\r
403 if (line) SetBits(t,line ,s->bits.line);
\r
408 /* TODO: sometimes we want to set fillstyle 0, as that's the empty fill
\r
409 used for line drawings. At the moment, we can't, as 0 fill be considered
\r
410 nonexistent and therefore not set.
\r
411 these defines are a workaround (they also reduce the maximal number of
\r
412 fill styles to 32768)
\r
414 #define FILL_RESET 0x8000
\r
415 #define LINE_RESET 0x8000
\r
416 int ShapeSetAll(LPTAG t,LPSHAPE s,S32 x,S32 y,U16 line,U16 fill0,U16 fill1)
\r
418 if ((!t)||(!s)) return -1;
\r
421 SetBits(t,SF_MOVETO|(line?SF_LINE:0)|(fill0?SF_FILL0:0)|(fill1?SF_FILL1:0),5);
\r
423 b = CountBits(x,0);
\r
424 b = CountBits(y,b);
\r
431 if (fill0) SetBits(t,fill0,s->bits.fill);
\r
432 if (fill1) SetBits(t,fill1,s->bits.fill);
\r
433 if (line) SetBits(t,line ,s->bits.line);
\r
438 int ShapeSetEnd(LPTAG t)
\r
439 { if (!t) return -1;
\r
444 int ShapeSetLine(LPTAG t,LPSHAPE s,S32 x,S32 y)
\r
447 SetBits(t,3,2); // Straight Edge
\r
449 if ((!s)||(s->px!=x)||(s->py!=y))
\r
450 { b = CountBits(x,2);
\r
451 b = CountBits(y,b);
\r
464 { b = CountBits(y,2);
\r
471 { b = CountBits(x,2);
\r
480 int ShapeSetCurve(LPTAG t,LPSHAPE s,S32 x,S32 y,S32 ax,S32 ay)
\r
486 b = CountBits(ax,2);
\r
487 b = CountBits(ay,b);
\r
488 b = CountBits(x,b);
\r
489 b = CountBits(y,b);
\r
504 int ShapeSetCircle(LPTAG t,LPSHAPE s,S32 x,S32 y,S32 rx,S32 ry)
\r
505 { double C1 = 0.2930;
\r
506 double C2 = 0.4140;
\r
507 double begin = 0.7070;
\r
511 ShapeSetMove(t,s,x+begin*rx,y+begin*ry);
\r
512 ShapeSetCurve(t,s, -C1*rx, C1*ry, -C2*rx, 0);
\r
513 ShapeSetCurve(t,s, -C2*rx, 0, -C1*rx, -C1*ry);
\r
514 ShapeSetCurve(t,s, -C1*rx, -C1*ry, 0, -C2*ry);
\r
515 ShapeSetCurve(t,s, 0, -C2*ry, C1*rx, -C1*ry);
\r
516 ShapeSetCurve(t,s, C1*rx, -C1*ry, C2*rx, 0);
\r
517 ShapeSetCurve(t,s, C2*rx, 0, C1*rx, C1*ry);
\r
518 ShapeSetCurve(t,s, C1*rx, C1*ry, 0, C2*ry);
\r
519 ShapeSetCurve(t,s, 0, C2*ry, -C1*rx, C1*ry);
\r