allow bounding box selection in swfstrings
authorMatthias Kramm <kramm@quiss.org>
Thu, 13 Aug 2009 21:04:14 +0000 (23:04 +0200)
committerMatthias Kramm <kramm@quiss.org>
Thu, 13 Aug 2009 21:04:14 +0000 (23:04 +0200)
src/jpeg2swf.1
src/jpeg2swf.doc
src/swfdump.c
src/swfstrings.1
src/swfstrings.c
src/swfstrings.doc [new file with mode: 0644]

index e9a854b..3da21bb 100644 (file)
@@ -1,4 +1,4 @@
-.TH jpeg2swf "1" "April 2009" "jpeg2swf" "swftools"
+.TH jpeg2swf "1" "August 2009" "jpeg2swf" "swftools"
 .SH NAME
 jpeg2swf - Converts jpeg images to SWF.
 
@@ -6,7 +6,7 @@ jpeg2swf - Converts jpeg images to SWF.
 .B jpeg2swf [-options [value]] imagefiles[.jpg]|[.jpeg] [...]
 
 .SH DESCRIPTION
-This tools converts jpeg image files into an SWF animation. It takes any
+This tool converts jpeg image files into an SWF animation. It takes any
 number of input pictures, and converts them to SWF one-by-one, where every
 converted picture is a seperate frame in the target SWF.
 
index ec04d0c..c386fcc 100644 (file)
@@ -2,7 +2,7 @@ Usage: %s [-options [value]] imagefiles[.jpg]|[.jpeg] [...]
 
 Converts jpeg images to SWF.
 
-This tools converts jpeg image files into an SWF animation. It takes any
+This tool converts jpeg image files into an SWF animation. It takes any
 number of input pictures, and converts them to SWF one-by-one, where every
 converted picture is a seperate frame in the target SWF.
 
index 0d63c2c..a082a5f 100644 (file)
@@ -375,9 +375,9 @@ void dumpFont(TAG*tag, char*prefix)
     swf_FontFree(font);
 }
 
