X-Git-Url: http://git.asbjorn.biz/?p=swftools.git;a=blobdiff_plain;f=src%2Fswfstrings.c;h=f288276687286584da453252998a441ed0001169;hp=44a0432174de0b3d120d186ea3b3c984330a8539;hb=eac1308d474f42fb9a7f8c89b1a02ff63c7c60e2;hpb=d9c36e82b736ea22d3ae14c9c9f085a069991407 diff --git a/src/swfstrings.c b/src/swfstrings.c index 44a0432..f288276 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,47 +95,152 @@ 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 || yy > y+h) { + /* outside of bounding box */ + ///printf("(%d+%d,%d) -> (%d,%d)\n", startx, advance[t]/20, starty, xx, yy); + if(t==nr-1) return; + else continue; + } + } + + unsigned char a; + int advance = 0; + if(font) { + 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) -{ LPSWFFONT font; - LPTAG t; +{ SWFFONT* font; + TAG* t; swf_FontExtract(&swf,id,&font); - printf("#< %s %s %s>\n",name,swf_FontIsBold(font)?"bold":"",swf_FontIsItalic(font)?"italic":""); t = swf.firstTag; - while (t) - { swf_TextPrintDefineText(t,font); - t = swf_NextTag(t); - } - swf_FontFree(font); } -int main (int argc,char ** argv) -{ int f; +TAG**id2tag = 0; - 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; + } + + id2tag = malloc(sizeof(TAG)*65536); + + 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)) { + id2tag[swf_GetDefineID(tag)] = tag; + } else if(swf_isPlaceTag(tag)) { + SWFPLACEOBJECT po; + swf_SetTagPos(tag, 0); + swf_GetPlaceObject(tag, &po); + if(!po.move && id2tag[po.id]) { + TAG*text = id2tag[po.id]; + swf_SetTagPos(text, 0); + swf_GetU16(text); + swf_GetRect(text, NULL); + swf_ResetReadBits(text); + MATRIX m,tm; + swf_GetMatrix(text, &tm); + swf_MatrixJoin(&m, &po.matrix, &tm); + swf_ParseDefineText(text, textcallback, &m); + } + } + tag = tag->next; } - } else { - fprintf(stderr,"File not found: %s\n",argv[1]); - } - return 0; + swf_FreeTags(&swf); + return 0; }