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 #define FILL_SOLID 0x00
31 #define FILL_LINEAR 0x10 // Gradient
32 #define FILL_RADIAL 0x12
33 #define FILL_TILED 0x40 // Bitmap
34 #define FILL_CLIPPED 0x41
36 void swf_ShapeFree(SHAPE * s)
38 { if (s->linestyle.data) free(s->linestyle.data);
39 s->linestyle.data = NULL;
41 if (s->fillstyle.data) free(s->fillstyle.data);
42 s->fillstyle.data = NULL;
44 if (s->data) free(s->data);
50 int swf_ShapeNew(SHAPE * * s)
53 sh = (SHAPE *)malloc(sizeof(SHAPE)); s[0] = sh;
54 if (sh) memset(sh,0x00,sizeof(SHAPE));
58 int swf_GetSimpleShape(TAG * t,SHAPE * * s) // without Linestyle/Fillstyle Record
64 if (FAILED(swf_ShapeNew(s))) return -1;
68 sh->bits.fill = (U16)swf_GetBits(t,4);
69 sh->bits.line = (U16)swf_GetBits(t,4);
70 bitl = 0; end = 0; pos = swf_GetTagPos(t);
73 { int edge = swf_GetBits(t,1); bitl+=1;
76 if (swf_GetBits(t,1)) // Line
77 { U16 nbits = swf_GetBits(t,4)+2;
80 if (swf_GetBits(t,1)) // x/y Line
81 { swf_GetBits(t,nbits);
86 { swf_GetBits(t,nbits+1);
91 { U16 nbits = swf_GetBits(t,4)+2;
103 { U16 flags = swf_GetBits(t,5); bitl+=5;
107 { U16 nbits = swf_GetBits(t,5); bitl+=5;
108 swf_GetBits(t,nbits);
109 swf_GetBits(t,nbits);
114 { swf_GetBits(t,sh->bits.fill);
119 { swf_GetBits(t,sh->bits.fill);
124 { swf_GetBits(t,sh->bits.line);
128 if (flags&SF_NEWSTYLE)
129 { fprintf(stderr,"RFXSWF: Can't process extended styles in shape.\n");
135 swf_SetTagPos(t,pos);
138 if (sh->data) free(sh->data);
139 sh->data = (U8*)malloc(len);
143 swf_GetBlock(t,sh->data,len);
150 int swf_SetSimpleShape(TAG * t,SHAPE * s) // without Linestyle/Fillstyle Record
157 { swf_ResetWriteBits(t);
159 swf_SetBits(t,s->bits.fill,4);
160 swf_SetBits(t,s->bits.line,4);
161 swf_SetBlock(t,s->data,l);
163 swf_ResetWriteBits(t);
168 int swf_SetFillStyle(TAG * t,FILLSTYLE * f)
169 { if ((!t)||(!f)) return -1;
170 swf_SetU8(t,f->type);
176 if (swf_GetTagID(t)!=ST_DEFINESHAPE3) swf_SetRGB(t,&f->color);
177 else swf_SetRGBA(t,&f->color);
182 swf_SetU16(t,f->id_bitmap);
183 swf_SetMatrix(t,&f->m);
187 swf_SetMatrix(t,&f->m);
188 swf_SetGradient(t,&f->gradient,/*alpha?*/t->id==ST_DEFINESHAPE3?1:0);
195 int swf_SetLineStyle(TAG * t,LINESTYLE * l)
196 { if ((!l)||(!t)) return -1;
197 swf_SetU16(t,l->width);
199 if (swf_GetTagID(t)!=ST_DEFINESHAPE3) swf_SetRGB(t,&l->color);
200 else swf_SetRGBA(t,&l->color);
205 int swf_SetShapeStyleCount(TAG * t,U16 n)
217 int swf_SetShapeStyles(TAG * t,SHAPE * s)
222 l += swf_SetShapeStyleCount(t,s->fillstyle.n);
224 for (i=0;i<s->fillstyle.n;i++)
225 l+=swf_SetFillStyle(t,&s->fillstyle.data[i]);
227 l += swf_SetShapeStyleCount(t,s->linestyle.n);
229 for (i=0;i<s->linestyle.n;i++)
230 l+=swf_SetLineStyle(t,&s->linestyle.data[i]);
235 int swf_ShapeCountBits(SHAPE * s,U8 * fbits,U8 * lbits)
237 s->bits.fill = swf_CountUBits(s->fillstyle.n, 0);
238 s->bits.line = swf_CountUBits(s->linestyle.n, 0);
239 if (fbits) fbits[0] = s->bits.fill;
240 if (lbits) lbits[0] = s->bits.line;
244 int swf_SetShapeBits(TAG * t,SHAPE * s)
245 { if ((!t)||(!s)) return -1;
246 swf_ResetWriteBits(t);
247 swf_SetBits(t,s->bits.fill,4);
248 swf_SetBits(t,s->bits.line,4);
252 int swf_SetShapeHeader(TAG * t,SHAPE * s)
254 res = swf_SetShapeStyles(t,s);
255 if (res>=0) res = swf_ShapeCountBits(s,NULL,NULL);
256 if (res>=0) res = swf_SetShapeBits(t,s);
260 int swf_ShapeAddFillStyle(SHAPE * s,U8 type,MATRIX * m,RGBA * color,U16 id_bitmap, GRADIENT*gradient)
271 def_c.r = def_c.g = def_c.b = 0;
275 swf_GetMatrix(NULL,m);
280 swf_GetGradient(NULL, gradient, 1);
285 if (s->fillstyle.data)
286 { FILLSTYLE * new = (FILLSTYLE *)realloc(s->fillstyle.data,(s->fillstyle.n+1)*sizeof(FILLSTYLE));
288 s->fillstyle.data = new;
291 { s->fillstyle.data = (FILLSTYLE *)malloc(sizeof(FILLSTYLE));
293 if (!s->fillstyle.data) return -1;
298 s->fillstyle.data[s->fillstyle.n].type = type;
299 s->fillstyle.data[s->fillstyle.n].id_bitmap = id_bitmap;
300 memcpy(&s->fillstyle.data[s->fillstyle.n].m,m,sizeof(MATRIX));
301 memcpy(&s->fillstyle.data[s->fillstyle.n].color,color,sizeof(RGBA));
302 memcpy(&s->fillstyle.data[s->fillstyle.n].gradient,gradient,sizeof(GRADIENT));
304 return (++s->fillstyle.n);
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 * new = (LINESTYLE *)realloc(s->linestyle.data,(s->linestyle.n+1)*sizeof(LINESTYLE));
330 s->linestyle.data = new;
333 { s->linestyle.data = (LINESTYLE *)malloc(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);
360 int swf_ShapeSetStyle(TAG * t,SHAPE * s,int line,int fill0,int fill1)
361 { if ((!t)||(!s)) return -1;
364 swf_SetBits(t,(line?SF_LINE:0)|(fill0?SF_FILL0:0)|(fill1?SF_FILL1:0),5);
366 if (fill0) swf_SetBits(t,fill0,s->bits.fill);
367 if (fill1) swf_SetBits(t,fill1,s->bits.fill);
368 if (line) swf_SetBits(t,line ,s->bits.line);
373 /* TODO: sometimes we want to set fillstyle 0, as that's the empty fill
374 used for line drawings. At the moment, we can't, as 0 fill be considered
375 nonexistent and therefore not set.
376 these defines are a workaround (they also reduce the maximal number of
377 fill styles to 32768)
379 #define UNDEFINED_COORD 0x7fffffff
381 int swf_ShapeSetAll(TAG * t,SHAPE * s,S32 x,S32 y,int line,int fill0,int fill1)
384 if ((!t)||(!s)) return -1;
386 if(x!=UNDEFINED_COORD || y!=UNDEFINED_COORD)
390 swf_SetBits(t,(hasmove?SF_MOVETO:0)|(line?SF_LINE:0)|(fill0?SF_FILL0:0)|(fill1?SF_FILL1:0),5);
393 b = swf_CountBits(x,0);
394 b = swf_CountBits(y,b);
400 if (fill0) swf_SetBits(t,fill0,s->bits.fill);
401 if (fill1) swf_SetBits(t,fill1,s->bits.fill);
402 if (line) swf_SetBits(t,line ,s->bits.line);
407 int swf_ShapeSetEnd(TAG * t)
410 swf_ResetWriteBits(t);
414 int swf_ShapeSetLine(TAG * t,SHAPE * s,S32 x,S32 y)
417 swf_SetBits(t,3,2); // Straight Edge
419 if ((!s)||((x!=0)&&(y!=0)))
420 { b = swf_CountBits(x,2);
421 b = swf_CountBits(y,b);
424 fprintf(stderr, "Bit overflow in swf_ShapeSetLine(1)- %d\n", b);
428 swf_SetBits(t, b-2, 4);
436 { b = swf_CountBits(y,2);
440 fprintf(stderr, "Bit overflow in swf_ShapeSetLine(2)- %d\n", b);
443 swf_SetBits(t, b-2, 4);
448 { b = swf_CountBits(x,2);
452 fprintf(stderr, "Bit overflow in swf_ShapeSetLine(3)- %d\n", b);
455 swf_SetBits(t, b-2, 4);
462 int swf_ShapeSetCurve(TAG * t,SHAPE * s,S32 x,S32 y,S32 ax,S32 ay)
468 b = swf_CountBits(ax,2);
469 b = swf_CountBits(ay,b);
470 b = swf_CountBits(x,b);
471 b = swf_CountBits(y,b);
473 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 /* todo: merge this with swf_GetSimpleShape */
503 SHAPELINE* swf_ParseShapeData(U8*data, int bits, int fillbits, int linebits)
506 SHAPELINE*lines = &_lines;
515 memset(tag, 0, sizeof(TAG));
517 tag->len = tag->memsize = (bits+7)/8;
523 flags = swf_GetBits(tag, 1);
524 if(!flags) { //style change
525 flags = swf_GetBits(tag, 5);
529 int n = swf_GetBits(tag, 5);
530 x = swf_GetSBits(tag, n); //x
531 y = swf_GetSBits(tag, n); //y
534 fill0 = swf_GetBits(tag, fillbits);
536 fill1 = swf_GetBits(tag, fillbits);
538 line = swf_GetBits(tag, linebits);
540 fprintf(stderr, "Additional file styles style change not yet supported\n");
542 //enumerateUsedIDs_styles(tag, callback, callback_data, num);
543 fillbits = swf_GetBits(tag, 4);
544 linebits = swf_GetBits(tag, 4);
547 lines->next = (SHAPELINE*)malloc(sizeof(SHAPELINE));
549 lines->type = moveTo;
552 lines->sx = lines->sy = 0;
553 lines->fillstyle0 = fill0;
554 lines->fillstyle1 = fill1;
555 lines->linestyle = line;
559 flags = swf_GetBits(tag, 1);
560 if(flags) { //straight edge
561 int n = swf_GetBits(tag, 4) + 2;
562 if(swf_GetBits(tag, 1)) { //line flag
563 x += swf_GetSBits(tag, n); //delta x
564 y += swf_GetSBits(tag, n); //delta y
566 int v=swf_GetBits(tag, 1);
568 d = swf_GetSBits(tag, n); //vert/horz
572 lines->next = (SHAPELINE*)malloc(sizeof(SHAPELINE));
574 lines->type = lineTo;
577 lines->sx = lines->sy = 0;
578 lines->fillstyle0 = fill0;
579 lines->fillstyle1 = fill1;
580 lines->linestyle = line;
582 } else { //curved edge
583 int n = swf_GetBits(tag, 4) + 2;
585 x += swf_GetSBits(tag, n);
586 y += swf_GetSBits(tag, n);
589 x += swf_GetSBits(tag, n);
590 y += swf_GetSBits(tag, n);
592 lines->next = (SHAPELINE*)malloc(sizeof(SHAPELINE));
594 lines->type = splineTo;
599 lines->fillstyle0 = fill0;
600 lines->fillstyle1 = fill1;
601 lines->linestyle = line;
609 SRECT swf_GetShapeBoundingBox(SHAPE2*shape2)
612 SHAPELINE*l = shape2->lines;
615 r.xmin = r.ymin = SCOORD_MAX;
616 r.xmax = r.ymax = SCOORD_MIN;
621 t1 = shape2->linestyles[l->linestyle - 1].width*3/2;
626 if(l->type == lineTo || l->type == splineTo)
629 if(lastx - t1 < r.xmin) r.xmin = lastx - t1;
630 if(lasty - t1 < r.ymin) r.ymin = lasty - t1;
631 if(lastx + t1 > r.xmax) r.xmax = lastx + t1;
632 if(lasty + t1 > r.ymax) r.ymax = lasty + t1;
633 if(l->x - t1 < r.xmin) r.xmin = l->x - t1;
634 if(l->y - t1 < r.ymin) r.ymin = l->y - t1;
635 if(l->x + t1 > r.xmax) r.xmax = l->x + t1;
636 if(l->y + t1 > r.ymax) r.ymax = l->y + t1;
637 if(l->type == splineTo) {
638 if(l->sx - t1 < r.xmin) r.xmin = l->sx - t1;
639 if(l->sy - t1 < r.ymin) r.ymin = l->sy - t1;
640 if(l->sx + t1 > r.xmax) r.xmax = l->sx + t1;
641 if(l->sy + t1 > r.ymax) r.ymax = l->sy + t1;
648 if(!valid) memset(&r, 0, sizeof(SRECT));
652 void swf_Shape2Free(SHAPE2 * s)
654 SHAPELINE*line = s->lines;
656 SHAPELINE*next = line->next;
668 SHAPE2* swf_ShapeToShape2(SHAPE*shape) {
670 SHAPE2*shape2 = (SHAPE2*)malloc(sizeof(SHAPE2));
672 shape2->numlinestyles = shape->linestyle.n;
673 shape2->linestyles = (LINESTYLE*)malloc(sizeof(LINESTYLE)*shape->linestyle.n);
674 memcpy(shape2->linestyles, shape->linestyle.data, sizeof(LINESTYLE)*shape->linestyle.n);
676 shape2->numfillstyles = shape->fillstyle.n;
677 shape2->fillstyles = (FILLSTYLE*)malloc(sizeof(FILLSTYLE)*shape->fillstyle.n);
678 memcpy(shape2->fillstyles, shape->fillstyle.data, sizeof(FILLSTYLE)*shape->fillstyle.n);
680 shape2->lines = swf_ParseShapeData(shape->data, shape->bitlen, shape->bits.fill, shape->bits.line);
685 void swf_ShapeSetBitmapRect(TAG*tag, U16 gfxid, int width, int height)
693 swf_ShapeNew(&shape);
694 rgb.b = rgb.g = rgb.r = 0xff;
696 ls = swf_ShapeAddLineStyle(shape,20,&rgb);
697 swf_GetMatrix(NULL,&m);
701 fs = swf_ShapeAddBitmapFillStyle(shape,&m,gfxid,0);
708 swf_SetShapeStyles(tag,shape);
709 swf_ShapeCountBits(shape,NULL,NULL);
710 swf_SetShapeBits(tag,shape);
712 swf_ShapeSetAll(tag,shape,0,0,lines?ls:0,fs,0);
714 swf_ShapeSetLine(tag,shape,width*20,0);
715 swf_ShapeSetLine(tag,shape,0,height*20);
716 swf_ShapeSetLine(tag,shape,-width*20,0);
717 swf_ShapeSetLine(tag,shape,0,-height*20);
718 swf_ShapeSetEnd(tag);
719 swf_ShapeFree(shape);
722 void swf_Shape2ToShape(SHAPE2*shape2, SHAPE*shape)
724 TAG*tag = swf_InsertTag(0,0);
726 int newx=0,newy=0,lastx=0,lasty=0,oldls=0,oldfs0=0,oldfs1=0;
728 memset(shape, 0, sizeof(SHAPE));
730 shape->linestyle.n = shape2->numlinestyles;
731 shape->linestyle.data = (LINESTYLE*)malloc(sizeof(LINESTYLE)*shape->linestyle.n);
732 memcpy(shape->linestyle.data, shape2->linestyles, sizeof(LINESTYLE)*shape->linestyle.n);
734 shape->fillstyle.n = shape2->numfillstyles;
735 shape->fillstyle.data = (FILLSTYLE*)malloc(sizeof(FILLSTYLE)*shape->fillstyle.n);
736 memcpy(shape->fillstyle.data, shape2->fillstyles, sizeof(FILLSTYLE)*shape->fillstyle.n);
738 swf_ShapeCountBits(shape,NULL,NULL);
743 int ls=0,fs0=0,fs1=0;
745 if(l->type != moveTo) {
746 if(oldls != l->linestyle) {oldls = ls = l->linestyle;if(!ls) ls=0x8000;}
747 if(oldfs0 != l->fillstyle0) {oldfs0 = fs0 = l->fillstyle0;if(!fs0) fs0=0x8000;}
748 if(oldfs1 != l->fillstyle1) {oldfs1 = fs1 = l->fillstyle1;if(!fs1) fs1=0x8000;}
750 if(ls || fs0 || fs1 || newx!=0x7fffffff || newy!=0x7fffffff) {
751 swf_ShapeSetAll(tag,shape,newx,newy,ls,fs0,fs1);
757 if(l->type == lineTo) {
758 swf_ShapeSetLine(tag,shape,l->x-lastx,l->y-lasty);
759 } else if(l->type == splineTo) {
760 swf_ShapeSetCurve(tag,shape, l->sx-lastx,l->sy-lasty, l->x-l->sx,l->y-l->sy);
762 if(l->type == moveTo) {
771 swf_ShapeSetEnd(tag);
772 shape->data = tag->data;
773 shape->bitlen = tag->len*8;
776 void swf_SetShape2(TAG*tag, SHAPE2*shape2)
779 swf_Shape2ToShape(shape2, &shape);
781 swf_SetRect(tag,shape2->bbox);
782 swf_SetShapeStyles(tag, &shape);
783 swf_ShapeCountBits(&shape,NULL,NULL);
784 swf_SetShapeBits(tag,&shape);
786 swf_SetBlock(tag, shape.data, (shape.bitlen+7)/8);
789 static void parseFillStyleArray(TAG*tag, SHAPE2*shape)
794 if(tag->id == ST_DEFINESHAPE)
796 else if(tag->id == ST_DEFINESHAPE2)
798 else if(tag->id == ST_DEFINESHAPE3)
801 count = swf_GetU8(tag);
802 if(count == 0xff && num>1) // defineshape2,3 only
803 count = swf_GetU16(tag);
805 shape->numfillstyles = count;
806 shape->fillstyles = malloc(sizeof(FILLSTYLE)*count);
812 FILLSTYLE*dest = &shape->fillstyles[t];
813 type = swf_GetU8(tag); //type
814 shape->fillstyles[t].type = type;
818 swf_GetRGBA(tag, &dest->color);
820 swf_GetRGB(tag, &dest->color);
822 else if(type == 0x10 || type == 0x12)
824 /* linear/radial gradient fill */
825 swf_ResetReadBits(tag);
826 swf_GetMatrix(tag, &dest->m);
827 swf_ResetReadBits(tag);
828 swf_GetGradient(tag, &dest->gradient, num>=3?1:0);
830 else if(type == 0x40 || type == 0x41)
833 swf_ResetReadBits(tag);
834 dest->id_bitmap = swf_GetU16(tag); //id
835 swf_ResetReadBits(tag); //?
836 swf_GetMatrix(tag, &dest->m);
839 fprintf(stderr, "rfxswf:swftools.c Unknown fillstyle:0x%02x\n",type);
842 swf_ResetReadBits(tag);
843 count = swf_GetU8(tag); // line style array
845 count = swf_GetU16(tag);
847 //if(verbose) printf("lnum: %d\n", count);
849 shape->numlinestyles = count;
850 shape->linestyles = malloc(sizeof(LINESTYLE)*count);
851 /* TODO: should we start with 1 and insert a correct definition of the
852 "built in" linestyle 0? */
855 shape->linestyles[t].width = swf_GetU16(tag);
857 swf_GetRGBA(tag, &shape->linestyles[t].color);
859 swf_GetRGB(tag, &shape->linestyles[t].color);
864 void swf_ParseDefineShape(TAG*tag, SHAPE2*shape)
871 if(tag->id == ST_DEFINESHAPE)
873 else if(tag->id == ST_DEFINESHAPE2)
875 else if(tag->id == ST_DEFINESHAPE3)
878 fprintf(stderr, "parseDefineShape must be called with a shape tag");
881 id = swf_GetU16(tag); //id
882 memset(shape, 0, sizeof(SHAPE2));
883 shape->bbox = malloc(sizeof(SRECT));
884 swf_GetRect(tag, &r);
886 memcpy(shape->bbox, &r, sizeof(SRECT));
887 parseFillStyleArray(tag, shape);
889 swf_ResetReadBits(tag);
890 fill = (U16)swf_GetBits(tag,4);
891 line = (U16)swf_GetBits(tag,4);
893 shape->lines = swf_ParseShapeData(&tag->data[tag->pos], (tag->len - tag->pos)*8, fill, line);