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