/**
   * Create a new ShapeGroup and create an instance of <code>EscherSpgrContainer</code> which
   * represents a group of shapes
   */
  protected EscherContainerRecord createSpContainer(boolean isChild) {
    EscherContainerRecord spgr = new EscherContainerRecord();
    spgr.setRecordId(EscherContainerRecord.SPGR_CONTAINER);
    spgr.setOptions((short) 15);

    // The group itself is a shape, and always appears as the first EscherSpContainer in the group
    // container.
    EscherContainerRecord spcont = new EscherContainerRecord();
    spcont.setRecordId(EscherContainerRecord.SP_CONTAINER);
    spcont.setOptions((short) 15);

    EscherSpgrRecord spg = new EscherSpgrRecord();
    spg.setOptions((short) 1);
    spcont.addChildRecord(spg);

    EscherSpRecord sp = new EscherSpRecord();
    short type = (ShapeTypes.NotPrimitive << 4) + 2;
    sp.setOptions(type);
    sp.setFlags(EscherSpRecord.FLAG_HAVEANCHOR | EscherSpRecord.FLAG_GROUP);
    spcont.addChildRecord(sp);

    EscherClientAnchorRecord anchor = new EscherClientAnchorRecord();
    spcont.addChildRecord(anchor);

    spgr.addChildRecord(spcont);
    return spgr;
  }
  public void testHSSFShapeCompatibility() {
    HSSFSimpleShape shape = new HSSFSimpleShape(null, new HSSFClientAnchor());
    shape.setShapeType(HSSFSimpleShape.OBJECT_TYPE_LINE);
    assertEquals(0x08000040, shape.getLineStyleColor());
    assertEquals(0x08000009, shape.getFillColor());
    assertEquals(HSSFShape.LINEWIDTH_DEFAULT, shape.getLineWidth());
    assertEquals(HSSFShape.LINESTYLE_SOLID, shape.getLineStyle());
    assertFalse(shape.isNoFill());

    AbstractShape sp = AbstractShape.createShape(shape, 1);
    EscherContainerRecord spContainer = sp.getSpContainer();
    EscherOptRecord opt = spContainer.getChildById(EscherOptRecord.RECORD_ID);

    assertEquals(7, opt.getEscherProperties().size());
    assertEquals(
        true,
        ((EscherBoolProperty) opt.lookup(EscherProperties.TEXT__SIZE_TEXT_TO_FIT_SHAPE)).isTrue());
    assertEquals(
        0x00000004,
        ((EscherSimpleProperty) opt.lookup(EscherProperties.GEOMETRY__SHAPEPATH))
            .getPropertyValue());
    assertEquals(
        0x08000009,
        ((EscherSimpleProperty) opt.lookup(EscherProperties.FILL__FILLCOLOR)).getPropertyValue());
    assertEquals(
        true, ((EscherBoolProperty) opt.lookup(EscherProperties.FILL__NOFILLHITTEST)).isTrue());
    assertEquals(
        0x08000040,
        ((EscherSimpleProperty) opt.lookup(EscherProperties.LINESTYLE__COLOR)).getPropertyValue());
    assertEquals(
        true,
        ((EscherBoolProperty) opt.lookup(EscherProperties.LINESTYLE__NOLINEDRAWDASH)).isTrue());
    assertEquals(
        true, ((EscherBoolProperty) opt.lookup(EscherProperties.GROUPSHAPE__PRINT)).isTrue());
  }
  public void testShapeIds() {
    HSSFWorkbook wb = new HSSFWorkbook();
    HSSFSheet sheet1 = wb.createSheet();
    HSSFPatriarch patriarch1 = sheet1.createDrawingPatriarch();
    for (int i = 0; i < 2; i++) {
      patriarch1.createSimpleShape(new HSSFClientAnchor());
    }

    wb = HSSFTestDataSamples.writeOutAndReadBack(wb);
    sheet1 = wb.getSheetAt(0);
    patriarch1 = sheet1.getDrawingPatriarch();

    EscherAggregate agg1 = HSSFTestHelper.getEscherAggregate(patriarch1);
    // last shape ID cached in EscherDgRecord
    EscherDgRecord dg1 = agg1.getEscherContainer().getChildById(EscherDgRecord.RECORD_ID);
    assertEquals(1026, dg1.getLastMSOSPID());

    // iterate over shapes and check shapeId
    EscherContainerRecord spgrContainer = agg1.getEscherContainer().getChildContainers().get(0);
    // root spContainer + 2 spContainers for shapes
    assertEquals(3, spgrContainer.getChildRecords().size());

    EscherSpRecord sp0 =
        ((EscherContainerRecord) spgrContainer.getChild(0)).getChildById(EscherSpRecord.RECORD_ID);
    assertEquals(1024, sp0.getShapeId());

    EscherSpRecord sp1 =
        ((EscherContainerRecord) spgrContainer.getChild(1)).getChildById(EscherSpRecord.RECORD_ID);
    assertEquals(1025, sp1.getShapeId());

    EscherSpRecord sp2 =
        ((EscherContainerRecord) spgrContainer.getChild(2)).getChildById(EscherSpRecord.RECORD_ID);
    assertEquals(1026, sp2.getShapeId());
  }
  /** No NullPointerException should appear */
  public void testDefaultSettingsWithEmptyContainer() {
    EscherContainerRecord container = new EscherContainerRecord();
    EscherOptRecord opt = new EscherOptRecord();
    opt.setRecordId(EscherOptRecord.RECORD_ID);
    container.addChildRecord(opt);
    ObjRecord obj = new ObjRecord();
    CommonObjectDataSubRecord cod = new CommonObjectDataSubRecord();
    cod.setObjectType(HSSFSimpleShape.OBJECT_TYPE_PICTURE);
    obj.addSubRecord(cod);
    HSSFPicture picture = new HSSFPicture(container, obj);

    assertEquals(picture.getLineWidth(), HSSFShape.LINEWIDTH_DEFAULT);
    assertEquals(picture.getFillColor(), HSSFShape.FILL__FILLCOLOR_DEFAULT);
    assertEquals(picture.getLineStyle(), HSSFShape.LINESTYLE_DEFAULT);
    assertEquals(picture.getLineStyleColor(), HSSFShape.LINESTYLE__COLOR_DEFAULT);
    assertEquals(picture.isNoFill(), HSSFShape.NO_FILL_DEFAULT);
    assertEquals(picture.getPictureIndex(), -1); // not set yet
  }
