added some comments.
[swftools.git] / lib / modules / swfobject.c
1 /* swfobject.c
2
3    Object place and move routines
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 PF_MOVE         0x01
25 #define PF_CHAR         0x02
26 #define PF_MATRIX       0x04
27 #define PF_CXFORM       0x08
28 #define PF_RATIO        0x10
29 #define PF_NAME         0x20
30 #define PF_CLIPACTION   0x40
31 #define PF_ACTIONEVENT  0x80
32
33 int isUnitMatrix(MATRIX* m)
34 {
35     /* a matrix with all zeros is also considered
36        "unit matrix", as a zeroed out MATRIX structure
37        usually means that the caller doesn't want to
38        set the matrix */
39     if((   (m->sx == 0x10000 && m->sy == 0x10000) 
40         || (m->sx == 0 && m->sy == 0))
41                 && ((m->r0|m->r1|m->tx|m->ty) == 0)
42       )
43         return 1;
44     return 0;
45 }
46
47 int isUnitCXForm(CXFORM* cx)
48 {
49     if((cx->a0==256 && cx->r0==256 && cx->g0==256 && cx->b0==256) &&
50        (cx->a1==0 && cx->r1==0 && cx->g1==0 && cx->b1==0))
51         return 1;
52     /* A CXForm of all zeros is, unfortunately, not as unlikely
53        as a matrix of all zeros. However, we still treat it
54        as non-existent/uniform transform */
55     if((cx->a0==0 && cx->r0==0 && cx->g0==0 && cx->b0==0) &&
56        (cx->a1==0 && cx->r1==0 && cx->g1==0 && cx->b1==0))
57         return 1;
58     return 0;
59 }
60
61 int swf_ObjectPlace(TAG * t,U16 id,U16 depth,MATRIX * m,CXFORM * cx,U8 * name)
62 { U8 flags;
63   if (!t) return -1;
64
65   if(cx && cx->r1==0 && cx->g1==0 && cx->b1==0 && cx->a1==0
66         && cx->r0==256 && cx->g0==256 && cx->b0==256 && cx->a0==256)
67       cx = 0;
68
69   if(m && isUnitMatrix(m)) 
70       m = 0;
71
72   flags = (id?PF_CHAR:0)|(m?PF_MATRIX:0)|(cx?PF_CXFORM:0)|(name?PF_NAME:0)|((m||cx)&&(!id)?PF_MOVE:0);
73
74   swf_SetU8(t,flags);
75   swf_SetU16(t,depth);
76   if (flags&PF_CHAR) swf_SetU16(t,id);
77   if (flags&PF_MATRIX) swf_SetMatrix(t,m);
78   if (flags&PF_CXFORM) swf_SetCXForm(t,cx,1);
79   if (flags&PF_RATIO) swf_SetU16(t,0);
80   if (flags&PF_NAME) swf_SetString(t,name);
81   
82   return 0; 
83 }
84
85 int swf_ObjectPlaceClip(TAG * t,U16 id,U16 depth,MATRIX * m,CXFORM * cx,U8 * name, U16 clipaction)
86 { U8 flags;
87   if (!t) return -1;
88   
89   if(cx && cx->r1==0 && cx->g1==0 && cx->b1==0 && cx->a1==0
90         && cx->r0==256 && cx->g0==256 && cx->b0==256 && cx->a0==256)
91       cx = 0;
92   
93   if(m && isUnitMatrix(m)) 
94       m = 0;
95
96   flags = (id?PF_CHAR:0)|(m?PF_MATRIX:0)|(cx?PF_CXFORM:0)|(name?PF_NAME:0)|
97           ((m||cx)&&(!id)?PF_MOVE:0)|(clipaction?PF_CLIPACTION:0);
98
99   swf_SetU8(t,flags);
100   swf_SetU16(t,depth);
101   if (flags&PF_CHAR) swf_SetU16(t,id);
102   if (flags&PF_MATRIX) swf_SetMatrix(t,m);
103   if (flags&PF_CXFORM) swf_SetCXForm(t,cx,1);
104   if (flags&PF_RATIO) swf_SetU16(t,0);
105
106   /* ??? The spec states that name comes first? */
107   if (flags&PF_CLIPACTION) swf_SetU16(t, clipaction);
108   if (flags&PF_NAME) swf_SetString(t,name);
109   return 0; 
110 }
111
112 int swf_ObjectMove(TAG * t,U16 depth,MATRIX * m,CXFORM * cx)
113 { return swf_ObjectPlace(t,0,depth,m,cx,NULL);
114 }
115
116 void swf_SetPlaceObject(TAG * t,SWFPLACEOBJECT* obj)
117
118     U8 flags;
119     if (!t) return ;
120     if(t->id == ST_PLACEOBJECT) {
121         swf_SetU16(t, obj->id);
122         swf_SetU16(t, obj->depth);      
123         swf_SetMatrix(t, &obj->matrix);
124         swf_SetCXForm(t, &obj->cxform, 0);
125     } else {
126         int m = !isUnitMatrix(&obj->matrix);
127         int cx = !isUnitCXForm(&obj->cxform);
128
129         flags = (obj->id?PF_CHAR:0)|(m?PF_MATRIX:0)|(cx?PF_CXFORM:0)|(obj->ratio?PF_RATIO:0)|
130                 (obj->name?PF_NAME:0)|(obj->move?PF_MOVE:0)|
131                 (obj->clipdepth?PF_CLIPACTION:0);
132
133         swf_SetU8(t,flags);
134         swf_SetU16(t,obj->depth);
135         if (flags&PF_CHAR) swf_SetU16(t,obj->id);
136         if (flags&PF_MATRIX) swf_SetMatrix(t,&obj->matrix);
137         if (flags&PF_CXFORM) swf_SetCXForm(t,&obj->cxform,1);
138         if (flags&PF_RATIO) swf_SetU16(t,obj->ratio);
139   
140         /* ??? The spec states that name comes first? */
141         if (flags&PF_CLIPACTION) swf_SetU16(t,obj->clipdepth);
142         if (flags&PF_NAME) swf_SetString(t,obj->name);
143         if (flags&PF_ACTIONEVENT) {
144             // ...
145         }
146     }
147 }
148
149 void swf_GetPlaceObject(TAG * tag,SWFPLACEOBJECT* obj)
150 {
151     U8 flags;
152     if(!tag) {
153         memset(obj, 0, sizeof(SWFPLACEOBJECT));
154         swf_GetMatrix(0, &obj->matrix);
155         swf_GetCXForm(0, &obj->cxform, 1);
156         //obj->internal = PF_CHAR|PF_MATRIX|PF_CXFORM;
157         return;
158     }
159     swf_SetTagPos(tag, 0);
160     
161     if(tag->id == ST_PLACEOBJECT) {
162         obj->id = swf_GetU16(tag);
163         obj->depth = swf_GetU16(tag);
164         swf_GetMatrix(tag, &obj->matrix);
165         swf_GetCXForm(tag, &obj->cxform, 0);
166         //obj->internal = PF_CHAR|PF_MATRIX|PF_CXFORM;
167     } else if(tag->id == ST_PLACEOBJECT2) {
168         flags = swf_GetU8(tag);
169         memset(obj,0,sizeof(SWFPLACEOBJECT));
170             
171         swf_GetMatrix(0,&obj->matrix);
172         swf_GetCXForm(0,&obj->cxform,1);
173
174         obj->depth = swf_GetU16(tag);
175         //obj->internal = flags;
176         //flags&1: move
177         if(flags&2) obj->id = swf_GetU16(tag);
178         if(flags&4) swf_GetMatrix(tag, &obj->matrix);
179         if(flags&8) swf_GetCXForm(tag, &obj->cxform,1);
180         if(flags&16) obj->ratio = swf_GetU16(tag);
181         /* if you modify the order of these operations, also
182            modify it in ../src/swfcombine.c */
183         if(flags&64) 
184             obj->clipdepth = swf_GetU16(tag); //clip
185         if(flags&32) {
186             int l,t;
187             U8*data;
188             swf_ResetReadBits(tag);
189             l = strlen(&tag->data[tag->pos]);
190             t = 0;
191             data = rfx_alloc(l+1);
192             obj->name = data;
193             while((data[t++] = swf_GetU8(tag))); 
194         }
195
196         /* Actionscript ignored (for now) */
197         obj->actions = 0;
198     } else {
199         fprintf(stderr, "rfxswf: Bad Tag: %d not a placeobject\n", tag->id);
200     }
201 }
202
203 void swf_PlaceObjectFree(SWFPLACEOBJECT* obj)
204 {
205     if(obj->name)
206         rfx_free(obj->name);
207 }
208