use of org.apache.poi.ddf.EscherRecord in project poi by apache.
the class EscherAggregate method createAggregate.
/**
* Collapses the drawing records into an aggregate.
* read Drawing, Obj, TxtObj, Note and Continue records into single byte array,
* create Escher tree from byte array, create map <EscherRecord, Record>
*
* @param records - list of all records inside sheet
* @param locFirstDrawingRecord - location of the first DrawingRecord inside sheet
* @return new EscherAggregate create from all aggregated records which belong to drawing layer
*/
public static EscherAggregate createAggregate(List<RecordBase> records, int locFirstDrawingRecord) {
// Keep track of any shape records created so we can match them back to the object id's.
// Textbox objects are also treated as shape objects.
final List<EscherRecord> shapeRecords = new ArrayList<EscherRecord>();
EscherRecordFactory recordFactory = new DefaultEscherRecordFactory() {
public EscherRecord createRecord(byte[] data, int offset) {
EscherRecord r = super.createRecord(data, offset);
if (r.getRecordId() == EscherClientDataRecord.RECORD_ID || r.getRecordId() == EscherTextboxRecord.RECORD_ID) {
shapeRecords.add(r);
}
return r;
}
};
// Create one big buffer
ByteArrayOutputStream buffer = new ByteArrayOutputStream();
EscherAggregate agg = new EscherAggregate(false);
int loc = locFirstDrawingRecord;
while (loc + 1 < records.size() && (isDrawingLayerRecord(sid(records, loc)))) {
try {
if (!(sid(records, loc) == DrawingRecord.sid || sid(records, loc) == ContinueRecord.sid)) {
loc++;
continue;
}
if (sid(records, loc) == DrawingRecord.sid) {
buffer.write(((DrawingRecord) records.get(loc)).getRecordData());
} else {
buffer.write(((ContinueRecord) records.get(loc)).getData());
}
} catch (IOException e) {
throw new RuntimeException("Couldn't get data from drawing/continue records", e);
}
loc++;
}
// Decode the shapes
// agg.escherRecords = new ArrayList();
int pos = 0;
while (pos < buffer.size()) {
EscherRecord r = recordFactory.createRecord(buffer.toByteArray(), pos);
int bytesRead = r.fillFields(buffer.toByteArray(), pos, recordFactory);
agg.addEscherRecord(r);
pos += bytesRead;
}
// Associate the object records with the shapes
loc = locFirstDrawingRecord + 1;
int shapeIndex = 0;
while (loc < records.size() && (isDrawingLayerRecord(sid(records, loc)))) {
if (!isObjectRecord(records, loc)) {
loc++;
continue;
}
Record objRecord = (Record) records.get(loc);
agg.shapeToObj.put(shapeRecords.get(shapeIndex++), objRecord);
loc++;
}
// any NoteRecords that follow the drawing block must be aggregated and and saved in the tailRec collection
while (loc < records.size()) {
if (sid(records, loc) == NoteRecord.sid) {
NoteRecord r = (NoteRecord) records.get(loc);
agg.tailRec.put(r.getShapeId(), r);
} else {
break;
}
loc++;
}
int locLastDrawingRecord = loc;
// replace drawing block with the created EscherAggregate
records.subList(locFirstDrawingRecord, locLastDrawingRecord).clear();
records.add(locFirstDrawingRecord, agg);
return agg;
}
use of org.apache.poi.ddf.EscherRecord in project poi by apache.
the class EscherAggregate method toXml.
/**
* Calculates the xml representation of this record. This is
* simply a dump of all the records.
* @param tab - string which must be added before each line (used by default '\t')
* @return xml representation of the all aggregated records
*/
public String toXml(String tab) {
StringBuilder builder = new StringBuilder();
builder.append(tab).append("<").append(getRecordName()).append(">\n");
for (EscherRecord escherRecord : getEscherRecords()) {
builder.append(escherRecord.toXml(tab + "\t"));
}
builder.append(tab).append("</").append(getRecordName()).append(">\n");
return builder.toString();
}
use of org.apache.poi.ddf.EscherRecord in project poi by apache.
the class DrawingGroupRecord method getRawDataSize.
private int getRawDataSize() {
List<EscherRecord> escherRecords = getEscherRecords();
byte[] rawData = getRawData();
if (escherRecords.size() == 0 && rawData != null) {
return rawData.length;
}
int size = 0;
for (Iterator<EscherRecord> iterator = escherRecords.iterator(); iterator.hasNext(); ) {
EscherRecord r = iterator.next();
size += r.getRecordSize();
}
return size;
}
use of org.apache.poi.ddf.EscherRecord in project poi by apache.
the class InternalWorkbook method createDrawingGroup.
/**
* Creates a primary drawing group record. If it already
* exists then it's modified.
*/
public void createDrawingGroup() {
if (drawingManager == null) {
EscherContainerRecord dggContainer = new EscherContainerRecord();
EscherDggRecord dgg = new EscherDggRecord();
EscherOptRecord opt = new EscherOptRecord();
EscherSplitMenuColorsRecord splitMenuColors = new EscherSplitMenuColorsRecord();
dggContainer.setRecordId((short) 0xF000);
dggContainer.setOptions((short) 0x000F);
dgg.setRecordId(EscherDggRecord.RECORD_ID);
dgg.setOptions((short) 0x0000);
dgg.setShapeIdMax(1024);
dgg.setNumShapesSaved(0);
dgg.setDrawingsSaved(0);
dgg.setFileIdClusters(new EscherDggRecord.FileIdCluster[] {});
drawingManager = new DrawingManager2(dgg);
EscherContainerRecord bstoreContainer = null;
if (!escherBSERecords.isEmpty()) {
bstoreContainer = new EscherContainerRecord();
bstoreContainer.setRecordId(EscherContainerRecord.BSTORE_CONTAINER);
bstoreContainer.setOptions((short) ((escherBSERecords.size() << 4) | 0xF));
for (EscherRecord escherRecord : escherBSERecords) {
bstoreContainer.addChildRecord(escherRecord);
}
}
opt.setRecordId((short) 0xF00B);
opt.setOptions((short) 0x0033);
opt.addEscherProperty(new EscherBoolProperty(EscherProperties.TEXT__SIZE_TEXT_TO_FIT_SHAPE, 524296));
opt.addEscherProperty(new EscherRGBProperty(EscherProperties.FILL__FILLCOLOR, 0x08000041));
opt.addEscherProperty(new EscherRGBProperty(EscherProperties.LINESTYLE__COLOR, 134217792));
splitMenuColors.setRecordId((short) 0xF11E);
splitMenuColors.setOptions((short) 0x0040);
splitMenuColors.setColor1(0x0800000D);
splitMenuColors.setColor2(0x0800000C);
splitMenuColors.setColor3(0x08000017);
splitMenuColors.setColor4(0x100000F7);
dggContainer.addChildRecord(dgg);
if (bstoreContainer != null) {
dggContainer.addChildRecord(bstoreContainer);
}
dggContainer.addChildRecord(opt);
dggContainer.addChildRecord(splitMenuColors);
int dgLoc = findFirstRecordLocBySid(DrawingGroupRecord.sid);
if (dgLoc == -1) {
DrawingGroupRecord drawingGroup = new DrawingGroupRecord();
drawingGroup.addEscherRecord(dggContainer);
int loc = findFirstRecordLocBySid(CountryRecord.sid);
getRecords().add(loc + 1, drawingGroup);
} else {
DrawingGroupRecord drawingGroup = new DrawingGroupRecord();
drawingGroup.addEscherRecord(dggContainer);
getRecords().set(dgLoc, drawingGroup);
}
}
}
use of org.apache.poi.ddf.EscherRecord 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);
}
}
}
}
}
}
}
Aggregations