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 program is free software; you can redistribute it and/or modify
11 it under the terms of the GNU General Public License as published by
12 the Free Software Foundation; either version 2 of the License, or
13 (at your option) any later version.
15 This program is distributed in the hope that it will be useful,
16 but WITHOUT ANY WARRANTY; without even the implied warranty of
17 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 GNU General Public License for more details.
20 You should have received a copy of the GNU General Public License
21 along with this program; if not, write to the Free Software
22 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA */
24 #include "../rfxswf.h"
26 #define SF_MOVETO 0x01
30 #define SF_NEWSTYLE 0x10
32 void swf_ShapeFree(SHAPE * s)
36 if (s->linestyle.data) rfx_free(s->linestyle.data);
37 s->linestyle.data = NULL;
39 if (s->fillstyle.data) rfx_free(s->fillstyle.data);
40 s->fillstyle.data = NULL;
42 if (s->data) rfx_free(s->data);
47 int swf_ShapeNew(SHAPE * * s)
51 sh = (SHAPE *)rfx_calloc(sizeof(SHAPE));
56 int swf_GetSimpleShape(TAG * t,SHAPE * * s) // without Linestyle/Fillstyle Record
62 if (FAILED(swf_ShapeNew(s))) return -1;
66 sh->bits.fill = (U16)swf_GetBits(t,4);
67 sh->bits.line = (U16)swf_GetBits(t,4);
68 bitl = 0; end = 0; pos = swf_GetTagPos(t);
71 { int edge = swf_GetBits(t,1); bitl+=1;
74 if (swf_GetBits(t,1)) // Line
75 { U16 nbits = swf_GetBits(t,4)+2;
78 if (swf_GetBits(t,1)) // x/y Line
79 { swf_GetBits(t,nbits);
84 { swf_GetBits(t,nbits+1);
89 { U16 nbits = swf_GetBits(t,4)+2;
101 { U16 flags = swf_GetBits(t,5); bitl+=5;
105 { U16 nbits = swf_GetBits(t,5); bitl+=5;
106 swf_GetBits(t,nbits);
107 swf_GetBits(t,nbits);
112 { swf_GetBits(t,sh->bits.fill);
117 { swf_GetBits(t,sh->bits.fill);
122 { swf_GetBits(t,sh->bits.line);
126 if (flags&SF_NEWSTYLE)
127 { fprintf(stderr,"RFXSWF: Can't process extended styles in shape.\n");
133 swf_SetTagPos(t,pos);
136 if (sh->data) rfx_free(sh->data);
137 sh->data = (U8*)rfx_alloc(len);
141 swf_GetBlock(t,sh->data,len);
148 int swf_SetSimpleShape(TAG * t,SHAPE * s) // without Linestyle/Fillstyle Record
155 { swf_ResetWriteBits(t);
157 swf_SetBits(t,s->bits.fill,4);
158 swf_SetBits(t,s->bits.line,4);
159 swf_SetBlock(t,s->data,l);
161 swf_ResetWriteBits(t);
166 int swf_SetFillStyle(TAG * t,FILLSTYLE * f)
167 { if ((!t)||(!f)) return -1;
168 swf_SetU8(t,f->type);
172 if (swf_GetTagID(t)!=ST_DEFINESHAPE3) swf_SetRGB(t,&f->color);
173 else swf_SetRGBA(t,&f->color);
178 swf_SetU16(t,f->id_bitmap);
179 swf_SetMatrix(t,&f->m);
183 swf_SetMatrix(t,&f->m);
184 swf_SetGradient(t,&f->gradient,/*alpha?*/t->id==ST_DEFINESHAPE3?1:0);
191 int swf_SetLineStyle(TAG * t,LINESTYLE * l)
192 { if ((!l)||(!t)) return -1;
193 swf_SetU16(t,l->width);
195 if (swf_GetTagID(t)!=ST_DEFINESHAPE3) swf_SetRGB(t,&l->color);
196 else swf_SetRGBA(t,&l->color);
201 int swf_SetShapeStyleCount(TAG * t,U16 n)
208 { swf_SetU8(t,(U8)n);
213 int swf_SetShapeStyles(TAG * t,SHAPE * s)
218 l += swf_SetShapeStyleCount(t,s->fillstyle.n);
220 for (i=0;i<s->fillstyle.n;i++)
221 l+=swf_SetFillStyle(t,&s->fillstyle.data[i]);
223 l += swf_SetShapeStyleCount(t,s->linestyle.n);
225 for (i=0;i<s->linestyle.n;i++)
226 l+=swf_SetLineStyle(t,&s->linestyle.data[i]);
231 int swf_ShapeCountBits(SHAPE * s,U8 * fbits,U8 * lbits)
233 s->bits.fill = swf_CountUBits(s->fillstyle.n, 0);
234 s->bits.line = swf_CountUBits(s->linestyle.n, 0);
235 if (fbits) fbits[0] = s->bits.fill;
236 if (lbits) lbits[0] = s->bits.line;
240 int swf_SetShapeBits(TAG * t,SHAPE * s)
241 { if ((!t)||(!s)) return -1;
242 swf_ResetWriteBits(t);
243 swf_SetBits(t,s->bits.fill,4);
244 swf_SetBits(t,s->bits.line,4);
248 int swf_SetShapeHeader(TAG * t,SHAPE * s)
250 res = swf_SetShapeStyles(t,s);
251 if (res>=0) res = swf_ShapeCountBits(s,NULL,NULL);
252 if (res>=0) res = swf_SetShapeBits(t,s);
256 int swf_ShapeAddFillStyle(SHAPE * s,U8 type,MATRIX * m,RGBA * color,U16 id_bitmap, GRADIENT*gradient)
267 def_c.r = def_c.g = def_c.b = 0;
271 swf_GetMatrix(NULL,m);
276 swf_GetGradient(NULL, gradient, 1);
281 if (s->fillstyle.data)
282 { FILLSTYLE * xnew = (FILLSTYLE *)rfx_realloc(s->fillstyle.data,(s->fillstyle.n+1)*sizeof(FILLSTYLE));
283 if (!xnew) return -1;
284 s->fillstyle.data = xnew;
287 { s->fillstyle.data = (FILLSTYLE *)rfx_alloc(sizeof(FILLSTYLE));
289 if (!s->fillstyle.data) return -1;
294 s->fillstyle.data[s->fillstyle.n].type = type;
295 s->fillstyle.data[s->fillstyle.n].id_bitmap = id_bitmap;
296 memcpy(&s->fillstyle.data[s->fillstyle.n].m,m,sizeof(MATRIX));
297 memcpy(&s->fillstyle.data[s->fillstyle.n].color,color,sizeof(RGBA));
298 memcpy(&s->fillstyle.data[s->fillstyle.n].gradient,gradient,sizeof(GRADIENT));
300 return (++s->fillstyle.n);
302 int swf_ShapeAddFillStyle2(SHAPE * s,FILLSTYLE*fs)
304 return swf_ShapeAddFillStyle(s, fs->type, &fs->m, &fs->color, fs->id_bitmap, &fs->gradient);
307 int swf_ShapeAddSolidFillStyle(SHAPE * s,RGBA * color)
308 { return swf_ShapeAddFillStyle(s,FILL_SOLID,NULL,color,0,0);
311 int swf_ShapeAddBitmapFillStyle(SHAPE * s,MATRIX * m,U16 id_bitmap,int clip)
312 { return swf_ShapeAddFillStyle(s,clip?FILL_CLIPPED:FILL_TILED,m,NULL,id_bitmap,0);
315 int swf_ShapeAddGradientFillStyle(SHAPE * s,MATRIX * m,GRADIENT* gradient,int radial)
316 { return swf_ShapeAddFillStyle(s,radial?FILL_RADIAL:FILL_LINEAR,m,NULL,0,gradient);
319 int swf_ShapeAddLineStyle(SHAPE * s,U16 width,RGBA * color)
325 def.r = def.g = def.b = 0;
327 if (s->linestyle.data)
328 { LINESTYLE * xnew = (LINESTYLE *)rfx_realloc(s->linestyle.data,(s->linestyle.n+1)*sizeof(LINESTYLE));
329 if (!xnew) return -1;
330 s->linestyle.data = xnew;
333 { s->linestyle.data = (LINESTYLE *)rfx_alloc(sizeof(LINESTYLE));
335 if (!s->linestyle.data) return -1;
338 s->linestyle.data[s->linestyle.n].width = width;
339 memcpy(&s->linestyle.data[s->linestyle.n].color,color,sizeof(RGBA));
341 return (++s->linestyle.n);
344 int swf_ShapeSetMove(TAG * t,SHAPE * s,S32 x,S32 y)
348 swf_SetBits(t,SF_MOVETO,5);
350 b = swf_CountBits(x,0);
351 b = swf_CountBits(y,b);
354 fprintf(stderr, "Warning: bad moveTo (%f,%f)\n", x/20.0, y/20.0);
365 int swf_ShapeSetStyle(TAG * t,SHAPE * s,int line,int fill0,int fill1)
366 { if ((!t)||(!s)) return -1;
369 swf_SetBits(t,(line?SF_LINE:0)|(fill0?SF_FILL0:0)|(fill1?SF_FILL1:0),5);
371 if (fill0) swf_SetBits(t,fill0,s->bits.fill);
372 if (fill1) swf_SetBits(t,fill1,s->bits.fill);
373 if (line) swf_SetBits(t,line ,s->bits.line);
378 /* TODO: sometimes we want to set fillstyle 0, as that's the empty fill
379 used for line drawings. At the moment, we can't, as 0 fill be considered
380 nonexistent and therefore not set.
381 these defines are a workaround (they also reduce the maximal number of
382 fill styles to 32768)
384 #define UNDEFINED_COORD 0x7fffffff
386 int swf_ShapeSetAll(TAG * t,SHAPE * s,S32 x,S32 y,int line,int fill0,int fill1)
389 if ((!t)||(!s)) return -1;
391 if(x!=UNDEFINED_COORD || y!=UNDEFINED_COORD)
395 swf_SetBits(t,(hasmove?SF_MOVETO:0)|(line?SF_LINE:0)|(fill0?SF_FILL0:0)|(fill1?SF_FILL1:0),5);
398 b = swf_CountBits(x,0);
399 b = swf_CountBits(y,b);
405 if (fill0) swf_SetBits(t,fill0,s->bits.fill);
406 if (fill1) swf_SetBits(t,fill1,s->bits.fill);
407 if (line) swf_SetBits(t,line ,s->bits.line);
412 int swf_ShapeSetEnd(TAG * t)
415 swf_ResetWriteBits(t);
419 int swf_ShapeSetLine(TAG * t,SHAPE * s,S32 x,S32 y)
424 b = swf_CountBits(x,2);
425 b = swf_CountBits(y,b);
429 /* do not split into more than 64 segments. If the line is *that* long, something's broken */
430 fprintf(stderr, "Warning: Line to %.2f,%.2f is too long (%d bits)\n", (double)x,(double)y, b);
435 if(x>=0) { x1 = x/2;x2 = (x+1)/2;}
436 else { x1 = x/2;x2 = (x-1)/2;}
437 if(y>=0) { y1 = y/2;y2 = (y+1)/2;}
438 else { y1 = y/2;y2 = (y-1)/2;}
439 swf_ShapeSetLine(t, s, x1,y1);
440 swf_ShapeSetLine(t, s, x2,y2);
445 if(x!=0 && y!=0) { //(!s)||((x!=0)&&(y!=0)))
446 swf_SetBits(t,3,2); // Straight Edge
447 swf_SetBits(t, b-2, 4); //Number of Bits in x/y
448 swf_SetBits(t,1,1); // Diagonal
452 swf_SetBits(t,3,2); // Straight Edge
453 swf_SetBits(t, b-2, 4); //Number of Bits in y
454 swf_SetBits(t,1,2); // Vertical
457 swf_SetBits(t,3,2); // Straight Edge
458 swf_SetBits(t, b-2, 4); //Number of Bits in x
459 swf_SetBits(t,0,2); // Horizontal
465 int swf_ShapeSetCurve(TAG * t,SHAPE * s,S32 x,S32 y,S32 ax,S32 ay)
470 b = swf_CountBits(ax,2);
471 b = swf_CountBits(ay,b);
472 b = swf_CountBits(x,b);
473 b = swf_CountBits(y,b);
476 fprintf(stderr, "Bit overflow in swf_ShapeSetCurve- %d (%d,%d,%d,%d)\n", b, ax,ay,x,y);
477 return swf_ShapeSetLine(t, s, x+ax, y+ay);
481 swf_SetBits(t,b-2,4);
489 int swf_ShapeSetCircle(TAG * t,SHAPE * s,S32 x,S32 y,S32 rx,S32 ry)
490 { double C1 = 0.2930;
492 double begin = 0.7070;
496 swf_ShapeSetMove(t,s,x+begin*rx,y+begin*ry);
497 swf_ShapeSetCurve(t,s, -C1*rx, C1*ry, -C2*rx, 0);
498 swf_ShapeSetCurve(t,s, -C2*rx, 0, -C1*rx, -C1*ry);
499 swf_ShapeSetCurve(t,s, -C1*rx, -C1*ry, 0, -C2*ry);
500 swf_ShapeSetCurve(t,s, 0, -C2*ry, C1*rx, -C1*ry);
501 swf_ShapeSetCurve(t,s, C1*rx, -C1*ry, C2*rx, 0);
502 swf_ShapeSetCurve(t,s, C2*rx, 0, C1*rx, C1*ry);
503 swf_ShapeSetCurve(t,s, C1*rx, C1*ry, 0, C2*ry);
504 swf_ShapeSetCurve(t,s, 0, C2*ry, -C1*rx, C1*ry);
509 void dummycallback1(TAG*tag, int x, void*y)
514 void enumerateUsedIDs_styles(TAG * tag, void (*callback)(TAG*, int, void*), void*callback_data, int num, int morph);
516 static void parseFillStyle(FILLSTYLE*dest, TAG*tag, int num)
518 int type = swf_GetU8(tag); //type
523 swf_GetRGBA(tag, &dest->color);
525 swf_GetRGB(tag, &dest->color);
527 else if(type == 0x10 || type == 0x11 || type == 0x12 || type == 0x13)
529 /* linear/radial gradient fill */
530 swf_ResetReadBits(tag);
531 swf_GetMatrix(tag, &dest->m);
532 swf_ResetReadBits(tag);
533 swf_GetGradient(tag, &dest->gradient, num>=3?1:0);
537 else if(type == 0x40 || type == 0x41 || type == 0x42 || type == 0x43)
540 swf_ResetReadBits(tag);
541 dest->id_bitmap = swf_GetU16(tag); //id
542 swf_ResetReadBits(tag); //?
543 swf_GetMatrix(tag, &dest->m);
546 fprintf(stderr, "rfxswf:swfshape.c Unknown fillstyle:0x%02x in tag %02d\n",type, tag->id);
549 static int parseFillStyleArray(TAG*tag, SHAPE2*shape)
554 int fillstylestart = shape->numfillstyles;
555 int linestylestart = shape->numlinestyles;
557 if(tag->id == ST_DEFINESHAPE)
559 else if(tag->id == ST_DEFINESHAPE2)
561 else if(tag->id == ST_DEFINESHAPE3)
563 else if(tag->id == ST_DEFINESHAPE4)
566 count = swf_GetU8(tag);
567 if(count == 0xff && num>1) // defineshape2,3 only
568 count = swf_GetU16(tag);
570 shape->numfillstyles += count;
571 if(shape->numfillstyles) {
572 shape->fillstyles = (FILLSTYLE*)rfx_realloc(shape->fillstyles, sizeof(FILLSTYLE)*shape->numfillstyles);
574 for(t=fillstylestart;t<shape->numfillstyles;t++) {
575 parseFillStyle(&shape->fillstyles[t], tag, num);
579 swf_ResetReadBits(tag);
580 count = swf_GetU8(tag); // line style array
582 count = swf_GetU16(tag);
584 shape->numlinestyles += count;
586 shape->linestyles = (LINESTYLE*)rfx_realloc(shape->linestyles, sizeof(LINESTYLE)*shape->numlinestyles);
587 /* TODO: should we start with 1 and insert a correct definition of the
588 "built in" linestyle 0? */
589 for(t=linestylestart;t<shape->numlinestyles;t++)
592 shape->linestyles[t].width = swf_GetU16(tag);
595 U16 flags = swf_GetU16(tag);
596 if((flags & 0x30) == 0x20)
597 swf_GetU16(tag); // miter limit
599 fprintf(stderr, "Warning: Filled strokes parsing not yet fully supported\n");
606 parseFillStyle(&f, tag, num);
607 shape->linestyles[t].color = f.color;
610 swf_GetRGBA(tag, &shape->linestyles[t].color);
612 swf_GetRGB(tag, &shape->linestyles[t].color);
620 /* todo: merge this with swf_GetSimpleShape */
621 static SHAPELINE* swf_ParseShapeData(U8*data, int bits, int fillbits, int linebits, int version, SHAPE2*shape2)
624 SHAPELINE*lines = &_lines;
635 memset(tag, 0, sizeof(TAG));
637 tag->len = tag->memsize = (bits+7)/8;
639 tag->id = version==1?ST_DEFINESHAPE:(version==2?ST_DEFINESHAPE2:(version==3?ST_DEFINESHAPE3:ST_DEFINESHAPE4));
644 flags = swf_GetBits(tag, 1);
645 if(!flags) { //style change
646 flags = swf_GetBits(tag, 5);
650 int n = swf_GetBits(tag, 5);
651 x = swf_GetSBits(tag, n); //x
652 y = swf_GetSBits(tag, n); //y
655 fill0 = swf_GetBits(tag, fillbits) + fillstyleadd;
657 fill1 = swf_GetBits(tag, fillbits) + fillstyleadd;
659 line = swf_GetBits(tag, linebits) + linestyleadd;
662 fprintf(stderr, "rfxswf: Error: Additional fillstyles not supported\n");fflush(stderr);
663 enumerateUsedIDs_styles(tag, dummycallback1, 0, version, 0);
665 linestyleadd = shape2->numlinestyles;
666 fillstyleadd = shape2->numfillstyles;
667 if(!parseFillStyleArray(tag, shape2))
670 fillbits = swf_GetBits(tag, 4);
671 linebits = swf_GetBits(tag, 4);
674 lines->next = (SHAPELINE*)rfx_alloc(sizeof(SHAPELINE));
676 lines->type = moveTo;
679 lines->sx = lines->sy = 0;
680 lines->fillstyle0 = fill0;
681 lines->fillstyle1 = fill1;
682 lines->linestyle = line;
686 flags = swf_GetBits(tag, 1);
687 if(flags) { //straight edge
688 int n = swf_GetBits(tag, 4) + 2;
689 if(swf_GetBits(tag, 1)) { //line flag
690 x += swf_GetSBits(tag, n); //delta x
691 y += swf_GetSBits(tag, n); //delta y
693 int v=swf_GetBits(tag, 1);
695 d = swf_GetSBits(tag, n); //vert/horz
699 lines->next = (SHAPELINE*)rfx_alloc(sizeof(SHAPELINE));
701 lines->type = lineTo;
704 lines->sx = lines->sy = 0;
705 lines->fillstyle0 = fill0;
706 lines->fillstyle1 = fill1;
707 lines->linestyle = line;
709 } else { //curved edge
710 int n = swf_GetBits(tag, 4) + 2;
712 x += swf_GetSBits(tag, n);
713 y += swf_GetSBits(tag, n);
716 x += swf_GetSBits(tag, n);
717 y += swf_GetSBits(tag, n);
719 lines->next = (SHAPELINE*)rfx_alloc(sizeof(SHAPELINE));
721 lines->type = splineTo;
726 lines->fillstyle0 = fill0;
727 lines->fillstyle1 = fill1;
728 lines->linestyle = line;
736 SRECT swf_GetShapeBoundingBox(SHAPE2*shape2)
739 SHAPELINE*l = shape2->lines;
742 r.xmin = r.ymin = SCOORD_MAX;
743 r.xmax = r.ymax = SCOORD_MIN;
748 t1 = shape2->linestyles[l->linestyle - 1].width*3/2;
753 if(l->type == lineTo || l->type == splineTo)
756 if(lastx - t1 < r.xmin) r.xmin = lastx - t1;
757 if(lasty - t1 < r.ymin) r.ymin = lasty - t1;
758 if(lastx + t1 > r.xmax) r.xmax = lastx + t1;
759 if(lasty + t1 > r.ymax) r.ymax = lasty + t1;
760 if(l->x - t1 < r.xmin) r.xmin = l->x - t1;
761 if(l->y - t1 < r.ymin) r.ymin = l->y - t1;
762 if(l->x + t1 > r.xmax) r.xmax = l->x + t1;
763 if(l->y + t1 > r.ymax) r.ymax = l->y + t1;
764 if(l->type == splineTo) {
765 if(l->sx - t1 < r.xmin) r.xmin = l->sx - t1;
766 if(l->sy - t1 < r.ymin) r.ymin = l->sy - t1;
767 if(l->sx + t1 > r.xmax) r.xmax = l->sx + t1;
768 if(l->sy + t1 > r.ymax) r.ymax = l->sy + t1;
775 if(!valid) memset(&r, 0, sizeof(SRECT));
779 void swf_Shape2Free(SHAPE2 * s)
781 SHAPELINE*line = s->lines;
784 SHAPELINE*next = line->next;
791 rfx_free(s->linestyles);
795 rfx_free(s->fillstyles);
804 SHAPE2* swf_Shape2Clone(SHAPE2 * s)
806 SHAPELINE*line = s->lines;
808 SHAPE2*s2 = (SHAPE2*)rfx_alloc(sizeof(SHAPE2));
809 memcpy(s2,s,sizeof(SHAPE2));
810 s2->linestyles = (LINESTYLE*)rfx_alloc(sizeof(LINESTYLE)*s->numlinestyles);
811 memcpy(s2->linestyles, s->linestyles, sizeof(LINESTYLE)*s->numlinestyles);
812 s2->fillstyles = (FILLSTYLE*)rfx_alloc(sizeof(FILLSTYLE)*s->numfillstyles);
813 memcpy(s2->fillstyles, s->fillstyles, sizeof(FILLSTYLE)*s->numfillstyles);
816 SHAPELINE*line2 = (SHAPELINE*)rfx_alloc(sizeof(SHAPELINE));
817 memcpy(line2, line, sizeof(SHAPELINE));
827 s2->bbox = (SRECT*)rfx_alloc(sizeof(SRECT));
828 memcpy(s2->bbox, s->bbox, sizeof(SRECT));
833 SHAPE2* swf_ShapeToShape2(SHAPE*shape) {
835 SHAPE2*shape2 = (SHAPE2*)rfx_calloc(sizeof(SHAPE2));
837 shape2->numlinestyles = shape->linestyle.n;
838 if(shape2->numlinestyles) {
839 shape2->linestyles = (LINESTYLE*)rfx_alloc(sizeof(LINESTYLE)*shape->linestyle.n);
840 memcpy(shape2->linestyles, shape->linestyle.data, sizeof(LINESTYLE)*shape->linestyle.n);
843 shape2->numfillstyles = shape->fillstyle.n;
844 if(shape2->numfillstyles) {
845 shape2->fillstyles = (FILLSTYLE*)rfx_alloc(sizeof(FILLSTYLE)*shape->fillstyle.n);
846 memcpy(shape2->fillstyles, shape->fillstyle.data, sizeof(FILLSTYLE)*shape->fillstyle.n);
849 shape2->lines = swf_ParseShapeData(shape->data, shape->bitlen, shape->bits.fill, shape->bits.line, 1, 0);
854 void swf_ShapeSetBitmapRect(TAG*tag, U16 gfxid, int width, int height)
862 swf_ShapeNew(&shape);
863 rgb.b = rgb.g = rgb.r = 0xff;
865 ls = swf_ShapeAddLineStyle(shape,20,&rgb);
866 swf_GetMatrix(NULL,&m);
870 fs = swf_ShapeAddBitmapFillStyle(shape,&m,gfxid,0);
877 swf_SetShapeStyles(tag,shape);
878 swf_ShapeCountBits(shape,NULL,NULL);
879 swf_SetShapeBits(tag,shape);
881 swf_ShapeSetAll(tag,shape,0,0,lines?ls:0,fs,0);
883 swf_ShapeSetLine(tag,shape,width*20,0);
884 swf_ShapeSetLine(tag,shape,0,height*20);
885 swf_ShapeSetLine(tag,shape,-width*20,0);
886 swf_ShapeSetLine(tag,shape,0,-height*20);
887 swf_ShapeSetEnd(tag);
888 swf_ShapeFree(shape);
891 void swf_ShapeSetRectangle(TAG*tag, U16 shapeid, int width, int height, RGBA*rgba)
893 RGBA white={255,255,255,255};
899 int fs = swf_ShapeAddSolidFillStyle(s, rgba);
900 swf_SetU16(tag,shapeid);
907 swf_SetShapeHeader(tag,s);
908 swf_ShapeSetAll(tag,s,0,0,0,fs,0);
909 swf_ShapeSetLine(tag,s,width,0);
910 swf_ShapeSetLine(tag,s,0,height);
911 swf_ShapeSetLine(tag,s,-width,0);
912 swf_ShapeSetLine(tag,s,0,-height);
913 swf_ShapeSetEnd(tag);
917 void swf_ShapeSetRectangleWithBorder(TAG*tag, U16 shapeid, int width, int height, RGBA*rgba, int linewidth, RGBA*linecolor)
919 RGBA white={255,255,255,255};
925 int fs = swf_ShapeAddSolidFillStyle(s, rgba);
926 int ls = swf_ShapeAddLineStyle(s, linewidth, linecolor);
927 swf_SetU16(tag,shapeid);
934 swf_SetShapeHeader(tag,s);
935 swf_ShapeSetAll(tag,s,0,0,ls,fs,0);
936 swf_ShapeSetLine(tag,s,width,0);
937 swf_ShapeSetLine(tag,s,0,height);
938 swf_ShapeSetLine(tag,s,-width,0);
939 swf_ShapeSetLine(tag,s,0,-height);
940 swf_ShapeSetEnd(tag);
944 void swf_Shape2ToShape(SHAPE2*shape2, SHAPE*shape)
946 TAG*tag = swf_InsertTag(0,0);
948 int newx=0,newy=0,lastx=0,lasty=0,oldls=0,oldfs0=0,oldfs1=0;
950 memset(shape, 0, sizeof(SHAPE));
952 shape->linestyle.n = shape2->numlinestyles;
953 shape->linestyle.data = (LINESTYLE*)rfx_alloc(sizeof(LINESTYLE)*shape->linestyle.n);
954 memcpy(shape->linestyle.data, shape2->linestyles, sizeof(LINESTYLE)*shape->linestyle.n);
956 shape->fillstyle.n = shape2->numfillstyles;
957 shape->fillstyle.data = (FILLSTYLE*)rfx_alloc(sizeof(FILLSTYLE)*shape->fillstyle.n);
958 memcpy(shape->fillstyle.data, shape2->fillstyles, sizeof(FILLSTYLE)*shape->fillstyle.n);
960 swf_ShapeCountBits(shape,NULL,NULL);
965 int ls=0,fs0=0,fs1=0;
967 if(l->type != moveTo) {
968 if(oldls != l->linestyle) {oldls = ls = l->linestyle;if(!ls) ls=0x8000;}
969 if(oldfs0 != l->fillstyle0) {oldfs0 = fs0 = l->fillstyle0;if(!fs0) fs0=0x8000;}
970 if(oldfs1 != l->fillstyle1) {oldfs1 = fs1 = l->fillstyle1;if(!fs1) fs1=0x8000;}
972 if(ls || fs0 || fs1 || newx!=0x7fffffff || newy!=0x7fffffff) {
973 swf_ShapeSetAll(tag,shape,newx,newy,ls,fs0,fs1);
979 if(l->type == lineTo) {
980 swf_ShapeSetLine(tag,shape,l->x-lastx,l->y-lasty);
981 } else if(l->type == splineTo) {
982 swf_ShapeSetCurve(tag,shape, l->sx-lastx,l->sy-lasty, l->x-l->sx,l->y-l->sy);
984 if(l->type == moveTo) {
993 swf_ShapeSetEnd(tag);
994 shape->data = tag->data;
995 shape->bitlen = tag->len*8;
999 void swf_SetShape2(TAG*tag, SHAPE2*shape2)
1002 swf_Shape2ToShape(shape2, &shape);
1004 swf_SetRect(tag,shape2->bbox);
1005 swf_SetShapeStyles(tag, &shape);
1006 //swf_ShapeCountBits(&shape,NULL,NULL); // done in swf_Shape2ToShape()
1007 swf_SetShapeBits(tag,&shape);
1009 swf_SetBlock(tag, shape.data, (shape.bitlen+7)/8);
1012 void swf_ParseDefineShape(TAG*tag, SHAPE2*shape)
1017 if(tag->id == ST_DEFINESHAPE)
1019 else if(tag->id == ST_DEFINESHAPE2)
1021 else if(tag->id == ST_DEFINESHAPE3)
1023 else if(tag->id == ST_DEFINESHAPE4)
1026 fprintf(stderr, "parseDefineShape must be called with a shape tag");
1028 swf_SetTagPos(tag, 0);
1030 id = swf_GetU16(tag); //id
1031 memset(shape, 0, sizeof(SHAPE2));
1032 shape->bbox = (SRECT*)rfx_alloc(sizeof(SRECT));
1033 swf_GetRect(tag, shape->bbox);
1036 swf_ResetReadBits(tag);
1037 swf_GetRect(tag, &r2); // edge bounds
1038 U8 flags = swf_GetU8(tag); // flags, &1: contains scaling stroke, &2: contains non-scaling stroke
1041 if(!parseFillStyleArray(tag, shape)) {
1045 swf_ResetReadBits(tag);
1046 fill = (U16)swf_GetBits(tag,4);
1047 line = (U16)swf_GetBits(tag,4);
1048 if(!fill && !line) {
1049 fprintf(stderr, "fill/line bits are both zero\n");
1052 shape->lines = swf_ParseShapeData(&tag->data[tag->pos], (tag->len - tag->pos)*8, fill, line, num, shape);
1057 static void free_lines(SHAPELINE* lines)
1060 free_lines(lines->next);
1064 void swf_RecodeShapeData(U8*data, int bitlen, int in_bits_fill, int in_bits_line,
1065 U8**destdata, U32*destbitlen, int out_bits_fill, int out_bits_line)
1070 memset(&s2, 0, sizeof(s2));
1071 s2.lines = swf_ParseShapeData(data, bitlen, in_bits_fill, in_bits_line, 1, 0);
1072 s2.numfillstyles = out_bits_fill?1<<(out_bits_fill-1):0;
1073 s2.numlinestyles = out_bits_line?1<<(out_bits_line-1):0;
1074 s2.fillstyles = (FILLSTYLE*)rfx_calloc(sizeof(FILLSTYLE)*s2.numfillstyles);
1075 s2.linestyles = (LINESTYLE*)rfx_calloc(sizeof(LINESTYLE)*s2.numlinestyles);
1079 if(line->fillstyle0 > s2.numfillstyles) line->fillstyle0 = 0;
1080 if(line->fillstyle1 > s2.numfillstyles) line->fillstyle1 = 0;
1081 if(line->linestyle > s2.numlinestyles) line->linestyle = 0;
1085 swf_Shape2ToShape(&s2,&s);
1087 free_lines(s2.lines);
1088 free(s2.fillstyles);
1089 free(s2.linestyles);
1090 free(s.fillstyle.data);
1091 free(s.linestyle.data);
1093 *destbitlen = s.bitlen;