PageRenderTime 56ms CodeModel.GetById 24ms RepoModel.GetById 0ms app.codeStats 0ms

/components/forks/poi/src/loci/poi/hssf/record/EscherAggregate.java

http://github.com/openmicroscopy/bioformats
Java | 765 lines | 570 code | 66 blank | 129 comment | 52 complexity | 9e212df4a5b523744fd315d99c376d1b MD5 | raw file
Possible License(s): GPL-2.0, LGPL-2.1, Apache-2.0, BSD-2-Clause, MPL-2.0-no-copyleft-exception
  1. /*
  2. * #%L
  3. * Fork of Apache Jakarta POI.
  4. * %%
  5. * Copyright (C) 2008 - 2013 Open Microscopy Environment:
  6. * - Board of Regents of the University of Wisconsin-Madison
  7. * - Glencoe Software, Inc.
  8. * - University of Dundee
  9. * %%
  10. * Licensed under the Apache License, Version 2.0 (the "License");
  11. * you may not use this file except in compliance with the License.
  12. * You may obtain a copy of the License at
  13. *
  14. * http://www.apache.org/licenses/LICENSE-2.0
  15. *
  16. * Unless required by applicable law or agreed to in writing, software
  17. * distributed under the License is distributed on an "AS IS" BASIS,
  18. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  19. * See the License for the specific language governing permissions and
  20. * limitations under the License.
  21. * #L%
  22. */
  23. /* ====================================================================
  24. Licensed to the Apache Software Foundation (ASF) under one or more
  25. contributor license agreements. See the NOTICE file distributed with
  26. this work for additional information regarding copyright ownership.
  27. The ASF licenses this file to You under the Apache License, Version 2.0
  28. (the "License"); you may not use this file except in compliance with
  29. the License. You may obtain a copy of the License at
  30. http://www.apache.org/licenses/LICENSE-2.0
  31. Unless required by applicable law or agreed to in writing, software
  32. distributed under the License is distributed on an "AS IS" BASIS,
  33. WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  34. See the License for the specific language governing permissions and
  35. limitations under the License.
  36. ==================================================================== */
  37. package loci.poi.hssf.record;
  38. import loci.poi.ddf.*;
  39. import loci.poi.hssf.usermodel.*;
  40. import loci.poi.hssf.model.AbstractShape;
  41. import loci.poi.hssf.model.TextboxShape;
  42. import loci.poi.hssf.model.DrawingManager2;
  43. import loci.poi.hssf.model.ConvertAnchor;
  44. import loci.poi.hssf.model.CommentShape;
  45. import java.util.*;
  46. /**
  47. * This class is used to aggregate the MSODRAWING and OBJ record
  48. * combinations. This is necessary due to the bizare way in which
  49. * these records are serialized. What happens is that you get a
  50. * combination of MSODRAWING -> OBJ -> MSODRAWING -> OBJ records
  51. * but the escher records are serialized _across_ the MSODRAWING
  52. * records.
  53. * <p>
  54. * It gets even worse when you start looking at TXO records.
  55. * <p>
  56. * So what we do with this class is aggregate lazily. That is
  57. * we don't aggregate the MSODRAWING -> OBJ records unless we
  58. * need to modify them.
  59. *
  60. *
  61. * @author Glen Stampoultzis (glens at apache.org)
  62. */
  63. public class EscherAggregate extends AbstractEscherHolderRecord
  64. {
  65. public static final short sid = 9876;
  66. public static final short ST_MIN = (short) 0;
  67. public static final short ST_NOT_PRIMATIVE = ST_MIN;
  68. public static final short ST_RECTANGLE = (short) 1;
  69. public static final short ST_ROUNDRECTANGLE = (short) 2;
  70. public static final short ST_ELLIPSE = (short) 3;
  71. public static final short ST_DIAMOND = (short) 4;
  72. public static final short ST_ISOCELESTRIANGLE = (short) 5;
  73. public static final short ST_RIGHTTRIANGLE = (short) 6;
  74. public static final short ST_PARALLELOGRAM = (short) 7;
  75. public static final short ST_TRAPEZOID = (short) 8;
  76. public static final short ST_HEXAGON = (short) 9;
  77. public static final short ST_OCTAGON = (short) 10;
  78. public static final short ST_PLUS = (short) 11;
  79. public static final short ST_STAR = (short) 12;
  80. public static final short ST_ARROW = (short) 13;
  81. public static final short ST_THICKARROW = (short) 14;
  82. public static final short ST_HOMEPLATE = (short) 15;
  83. public static final short ST_CUBE = (short) 16;
  84. public static final short ST_BALLOON = (short) 17;
  85. public static final short ST_SEAL = (short) 18;
  86. public static final short ST_ARC = (short) 19;
  87. public static final short ST_LINE = (short) 20;
  88. public static final short ST_PLAQUE = (short) 21;
  89. public static final short ST_CAN = (short) 22;
  90. public static final short ST_DONUT = (short) 23;
  91. public static final short ST_TEXTSIMPLE = (short) 24;
  92. public static final short ST_TEXTOCTAGON = (short) 25;
  93. public static final short ST_TEXTHEXAGON = (short) 26;
  94. public static final short ST_TEXTCURVE = (short) 27;
  95. public static final short ST_TEXTWAVE = (short) 28;
  96. public static final short ST_TEXTRING = (short) 29;
  97. public static final short ST_TEXTONCURVE = (short) 30;
  98. public static final short ST_TEXTONRING = (short) 31;
  99. public static final short ST_STRAIGHTCONNECTOR1 = (short) 32;
  100. public static final short ST_BENTCONNECTOR2 = (short) 33;
  101. public static final short ST_BENTCONNECTOR3 = (short) 34;
  102. public static final short ST_BENTCONNECTOR4 = (short) 35;
  103. public static final short ST_BENTCONNECTOR5 = (short) 36;
  104. public static final short ST_CURVEDCONNECTOR2 = (short) 37;
  105. public static final short ST_CURVEDCONNECTOR3 = (short) 38;
  106. public static final short ST_CURVEDCONNECTOR4 = (short) 39;
  107. public static final short ST_CURVEDCONNECTOR5 = (short) 40;
  108. public static final short ST_CALLOUT1 = (short) 41;
  109. public static final short ST_CALLOUT2 = (short) 42;
  110. public static final short ST_CALLOUT3 = (short) 43;
  111. public static final short ST_ACCENTCALLOUT1 = (short) 44;
  112. public static final short ST_ACCENTCALLOUT2 = (short) 45;
  113. public static final short ST_ACCENTCALLOUT3 = (short) 46;
  114. public static final short ST_BORDERCALLOUT1 = (short) 47;
  115. public static final short ST_BORDERCALLOUT2 = (short) 48;
  116. public static final short ST_BORDERCALLOUT3 = (short) 49;
  117. public static final short ST_ACCENTBORDERCALLOUT1 = (short) 50;
  118. public static final short ST_ACCENTBORDERCALLOUT2 = (short) 51;
  119. public static final short ST_ACCENTBORDERCALLOUT3 = (short) 52;
  120. public static final short ST_RIBBON = (short) 53;
  121. public static final short ST_RIBBON2 = (short) 54;
  122. public static final short ST_CHEVRON = (short) 55;
  123. public static final short ST_PENTAGON = (short) 56;
  124. public static final short ST_NOSMOKING = (short) 57;
  125. public static final short ST_SEAL8 = (short) 58;
  126. public static final short ST_SEAL16 = (short) 59;
  127. public static final short ST_SEAL32 = (short) 60;
  128. public static final short ST_WEDGERECTCALLOUT = (short) 61;
  129. public static final short ST_WEDGERRECTCALLOUT = (short) 62;
  130. public static final short ST_WEDGEELLIPSECALLOUT = (short) 63;
  131. public static final short ST_WAVE = (short) 64;
  132. public static final short ST_FOLDEDCORNER = (short) 65;
  133. public static final short ST_LEFTARROW = (short) 66;
  134. public static final short ST_DOWNARROW = (short) 67;
  135. public static final short ST_UPARROW = (short) 68;
  136. public static final short ST_LEFTRIGHTARROW = (short) 69;
  137. public static final short ST_UPDOWNARROW = (short) 70;
  138. public static final short ST_IRREGULARSEAL1 = (short) 71;
  139. public static final short ST_IRREGULARSEAL2 = (short) 72;
  140. public static final short ST_LIGHTNINGBOLT = (short) 73;
  141. public static final short ST_HEART = (short) 74;
  142. public static final short ST_PICTUREFRAME = (short) 75;
  143. public static final short ST_QUADARROW = (short) 76;
  144. public static final short ST_LEFTARROWCALLOUT = (short) 77;
  145. public static final short ST_RIGHTARROWCALLOUT = (short) 78;
  146. public static final short ST_UPARROWCALLOUT = (short) 79;
  147. public static final short ST_DOWNARROWCALLOUT = (short) 80;
  148. public static final short ST_LEFTRIGHTARROWCALLOUT = (short) 81;
  149. public static final short ST_UPDOWNARROWCALLOUT = (short) 82;
  150. public static final short ST_QUADARROWCALLOUT = (short) 83;
  151. public static final short ST_BEVEL = (short) 84;
  152. public static final short ST_LEFTBRACKET = (short) 85;
  153. public static final short ST_RIGHTBRACKET = (short) 86;
  154. public static final short ST_LEFTBRACE = (short) 87;
  155. public static final short ST_RIGHTBRACE = (short) 88;
  156. public static final short ST_LEFTUPARROW = (short) 89;
  157. public static final short ST_BENTUPARROW = (short) 90;
  158. public static final short ST_BENTARROW = (short) 91;
  159. public static final short ST_SEAL24 = (short) 92;
  160. public static final short ST_STRIPEDRIGHTARROW = (short) 93;
  161. public static final short ST_NOTCHEDRIGHTARROW = (short) 94;
  162. public static final short ST_BLOCKARC = (short) 95;
  163. public static final short ST_SMILEYFACE = (short) 96;
  164. public static final short ST_VERTICALSCROLL = (short) 97;
  165. public static final short ST_HORIZONTALSCROLL = (short) 98;
  166. public static final short ST_CIRCULARARROW = (short) 99;
  167. public static final short ST_NOTCHEDCIRCULARARROW = (short) 100;
  168. public static final short ST_UTURNARROW = (short) 101;
  169. public static final short ST_CURVEDRIGHTARROW = (short) 102;
  170. public static final short ST_CURVEDLEFTARROW = (short) 103;
  171. public static final short ST_CURVEDUPARROW = (short) 104;
  172. public static final short ST_CURVEDDOWNARROW = (short) 105;
  173. public static final short ST_CLOUDCALLOUT = (short) 106;
  174. public static final short ST_ELLIPSERIBBON = (short) 107;
  175. public static final short ST_ELLIPSERIBBON2 = (short) 108;
  176. public static final short ST_FLOWCHARTPROCESS = (short) 109;
  177. public static final short ST_FLOWCHARTDECISION = (short) 110;
  178. public static final short ST_FLOWCHARTINPUTOUTPUT = (short) 111;
  179. public static final short ST_FLOWCHARTPREDEFINEDPROCESS = (short) 112;
  180. public static final short ST_FLOWCHARTINTERNALSTORAGE = (short) 113;
  181. public static final short ST_FLOWCHARTDOCUMENT = (short) 114;
  182. public static final short ST_FLOWCHARTMULTIDOCUMENT = (short) 115;
  183. public static final short ST_FLOWCHARTTERMINATOR = (short) 116;
  184. public static final short ST_FLOWCHARTPREPARATION = (short) 117;
  185. public static final short ST_FLOWCHARTMANUALINPUT = (short) 118;
  186. public static final short ST_FLOWCHARTMANUALOPERATION = (short) 119;
  187. public static final short ST_FLOWCHARTCONNECTOR = (short) 120;
  188. public static final short ST_FLOWCHARTPUNCHEDCARD = (short) 121;
  189. public static final short ST_FLOWCHARTPUNCHEDTAPE = (short) 122;
  190. public static final short ST_FLOWCHARTSUMMINGJUNCTION = (short) 123;
  191. public static final short ST_FLOWCHARTOR = (short) 124;
  192. public static final short ST_FLOWCHARTCOLLATE = (short) 125;
  193. public static final short ST_FLOWCHARTSORT = (short) 126;
  194. public static final short ST_FLOWCHARTEXTRACT = (short) 127;
  195. public static final short ST_FLOWCHARTMERGE = (short) 128;
  196. public static final short ST_FLOWCHARTOFFLINESTORAGE = (short) 129;
  197. public static final short ST_FLOWCHARTONLINESTORAGE = (short) 130;
  198. public static final short ST_FLOWCHARTMAGNETICTAPE = (short) 131;
  199. public static final short ST_FLOWCHARTMAGNETICDISK = (short) 132;
  200. public static final short ST_FLOWCHARTMAGNETICDRUM = (short) 133;
  201. public static final short ST_FLOWCHARTDISPLAY = (short) 134;
  202. public static final short ST_FLOWCHARTDELAY = (short) 135;
  203. public static final short ST_TEXTPLAINTEXT = (short) 136;
  204. public static final short ST_TEXTSTOP = (short) 137;
  205. public static final short ST_TEXTTRIANGLE = (short) 138;
  206. public static final short ST_TEXTTRIANGLEINVERTED = (short) 139;
  207. public static final short ST_TEXTCHEVRON = (short) 140;
  208. public static final short ST_TEXTCHEVRONINVERTED = (short) 141;
  209. public static final short ST_TEXTRINGINSIDE = (short) 142;
  210. public static final short ST_TEXTRINGOUTSIDE = (short) 143;
  211. public static final short ST_TEXTARCHUPCURVE = (short) 144;
  212. public static final short ST_TEXTARCHDOWNCURVE = (short) 145;
  213. public static final short ST_TEXTCIRCLECURVE = (short) 146;
  214. public static final short ST_TEXTBUTTONCURVE = (short) 147;
  215. public static final short ST_TEXTARCHUPPOUR = (short) 148;
  216. public static final short ST_TEXTARCHDOWNPOUR = (short) 149;
  217. public static final short ST_TEXTCIRCLEPOUR = (short) 150;
  218. public static final short ST_TEXTBUTTONPOUR = (short) 151;
  219. public static final short ST_TEXTCURVEUP = (short) 152;
  220. public static final short ST_TEXTCURVEDOWN = (short) 153;
  221. public static final short ST_TEXTCASCADEUP = (short) 154;
  222. public static final short ST_TEXTCASCADEDOWN = (short) 155;
  223. public static final short ST_TEXTWAVE1 = (short) 156;
  224. public static final short ST_TEXTWAVE2 = (short) 157;
  225. public static final short ST_TEXTWAVE3 = (short) 158;
  226. public static final short ST_TEXTWAVE4 = (short) 159;
  227. public static final short ST_TEXTINFLATE = (short) 160;
  228. public static final short ST_TEXTDEFLATE = (short) 161;
  229. public static final short ST_TEXTINFLATEBOTTOM = (short) 162;
  230. public static final short ST_TEXTDEFLATEBOTTOM = (short) 163;
  231. public static final short ST_TEXTINFLATETOP = (short) 164;
  232. public static final short ST_TEXTDEFLATETOP = (short) 165;
  233. public static final short ST_TEXTDEFLATEINFLATE = (short) 166;
  234. public static final short ST_TEXTDEFLATEINFLATEDEFLATE = (short) 167;
  235. public static final short ST_TEXTFADERIGHT = (short) 168;
  236. public static final short ST_TEXTFADELEFT = (short) 169;
  237. public static final short ST_TEXTFADEUP = (short) 170;
  238. public static final short ST_TEXTFADEDOWN = (short) 171;
  239. public static final short ST_TEXTSLANTUP = (short) 172;
  240. public static final short ST_TEXTSLANTDOWN = (short) 173;
  241. public static final short ST_TEXTCANUP = (short) 174;
  242. public static final short ST_TEXTCANDOWN = (short) 175;
  243. public static final short ST_FLOWCHARTALTERNATEPROCESS = (short) 176;
  244. public static final short ST_FLOWCHARTOFFPAGECONNECTOR = (short) 177;
  245. public static final short ST_CALLOUT90 = (short) 178;
  246. public static final short ST_ACCENTCALLOUT90 = (short) 179;
  247. public static final short ST_BORDERCALLOUT90 = (short) 180;
  248. public static final short ST_ACCENTBORDERCALLOUT90 = (short) 181;
  249. public static final short ST_LEFTRIGHTUPARROW = (short) 182;
  250. public static final short ST_SUN = (short) 183;
  251. public static final short ST_MOON = (short) 184;
  252. public static final short ST_BRACKETPAIR = (short) 185;
  253. public static final short ST_BRACEPAIR = (short) 186;
  254. public static final short ST_SEAL4 = (short) 187;
  255. public static final short ST_DOUBLEWAVE = (short) 188;
  256. public static final short ST_ACTIONBUTTONBLANK = (short) 189;
  257. public static final short ST_ACTIONBUTTONHOME = (short) 190;
  258. public static final short ST_ACTIONBUTTONHELP = (short) 191;
  259. public static final short ST_ACTIONBUTTONINFORMATION = (short) 192;
  260. public static final short ST_ACTIONBUTTONFORWARDNEXT = (short) 193;
  261. public static final short ST_ACTIONBUTTONBACKPREVIOUS = (short) 194;
  262. public static final short ST_ACTIONBUTTONEND = (short) 195;
  263. public static final short ST_ACTIONBUTTONBEGINNING = (short) 196;
  264. public static final short ST_ACTIONBUTTONRETURN = (short) 197;
  265. public static final short ST_ACTIONBUTTONDOCUMENT = (short) 198;
  266. public static final short ST_ACTIONBUTTONSOUND = (short) 199;
  267. public static final short ST_ACTIONBUTTONMOVIE = (short) 200;
  268. public static final short ST_HOSTCONTROL = (short) 201;
  269. public static final short ST_TEXTBOX = (short) 202;
  270. public static final short ST_NIL = (short) 0x0FFF;
  271. protected HSSFPatriarch patriarch;
  272. /** Maps shape container objects to their OBJ records */
  273. private Map shapeToObj = new HashMap();
  274. private DrawingManager2 drawingManager;
  275. private short drawingGroupId;
  276. /**
  277. * list of "tail" records that need to be serialized after all drawing group records
  278. */
  279. private List tailRec = new ArrayList();
  280. public EscherAggregate( DrawingManager2 drawingManager )
  281. {
  282. this.drawingManager = drawingManager;
  283. }
  284. /**
  285. * @return Returns the current sid.
  286. */
  287. public short getSid()
  288. {
  289. return sid;
  290. }
  291. /**
  292. * Unused since this is an aggregate record. Use createAggregate().
  293. *
  294. * @see #createAggregate
  295. */
  296. protected void fillFields( byte[] data, short size, int offset )
  297. {
  298. throw new IllegalStateException( "Should not reach here" );
  299. }
  300. /**
  301. * Calculates the string representation of this record. This is
  302. * simply a dump of all the records.
  303. */
  304. public String toString()
  305. {
  306. String nl = System.getProperty( "line.separtor" );
  307. StringBuffer result = new StringBuffer();
  308. result.append( '[' ).append( getRecordName() ).append( ']' + nl );
  309. for ( Iterator iterator = getEscherRecords().iterator(); iterator.hasNext(); )
  310. {
  311. EscherRecord escherRecord = (EscherRecord) iterator.next();
  312. result.append( escherRecord.toString() );
  313. }
  314. result.append( "[/" ).append( getRecordName() ).append( ']' + nl );
  315. return result.toString();
  316. }
  317. /**
  318. * Collapses the drawing records into an aggregate.
  319. */
  320. public static EscherAggregate createAggregate( List records, int locFirstDrawingRecord, DrawingManager2 drawingManager )
  321. {
  322. // Keep track of any shape records created so we can match them back to the object id's.
  323. // Textbox objects are also treated as shape objects.
  324. final List shapeRecords = new ArrayList();
  325. EscherRecordFactory recordFactory = new DefaultEscherRecordFactory()
  326. {
  327. public EscherRecord createRecord( byte[] data, int offset )
  328. {
  329. EscherRecord r = super.createRecord( data, offset );
  330. if ( r.getRecordId() == EscherClientDataRecord.RECORD_ID || r.getRecordId() == EscherTextboxRecord.RECORD_ID )
  331. {
  332. shapeRecords.add( r );
  333. }
  334. return r;
  335. }
  336. };
  337. // Calculate the size of the buffer
  338. EscherAggregate agg = new EscherAggregate(drawingManager);
  339. int loc = locFirstDrawingRecord;
  340. int dataSize = 0;
  341. while ( loc + 1 < records.size()
  342. && sid( records, loc ) == DrawingRecord.sid
  343. && isObjectRecord( records, loc + 1 ) )
  344. {
  345. dataSize += ( (DrawingRecord) records.get( loc ) ).getData().length;
  346. loc += 2;
  347. }
  348. // Create one big buffer
  349. byte buffer[] = new byte[dataSize];
  350. int offset = 0;
  351. loc = locFirstDrawingRecord;
  352. while ( loc + 1 < records.size()
  353. && sid( records, loc ) == DrawingRecord.sid
  354. && isObjectRecord( records, loc + 1 ) )
  355. {
  356. DrawingRecord drawingRecord = (DrawingRecord) records.get( loc );
  357. System.arraycopy( drawingRecord.getData(), 0, buffer, offset, drawingRecord.getData().length );
  358. offset += drawingRecord.getData().length;
  359. loc += 2;
  360. }
  361. // Decode the shapes
  362. // agg.escherRecords = new ArrayList();
  363. int pos = 0;
  364. while ( pos < dataSize )
  365. {
  366. EscherRecord r = recordFactory.createRecord( buffer, pos );
  367. int bytesRead = r.fillFields( buffer, pos, recordFactory );
  368. agg.addEscherRecord( r );
  369. pos += bytesRead;
  370. }
  371. // Associate the object records with the shapes
  372. loc = locFirstDrawingRecord;
  373. int shapeIndex = 0;
  374. agg.shapeToObj = new HashMap();
  375. while ( loc + 1 < records.size()
  376. && sid( records, loc ) == DrawingRecord.sid
  377. && isObjectRecord( records, loc + 1 ) )
  378. {
  379. Record objRecord = (Record) records.get( loc + 1 );
  380. agg.shapeToObj.put( shapeRecords.get( shapeIndex++ ), objRecord );
  381. loc += 2;
  382. }
  383. return agg;
  384. }
  385. /**
  386. * Serializes this aggregate to a byte array. Since this is an aggregate
  387. * record it will effectively serialize the aggregated records.
  388. *
  389. * @param offset The offset into the start of the array.
  390. * @param data The byte array to serialize to.
  391. * @return The number of bytes serialized.
  392. */
  393. public int serialize( int offset, byte[] data )
  394. {
  395. convertUserModelToRecords();
  396. // Determine buffer size
  397. List records = getEscherRecords();
  398. int size = getEscherRecordSize( records );
  399. byte[] buffer = new byte[size];
  400. // Serialize escher records into one big data structure and keep note of ending offsets.
  401. final List spEndingOffsets = new ArrayList();
  402. final List shapes = new ArrayList();
  403. int pos = 0;
  404. for ( Iterator iterator = records.iterator(); iterator.hasNext(); )
  405. {
  406. EscherRecord e = (EscherRecord) iterator.next();
  407. pos += e.serialize( pos, buffer, new EscherSerializationListener()
  408. {
  409. public void beforeRecordSerialize( int offset, short recordId, EscherRecord record )
  410. {
  411. }
  412. public void afterRecordSerialize( int offset, short recordId, int size, EscherRecord record )
  413. {
  414. if ( recordId == EscherClientDataRecord.RECORD_ID || recordId == EscherTextboxRecord.RECORD_ID )
  415. {
  416. spEndingOffsets.add( new Integer( offset ) );
  417. shapes.add( record );
  418. }
  419. }
  420. } );
  421. }
  422. // todo: fix this
  423. shapes.add( 0, null );
  424. spEndingOffsets.add( 0, null );
  425. // Split escher records into separate MSODRAWING and OBJ, TXO records. (We don't break on
  426. // the first one because it's the patriach).
  427. pos = offset;
  428. for ( int i = 1; i < shapes.size(); i++ )
  429. {
  430. int endOffset = ( (Integer) spEndingOffsets.get( i ) ).intValue() - 1;
  431. int startOffset;
  432. if ( i == 1 )
  433. startOffset = 0;
  434. else
  435. startOffset = ( (Integer) spEndingOffsets.get( i - 1 ) ).intValue();
  436. // Create and write a new MSODRAWING record
  437. DrawingRecord drawing = new DrawingRecord();
  438. byte[] drawingData = new byte[endOffset - startOffset + 1];
  439. System.arraycopy( buffer, startOffset, drawingData, 0, drawingData.length );
  440. drawing.setData( drawingData );
  441. int temp = drawing.serialize( pos, data );
  442. pos += temp;
  443. // Write the matching OBJ record
  444. Record obj = (Record) shapeToObj.get( shapes.get( i ) );
  445. temp = obj.serialize( pos, data );
  446. pos += temp;
  447. }
  448. // write records that need to be serialized after all drawing group records
  449. for ( int i = 0; i < tailRec.size(); i++ )
  450. {
  451. Record rec = (Record)tailRec.get(i);
  452. pos += rec.serialize( pos, data );
  453. }
  454. int bytesWritten = pos - offset;
  455. if ( bytesWritten != getRecordSize() )
  456. throw new RecordFormatException( bytesWritten + " bytes written but getRecordSize() reports " + getRecordSize() );
  457. return bytesWritten;
  458. }
  459. /**
  460. * How many bytes do the raw escher records contain.
  461. * @param records List of escher records
  462. * @return the number of bytes
  463. */
  464. private int getEscherRecordSize( List records )
  465. {
  466. int size = 0;
  467. for ( Iterator iterator = records.iterator(); iterator.hasNext(); )
  468. size += ( (EscherRecord) iterator.next() ).getRecordSize();
  469. return size;
  470. }
  471. /**
  472. * The number of bytes required to serialize this record.
  473. */
  474. public int getRecordSize()
  475. {
  476. convertUserModelToRecords();
  477. List records = getEscherRecords();
  478. int rawEscherSize = getEscherRecordSize( records );
  479. int drawingRecordSize = rawEscherSize + ( shapeToObj.size() ) * 4;
  480. int objRecordSize = 0;
  481. for ( Iterator iterator = shapeToObj.values().iterator(); iterator.hasNext(); )
  482. {
  483. Record r = (Record) iterator.next();
  484. objRecordSize += r.getRecordSize();
  485. }
  486. int tailRecordSize = 0;
  487. for ( Iterator iterator = tailRec.iterator(); iterator.hasNext(); )
  488. {
  489. Record r = (Record) iterator.next();
  490. tailRecordSize += r.getRecordSize();
  491. }
  492. return drawingRecordSize + objRecordSize + tailRecordSize;
  493. }
  494. /**
  495. * Associates an escher record to an OBJ record or a TXO record.
  496. */
  497. public Object assoicateShapeToObjRecord( EscherRecord r, Record objRecord )
  498. {
  499. return shapeToObj.put( r, objRecord );
  500. }
  501. public HSSFPatriarch getPatriarch()
  502. {
  503. return patriarch;
  504. }
  505. public void setPatriarch( HSSFPatriarch patriarch )
  506. {
  507. this.patriarch = patriarch;
  508. }
  509. public void clear()
  510. {
  511. clearEscherRecords();
  512. shapeToObj.clear();
  513. // lastShapeId = 1024;
  514. }
  515. protected String getRecordName()
  516. {
  517. return "ESCHERAGGREGATE";
  518. }
  519. // =============== Private methods ========================
  520. private static boolean isObjectRecord( List records, int loc )
  521. {
  522. return sid( records, loc ) == ObjRecord.sid || sid( records, loc ) == TextObjectRecord.sid;
  523. }
  524. private void convertUserModelToRecords()
  525. {
  526. if ( patriarch != null )
  527. {
  528. shapeToObj.clear();
  529. tailRec.clear();
  530. clearEscherRecords();
  531. if ( patriarch.getChildren().size() != 0 )
  532. {
  533. convertPatriarch( patriarch );
  534. EscherContainerRecord dgContainer = (EscherContainerRecord) getEscherRecord( 0 );
  535. EscherContainerRecord spgrContainer = null;
  536. for ( int i = 0; i < dgContainer.getChildRecords().size(); i++ )
  537. if ( dgContainer.getChild( i ).getRecordId() == EscherContainerRecord.SPGR_CONTAINER )
  538. spgrContainer = (EscherContainerRecord) dgContainer.getChild( i );
  539. convertShapes( patriarch, spgrContainer, shapeToObj );
  540. patriarch = null;
  541. }
  542. }
  543. }
  544. private void convertShapes( HSSFShapeContainer parent, EscherContainerRecord escherParent, Map shapeToObj )
  545. {
  546. if ( escherParent == null ) throw new IllegalArgumentException( "Parent record required" );
  547. List shapes = parent.getChildren();
  548. for ( Iterator iterator = shapes.iterator(); iterator.hasNext(); )
  549. {
  550. HSSFShape shape = (HSSFShape) iterator.next();
  551. if ( shape instanceof HSSFShapeGroup )
  552. {
  553. convertGroup( (HSSFShapeGroup) shape, escherParent, shapeToObj );
  554. }
  555. else
  556. {
  557. AbstractShape shapeModel = AbstractShape.createShape(
  558. shape,
  559. drawingManager.allocateShapeId(drawingGroupId) );
  560. shapeToObj.put( findClientData( shapeModel.getSpContainer() ), shapeModel.getObjRecord() );
  561. if ( shapeModel instanceof TextboxShape )
  562. {
  563. EscherRecord escherTextbox = ( (TextboxShape) shapeModel ).getEscherTextbox();
  564. shapeToObj.put( escherTextbox, ( (TextboxShape) shapeModel ).getTextObjectRecord() );
  565. // escherParent.addChildRecord(escherTextbox);
  566. if ( shapeModel instanceof CommentShape ){
  567. CommentShape comment = (CommentShape)shapeModel;
  568. tailRec.add(comment.getNoteRecord());
  569. }
  570. }
  571. escherParent.addChildRecord( shapeModel.getSpContainer() );
  572. }
  573. }
  574. // drawingManager.newCluster( (short)1 );
  575. // drawingManager.newCluster( (short)2 );
  576. }
  577. private void convertGroup( HSSFShapeGroup shape, EscherContainerRecord escherParent, Map shapeToObj )
  578. {
  579. EscherContainerRecord spgrContainer = new EscherContainerRecord();
  580. EscherContainerRecord spContainer = new EscherContainerRecord();
  581. EscherSpgrRecord spgr = new EscherSpgrRecord();
  582. EscherSpRecord sp = new EscherSpRecord();
  583. EscherOptRecord opt = new EscherOptRecord();
  584. EscherRecord anchor;
  585. EscherClientDataRecord clientData = new EscherClientDataRecord();
  586. spgrContainer.setRecordId( EscherContainerRecord.SPGR_CONTAINER );
  587. spgrContainer.setOptions( (short) 0x000F );
  588. spContainer.setRecordId( EscherContainerRecord.SP_CONTAINER );
  589. spContainer.setOptions( (short) 0x000F );
  590. spgr.setRecordId( EscherSpgrRecord.RECORD_ID );
  591. spgr.setOptions( (short) 0x0001 );
  592. spgr.setRectX1( shape.getX1() );
  593. spgr.setRectY1( shape.getY1() );
  594. spgr.setRectX2( shape.getX2() );
  595. spgr.setRectY2( shape.getY2() );
  596. sp.setRecordId( EscherSpRecord.RECORD_ID );
  597. sp.setOptions( (short) 0x0002 );
  598. int shapeId = drawingManager.allocateShapeId(drawingGroupId);
  599. sp.setShapeId( shapeId );
  600. if (shape.getAnchor() instanceof HSSFClientAnchor)
  601. sp.setFlags( EscherSpRecord.FLAG_GROUP | EscherSpRecord.FLAG_HAVEANCHOR );
  602. else
  603. sp.setFlags( EscherSpRecord.FLAG_GROUP | EscherSpRecord.FLAG_HAVEANCHOR | EscherSpRecord.FLAG_CHILD );
  604. opt.setRecordId( EscherOptRecord.RECORD_ID );
  605. opt.setOptions( (short) 0x0023 );
  606. opt.addEscherProperty( new EscherBoolProperty( EscherProperties.PROTECTION__LOCKAGAINSTGROUPING, 0x00040004 ) );
  607. opt.addEscherProperty( new EscherBoolProperty( EscherProperties.GROUPSHAPE__PRINT, 0x00080000 ) );
  608. anchor = ConvertAnchor.createAnchor( shape.getAnchor() );
  609. // clientAnchor.setCol1( ( (HSSFClientAnchor) shape.getAnchor() ).getCol1() );
  610. // clientAnchor.setRow1( (short) ( (HSSFClientAnchor) shape.getAnchor() ).getRow1() );
  611. // clientAnchor.setDx1( (short) shape.getAnchor().getDx1() );
  612. // clientAnchor.setDy1( (short) shape.getAnchor().getDy1() );
  613. // clientAnchor.setCol2( ( (HSSFClientAnchor) shape.getAnchor() ).getCol2() );
  614. // clientAnchor.setRow2( (short) ( (HSSFClientAnchor) shape.getAnchor() ).getRow2() );
  615. // clientAnchor.setDx2( (short) shape.getAnchor().getDx2() );
  616. // clientAnchor.setDy2( (short) shape.getAnchor().getDy2() );
  617. clientData.setRecordId( EscherClientDataRecord.RECORD_ID );
  618. clientData.setOptions( (short) 0x0000 );
  619. spgrContainer.addChildRecord( spContainer );
  620. spContainer.addChildRecord( spgr );
  621. spContainer.addChildRecord( sp );
  622. spContainer.addChildRecord( opt );
  623. spContainer.addChildRecord( anchor );
  624. spContainer.addChildRecord( clientData );
  625. ObjRecord obj = new ObjRecord();
  626. CommonObjectDataSubRecord cmo = new CommonObjectDataSubRecord();
  627. cmo.setObjectType( CommonObjectDataSubRecord.OBJECT_TYPE_GROUP );
  628. cmo.setObjectId( (short) ( shapeId ) );
  629. cmo.setLocked( true );
  630. cmo.setPrintable( true );
  631. cmo.setAutofill( true );
  632. cmo.setAutoline( true );
  633. GroupMarkerSubRecord gmo = new GroupMarkerSubRecord();
  634. EndSubRecord end = new EndSubRecord();
  635. obj.addSubRecord( cmo );
  636. obj.addSubRecord( gmo );
  637. obj.addSubRecord( end );
  638. shapeToObj.put( clientData, obj );
  639. escherParent.addChildRecord( spgrContainer );
  640. convertShapes( shape, spgrContainer, shapeToObj );
  641. }
  642. private EscherRecord findClientData( EscherContainerRecord spContainer )
  643. {
  644. for ( Iterator iterator = spContainer.getChildRecords().iterator(); iterator.hasNext(); )
  645. {
  646. EscherRecord r = (EscherRecord) iterator.next();
  647. if ( r.getRecordId() == EscherClientDataRecord.RECORD_ID )
  648. return r;
  649. }
  650. throw new IllegalArgumentException( "Can not find client data record" );
  651. }
  652. private void convertPatriarch( HSSFPatriarch patriarch )
  653. {
  654. EscherContainerRecord dgContainer = new EscherContainerRecord();
  655. EscherDgRecord dg;
  656. EscherContainerRecord spgrContainer = new EscherContainerRecord();
  657. EscherContainerRecord spContainer1 = new EscherContainerRecord();
  658. EscherSpgrRecord spgr = new EscherSpgrRecord();
  659. EscherSpRecord sp1 = new EscherSpRecord();
  660. dgContainer.setRecordId( EscherContainerRecord.DG_CONTAINER );
  661. dgContainer.setOptions( (short) 0x000F );
  662. dg = drawingManager.createDgRecord();
  663. drawingGroupId = dg.getDrawingGroupId();
  664. // dg.setOptions( (short) ( drawingId << 4 ) );
  665. // dg.setNumShapes( getNumberOfShapes( patriarch ) );
  666. // dg.setLastMSOSPID( 0 ); // populated after all shape id's are assigned.
  667. spgrContainer.setRecordId( EscherContainerRecord.SPGR_CONTAINER );
  668. spgrContainer.setOptions( (short) 0x000F );
  669. spContainer1.setRecordId( EscherContainerRecord.SP_CONTAINER );
  670. spContainer1.setOptions( (short) 0x000F );
  671. spgr.setRecordId( EscherSpgrRecord.RECORD_ID );
  672. spgr.setOptions( (short) 0x0001 ); // version
  673. spgr.setRectX1( patriarch.getX1() );
  674. spgr.setRectY1( patriarch.getY1() );
  675. spgr.setRectX2( patriarch.getX2() );
  676. spgr.setRectY2( patriarch.getY2() );
  677. sp1.setRecordId( EscherSpRecord.RECORD_ID );
  678. sp1.setOptions( (short) 0x0002 );
  679. sp1.setShapeId( drawingManager.allocateShapeId(dg.getDrawingGroupId()) );
  680. sp1.setFlags( EscherSpRecord.FLAG_GROUP | EscherSpRecord.FLAG_PATRIARCH );
  681. dgContainer.addChildRecord( dg );
  682. dgContainer.addChildRecord( spgrContainer );
  683. spgrContainer.addChildRecord( spContainer1 );
  684. spContainer1.addChildRecord( spgr );
  685. spContainer1.addChildRecord( sp1 );
  686. addEscherRecord( dgContainer );
  687. }
  688. /** Retrieve the number of shapes (including the patriarch). */
  689. // private int getNumberOfShapes( HSSFPatriarch patriarch )
  690. // {
  691. // return patriarch.countOfAllChildren();
  692. // }
  693. private static short sid( List records, int loc )
  694. {
  695. return ( (Record) records.get( loc ) ).getSid();
  696. }
  697. }