added optimized routines for actioncode generation
[swftools.git] / lib / rfxswf.h
1 /* rfxswf.h
2
3    Headers for rfxswf.c and modules
4
5    Part of the swftools package.
6
7    Copyright (c) 2000, 2001 Rainer Böhme <rfxswf@reflex-studio.de>
8  
9    This file is distributed under the GPL, see file COPYING for details 
10
11 */
12
13 #ifndef __RFX_SWF_INCLUDED__
14 #define __RFX_SWF_INCLUDED__
15
16 #include <stdio.h>
17 #include <stdlib.h>
18 #include <string.h>
19 #include "../config.h"
20
21 #define DEBUG_RFXSWF
22
23 // SWF Types
24
25 typedef         unsigned long   U32;
26 typedef         signed long     S32;
27 typedef         unsigned short  U16;
28 typedef         signed short    S16;
29 typedef         unsigned char   U8;
30 typedef         signed char     S8;
31 typedef         signed long     SFIXED;
32 typedef         signed long     SCOORD;
33
34 // Basic Structures
35
36 typedef struct _SPOINT
37 { SCOORD        x;
38   SCOORD        y;
39 } SPOINT, * LPSPOINT;
40
41 typedef struct _RGBA
42 { U8    a;
43   U8    r;
44   U8    g;
45   U8    b;
46 } RGBA, * LPRGBA;
47
48 typedef struct _SRECT
49 { SCOORD        xmin;
50   SCOORD        ymin;
51   SCOORD        xmax;
52   SCOORD        ymax;
53 } SRECT, * LPSRECT;
54
55 typedef struct _MATRIX
56 { SFIXED        sx;     // factor x
57   SFIXED        sy;
58   SFIXED        r0;     // rotation
59   SFIXED        r1;
60   SCOORD        tx;     // delta x
61   SCOORD        ty;
62 } MATRIX, * LPMATRIX;
63
64 typedef struct _CXFORM
65 { S16           a0, a1;
66   S16           r0, r1;
67   S16           g0, g1;
68   S16           b0, b1;
69 } CXFORM, * LPCXFORM;
70
71 typedef struct _TAG             // NEVER access a Tag-Struct directly !
72 { U16           id;
73   U32           len;
74   U8 *          data;
75
76   int           frame;
77
78   struct _TAG * next;
79   struct _TAG * prev;
80
81   U32           memsize;        // to minimize realloc() calls
82   U32           pos;            // for Get/Set-Access
83   U8            bitmask;        // for Bit-Manipulating Functions [read]
84   U8            bitcount;       // [write]
85 } TAG, * LPTAG;
86
87 typedef struct _ActionTAG 
88 { U8            op;
89   U16           len;
90   U8 *          data;
91
92   struct _ActionTAG * next;
93   struct _ActionTAG * prev;
94
95   TAG* parent; // may be null
96   U8 tmp[4]; // store small operands here.
97 } ActionTAG;
98
99
100 typedef struct _SWF
101 { U8            fileVersion;
102   U32           fileSize;       // valid after load and save
103   SRECT         movieSize;
104   U16           frameRate;
105   U16           frameCount;     // valid after load and save
106   TAG *         firstTag;
107 } SWF, * LPSWF;
108
109 // Basic Functions
110
111 int  swf_ReadSWF(int handle,SWF * swf);     // Reads SWF to memory (malloc'ed), returns length or <0 if fails
112 int  swf_WriteSWF(int handle,SWF * swf);    // Writes SWF to file, returns length or <0 if fails
113 int  swf_WriteCGI(SWF * swf);               // Outputs SWF with valid CGI header to stdout
114 void swf_FreeTags(SWF * swf);               // Frees all malloc'ed memory for swf
115     
116 TAG * swf_InsertTag(TAG * after,U16 id);    // updates frames, if necessary
117 int   swf_DeleteTag(TAG * t);
118
119 void  swf_SetTagPos(TAG * t,U32 pos);       // resets Bitcount
120 U32   swf_GetTagPos(TAG * t);
121
122 TAG * swf_NextTag(TAG * t);
123 TAG * swf_PrevTag(TAG * t);
124
125 int   swf_GetFrameNo(TAG * t);              // should be renamed to TagGetFrame
126 U16   swf_GetTagID(TAG * t);                // ... TagGetID
127 U32   swf_GetDataSize(TAG * t);             // ... TagGetDataSize
128 U8*   swf_GetDataSizePtr(TAG * t);
129
130 U32   swf_GetBits(TAG * t,int nbits);
131 S32   swf_GetSBits(TAG * t,int nbits);
132 int   swf_SetBits(TAG * t,U32 v,int nbits);
133
134 int   swf_GetBlock(TAG * t,U8 * b,int l);   // resets Bitcount
135 int   swf_SetBlock(TAG * t,U8 * b,int l);
136
137 U8    swf_GetU8(TAG * t);                   // resets Bitcount
138 U16   swf_GetU16(TAG * t);
139 U32   swf_GetU32(TAG * t);
140
141 int   swf_SetU8(TAG * t,U8 v);              // resets Bitcount
142 int   swf_SetU16(TAG * t,U16 v);
143 int   swf_SetU32(TAG * t,U32 v);
144
145 int   swf_GetPoint(TAG * t,SPOINT * p);     // resets Bitcount
146 int   swf_GetRect(TAG * t,SRECT * r);
147 int   swf_GetMatrix(TAG * t,MATRIX * m);
148 int   swf_GetCXForm(TAG * t,CXFORM * cx,U8 alpha);
149
150 int   swf_SetPoint(TAG * t,SPOINT * p);     // resets Bitcount
151 int   swf_SetRect(TAG * t,SRECT * r);
152 int   swf_SetMatrix(TAG * t,MATRIX * m);
153 int   swf_SetCXForm(TAG * t,CXFORM * cx,U8 alpha);
154 int   swf_SetRGB(TAG * t,RGBA * col);
155 int   swf_SetRGBA(TAG * t,RGBA * col);
156
157 // Function Macros
158
159 #define swf_GetS8(tag)      ((S8)swf_GetU8(tag))
160 #define swf_GetS16(tag)     ((S16)swf_GetU16(tag))
161 #define swf_GetS32(tag)     ((S32)swf_GetU32(tag))
162 #define swf_GetCoord(tag)   ((SCOORD)swf_GetU32(tag))
163 #define swf_GetFixed(tag)   ((SFIXED)swf_GetU32(tag))
164
165 #define swf_SetS8(tag,v)    swf_SetU8(tag,(U8)v)
166 #define swf_SetS16(tag,v)   swf_SetU16(tag,(U16)v)
167 #define swf_SetS32(tag,v)   swf_SetU32(tag,(U32)v)
168 #define swf_SetCoord(tag,v) swf_SetU32(tag,(U32)v)
169 #define swf_SetFixed(tag,v) swf_SetU32(tag,(U32)v)
170 #define swf_SetString(t,s)  swf_SetBlock(t,s,strlen(s)+1)
171
172 #define FAILED(b)       ((b)<0)
173 #define SUCCEDED(b)     ((b)>=0)
174
175 // Tag IDs (adopted from J. C. Kessels' Form2Flash)
176
177 #define ST_END                  0
178 #define ST_SHOWFRAME            1
179 #define ST_DEFINESHAPE          2
180 #define ST_FREECHARACTER        3
181 #define ST_PLACEOBJECT          4
182 #define ST_REMOVEOBJECT         5
183 #define ST_DEFINEBITS           6
184 #define ST_DEFINEBUTTON         7
185 #define ST_JPEGTABLES           8
186 #define ST_SETBACKGROUNDCOLOR   9
187 #define ST_DEFINEFONT           10
188 #define ST_DEFINETEXT           11
189 #define ST_DOACTION             12
190 #define ST_DEFINEFONTINFO       13
191 #define ST_DEFINESOUND          14 /* Event sound tags. */
192 #define ST_STARTSOUND           15
193 #define ST_DEFINEBUTTONSOUND    17
194 #define ST_SOUNDSTREAMHEAD      18
195 #define ST_SOUNDSTREAMBLOCK     19
196 #define ST_DEFINEBITSLOSSLESS   20 /* A bitmap using lossless zlib compression. */
197 #define ST_DEFINEBITSJPEG2      21 /* A bitmap using an internal JPEG compression table. */
198 #define ST_DEFINESHAPE2         22
199 #define ST_DEFINEBUTTONCXFORM   23
200 #define ST_PROTECT              24 /* This file should not be importable for editing. */
201 #define ST_PLACEOBJECT2         26 /* The new style place w/ alpha color transform and name. */
202 #define ST_REMOVEOBJECT2        28 /* A more compact remove object that omits the character tag (just depth). */
203 #define ST_DEFINESHAPE3         32 /* A shape V3 includes alpha values. */
204 #define ST_DEFINETEXT2          33 /* A text V2 includes alpha values. */
205 #define ST_DEFINEBUTTON2        34 /* A button V2 includes color transform, alpha and multiple actions */
206 #define ST_DEFINEBITSJPEG3      35 /* A JPEG bitmap with alpha info. */
207 #define ST_DEFINEBITSLOSSLESS2  36 /* A lossless bitmap with alpha info. */
208 #define ST_DEFINEEDITTEXT       37
209 #define ST_DEFINEMOVIE          38
210 #define ST_DEFINESPRITE         39 /* Define a sequence of tags that describe the behavior of a sprite. */
211 #define ST_NAMECHARACTER        40 /* Name a character definition, character id and a string, (used for buttons, bitmaps, sprites and sounds). */
212 #define ST_SERIALNUMBER         41
213 #define ST_GENERATORTEXT        42 /* contains an id */
214 #define ST_FRAMELABEL           43 /* A string label for the current frame. */
215 #define ST_SOUNDSTREAMHEAD2     45 /* For lossless streaming sound, should not have needed this... */
216 #define ST_DEFINEMORPHSHAPE     46 /* A morph shape definition */
217 #define ST_DEFINEFONT2          48
218 #define ST_TEMPLATECOMMAND      49
219 #define ST_GENERATOR3           51
220 #define ST_EXTERNALFONT         52
221
222 #define ST_REFLEX              777 /* to identify generator software */
223
224 // Advanced Funtions
225
226 // swfdump.c
227
228 void swf_DumpHeader(FILE * f,SWF * swf);
229 void swf_DumpMatrix(FILE * f,MATRIX * m);
230 void swf_DumpTag(FILE * f,TAG * t); 
231 char* swf_TagGetName(TAG*tag);
232
233 // swfshape.c
234
235 typedef struct _LINESTYLE
236 { U16           width;
237   RGBA          color;
238 } LINESTYLE, * LPLINESTYLE;
239
240 typedef struct _FILLSTYLE
241 { U8     type;
242   RGBA   color;
243   MATRIX         m; 
244   U16    id_bitmap;
245 } FILLSTYLE, * LPFILLSTYLE;
246      
247 typedef struct _SHAPE           // NEVER access a Shape-Struct directly !
248 {                 
249   struct
250   { LINESTYLE * data;
251     U16         n;
252   } linestyle;
253                   // note: changes of shape structure
254   struct                  // lead to incompatible .efont formats
255   { FILLSTYLE * data;
256     U16         n;
257   } fillstyle;
258
259   S32           px;
260   S32           py;
261   
262   struct
263   { U16         fill;
264     U16         line;
265   } bits;
266   
267   U8 *          data;
268   U32           bitlen;         // length of data in bits
269 } SHAPE, * LPSHAPE;
270
271 // Shapes
272
273 int   swf_ShapeNew(SHAPE ** s);
274 void  swf_ShapeFree(SHAPE * s);
275
276 int   swf_GetSimpleShape(TAG * t,SHAPE ** s); // without Linestyle/Fillstyle Record
277 int   swf_SetSimpleShape(TAG * t,SHAPE * s);   // without Linestyle/Fillstyle Record
278
279 int   swf_ShapeAddLineStyle(SHAPE * s,U16 width,RGBA * color);
280 int   swf_ShapeAddSolidFillStyle(SHAPE * s,RGBA * color);
281 int   swf_ShapeAddBitmapFillStyle(SHAPE * s,MATRIX * m,U16 id_bitmap,int clip);
282
283 int   swf_SetShapeStyles(TAG * t,SHAPE * s);
284 int   swf_ShapeCountBits(SHAPE * s,U8 * fbits,U8 * lbits);
285 int   swf_SetShapeBits(TAG * t,SHAPE * s);
286 int   swf_SetShapeHeader(TAG * t,SHAPE * s); // one call for upper three functions
287
288 int   swf_ShapeSetMove(TAG * t,SHAPE * s,S32 x,S32 y);
289 int   swf_ShapeSetStyle(TAG * t,SHAPE * s,U16 line,U16 fill0,U16 fill1);
290 int   swf_ShapeSetAll(TAG * t,SHAPE * s,S32 x,S32 y,U16 line,U16 fill0,U16 fill1);
291
292 int   swf_ShapeSetLine(TAG * t,SHAPE * s,S32 x,S32 y);
293 int   swf_ShapeSetCurve(TAG * t,SHAPE * s,S32 x,S32 y,S32 ax,S32 ay);
294 int   swf_ShapeSetCircle(TAG * t,SHAPE * s,S32 x,S32 y,S32 rx,S32 ry);
295 int   swf_ShapeSetEnd(TAG * t);
296
297
298 // swffont.c
299
300 // does not support wide characters !
301
302 #define MAX_CHAR_PER_FONT 256
303
304 typedef struct _SWFLAYOUT
305 { S16         ascent;
306   S16         descent;
307   S16         leading;
308   SRECT       bounds[MAX_CHAR_PER_FONT];
309   struct
310   { U16       count;
311     U8 *      data;  // size = count*4 bytes
312   } kerning;
313 } SWFLAYOUT, * LPSWFLAYOUT;
314
315 typedef struct _SWFFONT
316 { U16           id;
317   U8 *          name;
318   SWFLAYOUT *   layout;
319
320   U8            flags; // bold/italic/unicode/ansi ...
321
322   U16           codes[MAX_CHAR_PER_FONT];
323   
324   struct
325   { U16         advance;
326     U16         gid;            // Glyph-ID after DefineFont
327     SHAPE *     shape;
328   }             glyph[MAX_CHAR_PER_FONT];
329 } SWFFONT, * LPSWFFONT;
330
331 typedef struct _FONTUSAGE
332 { U8 code[MAX_CHAR_PER_FONT];
333 } FONTUSAGE, * LPFONTUSAGE;
334
335 int swf_FontEnumerate(SWF * swf,void (*FontCallback) (U16,U8*));
336 // -> void fontcallback(U16 id,U8 * name); returns number of defined fonts
337
338 int swf_FontExtract(SWF * swf,int id,SWFFONT ** f);
339 // Fetches all available information from DefineFont, DefineFontInfo, DefineText, ...
340 // id = FontID, id=0 -> Extract first Font
341
342 int swf_FontIsItalic(SWFFONT * f);
343 int swf_FontIsBold(SWFFONT * f);
344
345 int swf_FontSetID(SWFFONT * f,U16 id);
346 int swf_FontReduce(SWFFONT * f,FONTUSAGE * use);
347
348 int swf_FontInitUsage(FONTUSAGE * use);
349 int swf_FontUse(FONTUSAGE * use,U8 * s);
350
351 int swf_FontSetDefine(TAG * t,SWFFONT * f);
352 int swf_FontSetInfo(TAG * t,SWFFONT * f);
353
354 int swf_FontExport(int handle,SWFFONT * f);
355 int swf_FontImport(int handle,SWFFONT * * f);
356
357 void swf_FontFree(SWFFONT * f);
358
359 U32 swf_TextGetWidth(SWFFONT * font,U8 * s,int scale);
360 int swf_TextCountBits(SWFFONT * font,U8 * s,int scale,U8 * gbits,U8 * abits);
361
362 int swf_TextSetInfoRecord(TAG * t,SWFFONT * font,U16 size,RGBA * color,S16 dx,S16 dy);
363 int swf_TextSetCharRecord(TAG * t,SWFFONT * font,U8 * s,int scale,U8 gbits,U8 abits);
364
365 int swf_TextPrintDefineText(TAG * t,SWFFONT * f);
366 // Prints text defined in tag t with font f to stdout
367
368
369 // swfobject.c
370
371 // Always use ST_PLACEOBJECT2 !!!
372
373 int swf_ObjectPlace(TAG * t,U16 id,U16 depth,MATRIX * m,CXFORM * cx,U8 * name);
374 int swf_ObjectPlaceClip(TAG * t,U16 id,U16 depth,MATRIX * m,CXFORM * cx,U8 * name, U16 clipaction);
375 int swf_ObjectMove(TAG * t,U16 depth,MATRIX * m,CXFORM * cx);
376
377 // swfbutton.c
378
379 // Button States
380
381 #define BS_HIT          0x08
382 #define BS_DOWN         0x04
383 #define BS_OVER         0x02
384 #define BS_UP           0x01
385
386 // Button Conditions
387
388 #define BC_OVERDOWN_IDLE        0x0100
389 #define BC_IDLE_OVERDOWN        0x0080
390 #define BC_OUTDOWN_IDLE         0x0040
391 #define BC_OUTDOWN_OVERDOWN     0x0020
392 #define BC_OVERDOWN_OUTDOWN     0x0010
393 #define BC_OVERDOWN_OVERUP      0x0008
394 #define BC_OVERUP_OVERDOWN      0x0004
395 #define BC_OVERUP_IDLE          0x0002
396 #define BC_IDLE_OVERUP          0x0001
397
398 #define BC_KEY(c) (c<<9)
399
400 #define BC_CURSORLEFT           0x0200
401 #define BC_CURSORRIGHT          0x0400
402 #define BC_POS1                 0x0600
403 #define BC_END                  0x0800
404 #define BC_INSERT               0x0a00
405 #define BC_DELETE               0x0c00
406 #define BC_BACKSPACE            0x1000
407 #define BC_ENTER                0x1a00
408 #define BC_CURSORUP             0x1c00
409 #define BC_CURSORDOWN           0x1e00
410 #define BC_PAGEUP               0x2000
411 #define BC_PAGEDOWN             0x2200
412 #define BC_TAB                  0x2400
413 #define BC_SPACE                0x4000
414
415 // Button Flag
416
417 #define BF_TRACKMENU            0x01
418
419 int swf_ButtonSetRecord(TAG * t,U8 state,U16 id,U16 layer,MATRIX * m,CXFORM * cx);
420 int swf_ButtonSetCondition(TAG * t,U16 condition); // for DefineButton2
421 int swf_ButtonSetFlags(TAG * t,U8 flags);  // necessary for DefineButton2
422 int swf_ButtonPostProcess(TAG * t,int anz_action); // Set all offsets in DefineButton2-Tags (how many conditions to process)
423
424 // swfbits.c
425
426 typedef int JPEGBITS,* LPJPEGBITS; // cover libjpeg structures
427
428 JPEGBITS * swf_SetJPEGBitsStart(TAG * t,int width,int height,int quality);
429 int swf_SetJPEGBitsLines(JPEGBITS * jpegbits,U8 ** data,int n);
430 int swf_SetJPEGBitsLine(JPEGBITS * jpegbits,U8 * data);
431 int swf_SetJPEGBitsFinish(JPEGBITS * jpegbits);
432
433 int swf_SetJPEGBits(TAG * t,char * fname,int quality); // paste jpg file into swf stream
434
435 #define BYTES_PER_SCANLINE(width) ((width+3)&0xfffffffc)
436
437 #define BMF_8BIT        3               // Bitmap formats
438 #define BMF_16BIT       4
439 #define BMF_32BIT       5
440
441 #define BM16_BLUE       0xf800          // Bitmasks for 16 Bit Color
442 #define BM16_RED        0x00f0
443 #define BM16_GREEN      0x000f
444
445 #define BM32_BLUE       0xff000000      // Bitmasks for 32 Bit Color
446 #define BM32_GREEN      0x00ff0000
447 #define BM32_RED        0x0000ff00
448
449 int swf_SetLosslessBits(TAG * t,U16 width,U16 height,void * bitmap,U8 bitmap_flags);
450 int swf_SetLosslessBitsIndexed(TAG * t,U16 width,U16 height,U8 * bitmap,RGBA * palette,U16 ncolors);
451 int swf_SetLosslessBitsGrayscale(TAG * t,U16 width,U16 height,U8 * bitmap);
452
453 // swftools.c
454
455 U8 swf_isDefiningTag(TAG * t);
456 U8 swf_isAllowedSpriteTag(TAG * t);
457 U16 swf_GetDefineID(TAG * t);
458 U16 swf_GetPlaceID(TAG * t); //PLACEOBJECT, PLACEOBJECT2 (sometimes), REMOVEOBJECT
459 U16 swf_GetDepth(TAG * t); //PLACEOBJECT,PLACEOBJECT2,REMOVEOBJECT,REMOVEOBJECT2
460 char* swf_GetTagName(TAG * t); //PLACEOBJECT2, FRAMELABEL
461 MATRIX * swf_MatrixJoin(MATRIX * d,MATRIX * s1,MATRIX * s2);
462 MATRIX * swf_MatrixMapTriangle(MATRIX * m,int dx,int dy,
463                     int x0,int y0,int x1,int y1,int x2,int y2);
464
465
466 // swfcgi.c
467
468 void swf_uncgi();  // same behaviour as Steven Grimm's uncgi-library
469
470 // swfaction.c
471
472 ActionTAG* swf_GetActions(TAG*tag);
473 void swf_SetActions(TAG*tag, ActionTAG*actions);
474 void swf_DumpActions(ActionTAG*atag, char*prefix);
475 void swf_ActionEnumerateURLs(ActionTAG*atag, char*(*callback)(char*));
476 void swf_ActionEnumerateTargets(ActionTAG*atag, char*(*callback)(char*));
477 void swf_ActionEnumerateStrings(ActionTAG*atag, char*(*callback)(char*));
478
479 ActionTAG* swf_ActionStart();
480 void action_End();
481 void action_NextFrame();
482 void action_PreviousFrame();
483 void action_Play();
484 void action_Stop();
485 void action_ToggleQuality();
486 void action_StopSounds();
487 void action_Add();
488 void action_Subtract();
489 void action_Multiply();
490 void action_Divide();
491 void action_Equals();
492 void action_Less();
493 void action_And();
494 void action_Or();
495 void action_Not();
496 void action_StringEquals();
497 void action_StringLength();
498 void action_StringExtract();
499 void action_Pop();
500 void action_ToInteger();
501 void action_GetVariable();
502 void action_SetVariable();
503 void action_SetTarget2();
504 void action_StringAdd();
505 void action_GetProperty();
506 void action_SetProperty();
507 void action_CloneSprite();
508 void action_RemoveSprite();
509 void action_Trace();
510 void action_StartDrag();
511 void action_EndDrag();
512 void action_StringLess();
513 void action_RandomNumber();
514 void action_MBStringLength();
515 void action_CharToAscii();
516 void action_AsciiToChar();
517 void action_GetTime();
518 void action_MBStringExtract();
519 void action_MBCharToAscii();
520 void action_MBAsciiToChar();
521 void action_Delete();
522 void action_Delete2();
523 void action_DefineLocal();
524 void action_CallFunction();
525 void action_Return();
526 void action_Modulo();
527 void action_NewObject();
528 void action_DefineLocal2();
529 void action_InitArray();
530 void action_Makehash();
531 void action_TypeOf();
532 void action_TargetPath();
533 void action_Enumerate();
534 void action_Add2();
535 void action_Less2();
536 void action_Equals2();
537 void action_ToNumber();
538 void action_ToString();
539 void action_PushDuplicate();
540 void action_StackSwap();
541 void action_GetMember();
542 void action_SetMember();
543 void action_Increment();
544 void action_Decrement();
545 void action_CallMethod();
546 void action_NewMethod();
547 void action_BitAnd();
548 void action_BitOr();
549 void action_BitXor();
550 void action_BitLShift();
551 void action_BitRShift();
552 void action_BitURShift();
553 void action_GotoFrame(U16 frame);
554 void action_GetUrl(char* url, char* label);
555 void action_StoreRegister(U8 reg);
556 void action_Constantpool(char* constantpool);
557 void action_WaitForFrame(U16 frame, U8 skip);
558 void action_SetTarget(char* target);
559 void action_GotoLabel(char* label);
560 void action_WaitForFrame2(U8 skip);
561 void action_With(char*object);
562 void action_PushString(char*str);
563 void action_PushFloat(float f);
564 void action_PushNULL();
565 void action_PushRegister(U8 reg);
566 void action_PushBoolean(char c);
567 void action_PushDouble(double d);
568 void action_PushInt(int i);
569 void action_PushLookup(U8 index);
570 void action_Jump(U16 branch);
571 void action_GetUrl2(U8 method);
572 void action_DefineFunction(U8*data, int len);
573 void action_If(U16 branch);
574 void action_Call();
575 void action_GotoFrame2(U8 method);
576 void swf_ActionEnd();
577
578 #endif