1 50 51 package org.openlaszlo.iv.flash.api.shape; 52 53 import java.awt.geom.*; 54 import java.io.PrintStream ; 55 56 import org.openlaszlo.iv.flash.parser.*; 57 import org.openlaszlo.iv.flash.util.*; 58 import org.openlaszlo.iv.flash.api.*; 59 60 public class LazyShape extends FlashDef { 61 62 public static class BitmapRef { 63 public FlashDef bitmap; 64 public int offset; 65 public BitmapRef( FlashDef bitmap, int offset ) { 66 this.bitmap = bitmap; 67 this.offset = offset; 68 } 69 } 70 71 protected IVVector bitmaps; 72 protected DataMarker data; 73 protected Rectangle2D bounds; 74 protected int tagCode; 75 76 public LazyShape() {} 77 78 public int getTag() { 79 return tagCode; 80 } 81 82 public static FlashDef parse( Parser p ) { 83 FlashFile file = p.getFile(); 84 if( file.isFullParsing() ) { 85 Shape shape = Shape.parse(p); 86 return shape; 87 } else { 88 LazyShape shape = new LazyShape(); 89 shape.tagCode = p.getTagCode(); 90 shape.setID( p.getUWord() ); 91 shape.bounds = p.getRect(); 92 shape.data = new DataMarker( p.getBuf(), p.getPos(), p.getTagEndPos() ); 94 extractBitmaps( p, shape ); 95 return shape; 96 } 97 } 98 99 public void write( FlashOutput fob ) { 100 fob.writeTag( tagCode, 2+GeomHelper.getSize(bounds)+data.length() ); 101 fob.writeDefID( this ); 102 fob.write(bounds); 103 int pos = fob.getPos(); 104 data.write( fob ); 105 if( bitmaps != null ) { 106 for( int i=0; i<bitmaps.size(); i++ ) { 107 BitmapRef ref = (BitmapRef) bitmaps.elementAt(i); 108 int bitmapID; 109 if( ref.bitmap == null ) { bitmapID = 0xffff; 111 } else { 112 bitmapID = fob.getDefID(ref.bitmap); 113 } 114 fob.writeWordAt( bitmapID, pos+ref.offset ); 115 } 116 } 117 } 118 119 private static void parseShapeStyle( Parser p, LazyShape shape, boolean withAlpha, int pos ) { 120 int nFills = p.getUByte(); 122 if( nFills == 255 ) { 123 nFills = p.getUWord(); 124 } 125 126 for( int i=0; i<nFills; i++ ) { 128 int fillStyle = p.getUByte(); 129 if( (fillStyle&0x10) != 0 ) { p.skipMatrix(); 132 int nColors = p.getUByte(); 134 for( int j=0; j<nColors; j++ ) { 136 p.skip(1); Color.skip(p, withAlpha); 138 } 139 } else if( (fillStyle&0x40) != 0 ) { int id_pos = p.getPos()-pos; 141 int id = p.getUWord(); FlashDef def = p.getDef(id); 143 shape.addBitmap(def, id_pos); 144 p.skipMatrix(); 145 } else { Color.skip(p, withAlpha); 147 } 148 } 149 150 int nLines = p.getUByte(); 151 if( nLines == 255 ) { 152 nLines = p.getUWord(); 153 } 154 for( int i=0; i<nLines; i++ ) { 156 p.skip(2); Color.skip(p, withAlpha); 158 } 159 160 } 161 162 private static void extractBitmaps( Parser p, LazyShape shape ) { 163 int pos = p.getPos(); 164 165 boolean withAlpha = shape.getTag() == Tag.DEFINESHAPE3; 166 167 parseShapeStyle( p, shape, withAlpha, pos ); 168 169 int nBits = p.getUByte(); 170 int nFillBits = (nBits&0xf0)>>4; 171 int nLineBits = nBits&0x0f; 172 p.initBits(); 174 for(;;) { 175 if( p.getBool() ) { if( p.getBool() ) { int nb = p.getBits(4)+2; 178 if( p.getBool() ) { 179 p.skipBits(nb+nb); } else { 181 p.skipBits(1+nb); } 183 184 } else { int nb = p.getBits(4)+2; 186 p.skipBits(nb*4); 187 } 188 } else { int flags = p.getBits(5); 190 if( flags == 0 ) break; if( (flags & StyleChangeRecord.MOVETO) != 0 ) { 192 int nMoveBits = p.getBits(5); 193 p.skipBits(nMoveBits+nMoveBits); 194 } 195 if( (flags & StyleChangeRecord.FILLSTYLE0) != 0 ) { 196 p.skipBits(nFillBits); 197 } 198 if( (flags & StyleChangeRecord.FILLSTYLE1) != 0 ) { 199 p.skipBits(nFillBits); 200 } 201 if( (flags & StyleChangeRecord.LINESTYLE) != 0 ) { 202 p.skipBits(nLineBits); 203 } 204 if( (flags & StyleChangeRecord.NEW_STYLES) != 0 ) { 205 parseShapeStyle( p, shape, withAlpha, pos ); 206 nBits = p.getUByte(); 207 nFillBits = (nBits&0xf0)>>4; 208 nLineBits = nBits&0x0f; 209 p.initBits(); 210 } 211 if( (flags&0x80) != 0 ) { 212 break; 213 } 214 } 215 } 216 } 217 218 public void printContent( PrintStream out, String indent ) { 219 out.println( indent+"LazyShape("+Tag.tagNames[tagCode]+"): id="+getID()+", name='"+getName()+"'" ); 220 out.println( indent+" "+bounds ); 221 if( bitmaps != null ) { 222 out.print( indent+" bitmaps used: " ); 223 for( int i=0; i<bitmaps.size(); i++ ) { 224 BitmapRef ref = (BitmapRef) bitmaps.elementAt(i); 225 out.print( "id["+i+"]="+(ref.bitmap!=null?ref.bitmap.getID():0xffff)+" " ); 226 } 227 out.println(); 228 } 229 } 230 231 public boolean isConstant() { 232 return true; 233 } 234 235 public Rectangle2D getBounds() { 236 return bounds; 237 } 238 239 public void collectDeps( DepsCollector dc ) { 240 if( bitmaps != null ) { 241 for( int i=0; i<bitmaps.size(); i++ ) { 242 BitmapRef ref = (BitmapRef) bitmaps.elementAt(i); 243 if( ref.bitmap != null ) dc.addDep( ref.bitmap ); 244 } 245 } 246 } 247 248 public void addBitmap( FlashDef def, int offset ) { 249 if( bitmaps == null ) bitmaps = new IVVector(); 250 bitmaps.addElement( new BitmapRef(def, offset) ); 251 } 252 253 protected FlashItem copyInto( FlashItem item, ScriptCopier copier ) { 254 super.copyInto( item, copier ); 255 ((LazyShape)item).data = (DataMarker) data.getCopy(); 256 ((LazyShape)item).tagCode = tagCode; 257 ((LazyShape)item).bounds = (Rectangle2D) bounds.clone(); 258 if( bitmaps != null ) { 259 IVVector v = new IVVector(bitmaps.size()); 260 for( int i=0; i<bitmaps.size(); i++ ) { 261 BitmapRef ref = (BitmapRef) bitmaps.elementAt(i); 262 v.addElement( new BitmapRef( copier.copy(ref.bitmap), ref.offset ) ); 263 } 264 ((LazyShape)item).bitmaps = v; 265 } 266 return item; 267 } 268 269 public FlashItem getCopy( ScriptCopier copier ) { 270 return copyInto( new LazyShape(), copier ); 271 } 272 } 273 | Popular Tags |