1 50 51 55 56 package org.openlaszlo.iv.flash.api; 57 58 import java.io.*; 59 import java.util.*; 60 import java.awt.geom.Rectangle2D ; 61 import java.awt.geom.AffineTransform ; 62 63 import org.openlaszlo.iv.flash.util.*; 64 65 import org.openlaszlo.iv.flash.parser.*; 66 import org.openlaszlo.iv.flash.commands.*; 67 import org.openlaszlo.iv.flash.cache.*; 68 import org.openlaszlo.iv.flash.api.text.*; 69 import org.openlaszlo.iv.flash.api.image.*; 70 import org.openlaszlo.iv.flash.api.sound.*; 71 import org.openlaszlo.iv.flash.api.shape.*; 72 import org.openlaszlo.iv.flash.api.action.*; 73 import org.openlaszlo.iv.flash.api.button.*; 74 import org.openlaszlo.iv.flash.context.Context; 75 import org.openlaszlo.iv.flash.context.FakeContext; 76 import org.openlaszlo.iv.flash.context.StandardContext; 77 78 106 public final class Script extends FlashDef { 107 108 private Timeline timeline; private boolean isProcessed = false; private IVVector gl_commands; private IVVector zero_frame; private SetBackgroundColor bkgColor; 114 117 public Script() {} 118 119 125 public Script( int frameNumber ) { 126 this( 0, frameNumber ); 127 } 128 129 139 public Script( int objID, int frameNumber ) { 140 timeline = new Timeline( frameNumber+1 ); 141 setID( objID ); 142 } 143 144 151 public boolean isMain() { 152 return getID() == -1; 153 } 154 155 158 public void setMain() { 159 setID( -1 ); 160 } 161 162 165 public void resetMain() { 166 setID( 0 ); 167 } 168 169 174 public Timeline getTimeline() { 175 return timeline; 176 } 177 178 187 public void generate( FlashOutput fob ) { 188 int frameCount = timeline.getFrameCount(); 189 fob.writeWord( frameCount ); 190 191 FontsCollector fc = new FontsCollector(); 193 collectFonts( fc ); 194 195 int licenseLen = 32; 197 fob.writeTag( Tag.SERIALNUMBER, licenseLen ); 198 for (int i=0; i < licenseLen; i++) 199 fob.writeByte( 0x78 ); 201 if( bkgColor != null ) bkgColor.write( fob ); 203 204 mergeFonts( fc.getFonts() ); 205 206 timeline.generate(fob, new DepsCollector(fc)); 208 209 Tag.END_TAG.write( fob ); 210 } 211 212 224 public void generate( FlashOutput fob, FontsCollector fc1, 225 FontsCollector pfc, boolean hasPreloader ) { 226 int frameCount = timeline.getFrameCount(); 227 fob.writeWord( frameCount ); 228 229 if( bkgColor != null ) bkgColor.write( fob ); 231 232 mergeFonts( fc1.getFonts() ); 241 if (hasPreloader) { 242 mergeFonts( pfc.getFonts() ); 243 } 244 245 IVVector fonts; 246 int frame = 0; 247 if (hasPreloader) { 250 fonts = pfc.getFonts(); 252 for(int i = 0; i < fonts.size(); i++) { 253 FontDef fontDef = (FontDef)fonts.elementAt(i); 254 fontDef.write( fob ); 255 } 256 257 Frame fo = (Frame) timeline.elementAt(frame++); 260 fo.generate( fob, new DepsCollector(new FontsCollector()) ); 261 } 262 263 fonts = fc1.getFonts(); 265 for(int i = 0; i < fonts.size(); i++) { 266 FontDef fontDef = (FontDef)fonts.elementAt(i); 267 fontDef.write( fob ); 268 } 269 270 timeline.generate(fob, new DepsCollector(new FontsCollector()), frame); 273 274 Tag.END_TAG.write( fob ); 275 } 276 277 286 public void write( FlashOutput fob ) { 287 int start = fob.getPos(); fob.skip( 6 ); fob.writeDefID( this ); 290 int frameCount = timeline.getFrameCount(); 291 fob.writeWord( frameCount ); 292 293 if( bkgColor != null ) bkgColor.write( fob ); 295 296 if( zero_frame != null ) zero_frame.write( fob ); 298 299 timeline.write( fob ); 301 302 Tag.END_TAG.write( fob ); 304 305 int size = fob.getPos()-start-6; fob.writeLongTagAt( Tag.DEFINESPRITE, size, start ); 307 } 308 309 317 public static Script parse( Parser p, boolean isMain ) throws IVException { 318 int objID = -1; 319 if( !isMain ) { 320 objID = p.getUWord(); 321 } 322 323 int frameNum = p.getUWord(); 325 Script sc = new Script( objID, frameNum ); 327 328 Timeline tl = sc.getTimeline(); 329 IVVector tFrame = new IVVector(); 330 sc.zero_frame = tFrame; 331 String frameName = null; 332 boolean is_anchor = false; 333 334 for(;;) { 335 int code = p.getTag(); 336 337 switch( code ) { 339 340 case Tag.SHOWFRAME: { 341 Frame f = new Frame( tFrame ); 342 f.setName( frameName ); 343 f.setAnchor(is_anchor); 344 frameName = null; 345 is_anchor = false; 346 tl.addFrame( f ); 347 tFrame.reset(); 348 break; 349 } 350 case Tag.END: 351 return sc; 352 case Tag.PLACEOBJECT: 353 tFrame.addElement( Instance.parse( p ) ); 354 break; 355 case Tag.PLACEOBJECT2: { 356 Instance instance = Instance.parse2(p); 357 GenericCommand cmd = GenericCommand.checkAndParseMX(instance); 358 if( cmd != null && cmd.isGlobal() ) { 359 sc.addGlobalCommand(cmd); 360 } else { 361 tFrame.addElement(instance); 362 } 363 break; 364 } 365 case Tag.REMOVEOBJECT: 366 tFrame.addElement( RemoveObject.parse( p ) ); 367 break; 368 case Tag.REMOVEOBJECT2: 369 tFrame.addElement( RemoveObject.parse2( p ) ); 370 break; 371 case Tag.SETBKGCOLOR: 372 sc.setBackgroundColor( SetBackgroundColor.parse(p) ); 373 break; 374 case Tag.STARTSOUND: 375 tFrame.addElement( StartSound.parse(p) ); 376 break; 377 case Tag.DOACTION: 378 tFrame.addElement( DoAction.parse(p) ); 379 break; 380 case Tag.INITCLIPACTION: 381 tFrame.addElement( InitClipAction.parse(p) ); 382 break; 383 case Tag.TEMPLATECOMMAND: { 384 GenericCommand cmd = GenericCommand.parse(p); 385 if( cmd == null ) break; 386 if( !cmd.isGlobal() ) { 387 int j=tl.getFrameCount(); 389 IVVector myFrame = tFrame; 390 timelineLoop: 391 for(;;) { 392 for( int i=myFrame.size(); --i>=0; ) { 393 Object o = myFrame.elementAt(i); 394 if( o instanceof Instance ) { 395 Instance inst = (Instance) o; 396 if( inst.depth != cmd.getDepth() ) continue; 397 cmd.setInstance(inst); 398 inst.setCommand(cmd); 399 break timelineLoop; 400 } 401 } 402 if( --j < 0 ) break; 403 myFrame = tl.getFrameAt(j); 404 }; 405 if( j<0 ) { 406 Log.logRB(Resource.GENCMDERR, new Object [] {cmd.getCommandName()}); 407 } 408 } else { 409 sc.addGlobalCommand( cmd ); 410 } 411 break; 412 } 413 414 case Tag.FLASHGENERATOR: 415 p.getFile().setTemplate(true); 416 break; 417 case Tag.PROTECT: 418 tFrame.addElement( p.newUnknownTag() ); 419 break; 420 case Tag.SERIALNUMBER: 421 break; 422 case Tag.FRAMELABEL: 423 frameName = p.getString(); 425 if( p.getPos() < p.getTagEndPos() ) { 426 is_anchor = p.getUByte()==1; 427 } 428 break; 429 case Tag.GENERATORTEXT: { 430 int id = p.getUWord(); 431 Text text = (Text) p.getDef(id); 432 text.parseGenText(p); 433 break; 434 } 435 case Tag.NAMECHARACTER: { 436 int id = p.getUWord(); 437 String name = p.getString(); 438 FlashDef def = p.getDef(id); 439 def.setName(name); 440 p.addDefToLibrary( name, def ); 441 break; 442 } 443 case Tag.FREECHARACTER: 444 tFrame.addElement( FreeCharacter.parse( p ) ); 445 break; 446 447 case Tag.SOUNDSTREAMHEAD: 448 case Tag.SOUNDSTREAMHEAD2: 449 tFrame.addElement( SoundStreamHead.parse( p ) ); 450 break; 451 case Tag.SOUNDSTREAMBLOCK: 452 tFrame.addElement( SoundStreamBlock.parse( p ) ); 453 break; 454 455 case Tag.DEFINESPRITE: 456 p.addDef( Script.parse( p, false ) ); 457 break; 458 case Tag.DEFINEMOVIE: 459 p.addDef( QTMovie.parse(p) ); 460 break; 461 case Tag.DEFINESHAPE: 462 case Tag.DEFINESHAPE2: 463 case Tag.DEFINESHAPE3: 464 p.addDef( LazyShape.parse( p ) ); 465 break; 466 case Tag.DEFINEMORPHSHAPE: 467 p.addDef( LazyMorphShape.parse( p ) ); 468 break; 469 case Tag.DEFINESOUND: 470 p.addDef( LazySound.parse( p ) ); 471 break; 472 case Tag.DEFINEFONT: 473 p.addDef( FontDef.parse( p ) ); 474 break; 475 case Tag.DEFINEFONTINFO: 476 FontDef.parseFontInfoTag( p ); 477 break; 478 case Tag.DEFINEFONTINFO2: 479 FontDef.parseFontInfoTag2( p ); 480 break; 481 case Tag.DEFINEFONT2: 482 p.addDef( FontDef.parse2( p ) ); 483 break; 484 case Tag.EXTERNALFONT: { 485 FlashDef def = FontDef.parseExternalFontTag( p ); 486 if( def != null ) p.addDef( def ); 487 break; 488 } 489 case Tag.DEFINETEXT: 490 p.addDef( Text.parse( p, false ) ); 491 break; 492 case Tag.DEFINETEXT2: 493 p.addDef( Text.parse( p, true ) ); 494 break; 495 case Tag.DEFINEEDITTEXT: 496 p.addDef( TextField.parse( p ) ); 497 break; 498 case Tag.DEFINEBUTTON: 499 p.addDef( Button.parse( p ) ); 500 break; 501 case Tag.DEFINEBUTTON2: 502 p.addDef( Button2.parse2( p ) ); 503 break; 504 case Tag.DEFINEBUTTONCXFORM: 505 ButtonCXForm.parse(p); break; 507 case Tag.DEFINEBUTTONSOUND: 508 ButtonSound.parse(p); break; 510 case Tag.DEFINEBITS: 511 case Tag.DEFINEBITSJPEG2: 512 case Tag.DEFINEBITSJPEG3: 513 p.addDef( JPEGBitmap.parse(p) ); 514 break; 515 case Tag.DEFINEBITSLOSSLESS: 516 case Tag.DEFINEBITSLOSSLESS2: 517 p.addDef( LLBitmap.parse(p) ); 518 break; 519 case Tag.JPEGTABLES: 520 JPEGBitmap.parseJPegTables(p); 521 break; 522 523 case Tag.ENABLEDEBUGGER: 524 tFrame.addElement( p.newUnknownTag() ); 525 break; 526 case Tag.EXPORTASSETS: { 527 ExportAssets ea = ExportAssets.parse(p); 528 if( ea != null ) tFrame.addElement(ea); 529 break; 530 } 531 case Tag.PATHSAREPOSTSCRIPT: 532 break; 533 case Tag.IMPORTASSETS: 534 tFrame.addElement( ImportAssets.parse(p) ); 535 break; 536 537 default: 538 Log.logRB(Resource.UNKNOWNTAG, new Object [] {Util.b2h(code)}); 539 tFrame.addElement( p.newUnknownTag() ); 540 break; 541 } 542 p.skipLastTag(); 543 } 544 } 545 546 551 public SetBackgroundColor getBackgroundColor() { 552 return bkgColor; 553 } 554 555 560 public void setBackgroundColor( SetBackgroundColor bkgColor ) { 561 this.bkgColor = bkgColor; 562 } 563 564 protected void addGlobalCommand( GenericCommand cmd ) { 565 if( gl_commands == null ) gl_commands = new IVVector(); 566 gl_commands.addElement( cmd ); 567 } 568 569 576 public void removeFileDepGlobalCommands() { 577 if( gl_commands == null ) return; 578 for( int i=0; i<gl_commands.size(); i++ ) { 579 GenericCommand cmd = (GenericCommand) gl_commands.elementAt(i); 580 if( cmd instanceof MovieSetCommand ) { 581 gl_commands.removeElementAt(i); 582 i--; 583 } 584 } 585 } 586 587 596 public Instance findInstance( String name ) { 597 for( int i=0; i<timeline.getFrameCount(); i++ ) { 598 Frame frame = timeline.getFrameAt(i); 599 for( int j=0; j<frame.size(); j++ ) { 600 FlashObject fo = frame.getFlashObjectAt(j); 601 if( !(fo instanceof Instance) ) continue; 602 Instance inst = (Instance) fo; 603 if( inst.name != null && inst.name.equals( name ) ) return inst; 604 if( inst.isScript() ) { 605 inst = inst.getScript().findInstance(name); 606 if( inst != null ) return inst; 607 } 608 } 609 } 610 return null; 611 } 612 613 620 public void process( FlashFile file, Context context ) throws IVException { 621 if( isProcessed() ) return; 622 623 FakeContext fakeContext = null; 624 Context localContext = null; 625 626 if( gl_commands != null ) { 628 fakeContext = new FakeContext(context); 629 for( int i=0; i<gl_commands.size(); i++ ) { 630 GenericCommand cmd = (GenericCommand) gl_commands.elementAt(i); 631 try { 632 cmd.doCommand(file, localContext!=null?localContext:fakeContext, this, 0); 633 localContext = fakeContext.getContext(); 634 } catch( Exception e ) { 635 Log.logRB(Resource.ERRDOCMD, new Object [] {file.getFullName(), getName(), "0", cmd.getCommandName()}, e); 636 } 637 } 638 } 639 640 if( localContext == null ) { 641 localContext = new StandardContext(); 642 } 643 localContext.setParent(context); 644 645 timeline.doCommand( file, localContext, this ); 647 648 timeline.process( file, localContext ); 650 651 apply( localContext ); 653 } 654 655 660 public boolean isProcessed() { 661 return isProcessed; 662 } 663 664 667 public void setProcessed() { 668 isProcessed = true; 669 } 670 671 676 public Script copyScript() { 677 return (Script) getCopy( new ScriptCopier() ); 678 } 679 680 690 public void appendScript( Script sc ) { 691 Timeline scTm = sc.getTimeline(); 692 693 Frame lastFrame = newFrame(); 694 removeAllInstances(lastFrame); 695 Frame firstFrame = scTm.getFrameAt(0); 696 lastFrame.setName( firstFrame.getName() ); 697 lastFrame.append( firstFrame ); 698 699 for( int i=1; i<scTm.getFrameCount(); i++ ) { 700 Frame frame = scTm.getFrameAt(i); 701 timeline.addFrame(frame); 702 } 703 } 704 705 713 public void removeAllInstances( Frame lastFrame ) { 714 IVVector layers = getOccupiedLayers(); 715 for( int i=0; i<layers.size(); i++ ) { 716 Instance inst = (Instance) layers.elementAt(i); 717 if( inst == null ) continue; 718 lastFrame.removeInstance(i); 719 } 720 } 721 722 729 public Rectangle2D getBounds() { 730 Rectangle2D rect = null; 731 IVVector layers = new IVVector(); 732 int[] masks = null; 733 for( int i=0; i<timeline.getFrameCount(); i++ ) { 734 Frame frame = timeline.getFrameAt(i); 735 for( int k=0; k<frame.size(); k++ ) { 736 FlashObject fo = frame.getFlashObjectAt(k); 737 if( fo instanceof Instance ) { 738 Instance inst = (Instance) fo; 739 int layer = inst.depth; 740 Rectangle2D bounds = null; 741 FlashDef def = inst.def; 742 if( def != null ) { 743 bounds = def.getBounds(); 744 layers.setElementAt(bounds, layer); 745 } else if( inst.matrix != null ) { 746 bounds = (Rectangle2D ) layers.elementAt(layer); 747 } 748 749 if( masks!=null && masks.length>layer && masks[layer]!=0 ) continue; 751 752 if( inst.matrix != null && bounds != null ) { 753 bounds = GeomHelper.calcBounds( inst.matrix, bounds ); 754 } 755 rect = GeomHelper.add( rect, bounds ); 756 757 if( inst.clip > 0 ) { 758 int clip = inst.clip; 759 if( masks == null ) { 760 masks = new int[clip+10]; 761 } else if( masks.length <= clip ) { 762 int[] masks1 = new int[clip+10]; 763 System.arraycopy(masks, 0, masks1, 0, masks.length); 764 masks = masks1; 765 } 766 masks[layer] = clip; 767 for( int m=layer+1; m<=clip; m++ ) { 768 masks[m] = -1; 769 } 770 } 771 } else if( fo instanceof RemoveObject ) { 772 RemoveObject ro = (RemoveObject) fo; 773 int layer = ro.depth; 774 if( masks!=null && masks.length>layer && masks[layer]>0 ) { 775 int clip = masks[layer]; 777 for( int m=layer; m<=clip; m++ ) { 778 masks[m] = 0; 779 } 780 } 781 layers.setElementAt(null, layer); 782 } else { 783 rect = GeomHelper.add( rect, fo.getBounds() ); 784 } 785 } 786 } 787 if( rect == null ) { 788 rect = GeomHelper.newRectangle(); 789 } 790 return rect; 791 } 792 793 807 public int reserveLayers( int from, int num ) { 808 int cnt = timeline.getFrameCount(); 809 for( int i=0; i<cnt; i++ ) { 810 Frame frame = timeline.getFrameAt(i); 811 int fsz = frame.size(); 812 for( int k=0; k<fsz; k++ ) { 813 FlashObject fo = frame.getFlashObjectAt(k); 814 if( fo instanceof Instance ) { 815 Instance inst = (Instance) fo; 816 if( inst.depth >= from ) inst.depth += num; 817 if( inst.clip >= from ) inst.clip += num; 818 } else if( fo instanceof RemoveObject ) { 819 RemoveObject ro = (RemoveObject) fo; 820 if( ro.depth >= from ) ro.depth += num; 821 } 822 } 823 } 824 return from; 825 } 826 827 832 public int getMaxDepth() { 833 int max = 0; 834 for( int i=0; i<timeline.getFrameCount(); i++ ) { 835 Frame frame = timeline.getFrameAt(i); 836 for( int k=0; k<frame.size(); k++ ) { 837 FlashObject fo = frame.getFlashObjectAt(k); 838 if( fo instanceof Instance ) { 839 Instance inst = (Instance) fo; 840 if( inst.depth > max ) max = inst.depth; 841 if( inst.clip > max ) max = inst.clip; 842 } 843 } 844 } 845 return max; 846 } 847 848 856 public static AffineTransform interLinear( 857 double t, AffineTransform startMatrix, AffineTransform endMatrix ) 858 { 859 double t1 = 1.0-t; 860 double m00 = startMatrix.getScaleX()*t1 + endMatrix.getScaleX()*t; 861 double m11 = startMatrix.getScaleY()*t1 + endMatrix.getScaleY()*t; 862 double m01 = startMatrix.getShearX()*t1 + endMatrix.getShearX()*t; 863 double m10 = startMatrix.getShearY()*t1 + endMatrix.getShearY()*t; 864 double m02 = startMatrix.getTranslateX()*t1 + endMatrix.getTranslateX()*t; 865 double m12 = startMatrix.getTranslateY()*t1 + endMatrix.getTranslateY()*t; 866 return new AffineTransform (m00, m10, m01, m11, m02, m12); 867 } 868 869 882 public Frame addTweening( FlashDef def, int layer, 883 int startFrame, AffineTransform startMatrix, CXForm startCXF, 884 int endFrame, AffineTransform endMatrix, CXForm endCXF ) 885 { 886 return addTweening(def, layer, getFrameAt(startFrame), endFrame-startFrame, 887 startMatrix, startCXF, endMatrix, endCXF); 888 } 889 890 905 public Frame addTweening( FlashDef def, int layer, 906 int startFrame, AffineTransform startMatrix, CXForm startCXF, 907 int endFrame, AffineTransform endMatrix, CXForm endCXF, String name ) 908 { 909 return addTweening(def, layer, getFrameAt(startFrame), endFrame-startFrame, 910 startMatrix, startCXF, endMatrix, endCXF, name); 911 } 912 913 926 public Frame addTweening( FlashDef def, int layer, Frame frame, int num, 927 AffineTransform startMatrix, CXForm startCXF, 928 AffineTransform endMatrix, CXForm endCXF ) 929 { 930 return addTweening(def, layer, frame, num, startMatrix, startCXF, endMatrix, endCXF, null); 931 } 932 933 947 public Frame addTweening( FlashDef def, int layer, Frame frame, int num, 948 AffineTransform startMatrix, CXForm startCXF, 949 AffineTransform endMatrix, CXForm endCXF, String name ) 950 { 951 int startFrame = getFrameIndex(frame); 952 int endFrame = startFrame+num; 953 frame.addInstance(def, layer, startMatrix, startCXF, name); 954 for( int i=1; i<num; i++ ) { 955 double t = (double)i/num; 956 AffineTransform matrix = interLinear(t, startMatrix, endMatrix); 957 CXForm cxform = startCXF!=null? CXForm.interLinear(t, startCXF, endCXF): null; 958 frame = getFrameAt(startFrame+i); 959 frame.addInstance(layer, matrix, cxform); 960 } 961 if( startFrame != endFrame ) { 962 frame = getFrameAt(endFrame); 963 frame.addInstance(layer, endMatrix, endCXF); 964 } 965 return frame; 966 } 967 968 973 public void fadeOut( int num ) { 974 IVVector layers = getOccupiedLayers(); 975 int startFrame = getFrameCount()-1;; 976 CXForm startcx = CXForm.newAlpha(256); 977 CXForm endcx = CXForm.newAlpha(0); 978 for( int i=0; i<layers.size(); i++ ) { 979 Instance inst = (Instance) layers.elementAt(i); 980 if( inst == null ) continue; 981 for( int j=1; j<=num; j++ ) { 982 double t = (double)j/num; 983 CXForm cxform = CXForm.interLinear(t, startcx, endcx); 984 Frame frame = getFrameAt(startFrame+j); 985 frame.addInstance(i, null, cxform); 986 } 987 } 988 } 989 990 995 public IVVector getOccupiedLayers() { 996 IVVector layers = new IVVector(); 997 for( int i=0; i<timeline.getFrameCount(); i++ ) { 998 Frame frame = timeline.getFrameAt(i); 999 for( int k=0; k<frame.size(); k++ ) { 1000 FlashObject fo = frame.getFlashObjectAt(k); 1001 if( fo instanceof Instance ) { 1002 Instance inst = (Instance) fo; 1003 int layer = inst.depth; 1004 layers.setElementAt(inst, layer); 1005 } else if( fo instanceof RemoveObject ) { 1006 RemoveObject ro = (RemoveObject) fo; 1007 int layer = ro.depth; 1008 layers.setElementAt(null, layer); 1009 } 1010 } 1011 } 1012 return layers; 1013 } 1014 1015 1020 public int getFrameCount() { 1021 return timeline.getFrameCount(); 1022 } 1023 1024 1033 public Frame getFrameAt( int frameNum ) { 1034 if( timeline.getFrameCount() <= frameNum ) { 1035 for( int i=timeline.getFrameCount(); i<=frameNum; i++ ) newFrame(); 1036 } 1037 return timeline.getFrameAt(frameNum); 1038 } 1039 1040 1046 public int getFrameIndex( Frame frame ) { 1047 return timeline.getFrameIndex(frame); 1048 } 1049 1050 1055 public Frame newFrame() { 1056 return timeline.newFrame(); 1057 } 1058 1059 1064 public Frame getLastFrame() { 1065 return timeline.getFrameAt( getFrameCount()-1 ); 1066 } 1067 1068 public void apply( Context context ) { 1069 if( isConstant() || isProcessed() ) return; 1070 timeline.apply(context); 1071 } 1072 1073 public boolean isConstant() { 1074 return timeline.isConstant(); 1075 } 1076 1077 protected FlashItem copyInto( FlashItem item, ScriptCopier copier ) { 1078 super.copyInto( item, copier ); 1079 ((Script)item).zero_frame = zero_frame!=null? zero_frame.getCopy(copier): null; 1080 ((Script)item).timeline = (Timeline) timeline.getCopy(copier); 1081 ((Script)item).isProcessed = isProcessed; 1082 if( gl_commands != null ) { 1083 IVVector v = new IVVector( gl_commands.size() ); 1084 for( int i=0; i<gl_commands.size(); i++ ) { 1085 GenericCommand cmd = (GenericCommand) gl_commands.elementAt(i); 1086 v.addElement( cmd.getCopy(null) ); 1087 } 1088 ((Script)item).gl_commands = v; 1089 } 1090 ((Script)item).bkgColor = bkgColor!=null? (SetBackgroundColor)bkgColor.getCopy(copier): null; 1091 return item; 1092 } 1093 1094 public FlashItem getCopy( ScriptCopier copier ) { 1095 return copyInto( new Script(), copier ); 1096 } 1097 1098 public void collectDeps( DepsCollector dc ) { 1099 timeline.collectDeps(dc); 1101 } 1102 1103 public void collectFonts( FontsCollector fc ) { 1104 for( int i=0; i<timeline.getFrameCount(); i++ ) { 1105 Frame frame = timeline.getFrameAt(i); 1106 for( int k=0; k<frame.size(); k++ ) { 1107 FlashObject fo = frame.getFlashObjectAt(k); 1108 fo.collectFonts( fc ); 1109 } 1110 } 1111 } 1112 1113 public int getTag() { 1114 return Tag.DEFINESPRITE; 1115 } 1116 1117 public void printContent( PrintStream out, String indent ) { 1118 String id = isMain()? "main": Integer.toString(getID()); 1119 out.println( indent+"Script: id="+id+" frames="+timeline.getFrameCount()+" name='"+getName()+"'" ); 1120 if( zero_frame != null ) zero_frame.printContent( out, indent+" " ); 1121 timeline.printContent( out, indent+" " ); 1122 out.println( indent+"End Script("+id+") name='"+getName()+"'" ); 1123 } 1124 1125 1130 protected void mergeFonts( IVVector fonts ) { 1131 IVVector fonts_blocks = new IVVector(); 1133 1134 Hashtable fonts_map = new Hashtable(); 1136 1137 for( int i=0; i<fonts.size(); i++ ) { 1139 FontDef fdef = (FontDef) fonts.elementAt(i); 1140 Font font = fdef.getFont(); 1141 1143 Font font2 = FontCache.getFont(font.fontKey); 1145 if( font2 != null && font2 != font ) { 1146 if( font.isLargeThan(font2) ) { 1147 FontCache.removeFont(font2.fontKey); 1150 FontCache.addFont(font.fontKey, font); 1152 } 1153 } 1154 1155 FontDef prev_fdef = (FontDef) fonts_map.get(font.fontKey); 1156 if( prev_fdef == null ) { 1157 fonts_map.put(font.fontKey, fdef); 1159 } else { 1160 Font font_prev = prev_fdef.getFont(); 1161 if( font_prev != font ) { 1163 Font font3 = FontDef.mergeFonts(font_prev, font); 1165 if( font3 == font ) { 1166 prev_fdef.setFont(font); 1168 fonts_blocks.addElement(new Object [] {font_prev, prev_fdef.getTextBlocks()}); 1169 fonts_map.remove(font.fontKey); 1170 fonts_map.put(font.fontKey, fdef); 1171 fonts.removeElement(prev_fdef); 1172 } else { 1173 fdef.setFont(font_prev); 1175 fonts_blocks.addElement(new Object [] {font, fdef.getTextBlocks()}); 1176 fonts.removeElement(fdef); 1177 } 1178 i--; 1179 } 1180 } 1181 } 1182 1183 for( int i=0; i<fonts_blocks.size(); i++ ) { 1184 Object [] objs = (Object []) fonts_blocks.elementAt(i); 1185 Font old_font = (Font) objs[0]; 1186 String fontKey = old_font.fontKey; 1187 IVVector text_blocks = (IVVector) objs[1]; 1188 FontDef fontDef = (FontDef) fonts_map.get(fontKey); 1189 fontDef.addTextBlocks( text_blocks ); 1190 Font new_font = fontDef.getFont(); 1191 for( int j=0; j<text_blocks.size(); j++ ) { 1193 TextBlock tblock = (TextBlock) text_blocks.elementAt(j); 1194 tblock.layout(); 1195 tblock.changeFont(old_font, new_font); 1196 } 1197 } 1198 } 1199 1200 1205 public IVVector getZero_frame() { 1206 return zero_frame; 1207 } 1208 1209} 1210 | Popular Tags |