1 50 51 package org.openlaszlo.iv.flash.api.shape; 52 53 import java.io.PrintStream ; 54 import java.awt.geom.*; 55 import java.util.*; 56 57 import org.openlaszlo.iv.flash.util.*; 58 import org.openlaszlo.iv.flash.api.*; 59 import org.openlaszlo.iv.flash.parser.*; 60 61 68 public class ShapeRecords extends FlashItem { 69 70 private IVVector shape_records; 71 72 private int last_x = Integer.MAX_VALUE; 74 private int last_y = Integer.MAX_VALUE; 75 76 public ShapeRecords() { 77 this( new IVVector() ); 78 } 79 80 public ShapeRecords( IVVector shape_records ) { 81 this.shape_records = shape_records; 82 } 83 84 89 public IVVector getShapeRecords() { 90 return shape_records; 91 } 92 93 102 public void drawCurveTo( int cx, int cy, int ax, int ay ) { 103 shape_records.addElement( new CurvedEdgeRecord(cx-last_x, cy-last_y, ax-cx, ay-cy) ); 104 last_x = ax; 105 last_y = ay; 106 } 107 108 119 public void drawCurve( int ax1, int ay1, int cx, int cy, int ax2, int ay2 ) { 120 movePenTo(ax1, ay1); 121 drawCurveTo(cx, cy, ax2, ay2); 122 } 123 124 132 public void drawCurve( Point2D anchor1, Point2D control, Point2D anchor2 ) { 133 drawCurve( (int) anchor1.getX(), (int) anchor1.getY(), 134 (int) control.getX(), (int) control.getY(), 135 (int) anchor2.getX(), (int) anchor2.getY() ); 136 } 137 138 145 public void drawLineTo( int x, int y ) { 146 int deltaX = x-last_x; 147 int deltaY = y-last_y; 148 if( deltaX == 0 ) { 149 if( deltaY == 0 ) return; 150 shape_records.addElement( StrightEdgeRecord.newVLine(deltaY) ); 151 } else if( deltaY == 0 ) { 152 shape_records.addElement( StrightEdgeRecord.newHLine(deltaX) ); 153 } else { 154 shape_records.addElement( StrightEdgeRecord.newLine(deltaX,deltaY) ); 155 } 156 last_x = x; 157 last_y = y; 158 } 159 160 166 public void drawLineTo( Point2D p1 ) { 167 drawLineTo( (int) p1.getX(), (int) p1.getY() ); 168 } 169 170 180 public void drawLine( int x1, int y1, int x2, int y2 ) { 181 movePenTo( x1, y1 ); 182 drawLineTo( x2, y2 ); 183 } 184 185 193 public void drawLine( Point2D p0, Point2D p1 ) { 194 drawLine( (int) p0.getX(), (int) p0.getY(), (int) p1.getX(), (int) p1.getY() ); 195 } 196 197 207 public void drawRectangle( int x, int y, int width, int height ) { 208 movePenTo( x, y ); 209 drawLineTo( x+width, y ); 210 drawLineTo( x+width, y+height ); 211 drawLineTo( x, y+height ); 212 drawLineTo( x, y ); 213 } 214 215 222 public void drawRectangle( Rectangle2D r ) { 223 drawRectangle( (int) r.getX(), (int) r.getY(), (int) r.getWidth(), (int) r.getHeight() ); 224 } 225 226 233 public void movePenTo( int x, int y ) { 234 if( last_x != x || last_y != y ) { 235 StyleChangeRecord sc = getStyleChange(); 236 sc.addFlags( StyleChangeRecord.MOVETO ); 237 sc.setDeltaX(x); 238 sc.setDeltaY(y); 239 last_x = x; 240 last_y = y; 241 } 242 } 243 244 250 public void movePenTo( Point2D p ) { 251 movePenTo( (int) p.getX(), (int) p.getY() ); 252 } 253 254 259 public Point2D getCurrentPos() { 260 return new Point2D.Double(last_x, last_y); 261 } 262 263 268 public Point2D getFirstPos() { 269 for( int i=0; i<shape_records.size(); i++ ) { 270 FlashItem item = (FlashItem) shape_records.elementAt(i); 271 if( item instanceof StyleChangeRecord ) { 272 StyleChangeRecord sr = (StyleChangeRecord) item; 273 if( (sr.getFlags()&StyleChangeRecord.MOVETO) == 0 ) continue; 274 return new Point2D.Double(sr.getDeltaX(), sr.getDeltaY()); 275 } 276 277 } 278 return new Point2D.Double(0,0); 279 } 280 281 288 public void drawAWTShape( java.awt.Shape shape ) { 289 PathIterator pi = shape.getPathIterator(null); 290 drawAWTPathIterator(pi); 291 } 292 293 300 public void drawAWTShape( java.awt.Shape shape, AffineTransform matrix ) { 301 PathIterator pi = shape.getPathIterator(matrix); 302 drawAWTPathIterator(pi); 303 } 304 305 312 public void drawAWTPathIterator( java.awt.geom.PathIterator pi ) { 313 double[] coords = new double[6]; 314 315 int last_move_x = 0; 316 int last_move_y = 0; 317 318 while( !pi.isDone() ) { 319 int type = pi.currentSegment( coords ); 320 switch( type ) { 321 case PathIterator.SEG_MOVETO: { 322 last_move_x = (int) coords[0]; 323 last_move_y = (int) coords[1]; 324 movePenTo( last_move_x, last_move_y ); 325 break; 326 } 327 case PathIterator.SEG_CLOSE: { 328 drawLineTo( last_move_x, last_move_y ); 329 break; 330 } 331 case PathIterator.SEG_LINETO: { 332 int x = (int) coords[0]; 333 int y = (int) coords[1]; 334 drawLineTo( x, y ); 335 break; 336 } 337 case PathIterator.SEG_QUADTO: { 338 int cx = (int) coords[0]; 339 int cy = (int) coords[1]; 340 int ax = (int) coords[2]; 341 int ay = (int) coords[3]; 342 drawCurveTo( cx, cy, ax, ay ); 343 break; 344 } 345 case PathIterator.SEG_CUBICTO: { 346 double c1x = coords[0]; 347 double c1y = coords[1]; 348 double c2x = coords[2]; 349 double c2y = coords[3]; 350 double ax = coords[4]; 351 double ay = coords[5]; 352 353 Point2D p0 = new Point2D.Double(last_x, last_y); 355 Point2D p1 = new Point2D.Double(c1x, c1y); 356 Point2D p2 = new Point2D.Double(c2x, c2y); 357 Point2D p3 = new Point2D.Double(ax, ay); 358 359 Point2D[] quad_curves = GeomHelper.CubicToQudratricBezier(p0, p1, p2, p3); 361 362 for( int i=0; i<quad_curves.length; i+=3 ) { 364 Point2D anchor1 = quad_curves[i]; 365 Point2D control = quad_curves[i+1]; 366 Point2D anchor2 = quad_curves[i+2]; 367 drawCurve(anchor1, control, anchor2); 368 } 369 break; 370 } 371 } 372 pi.next(); 373 } 374 } 375 376 381 public StyleChangeRecord addStyleChangeRecord( StyleChangeRecord scr ) { 382 shape_records.addElement(scr); 383 return scr; 384 } 385 386 389 public StyleChangeRecord addStyleChangeRecord() { 390 StyleChangeRecord scr = new StyleChangeRecord(); 391 shape_records.addElement(scr); 392 return scr; 393 } 394 395 protected StyleChangeRecord getStyleChange() { 396 if( shape_records.size() > 0 ) { 397 FlashItem item = (FlashItem) shape_records.elementAt( shape_records.size()-1 ); 398 if( item instanceof StyleChangeRecord ) return (StyleChangeRecord) item; 399 } 400 return addStyleChangeRecord(); 401 } 402 403 public static ShapeRecords parse( Parser p ) { 404 return new ShapeRecords(parseShapeRecords(p)); 405 } 406 407 public static IVVector parseShapeRecords( Parser p ) { 408 IVVector shape_records = new IVVector(); 409 410 int nBits = p.getUByte(); 412 int nFillBits = (nBits&0xf0)>>4; 413 int nLineBits = nBits&0x0f; 414 p.initBits(); 415 for(;;) { 416 if( p.getBool() ) { if( p.getBool() ) { int nb = p.getBits(4)+2; 419 if( p.getBool() ) { int deltaX = p.getSBits(nb); 421 int deltaY = p.getSBits(nb); 422 shape_records.addElement( StrightEdgeRecord.newLine(deltaX, deltaY) ); 423 } else if( p.getBool() ) { int deltaY = p.getSBits(nb); 425 shape_records.addElement( StrightEdgeRecord.newVLine(deltaY) ); 426 } else { int deltaX = p.getSBits(nb); 428 shape_records.addElement( StrightEdgeRecord.newHLine(deltaX) ); 429 } 430 } else { int nb = p.getBits(4)+2; 432 int cx = p.getSBits(nb); 433 int cy = p.getSBits(nb); 434 int ax = p.getSBits(nb); 435 int ay = p.getSBits(nb); 436 shape_records.addElement( new CurvedEdgeRecord(cx, cy, ax, ay) ); 437 } 438 } else { int flags = p.getBits(5); 440 if( flags == 0 ) break; StyleChangeRecord scr = new StyleChangeRecord(); 442 scr.setFlags( flags ); 443 444 if( (flags & StyleChangeRecord.MOVETO) != 0 ) { 445 int nMoveBits = p.getBits(5); 446 scr.setDeltaX( p.getSBits(nMoveBits) ); 447 scr.setDeltaY( p.getSBits(nMoveBits) ); 448 } 449 450 if( (flags & StyleChangeRecord.FILLSTYLE0) != 0 ) { 451 scr.setFillStyle0( p.getBits(nFillBits) ); 452 } 453 454 if( (flags & StyleChangeRecord.FILLSTYLE1) != 0 ) { 455 scr.setFillStyle1( p.getBits(nFillBits) ); 456 } 457 458 if( (flags & StyleChangeRecord.LINESTYLE) != 0 ) { 459 scr.setLineStyle( p.getBits(nLineBits) ); 460 } 461 462 shape_records.addElement( scr ); 463 464 if( (flags&0x80) != 0 ) { 465 break; 466 } 467 } 468 } 469 470 return shape_records; 471 } 472 473 479 public void write( FlashOutput fob ) { 480 fob.initBits(); 481 shape_records.write(fob); 482 fob.writeBits(0, 6); 483 fob.flushBits(); 484 } 485 486 494 public void write( FlashOutput fob, int nFillBits, int nLineBits ) { 495 fob.initBits(); 496 for( int i=0; i<shape_records.size(); i++ ) { 497 FlashItem item = (FlashItem) shape_records.elementAt(i); 498 if( item instanceof StyleChangeRecord ) { 499 StyleChangeRecord scr = (StyleChangeRecord) item; 500 scr.write(fob, nFillBits, nLineBits); 501 } else { 502 item.write(fob); 503 } 504 } 505 } 506 507 public void printContent( PrintStream out, String indent ) { 508 out.println( indent+"ShapeRecords:" ); 509 shape_records.printContent(out, indent+" "); 510 } 511 512 protected FlashItem copyInto( FlashItem item, ScriptCopier copier ) { 513 super.copyInto( item, copier ); 514 ((ShapeRecords)item).shape_records = shape_records.getCopy(copier); 515 return item; 516 } 517 518 public FlashItem getCopy( ScriptCopier copier ) { 519 return copyInto( new ShapeRecords(null), copier ); 520 } 521 } 522 523 | Popular Tags |