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.

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