You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

PPDrawing.java 17KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432
  1. /* ====================================================================
  2. Licensed to the Apache Software Foundation (ASF) under one or more
  3. contributor license agreements. See the NOTICE file distributed with
  4. this work for additional information regarding copyright ownership.
  5. The ASF licenses this file to You under the Apache License, Version 2.0
  6. (the "License"); you may not use this file except in compliance with
  7. the License. You may obtain a copy of the License at
  8. http://www.apache.org/licenses/LICENSE-2.0
  9. Unless required by applicable law or agreed to in writing, software
  10. distributed under the License is distributed on an "AS IS" BASIS,
  11. WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. See the License for the specific language governing permissions and
  13. limitations under the License.
  14. ==================================================================== */
  15. package org.apache.poi.hslf.record;
  16. import org.apache.poi.util.LittleEndian;
  17. import org.apache.poi.util.POILogger;
  18. import org.apache.poi.ddf.*;
  19. import org.apache.poi.hslf.model.ShapeTypes;
  20. import java.io.IOException;
  21. import java.io.OutputStream;
  22. import java.util.LinkedList;
  23. import java.util.List;
  24. import java.util.Vector;
  25. import java.util.Iterator;
  26. /**
  27. * These are actually wrappers onto Escher drawings. Make use of
  28. * the DDF classes to do useful things with them.
  29. * For now, creates a tree of the Escher records, and then creates any
  30. * PowerPoint (hslf) records found within the EscherTextboxRecord
  31. * (msofbtClientTextbox) records.
  32. * Also provides easy access to the EscherTextboxRecords, so that their
  33. * text may be extracted and used in Sheets
  34. *
  35. * @author Nick Burch
  36. */
  37. // For now, pretending to be an atom. Might not always be, but that
  38. // would require a wrapping class
  39. public final class PPDrawing extends RecordAtom {
  40. private byte[] _header;
  41. private long _type;
  42. private EscherRecord[] childRecords;
  43. private EscherTextboxWrapper[] textboxWrappers;
  44. //cached EscherDgRecord
  45. private EscherDgRecord dg;
  46. /**
  47. * Get access to the underlying Escher Records
  48. */
  49. public EscherRecord[] getEscherRecords() { return childRecords; }
  50. /**
  51. * Get access to the atoms inside Textboxes
  52. */
  53. public EscherTextboxWrapper[] getTextboxWrappers() { return textboxWrappers; }
  54. /* ******************** record stuff follows ********************** */
  55. /**
  56. * Sets everything up, groks the escher etc
  57. */
  58. protected PPDrawing(byte[] source, int start, int len) {
  59. // Get the header
  60. _header = new byte[8];
  61. System.arraycopy(source,start,_header,0,8);
  62. // Get the type
  63. _type = LittleEndian.getUShort(_header,2);
  64. // Get the contents for now
  65. final byte[] contents = new byte[len];
  66. System.arraycopy(source,start,contents,0,len);
  67. // Build up a tree of Escher records contained within
  68. final DefaultEscherRecordFactory erf = new DefaultEscherRecordFactory();
  69. final Vector<EscherRecord> escherChildren = new Vector<EscherRecord>();
  70. findEscherChildren(erf, contents, 8, len-8, escherChildren);
  71. this.childRecords = (EscherRecord[]) escherChildren.toArray(new EscherRecord[escherChildren.size()]);
  72. if (1 == this.childRecords.length && (short)0xf002 == this.childRecords[0].getRecordId() && this.childRecords[0] instanceof EscherContainerRecord) {
  73. this.textboxWrappers = findInDgContainer((EscherContainerRecord) this.childRecords[0]);
  74. } else {
  75. // Find and EscherTextboxRecord's, and wrap them up
  76. final Vector<EscherTextboxWrapper> textboxes = new Vector<EscherTextboxWrapper>();
  77. findEscherTextboxRecord(childRecords, textboxes);
  78. this.textboxWrappers = (EscherTextboxWrapper[]) textboxes.toArray(new EscherTextboxWrapper[textboxes.size()]);
  79. }
  80. }
  81. private EscherTextboxWrapper[] findInDgContainer(final EscherContainerRecord escherContainerF002) {
  82. final List<EscherTextboxWrapper> found = new LinkedList<EscherTextboxWrapper>();
  83. final EscherContainerRecord SpgrContainer = findFirstEscherContainerRecordOfType((short)0xf003, escherContainerF002);
  84. final EscherContainerRecord[] escherContainersF004 = findAllEscherContainerRecordOfType((short)0xf004, SpgrContainer);
  85. for (EscherContainerRecord spContainer : escherContainersF004) {
  86. StyleTextProp9Atom nineAtom = findInSpContainer(spContainer);
  87. EscherSpRecord sp = null;
  88. final EscherRecord escherContainerF00A = findFirstEscherRecordOfType((short)0xf00a, spContainer);
  89. if (null != escherContainerF00A) {
  90. if (escherContainerF00A instanceof EscherSpRecord) {
  91. sp = (EscherSpRecord) escherContainerF00A;
  92. }
  93. }
  94. final EscherRecord escherContainerF00D = findFirstEscherRecordOfType((short)0xf00d, spContainer);
  95. if (null == escherContainerF00D) { continue; }
  96. if (escherContainerF00D instanceof EscherTextboxRecord) {
  97. EscherTextboxRecord tbr = (EscherTextboxRecord) escherContainerF00D;
  98. EscherTextboxWrapper w = new EscherTextboxWrapper(tbr);
  99. w.setStyleTextProp9Atom(nineAtom);
  100. if (null != sp) {
  101. w.setShapeId(sp.getShapeId());
  102. }
  103. found.add(w);
  104. }
  105. }
  106. return (EscherTextboxWrapper[]) found.toArray(new EscherTextboxWrapper[found.size()]);
  107. }
  108. private StyleTextProp9Atom findInSpContainer(final EscherContainerRecord spContainer) {
  109. final EscherContainerRecord escherContainerF011 = findFirstEscherContainerRecordOfType((short)0xf011, spContainer);
  110. if (null == escherContainerF011) { return null; }
  111. final EscherContainerRecord escherContainer1388 = findFirstEscherContainerRecordOfType((short)0x1388, escherContainerF011);
  112. if (null == escherContainer1388) { return null; }
  113. final EscherContainerRecord escherContainer138A = findFirstEscherContainerRecordOfType((short)0x138A, escherContainer1388);
  114. if (null == escherContainer138A) { return null; }
  115. int size = escherContainer138A.getChildRecords().size();
  116. if (2 != size) { return null; }
  117. final Record r0 = buildFromUnknownEscherRecord((UnknownEscherRecord) escherContainer138A.getChild(0));
  118. final Record r1 = buildFromUnknownEscherRecord((UnknownEscherRecord) escherContainer138A.getChild(1));
  119. if (!(r0 instanceof CString)) { return null; }
  120. if (!("___PPT9".equals(((CString) r0).getText()))) { return null; };
  121. if (!(r1 instanceof BinaryTagDataBlob )) { return null; }
  122. final BinaryTagDataBlob blob = (BinaryTagDataBlob) r1;
  123. if (1 != blob.getChildRecords().length) { return null; }
  124. return (StyleTextProp9Atom) blob.findFirstOfType(0x0FACL);
  125. }
  126. /**
  127. * Creates a new, empty, PPDrawing (typically for use with a new Slide
  128. * or Notes)
  129. */
  130. public PPDrawing(){
  131. _header = new byte[8];
  132. LittleEndian.putUShort(_header, 0, 15);
  133. LittleEndian.putUShort(_header, 2, RecordTypes.PPDrawing.typeID);
  134. LittleEndian.putInt(_header, 4, 0);
  135. textboxWrappers = new EscherTextboxWrapper[]{};
  136. create();
  137. }
  138. /**
  139. * Tree walking way of finding Escher Child Records
  140. */
  141. private void findEscherChildren(DefaultEscherRecordFactory erf, byte[] source, int startPos, int lenToGo, Vector<EscherRecord> found) {
  142. int escherBytes = LittleEndian.getInt( source, startPos + 4 ) + 8;
  143. // Find the record
  144. EscherRecord r = erf.createRecord(source,startPos);
  145. // Fill it in
  146. r.fillFields( source, startPos, erf );
  147. // Save it
  148. found.add(r);
  149. // Wind on
  150. int size = r.getRecordSize();
  151. if(size < 8) {
  152. logger.log(POILogger.WARN, "Hit short DDF record at " + startPos + " - " + size);
  153. }
  154. /**
  155. * Sanity check. Always advance the cursor by the correct value.
  156. *
  157. * getRecordSize() must return exactly the same number of bytes that was written in fillFields.
  158. * Sometimes it is not so, see an example in bug #44770. Most likely reason is that one of ddf records calculates wrong size.
  159. */
  160. if(size != escherBytes){
  161. logger.log(POILogger.WARN, "Record length=" + escherBytes + " but getRecordSize() returned " + r.getRecordSize() + "; record: " + r.getClass());
  162. size = escherBytes;
  163. }
  164. startPos += size;
  165. lenToGo -= size;
  166. if(lenToGo >= 8) {
  167. findEscherChildren(erf, source, startPos, lenToGo, found);
  168. }
  169. }
  170. /**
  171. * Look for EscherTextboxRecords
  172. */
  173. private void findEscherTextboxRecord(EscherRecord[] toSearch, Vector<EscherTextboxWrapper> found) {
  174. for(int i=0; i<toSearch.length; i++) {
  175. if(toSearch[i] instanceof EscherTextboxRecord) {
  176. EscherTextboxRecord tbr = (EscherTextboxRecord)toSearch[i];
  177. EscherTextboxWrapper w = new EscherTextboxWrapper(tbr);
  178. found.add(w);
  179. for (int j = i; j >= 0; j--) {
  180. if(toSearch[j] instanceof EscherSpRecord){
  181. EscherSpRecord sp = (EscherSpRecord)toSearch[j];
  182. w.setShapeId(sp.getShapeId());
  183. break;
  184. }
  185. }
  186. } else {
  187. // If it has children, walk them
  188. if(toSearch[i].isContainerRecord()) {
  189. List<EscherRecord> childrenL = toSearch[i].getChildRecords();
  190. EscherRecord[] children = new EscherRecord[childrenL.size()];
  191. childrenL.toArray(children);
  192. findEscherTextboxRecord(children,found);
  193. }
  194. }
  195. }
  196. }
  197. /**
  198. * We are type 1036
  199. */
  200. public long getRecordType() { return _type; }
  201. /**
  202. * We're pretending to be an atom, so return null
  203. */
  204. public Record[] getChildRecords() { return null; }
  205. /**
  206. * Write the contents of the record back, so it can be written
  207. * to disk
  208. * Walks the escher layer to get the contents
  209. */
  210. public void writeOut(OutputStream out) throws IOException {
  211. // Ensure the escher layer reflects the text changes
  212. for(int i=0; i<textboxWrappers.length; i++) {
  213. textboxWrappers[i].writeOut(null);
  214. }
  215. // Find the new size of the escher children;
  216. int newSize = 0;
  217. for(int i=0; i<childRecords.length; i++) {
  218. newSize += childRecords[i].getRecordSize();
  219. }
  220. // Update the size (header bytes 5-8)
  221. LittleEndian.putInt(_header,4,newSize);
  222. // Write out our header
  223. out.write(_header);
  224. // Now grab the children's data
  225. byte[] b = new byte[newSize];
  226. int done = 0;
  227. for(int i=0; i<childRecords.length; i++) {
  228. int written = childRecords[i].serialize( done, b );
  229. done += written;
  230. }
  231. // Finally, write out the children
  232. out.write(b);
  233. }
  234. /**
  235. * Create the Escher records associated with a new PPDrawing
  236. */
  237. private void create(){
  238. EscherContainerRecord dgContainer = new EscherContainerRecord();
  239. dgContainer.setRecordId( EscherContainerRecord.DG_CONTAINER );
  240. dgContainer.setOptions((short)15);
  241. EscherDgRecord dg = new EscherDgRecord();
  242. dg.setOptions((short)16);
  243. dg.setNumShapes(1);
  244. dgContainer.addChildRecord(dg);
  245. EscherContainerRecord spgrContainer = new EscherContainerRecord();
  246. spgrContainer.setOptions((short)15);
  247. spgrContainer.setRecordId(EscherContainerRecord.SPGR_CONTAINER);
  248. EscherContainerRecord spContainer = new EscherContainerRecord();
  249. spContainer.setOptions((short)15);
  250. spContainer.setRecordId(EscherContainerRecord.SP_CONTAINER);
  251. EscherSpgrRecord spgr = new EscherSpgrRecord();
  252. spgr.setOptions((short)1);
  253. spContainer.addChildRecord(spgr);
  254. EscherSpRecord sp = new EscherSpRecord();
  255. sp.setOptions((short)((ShapeTypes.NotPrimitive << 4) + 2));
  256. sp.setFlags(EscherSpRecord.FLAG_PATRIARCH | EscherSpRecord.FLAG_GROUP);
  257. spContainer.addChildRecord(sp);
  258. spgrContainer.addChildRecord(spContainer);
  259. dgContainer.addChildRecord(spgrContainer);
  260. spContainer = new EscherContainerRecord();
  261. spContainer.setOptions((short)15);
  262. spContainer.setRecordId(EscherContainerRecord.SP_CONTAINER);
  263. sp = new EscherSpRecord();
  264. sp.setOptions((short)((ShapeTypes.Rectangle << 4) + 2));
  265. sp.setFlags(EscherSpRecord.FLAG_BACKGROUND | EscherSpRecord.FLAG_HASSHAPETYPE);
  266. spContainer.addChildRecord(sp);
  267. EscherOptRecord opt = new EscherOptRecord();
  268. opt.setRecordId(EscherOptRecord.RECORD_ID);
  269. opt.addEscherProperty(new EscherRGBProperty(EscherProperties.FILL__FILLCOLOR, 134217728));
  270. opt.addEscherProperty(new EscherRGBProperty(EscherProperties.FILL__FILLBACKCOLOR, 134217733));
  271. opt.addEscherProperty(new EscherSimpleProperty(EscherProperties.FILL__RECTRIGHT, 10064750));
  272. opt.addEscherProperty(new EscherSimpleProperty(EscherProperties.FILL__RECTBOTTOM, 7778750));
  273. opt.addEscherProperty(new EscherBoolProperty(EscherProperties.FILL__NOFILLHITTEST, 1179666));
  274. opt.addEscherProperty(new EscherBoolProperty(EscherProperties.LINESTYLE__NOLINEDRAWDASH, 524288));
  275. opt.addEscherProperty(new EscherSimpleProperty(EscherProperties.SHAPE__BLACKANDWHITESETTINGS, 9));
  276. opt.addEscherProperty(new EscherSimpleProperty(EscherProperties.SHAPE__BACKGROUNDSHAPE, 65537));
  277. spContainer.addChildRecord(opt);
  278. dgContainer.addChildRecord(spContainer);
  279. childRecords = new EscherRecord[]{
  280. dgContainer
  281. };
  282. }
  283. /**
  284. * Add a new EscherTextboxWrapper to this <code>PPDrawing</code>.
  285. */
  286. public void addTextboxWrapper(EscherTextboxWrapper txtbox){
  287. EscherTextboxWrapper[] tw = new EscherTextboxWrapper[textboxWrappers.length + 1];
  288. System.arraycopy(textboxWrappers, 0, tw, 0, textboxWrappers.length);
  289. tw[textboxWrappers.length] = txtbox;
  290. textboxWrappers = tw;
  291. }
  292. /**
  293. * Return EscherDgRecord which keeps track of the number of shapes and shapeId in this drawing group
  294. *
  295. * @return EscherDgRecord
  296. */
  297. public EscherDgRecord getEscherDgRecord(){
  298. if(dg == null){
  299. EscherContainerRecord dgContainer = (EscherContainerRecord)childRecords[0];
  300. for(Iterator<EscherRecord> it = dgContainer.getChildIterator(); it.hasNext();){
  301. EscherRecord r = it.next();
  302. if(r instanceof EscherDgRecord){
  303. dg = (EscherDgRecord)r;
  304. break;
  305. }
  306. }
  307. }
  308. return dg;
  309. }
  310. protected EscherContainerRecord findFirstEscherContainerRecordOfType(short type, EscherContainerRecord parent) {
  311. if (null == parent) { return null; }
  312. final List<EscherContainerRecord> children = parent.getChildContainers();
  313. for (EscherContainerRecord child : children) {
  314. if (type == child.getRecordId()) {
  315. return child;
  316. }
  317. }
  318. return null;
  319. }
  320. protected EscherRecord findFirstEscherRecordOfType(short type, EscherContainerRecord parent) {
  321. if (null == parent) { return null; }
  322. final List<EscherRecord> children = parent.getChildRecords();
  323. for (EscherRecord child : children) {
  324. if (type == child.getRecordId()) {
  325. return child;
  326. }
  327. }
  328. return null;
  329. }
  330. protected EscherContainerRecord[] findAllEscherContainerRecordOfType(short type, EscherContainerRecord parent) {
  331. if (null == parent) { return new EscherContainerRecord[0]; }
  332. final List<EscherContainerRecord> children = parent.getChildContainers();
  333. final List<EscherContainerRecord> result = new LinkedList<EscherContainerRecord>();
  334. for (EscherContainerRecord child : children) {
  335. if (type == child.getRecordId()) {
  336. result.add(child);
  337. }
  338. }
  339. return (EscherContainerRecord[]) result.toArray(new EscherContainerRecord[result.size()]);
  340. }
  341. protected Record buildFromUnknownEscherRecord(UnknownEscherRecord unknown) {
  342. byte[] bingo = unknown.getData();
  343. byte[] restoredRecord = new byte[8 + bingo.length];
  344. System.arraycopy(bingo, 0, restoredRecord, 8, bingo.length);
  345. short recordVersion = unknown.getVersion();
  346. short recordId = unknown.getRecordId();
  347. int recordLength = unknown.getRecordSize();
  348. LittleEndian.putShort(restoredRecord, 0, recordVersion);
  349. LittleEndian.putShort(restoredRecord, 2, recordId);
  350. LittleEndian.putInt(restoredRecord, 4, recordLength);
  351. return Record.createRecordForType(recordId, restoredRecord, 0, restoredRecord.length);
  352. }
  353. public StyleTextProp9Atom[] getNumberedListInfo() {
  354. final List<StyleTextProp9Atom> result = new LinkedList<StyleTextProp9Atom>();
  355. EscherRecord[] escherRecords = this.getEscherRecords();
  356. for (EscherRecord escherRecord : escherRecords) {
  357. if (escherRecord instanceof EscherContainerRecord && (short)0xf002 == escherRecord.getRecordId()) {
  358. EscherContainerRecord escherContainerF002 = (EscherContainerRecord) escherRecord;
  359. final EscherContainerRecord escherContainerF003 = findFirstEscherContainerRecordOfType((short)0xf003, escherContainerF002);
  360. final EscherContainerRecord[] escherContainersF004 = findAllEscherContainerRecordOfType((short)0xf004, escherContainerF003);
  361. for (EscherContainerRecord containerF004 : escherContainersF004) {
  362. final EscherContainerRecord escherContainerF011 = findFirstEscherContainerRecordOfType((short)0xf011, containerF004);
  363. if (null == escherContainerF011) { continue; }
  364. final EscherContainerRecord escherContainer1388 = findFirstEscherContainerRecordOfType((short)0x1388, escherContainerF011);
  365. if (null == escherContainer1388) { continue; }
  366. final EscherContainerRecord escherContainer138A = findFirstEscherContainerRecordOfType((short)0x138A, escherContainer1388);
  367. if (null == escherContainer138A) { continue; }
  368. int size = escherContainer138A.getChildRecords().size();
  369. if (2 != size) { continue; }
  370. final Record r0 = buildFromUnknownEscherRecord((UnknownEscherRecord) escherContainer138A.getChild(0));
  371. final Record r1 = buildFromUnknownEscherRecord((UnknownEscherRecord) escherContainer138A.getChild(1));
  372. if (!(r0 instanceof CString)) { continue; }
  373. if (!("___PPT9".equals(((CString) r0).getText()))) { continue; };
  374. if (!(r1 instanceof BinaryTagDataBlob )) { continue; }
  375. final BinaryTagDataBlob blob = (BinaryTagDataBlob) r1;
  376. if (1 != blob.getChildRecords().length) { continue; }
  377. result.add((StyleTextProp9Atom) blob.findFirstOfType(0x0FACL));
  378. }
  379. }
  380. }
  381. return (StyleTextProp9Atom[]) result.toArray(new StyleTextProp9Atom[result.size()]);
  382. }
  383. }