-SWF swf;
-int fontnum = 0;
-SWFFONT**fonts;
+static SWF swf;
+static int fontnum = 0;
+static SWFFONT**fonts;
 
 void textcallback(void*self, int*glyphs, int*ypos, int nr, int fontid, int fontsize, int startx, int starty, RGBA*color) 
 {
index 0ab12a7..9794713 100644 (file)
@@ -1,24 +1,35 @@
-.TH swfstrings "1" "October 2001" "swfstrings" "swftools"
+.TH swfstrings "1" "August 2009" "swfstrings" "swftools"
 .SH NAME
-swfstrings - a tool for extracting text out of swf files.
+swfstrings - Extracts strings from SWF files.
+
 .SH Synopsis
-.B swfstrings
-[\fIoptions\fR] [\fIfile.swf\fR]
+.B swfstrings [options] file.swf
+
 .SH DESCRIPTION
-swfstrings extracts text out of DEFINETEXT tags of the .swf file. It also
-displays the fonts being used.
-.PP
-SWF files are animation files which can be displayed in Web Browsers using
-the Flash Plugin.
+This tool extracts strings from SWF files. It parses SWF font records
+and text records and prints unicode-encoded characters to stdout.
+
 .SH OPTIONS
 .TP
-\fB\-h\fR, \fB\-\-help\fR
-Print short help message and exit
-.\".TP
-.\" \fB\-\-version\fR
-.\" Print version info and exit
-
-.SH AUTHOR
+\fB\-f\fR, \fB\-\-fonts\fR 
+    Print out font information for each text block
+.TP
+\fB\-x\fR, \fB\-\-xpos\fR \fIx\fR
+    Set bounding box x coordinate
+.TP
+\fB\-y\fR, \fB\-\-ypos\fR \fIy\fR
+    Set bounding box y coordinate
+.TP
+\fB\-W\fR, \fB\-\-width\fR \fIwidth\fR
+    Set bounding box width
+.TP
+\fB\-H\fR, \fB\-\-height\fR \fIheight\fR
+    Set bounding box height
+.TP
+\fB\-V\fR, \fB\-\-version\fR 
+    Print version information and exit
+.SH AUTHORS
 
 Rainer Böhme <rfxswf@reflex-studio.de>
-
+.TP
+Matthias Kramm <kramm@quiss.org>
index 86d8aed..bfca4fa 100644 (file)
 #include <fcntl.h>
 #include "../lib/rfxswf.h"
 #include "../lib/args.h"
+#include "../lib/utf8.h"
 
-char * filename = 0;
+static char * filename = 0;
+static char showfonts = 0;
+static int x=0,y=0,w=0,h=0;
 
-struct options_t options[] =
-{
- {"v","verbose"},
- {"V","version"},
- {0,0}
+static struct options_t options[] = {
+{"f", "fonts"},
+{"x", "xpos"},
+{"y", "ypos"},
+{"W", "width"},
+{"H", "height"},
+{"V", "version"},
+{0,0}
 };
 
 int args_callback_option(char*name,char*val)
@@ -38,6 +44,27 @@ int args_callback_option(char*name,char*val)
     if(!strcmp(name, "V")) {
         printf("swfstrings - part of %s %s\n", PACKAGE, VERSION);
         exit(0);
+    } else if(!strcmp(name, "x")) {
+       x = atoi(val);
+       return 1;
+    } else if(!strcmp(name, "y")) {
+       y = atoi(val);
+       return 1;
+    } else if(!strcmp(name, "W")) {
+       w = atoi(val);
+       return 1;
+    } else if(!strcmp(name, "H")) {
+       h = atoi(val);
+       return 1;
+    } else if(!strcmp(name, "f")) {
+       showfonts = 1;
+       return 0;
+    } else if(!strcmp(name, "V")) {
+        printf("swfstrings - part of %s %s\n", PACKAGE, VERSION);
+        exit(0);
+    } else {
+       fprintf(stderr, "Unknown option: -%s\n", name);
+       exit(1);
     }
     return 0;
 }
@@ -45,10 +72,18 @@ int args_callback_longoption(char*name,char*val)
 {
     return args_long2shortoption(options, name, val);
 }
-void args_callback_usage(char*name)
-{    
-    printf("\nreflex SWF Text Scan Utility\n(w) 2000 by Rainer Boehme <rb@reflex-studio.de>\n\nUsage: %s filename.swf\n", name);
-    exit(0);
+void args_callback_usage(char *name)
+{
+    printf("\n");
+    printf("Usage: %s [options] file.swf\n", name);
+    printf("\n");
+    printf("-f , --fonts                   Print out font information for each text block\n");
+    printf("-x , --xpos <x>                Set bounding box x coordinate\n");
+    printf("-y , --ypos <y>                Set bounding box y coordinate\n");
+    printf("-W , --width <width>           Set bounding box width\n");
+    printf("-H , --height <height>         Set bounding box height\n");
+    printf("-V , --version                 Print version information and exit\n");
+    printf("\n");
 }
 int args_callback_command(char*name,char*val)
 {
@@ -60,49 +95,126 @@ int args_callback_command(char*name,char*val)
     return 0;
 }
 
-SWF swf;
-  
+static SWF swf;
+static int fontnum = 0;
+static SWFFONT**fonts = 0;
+
+void fontcallback1(void*self, U16 id,U8 * name)
+{ fontnum++;
+}
+
+void fontcallback2(void*self, U16 id,U8 * name)
+{ 
+  swf_FontExtract(&swf,id,&fonts[fontnum]);
+  fontnum++;
+}
+
+
+void textcallback(void*self, int*glyphs, int*advance, int nr, int fontid, int fontsize, int startx, int starty, RGBA*color) 
+{
+    SWFFONT*font = 0;
+    int t;
+    for(t=0;t<fontnum;t++)
+    {
+       if(fonts[t]->id == fontid) {
+           font = fonts[t];
+           break;
+       }
+    }
+
+    if(showfonts) {
+       if(font)
+           printf("#<font %d \"%s\"%s%s>\n", fontid, font->name, swf_FontIsBold(font)?" bold":"",swf_FontIsItalic(font)?" italic":"");
+       printf("#<color #%02x%02x%02x%02x>\n", color->r, color->g, color->b, color->a);
+       printf("#<size %d>\n", fontsize);
+    }
+
+    for(t=0;t<nr;t++)
+    {
+       int xx = startx + advance[t]/20;
+       if(x|y|w|h) {
+           /* TODO: this does not do any matrix handling yet */
+
+           if(xx < x || starty < y || xx > x+w || starty > y+h) {
+               /* outside of bounding box */
+               continue;
+           }
+       }
+
+       unsigned char a; 
+       int advance = 0;
+       if(font>=0) {
+           if(glyphs[t]<0 || glyphs[t] >= font->numchars  /*glyph is not in range*/
+                   || !font->glyph2ascii /* font has ascii<->glyph mapping */
+             ) a = glyphs[t];
+           else {
+               if(font->glyph2ascii[glyphs[t]])
+                   a = font->glyph2ascii[glyphs[t]];
+               else
+                   a = glyphs[t];
+           }
+       } else {
+           a = glyphs[t];
+       }
+
+       if(a>=32) {
+           char* utf8 = getUTF8(a);
+           printf("%s", utf8);
+       } else {
+           printf("\\x%x", (int)a);
+       }
+    }
+    printf("\n");
+}
+
 void fontcallback(void*self,U16 id,U8 * name)
 { SWFFONT* font;
   TAG* t;
   
   swf_FontExtract(&swf,id,&font);
-  printf("#<font %d \"%s\"%s%s>\n",id, name,swf_FontIsBold(font)?" bold":"",swf_FontIsItalic(font)?" italic":"");
 
   t = swf.firstTag;
 
-  while (t)
-  { 
-    if(swf_isTextTag(t))
-       swf_TextPrintDefineText(t,font);
-    t = swf_NextTag(t);
-  }
-  
   swf_FontFree(font);
 }
 
-int main (int argc,char ** argv)
-{ int f;
 
-  processargs(argc, argv);
-  if(!filename)
-      exit(0);
+int main (int argc,char ** argv)
+{ 
+    int f;
+    processargs(argc, argv);
+    if(!filename)
+       exit(0);
 
-  f = open(filename,O_RDONLY|O_BINARY);
-  if (f>=0)
-  { if FAILED(swf_ReadSWF(f,&swf))
-    { fprintf(stderr,"%s is not a valid SWF file or contains errors.\n",filename);
-      close(f);
+    f = open(filename,O_RDONLY|O_BINARY);
+    if (f<0 || swf_ReadSWF(f,&swf)<0) {
+       fprintf(stderr,"%s is not a valid SWF file or contains errors.\n",filename);
+       if(f>=0) close(f);
+       exit(-1);
     }
-    else
-    { close(f);
-      swf_FontEnumerate(&swf,&fontcallback,0);
-      swf_FreeTags(&swf);
+    close(f);
+    
+    if(x|y|w|h) {
+       if(!w) w = (swf.movieSize.xmax - swf.movieSize.xmin) / 20;
+       if(!h) h = (swf.movieSize.ymax - swf.movieSize.ymin) / 20;
+    }
+
+    fontnum = 0;
+    swf_FontEnumerate(&swf,&fontcallback1, 0);
+    fonts = (SWFFONT**)malloc(fontnum*sizeof(SWFFONT*));
+    fontnum = 0;
+    swf_FontEnumerate(&swf,&fontcallback2, 0);
+    TAG*tag = swf.firstTag;
+    while (tag)
+    { 
+       if(swf_isTextTag(tag)) {
+           swf_ParseDefineText(tag, textcallback, 0);
+       }
+       tag = tag->next;
     }
-  } else {
-      fprintf(stderr,"File not found: %s\n",argv[1]);
-  }
   
-  return 0;
+    swf_FreeTags(&swf);
+    return 0;
 }
 
diff --git a/src/swfstrings.doc b/src/swfstrings.doc
new file mode 100644 (file)
index 0000000..5ee8cf8
--- /dev/null
@@ -0,0 +1,25 @@
+Usage: %s [options] file.swf
+
+Extracts strings from SWF files.
+
+This tool extracts strings from SWF files. It parses SWF font records
+and text records and prints unicode-encoded characters to stdout.
+
+-f --fonts
+    Print out font information for each text block
+-x --xpos <x>
+    Set bounding box x coordinate
+-y --ypos <y>
+    Set bounding box y coordinate
+-W --width <width>
+    Set bounding box width
+-H --height <height>
+    Set bounding box height
+-V --version  
+    Print version information and exit
+
+.SH AUTHORS
+
+Rainer Böhme <rfxswf@reflex-studio.de>
+.TP
+Matthias Kramm <kramm@quiss.org>