some indenting/style fixes.
[swftools.git] / lib / modules / swfshape.c
1 /* swfshape.c
2
3    shape functions
4       
5    Extension module for the rfxswf library.
6    Part of the swftools package.
7
8    Copyright (c) 2001 Rainer Böhme <rfxswf@reflex-studio.de>
9  
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.
14
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.
19
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 */
23
24 #define SF_MOVETO       0x01
25 #define SF_FILL0        0x02
26 #define SF_FILL1        0x04
27 #define SF_LINE         0x08
28 #define SF_NEWSTYLE     0x10
29
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
35
36 void swf_ShapeFree(SHAPE * s)
37
38     if(!s)
39         return;
40     if (s->linestyle.data) free(s->linestyle.data);
41     s->linestyle.data = NULL;
42     s->linestyle.n    = 0;
43     if (s->fillstyle.data) free(s->fillstyle.data);
44     s->fillstyle.data = NULL;
45     s->fillstyle.n    = 0;
46     if (s->data) free(s->data);
47     s->data = NULL;
48     free(s);
49 }
50
51 int swf_ShapeNew(SHAPE * * s)
52
53     SHAPE * sh;
54     if (!s) return -1;
55     sh = (SHAPE *)malloc(sizeof(SHAPE)); 
56     *s = sh;
57     memset(sh,0,sizeof(SHAPE));
58     return 0;
59 }
60
61 int swf_GetSimpleShape(TAG * t,SHAPE * * s) // without Linestyle/Fillstyle Record
62 { SHAPE * sh;
63   int bitl, len;
64   int end;
65   U32 pos;
66   
67   if (FAILED(swf_ShapeNew(s))) return -1;
68   sh = s[0];
69
70   swf_ResetReadBits(t); 
71   sh->bits.fill = (U16)swf_GetBits(t,4);
72   sh->bits.line = (U16)swf_GetBits(t,4);
73   bitl = 0; end = 0; pos = swf_GetTagPos(t);
74
75   while (!end)
76   { int edge = swf_GetBits(t,1); bitl+=1;
77     if (edge)
78     { bitl+=1;
79       if (swf_GetBits(t,1))                 // Line
80       { U16 nbits = swf_GetBits(t,4)+2;
81         bitl+=5;
82
83         if (swf_GetBits(t,1))               // x/y Line
84         { swf_GetBits(t,nbits);
85           swf_GetBits(t,nbits);
86           bitl+=nbits*2;
87         }
88         else                            // hline/vline
89         { swf_GetBits(t,nbits+1);
90           bitl+=nbits+1;
91         }
92       }
93       else                              // Curve
94       { U16 nbits = swf_GetBits(t,4)+2;
95         bitl+=4;
96
97         swf_GetBits(t,nbits);
98         swf_GetBits(t,nbits);
99         swf_GetBits(t,nbits);
100         swf_GetBits(t,nbits);
101
102         bitl+=4*nbits;
103       }
104     }
105     else
106     { U16 flags = swf_GetBits(t,5); bitl+=5;
107       if (flags)
108       {
109         if (flags&SF_MOVETO)
110         { U16 nbits = swf_GetBits(t,5); bitl+=5;
111           swf_GetBits(t,nbits);
112           swf_GetBits(t,nbits);
113           bitl+=2*nbits;
114         }
115         
116         if (flags&SF_FILL0)
117         { swf_GetBits(t,sh->bits.fill);
118           bitl+=sh->bits.fill;
119         }
120         
121         if (flags&SF_FILL1)
122         { swf_GetBits(t,sh->bits.fill);
123           bitl+=sh->bits.fill;
124         }
125
126         if (flags&SF_LINE)
127         { swf_GetBits(t,sh->bits.line);
128           bitl+=sh->bits.line;
129         }
130
131         if (flags&SF_NEWSTYLE)
132         { fprintf(stderr,"RFXSWF: Can't process extended styles in shape.\n");
133         }
134       }
135       else end = 1;
136     }
137   }
138   swf_SetTagPos(t,pos);
139   len = (bitl+7)/8;
140   
141   if (sh->data) free(sh->data);
142   sh->data = (U8*)malloc(len);
143   
144   if (sh->data)
145   { sh->bitlen = bitl;
146     swf_GetBlock(t,sh->data,len);
147   }
148   else return -1;
149   
150   return len;
151 }
152
153 int swf_SetSimpleShape(TAG * t,SHAPE * s) // without Linestyle/Fillstyle Record
154 { int l;
155
156   if (!s) return -1;
157   l = (s->bitlen+7)/8;
158
159   if (t)
160   { swf_ResetWriteBits(t);
161
162     swf_SetBits(t,s->bits.fill,4);
163     swf_SetBits(t,s->bits.line,4);
164     swf_SetBlock(t,s->data,l);
165
166     swf_ResetWriteBits(t);
167   }
168   return l+1;
169 }
170
171 int swf_SetFillStyle(TAG * t,FILLSTYLE * f)
172 { if ((!t)||(!f)) return -1;
173   swf_SetU8(t,f->type);
174   
175   switch (f->type)
176   { case FILL_SOLID:
177       if (swf_GetTagID(t)!=ST_DEFINESHAPE3) swf_SetRGB(t,&f->color);
178       else swf_SetRGBA(t,&f->color);
179       break;
180
181     case FILL_TILED:
182     case FILL_CLIPPED:
183       swf_SetU16(t,f->id_bitmap);
184       swf_SetMatrix(t,&f->m);
185       break;
186     case FILL_LINEAR:
187     case FILL_RADIAL:
188       swf_SetMatrix(t,&f->m);
189       swf_SetGradient(t,&f->gradient,/*alpha?*/t->id==ST_DEFINESHAPE3?1:0);
190       break;
191   }
192   
193   return 0;
194 }
195
196 int swf_SetLineStyle(TAG * t,LINESTYLE * l)
197 { if ((!l)||(!t)) return -1;
198   swf_SetU16(t,l->width);
199
200   if (swf_GetTagID(t)!=ST_DEFINESHAPE3) swf_SetRGB(t,&l->color);
201   else swf_SetRGBA(t,&l->color);
202   
203   return 0;
204 }
205
206 int swf_SetShapeStyleCount(TAG * t,U16 n)
207 { if (n>254)
208   { swf_SetU8(t,0xff);
209     swf_SetU16(t,n);
210     return 3;
211   }
212   else
213   { swf_SetU8(t,n);
214     return 1;
215   }
216 }
217
218 int swf_SetShapeStyles(TAG * t,SHAPE * s)
219 { int i,l;
220   if (!s) return -1;
221
222   l = 0;
223   l += swf_SetShapeStyleCount(t,s->fillstyle.n);
224
225   for (i=0;i<s->fillstyle.n;i++)
226     l+=swf_SetFillStyle(t,&s->fillstyle.data[i]);
227
228   l += swf_SetShapeStyleCount(t,s->linestyle.n);
229
230   for (i=0;i<s->linestyle.n;i++)
231     l+=swf_SetLineStyle(t,&s->linestyle.data[i]);
232
233   return l;
234 }
235
236 int swf_ShapeCountBits(SHAPE * s,U8 * fbits,U8 * lbits)
237 { if (!s) return -1;
238   s->bits.fill = swf_CountUBits(s->fillstyle.n, 0);
239   s->bits.line = swf_CountUBits(s->linestyle.n, 0);
240   if (fbits) fbits[0] = s->bits.fill;
241   if (lbits) lbits[0] = s->bits.line;
242   return 0;    
243 }
244
245 int swf_SetShapeBits(TAG * t,SHAPE * s)
246 { if ((!t)||(!s)) return -1;
247   swf_ResetWriteBits(t);
248   swf_SetBits(t,s->bits.fill,4);
249   swf_SetBits(t,s->bits.line,4);
250   return 0;
251 }
252
253 int swf_SetShapeHeader(TAG * t,SHAPE * s)
254 { int res;
255   res = swf_SetShapeStyles(t,s);
256   if (res>=0) res = swf_ShapeCountBits(s,NULL,NULL);
257   if (res>=0) res = swf_SetShapeBits(t,s);
258   return res;
259 }
260
261 int swf_ShapeAddFillStyle(SHAPE * s,U8 type,MATRIX * m,RGBA * color,U16 id_bitmap, GRADIENT*gradient)
262 { RGBA def_c;
263   MATRIX def_m;    
264   GRADIENT def_g;
265
266   // handle defaults
267   
268   if (!s) return -1;
269   if (!color)
270   { color = &def_c;
271     def_c.a = 0xff;
272     def_c.r = def_c.g = def_c.b = 0;
273   }
274   if (!m)
275   { m = &def_m;
276     swf_GetMatrix(NULL,m);
277   }
278   if(!gradient)
279   {
280     gradient = &def_g;
281     swf_GetGradient(NULL, gradient, 1);
282   }
283
284   // handle memory
285   
286   if (s->fillstyle.data)
287   { FILLSTYLE * new = (FILLSTYLE *)realloc(s->fillstyle.data,(s->fillstyle.n+1)*sizeof(FILLSTYLE));
288     if (!new) return -1;
289     s->fillstyle.data = new;
290   }
291   else
292   { s->fillstyle.data = (FILLSTYLE *)malloc(sizeof(FILLSTYLE));
293     s->fillstyle.n = 0;
294     if (!s->fillstyle.data) return -1;
295   }
296
297   // set fillstyle
298   
299   s->fillstyle.data[s->fillstyle.n].type = type; 
300   s->fillstyle.data[s->fillstyle.n].id_bitmap = id_bitmap;
301   memcpy(&s->fillstyle.data[s->fillstyle.n].m,m,sizeof(MATRIX));
302   memcpy(&s->fillstyle.data[s->fillstyle.n].color,color,sizeof(RGBA));
303   memcpy(&s->fillstyle.data[s->fillstyle.n].gradient,gradient,sizeof(GRADIENT));
304           
305   return (++s->fillstyle.n);
306 }
307
308 int swf_ShapeAddSolidFillStyle(SHAPE * s,RGBA * color)
309 { return swf_ShapeAddFillStyle(s,FILL_SOLID,NULL,color,0,0);
310 }
311
312 int swf_ShapeAddBitmapFillStyle(SHAPE * s,MATRIX * m,U16 id_bitmap,int clip)
313 { return swf_ShapeAddFillStyle(s,clip?FILL_CLIPPED:FILL_TILED,m,NULL,id_bitmap,0);
314 }
315
316 int swf_ShapeAddGradientFillStyle(SHAPE * s,MATRIX * m,GRADIENT* gradient,int radial)
317 { return swf_ShapeAddFillStyle(s,radial?FILL_RADIAL:FILL_LINEAR,m,NULL,0,gradient);
318 }
319
320 int swf_ShapeAddLineStyle(SHAPE * s,U16 width,RGBA * color)
321 { RGBA def;
322   if (!s) return -1;
323   if (!color)
324   { color = &def;
325     def.a = 0xff;
326     def.r = def.g = def.b = 0; 
327   }
328   if (s->linestyle.data)
329   { LINESTYLE * new = (LINESTYLE *)realloc(s->linestyle.data,(s->linestyle.n+1)*sizeof(LINESTYLE));
330     if (!new) return -1;
331     s->linestyle.data = new;
332   }
333   else
334   { s->linestyle.data = (LINESTYLE *)malloc(sizeof(LINESTYLE));
335     s->linestyle.n = 0;
336     if (!s->linestyle.data) return -1;
337   }
338   
339   s->linestyle.data[s->linestyle.n].width = width;
340   memcpy(&s->linestyle.data[s->linestyle.n].color,color,sizeof(RGBA));
341
342   return (++s->linestyle.n);
343 }
344
345 int swf_ShapeSetMove(TAG * t,SHAPE * s,S32 x,S32 y)
346 { U8 b;
347   if (!t) return -1;
348   swf_SetBits(t,0,1);
349   swf_SetBits(t,SF_MOVETO,5);
350   
351   b = swf_CountBits(x,0);
352   b = swf_CountBits(y,b);
353   
354   swf_SetBits(t,b,5);
355   swf_SetBits(t,x,b);
356   swf_SetBits(t,y,b);
357
358   return 0;
359 }
360
361 int swf_ShapeSetStyle(TAG * t,SHAPE * s,int line,int fill0,int fill1)
362 { if ((!t)||(!s)) return -1;
363     
364   swf_SetBits(t,0,1);
365   swf_SetBits(t,(line?SF_LINE:0)|(fill0?SF_FILL0:0)|(fill1?SF_FILL1:0),5);
366
367   if (fill0) swf_SetBits(t,fill0,s->bits.fill);
368   if (fill1) swf_SetBits(t,fill1,s->bits.fill);
369   if (line)  swf_SetBits(t,line ,s->bits.line);
370   
371   return 0;
372 }
373
374 /* TODO: sometimes we want to set fillstyle 0, as that's the empty fill
375    used for line drawings. At the moment, we can't, as 0 fill be considered
376    nonexistent and therefore not set.
377    these defines are a workaround (they also reduce the maximal number of
378    fill styles to 32768)
379  */
380 #define UNDEFINED_COORD 0x7fffffff
381
382 int swf_ShapeSetAll(TAG * t,SHAPE * s,S32 x,S32 y,int line,int fill0,int fill1)
383 { U8 b;
384   U8 hasmove = 0;
385   if ((!t)||(!s)) return -1;
386
387   if(x!=UNDEFINED_COORD || y!=UNDEFINED_COORD)
388       hasmove=1;
389
390   swf_SetBits(t,0,1);
391   swf_SetBits(t,(hasmove?SF_MOVETO:0)|(line?SF_LINE:0)|(fill0?SF_FILL0:0)|(fill1?SF_FILL1:0),5);
392
393   if(hasmove) {
394     b = swf_CountBits(x,0);
395     b = swf_CountBits(y,b);
396     swf_SetBits(t,b,5);
397     swf_SetBits(t,x,b);
398     swf_SetBits(t,y,b);
399   }
400
401   if (fill0) swf_SetBits(t,fill0,s->bits.fill);
402   if (fill1) swf_SetBits(t,fill1,s->bits.fill);
403   if (line)  swf_SetBits(t,line ,s->bits.line);
404   
405   return 0;
406 }
407
408 int swf_ShapeSetEnd(TAG * t)
409 { if (!t) return -1;
410   swf_SetBits(t,0,6);
411   swf_ResetWriteBits(t);
412   return 0;
413 }
414
415 int swf_ShapeSetLine(TAG * t,SHAPE * s,S32 x,S32 y)
416
417     U8 b;
418     if (!t) return -1;
419    
420     b = swf_CountBits(x,2);
421     b = swf_CountBits(y,b);
422     if (b<2) b=2;
423     if(b >= 18) {
424         if(b >= 18 + 4) {
425             /* do not split into more than 16 segments. If the line is *that* long, something's broken */
426             fprintf(stderr, "Warning: Line to %.2f,%.2f is too long", (double)x,(double)y);
427             return -1;
428         } else {
429             /* split line */
430             int x1,y1,x2,y2;
431             if(x>=0) { x1 = x/2;x2 = (x+1)/2;} 
432             else     { x1 = x/2;x2 = (x-1)/2;}
433             if(y>=0) { y1 = y/2;y2 = (y+1)/2;} 
434             else     { y1 = y/2;y2 = (y-1)/2;}
435             swf_ShapeSetLine(t, s, x1,y1);
436             swf_ShapeSetLine(t, s, x2,y2);
437             return 0;
438         }
439     }
440
441     if(x!=0 && y!=0) { //(!s)||((x!=0)&&(y!=0)))
442         swf_SetBits(t,3,2); // Straight Edge
443         swf_SetBits(t, b-2, 4); //Number of Bits in x/y
444         swf_SetBits(t,1,1); // Diagonal
445         swf_SetBits(t,x,b);
446         swf_SetBits(t,y,b);
447     } else if (x==0) {
448         swf_SetBits(t,3,2); // Straight Edge
449         swf_SetBits(t, b-2, 4); //Number of Bits in y
450         swf_SetBits(t,1,2); // Vertical
451         swf_SetBits(t,y,b);
452     } else {
453         swf_SetBits(t,3,2); // Straight Edge
454         swf_SetBits(t, b-2, 4); //Number of Bits in x
455         swf_SetBits(t,0,2); // Horizontal
456         swf_SetBits(t,x,b);
457     }
458     return 0;
459 }
460
461 int swf_ShapeSetCurve(TAG * t,SHAPE * s,S32 x,S32 y,S32 ax,S32 ay)
462
463     U8 b;
464     if (!t) return -1;
465
466     b = swf_CountBits(ax,2);
467     b = swf_CountBits(ay,b);
468     b = swf_CountBits(x,b);
469     b = swf_CountBits(y,b);
470
471     if(b >= 18) {
472           fprintf(stderr, "Bit overflow in swf_ShapeSetCurve- %d (%d,%d,%d,%d)\n", b, ax,ay,x,y);
473           b = 17;
474     }
475
476     swf_SetBits(t,2,2);
477     swf_SetBits(t,b-2,4);
478     swf_SetBits(t,x,b);
479     swf_SetBits(t,y,b);
480     swf_SetBits(t,ax,b);
481     swf_SetBits(t,ay,b);
482     return 0;
483 }
484
485 int swf_ShapeSetCircle(TAG * t,SHAPE * s,S32 x,S32 y,S32 rx,S32 ry)
486 { double C1 = 0.2930;    
487   double C2 = 0.4140;   
488   double begin = 0.7070; 
489
490   if (!t) return -1;
491   
492   swf_ShapeSetMove(t,s,x+begin*rx,y+begin*ry);
493   swf_ShapeSetCurve(t,s, -C1*rx,  C1*ry, -C2*rx,      0);
494   swf_ShapeSetCurve(t,s, -C2*rx,      0, -C1*rx, -C1*ry);
495   swf_ShapeSetCurve(t,s, -C1*rx, -C1*ry,      0, -C2*ry);
496   swf_ShapeSetCurve(t,s,      0, -C2*ry,  C1*rx, -C1*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   
502   return 0;
503 }
504
505 /* todo: merge this with swf_GetSimpleShape */
506 SHAPELINE* swf_ParseShapeData(U8*data, int bits, int fillbits, int linebits)
507 {
508     SHAPELINE _lines;
509     SHAPELINE*lines = &_lines;
510
511     TAG _tag;
512     TAG* tag = &_tag;
513     int fill0 = 0;
514     int fill1 = 0;
515     int line = 0;
516     int x=0,y=0;
517     
518     memset(tag, 0, sizeof(TAG));
519     tag->data = data;
520     tag->len = tag->memsize = (bits+7)/8;
521     tag->pos = 0;
522
523     lines->next = 0;
524     while(1) {
525         int flags;
526         flags = swf_GetBits(tag, 1);
527         if(!flags) { //style change
528             flags = swf_GetBits(tag, 5);
529             if(!flags)
530                 break;
531             if(flags&1) { //move
532                 int n = swf_GetBits(tag, 5); 
533                 x = swf_GetSBits(tag, n); //x
534                 y = swf_GetSBits(tag, n); //y
535             }
536             if(flags&2)
537                 fill0 = swf_GetBits(tag, fillbits); 
538             if(flags&4)
539                 fill1 = swf_GetBits(tag, fillbits); 
540             if(flags&8)
541                 line = swf_GetBits(tag, linebits); 
542             if(flags&16) {
543                 fprintf(stderr, "Additional file styles style change not yet supported\n");
544                 exit(1);
545                 //enumerateUsedIDs_styles(tag, callback, callback_data, num);
546                 fillbits = swf_GetBits(tag, 4);
547                 linebits = swf_GetBits(tag, 4);
548             }
549             if(flags&1) { //move
550                 lines->next = (SHAPELINE*)malloc(sizeof(SHAPELINE));
551                 lines = lines->next;
552                 lines->type = moveTo;
553                 lines->x = x; 
554                 lines->y = y; 
555                 lines->sx = lines->sy = 0;
556                 lines->fillstyle0 = fill0;
557                 lines->fillstyle1 = fill1;
558                 lines->linestyle = line;
559                 lines->next = 0;
560             }
561         } else {
562             flags = swf_GetBits(tag, 1);
563             if(flags) { //straight edge
564                 int n = swf_GetBits(tag, 4) + 2;
565                 if(swf_GetBits(tag, 1)) { //line flag
566                     x += swf_GetSBits(tag, n); //delta x
567                     y += swf_GetSBits(tag, n); //delta y
568                 } else {
569                     int v=swf_GetBits(tag, 1);
570                     int d;
571                     d = swf_GetSBits(tag, n); //vert/horz
572                     if(v) y += d;
573                     else  x += d;
574                 }
575                 lines->next = (SHAPELINE*)malloc(sizeof(SHAPELINE));
576                 lines = lines->next;
577                 lines->type = lineTo;
578                 lines->x = x; 
579                 lines->y = y; 
580                 lines->sx = lines->sy = 0;
581                 lines->fillstyle0 = fill0;
582                 lines->fillstyle1 = fill1;
583                 lines->linestyle = line;
584                 lines->next = 0;
585             } else { //curved edge
586                 int n = swf_GetBits(tag, 4) + 2;
587                 int x1,y1;
588                 x += swf_GetSBits(tag, n);
589                 y += swf_GetSBits(tag, n);
590                 x1 = x;
591                 y1 = y;
592                 x += swf_GetSBits(tag, n);
593                 y += swf_GetSBits(tag, n);
594
595                 lines->next = (SHAPELINE*)malloc(sizeof(SHAPELINE));
596                 lines = lines->next;
597                 lines->type = splineTo;
598                 lines->sx = x1; 
599                 lines->sy = y1; 
600                 lines->x = x; 
601                 lines->y = y; 
602                 lines->fillstyle0 = fill0;
603                 lines->fillstyle1 = fill1;
604                 lines->linestyle = line;
605                 lines->next = 0;
606             }
607         }
608     }
609     return _lines.next;
610 }
611
612 SRECT swf_GetShapeBoundingBox(SHAPE2*shape2)
613 {
614     SRECT r;
615     SHAPELINE*l = shape2->lines;
616     int lastx=0,lasty=0;
617     int valid = 0;
618     r.xmin = r.ymin = SCOORD_MAX;
619     r.xmax = r.ymax = SCOORD_MIN;
620
621     while(l) {
622         int t1;
623         if(l->linestyle>0) {
624             t1 = shape2->linestyles[l->linestyle - 1].width*3/2;
625         } else {
626             t1 = 0;
627         }
628
629         if(l->type == lineTo || l->type == splineTo)
630         {
631             valid = 1;
632             if(lastx - t1 < r.xmin) r.xmin = lastx - t1;
633             if(lasty - t1 < r.ymin) r.ymin = lasty - t1;
634             if(lastx + t1 > r.xmax) r.xmax = lastx + t1;
635             if(lasty + t1 > r.ymax) r.ymax = lasty + t1;
636             if(l->x - t1 < r.xmin) r.xmin = l->x - t1;
637             if(l->y - t1 < r.ymin) r.ymin = l->y - t1;
638             if(l->x + t1 > r.xmax) r.xmax = l->x + t1;
639             if(l->y + t1 > r.ymax) r.ymax = l->y + t1;
640             if(l->type == splineTo) {
641                 if(l->sx - t1 < r.xmin) r.xmin = l->sx - t1;
642                 if(l->sy - t1 < r.ymin) r.ymin = l->sy - t1;
643                 if(l->sx + t1 > r.xmax) r.xmax = l->sx + t1;
644                 if(l->sy + t1 > r.ymax) r.ymax = l->sy + t1;
645             }
646         }
647         lastx = l->x;
648         lasty = l->y;
649         l = l->next;
650     }
651     if(!valid) memset(&r, 0, sizeof(SRECT));
652     return r;
653 }
654
655 void swf_Shape2Free(SHAPE2 * s)
656 {
657     SHAPELINE*line = s->lines;
658     while(line) {
659         SHAPELINE*next = line->next;
660         free(line);
661         line = next;
662     }
663     if(s->linestyles)
664         free(s->linestyles);
665     if(s->fillstyles)
666         free(s->fillstyles);
667     if(s->bbox)
668         free(s->bbox);
669 }
670
671 SHAPE2* swf_ShapeToShape2(SHAPE*shape) {
672
673     SHAPE2*shape2 = (SHAPE2*)malloc(sizeof(SHAPE2));
674     
675     shape2->numlinestyles = shape->linestyle.n;
676     shape2->linestyles = (LINESTYLE*)malloc(sizeof(LINESTYLE)*shape->linestyle.n);
677     memcpy(shape2->linestyles, shape->linestyle.data, sizeof(LINESTYLE)*shape->linestyle.n);
678     
679     shape2->numfillstyles = shape->fillstyle.n;
680     shape2->fillstyles = (FILLSTYLE*)malloc(sizeof(FILLSTYLE)*shape->fillstyle.n);
681     memcpy(shape2->fillstyles, shape->fillstyle.data, sizeof(FILLSTYLE)*shape->fillstyle.n);
682
683     shape2->lines = swf_ParseShapeData(shape->data, shape->bitlen, shape->bits.fill, shape->bits.line);
684     shape2->bbox = 0;
685     return shape2;
686 };
687
688 void swf_ShapeSetBitmapRect(TAG*tag, U16 gfxid, int width, int height)
689 {
690     SHAPE*shape;
691     MATRIX m;
692     RGBA rgb;
693     SRECT r;
694     int lines = 0;
695     int ls=0,fs;
696     swf_ShapeNew(&shape);
697     rgb.b = rgb.g = rgb.r = 0xff;
698     if(lines)
699         ls = swf_ShapeAddLineStyle(shape,20,&rgb);  
700     swf_GetMatrix(NULL,&m);
701     m.sx = 20*65536;
702     m.sy = 20*65536;
703
704     fs = swf_ShapeAddBitmapFillStyle(shape,&m,gfxid,0);
705     r.xmin = 0;
706     r.ymin = 0;
707     r.xmax = width*20;
708     r.ymax = height*20;
709     swf_SetRect(tag,&r);
710
711     swf_SetShapeStyles(tag,shape);
712     swf_ShapeCountBits(shape,NULL,NULL);
713     swf_SetShapeBits(tag,shape);
714
715     swf_ShapeSetAll(tag,shape,0,0,lines?ls:0,fs,0);
716
717     swf_ShapeSetLine(tag,shape,width*20,0);
718     swf_ShapeSetLine(tag,shape,0,height*20);
719     swf_ShapeSetLine(tag,shape,-width*20,0);
720     swf_ShapeSetLine(tag,shape,0,-height*20);
721     swf_ShapeSetEnd(tag);
722     swf_ShapeFree(shape);
723 }
724
725 void swf_Shape2ToShape(SHAPE2*shape2, SHAPE*shape)
726 {
727     TAG*tag = swf_InsertTag(0,0);
728     SHAPELINE*l,*next;
729     int newx=0,newy=0,lastx=0,lasty=0,oldls=0,oldfs0=0,oldfs1=0;
730
731     memset(shape, 0, sizeof(SHAPE));
732
733     shape->linestyle.n = shape2->numlinestyles;
734     shape->linestyle.data = (LINESTYLE*)malloc(sizeof(LINESTYLE)*shape->linestyle.n);
735     memcpy(shape->linestyle.data, shape2->linestyles, sizeof(LINESTYLE)*shape->linestyle.n);
736     
737     shape->fillstyle.n =  shape2->numfillstyles;
738     shape->fillstyle.data = (FILLSTYLE*)malloc(sizeof(FILLSTYLE)*shape->fillstyle.n);
739     memcpy(shape->fillstyle.data, shape2->fillstyles, sizeof(FILLSTYLE)*shape->fillstyle.n);
740
741     swf_ShapeCountBits(shape,NULL,NULL);
742
743     l = shape2->lines;
744
745     while(l) {
746         int ls=0,fs0=0,fs1=0;
747
748         if(l->type != moveTo) {
749             if(oldls != l->linestyle) {oldls = ls = l->linestyle;if(!ls) ls=0x8000;}
750             if(oldfs0 != l->fillstyle0) {oldfs0 = fs0 = l->fillstyle0;if(!fs0) fs0=0x8000;}
751             if(oldfs1 != l->fillstyle1) {oldfs1 = fs1 = l->fillstyle1;if(!fs1) fs1=0x8000;}
752
753             if(ls || fs0 || fs1 || newx!=0x7fffffff || newy!=0x7fffffff) {
754                 swf_ShapeSetAll(tag,shape,newx,newy,ls,fs0,fs1);
755                 newx = 0x7fffffff;
756                 newy = 0x7fffffff;
757             }
758         }
759
760         if(l->type == lineTo) {
761             swf_ShapeSetLine(tag,shape,l->x-lastx,l->y-lasty);
762         } else if(l->type == splineTo) {
763             swf_ShapeSetCurve(tag,shape, l->sx-lastx,l->sy-lasty, l->x-l->sx,l->y-l->sy);
764         }
765         if(l->type == moveTo) {
766             newx = l->x;
767             newy = l->y;
768         }
769
770         lastx = l->x;
771         lasty = l->y;
772         l = l->next;
773     }
774     swf_ShapeSetEnd(tag);
775     shape->data = tag->data;
776     shape->bitlen = tag->len*8;
777 }
778
779 void swf_SetShape2(TAG*tag, SHAPE2*shape2)
780 {
781     SHAPE shape;
782     swf_Shape2ToShape(shape2, &shape);
783
784     swf_SetRect(tag,shape2->bbox);
785     swf_SetShapeStyles(tag, &shape);
786     swf_ShapeCountBits(&shape,NULL,NULL);
787     swf_SetShapeBits(tag,&shape);
788
789     swf_SetBlock(tag, shape.data, (shape.bitlen+7)/8);
790 }
791
792 static void parseFillStyleArray(TAG*tag, SHAPE2*shape)
793 {
794     U16 count;
795     int t;
796     int num=0;
797     if(tag->id == ST_DEFINESHAPE)
798         num = 1;
799     else if(tag->id == ST_DEFINESHAPE2)
800         num = 2;
801     else if(tag->id == ST_DEFINESHAPE3)
802         num = 3;
803
804     count = swf_GetU8(tag);
805     if(count == 0xff && num>1) // defineshape2,3 only
806         count = swf_GetU16(tag);
807
808     shape->numfillstyles = count;
809     shape->fillstyles = malloc(sizeof(FILLSTYLE)*count);
810
811     for(t=0;t<count;t++)
812     {
813         int type;
814         U8*pos;
815         FILLSTYLE*dest = &shape->fillstyles[t];
816         type = swf_GetU8(tag); //type
817         shape->fillstyles[t].type = type;
818         if(type == 0) {
819             /* plain color */
820             if(num == 3)
821                 swf_GetRGBA(tag, &dest->color);
822             else 
823                 swf_GetRGB(tag, &dest->color);
824         }
825         else if(type == 0x10 || type == 0x12)
826         {
827             /* linear/radial gradient fill */
828             swf_ResetReadBits(tag);
829             swf_GetMatrix(tag, &dest->m);
830             swf_ResetReadBits(tag);
831             swf_GetGradient(tag, &dest->gradient, num>=3?1:0);
832         }
833         else if(type == 0x40 || type == 0x41)
834         {
835             /* bitmap fill */
836             swf_ResetReadBits(tag);
837             dest->id_bitmap = swf_GetU16(tag); //id
838             swf_ResetReadBits(tag); //?
839             swf_GetMatrix(tag, &dest->m);
840         }
841         else {
842             fprintf(stderr, "rfxswf:swftools.c Unknown fillstyle:0x%02x\n",type);
843         }
844     }
845     swf_ResetReadBits(tag);
846     count = swf_GetU8(tag); // line style array
847     if(count == 0xff)
848         count = swf_GetU16(tag);
849
850     //if(verbose) printf("lnum: %d\n", count);
851
852     shape->numlinestyles = count;
853     shape->linestyles = malloc(sizeof(LINESTYLE)*count);
854     /* TODO: should we start with 1 and insert a correct definition of the
855        "built in" linestyle 0? */
856     for(t=0;t<count;t++) 
857     {
858         shape->linestyles[t].width = swf_GetU16(tag);
859         if(num == 3)
860             swf_GetRGBA(tag, &shape->linestyles[t].color);
861         else
862             swf_GetRGB(tag, &shape->linestyles[t].color);
863     }
864     return;
865 }
866
867 void swf_ParseDefineShape(TAG*tag, SHAPE2*shape)
868 {
869     int num = 0, id;
870     U16 fill,line;
871     SRECT r;
872     SRECT r2;
873     SHAPELINE*l;
874     if(tag->id == ST_DEFINESHAPE)
875         num = 1;
876     else if(tag->id == ST_DEFINESHAPE2)
877         num = 2;
878     else if(tag->id == ST_DEFINESHAPE3)
879         num = 3;
880     else {
881         fprintf(stderr, "parseDefineShape must be called with a shape tag");
882     }
883
884     id = swf_GetU16(tag); //id
885     memset(shape, 0, sizeof(SHAPE2));
886     shape->bbox = malloc(sizeof(SRECT));
887     swf_GetRect(tag, &r);
888
889     memcpy(shape->bbox, &r, sizeof(SRECT));
890     parseFillStyleArray(tag, shape);
891
892     swf_ResetReadBits(tag); 
893     fill = (U16)swf_GetBits(tag,4);
894     line = (U16)swf_GetBits(tag,4);
895
896     shape->lines = swf_ParseShapeData(&tag->data[tag->pos], (tag->len - tag->pos)*8, fill, line);
897
898     l = shape->lines;
899 }
900