fixed parsing of filter expressions
[swftools.git] / src / swfbbox.c
blob9eafc24a655668d6abf168a404af837b99820e34
1 /* swfbbox.c
2 Tool for playing around with SWF bounding boxes.
4 Part of the swftools package.
6 Copyright (c) 2003 Matthias Kramm <kramm@quiss.org>
8 This program is free software; you can redistribute it and/or modify
9 it under the terms of the GNU General Public License as published by
10 the Free Software Foundation; either version 2 of the License, or
11 (at your option) any later version.
13 This program is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 GNU General Public License for more details.
18 You should have received a copy of the GNU General Public License
19 along with this program; if not, write to the Free Software
20 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA */
22 #include "../config.h"
23 #include <stdio.h>
24 #include <stdarg.h>
25 #include <assert.h>
26 #include <unistd.h>
27 #include <fcntl.h>
28 #include "../lib/rfxswf.h"
29 #include "../lib/args.h"
30 #include "../lib/log.h"
32 static char * filename = 0;
33 static char * outfilename = "output.swf";
34 static int optimize = 0;
35 static int swifty = 0;
36 static int verbose = 0;
37 static int showbbox = 0;
38 static int showorigbbox = 1;
39 static int expand = 0;
40 static int clip = 0;
41 static int checkclippings = 0;
43 static struct options_t options[] = {
44 {"h", "help"},
45 {"b", "bbox"},
46 {"B", "newbbox"},
47 {"e", "expand"},
48 {"O", "optimize"},
49 {"S", "swifty"},
50 {"c", "clip"},
51 {"o", "output"},
52 {"v", "verbose"},
53 {"V", "version"},
54 {0,0}
57 int args_callback_option(char*name,char*val)
59 if(!strcmp(name, "V")) {
60 printf("swfbbox - part of %s %s\n", PACKAGE, VERSION);
61 exit(0);
63 else if(!strcmp(name, "b")) {
64 showorigbbox = 2;
65 if(showbbox == 1) showbbox = 0;
66 return 0;
68 else if(!strcmp(name, "B")) {
69 showbbox = 2;
70 return 0;
72 else if(!strcmp(name, "O")) {
73 optimize = 1;
74 if(showorigbbox == 1) showorigbbox = 0;
75 return 0;
77 else if(!strcmp(name, "S")) {
78 swifty = 1;
79 if(showorigbbox == 1) showorigbbox = 0;
80 return 0;
82 else if(!strcmp(name, "c")) {
83 if(showorigbbox == 1) showorigbbox = 0;
84 clip = 1;
85 return 0;
87 else if(!strcmp(name, "v")) {
88 verbose ++;
89 return 0;
91 else if(!strcmp(name, "q")) {
92 if(verbose)
93 verbose --;
94 return 0;
96 else if(!strcmp(name, "Q")) {
97 /* DEPRECATED- was used for testing the bbox-clip feature
98 of pdf2swf */
99 if(showorigbbox == 1) showorigbbox = 0;
100 checkclippings = 1;
101 return 0;
103 else if(!strcmp(name, "e")) {
104 expand = 1;
105 return 0;
107 else if(!strcmp(name, "o")) {
108 outfilename = val;
109 return 1;
111 else {
112 printf("Unknown option: -%s\n", name);
113 exit(1);
116 return 0;
118 int args_callback_longoption(char*name,char*val)
120 return args_long2shortoption(options, name, val);
122 void args_callback_usage(char *name)
124 printf("\n");
125 printf("Usage: %s [-OS] file.swf\n", name);
126 printf("\n");
127 printf("-h , --help Print help and exit\n");
128 printf("-b , --bbox Show movie bounding box (default)\n");
129 printf("-B , --newbbox Show recalculated (optimized/expanded) bounding box\n");
130 printf("-e , --expand Write out a new file using the recalculated header bounding box\n");
131 printf("-O , --optimize Recalculate all object bounding boxes (except for the header)\n");
132 printf("-S , --swifty Print out transformed bounding boxes\n");
133 printf("-c , --clip Clip bounding boxes to movie size\n");
134 printf("-o , --output <filename> Set output filename to <filename> (for -O)\n");
135 printf("-v , --verbose Be more verbose\n");
136 printf("-V , --version Print program version and exit\n");
137 printf("\n");
139 int args_callback_command(char*name,char*val)
141 if(filename) {
142 fprintf(stderr, "Only one file allowed. You supplied at least two. (%s and %s)\n",
143 filename, name);
145 filename = name;
146 return 0;
149 #define swf_ResetReadBits(tag) if (tag->readBit) { tag->pos++; tag->readBit = 0; }
151 void swf_Shape2Optimize(SHAPE2*shape)
153 if(!shape->bbox)
154 shape->bbox = malloc(sizeof(SRECT));
155 *(shape->bbox) = swf_GetShapeBoundingBox(shape);
159 {char {x1 y1 x2 y2 x3 y3 x4 y4]]
162 SRECT bboxes[65536];
163 U16 depth2id[65536];
164 char*depth2name[65536];
166 int hasid(TAG*tag)
168 if(tag->id == ST_PLACEOBJECT)
169 return 1;
170 if(tag->id == ST_PLACEOBJECT2 && (tag->data[0] & 2))
171 return 1;
172 if(tag->id == ST_PLACEOBJECT3 && (tag->data[0] & 2))
173 return 1;
174 return 0;
177 int hasname(TAG*tag)
179 if(tag->id == ST_PLACEOBJECT)
180 return 0;
181 if(tag->id == ST_PLACEOBJECT2 && (tag->data[0] & 0x20))
182 return 1;
183 if(tag->id == ST_PLACEOBJECT3 && (tag->data[0] & 0x20))
184 return 1;
185 return 0;
188 char* getname(TAG*tag)
190 if(tag->id == ST_PLACEOBJECT)
191 return 0;
192 if(tag->id == ST_PLACEOBJECT2 && (tag->data[0] & 0x20)) {
193 SWFPLACEOBJECT o;
194 tag->pos = 0;tag->readBit = 0;
195 swf_GetPlaceObject(tag, &o);
196 return o.name;
198 if(tag->id == ST_PLACEOBJECT3 && (tag->data[0] & 0x20)) {
199 SWFPLACEOBJECT o;
200 tag->pos = 0;tag->readBit = 0;
201 swf_GetPlaceObject(tag, &o);
202 return o.name;
204 return 0;
207 MATRIX getmatrix(TAG*tag)
209 SWFPLACEOBJECT o;
210 tag->pos = 0;tag->readBit = 0;
211 swf_GetPlaceObject(tag, &o);
212 return o.matrix;
216 static int fontnum = -1;
217 static SWFFONT**fonts;
218 static SWF*c_swf;
219 static void fontcallback1(void*self, U16 id,U8 * name)
220 { fontnum++;
222 static void fontcallback2(void*self, U16 id,U8 * name)
224 fonts[fontnum] = 0;
225 swf_FontExtract(c_swf,id,&fonts[fontnum]);
226 if(verbose) {
227 if(fonts[fontnum]) printf("Extracting font %d (%s)\n", id, name);
228 else printf("Extracting font %d (%s) failed\n", id, name);
229 fflush(stdout);
231 fontnum++;
233 typedef struct _textbounds
235 SRECT r;
236 MATRIX m; // character transform matrix
237 } textbounds_t;
239 typedef struct _placement
241 SWFPLACEOBJECT* po;
242 int num;
243 } placement_t;
245 static placement_t* placements;
247 static placement_t* readPlacements(SWF*swf)
249 placement_t* p = (placement_t*)rfx_calloc(sizeof(placement_t)*65536);
250 TAG*tag = swf->firstTag;
251 while(tag) {
252 if(swf_isPlaceTag(tag)) {
253 SWFPLACEOBJECT*po = rfx_alloc(sizeof(SWFPLACEOBJECT));
254 int id;
255 swf_GetPlaceObject(tag, po);
256 id = po->id;
257 if(po->move) {
258 fprintf(stderr, "MOVE tags not supported with -c");
260 p[id].po = po;
261 p[id].num++;
263 tag = tag->next;
266 return p;
269 static void freePlacements(placement_t*p)
271 int t;
272 for(t=0;t<65536;t++) {
273 if(p[t].po) {
274 swf_PlaceObjectFree(p[t].po); p[t].po = 0;
277 rfx_free(p);
280 static SRECT clipBBox(TAG*tag, SRECT mbbox, SRECT r)
282 int id = swf_GetDefineID(tag);
283 MATRIX m;
284 if(!placements[id].po) {
285 if(verbose)
286 printf("Id %d is never set\n", id);
287 return r;
289 if(placements[id].num>1) {
290 if(verbose)
291 printf("Id %d is set more than once\n", id);
292 return r;
294 m = placements[id].po->matrix;
295 if(m.r0 || m.r1) {
296 fprintf(stderr, "Rotating PLACEOBJECTS are not supported with -c\n");
297 return r;
300 if(verbose) {
301 printf("ID %d\n", id);
302 swf_DumpMatrix(stdout, &m);
304 mbbox.xmin -= m.tx;
305 mbbox.ymin -= m.ty;
306 mbbox.xmax -= m.tx;
307 mbbox.ymax -= m.ty;
308 mbbox.xmin *= 65536.0/m.sx;
309 mbbox.xmax *= 65536.0/m.sx;
310 mbbox.ymin *= 65536.0/m.sy;
311 mbbox.ymax *= 65536.0/m.sy;
313 if(verbose) {
314 printf("border: %f/%f/%f/%f - rect: %f/%f/%f/%f\n",
315 mbbox.xmin /20.0,
316 mbbox.ymin /20.0,
317 mbbox.xmax /20.0,
318 mbbox.ymax /20.0,
319 r.xmin /20.0,
320 r.ymin /20.0,
321 r.xmax /20.0,
322 r.ymax /20.0);
325 if(checkclippings) {
326 int clip = 0;
327 if(r.xmax > mbbox.xmax) clip += r.xmax - mbbox.xmax;
328 if(r.ymax > mbbox.ymax) clip += r.ymax - mbbox.ymax;
329 if(r.xmax < mbbox.xmin) clip += -(r.xmax - mbbox.xmin);
330 if(r.ymax < mbbox.ymin) clip += -(r.ymax - mbbox.ymin);
332 if(r.xmin > mbbox.xmax) clip += r.xmin = mbbox.xmax;
333 if(r.ymin > mbbox.ymax) clip += r.ymin = mbbox.ymax;
334 if(r.xmin < mbbox.xmin) clip += -(r.xmin = mbbox.xmin);
335 if(r.ymin < mbbox.ymin) clip += -(r.ymin = mbbox.ymin);
336 if(clip > 3*20) {
337 printf("needs clipping: [%.2f %.2f %2.f %2.f] is outside [%.2f %2.f %2.f %2.f]\n",
338 r.xmin / 20.0, r.ymin / 20.0, r.xmax / 20.0, r.ymax / 20.0,
339 mbbox.xmin / 20.0, mbbox.ymin / 20.0, mbbox.xmax / 20.0, mbbox.ymax / 20.0
344 r = swf_ClipRect(mbbox, r);
346 if(verbose) {
347 printf("new rect: %f/%f/%f/%f\n",
348 r.xmin /20.0,
349 r.ymin /20.0,
350 r.xmax /20.0,
351 r.ymax /20.0);
354 return r;
358 static void textcallback(void*self, int*chars, int*xpos, int nr, int fontid, int fontsize,
359 int xstart, int ystart, RGBA* color)
361 textbounds_t * bounds = (textbounds_t*)self;
362 SWFFONT*font = 0;
363 int t;
364 for(t=0;t<fontnum;t++) {
365 if(fonts[t]->id == fontid) {
366 font = fonts[t];
367 break;
370 if(!font) {
371 fprintf(stderr, "Font %d unknown\n", fontid);
372 exit(1);
374 if(!font->layout) {
375 /* This is an expensive operation- but what should we do, we
376 need the glyph's bounding boxes */
377 swf_FontCreateLayout(font);
380 if(verbose)
381 printf("%d chars, font %d, size %d, at (%d,%d)\n", nr, fontid, fontsize, xstart, ystart);
383 for(t=0;t<nr;t++) {
384 /* not tested yet- the matrix/fontsize calculation is probably all wrong */
385 int x = xstart + xpos[t];
386 int y = ystart;
387 int ch = 0;
388 SRECT newglyphbbox, glyphbbox = font->layout->bounds[chars[t]];
389 MATRIX m = bounds->m;
390 SPOINT p;
392 if(chars[t] < font->numchars && font->glyph2ascii) {
393 ch = font->glyph2ascii[chars[t]];
396 p.x = x; p.y = y;
397 p = swf_TurnPoint(p, &m);
399 m.sx = (m.sx * fontsize) / 1024;
400 m.sy = (m.sy * fontsize) / 1024;
401 m.r0 = (m.r0 * fontsize) / 1024;
402 m.r1 = (m.r1 * fontsize) / 1024;
404 m.tx += p.x;
405 m.ty += p.y;
406 newglyphbbox = swf_TurnRect(glyphbbox, &m);
408 if(ch<32) ch='?';
410 swf_ExpandRect2(&(bounds->r), &newglyphbbox);
411 if(verbose >= 2) {
412 printf("%5d %c, %d %d %d %d (%d %d %d %d) -> %d %d %d %d\n",
413 xpos[t], ch,
414 glyphbbox.xmin, glyphbbox.ymin, glyphbbox.xmax, glyphbbox.ymax,
415 newglyphbbox.xmin, newglyphbbox.ymin, newglyphbbox.xmax, newglyphbbox.ymax,
416 bounds->r.xmin, bounds->r.ymin, bounds->r.xmax, bounds->r.ymax);
422 static void swf_OptimizeBoundingBoxes(SWF*swf)
424 TAG* tag = swf->firstTag;
426 while (tag) {
427 if (tag->id == ST_DEFINESHAPE ||
428 tag->id == ST_DEFINESHAPE2 ||
429 tag->id == ST_DEFINESHAPE3 ||
430 tag->id == ST_DEFINESHAPE4) {
431 SHAPE2 s;
432 if(verbose) printf("%s\n", swf_TagGetName(tag));
433 swf_ParseDefineShape(tag, &s);
434 if(optimize)
435 swf_Shape2Optimize(&s);
436 tag->len = 2;
437 tag->pos = 0;
438 if(!s.bbox) {
439 fprintf(stderr, "Internal error (5)\n");
440 exit(1);
442 if(clip || checkclippings) {
443 *s.bbox = clipBBox(tag, swf->movieSize, *s.bbox);
445 swf_SetShape2(tag, &s);
447 if (tag->id == ST_DEFINETEXT || tag->id == ST_DEFINETEXT2) {
448 SRECT oldbox;
449 int matrix_offset;
450 int len;
451 U8*data;
452 textbounds_t bounds;
453 if(verbose) printf("%s\n", swf_TagGetName(tag));
454 if(fontnum < 0) {
455 if(verbose) printf("Extracting fonts...\n");
456 c_swf = swf;
457 fontnum = 0;
458 swf_FontEnumerate(swf,&fontcallback1,0);
459 fonts = (SWFFONT**)malloc(fontnum*sizeof(SWFFONT*));
460 memset(fonts, 0, fontnum*sizeof(SWFFONT*));
461 fontnum = 0;
462 swf_FontEnumerate(swf,&fontcallback2,0);
465 memset(&bounds, 0, sizeof(bounds));
467 swf_SetTagPos(tag, 0);
468 swf_GetU16(tag);
469 swf_GetRect(tag,&oldbox);
470 swf_ResetReadBits(tag);
471 matrix_offset = tag->pos;
472 swf_GetMatrix(tag,&bounds.m);
473 swf_ParseDefineText(tag, textcallback, &bounds);
474 if(verbose) {
475 printf("\n");
476 swf_DumpMatrix(stdout, &bounds.m);
477 printf("old: %d %d %d %d\n", oldbox.xmin, oldbox.ymin, oldbox.xmax, oldbox.ymax);
478 printf("new: %d %d %d %d\n", bounds.r.xmin, bounds.r.ymin, bounds.r.xmax, bounds.r.ymax);
480 if(!optimize)
481 bounds.r = oldbox; //set to old bounds from the tag header
482 if(clip || checkclippings) {
483 bounds.r = clipBBox(tag, swf->movieSize, bounds.r);
486 /* now comes the tricky part:
487 we have to fiddle the data back in
488 thank heavens that the bbox is follow by a matrix
489 struct, which always starts on a byte boundary.
491 len = tag->len - matrix_offset;
492 data = malloc(len);
493 memcpy(data, &tag->data[matrix_offset], len);
494 tag->writeBit = 0;
495 tag->len = 2;
496 swf_SetRect(tag, &bounds.r);
497 swf_SetBlock(tag, data, len);
498 free(data);
499 tag->pos = tag->readBit = 0;
501 tag = tag->next;
505 static void showSwiftyOutput(SWF*swf)
507 TAG*tag = swf->firstTag;
508 int frame=0;
509 printf("{\n\t{frame %d}\n", frame++);
511 while (tag) {
512 if (tag->id == ST_SHOWFRAME) {
513 printf("}\n{\n\t{frame %d}\n", frame++);
515 if (swf_isPlaceTag(tag)) {
516 if(hasid(tag)) {
517 depth2id[swf_GetDepth(tag)] = swf_GetPlaceID(tag);
519 if(hasname(tag)) {
520 depth2name[swf_GetDepth(tag)] = getname(tag);
523 if (swf_isPlaceTag(tag)) {
524 MATRIX m = getmatrix(tag);
525 U16 id = depth2id[swf_GetDepth(tag)];
526 char*name = depth2name[swf_GetDepth(tag)];
527 char buf[40];
528 SRECT bbox = bboxes[id];
529 SPOINT p1,p2,p3,p4;
530 p1.x = bbox.xmin; p1.y = bbox.ymin;
531 p2.x = bbox.xmax; p2.y = bbox.ymin;
532 p3.x = bbox.xmin; p3.y = bbox.ymax;
533 p4.x = bbox.xmax; p4.y = bbox.ymax;
534 p1 = swf_TurnPoint(p1, &m);
535 p2 = swf_TurnPoint(p2, &m);
536 p3 = swf_TurnPoint(p3, &m);
537 p4 = swf_TurnPoint(p4, &m);
538 if(!name) {
539 sprintf(buf, "ID%d", id);name = buf;
541 //printf("\t#%.4f %.4f %.4f %.4f | %.4f %.4f\n", m.sx/65536.0, m.r1/65536.0, m.r0/65536.0, m.sy/65536.0, m.tx/20.0, m.ty/20.0);
542 printf("\t{%s {%.2f %.2f %.2f %.2f %.2f %.2f %.2f %.2f}}\n", name,
543 p1.x/20.0, p1.y/20.0, p2.x/20.0, p2.y/20.0,
544 p3.x/20.0, p3.y/20.0, p4.x/20.0, p4.y/20.0);
546 tag = tag->next;
548 printf("}\n");
550 static SRECT getMovieClipBBox(TAG*tag)
552 //TAG*tag = swf->firstTag;
553 int frame=0;
554 SRECT movieSize;
555 U16 depth2id[65536];
556 memset(depth2id, 0, sizeof(depth2id));
558 memset(&movieSize,0,sizeof(SRECT));
560 while (tag && tag->id != ST_END) {
561 if (swf_isPlaceTag(tag)) {
562 if(hasid(tag)) {
563 depth2id[swf_GetDepth(tag)] = swf_GetPlaceID(tag);
566 if (swf_isPlaceTag(tag)) {
567 MATRIX m = getmatrix(tag);
568 U16 id = depth2id[swf_GetDepth(tag)];
569 SRECT bbox = bboxes[id];
571 SRECT tbbox = swf_TurnRect(bbox, &m);
572 swf_ExpandRect2(&movieSize, &tbbox);
574 tag = tag->next;
576 return movieSize;
579 static SRECT getSWFBBox(SWF*swf)
581 SRECT movieSize = getMovieClipBBox(swf->firstTag);
583 return movieSize;
586 int main (int argc,char ** argv)
588 TAG*tag;
589 SWF swf;
590 int fi;
591 SRECT oldMovieSize;
592 SRECT newMovieSize;
593 memset(bboxes, 0, sizeof(bboxes));
594 memset(depth2name, 0, sizeof(depth2name));
596 processargs(argc, argv);
597 initLog(0,0,0,0,0,verbose?LOGLEVEL_DEBUG:LOGLEVEL_WARNING);
599 if(!filename) {
600 fprintf(stderr, "You must supply a filename.\n");
601 return 1;
604 fi = open(filename,O_RDONLY|O_BINARY);
606 if (fi<0)
608 perror("Couldn't open file: ");
609 exit(1);
611 if FAILED(swf_ReadSWF(fi,&swf))
613 fprintf(stderr, "%s is not a valid SWF file or contains errors.\n",filename);
614 close(fi);
615 exit(1);
617 close(fi);
619 swf_OptimizeTagOrder(&swf);
621 if(clip || checkclippings) {
622 placements = readPlacements(&swf);
625 swf_FoldAll(&swf);
627 /* Optimize bounding boxes in case -O flag was set */
628 if(optimize || checkclippings || clip) {
629 swf_OptimizeBoundingBoxes(&swf);
632 /* Create an ID to Bounding Box table */
633 tag = swf.firstTag;
634 while (tag) {
635 if(swf_isDefiningTag(tag)) {
636 int id = swf_GetDefineID(tag);
637 if(tag->id != ST_DEFINESPRITE) {
638 bboxes[id] = swf_GetDefineBBox(tag);
639 } else {
640 swf_UnFoldSprite(tag);
641 bboxes[id] = getMovieClipBBox(tag);
642 swf_FoldSprite(tag);
643 if(verbose) {
644 printf("sprite %d is %.2fx%.2f\n", id,
645 (bboxes[id].xmax - bboxes[id].xmin)/20.0,
646 (bboxes[id].ymax - bboxes[id].ymin)/20.0);
650 tag = tag->next;
653 /* Create an ID->Bounding Box table for all bounding boxes */
654 if(swifty) {
655 showSwiftyOutput(&swf);
658 oldMovieSize = swf.movieSize;
659 newMovieSize = getSWFBBox(&swf);
661 if(optimize || expand) {
663 if(expand)
664 swf.movieSize = newMovieSize;
666 fi = open(outfilename, O_BINARY | O_RDWR | O_CREAT | O_TRUNC, 0666);
667 if(swf_WriteSWF(fi, &swf) < 0) {
668 fprintf(stderr, "Error writing file %s", outfilename);
669 close(fi);
670 exit(1);
672 close(fi);
675 if(showbbox) {
676 if(verbose>=0)
677 printf("Real Movie Size (size of visible objects): ");
678 printf("%.2f x %.2f :%.2f :%.2f\n",
679 (newMovieSize.xmax-newMovieSize.xmin)/20.0,
680 (newMovieSize.ymax-newMovieSize.ymin)/20.0,
681 (newMovieSize.xmin)/20.0,
682 (newMovieSize.ymin)/20.0
685 if(showorigbbox) {
686 if(verbose>=0)
687 printf("Movie Size accordings to file header: ");
688 printf("%.2f x %.2f :%.2f :%.2f\n",
689 (oldMovieSize.xmax-oldMovieSize.xmin)/20.0,
690 (oldMovieSize.ymax-oldMovieSize.ymin)/20.0,
691 (oldMovieSize.xmin)/20.0,
692 (oldMovieSize.ymin)/20.0
696 swf_FreeTags(&swf);
698 if(placements) {
699 freePlacements(placements);
701 return 0;