renamed png functions
[swftools.git] / src / swfc.c
index 19a53c7..e34f9fc 100644 (file)
@@ -48,6 +48,7 @@ static int override_outputname = 0;
 static int do_cgi = 0;
 static int change_sets_all = 0;
 static int do_exports = 0;
+static char * mainclass = "";
 
 static struct options_t options[] = {
 {"h", "help"},
@@ -177,11 +178,13 @@ static struct level
    /* for swf (0): */
    SWF*swf;
    char*filename;
+   char as3;
 
    /* for sprites (1): */
    TAG*tag;
    U16 id;
    char*name;
+   char*as3name;
    U16 olddepth;
    int oldframe;
    dict_t oldinstances;
@@ -340,22 +343,22 @@ static void free_outline(void* o)
 
 static void freeDictionaries()
 {
-    dict_free_all(&instances, free_instance);
-    dict_free_all(&characters, free);
-    dict_free_all(&images, free);
-    dict_free_all(&textures, free);
-    dict_free_all(&outlines, free_outline);
-    dict_free_all(&gradients, free_gradient);
-    dict_free_all(&filters, free);
-    dict_free_all(&fonts, free_font);
-    dict_free_all(&sounds, free);
-    dict_free_all(&interpolations, free);
+    dict_free_all(&instances, 1, free_instance);
+    dict_free_all(&characters, 1, free);
+    dict_free_all(&images, 1, free);
+    dict_free_all(&textures, 1, free);
+    dict_free_all(&outlines, 1, free_outline);
+    dict_free_all(&gradients, 1, free_gradient);
+    dict_free_all(&filters, 1, free);
+    dict_free_all(&fonts, 1, free_font);
+    dict_free_all(&sounds, 1, free);
+    dict_free_all(&interpolations, 1, free);
     cleanUp = 0;
 }
 
 static void freeFontDictionary()
 {
-    dict_free_all(&fonts, free_font);
+    dict_free_all(&fonts, 1, free_font);
 }
 
 static void incrementid()
@@ -638,7 +641,7 @@ void s_swf(const char*name, SRECT r, int version, int fps, int compress, RGBA ba
     incrementid();
 }
 
-void s_sprite(const char*name, SRECT*scalegrid)
+void s_sprite(const char*name, SRECT*scalegrid, const char*as3name)
 {
     tag = swf_InsertTag(tag, ST_DEFINESPRITE);
     swf_SetU16(tag, id); //id
@@ -653,6 +656,7 @@ void s_sprite(const char*name, SRECT*scalegrid)
     stack[stackpos].tag = tag;
     stack[stackpos].id = id;
     stack[stackpos].name = strdup(name);
+    stack[stackpos].as3name = strdup(as3name);
     if(scalegrid) {
        stack[stackpos].scalegrid = *scalegrid;
     } else {
@@ -686,7 +690,7 @@ typedef struct _button
 
 static button_t mybutton;
 
-void s_button(const char*name)
+void s_button(const char*name, const char*as3name)
 {
     tag = swf_InsertTag(tag, ST_DEFINEBUTTON2);
     swf_SetU16(tag, id); //id
@@ -699,6 +703,7 @@ void s_button(const char*name)
     stack[stackpos].tag = tag;
     stack[stackpos].id = id;
     stack[stackpos].name = strdup(name);
+    stack[stackpos].as3name = strdup(as3name);
     stack[stackpos].oldrect = currentrect;
     memset(&currentrect, 0, sizeof(currentrect));
 
@@ -815,6 +820,14 @@ static void s_endButton()
     currentrect = stack[stackpos].oldrect;
 
     s_addcharacter(stack[stackpos].name, stack[stackpos].id, stack[stackpos].tag, r);
+
+    if(*stack[stackpos].as3name) {
+        tag = swf_InsertTag(tag, ST_SYMBOLCLASS);
+        swf_SetU16(tag, 1);
+        swf_SetU16(tag, stack[stackpos].id);
+        swf_SetString(tag, stack[stackpos].as3name);
+    }
+
     free(stack[stackpos].name);
 }
 
@@ -864,7 +877,7 @@ static void free_filterlist(FILTERLIST* f_list)
     int i;
     for (i = 0; i < f_list->num; i++)
     {
-        if (f_list->filter[i]->type == FILTERTYPE_GRADIENTGLOW)
+        if(f_list->filter[i]->type == FILTERTYPE_GRADIENTGLOW)
             gradient_free(((FILTER_GRADIENTGLOW*)f_list->filter[i])->gradient);
         free(f_list->filter[i]);
     }
@@ -911,7 +924,7 @@ void setPlacement(TAG*tag, U16 id, U16 depth, MATRIX m, const char*name, paramet
     if(p->blendmode) {
     po.blendmode = p->blendmode;
     }
-    if (p->filters)
+    if(p->filters)
        po.filters = p->filters;
     swf_SetPlaceObject(tag, &po);
 }
@@ -929,7 +942,7 @@ static void writeInstance(void* _i)
         frame++;
         while (tag && tag->id != ST_SHOWFRAME)
             tag = tag->next;
-        if (parametersChange(i->history, frame))
+        if(parametersChange(i->history, frame))
         {
             readParameters(i->history, &p, frame);
             m = s_instancepos(i->character->size, &p);
@@ -939,7 +952,7 @@ static void writeInstance(void* _i)
             else
                tag = swf_InsertTag(tag, ST_PLACEOBJECT2);
             setPlacement(tag, 0, i->depth, m, 0, &p, 1);
-            if (p.filters)
+            if(p.filters)
                free_filterlist(p.filters);
         } else if(tag) {
             tag = tag->next;
@@ -993,7 +1006,7 @@ static void s_endSprite()
         syntaxerror("internal error(7)");
     /* TODO: before clearing, prepend "<spritename>." to names and
              copy into old instances dict */
-    dict_free_all(&instances, free_instance);
+    dict_free_all(&instances, 1, free_instance);
 
     currentframe = stack[stackpos].oldframe;
     currentrect = stack[stackpos].oldrect;
@@ -1001,6 +1014,15 @@ static void s_endSprite()
     instances = stack[stackpos].oldinstances;
 
     s_addcharacter(stack[stackpos].name, stack[stackpos].id, stack[stackpos].tag, r);
+
+    if(*stack[stackpos].as3name) {
+        tag = swf_InsertTag(tag, ST_SYMBOLCLASS);
+        swf_SetU16(tag, 1);
+        swf_SetU16(tag, stack[stackpos].id);
+        swf_SetString(tag, stack[stackpos].as3name);
+    }   
+
+
     free(stack[stackpos].name);
 }
 
@@ -1009,6 +1031,7 @@ static void s_endSWF()
     int fi;
     SWF* swf;
     char*filename;
+    char*mc="";
 
     dict_foreach_value(&instances, writeInstance);
 
@@ -1029,6 +1052,26 @@ static void s_endSWF()
     //    tag = swf_InsertTag(tag, ST_SHOWFRAME);
     tag = swf_InsertTag(tag, ST_SHOWFRAME);
 
+    if(stack[0].as3) {
+        TAG*tag = swf->firstTag;
+        tag = swf_InsertTag(tag, ST_DOABC);
+        void*code = as3_getcode();
+        swf_WriteABC(tag, code);
+        if(*mainclass)
+            mc = mainclass;
+        else if(as3_getglobalclass())
+            mc = as3_getglobalclass();
+        if(*mc) {
+            tag = swf_InsertTag(tag, ST_SYMBOLCLASS);
+            swf_SetU16(tag, 1);
+            swf_SetU16(tag, 0);
+            swf_SetString(tag, mc);
+        } else {
+            warning("no global public MovieClip subclass");
+        }
+        as3_destroy();
+    }
+
     tag = swf_InsertTag(tag, ST_END);
 
     swf_OptimizeTagOrder(swf);
@@ -1131,7 +1174,7 @@ int addFillStyle(SHAPE*s, SRECT*r, const char*name)
     if(name[0] == '#') {
        parseColor2(name, &color);
        return swf_ShapeAddSolidFillStyle(s, &color);
-    } else if ((texture = dict_lookup(&textures, name))) {
+    } else if((texture = dict_lookup(&textures, name))) {
        return swf_ShapeAddFillStyle2(s, &texture->fs);
     } else if((image = dict_lookup(&images, name))) {
        MATRIX m;
@@ -1141,7 +1184,7 @@ int addFillStyle(SHAPE*s, SRECT*r, const char*name)
        m.tx = r->xmin;
        m.ty = r->ymin;
        return swf_ShapeAddBitmapFillStyle(s, &m, image->id, 0);
-    }  else if ((gradient = dict_lookup(&gradients, name))) {
+    }  else if((gradient = dict_lookup(&gradients, name))) {
        SRECT r2;
        MATRIX rot,m;
        double ccos,csin;
@@ -1161,7 +1204,7 @@ int addFillStyle(SHAPE*s, SRECT*r, const char*name)
        m.tx = r->xmin + (r->xmax - r->xmin)/2;
        m.ty = r->ymin + (r->ymax - r->ymin)/2;
        return swf_ShapeAddGradientFillStyle(s, &m, &gradient->gradient, gradient->radial);
-    }  else if (parseColor2(name, &color)) {
+    }  else if(parseColor2(name, &color)) {
        return swf_ShapeAddSolidFillStyle(s, &color);
     } else {
        syntaxerror("not a color/fillstyle: %s", name);
@@ -1364,6 +1407,24 @@ void s_quicktime(const char*name, const char*url)
     incrementid();
 }
 
+void s_video(const char *name, int width, int height)
+{
+    SRECT r;
+
+    memset(&r, 0, sizeof(r));
+
+    tag = swf_InsertTag(tag, ST_DEFINEVIDEOSTREAM);
+    swf_SetU16(tag, id);
+    swf_SetU16(tag, 0); // numframes
+    swf_SetU16(tag, width);
+    swf_SetU16(tag, height);
+    swf_SetU8(tag, 0); // videoflags
+    swf_SetU8(tag, 0); // codecid
+
+    s_addcharacter(name, id, tag, r);
+    incrementid();
+}
+
 void s_edittext(const char*name, const char*fontname, int size, int width, int height, const char*text, RGBA*color, int maxlength, const char*variable, int flags, int align)
 {
     SWFFONT*font = 0;
@@ -1404,7 +1465,7 @@ void s_image(const char*name, const char*type, const char*filename, int quality)
     /* step 1: the bitmap */
     SRECT r;
     int imageID = id;
-    int width, height;
+    unsigned width, height;
     if(!strcmp(type,"jpeg")) {
 #ifndef HAVE_JPEGLIB
        warning("no jpeg support compiled in");
@@ -1432,7 +1493,7 @@ void s_image(const char*name, const char*type, const char*filename, int quality)
        RGBA*data = 0;
        swf_SetU16(tag, imageID);
 
-       getPNG(filename, &width, &height, (unsigned char**)&data);
+       png_load(filename, &width, &height, (unsigned char**)&data);
 
        if(!data) {
            syntaxerror("Image \"%s\" not found, or contains errors", filename);
@@ -1528,24 +1589,49 @@ void s_texture(const char*name, const char*object, int x, int y, float scalex, f
     dict_put(&textures, name, texture);
 }
 
+void s_createfont(const char*name, const char*filename, const char*glyphs, char flashtype)
+{
+    if(dict_lookup(&fonts, name))
+       syntaxerror("font %s defined twice", name);
+
+    SWFFONT* font = swf_LoadFont(filename, flashtype);
+    if(font == 0) {
+       warning("Couldn't open font file \"%s\"", filename);
+       font = (SWFFONT*)malloc(sizeof(SWFFONT));
+       memset(font, 0, sizeof(SWFFONT));
+       dict_put(&fonts, name, font);
+       return;
+    }
+    swf_FontPrepareForEditText(font);
+
+    if(!strcmp(glyphs, "all")) {
+       swf_FontUseAll(font);
+       font->use->glyphs_specified = 1;
+    } else {
+       if(!glyphs[0]) {
+           swf_FontInitUsage(font);
+       } else {
+           swf_FontUseUTF8(font, (const U8*)glyphs, 0xffff);
+           font->use->glyphs_specified = 1;
+       }
+    }
+    dict_put(&fonts, name, font);
+}
+
 void s_font(const char*name, const char*filename)
 {
     SWFFONT* font;
     font = dict_lookup(&fonts, name);
-    if(0)
-    {
-       /* fix the layout. Only needed for old fonts */
-       int t;
-       for(t=0;t<font->numchars;t++) {
-           font->glyph[t].advance = 0;
-       }
-       font->layout = 0;
-       swf_FontCreateLayout(font);
-    }
     font->id = id;
     swf_FontReduce_swfc(font);
-    tag = swf_InsertTag(tag, ST_DEFINEFONT2);
+       
+    if(font->version>=3 && stack[0].swf->fileVersion < 8) {
+       warning("flashtype not supported for flash versions 8 and below");
+    }
+
+    tag = swf_InsertTag(tag, font->version==3?ST_DEFINEFONT3:ST_DEFINEFONT2);
     swf_FontSetDefine2(tag, font);
+
     if(do_exports) {
        tag = swf_InsertTag(tag, ST_EXPORTASSETS);
        swf_SetU16(tag, 1);
@@ -1651,7 +1737,7 @@ void s_sound(const char*name, const char*filename)
 
     incrementid();
 
-    if (samples)
+    if(samples)
         free(samples);
 }
 
@@ -1724,7 +1810,7 @@ GRADIENT parseGradient(const char*str)
 
 FILTERLIST* parseFilters(char* list)
 {
-    if (!strcmp(list, "no_filters"))
+    if(!strcmp(list, "no_filters"))
        return 0;
     FILTER* f;
     FILTERLIST* f_list = (FILTERLIST*)malloc(sizeof(FILTERLIST));
@@ -1734,22 +1820,22 @@ FILTERLIST* parseFilters(char* list)
     while (f_start)
     {
        f_end = strchr(f_start, ',');
-       if (f_end)
+       if(f_end)
            *f_end = '\0';
        f = dict_lookup(&filters, f_start);
-       if (!f)
+       if(!f)
        {
            free(f_list);
            syntaxerror("unknown filter %s", f_start);
        }
-       if (f_list->num == 8)
+       if(f_list->num == 8)
        {
            warning("too many filters in filterlist, no more than 8 please, rest ignored");
            break;
        }
        f_list->filter[f_list->num] = f;
        f_list->num++;
-       if (f_end)
+       if(f_end)
        {
            *f_end = ',';
            f_start = f_end + 1;
@@ -1864,19 +1950,20 @@ void s_blur(const char*name, double blurx, double blury, int passes)
 
 void s_action(const char*text)
 {
-    ActionTAG* a = 0;
-    a = swf_ActionCompile(text, stack[0].swf->fileVersion);
-    if(!a)
-    {
+    if(stack[0].swf->fileVersion < 9) {
+        ActionTAG* a = 0;
+        a = swf_ActionCompile(text, stack[0].swf->fileVersion);
+        if(!a) {
+            swf_ActionFree(a);
+            syntaxerror("Couldn't compile ActionScript");
+        }
+        tag = swf_InsertTag(tag, ST_DOACTION);
+        swf_ActionSet(tag, a);
         swf_ActionFree(a);
-        syntaxerror("Couldn't compile ActionScript");
+    } else {
+        as3_parse_bytearray(stack[0].filename, text, strlen(text));
+        stack[0].as3 = 1;
     }
-
-    tag = swf_InsertTag(tag, ST_DOACTION);
-
-    swf_ActionSet(tag, a);
-
-    swf_ActionFree(a);
 }
 
 void s_initaction(const char*character, const char*text)
@@ -1981,12 +2068,12 @@ void s_includeswf(const char*name, const char*filename)
     int level = 0;
     U16 cutout[] = {ST_SETBACKGROUNDCOLOR, ST_PROTECT, ST_FREEALL, ST_REFLEX};
     f = open(filename,O_RDONLY|O_BINARY);
-    if (f<0) {
+    if(f<0) {
        warning("Couldn't open file \"%s\": %s", filename, strerror(errno));
        s_box(name, 0, 0, black, 20, 0);
        return;
     }
-    if (swf_ReadSWF(f,&swf)<0) {
+    if(swf_ReadSWF(f,&swf)<0) {
        warning("Only SWF files supported in .shape for now. File \"%s\" wasn't SWF.", filename);
        s_box(name, 0, 0, black, 20, 0);
        return;
@@ -2063,7 +2150,7 @@ void s_getParameters(const char*name, parameters_t* p)
     instance_t * i = dict_lookup(&instances, name);
     if(!i)
        syntaxerror("instance '%s' unknown(10)", name);
-    if (change_sets_all)
+    if(change_sets_all)
         readParameters(i->history, p, currentframe);
     else
        *p = i->parameters;
@@ -2162,51 +2249,51 @@ void s_put(const char*instance, const char*character, parameters_t p)
 
 void recordChanges(history_t* history, parameters_t p, int changeFunction, interpolation_t* inter)
 {
-    if (p.set & SF_X)
+    if(p.set & SF_X)
         history_remember(history, "x", currentframe, changeFunction, p.x, inter);
-    if (p.set & SF_Y)
+    if(p.set & SF_Y)
         history_remember(history, "y", currentframe, changeFunction, p.y, inter);
-    if (p.set & SF_SCALEX)
+    if(p.set & SF_SCALEX)
         history_remember(history, "scalex", currentframe, changeFunction, p.scalex, inter);
-    if (p.set & SF_SCALEY)
+    if(p.set & SF_SCALEY)
         history_remember(history, "scaley", currentframe, changeFunction, p.scaley, inter);
-    if (p.set & SF_CX_R)
+    if(p.set & SF_CX_R)
     {
         history_remember(history, "cxform.r0", currentframe, changeFunction, p.cxform.r0, inter);
         history_remember(history, "cxform.r1", currentframe, changeFunction, p.cxform.r1, inter);
     }
-    if (p.set & SF_CX_G)
+    if(p.set & SF_CX_G)
     {
         history_remember(history, "cxform.g0", currentframe, changeFunction, p.cxform.g0, inter);
         history_remember(history, "cxform.g1", currentframe, changeFunction, p.cxform.g1, inter);
     }
-    if (p.set & SF_CX_B)
+    if(p.set & SF_CX_B)
     {
         history_remember(history, "cxform.b0", currentframe, changeFunction, p.cxform.b0, inter);
         history_remember(history, "cxform.b1", currentframe, changeFunction, p.cxform.b1, inter);
     }
-    if (p.set & SF_CX_A)
+    if(p.set & SF_CX_A)
     {
         history_remember(history, "cxform.a0", currentframe, changeFunction, p.cxform.a0, inter);
         history_remember(history, "cxform.a1", currentframe, changeFunction, p.cxform.a1, inter);
     }
-    if (p.set & SF_ROTATE)
+    if(p.set & SF_ROTATE)
         history_remember(history, "rotate", currentframe, changeFunction, p.rotate, inter);
-    if (p.set & SF_SHEAR)
+    if(p.set & SF_SHEAR)
         history_remember(history, "shear", currentframe, changeFunction, p.shear, inter);
-    if (p.set & SF_PIVOT)
+    if(p.set & SF_PIVOT)
     {
         history_remember(history, "pivot.x", currentframe, changeFunction, p.pivot.x, inter);
         history_remember(history, "pivot.y", currentframe, changeFunction, p.pivot.y, inter);
     }
-    if (p.set & SF_PIN)
+    if(p.set & SF_PIN)
     {
         history_remember(history, "pin.x", currentframe, changeFunction, p.pin.x, inter);
         history_remember(history, "pin.y", currentframe, changeFunction, p.pin.y, inter);
     }
-    if (p.set & SF_BLEND)
+    if(p.set & SF_BLEND)
         history_remember(history, "blendmode", currentframe, changeFunction, p.blendmode, inter);
-    if (p.set & SF_FILTER)
+    if(p.set & SF_FILTER)
         history_rememberFilter(history, currentframe, changeFunction, p.filters, inter);
 }
 
@@ -2237,7 +2324,7 @@ void s_sweep(const char* instance, parameters_t p, float radius, int clockwise,
 void s_toggle(const char* instance, U16 flagsOn, U16 flagsOff)
 {
     instance_t* i = dict_lookup(&instances, instance);
-    if (!i)
+    if(!i)
         syntaxerror("instance %s not known", instance);
     U16 flags = (U16)history_value(i->history, currentframe, "flags");
     flags |= flagsOn;
@@ -2532,9 +2619,9 @@ int parseTwip(const char*str)
 
 int parseArc(const char* str)
 {
-    if (!strcmp(str, "short"))
+    if(!strcmp(str, "short"))
        return 1;
-    if (!strcmp(str, "long"))
+    if(!strcmp(str, "long"))
        return 0;
     syntaxerror("invalid value for the arc parameter: %s", str);
     return 1;
@@ -2542,9 +2629,9 @@ int parseArc(const char* str)
 
 int parseDir(const char* str)
 {
-    if (!strcmp(str, "clockwise"))
+    if(!strcmp(str, "clockwise"))
        return 1;
-    if (!strcmp(str, "counterclockwise"))
+    if(!strcmp(str, "counterclockwise"))
        return 0;
     syntaxerror("invalid value for the dir parameter: %s", str);
     return 1;
@@ -2604,7 +2691,7 @@ int parseColor2(const char*str, RGBA*color)
        return 1;
     }
     int len=strlen(str);
-    U8 alpha = 255;
+    int alpha = 255;
     if(strchr(str, '/')) {
        len = strchr(str, '/')-str;
        sscanf(str+len+1,"%02x", &alpha);
@@ -2696,7 +2783,7 @@ float parsePercent(const char*str)
     if(!l)
        return 1.0;
     if(str[l-1]=='%') {
-       return atoi(str)/100.0;
+       return atof(str)/100.0;
     }
     syntaxerror("Expression '%s' is not a percentage", str);
     return 0;
@@ -2770,6 +2857,7 @@ static int c_flash(map_t*args)
            syntaxerror("value \"%s\" not supported for the change-sets-all argument", change_modestr);
 
     do_exports=atoi(exportstr);
+    mainclass=strdup(lu(args, "mainclass"));
 
     s_swf(filename, bbox, version, fps, compress, color);
     return 0;
@@ -2806,19 +2894,19 @@ static int c_interpolation(map_t *args)
 {
     int i;
     const char* name = lu(args, "name");
-    if (dict_lookup(&interpolations, name))
+    if(dict_lookup(&interpolations, name))
         syntaxerror("interpolation %s defined twice", name);
 
     interpolation_t* inter = (interpolation_t*)malloc(sizeof(interpolation_t));
     const char* functionstr = lu(args, "function");
     inter->function = 0;
     for (i = 0; i < sizeof(interpolationFunctions) / sizeof(interpolationFunctions[0]); i++)
-        if (!strcmp(functionstr,interpolationFunctions[i]))
+        if(!strcmp(functionstr,interpolationFunctions[i]))
         {
             inter->function = i + 1;
             break;
         }
-    if (!inter->function)
+    if(!inter->function)
         syntaxerror("unkown interpolation function %s", functionstr);
     inter->speed = parseFloat(lu(args, "speed"));
     inter->amplitude = parseTwip(lu(args, "amplitude"));
@@ -2840,49 +2928,49 @@ SPOINT getPoint(SRECT r, const char*name)
         p.y = (r.ymin + r.ymax)/2;
         return p;
     }
-    if (!strcmp(name, "bottom-center")) {
+    if(!strcmp(name, "bottom-center")) {
         SPOINT p;
         p.x = (r.xmin + r.xmax)/2;
         p.y = r.ymax;
         return p;
     }
-    if (!strcmp(name, "top-center")) {
+    if(!strcmp(name, "top-center")) {
         SPOINT p;
         p.x = (r.xmin + r.xmax)/2;
         p.y = r.ymin;
         return p;
     }
-    if (!strcmp(name, "top-left")) {
+    if(!strcmp(name, "top-left")) {
         SPOINT p;
         p.x = r.xmin;
         p.y = r.ymin;
         return p;
     }
-    if (!strcmp(name, "top-right")) {
+    if(!strcmp(name, "top-right")) {
         SPOINT p;
         p.x = r.xmax;
         p.y = r.ymin;
         return p;
     }
-    if (!strcmp(name, "bottom-right")) {
+    if(!strcmp(name, "bottom-right")) {
         SPOINT p;
         p.x = r.xmax;
         p.y = r.ymax;
         return p;
     }
-    if (!strcmp(name, "bottom-left")) {
+    if(!strcmp(name, "bottom-left")) {
         SPOINT p;
         p.x = r.xmin;
         p.y = r.ymax;
         return p;
     }
-    if (!strcmp(name, "left-center")) {
+    if(!strcmp(name, "left-center")) {
         SPOINT p;
         p.x = r.xmin;
         p.y = (r.ymin + r.ymax)/2;
         return p;
     }
-    if (!strcmp(name, "right-center")) {
+    if(!strcmp(name, "right-center")) {
         SPOINT p;
         p.x = r.xmax;
         p.y = (r.ymin + r.ymax)/2;
@@ -2995,7 +3083,7 @@ static int c_gradient(map_t*args)
 static const char* checkFiltername(map_t* args)
 {
     const char* name = lu(args, "name");
-    if (strchr(name, ','))
+    if(strchr(name, ','))
        syntaxerror("the comma (,) is used to separate filters in filterlists. Please do not use in filternames.");
     return name;
 }
@@ -3239,7 +3327,7 @@ static int c_movement(map_t*args, int type)
         set = set | SF_Y;
     }
 
-    if (change_sets_all)
+    if(change_sets_all)
        set = SF_ALL;
     p.set = set;
 
@@ -3249,7 +3337,7 @@ static int c_movement(map_t*args, int type)
             {
                 const char* interstr = lu(args, "interpolation");
                 interpolation_t* inter = (interpolation_t*)dict_lookup(&interpolations, interstr);
-                if (!inter)
+                if(!inter)
                     syntaxerror("unkown interpolation %s", interstr);
                 s_change(instance, p, inter);
             }
@@ -3258,7 +3346,7 @@ static int c_movement(map_t*args, int type)
             {
                 const char* interstr = lu(args, "interpolation");
                 interpolation_t* inter = (interpolation_t*)dict_lookup(&interpolations, interstr);
-                if (!inter)
+                if(!inter)
                     syntaxerror("unkown interpolation %s", interstr);
                s_schange(instance, p, inter);
             }
@@ -3267,7 +3355,7 @@ static int c_movement(map_t*args, int type)
             {
                const char* rstr = lu(args, "r");
                int radius = parseTwip(rstr);
-               if (radius <= 0)
+               if(radius <= 0)
                        syntaxerror("sweep not possible: radius must be greater than 0.");
                const char* dirstr = lu(args, "dir");
                int clockwise = parseDir(dirstr);
@@ -3275,7 +3363,7 @@ static int c_movement(map_t*args, int type)
                int short_arc = parseArc(arcstr);
                 const char* interstr = lu(args, "interpolation");
                 interpolation_t* inter = (interpolation_t*)dict_lookup(&interpolations, interstr);
-                if (!inter)
+                if(!inter)
                     syntaxerror("unkown interpolation %s", interstr);
                s_sweep(instance, p, radius, clockwise, short_arc, inter);
             }
@@ -3346,7 +3434,7 @@ static int c_placement(map_t*args, int type)
        // put or startclip
        character = lu(args, "character");
        parameters_clear(&p);
-    } else if (type == PT_BUTTON) {
+    } else if(type == PT_BUTTON) {
        character = lu(args, "name");
        parameters_clear(&p);
        // button's show
@@ -3529,13 +3617,13 @@ static int c_placement(map_t*args, int type)
         set = set | SF_FILTER;
     }
 
-    if (type == PT_CHANGE && set & (SF_X | SF_Y))
+    if(type == PT_CHANGE && set & (SF_X | SF_Y))
        warning("As of version 0.8.2 using the .change command to modify an \
 object's position on the stage is considered deprecated. Future \
 versions may consider x and y parameters for the .change command \
 to be illegal; please use the .move command.");
 
-    if (change_sets_all)
+    if(change_sets_all)
        set = SF_ALL;
     p.set = set;
 
@@ -3548,7 +3636,7 @@ to be illegal; please use the .move command.");
             {
                 const char* interstr = lu(args, "interpolation");
                 interpolation_t* inter = (interpolation_t*)dict_lookup(&interpolations, interstr);
-                if (!inter)
+                if(!inter)
                     syntaxerror("unkown interpolation %s", interstr);
                 s_change(instance, p, inter);
             }
@@ -3557,7 +3645,7 @@ to be illegal; please use the .move command.");
             {
                 const char* interstr = lu(args, "interpolation");
                 interpolation_t* inter = (interpolation_t*)dict_lookup(&interpolations, interstr);
-                if (!inter)
+                if(!inter)
                     syntaxerror("unkown interpolation %s", interstr);
                s_schange(instance, p, inter);
             }
@@ -3585,7 +3673,7 @@ static int c_put(map_t*args)
 }
 static int c_change(map_t*args)
 {
-    if (currentframe == 0)
+    if(currentframe == 0)
         warning("change commands in frame 1 will be ignored, please use the put command to set object parameters");
     c_placement(args, PT_CHANGE);
     return 0;
@@ -3635,10 +3723,10 @@ static int c_toggle(map_t* args)
     const char*instance = lu(args, "name");
     U16 flagsOn = 0x0000, flagsOff = 0xffff;
     const char* alignstr = lu(args, "fixed_alignment");
-    if (!strcmp(alignstr, "on"))
+    if(!strcmp(alignstr, "on"))
        flagsOn += IF_FIXED_ALIGNMENT;
     else
-       if (!strcmp(alignstr, "off"))
+       if(!strcmp(alignstr, "off"))
            flagsOff -= IF_FIXED_ALIGNMENT;
        else
            syntaxerror("values for toggle must be \"on\" or \"off\". %s is not legal.", alignstr);
@@ -3660,12 +3748,13 @@ static int c_sprite(map_t*args)
 {
     const char* name = lu(args, "name");
     const char* scalinggrid = lu(args, "scalinggrid");
+    const char* as3name = lu(args, "as3name");
 
     if(scalinggrid && *scalinggrid) {
        SRECT r = parseBox(scalinggrid);
-       s_sprite(name, &r);
+       s_sprite(name, &r, as3name);
     } else {
-       s_sprite(name, 0);
+       s_sprite(name, 0, as3name);
     }
     return 0;
 }
@@ -3723,9 +3812,9 @@ static int c_primitive(map_t*args)
     if(type==0) {
        width = parseTwip(lu(args, "width"));
        height = parseTwip(lu(args, "height"));
-    } else if (type==1) {
+    } else if(type==1) {
        r = parseTwip(lu(args, "r"));
-    } else if (type==2) {
+    } else if(type==2) {
        outline = lu(args, "outline");
     }
 
@@ -3804,6 +3893,15 @@ static int c_quicktime(map_t*args)
     return 0;
 }
 
+static int c_video(map_t*args)
+{
+    const char*name = lu(args, "name");
+    int width = parseInt(lu(args, "width"));
+    int height = parseInt(lu(args, "height"));
+    s_video(name, width, height);
+    return 0;
+}
+
 static int c_image(map_t*args)
 {
     const char*command = lu(args, "commandname");
@@ -3841,7 +3939,8 @@ int fakechar(map_t*args)
 static int c_egon(map_t*args) {return fakechar(args);}
 static int c_button(map_t*args) {
     const char*name = lu(args, "name");
-    s_button(name);
+    const char*as3name = lu(args, "as3name");
+    s_button(name, as3name);
     return 0;
 }
 static int current_button_flags = 0;
@@ -4061,7 +4160,7 @@ static struct {
     command_func_t* func;
     char*arguments;
 } arguments[] =
-{{"flash", c_flash, "bbox=autocrop background=black version=6 fps=50 name= filename= @compress=default @change-sets-all=no @export=1"},
+{{"flash", c_flash, "bbox=autocrop background=black version=6 fps=50 name= filename= @compress=default @change-sets-all=no @export=1 @mainclass="},
  {"frame", c_frame, "n=<plus>1 name= @cut=no @anchor=no"},
  // "import" type stuff
  {"swf", c_swf, "name filename"},
@@ -4070,9 +4169,10 @@ static struct {
  {"png", c_image, "name filename"},
  {"movie", c_movie, "name filename"},
  {"sound", c_sound, "name filename"},
- {"font", c_font, "name filename glyphs="},
+ {"font", c_font, "name filename glyphs= @flashtype="},
  {"soundtrack", c_soundtrack, "filename"},
  {"quicktime", c_quicktime, "url"},
+ {"video", c_video, "name width= height="},
 
     // generators of primitives
 
@@ -4098,7 +4198,7 @@ static struct {
  {"text", c_text, "name text font size=100% color=white"},
  {"edittext", c_edittext, "name font= size=100% width height text="" color=white maxlength=0 variable="" @password=0 @wordwrap=0 @multiline=0 @html=0 @noselect=0 @readonly=0 @border=0 @autosize=0 align="},
  {"morphshape", c_morphshape, "name start end"},
- {"button", c_button, "name"},
+ {"button", c_button, "name as3name="},
     {"show", c_show,             "name x=0 y=0 red=+0 green=+0 blue=+0 alpha=+0 luminance= scale= scalex= scaley= blend= filter= pivot= pin= shear= rotate= ratio= above= below= as="},
     {"on_press", c_on_press, "position=inside"},
     {"on_release", c_on_release, "position=anywhere"},
@@ -4130,7 +4230,7 @@ static struct {
 
     // commands which start a block
 //startclip (see above)
- {"sprite", c_sprite, "name scalinggrid="},
+ {"sprite", c_sprite, "name scalinggrid= as3name="},
  {"action", c_action, "filename="},
  {"initaction", c_initaction, "name filename="},
 
@@ -4365,7 +4465,6 @@ static void analyseArgumentsForCommand(char*command)
 {
     int t;
     map_t args;
-    const char* fontfile;
     int nr = -1;
     U8* glyphs_to_include;
     msg("<verbose> analyse Command: %s (line %d)", command, line);
@@ -4384,57 +4483,26 @@ static void analyseArgumentsForCommand(char*command)
     map_dump(&args, stdout, "\t");fflush(stdout);
 #endif
     const char* name = lu(&args, "name");
-    if (!strcmp(command, "font"))
-    {
-       if(dict_lookup(&fonts, name))
-            syntaxerror("font %s defined twice", name);
-
-       SWFFONT* font;
-       fontfile = lu(&args, "filename");
-       font = swf_LoadFont(fontfile);
-       if(font == 0) {
-           warning("Couldn't open font file \"%s\"", fontfile);
-           font = (SWFFONT*)malloc(sizeof(SWFFONT));
-           memset(font, 0, sizeof(SWFFONT));
-       }
-       else
-       {
-           swf_FontPrepareForEditText(font);
-           glyphs_to_include = (U8*)lu(&args, "glyphs");
-           if (!strcmp(glyphs_to_include, "all"))
-           {
-               swf_FontUseAll(font);
-               font->use->glyphs_specified = 1;
-           }
-           else
-           {
-               if (strcmp (glyphs_to_include, ""))
-               {
-                   swf_FontUseUTF8(font, glyphs_to_include);
-                   font->use->glyphs_specified = 1;
-               }
-               else
-                   swf_FontInitUsage(font);
-           }
-       }
-       dict_put(&fonts, name, font);
-    }
-    else
+    if(!strcmp(command, "font"))
     {
+       const char* fontfile = lu(&args, "filename");
+       const char* glyphs = lu(&args, "glyphs");
+       const char* flashtype = lu(&args, "flashtype");
+       s_createfont(name, fontfile, glyphs, flashtype[0]);
+    } else {
         SWFFONT* font = dict_lookup(&fonts, lu(&args, "font"));
-        if (!font) {
+        if(!font) {
            //that's ok... it might be an edittext with a system font
             //syntaxerror("font %s is not known in line %d", lu(&args, "font"), line);
        } else
-            if (font->use && !font->use->glyphs_specified)
-            {
-               if (!strcmp(command, "edittext"))
+            if(font->use && !font->use->glyphs_specified) {
+               if(!strcmp(command, "edittext"))
                {
                    swf_FontUseAll(font);
                    font->use->glyphs_specified = 1;
                }
                else
-                   swf_FontUseUTF8(font, (U8*)lu(&args, "text"));
+                   swf_FontUseUTF8(font, (U8*)lu(&args, "text"), 0xffff);
             }
     }
     map_clear(&args);
@@ -4457,7 +4525,7 @@ void findFontUsage()
         readToken();
         if(type != COMMAND)
             syntaxerror("command expected");
-        if (strstr(fontRelated, text))
+        if(strstr(fontRelated, text))
             analyseArgumentsForCommand(text);
         else
             if(strcmp(text, "end"))