use of org.apache.poi.ddf.EscherDgRecord in project poi by apache.
the class InternalWorkbook method cloneDrawings.
/**
* Check if the cloned sheet has drawings. If yes, then allocate a new drawing group ID and
* re-generate shape IDs
*
* @param sheet the cloned sheet
*/
public void cloneDrawings(InternalSheet sheet) {
findDrawingGroup();
if (drawingManager == null) {
//this workbook does not have drawings
return;
}
//check if the cloned sheet has drawings
int aggLoc = sheet.aggregateDrawingRecords(drawingManager, false);
if (aggLoc == -1) {
return;
}
EscherAggregate agg = (EscherAggregate) sheet.findFirstRecordBySid(EscherAggregate.sid);
EscherContainerRecord escherContainer = agg.getEscherContainer();
if (escherContainer == null) {
return;
}
EscherDggRecord dgg = drawingManager.getDgg();
//register a new drawing group for the cloned sheet
int dgId = drawingManager.findNewDrawingGroupId();
dgg.addCluster(dgId, 0);
dgg.setDrawingsSaved(dgg.getDrawingsSaved() + 1);
EscherDgRecord dg = null;
for (EscherRecord er : escherContainer) {
if (er instanceof EscherDgRecord) {
dg = (EscherDgRecord) er;
//update id of the drawing in the cloned sheet
dg.setOptions((short) (dgId << 4));
} else if (er instanceof EscherContainerRecord) {
// iterate over shapes and re-generate shapeId
for (EscherRecord er2 : (EscherContainerRecord) er) {
for (EscherRecord shapeChildRecord : (EscherContainerRecord) er2) {
int recordId = shapeChildRecord.getRecordId();
if (recordId == EscherSpRecord.RECORD_ID) {
if (dg == null) {
throw new RecordFormatException("EscherDgRecord wasn't set/processed before.");
}
EscherSpRecord sp = (EscherSpRecord) shapeChildRecord;
int shapeId = drawingManager.allocateShapeId((short) dgId, dg);
//allocateShapeId increments the number of shapes. roll back to the previous value
dg.setNumShapes(dg.getNumShapes() - 1);
sp.setShapeId(shapeId);
} else if (recordId == EscherOptRecord.RECORD_ID) {
EscherOptRecord opt = (EscherOptRecord) shapeChildRecord;
EscherSimpleProperty prop = (EscherSimpleProperty) opt.lookup(EscherProperties.BLIP__BLIPTODISPLAY);
if (prop != null) {
int pictureIndex = prop.getPropertyValue();
// increment reference count for pictures
EscherBSERecord bse = getBSERecord(pictureIndex);
bse.setRef(bse.getRef() + 1);
}
}
}
}
}
}
}
use of org.apache.poi.ddf.EscherDgRecord in project poi by apache.
the class TestCloneSheet method testCloneSheetWithEmptyDrawingAggregate.
@Test
public void testCloneSheetWithEmptyDrawingAggregate() {
HSSFWorkbook b = new HSSFWorkbook();
HSSFSheet s = b.createSheet("Test");
HSSFPatriarch patriarch = s.createDrawingPatriarch();
EscherAggregate agg1 = patriarch.getBoundAggregate();
HSSFSheet s2 = s.cloneSheet(b);
patriarch = s2.getDrawingPatriarch();
EscherAggregate agg2 = patriarch.getBoundAggregate();
EscherSpRecord sp1 = (EscherSpRecord) agg1.getEscherContainer().getChild(1).getChild(0).getChild(1);
EscherSpRecord sp2 = (EscherSpRecord) agg2.getEscherContainer().getChild(1).getChild(0).getChild(1);
assertEquals(sp1.getShapeId(), 1024);
assertEquals(sp2.getShapeId(), 2048);
EscherDgRecord dg = (EscherDgRecord) agg2.getEscherContainer().getChild(0);
assertEquals(dg.getLastMSOSPID(), 2048);
assertEquals(dg.getInstance(), 0x2);
//everything except id and DgRecord.lastMSOSPID and DgRecord.Instance must be the same
sp2.setShapeId(1024);
dg.setLastMSOSPID(1024);
dg.setInstance((short) 0x1);
assertEquals(agg1.serialize().length, agg2.serialize().length);
assertEquals(agg1.toXml(""), agg2.toXml(""));
assertArrayEquals(agg1.serialize(), agg2.serialize());
}
use of org.apache.poi.ddf.EscherDgRecord in project poi by apache.
the class DrawingManager2 method createDgRecord.
/**
* Creates a new drawing group
*
* @return a new drawing group
*/
public EscherDgRecord createDgRecord() {
EscherDgRecord dg = new EscherDgRecord();
dg.setRecordId(EscherDgRecord.RECORD_ID);
short dgId = findNewDrawingGroupId();
dg.setOptions((short) (dgId << 4));
dg.setNumShapes(0);
dg.setLastMSOSPID(-1);
drawingGroups.add(dg);
dgg.addCluster(dgId, 0);
dgg.setDrawingsSaved(dgg.getDrawingsSaved() + 1);
return dg;
}
use of org.apache.poi.ddf.EscherDgRecord in project poi by apache.
the class DrawingManager method createDgRecord.
public EscherDgRecord createDgRecord() {
EscherDgRecord dg = new EscherDgRecord();
dg.setRecordId(EscherDgRecord.RECORD_ID);
short dgId = findNewDrawingGroupId();
dg.setOptions((short) (dgId << 4));
dg.setNumShapes(0);
dg.setLastMSOSPID(-1);
dgg.addCluster(dgId, 0);
dgg.setDrawingsSaved(dgg.getDrawingsSaved() + 1);
dgMap.put(dgId, dg);
return dg;
}
use of org.apache.poi.ddf.EscherDgRecord in project poi by apache.
the class TestDrawingManager method testAllocateShapeId.
public void testAllocateShapeId() {
EscherDggRecord dgg = new EscherDggRecord();
dgg.setDrawingsSaved(0);
dgg.setFileIdClusters(new EscherDggRecord.FileIdCluster[] {});
DrawingManager dm = new DrawingManager(dgg);
EscherDgRecord dg = dm.createDgRecord();
int shapeId = dm.allocateShapeId(dg.getDrawingGroupId());
assertEquals(1024, shapeId);
assertEquals(1025, dgg.getShapeIdMax());
assertEquals(1, dgg.getDrawingsSaved());
assertEquals(1, dgg.getFileIdClusters()[0].getDrawingGroupId());
assertEquals(1, dgg.getFileIdClusters()[0].getNumShapeIdsUsed());
assertEquals(1024, dg.getLastMSOSPID());
assertEquals(1, dg.getNumShapes());
}
Aggregations