1 50 51 package org.openlaszlo.iv.flash.api; 52 53 import java.awt.geom.*; 54 55 import java.io.PrintStream ; 56 import org.openlaszlo.iv.flash.parser.*; 57 import org.openlaszlo.iv.flash.util.*; 58 import org.openlaszlo.iv.flash.api.action.*; 59 import org.openlaszlo.iv.flash.commands.*; 60 import org.openlaszlo.iv.flash.context.Context; 61 62 118 public class Instance extends FlashObject { 119 120 public static final int HAS_CLIPACTIONS = 0x80; 121 public static final int HAS_CLIP = 0x40; 122 public static final int HAS_NAME = 0x20; 123 public static final int HAS_RATIO = 0x10; 124 public static final int HAS_COLOR_TRANSF = 0x08; 125 public static final int HAS_MATRIX = 0x04; 126 public static final int HAS_CHARACTER = 0x02; 127 public static final int MOVE = 0x01; 128 129 133 public boolean isMove; 134 135 138 public AffineTransform matrix; 139 140 143 public CXForm cxform; 144 145 148 public int ratio = -1; 149 150 153 public int clip = -1; 154 155 158 public int depth; 159 160 163 public String name; 164 165 168 public FlashDef def; 169 170 173 public ClipActions actions; 174 175 178 public GenericCommand command; 179 180 public Instance() {} 181 182 public int getTag() { 183 return Tag.PLACEOBJECT2; 184 } 185 186 192 public static Instance parse( Parser p ) { 193 Instance o = new Instance(); 194 o.def = p.getDef(p.getUWord()); 195 o.depth = p.getUWord(); 196 o.matrix = p.getMatrix(); 197 if( p.getPos() < p.getTagEndPos() ) { 198 o.cxform = CXForm.parse(p,false); 199 } 200 return o; 201 } 202 203 209 public static Instance parse2( Parser p ) { 210 Instance o = new Instance(); 211 int flags = p.getUByte(); 212 o.depth = p.getUWord(); 213 if( (flags&HAS_CHARACTER) != 0 ) { 214 o.def = p.getDef(p.getUWord()); 215 } 216 if( (flags&HAS_MATRIX) != 0 ) { 217 o.matrix = p.getMatrix(); 218 } 219 if( (flags&HAS_COLOR_TRANSF) != 0 ) { 220 o.cxform = CXForm.parse(p, true); 221 } 222 if( (flags&HAS_RATIO) != 0 ) { 223 o.ratio = p.getUWord(); 224 } 225 if( (flags&HAS_CLIP) != 0 ) { 226 o.clip = p.getUWord(); 227 } 228 if( (flags&HAS_NAME) != 0 ) { 229 o.name = p.getString(); 230 } 231 if( (flags&HAS_CLIPACTIONS) != 0 ) { 232 o.actions = ClipActions.parse(p); 233 } 234 o.isMove = (flags&MOVE) != 0; 235 return o; 236 } 237 238 239 244 public boolean isScript() { 245 return def instanceof Script; 246 } 247 248 253 public Script getScript() { 254 return (Script) def; 255 } 256 257 263 public Script setScript( Script script ) { 264 def = script; 265 return script; 266 } 267 268 273 public Script copyScript() { 274 def = ((Script)def).copyScript(); 275 return (Script) def; 276 } 277 278 283 public boolean isCommand() { 284 return command != null; 285 } 286 287 292 public void setCommand( GenericCommand command ) { 293 this.command = command; 294 } 295 296 301 public GenericCommand getCommand() { 302 return command; 303 } 304 305 312 public FlashDef getFirstNestedFlashDef( AffineTransform m ) { 313 if( def == null ) return null; 314 315 if( matrix != null ) m.concatenate(matrix); 316 317 if( def instanceof Script ) { 318 Script script = (Script) def; 319 if( script.getFrameCount() > 0 ) { 320 Frame frame = script.getFrameAt(0); 321 for( int i=0; i<frame.size(); i++ ) { 322 FlashObject fo = frame.getFlashObjectAt(i); 323 if( fo instanceof Instance ) { 324 FlashDef df = ((Instance)fo).getFirstNestedFlashDef(m); 325 if( df != null ) return df; 326 } else if( fo instanceof FlashDef ) { 327 return (FlashDef) fo; 328 } 329 } 330 } 331 return null; 332 } else { 333 return def; 334 } 335 } 336 337 public void collectDeps( DepsCollector dc ) { 338 if( clip != -1 && def instanceof Script ) { 341 AffineTransform m = new AffineTransform(); 342 def = getFirstNestedFlashDef(m); 343 matrix = m; 344 } 345 if( def != null ) { 346 dc.addDep(def); 347 } 349 } 350 351 public void collectFonts( FontsCollector fc ) { 352 if( def != null ) def.collectFonts( fc ); 353 } 354 355 public void write( FlashOutput fob ) { 356 int tagPos = fob.getPos(); 357 boolean longtag = (name != null && name.length() > 8) || actions != null; 359 if( longtag ) { 360 fob.skip(6); 361 } else { 362 fob.skip(2); 363 } 364 int flags = (actions!= null? HAS_CLIPACTIONS : 0) | 365 (def != null? HAS_CHARACTER : 0) | 366 (matrix != null? HAS_MATRIX : 0) | 367 (cxform != null? HAS_COLOR_TRANSF: 0) | 368 (name != null? HAS_NAME : 0) | 369 (ratio != -1 ? HAS_RATIO : 0) | 370 (clip != -1 ? HAS_CLIP : 0) | 371 (isMove ? MOVE : 0); 372 fob.writeByte(flags); 373 fob.writeWord(depth); 374 if( def != null ) fob.writeDefID(def); 375 if( matrix != null ) fob.write(matrix); 376 if( cxform != null ) cxform.write(fob); 377 if( ratio != -1 ) fob.writeWord(ratio); 378 if( clip != -1 ) fob.writeWord(clip); 379 if( name != null ) fob.writeStringZ(name); 380 if( actions!= null ) actions.write(fob); 381 382 if( longtag ) { 383 fob.writeLongTagAt( Tag.PLACEOBJECT2, fob.getPos()-tagPos-6, tagPos ); 384 } else { 385 fob.writeShortTagAt( Tag.PLACEOBJECT2, fob.getPos()-tagPos-2, tagPos ); 386 } 387 } 388 389 public void printContent( PrintStream out, String indent ) { 390 out.println( indent+"Instance: depth="+depth ); 391 if( matrix != null ) out.println( indent+" "+matrix.toString() ); 392 if( cxform != null ) cxform.printContent(out, indent+" "); 393 if( def != null ) out.println( indent+" charID="+def.getID() ); 394 if( ratio != -1 ) out.println( indent+" ratio="+ratio ); 395 if( clip != -1 ) out.println( indent+" clip="+clip ); 396 if( name != null ) out.println( indent+" name="+name ); 397 if( actions != null ) actions.printContent( out, indent+" " ); 398 if( command != null ) command.printContent( out, indent ); 399 } 400 401 protected boolean _isConstant() { 402 if( isCommand() ) return false; 403 if( name != null && Util.hasVar(name) ) return false; 404 if( actions != null && !actions.isConstant() ) return false; 405 if( def != null ) return def.isConstant(); 406 return true; 407 } 408 409 public void process( FlashFile file, Context context ) throws IVException { 410 if( def != null && !isCommand() ) { 411 def.process(file,context); 412 } 413 } 414 415 public boolean isProcessed() { 416 if( def != null && !isCommand() ) { 417 return def.isProcessed(); 418 } 419 return true; 420 } 421 422 public void setProcessed() { 423 if( def != null && !isCommand() ) { 424 def.setProcessed(); 425 } 426 } 427 428 public void apply( Context context ) { 429 if( isCommand() ) return; 430 super.apply(context); 431 name = context.apply(name); 432 if( actions != null ) actions.apply(context); 433 if( def != null ) def.apply(context); 434 } 435 436 public Rectangle2D getBounds() { 437 if( def == null ) return null; 438 if( matrix == null ) return def.getBounds(); 439 440 return GeomHelper.calcBounds(matrix, def.getBounds()); 441 } 442 443 protected FlashItem copyInto( FlashItem item, ScriptCopier copier ) { 444 super.copyInto( item, copier ); 445 ((Instance)item).matrix = matrix!=null? (AffineTransform) matrix.clone(): null; 446 ((Instance)item).cxform = cxform!=null? (CXForm) cxform.getCopy(copier): null; 447 ((Instance)item).isMove = isMove; 448 ((Instance)item).ratio = ratio; 449 ((Instance)item).clip = clip; 450 ((Instance)item).depth = depth; 451 ((Instance)item).name = name; 452 ((Instance)item).actions = actions != null? (ClipActions) actions.getCopy( copier ): null; 453 ((Instance)item).def = copier.copy(def); 454 GenericCommand myCommand = command!=null? (GenericCommand) command.getCopy(copier): null; 455 if( myCommand != null ) myCommand.setInstance((Instance)item); 456 ((Instance)item).command = myCommand; 457 return item; 458 } 459 460 public FlashItem getCopy( ScriptCopier copier ) { 461 return copyInto( new Instance(), copier ); 462 } 463 } 464 | Popular Tags |