From 409a8bf0de680f50bcbb194a7174fc729b82e5cf Mon Sep 17 00:00:00 2001 From: Matthias Kramm Date: Thu, 13 Aug 2009 23:04:14 +0200 Subject: [PATCH] allow bounding box selection in swfstrings --- src/jpeg2swf.1 | 4 +- src/jpeg2swf.doc | 2 +- src/swfdump.c | 6 +- src/swfstrings.1 | 45 ++++++++----- src/swfstrings.c | 188 +++++++++++++++++++++++++++++++++++++++++----------- src/swfstrings.doc | 25 +++++++ 6 files changed, 209 insertions(+), 61 deletions(-) create mode 100644 src/swfstrings.doc diff --git a/src/jpeg2swf.1 b/src/jpeg2swf.1 index e9a854b..3da21bb 100644 --- a/src/jpeg2swf.1 +++ b/src/jpeg2swf.1 @@ -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. diff --git a/src/jpeg2swf.doc b/src/jpeg2swf.doc index ec04d0c..c386fcc 100644 --- a/src/jpeg2swf.doc +++ b/src/jpeg2swf.doc @@ -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. diff --git a/src/swfdump.c b/src/swfdump.c index 0d63c2c..a082a5f 100644 --- a/src/swfdump.c +++ b/src/swfdump.c @@ -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) { diff --git a/src/swfstrings.1 b/src/swfstrings.1 index 0ab12a7..9794713 100644 --- a/src/swfstrings.1 +++ b/src/swfstrings.1 @@ -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 - +.TP +Matthias Kramm diff --git a/src/swfstrings.c b/src/swfstrings.c index 86d8aed..bfca4fa 100644 --- a/src/swfstrings.c +++ b/src/swfstrings.c @@ -23,14 +23,20 @@ #include #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 \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 Set bounding box x coordinate\n"); + printf("-y , --ypos Set bounding box y coordinate\n"); + printf("-W , --width Set bounding box width\n"); + printf("-H , --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;tid == fontid) { + font = fonts[t]; + break; + } + } + + if(showfonts) { + if(font) + printf("#\n", fontid, font->name, swf_FontIsBold(font)?" bold":"",swf_FontIsItalic(font)?" italic":""); + printf("#\n", color->r, color->g, color->b, color->a); + printf("#\n", fontsize); + } + + for(t=0;t 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("#\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 index 0000000..5ee8cf8 --- /dev/null +++ b/src/swfstrings.doc @@ -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 + Set bounding box x coordinate +-y --ypos + Set bounding box y coordinate +-W --width + Set bounding box width +-H --height + Set bounding box height +-V --version + Print version information and exit + +.SH AUTHORS + +Rainer Böhme +.TP +Matthias Kramm -- 1.7.10.4