Exemple #5
0
  @Override
  public HSLFPictureData addPicture(byte[] data, PictureType format) throws IOException {
    if (format == null || format.nativeId == -1) {
      throw new IllegalArgumentException("Unsupported picture format: " + format);
    }

    byte[] uid = HSLFPictureData.getChecksum(data);

    for (HSLFPictureData pd : getPictureData()) {
      if (Arrays.equals(pd.getUID(), uid)) {
        return pd;
      }
    }

    EscherContainerRecord bstore;

    EscherContainerRecord dggContainer = _documentRecord.getPPDrawingGroup().getDggContainer();
    bstore =
        (EscherContainerRecord)
            HSLFShape.getEscherChild(dggContainer, EscherContainerRecord.BSTORE_CONTAINER);
    if (bstore == null) {
      bstore = new EscherContainerRecord();
      bstore.setRecordId(EscherContainerRecord.BSTORE_CONTAINER);

      dggContainer.addChildBefore(bstore, EscherOptRecord.RECORD_ID);
    }

    HSLFPictureData pict = HSLFPictureData.create(format);
    pict.setData(data);

    int offset = _hslfSlideShow.addPicture(pict);

    EscherBSERecord bse = new EscherBSERecord();
    bse.setRecordId(EscherBSERecord.RECORD_ID);
    bse.setOptions((short) (0x0002 | (format.nativeId << 4)));
    bse.setSize(pict.getRawData().length + 8);
    bse.setUid(uid);

    bse.setBlipTypeMacOS((byte) format.nativeId);
    bse.setBlipTypeWin32((byte) format.nativeId);

    if (format == PictureType.EMF) {
      bse.setBlipTypeMacOS((byte) PictureType.PICT.nativeId);
    } else if (format == PictureType.WMF) {
      bse.setBlipTypeMacOS((byte) PictureType.PICT.nativeId);
    } else if (format == PictureType.PICT) {
      bse.setBlipTypeWin32((byte) PictureType.WMF.nativeId);
    }

    bse.setRef(0);
    bse.setOffset(offset);
    bse.setRemainingData(new byte[0]);

    bstore.addChildRecord(bse);
    int count = bstore.getChildRecords().size();
    bstore.setOptions((short) ((count << 4) | 0xF));

    return pict;
  }
 /**
  * Return type of the shape. In most cases shape group type is {@link
  * org.apache.poi.hslf.model.ShapeTypes#NotPrimitive}
  *
  * @return type of the shape.
  */
 public int getShapeType() {
   EscherContainerRecord groupInfoContainer = (EscherContainerRecord) _escherContainer.getChild(0);
   EscherSpRecord spRecord = groupInfoContainer.getChildById(EscherSpRecord.RECORD_ID);
   return spRecord.getOptions() >> 4;
 }
  public void testCorrectOrderInOptRecord() throws IOException {
    HSSFWorkbook wb = new HSSFWorkbook();

    try {
      HSSFSheet sheet = wb.createSheet();
      HSSFPatriarch patriarch = sheet.createDrawingPatriarch();

      HSSFTextbox textbox = patriarch.createTextbox(new HSSFClientAnchor());
      EscherOptRecord opt = HSSFTestHelper.getOptRecord(textbox);

      String opt1Str = opt.toXml();

      textbox.setFillColor(textbox.getFillColor());
      EscherContainerRecord container = HSSFTestHelper.getEscherContainer(textbox);
      EscherOptRecord optRecord = container.getChildById(EscherOptRecord.RECORD_ID);
      assertEquals(opt1Str, optRecord.toXml());
      textbox.setLineStyle(textbox.getLineStyle());
      assertEquals(opt1Str, optRecord.toXml());
      textbox.setLineWidth(textbox.getLineWidth());
      assertEquals(opt1Str, optRecord.toXml());
      textbox.setLineStyleColor(textbox.getLineStyleColor());
      assertEquals(opt1Str, optRecord.toXml());
    } finally {
      wb.close();
    }
  }