/** * Collapses the drawing records into an aggregate. */ public static EscherAggregate CreateAggregate(IList records, int locFirstDrawingRecord, DrawingManager2 drawingManager) { // 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. IList shapeRecords = new ArrayList(); EscherRecordFactory recordFactory = new CustomEscherRecordFactory(ref shapeRecords); // Calculate the size of the buffer EscherAggregate agg = new EscherAggregate(drawingManager); int loc = locFirstDrawingRecord; int dataSize = 0; while (loc + 1 < records.Count && GetSid(records, loc) == DrawingRecord.sid && IsObjectRecord(records, loc + 1)) { dataSize += ((DrawingRecord)records[loc]).Data.Length; loc += 2; } // Create one big buffer byte[] buffer = new byte[dataSize]; int offset = 0; loc = locFirstDrawingRecord; while (loc + 1 < records.Count && GetSid(records, loc) == DrawingRecord.sid && IsObjectRecord(records, loc + 1)) { DrawingRecord drawingRecord = (DrawingRecord)records[loc]; Array.Copy(drawingRecord.Data, 0, buffer, offset, drawingRecord.Data.Length); offset += drawingRecord.Data.Length; loc += 2; } // Decode the shapes // agg.escherRecords = new ArrayList(); int pos = 0; while (pos < dataSize) { EscherRecord r = recordFactory.CreateRecord(buffer, pos); int bytesRead = r.FillFields(buffer, pos, recordFactory); agg.AddEscherRecord(r); pos += bytesRead; } // Associate the object records with the shapes loc = locFirstDrawingRecord; int shapeIndex = 0; agg.shapeToObj = new Hashtable(); while (loc + 1 < records.Count && GetSid(records, loc) == DrawingRecord.sid && IsObjectRecord(records, loc + 1)) { Record objRecord = (Record)records[loc + 1]; agg.shapeToObj[shapeRecords[shapeIndex++]]= objRecord; loc += 2; } return agg; }
public EscherAggregate(DrawingManager2 drawingManager) { this.drawingManager = drawingManager; }
/** * 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.RecordId=unchecked((short)0xF000); dggContainer.Options=(short)0x000F; dgg.RecordId=EscherDggRecord.RECORD_ID; dgg.Options=(short)0x0000; dgg.ShapeIdMax=1024; dgg.NumShapesSaved=0; dgg.DrawingsSaved=0; dgg.FileIdClusters=new EscherDggRecord.FileIdCluster[] { }; drawingManager = new DrawingManager2(dgg); EscherContainerRecord bstoreContainer = null; if (escherBSERecords.Count > 0) { bstoreContainer = new EscherContainerRecord(); bstoreContainer.RecordId=EscherContainerRecord.BSTORE_CONTAINER; bstoreContainer.Options=(short)((escherBSERecords.Count << 4) | 0xF); for (IEnumerator iterator = escherBSERecords.GetEnumerator(); iterator.MoveNext(); ) { EscherRecord escherRecord = (EscherRecord)iterator.Current; bstoreContainer.AddChildRecord(escherRecord); } } opt.RecordId=unchecked((short)0xF00B); opt.Options=(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.RecordId=unchecked((short)0xF11E); splitMenuColors.Options=(short)0x0040; splitMenuColors.Color1=0x0800000D; splitMenuColors.Color2=0x0800000C; splitMenuColors.Color3=0x08000017; splitMenuColors.Color4=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); Records.Insert(loc + 1, drawingGroup); } else { DrawingGroupRecord drawingGroup = new DrawingGroupRecord(); drawingGroup.AddEscherRecord(dggContainer); Records[dgLoc]= drawingGroup; } } }
/** * Finds the DrawingRecord for our sheet, and * attaches it to the DrawingManager (which knows about * the overall DrawingGroup for our workbook). * If requested, will Create a new DrawRecord * if none currently exist * @param drawingManager The DrawingManager2 for our workbook * @param CreateIfMissing Should one be Created if missing? */ public int AggregateDrawingRecords(DrawingManager2 drawingManager, bool CreateIfMissing) { int loc = FindFirstRecordLocBySid(DrawingRecord.sid); bool noDrawingRecordsFound = (loc == -1); if (noDrawingRecordsFound) { if (!CreateIfMissing) { // None found, and not allowed to Add in return -1; } EscherAggregate aggregate = new EscherAggregate(drawingManager); loc = FindFirstRecordLocBySid(EscherAggregate.sid); if (loc == -1) { loc = FindFirstRecordLocBySid(WindowTwoRecord.sid); } else { Records.RemoveAt(loc); } Records.Insert(loc, aggregate); return loc; } else { IList records = Records; EscherAggregate r = EscherAggregate.CreateAggregate(records, loc, drawingManager); int startloc = loc; while (loc + 1 < records.Count && records[loc] is DrawingRecord && (records[loc + 1] is ObjRecord || records[loc + 1] is TextObjectRecord) ) { loc += 2; if (records[loc] is NoteRecord) loc ++; } int endloc = loc - 1; for (int i = 0; i < (endloc - startloc + 1); i++) { records.RemoveAt(startloc); } records.Insert(startloc, r); return startloc; } }
/** * Finds the primary drawing Group, if one already exists */ public DrawingManager2 FindDrawingGroup() { if (drawingManager != null) { // We already have it! return drawingManager; } // Need to Find a DrawingGroupRecord that // Contains a EscherDggRecord for (IEnumerator rit = records.GetEnumerator(); rit.MoveNext(); ) { Record r = (Record)rit.Current; if (r is DrawingGroupRecord) { DrawingGroupRecord dg = (DrawingGroupRecord)r; dg.ProcessChildRecords(); EscherContainerRecord cr = dg.GetEscherContainer(); if (cr == null) { continue; } EscherDggRecord dgg = null; EscherContainerRecord bStore = null; for (IEnumerator it = cr.ChildRecords.GetEnumerator(); it.MoveNext(); ) { EscherRecord er = (EscherRecord)it.Current; if (er is EscherDggRecord) { dgg = (EscherDggRecord)er; } else if (er.RecordId == EscherContainerRecord.BSTORE_CONTAINER) { bStore = (EscherContainerRecord)er; } } if (dgg != null) { drawingManager = new DrawingManager2(dgg); if (bStore != null) { foreach (EscherRecord bs in bStore.ChildRecords) { if (bs is EscherBSERecord) escherBSERecords.Add((EscherBSERecord)bs); } } return drawingManager; } } } // Look for the DrawingGroup record int dgLoc = FindFirstRecordLocBySid(DrawingGroupRecord.sid); // If there is one, does it have a EscherDggRecord? if (dgLoc != -1) { DrawingGroupRecord dg = (DrawingGroupRecord)records[dgLoc]; EscherDggRecord dgg = null; EscherContainerRecord bStore = null; for (IEnumerator it = dg.EscherRecords.GetEnumerator(); it.MoveNext(); ) { EscherRecord er = (EscherRecord)it.Current; if (er is EscherDggRecord) { dgg = (EscherDggRecord)er; } else if (er.RecordId == EscherContainerRecord.BSTORE_CONTAINER) { bStore = (EscherContainerRecord)er; } } if (dgg != null) { drawingManager = new DrawingManager2(dgg); if (bStore != null) { foreach (EscherRecord bs in bStore.ChildRecords) { if (bs is EscherBSERecord) escherBSERecords.Add((EscherBSERecord)bs); } } } } return drawingManager; }