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 #define SF_MOVETO 0x01
28 #define SF_NEWSTYLE 0x10
30 void swf_ShapeFree(SHAPE * s)
34 if (s->linestyle.data) rfx_free(s->linestyle.data);
35 s->linestyle.data = NULL;
37 if (s->fillstyle.data) rfx_free(s->fillstyle.data);
38 s->fillstyle.data = NULL;
40 if (s->data) rfx_free(s->data);
45 int swf_ShapeNew(SHAPE * * s)
49 sh = (SHAPE *)rfx_calloc(sizeof(SHAPE));
54 int swf_GetSimpleShape(TAG * t,SHAPE * * s) // without Linestyle/Fillstyle Record
60 if (FAILED(swf_ShapeNew(s))) return -1;
64 sh->bits.fill = (U16)swf_GetBits(t,4);
65 sh->bits.line = (U16)swf_GetBits(t,4);
66 bitl = 0; end = 0; pos = swf_GetTagPos(t);
69 { int edge = swf_GetBits(t,1); bitl+=1;
72 if (swf_GetBits(t,1)) // Line
73 { U16 nbits = swf_GetBits(t,4)+2;
76 if (swf_GetBits(t,1)) // x/y Line
77 { swf_GetBits(t,nbits);
82 { swf_GetBits(t,nbits+1);
87 { U16 nbits = swf_GetBits(t,4)+2;
99 { U16 flags = swf_GetBits(t,5); bitl+=5;
103 { U16 nbits = swf_GetBits(t,5); bitl+=5;
104 swf_GetBits(t,nbits);
105 swf_GetBits(t,nbits);
110 { swf_GetBits(t,sh->bits.fill);
115 { swf_GetBits(t,sh->bits.fill);
120 { swf_GetBits(t,sh->bits.line);
124 if (flags&SF_NEWSTYLE)
125 { fprintf(stderr,"RFXSWF: Can't process extended styles in shape.\n");
131 swf_SetTagPos(t,pos);
134 if (sh->data) rfx_free(sh->data);
135 sh->data = (U8*)rfx_alloc(len);
139 swf_GetBlock(t,sh->data,len);
146 int swf_SetSimpleShape(TAG * t,SHAPE * s) // without Linestyle/Fillstyle Record
153 { swf_ResetWriteBits(t);
155 swf_SetBits(t,s->bits.fill,4);
156 swf_SetBits(t,s->bits.line,4);
157 swf_SetBlock(t,s->data,l);
159 swf_ResetWriteBits(t);
164 int swf_SetFillStyle(TAG * t,FILLSTYLE * f)
165 { if ((!t)||(!f)) return -1;
166 swf_SetU8(t,f->type);
170 if (swf_GetTagID(t)!=ST_DEFINESHAPE3) swf_SetRGB(t,&f->color);
171 else swf_SetRGBA(t,&f->color);
176 swf_SetU16(t,f->id_bitmap);
177 swf_SetMatrix(t,&f->m);
181 swf_SetMatrix(t,&f->m);
182 swf_SetGradient(t,&f->gradient,/*alpha?*/t->id==ST_DEFINESHAPE3?1:0);
189 int swf_SetLineStyle(TAG * t,LINESTYLE * l)
190 { if ((!l)||(!t)) return -1;
191 swf_SetU16(t,l->width);
193 if (swf_GetTagID(t)!=ST_DEFINESHAPE3) swf_SetRGB(t,&l->color);
194 else swf_SetRGBA(t,&l->color);
199 int swf_SetShapeStyleCount(TAG * t,U16 n)
206 { swf_SetU8(t,(U8)n);
211 int swf_SetShapeStyles(TAG * t,SHAPE * s)
216 l += swf_SetShapeStyleCount(t,s->fillstyle.n);
218 for (i=0;i<s->fillstyle.n;i++)
219 l+=swf_SetFillStyle(t,&s->fillstyle.data[i]);
221 l += swf_SetShapeStyleCount(t,s->linestyle.n);
223 for (i=0;i<s->linestyle.n;i++)
224 l+=swf_SetLineStyle(t,&s->linestyle.data[i]);
229 int swf_ShapeCountBits(SHAPE * s,U8 * fbits,U8 * lbits)
231 s->bits.fill = swf_CountUBits(s->fillstyle.n, 0);
232 s->bits.line = swf_CountUBits(s->linestyle.n, 0);
233 if (fbits) fbits[0] = s->bits.fill;
234 if (lbits) lbits[0] = s->bits.line;
238 int swf_SetShapeBits(TAG * t,SHAPE * s)
239 { if ((!t)||(!s)) return -1;
240 swf_ResetWriteBits(t);
241 swf_SetBits(t,s->bits.fill,4);
242 swf_SetBits(t,s->bits.line,4);
246 int swf_SetShapeHeader(TAG * t,SHAPE * s)
248 res = swf_SetShapeStyles(t,s);
249 if (res>=0) res = swf_ShapeCountBits(s,NULL,NULL);
250 if (res>=0) res = swf_SetShapeBits(t,s);
254 int swf_ShapeAddFillStyle(SHAPE * s,U8 type,MATRIX * m,RGBA * color,U16 id_bitmap, GRADIENT*gradient)
265 def_c.r = def_c.g = def_c.b = 0;
269 swf_GetMatrix(NULL,m);
274 swf_GetGradient(NULL, gradient, 1);
279 if (s->fillstyle.data)
280 { FILLSTYLE * xnew = (FILLSTYLE *)rfx_realloc(s->fillstyle.data,(s->fillstyle.n+1)*sizeof(FILLSTYLE));
281 if (!xnew) return -1;
282 s->fillstyle.data = xnew;
285 { s->fillstyle.data = (FILLSTYLE *)rfx_alloc(sizeof(FILLSTYLE));
287 if (!s->fillstyle.data) return -1;
292 s->fillstyle.data[s->fillstyle.n].type = type;
293 s->fillstyle.data[s->fillstyle.n].id_bitmap = id_bitmap;
294 memcpy(&s->fillstyle.data[s->fillstyle.n].m,m,sizeof(MATRIX));
295 memcpy(&s->fillstyle.data[s->fillstyle.n].color,color,sizeof(RGBA));
296 memcpy(&s->fillstyle.data[s->fillstyle.n].gradient,gradient,sizeof(GRADIENT));
298 return (++s->fillstyle.n);
300 int swf_ShapeAddFillStyle2(SHAPE * s,FILLSTYLE*fs)
302 return swf_ShapeAddFillStyle(s, fs->type, &fs->m, &fs->color, fs->id_bitmap, &fs->gradient);
305 int swf_ShapeAddSolidFillStyle(SHAPE * s,RGBA * color)
306 { return swf_ShapeAddFillStyle(s,FILL_SOLID,NULL,color,0,0);
309 int swf_ShapeAddBitmapFillStyle(SHAPE * s,MATRIX * m,U16 id_bitmap,int clip)
310 { return swf_ShapeAddFillStyle(s,clip?FILL_CLIPPED:FILL_TILED,m,NULL,id_bitmap,0);
313 int swf_ShapeAddGradientFillStyle(SHAPE * s,MATRIX * m,GRADIENT* gradient,int radial)
314 { return swf_ShapeAddFillStyle(s,radial?FILL_RADIAL:FILL_LINEAR,m,NULL,0,gradient);
317 int swf_ShapeAddLineStyle(SHAPE * s,U16 width,RGBA * color)
323 def.r = def.g = def.b = 0;
325 if (s->linestyle.data)
326 { LINESTYLE * xnew = (LINESTYLE *)rfx_realloc(s->linestyle.data,(s->linestyle.n+1)*sizeof(LINESTYLE));
327 if (!xnew) return -1;
328 s->linestyle.data = xnew;
331 { s->linestyle.data = (LINESTYLE *)rfx_alloc(sizeof(LINESTYLE));
333 if (!s->linestyle.data) return -1;
336 s->linestyle.data[s->linestyle.n].width = width;
337 memcpy(&s->linestyle.data[s->linestyle.n].color,color,sizeof(RGBA));
339 return (++s->linestyle.n);
342 int swf_ShapeSetMove(TAG * t,SHAPE * s,S32 x,S32 y)
346 swf_SetBits(t,SF_MOVETO,5);
348 b = swf_CountBits(x,0);
349 b = swf_CountBits(y,b);
358 int swf_ShapeSetStyle(TAG * t,SHAPE * s,int line,int fill0,int fill1)
359 { if ((!t)||(!s)) return -1;
362 swf_SetBits(t,(line?SF_LINE:0)|(fill0?SF_FILL0:0)|(fill1?SF_FILL1:0),5);
364 if (fill0) swf_SetBits(t,fill0,s->bits.fill);
365 if (fill1) swf_SetBits(t,fill1,s->bits.fill);
366 if (line) swf_SetBits(t,line ,s->bits.line);
371 /* TODO: sometimes we want to set fillstyle 0, as that's the empty fill
372 used for line drawings. At the moment, we can't, as 0 fill be considered
373 nonexistent and therefore not set.
374 these defines are a workaround (they also reduce the maximal number of
375 fill styles to 32768)
377 #define UNDEFINED_COORD 0x7fffffff
379 int swf_ShapeSetAll(TAG * t,SHAPE * s,S32 x,S32 y,int line,int fill0,int fill1)
382 if ((!t)||(!s)) return -1;
384 if(x!=UNDEFINED_COORD || y!=UNDEFINED_COORD)
388 swf_SetBits(t,(hasmove?SF_MOVETO:0)|(line?SF_LINE:0)|(fill0?SF_FILL0:0)|(fill1?SF_FILL1:0),5);
391 b = swf_CountBits(x,0);
392 b = swf_CountBits(y,b);
398 if (fill0) swf_SetBits(t,fill0,s->bits.fill);
399 if (fill1) swf_SetBits(t,fill1,s->bits.fill);
400 if (line) swf_SetBits(t,line ,s->bits.line);
405 int swf_ShapeSetEnd(TAG * t)
408 swf_ResetWriteBits(t);
412 int swf_ShapeSetLine(TAG * t,SHAPE * s,S32 x,S32 y)
417 b = swf_CountBits(x,2);
418 b = swf_CountBits(y,b);
422 /* do not split into more than 64 segments. If the line is *that* long, something's broken */
423 fprintf(stderr, "Warning: Line to %.2f,%.2f is too long\n", (double)x,(double)y);
428 if(x>=0) { x1 = x/2;x2 = (x+1)/2;}
429 else { x1 = x/2;x2 = (x-1)/2;}
430 if(y>=0) { y1 = y/2;y2 = (y+1)/2;}
431 else { y1 = y/2;y2 = (y-1)/2;}
432 swf_ShapeSetLine(t, s, x1,y1);
433 swf_ShapeSetLine(t, s, x2,y2);
438 if(x!=0 && y!=0) { //(!s)||((x!=0)&&(y!=0)))
439 swf_SetBits(t,3,2); // Straight Edge
440 swf_SetBits(t, b-2, 4); //Number of Bits in x/y
441 swf_SetBits(t,1,1); // Diagonal
445 swf_SetBits(t,3,2); // Straight Edge
446 swf_SetBits(t, b-2, 4); //Number of Bits in y
447 swf_SetBits(t,1,2); // Vertical
450 swf_SetBits(t,3,2); // Straight Edge
451 swf_SetBits(t, b-2, 4); //Number of Bits in x
452 swf_SetBits(t,0,2); // Horizontal
458 int swf_ShapeSetCurve(TAG * t,SHAPE * s,S32 x,S32 y,S32 ax,S32 ay)
463 b = swf_CountBits(ax,2);
464 b = swf_CountBits(ay,b);
465 b = swf_CountBits(x,b);
466 b = swf_CountBits(y,b);
469 fprintf(stderr, "Bit overflow in swf_ShapeSetCurve- %d (%d,%d,%d,%d)\n", b, ax,ay,x,y);
474 swf_SetBits(t,b-2,4);
482 int swf_ShapeSetCircle(TAG * t,SHAPE * s,S32 x,S32 y,S32 rx,S32 ry)
483 { double C1 = 0.2930;
485 double begin = 0.7070;
489 swf_ShapeSetMove(t,s,x+begin*rx,y+begin*ry);
490 swf_ShapeSetCurve(t,s, -C1*rx, C1*ry, -C2*rx, 0);
491 swf_ShapeSetCurve(t,s, -C2*rx, 0, -C1*rx, -C1*ry);
492 swf_ShapeSetCurve(t,s, -C1*rx, -C1*ry, 0, -C2*ry);
493 swf_ShapeSetCurve(t,s, 0, -C2*ry, C1*rx, -C1*ry);
494 swf_ShapeSetCurve(t,s, C1*rx, -C1*ry, C2*rx, 0);
495 swf_ShapeSetCurve(t,s, C2*rx, 0, C1*rx, C1*ry);
496 swf_ShapeSetCurve(t,s, C1*rx, C1*ry, 0, C2*ry);
497 swf_ShapeSetCurve(t,s, 0, C2*ry, -C1*rx, C1*ry);
502 void dummycallback1(TAG*tag, int x, void*y)
507 void enumerateUsedIDs_styles(TAG * tag, void (*callback)(TAG*, int, void*), void*callback_data, int num, int morph);
509 static int parseFillStyleArray(TAG*tag, SHAPE2*shape)
514 int fillstylestart = shape->numfillstyles;
515 int linestylestart = shape->numlinestyles;
517 if(tag->id == ST_DEFINESHAPE)
519 else if(tag->id == ST_DEFINESHAPE2)
521 else if(tag->id == ST_DEFINESHAPE3)
523 else if(tag->id == ST_DEFINESHAPE4)
526 count = swf_GetU8(tag);
527 if(count == 0xff && num>1) // defineshape2,3 only
528 count = swf_GetU16(tag);
530 shape->numfillstyles += count;
531 if(shape->numfillstyles) {
532 shape->fillstyles = (FILLSTYLE*)rfx_realloc(shape->fillstyles, sizeof(FILLSTYLE)*shape->numfillstyles);
534 for(t=fillstylestart;t<shape->numfillstyles;t++)
537 FILLSTYLE*dest = &shape->fillstyles[t];
538 type = swf_GetU8(tag); //type
539 shape->fillstyles[t].type = type;
543 swf_GetRGBA(tag, &dest->color);
545 swf_GetRGB(tag, &dest->color);
547 else if(type == 0x10 || type == 0x11 || type == 0x12 || type == 0x13)
549 /* linear/radial gradient fill */
550 swf_ResetReadBits(tag);
551 swf_GetMatrix(tag, &dest->m);
552 swf_ResetReadBits(tag);
553 swf_GetGradient(tag, &dest->gradient, num>=3?1:0);
557 else if(type == 0x40 || type == 0x41 || type == 0x42 || type == 0x43)
560 swf_ResetReadBits(tag);
561 dest->id_bitmap = swf_GetU16(tag); //id
562 swf_ResetReadBits(tag); //?
563 swf_GetMatrix(tag, &dest->m);
566 fprintf(stderr, "rfxswf:swftools.c Unknown fillstyle:0x%02x\n",type);
571 swf_ResetReadBits(tag);
572 count = swf_GetU8(tag); // line style array
574 count = swf_GetU16(tag);
576 shape->numlinestyles += count;
578 shape->linestyles = (LINESTYLE*)rfx_realloc(shape->linestyles, sizeof(LINESTYLE)*shape->numlinestyles);
579 /* TODO: should we start with 1 and insert a correct definition of the
580 "built in" linestyle 0? */
581 for(t=linestylestart;t<shape->numlinestyles;t++)
583 shape->linestyles[t].width = swf_GetU16(tag);
586 U16 flags = swf_GetU16(tag);
588 swf_GetU16(tag); // miter limit
590 fprintf(stderr, "Filled strokes parsing not yet supported\n");
596 swf_GetRGBA(tag, &shape->linestyles[t].color);
598 swf_GetRGB(tag, &shape->linestyles[t].color);
605 /* todo: merge this with swf_GetSimpleShape */
606 static SHAPELINE* swf_ParseShapeData(U8*data, int bits, int fillbits, int linebits, int version, SHAPE2*shape2)
609 SHAPELINE*lines = &_lines;
620 memset(tag, 0, sizeof(TAG));
622 tag->len = tag->memsize = (bits+7)/8;
624 tag->id = version==1?ST_DEFINESHAPE:(version==2?ST_DEFINESHAPE2:ST_DEFINESHAPE3);
629 flags = swf_GetBits(tag, 1);
630 if(!flags) { //style change
631 flags = swf_GetBits(tag, 5);
635 int n = swf_GetBits(tag, 5);
636 x = swf_GetSBits(tag, n); //x
637 y = swf_GetSBits(tag, n); //y
640 fill0 = swf_GetBits(tag, fillbits) + fillstyleadd;
642 fill1 = swf_GetBits(tag, fillbits) + fillstyleadd;
644 line = swf_GetBits(tag, linebits) + linestyleadd;
647 fprintf(stderr, "rfxswf: Error: Additional fillstyles not supported\n");fflush(stderr);
648 enumerateUsedIDs_styles(tag, dummycallback1, 0, version, 0);
650 linestyleadd = shape2->numlinestyles;
651 fillstyleadd = shape2->numfillstyles;
652 if(!parseFillStyleArray(tag, shape2))
655 fillbits = swf_GetBits(tag, 4);
656 linebits = swf_GetBits(tag, 4);
659 lines->next = (SHAPELINE*)rfx_alloc(sizeof(SHAPELINE));
661 lines->type = moveTo;
664 lines->sx = lines->sy = 0;
665 lines->fillstyle0 = fill0;
666 lines->fillstyle1 = fill1;
667 lines->linestyle = line;
671 flags = swf_GetBits(tag, 1);
672 if(flags) { //straight edge
673 int n = swf_GetBits(tag, 4) + 2;
674 if(swf_GetBits(tag, 1)) { //line flag
675 x += swf_GetSBits(tag, n); //delta x
676 y += swf_GetSBits(tag, n); //delta y
678 int v=swf_GetBits(tag, 1);
680 d = swf_GetSBits(tag, n); //vert/horz
684 lines->next = (SHAPELINE*)rfx_alloc(sizeof(SHAPELINE));
686 lines->type = lineTo;
689 lines->sx = lines->sy = 0;
690 lines->fillstyle0 = fill0;
691 lines->fillstyle1 = fill1;
692 lines->linestyle = line;
694 } else { //curved edge
695 int n = swf_GetBits(tag, 4) + 2;
697 x += swf_GetSBits(tag, n);
698 y += swf_GetSBits(tag, n);
701 x += swf_GetSBits(tag, n);
702 y += swf_GetSBits(tag, n);
704 lines->next = (SHAPELINE*)rfx_alloc(sizeof(SHAPELINE));
706 lines->type = splineTo;
711 lines->fillstyle0 = fill0;
712 lines->fillstyle1 = fill1;
713 lines->linestyle = line;
721 SRECT swf_GetShapeBoundingBox(SHAPE2*shape2)
724 SHAPELINE*l = shape2->lines;
727 r.xmin = r.ymin = SCOORD_MAX;
728 r.xmax = r.ymax = SCOORD_MIN;
733 t1 = shape2->linestyles[l->linestyle - 1].width*3/2;
738 if(l->type == lineTo || l->type == splineTo)
741 if(lastx - t1 < r.xmin) r.xmin = lastx - t1;
742 if(lasty - t1 < r.ymin) r.ymin = lasty - t1;
743 if(lastx + t1 > r.xmax) r.xmax = lastx + t1;
744 if(lasty + t1 > r.ymax) r.ymax = lasty + t1;
745 if(l->x - t1 < r.xmin) r.xmin = l->x - t1;
746 if(l->y - t1 < r.ymin) r.ymin = l->y - t1;
747 if(l->x + t1 > r.xmax) r.xmax = l->x + t1;
748 if(l->y + t1 > r.ymax) r.ymax = l->y + t1;
749 if(l->type == splineTo) {
750 if(l->sx - t1 < r.xmin) r.xmin = l->sx - t1;
751 if(l->sy - t1 < r.ymin) r.ymin = l->sy - t1;
752 if(l->sx + t1 > r.xmax) r.xmax = l->sx + t1;
753 if(l->sy + t1 > r.ymax) r.ymax = l->sy + t1;
760 if(!valid) memset(&r, 0, sizeof(SRECT));
764 void swf_Shape2Free(SHAPE2 * s)
766 SHAPELINE*line = s->lines;
769 SHAPELINE*next = line->next;
776 rfx_free(s->linestyles);
780 rfx_free(s->fillstyles);
789 SHAPE2* swf_Shape2Clone(SHAPE2 * s)
791 SHAPELINE*line = s->lines;
793 SHAPE2*s2 = (SHAPE2*)rfx_alloc(sizeof(SHAPE2));
794 memcpy(s2,s,sizeof(SHAPE2));
795 s2->linestyles = (LINESTYLE*)rfx_alloc(sizeof(LINESTYLE)*s->numlinestyles);
796 memcpy(s2->linestyles, s->linestyles, sizeof(LINESTYLE)*s->numlinestyles);
797 s2->fillstyles = (FILLSTYLE*)rfx_alloc(sizeof(FILLSTYLE)*s->numfillstyles);
798 memcpy(s2->fillstyles, s->fillstyles, sizeof(FILLSTYLE)*s->numfillstyles);
801 SHAPELINE*line2 = (SHAPELINE*)rfx_alloc(sizeof(SHAPELINE));
802 memcpy(line2, line, sizeof(SHAPELINE));
812 s2->bbox = (SRECT*)rfx_alloc(sizeof(SRECT));
813 memcpy(s2->bbox, s->bbox, sizeof(SRECT));
818 SHAPE2* swf_ShapeToShape2(SHAPE*shape) {
820 SHAPE2*shape2 = (SHAPE2*)rfx_calloc(sizeof(SHAPE2));
822 shape2->numlinestyles = shape->linestyle.n;
823 if(shape2->numlinestyles) {
824 shape2->linestyles = (LINESTYLE*)rfx_alloc(sizeof(LINESTYLE)*shape->linestyle.n);
825 memcpy(shape2->linestyles, shape->linestyle.data, sizeof(LINESTYLE)*shape->linestyle.n);
828 shape2->numfillstyles = shape->fillstyle.n;
829 if(shape2->numfillstyles) {
830 shape2->fillstyles = (FILLSTYLE*)rfx_alloc(sizeof(FILLSTYLE)*shape->fillstyle.n);
831 memcpy(shape2->fillstyles, shape->fillstyle.data, sizeof(FILLSTYLE)*shape->fillstyle.n);
834 shape2->lines = swf_ParseShapeData(shape->data, shape->bitlen, shape->bits.fill, shape->bits.line, 1, 0);
839 void swf_ShapeSetBitmapRect(TAG*tag, U16 gfxid, int width, int height)
847 swf_ShapeNew(&shape);
848 rgb.b = rgb.g = rgb.r = 0xff;
850 ls = swf_ShapeAddLineStyle(shape,20,&rgb);
851 swf_GetMatrix(NULL,&m);
855 fs = swf_ShapeAddBitmapFillStyle(shape,&m,gfxid,0);
862 swf_SetShapeStyles(tag,shape);
863 swf_ShapeCountBits(shape,NULL,NULL);
864 swf_SetShapeBits(tag,shape);
866 swf_ShapeSetAll(tag,shape,0,0,lines?ls:0,fs,0);
868 swf_ShapeSetLine(tag,shape,width*20,0);
869 swf_ShapeSetLine(tag,shape,0,height*20);
870 swf_ShapeSetLine(tag,shape,-width*20,0);
871 swf_ShapeSetLine(tag,shape,0,-height*20);
872 swf_ShapeSetEnd(tag);
873 swf_ShapeFree(shape);
876 void swf_Shape2ToShape(SHAPE2*shape2, SHAPE*shape)
878 TAG*tag = swf_InsertTag(0,0);
880 int newx=0,newy=0,lastx=0,lasty=0,oldls=0,oldfs0=0,oldfs1=0;
882 memset(shape, 0, sizeof(SHAPE));
884 shape->linestyle.n = shape2->numlinestyles;
885 shape->linestyle.data = (LINESTYLE*)rfx_alloc(sizeof(LINESTYLE)*shape->linestyle.n);
886 memcpy(shape->linestyle.data, shape2->linestyles, sizeof(LINESTYLE)*shape->linestyle.n);
888 shape->fillstyle.n = shape2->numfillstyles;
889 shape->fillstyle.data = (FILLSTYLE*)rfx_alloc(sizeof(FILLSTYLE)*shape->fillstyle.n);
890 memcpy(shape->fillstyle.data, shape2->fillstyles, sizeof(FILLSTYLE)*shape->fillstyle.n);
892 swf_ShapeCountBits(shape,NULL,NULL);
897 int ls=0,fs0=0,fs1=0;
899 if(l->type != moveTo) {
900 if(oldls != l->linestyle) {oldls = ls = l->linestyle;if(!ls) ls=0x8000;}
901 if(oldfs0 != l->fillstyle0) {oldfs0 = fs0 = l->fillstyle0;if(!fs0) fs0=0x8000;}
902 if(oldfs1 != l->fillstyle1) {oldfs1 = fs1 = l->fillstyle1;if(!fs1) fs1=0x8000;}
904 if(ls || fs0 || fs1 || newx!=0x7fffffff || newy!=0x7fffffff) {
905 swf_ShapeSetAll(tag,shape,newx,newy,ls,fs0,fs1);
911 if(l->type == lineTo) {
912 swf_ShapeSetLine(tag,shape,l->x-lastx,l->y-lasty);
913 } else if(l->type == splineTo) {
914 swf_ShapeSetCurve(tag,shape, l->sx-lastx,l->sy-lasty, l->x-l->sx,l->y-l->sy);
916 if(l->type == moveTo) {
925 swf_ShapeSetEnd(tag);
926 shape->data = tag->data;
927 shape->bitlen = tag->len*8;
931 void swf_SetShape2(TAG*tag, SHAPE2*shape2)
934 swf_Shape2ToShape(shape2, &shape);
936 swf_SetRect(tag,shape2->bbox);
937 swf_SetShapeStyles(tag, &shape);
938 //swf_ShapeCountBits(&shape,NULL,NULL); // done in swf_Shape2ToShape()
939 swf_SetShapeBits(tag,&shape);
941 swf_SetBlock(tag, shape.data, (shape.bitlen+7)/8);
944 void swf_ParseDefineShape(TAG*tag, SHAPE2*shape)
949 if(tag->id == ST_DEFINESHAPE)
951 else if(tag->id == ST_DEFINESHAPE2)
953 else if(tag->id == ST_DEFINESHAPE3)
955 else if(tag->id == ST_DEFINESHAPE4)
958 fprintf(stderr, "parseDefineShape must be called with a shape tag");
960 swf_SetTagPos(tag, 0);
962 id = swf_GetU16(tag); //id
963 memset(shape, 0, sizeof(SHAPE2));
964 shape->bbox = (SRECT*)rfx_alloc(sizeof(SRECT));
965 swf_GetRect(tag, shape->bbox);
968 swf_ResetReadBits(tag);
969 swf_GetRect(tag, &r2); // edge bounds
970 U8 flags = swf_GetU8(tag); // flags, &1: contains scaling stroke, &2: contains non-scaling stroke
973 if(!parseFillStyleArray(tag, shape)) {
977 swf_ResetReadBits(tag);
978 fill = (U16)swf_GetBits(tag,4);
979 line = (U16)swf_GetBits(tag,4);
981 shape->lines = swf_ParseShapeData(&tag->data[tag->pos], (tag->len - tag->pos)*8, fill, line,
982 tag->id == ST_DEFINESHAPE?1:(tag->id==ST_DEFINESHAPE2?2:3), shape);
987 static void free_lines(SHAPELINE* lines)
990 free_lines(lines->next);
994 void swf_RecodeShapeData(U8*data, int bitlen, int in_bits_fill, int in_bits_line,
995 U8**destdata, U32*destbitlen, int out_bits_fill, int out_bits_line)
1000 memset(&s2, 0, sizeof(s2));
1001 s2.lines = swf_ParseShapeData(data, bitlen, in_bits_fill, in_bits_line, 1, 0);
1002 s2.numfillstyles = out_bits_fill?1<<(out_bits_fill-1):0;
1003 s2.numlinestyles = out_bits_line?1<<(out_bits_line-1):0;
1004 s2.fillstyles = (FILLSTYLE*)rfx_calloc(sizeof(FILLSTYLE)*s2.numfillstyles);
1005 s2.linestyles = (LINESTYLE*)rfx_calloc(sizeof(LINESTYLE)*s2.numlinestyles);
1009 if(line->fillstyle0 > s2.numfillstyles) line->fillstyle0 = 0;
1010 if(line->fillstyle1 > s2.numfillstyles) line->fillstyle1 = 0;
1011 if(line->linestyle > s2.numlinestyles) line->linestyle = 0;
1015 swf_Shape2ToShape(&s2,&s);
1017 free_lines(s2.lines);
1018 free(s2.fillstyles);
1019 free(s2.linestyles);
1020 free(s.fillstyle.data);
1021 free(s.linestyle.data);
1023 *destbitlen = s.bitlen;