1 19 20 package jxl.biff.drawing; 21 22 import java.io.IOException ; 23 import java.io.FileInputStream ; 24 25 import common.Assert; 26 import common.Logger; 27 28 import jxl.Image; 29 import jxl.WorkbookSettings; 30 import jxl.biff.ByteData; 31 import jxl.biff.IntegerHelper; 32 import jxl.biff.IndexMapping; 33 import jxl.biff.Type; 34 import jxl.write.biff.File; 35 import jxl.biff.drawing.MsoDrawingRecord; 36 import jxl.biff.drawing.ObjRecord; 37 38 42 public class Drawing implements DrawingGroupObject, Image 43 { 44 47 private static Logger logger = Logger.getLogger(Drawing.class); 48 49 52 private EscherContainer readSpContainer; 53 54 57 private MsoDrawingRecord msoDrawingRecord; 58 59 62 private ObjRecord objRecord; 63 64 67 private boolean initialized = false; 68 69 72 private java.io.File imageFile; 73 74 77 private byte[] imageData; 78 79 82 private int objectId; 83 84 87 private int blipId; 88 89 92 private double x; 93 94 97 private double y; 98 99 102 private double width; 103 104 107 private double height; 108 109 112 private int referenceCount; 113 114 117 private EscherContainer escherData; 118 119 122 private Origin origin; 123 124 127 private DrawingGroup drawingGroup; 128 129 132 private DrawingData drawingData; 133 134 137 private ShapeType type; 138 139 142 private int shapeId; 143 144 147 private int drawingNumber; 148 149 150 158 public Drawing(MsoDrawingRecord mso, ObjRecord obj, 159 DrawingData dd, 160 DrawingGroup dg) 161 { 162 drawingGroup = dg; 163 msoDrawingRecord = mso; 164 drawingData = dd; 165 objRecord = obj; 166 initialized = false; 167 origin = Origin.READ; 168 drawingData.addData(msoDrawingRecord.getData()); 169 drawingNumber = drawingData.getNumDrawings() - 1; 170 drawingGroup.addDrawing(this); 171 172 Assert.verify(mso != null && obj != null); 173 174 initialize(); 175 } 176 177 182 protected Drawing(DrawingGroupObject dgo, DrawingGroup dg) 183 { 184 Drawing d = (Drawing) dgo; 185 Assert.verify(d.origin == Origin.READ); 186 msoDrawingRecord = d.msoDrawingRecord; 187 objRecord = d.objRecord; 188 initialized = false; 189 origin = Origin.READ; 190 drawingData = d.drawingData; 191 drawingGroup = dg; 192 drawingNumber = d.drawingNumber; 193 drawingGroup.addDrawing(this); 194 } 195 196 205 public Drawing(double x, double y, double width, double height, 206 java.io.File image) 207 { 208 imageFile = image; 209 initialized = true; 210 origin = Origin.WRITE; 211 this.x = x; 212 this.y = y; 213 this.width = width; 214 this.height = height; 215 referenceCount = 1; 216 type = ShapeType.PICTURE_FRAME; 217 } 218 219 228 public Drawing(double x, double y, double width, double height, 229 byte[] image) 230 { 231 imageData = image; 232 initialized = true; 233 origin = Origin.WRITE; 234 this.x = x; 235 this.y = y; 236 this.width = width; 237 this.height = height; 238 referenceCount = 1; 239 type = ShapeType.PICTURE_FRAME; 240 } 241 242 245 private void initialize() 246 { 247 readSpContainer = drawingData.getSpContainer(drawingNumber); 248 Assert.verify(readSpContainer != null); 249 250 EscherRecord[] children = readSpContainer.getChildren(); 251 252 Sp sp = (Sp) readSpContainer.getChildren()[0]; 253 shapeId = sp.getShapeId(); 254 objectId = objRecord.getObjectId(); 255 type = ShapeType.getType(sp.getShapeType()); 256 257 if (type == ShapeType.UNKNOWN) 258 { 259 logger.warn("Unknown shape type"); 260 } 261 262 Opt opt = (Opt) readSpContainer.getChildren()[1]; 263 264 if (opt.getProperty(260) != null) 265 { 266 blipId = opt.getProperty(260).value; 267 } 268 269 if (opt.getProperty(261) != null) 270 { 271 imageFile = new java.io.File (opt.getProperty(261).stringValue); 272 } 273 else 274 { 275 if (type == ShapeType.PICTURE_FRAME) 276 { 277 logger.warn("no filename property for drawing"); 278 imageFile = new java.io.File (Integer.toString(blipId)); 279 } 280 } 281 282 ClientAnchor clientAnchor = null; 283 for (int i = 0 ; i < children.length && clientAnchor == null ; i++) 284 { 285 if (children[i].getType() == EscherRecordType.CLIENT_ANCHOR) 286 { 287 clientAnchor = (ClientAnchor) children[i]; 288 } 289 } 290 291 if (clientAnchor == null) 292 { 293 logger.warn("client anchor not found"); 294 } 295 else 296 { 297 x = clientAnchor.getX1(); 298 y = clientAnchor.getY1(); 299 width = clientAnchor.getX2() - x; 300 height = clientAnchor.getY2() - y; 301 } 302 303 if (blipId == 0) 304 { 305 logger.warn("linked drawings are not supported"); 306 } 307 308 initialized = true; 309 } 310 311 316 public java.io.File getImageFile() 317 { 318 return imageFile; 319 } 320 321 328 public String getImageFilePath() 329 { 330 if (imageFile == null) 331 { 332 return blipId != 0 ? Integer.toString(blipId) : "__new__image__"; 334 } 335 336 return imageFile.getPath(); 337 } 338 339 347 public final void setObjectId(int objid, int bip, int sid) 348 { 349 objectId = objid; 350 blipId = bip; 351 shapeId = sid; 352 353 if (origin == Origin.READ) 354 { 355 origin = Origin.READ_WRITE; 356 } 357 } 358 359 364 public final int getObjectId() 365 { 366 if (!initialized) 367 { 368 initialize(); 369 } 370 371 return objectId; 372 } 373 374 379 public int getShapeId() 380 { 381 if (!initialized) 382 { 383 initialize(); 384 } 385 386 return shapeId; 387 } 388 389 394 public final int getBlipId() 395 { 396 if (!initialized) 397 { 398 initialize(); 399 } 400 401 return blipId; 402 } 403 404 409 public MsoDrawingRecord getMsoDrawingRecord() 410 { 411 return msoDrawingRecord; 412 } 413 414 419 public EscherContainer getSpContainer() 420 { 421 if (!initialized) 422 { 423 initialize(); 424 } 425 426 if (origin == Origin.READ) 427 { 428 return getReadSpContainer(); 429 } 430 431 SpContainer spContainer = new SpContainer(); 432 Sp sp = new Sp(type, shapeId, 2560); 433 spContainer.add(sp); 434 Opt opt = new Opt(); 435 opt.addProperty(260, true, false, blipId); 436 437 if (type == ShapeType.PICTURE_FRAME) 438 { 439 String filePath = imageFile != null ? imageFile.getPath(): ""; 440 opt.addProperty(261, true, true, filePath.length() * 2, filePath); 441 opt.addProperty(447, false, false, 65536); 442 opt.addProperty(959, false, false, 524288); 443 spContainer.add(opt); 444 } 445 446 ClientAnchor clientAnchor = new ClientAnchor(x, y, x + width, y + height); 447 spContainer.add(clientAnchor); 448 ClientData clientData = new ClientData(); 449 spContainer.add(clientData); 450 451 return spContainer; 452 } 453 454 460 public void setDrawingGroup(DrawingGroup dg) 461 { 462 drawingGroup = dg; 463 } 464 465 470 public DrawingGroup getDrawingGroup() 471 { 472 return drawingGroup; 473 } 474 475 480 public Origin getOrigin() 481 { 482 return origin; 483 } 484 485 490 public int getReferenceCount() 491 { 492 return referenceCount; 493 } 494 495 500 public void setReferenceCount(int r) 501 { 502 referenceCount = r; 503 } 504 505 510 public double getX() 511 { 512 if (!initialized) 513 { 514 initialize(); 515 } 516 return x; 517 } 518 519 524 public void setX(double x) 525 { 526 if (origin == Origin.READ) 527 { 528 if (!initialized) 529 { 530 initialize(); 531 } 532 origin = Origin.READ_WRITE; 533 } 534 535 this.x = x; 536 } 537 538 543 public double getY() 544 { 545 if (!initialized) 546 { 547 initialize(); 548 } 549 550 return y; 551 } 552 553 558 public void setY(double y) 559 { 560 if (origin == Origin.READ) 561 { 562 if (!initialized) 563 { 564 initialize(); 565 } 566 origin = Origin.READ_WRITE; 567 } 568 569 this.y = y; 570 } 571 572 573 578 public double getWidth() 579 { 580 if (!initialized) 581 { 582 initialize(); 583 } 584 585 return width; 586 } 587 588 593 public void setWidth(double w) 594 { 595 if (origin == Origin.READ) 596 { 597 if (!initialized) 598 { 599 initialize(); 600 } 601 origin = Origin.READ_WRITE; 602 } 603 604 width = w; 605 } 606 607 612 public double getHeight() 613 { 614 if (!initialized) 615 { 616 initialize(); 617 } 618 619 return height; 620 } 621 622 627 public void setHeight(double h) 628 { 629 if (origin == Origin.READ) 630 { 631 if (!initialized) 632 { 633 initialize(); 634 } 635 origin = Origin.READ_WRITE; 636 } 637 638 height = h; 639 } 640 641 642 647 private EscherContainer getReadSpContainer() 648 { 649 if (!initialized) 650 { 651 initialize(); 652 } 653 654 return readSpContainer; 655 } 656 657 662 public byte[] getImageData() 663 { 664 Assert.verify(origin == Origin.READ || origin == Origin.READ_WRITE); 665 666 if (!initialized) 667 { 668 initialize(); 669 } 670 671 return drawingGroup.getImageData(blipId); 672 } 673 674 679 public byte[] getImageBytes() throws IOException 680 { 681 if (origin == Origin.READ || origin == Origin.READ_WRITE) 682 { 683 return getImageData(); 684 } 685 686 Assert.verify(origin == Origin.WRITE); 687 688 if (imageFile == null) 689 { 690 Assert.verify(imageData != null); 691 return imageData; 692 } 693 694 byte[] data = new byte[(int) imageFile.length()]; 695 FileInputStream fis = new FileInputStream (imageFile); 696 fis.read(data, 0, data.length); 697 fis.close(); 698 return data; 699 } 700 701 706 public ShapeType getType() 707 { 708 return type; 709 } 710 711 714 public void writeAdditionalRecords(File outputFile) throws IOException 715 { 716 if (origin == Origin.READ) 717 { 718 outputFile.write(objRecord); 719 return; 720 } 721 722 ObjRecord objRecord = new ObjRecord(objectId, 724 ObjRecord.PICTURE); 725 outputFile.write(objRecord); 726 } 727 728 733 public void writeTailRecords(File outputFile) throws IOException 734 { 735 } 737 738 743 public double getColumn() 744 { 745 return getX(); 746 } 747 748 753 public double getRow() 754 { 755 return getY(); 756 } 757 758 765 public boolean isFirst() 766 { 767 return msoDrawingRecord.isFirst(); 768 } 769 770 777 public boolean isFormObject() 778 { 779 return false; 780 } 781 782 } 783 784 785 786 | Popular Tags |