1 50 51 package org.openlaszlo.iv.flash.api.shape; 52 53 import java.awt.geom.*; 54 import java.io.PrintStream ; 55 import org.openlaszlo.iv.flash.util.*; 56 import org.openlaszlo.iv.flash.api.*; 57 import org.openlaszlo.iv.flash.parser.*; 58 import org.openlaszlo.iv.flash.api.image.*; 59 60 79 public final class FillStyle extends FlashItem { 80 81 public static final int SOLID = 0x00; 82 public static final int LINEAR_GRADIENT = 0x10; 83 public static final int RADIAL_GRADIENT = 0x12; 84 public static final int TILED_BITMAP = 0x40; 85 public static final int CLIPPED_BITMAP = 0x41; 86 87 private int type; private Color color; private AffineTransform matrix; private Gradient gradient; private Bitmap bitmap; 93 public FillStyle() {} 94 95 public int getType() { 96 return type; 97 } 98 99 public void setType( int type ) { 100 this.type = type; 101 } 102 103 public Color getColor() { 104 return color; 105 } 106 107 public void setColor( Color color ) { 108 this.color = color; 109 } 110 111 public AffineTransform getMatrix() { 112 return matrix; 113 } 114 115 public void setMatrix( AffineTransform matrix ) { 116 this.matrix = matrix; 117 } 118 119 public Gradient getGraduent() { 120 return gradient; 121 } 122 123 public void setGradient( Gradient gradient ) { 124 this.gradient = gradient; 125 } 126 127 public Bitmap getBitmap() { 128 return bitmap; 129 } 130 131 public void setBitmap( Bitmap bitmap ) { 132 this.bitmap = bitmap; 133 } 134 135 141 public static FillStyle newSolid( Color color ) { 142 FillStyle fs = new FillStyle(); 143 fs.setColor( color ); 144 fs.setType( SOLID ); 145 return fs; 146 } 147 148 155 public static FillStyle newLinearGradient( Gradient gradient, AffineTransform matrix ) { 156 FillStyle fs = new FillStyle(); 157 fs.setGradient( gradient ); 158 fs.setMatrix( matrix ); 159 fs.setType( LINEAR_GRADIENT ); 160 return fs; 161 } 162 163 170 public static FillStyle newRadialGradient( Gradient gradient, AffineTransform matrix ) { 171 FillStyle fs = new FillStyle(); 172 fs.setGradient( gradient ); 173 fs.setMatrix( matrix ); 174 fs.setType( RADIAL_GRADIENT ); 175 return fs; 176 } 177 178 185 public static FillStyle newTiledBitmap( Bitmap bitmap, AffineTransform matrix ) { 186 FillStyle fs = new FillStyle(); 187 fs.setBitmap( bitmap ); 188 fs.setMatrix( matrix ); 189 fs.setType( TILED_BITMAP ); 190 return fs; 191 } 192 193 200 public static FillStyle newClippedBitmap( Bitmap bitmap, AffineTransform matrix ) { 201 FillStyle fs = new FillStyle(); 202 fs.setBitmap( bitmap ); 203 fs.setMatrix( matrix ); 204 fs.setType( CLIPPED_BITMAP ); 205 return fs; 206 } 207 208 214 public static FillStyle newTiledBitmap( Bitmap bitmap ) { 215 AffineTransform matrix = AffineTransform.getScaleInstance(20.0, 20.0); 216 return newTiledBitmap( bitmap, matrix ); 217 } 218 219 225 public static FillStyle newClippedBitmap( Bitmap bitmap ) { 226 AffineTransform matrix = AffineTransform.getScaleInstance(20.0, 20.0); 227 return newClippedBitmap( bitmap, matrix ); 228 } 229 230 public static FillStyle parse( Parser p, boolean withAlpha ) { 231 FillStyle fs = new FillStyle(); 232 int type = fs.type = p.getUByte(); 233 234 if( (type&0x10) != 0 ) { 236 fs.matrix = p.getMatrix(); 238 fs.gradient = Gradient.parse(p, withAlpha); 240 241 } else if( (type&0x40) != 0 ) { 243 int id = p.getUWord(); fs.bitmap = (Bitmap) p.getDef(id); 245 fs.matrix = p.getMatrix(); 246 247 } else { 249 fs.color = Color.parse(p, withAlpha); 250 } 251 252 return fs; 253 } 254 255 public void write( FlashOutput fob ) { 256 fob.writeByte(type); 257 switch( type ) { 258 case SOLID: 259 Shape shape = (Shape) fob.getUserData(); 260 if( shape.isWithAlpha() ) { 261 color.writeRGBA(fob); 262 } else { 263 color.writeRGB(fob); 264 } 265 break; 266 case LINEAR_GRADIENT: 267 case RADIAL_GRADIENT: 268 fob.write(matrix); 269 gradient.write(fob); 270 break; 271 case TILED_BITMAP: 272 case CLIPPED_BITMAP: 273 if( bitmap == null ) { 274 fob.writeWord(0xffff); 275 } else { 276 fob.writeDefID(bitmap); 277 } 278 fob.write(matrix); 279 break; 280 } 281 } 282 283 public void printContent( PrintStream out, String indent ) { 284 switch( type ) { 285 case SOLID: 286 out.println( indent+"FillStyle (SOLID):" ); 287 color.printContent(out, indent+" "); 288 break; 289 case LINEAR_GRADIENT: 290 out.println( indent+"FillStyle (LINEAR_GRADIENT):" ); 291 out.println( indent+" "+matrix ); 292 gradient.printContent(out, indent+" "); 293 break; 294 case RADIAL_GRADIENT: 295 out.println( indent+"FillStyle (RADIAL_GRADIENT):" ); 296 out.println( indent+" "+matrix ); 297 gradient.printContent(out, indent+" "); 298 break; 299 case TILED_BITMAP: 300 out.println( indent+"FillStyle (TILED_BITMAP):" ); 301 out.println( indent+" "+matrix ); 302 out.println( indent+" bitmapID="+ (bitmap == null ? 0xffff : bitmap.getID()) ); 303 break; 304 case CLIPPED_BITMAP: 305 out.println( indent+"FillStyle (CLIPPED_BITMAP):" ); 306 out.println( indent+" "+matrix ); 307 out.println( indent+" bitmapID="+ (bitmap == null ? 0xffff : bitmap.getID()) ); 308 break; 309 } 310 } 311 312 protected FlashItem copyInto( FlashItem item, ScriptCopier copier ) { 313 ((FillStyle)item).type = type; 314 switch( type ) { 315 case SOLID: 316 ((FillStyle)item).color = (Color) color.getCopy(copier); 317 break; 318 case LINEAR_GRADIENT: 319 case RADIAL_GRADIENT: 320 ((FillStyle)item).gradient = (Gradient) gradient.getCopy(copier); 321 ((FillStyle)item).matrix = (AffineTransform) matrix.clone(); 322 break; 323 case TILED_BITMAP: 324 case CLIPPED_BITMAP: 325 ((FillStyle)item).bitmap = (Bitmap) copier.copy(bitmap); 326 ((FillStyle)item).matrix = (AffineTransform) matrix.clone(); 327 break; 328 } 329 return item; 330 } 331 332 public FlashItem getCopy( ScriptCopier copier ) { 333 return copyInto( new FillStyle(), copier ); 334 } 335 } 336 | Popular Tags |