fixed compile problems.
[swftools.git] / src / swfc.c
index e248874..1be48cc 100644 (file)
@@ -28,6 +28,7 @@
 #include <math.h>
 #include "../config.h"
 #include "../lib/rfxswf.h"
+#include "../lib/drawer.h"
 #include "../lib/log.h"
 #include "../lib/args.h"
 #include "../lib/q.h"
@@ -41,12 +42,12 @@ static char * outputname = "output.swf";
 static int verbose = 2;
 static int override_outputname = 0;
 
-static struct options_t options[] =
-{
- {"o","output"},
- {"v","verbose"},
- {"V","version"},
- {0,0}
+static struct options_t options[] = {
+{"h", "help"},
+{"V", "version"},
+{"v", "verbose"},
+{"o", "output"},
+{0,0}
 };
     
 int args_callback_option(char*name,char*val)
@@ -74,12 +75,16 @@ int args_callback_longoption(char*name,char*val)
 {
     return args_long2shortoption(options, name, val);
 }
-void args_callback_usage(char*name)
+void args_callback_usage(char *name)
 {
-    printf("Usage: %s [-o filename] file.wav\n", name);
-    printf("\t-v , --verbose\t\t\t Be more verbose\n");
-    printf("\t-o , --output filename\t\t set output filename (default: output.swf)\n");
-    printf("\t-V , --version\t\t\t Print program version and exit\n");
+    printf("\n");
+    printf("Usage: %s [-o file.swf] file.sc\n", name);
+    printf("\n");
+    printf("-h , --help                    Print short help message and exit\n");
+    printf("-V , --version                 Print version info and exit\n");
+    printf("-v , --verbose                 Increase verbosity. \n");
+    printf("-o , --output <filename>       Set output file to <filename>.\n");
+    printf("\n");
 }
 int args_callback_command(char*name,char*val)
 {
@@ -120,7 +125,7 @@ static void warning(char*format, ...)
     va_end(arglist);
     printf("\"%s\", line %d column %d: warning- %s\n", filename, line, column, buf);
 }
-   
+
 static void readToken()
 {
     type = file[pos].type;
@@ -720,12 +725,13 @@ void s_textshape(char*name, char*fontname, char*_text)
     outline->bbox = font->layout->bounds[g];
     
     {
-       SWFSHAPEDRAWER draw;
-       swf_DrawerInit(&draw,0);
+       drawer_t draw;
+       swf_Shape11DrawerInit(&draw, 0);
        swf_DrawText(&draw, font, _text);
-       swf_DrawerFinish(&draw);
-       outline->shape = swf_DrawerToShape(&draw);
-       outline->bbox = draw.bbox;
+       draw.finish(&draw);
+       outline->shape = swf_ShapeDrawerToShape(&draw);
+       outline->bbox = swf_ShapeDrawerGetBBox(&draw);
+       draw.dealloc(&draw);
     }
 
     if(dictionary_lookup(&outlines, name))
@@ -770,22 +776,30 @@ void s_image(char*name, char*type, char*filename, int quality)
        s_box(name, 0, 0, black, 20, 0);
        return;
     }
-    tag = swf_InsertTag(tag, ST_DEFINEBITSJPEG2);
-    swf_SetU16(tag, imageID);
+    if(type=="jpeg") {
+#ifndef HAVE_LIBJPEG
+       warning("no jpeg support compiled in");
+       s_box(name, 0, 0, black, 20, 0);
+       return;
+#else
+       tag = swf_InsertTag(tag, ST_DEFINEBITSJPEG2);
+       swf_SetU16(tag, imageID);
 
-    if(swf_SetJPEGBits(tag, filename, quality) < 0) {
-       syntaxerror("Image \"%s\" not found, or contains errors", filename);
-    }
+       if(swf_SetJPEGBits(tag, filename, quality) < 0) {
+           syntaxerror("Image \"%s\" not found, or contains errors", filename);
+       }
 
-    swf_GetJPEGSize(filename, &width, &height);
+       swf_GetJPEGSize(filename, &width, &height);
 
-    r.xmin = 0;
-    r.ymin = 0;
-    r.xmax = width*20;
-    r.ymax = height*20;
+       r.xmin = 0;
+       r.ymin = 0;
+       r.xmax = width*20;
+       r.ymax = height*20;
 
-    s_addimage(name, id, tag, r);
-    incrementid();
+       s_addimage(name, id, tag, r);
+       incrementid();
+#endif
+    }
 
     /* step 2: the character */
     tag = swf_InsertTag(tag, ST_DEFINESHAPE); // todo: should be defineshape2/3 once images can be transparent.(?)
@@ -809,26 +823,16 @@ void dumpSWF(SWF*swf)
     
 void s_font(char*name, char*filename)
 {
-    int f;
-    SWF swf;
     SWFFONT* font;
-    f = open(filename,O_RDONLY|O_BINARY);
-    if (f<0) { 
-       warning("Couldn't open file \"%s\": %s", filename, strerror(errno));
+    font = swf_LoadFont(filename);
+   
+    if(font == 0) {
+       warning("Couldn't open font file \"%s\"", filename);
        font = (SWFFONT*)malloc(sizeof(SWFFONT));
        memset(font, 0, sizeof(SWFFONT));
        dictionary_put2(&fonts, name, font);
        return;
     }
-    font = 0;
-    if (swf_ReadSWF(f,&swf)>=0) { 
-       swf_FontExtract(&swf, 0x4e46, &font);
-       swf_FreeTags(&swf);
-    }
-    close(f);
-    if (font==0) { 
-       syntaxerror("File \"%s\" isn't a valid rfxswf font file", filename);
-    }
 
     if(0)
     {
@@ -922,13 +926,15 @@ GRADIENT parseGradient(const char*str)
     memset(&gradient, 0, sizeof(GRADIENT));
     while(*p) {
        char*posstr,*colorstr;
+       float pos;
+       RGBA color;
        posstr = gradient_getToken(&p);
        if(!*posstr)
            break;
-       float pos = parsePercent(posstr);
+       pos = parsePercent(posstr);
        if(!*p) syntaxerror("Error in shape data: Color expected after %s", posstr);
        colorstr = gradient_getToken(&p);
-       RGBA color = parseColor(colorstr);
+       color = parseColor(colorstr);
        if(gradient.num == sizeof(gradient.ratios)/sizeof(gradient.ratios[0])) {
            warning("gradient record too big- max size is 8, rest ignored");
            break;
@@ -955,22 +961,39 @@ void s_gradient(char*name, const char*text, int radial)
     dictionary_put2(&gradients, name, gradient);
 }
 
+void s_action(const char*text)
+{
+    ActionTAG* a = 0;
+    a = swf_ActionCompile(text, stack[0].swf->fileVersion);
+    if(!a) {
+       syntaxerror("Couldn't compile ActionScript");
+    }
+
+    tag = swf_InsertTag(tag, ST_DOACTION);
+
+    swf_ActionSet(tag, a);
+
+    swf_ActionFree(a);
+}
+
 void s_outline(char*name, char*format, char*source)
 {
     outline_t* outline;
 
-    SWFSHAPEDRAWER draw;
+    drawer_t draw;
     SHAPE* shape;
     SHAPE2* shape2;
     SRECT bounds;
     
-    swf_DrawerInit(&draw, 0);
-    swf_DrawString(&draw, source);
-    swf_DrawerFinish(&draw);
-    shape = swf_DrawerToShape(&draw);
-    shape2 = swf_ShapeToShape2(shape);
-    bounds = swf_GetShapeBoundingBox(shape2);
+    swf_Shape11DrawerInit(&draw, 0);
+    draw_string(&draw, source);
+    draw.finish(&draw);
+    shape = swf_ShapeDrawerToShape(&draw);
+    //shape2 = swf_ShapeToShape2(shape);
+    //bounds = swf_GetShapeBoundingBox(shape2);
     //swf_Shape2Free(shape2);
+    bounds = swf_ShapeDrawerGetBBox(&draw);
+    draw.dealloc(&draw);
     
     outline = (outline_t*)malloc(sizeof(outline_t));
     memset(outline, 0, sizeof(outline_t));
@@ -1915,7 +1938,7 @@ static int c_primitive(map_t*args)
     int type=0;
     char* font;
     char* text;
-    char* outline;
+    char* outline=0;
     RGBA fill;
     if(!strcmp(command, "circle"))
        type = 1;
@@ -2034,7 +2057,16 @@ int fakechar(map_t*args)
 }
 
 static int c_egon(map_t*args) {return fakechar(args);}
-static int c_button(map_t*args) {return fakechar(args);}
+static int c_button(map_t*args) {
+    char*action = "";
+    readToken();
+    if(type == RAWDATA)
+       action = text;
+    else
+       pushBack();
+
+    return fakechar(args);
+}
 static int c_edittext(map_t*args) {return fakechar(args);}
 
 static int c_morphshape(map_t*args) {return fakechar(args);}
@@ -2043,7 +2075,18 @@ static int c_movie(map_t*args) {return fakechar(args);}
 static int c_buttonsounds(map_t*args) {return 0;}
 static int c_buttonput(map_t*args) {return 0;}
 static int c_texture(map_t*args) {return 0;}
-static int c_action(map_t*args) {return 0;}
+
+static int c_action(map_t*args) 
+{
+    readToken();
+    if(type != RAWDATA) {
+       syntaxerror("colon (:) expected");
+    }
+
+    s_action(text);
+   
+    return 0;
+}
 
 static struct {
     char*command;
@@ -2087,15 +2130,15 @@ static struct {
  {"stop", c_stop, "sound"},
 
     // object placement tags
- {"put", c_put,             "<i> x=0 y=0 red=+0 green=+0 blue=+0 alpha=+0 luminance= scale= scalex= scaley= pivot= pin= shear= rotate= above= below="},
- {"startclip", c_startclip, "<i> x=0 y=0 red=+0 green=+0 blue=+0 alpha=+0 luminance= scale= scalex= scaley= pivot= pin= shear= rotate= above= below="},
- {"change", c_change,   "name x= y= red= green= blue= alpha= luminance= scale= scalex= scaley= pivot= pin= shear= rotate= above= below="},
- {"arcchange", c_arcchange,   "name pivot= angle= red= green= blue= alpha= luminance= scale= scalex= scaley= pivot= pin= shear= rotate= above= below="},
- {"qchange", c_qchange, "name x= y= red= green= blue= alpha= luminance= scale= scalex= scaley= pivot= pin= shear= rotate= above= below="},
- {"jump", c_jump,       "name x= y= red= green= blue= alpha= luminance= scale= scalex= scaley= pivot= pin= shear= rotate= above= below="},
+ {"put", c_put,             "<i> x=0 y=0 red=+0 green=+0 blue=+0 alpha=+0 luminance= scale= scalex= scaley= pivot= pin= shear= rotate= ratio= above= below="},
+ {"startclip", c_startclip, "<i> x=0 y=0 red=+0 green=+0 blue=+0 alpha=+0 luminance= scale= scalex= scaley= pivot= pin= shear= rotate= ratio= above= below="},
+ {"change", c_change,   "name x= y= red= green= blue= alpha= luminance= scale= scalex= scaley= pivot= pin= shear= rotate= ratio= above= below="},
+ {"arcchange", c_arcchange,   "name pivot= angle= red= green= blue= alpha= luminance= scale= scalex= scaley= pivot= pin= shear= rotate= ratio= above= below="},
+ {"qchange", c_qchange, "name x= y= red= green= blue= alpha= luminance= scale= scalex= scaley= pivot= pin= shear= rotate= ratio= above= below="},
+ {"jump", c_jump,       "name x= y= red= green= blue= alpha= luminance= scale= scalex= scaley= pivot= pin= shear= rotate= ratio= above= below="},
  {"del", c_del, "name"},
     // virtual object placement
- {"buttonput", c_buttonput, "<i> x=0 y=0 red=+0 green=+0 blue=+0 alpha=+0 luminance= scale= scalex=100% scaley=100% shear=0 rotate=0 above= below="},
+ {"buttonput", c_buttonput, "<i> x=0 y=0 red=+0 green=+0 blue=+0 alpha=+0 luminance= scale= scalex=100% scaley=100% shear=0 rotate=0 above= below="}, //TODO: ratio???
  {"texture", c_texture, "<i> x=0 y=0 scale= scalex=100% scaley=100% shear=0 rotate=0"},
 
     // commands which start a block
@@ -2277,6 +2320,8 @@ static void parseArgumentsForCommand(char*command)
     int t;
     map_t args;
     int nr = -1;
+    msg("<verbose> parse Command: %s (line %d)", command, line);
+
     for(t=0;t<sizeof(arguments)/sizeof(arguments[0]);t++) {
        if(!strcmp(arguments[t].command, command)) {
 
@@ -2295,6 +2340,12 @@ static void parseArgumentsForCommand(char*command)
     }
     if(nr<0)
        syntaxerror("command %s not known", command);
+   
+    // catch missing .flash directives at the beginning of a file
+    if(strcmp(command, "flash") && !stackpos)
+    {
+       syntaxerror("No movie defined- use .flash first");
+    }
 
 #ifdef DEBUG
     printf(".%s\n", command);fflush(stdout);
@@ -2303,7 +2354,7 @@ static void parseArgumentsForCommand(char*command)
 
     (*arguments[nr].func)(&args);
 
-    if(!strcmp(command, "button") ||
+    /*if(!strcmp(command, "button") ||
        !strcmp(command, "action")) {
        while(1) {
            readToken();
@@ -2316,7 +2367,7 @@ static void parseArgumentsForCommand(char*command)
                }
            }
        }
-    }
+    }*/
 
     map_clear(&args);
     return;
@@ -2332,13 +2383,13 @@ int main (int argc,char ** argv)
        args_callback_usage(argv[0]);
        exit(1);
     }
+    
     file = generateTokens(filename);
     if(!file) {
        printf("parser returned error.\n");
        return 1;
     }
     pos=0;
-
     t=0;
 
     while(!noMoreTokens()) {