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.

HSLFSheet.java 15KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462
  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.usermodel;
  16. import java.awt.Graphics2D;
  17. import java.awt.geom.Rectangle2D;
  18. import java.util.ArrayList;
  19. import java.util.Iterator;
  20. import java.util.List;
  21. import org.apache.poi.ddf.EscherContainerRecord;
  22. import org.apache.poi.ddf.EscherDgRecord;
  23. import org.apache.poi.ddf.EscherDggRecord;
  24. import org.apache.poi.ddf.EscherRecord;
  25. import org.apache.poi.hslf.exceptions.HSLFException;
  26. import org.apache.poi.hslf.record.CString;
  27. import org.apache.poi.hslf.record.ColorSchemeAtom;
  28. import org.apache.poi.hslf.record.OEPlaceholderAtom;
  29. import org.apache.poi.hslf.record.PPDrawing;
  30. import org.apache.poi.hslf.record.RecordContainer;
  31. import org.apache.poi.hslf.record.RecordTypes;
  32. import org.apache.poi.hslf.record.RoundTripHFPlaceholder12;
  33. import org.apache.poi.hslf.record.SheetContainer;
  34. import org.apache.poi.sl.draw.DrawFactory;
  35. import org.apache.poi.sl.draw.Drawable;
  36. import org.apache.poi.sl.usermodel.PictureData;
  37. import org.apache.poi.sl.usermodel.ShapeType;
  38. import org.apache.poi.sl.usermodel.Sheet;
  39. import org.apache.poi.util.Internal;
  40. /**
  41. * This class defines the common format of "Sheets" in a powerpoint
  42. * document. Such sheets could be Slides, Notes, Master etc
  43. *
  44. * @author Nick Burch
  45. * @author Yegor Kozlov
  46. */
  47. public abstract class HSLFSheet implements HSLFShapeContainer, Sheet<HSLFShape,HSLFTextParagraph> {
  48. /**
  49. * The <code>SlideShow</code> we belong to
  50. */
  51. private HSLFSlideShow _slideShow;
  52. /**
  53. * Sheet background
  54. */
  55. private HSLFBackground _background;
  56. /**
  57. * Record container that holds sheet data.
  58. * For slides it is org.apache.poi.hslf.record.Slide,
  59. * for notes it is org.apache.poi.hslf.record.Notes,
  60. * for slide masters it is org.apache.poi.hslf.record.SlideMaster, etc.
  61. */
  62. private SheetContainer _container;
  63. private int _sheetNo;
  64. public HSLFSheet(SheetContainer container, int sheetNo) {
  65. _container = container;
  66. _sheetNo = sheetNo;
  67. }
  68. /**
  69. * Returns an array of all the TextRuns in the sheet.
  70. */
  71. public abstract List<List<HSLFTextParagraph>> getTextParagraphs();
  72. /**
  73. * Returns the (internal, RefID based) sheet number, as used
  74. * to in PersistPtr stuff.
  75. */
  76. public int _getSheetRefId() {
  77. return _container.getSheetId();
  78. }
  79. /**
  80. * Returns the (internal, SlideIdentifier based) sheet number, as used
  81. * to reference this sheet from other records.
  82. */
  83. public int _getSheetNumber() {
  84. return _sheetNo;
  85. }
  86. /**
  87. * Fetch the PPDrawing from the underlying record
  88. */
  89. public PPDrawing getPPDrawing() {
  90. return _container.getPPDrawing();
  91. }
  92. /**
  93. * Fetch the SlideShow we're attached to
  94. */
  95. public HSLFSlideShow getSlideShow() {
  96. return _slideShow;
  97. }
  98. /**
  99. * Return record container for this sheet
  100. */
  101. public SheetContainer getSheetContainer() {
  102. return _container;
  103. }
  104. /**
  105. * Set the SlideShow we're attached to.
  106. * Also passes it on to our child text paragraphs
  107. */
  108. @Internal
  109. protected void setSlideShow(HSLFSlideShow ss) {
  110. if (_slideShow != null) {
  111. throw new HSLFException("Can't change existing slideshow reference");
  112. }
  113. _slideShow = ss;
  114. List<List<HSLFTextParagraph>> trs = getTextParagraphs();
  115. if (trs == null) return;
  116. for (List<HSLFTextParagraph> ltp : trs) {
  117. HSLFTextParagraph.supplySheet(ltp, this);
  118. }
  119. }
  120. /**
  121. * Returns all shapes contained in this Sheet
  122. *
  123. * @return all shapes contained in this Sheet (Slide or Notes)
  124. */
  125. @Override
  126. public List<HSLFShape> getShapes() {
  127. PPDrawing ppdrawing = getPPDrawing();
  128. EscherContainerRecord dg = ppdrawing.getDgContainer();
  129. EscherContainerRecord spgr = null;
  130. for (Iterator<EscherRecord> it = dg.getChildIterator(); it.hasNext();) {
  131. EscherRecord rec = it.next();
  132. if (rec.getRecordId() == EscherContainerRecord.SPGR_CONTAINER) {
  133. spgr = (EscherContainerRecord) rec;
  134. break;
  135. }
  136. }
  137. if (spgr == null) {
  138. throw new IllegalStateException("spgr not found");
  139. }
  140. List<HSLFShape> shapeList = new ArrayList<HSLFShape>();
  141. Iterator<EscherRecord> it = spgr.getChildIterator();
  142. if (it.hasNext()) {
  143. // skip first item
  144. it.next();
  145. }
  146. for (; it.hasNext();) {
  147. EscherContainerRecord sp = (EscherContainerRecord) it.next();
  148. HSLFShape sh = HSLFShapeFactory.createShape(sp, null);
  149. sh.setSheet(this);
  150. shapeList.add(sh);
  151. }
  152. return shapeList;
  153. }
  154. /**
  155. * Add a new Shape to this Slide
  156. *
  157. * @param shape - the Shape to add
  158. */
  159. public void addShape(HSLFShape shape) {
  160. PPDrawing ppdrawing = getPPDrawing();
  161. EscherContainerRecord dgContainer = ppdrawing.getDgContainer();
  162. EscherContainerRecord spgr = (EscherContainerRecord) HSLFShape.getEscherChild(dgContainer, EscherContainerRecord.SPGR_CONTAINER);
  163. spgr.addChildRecord(shape.getSpContainer());
  164. shape.setSheet(this);
  165. shape.setShapeId(allocateShapeId());
  166. shape.afterInsert(this);
  167. }
  168. /**
  169. * Allocates new shape id for the new drawing group id.
  170. *
  171. * @return a new shape id.
  172. */
  173. public int allocateShapeId()
  174. {
  175. EscherDggRecord dgg = _slideShow.getDocumentRecord().getPPDrawingGroup().getEscherDggRecord();
  176. EscherDgRecord dg = _container.getPPDrawing().getEscherDgRecord();
  177. dgg.setNumShapesSaved( dgg.getNumShapesSaved() + 1 );
  178. // Add to existing cluster if space available
  179. for (int i = 0; i < dgg.getFileIdClusters().length; i++)
  180. {
  181. EscherDggRecord.FileIdCluster c = dgg.getFileIdClusters()[i];
  182. if (c.getDrawingGroupId() == dg.getDrawingGroupId() && c.getNumShapeIdsUsed() != 1024)
  183. {
  184. int result = c.getNumShapeIdsUsed() + (1024 * (i+1));
  185. c.incrementShapeId();
  186. dg.setNumShapes( dg.getNumShapes() + 1 );
  187. dg.setLastMSOSPID( result );
  188. if (result >= dgg.getShapeIdMax())
  189. dgg.setShapeIdMax( result + 1 );
  190. return result;
  191. }
  192. }
  193. // Create new cluster
  194. dgg.addCluster( dg.getDrawingGroupId(), 0, false );
  195. dgg.getFileIdClusters()[dgg.getFileIdClusters().length-1].incrementShapeId();
  196. dg.setNumShapes( dg.getNumShapes() + 1 );
  197. int result = (1024 * dgg.getFileIdClusters().length);
  198. dg.setLastMSOSPID( result );
  199. if (result >= dgg.getShapeIdMax())
  200. dgg.setShapeIdMax( result + 1 );
  201. return result;
  202. }
  203. /**
  204. * Removes the specified shape from this sheet.
  205. *
  206. * @param shape shape to be removed from this sheet, if present.
  207. * @return <tt>true</tt> if the shape was deleted.
  208. */
  209. public boolean removeShape(HSLFShape shape) {
  210. PPDrawing ppdrawing = getPPDrawing();
  211. EscherContainerRecord dg = ppdrawing.getDgContainer();
  212. EscherContainerRecord spgr = dg.getChildById(EscherContainerRecord.SPGR_CONTAINER);
  213. if(spgr == null) {
  214. return false;
  215. }
  216. List<EscherRecord> lst = spgr.getChildRecords();
  217. boolean result = lst.remove(shape.getSpContainer());
  218. spgr.setChildRecords(lst);
  219. return result;
  220. }
  221. /**
  222. * Called by SlideShow ater a new sheet is created
  223. */
  224. public void onCreate(){
  225. }
  226. /**
  227. * Return the master sheet .
  228. */
  229. public abstract HSLFMasterSheet getMasterSheet();
  230. /**
  231. * Color scheme for this sheet.
  232. */
  233. public ColorSchemeAtom getColorScheme() {
  234. return _container.getColorScheme();
  235. }
  236. /**
  237. * Returns the background shape for this sheet.
  238. *
  239. * @return the background shape for this sheet.
  240. */
  241. public HSLFBackground getBackground() {
  242. if (_background == null) {
  243. PPDrawing ppdrawing = getPPDrawing();
  244. EscherContainerRecord dg = ppdrawing.getDgContainer();
  245. EscherContainerRecord spContainer = dg.getChildById(EscherContainerRecord.SP_CONTAINER);
  246. _background = new HSLFBackground(spContainer, null);
  247. _background.setSheet(this);
  248. }
  249. return _background;
  250. }
  251. @Override
  252. public void draw(Graphics2D graphics) {
  253. DrawFactory drawFact = DrawFactory.getInstance(graphics);
  254. Drawable draw = drawFact.getDrawable(this);
  255. draw.draw(graphics);
  256. }
  257. /**
  258. * Subclasses should call this method and update the array of text runs
  259. * when a text shape is added
  260. *
  261. * @param shape
  262. */
  263. protected void onAddTextShape(HSLFTextShape shape) {
  264. }
  265. /**
  266. * Return placeholder by text type
  267. *
  268. * @param type type of text, See {@link org.apache.poi.hslf.record.TextHeaderAtom}
  269. * @return <code>TextShape</code> or <code>null</code>
  270. */
  271. public HSLFTextShape getPlaceholderByTextType(int type){
  272. for (HSLFShape shape : getShapes()) {
  273. if(shape instanceof HSLFTextShape){
  274. HSLFTextShape tx = (HSLFTextShape)shape;
  275. if (tx.getRunType() == type) {
  276. return tx;
  277. }
  278. }
  279. }
  280. return null;
  281. }
  282. /**
  283. * Search text placeholer by its type
  284. *
  285. * @param type type of placeholder to search. See {@link org.apache.poi.hslf.record.OEPlaceholderAtom}
  286. * @return <code>TextShape</code> or <code>null</code>
  287. */
  288. public HSLFTextShape getPlaceholder(int type){
  289. for (HSLFShape shape : getShapes()) {
  290. if(shape instanceof HSLFTextShape){
  291. HSLFTextShape tx = (HSLFTextShape)shape;
  292. int placeholderId = 0;
  293. OEPlaceholderAtom oep = tx.getPlaceholderAtom();
  294. if(oep != null) {
  295. placeholderId = oep.getPlaceholderId();
  296. } else {
  297. //special case for files saved in Office 2007
  298. RoundTripHFPlaceholder12 hldr = tx.getClientDataRecord(RecordTypes.RoundTripHFPlaceholder12.typeID);
  299. if(hldr != null) placeholderId = hldr.getPlaceholderId();
  300. }
  301. if(placeholderId == type){
  302. return tx;
  303. }
  304. }
  305. }
  306. return null;
  307. }
  308. /**
  309. * Return programmable tag associated with this sheet, e.g. <code>___PPT12</code>.
  310. *
  311. * @return programmable tag associated with this sheet.
  312. */
  313. public String getProgrammableTag(){
  314. String tag = null;
  315. RecordContainer progTags = (RecordContainer)
  316. getSheetContainer().findFirstOfType(
  317. RecordTypes.ProgTags.typeID
  318. );
  319. if(progTags != null) {
  320. RecordContainer progBinaryTag = (RecordContainer)
  321. progTags.findFirstOfType(
  322. RecordTypes.ProgBinaryTag.typeID
  323. );
  324. if(progBinaryTag != null) {
  325. CString binaryTag = (CString)
  326. progBinaryTag.findFirstOfType(
  327. RecordTypes.CString.typeID
  328. );
  329. if(binaryTag != null) tag = binaryTag.getText();
  330. }
  331. }
  332. return tag;
  333. }
  334. public Iterator<HSLFShape> iterator() {
  335. return getShapes().iterator();
  336. }
  337. /**
  338. * @return whether shapes on the master sheet should be shown. By default master graphics is turned off.
  339. * Sheets that support the notion of master (slide, slideLayout) should override it and
  340. * check this setting
  341. */
  342. public boolean getFollowMasterGraphics() {
  343. return false;
  344. }
  345. @Override
  346. public HSLFTextBox createTextBox() {
  347. HSLFTextBox s = new HSLFTextBox();
  348. s.setHorizontalCentered(true);
  349. s.setAnchor(new Rectangle2D.Double(0, 0, 100, 100));
  350. addShape(s);
  351. return s;
  352. }
  353. @Override
  354. public HSLFAutoShape createAutoShape() {
  355. HSLFAutoShape s = new HSLFAutoShape(ShapeType.RECT);
  356. s.setHorizontalCentered(true);
  357. s.setAnchor(new Rectangle2D.Double(0, 0, 100, 100));
  358. addShape(s);
  359. return s;
  360. }
  361. @Override
  362. public HSLFFreeformShape createFreeform() {
  363. HSLFFreeformShape s = new HSLFFreeformShape();
  364. s.setHorizontalCentered(true);
  365. s.setAnchor(new Rectangle2D.Double(0, 0, 100, 100));
  366. addShape(s);
  367. return s;
  368. }
  369. @Override
  370. public HSLFConnectorShape createConnector() {
  371. HSLFConnectorShape s = new HSLFConnectorShape();
  372. s.setAnchor(new Rectangle2D.Double(0, 0, 100, 100));
  373. addShape(s);
  374. return s;
  375. }
  376. @Override
  377. public HSLFGroupShape createGroup() {
  378. HSLFGroupShape s = new HSLFGroupShape();
  379. s.setAnchor(new Rectangle2D.Double(0, 0, 100, 100));
  380. addShape(s);
  381. return s;
  382. }
  383. @Override
  384. public HSLFPictureShape createPicture(PictureData pictureData) {
  385. if (!(pictureData instanceof HSLFPictureData)) {
  386. throw new IllegalArgumentException("pictureData needs to be of type HSLFPictureData");
  387. }
  388. HSLFPictureShape s = new HSLFPictureShape((HSLFPictureData)pictureData);
  389. s.setAnchor(new Rectangle2D.Double(0, 0, 100, 100));
  390. addShape(s);
  391. return s;
  392. }
  393. @Override
  394. public HSLFTable createTable(int numRows, int numCols) {
  395. if (numRows < 1 || numCols < 1) {
  396. throw new IllegalArgumentException("numRows and numCols must be greater than 0");
  397. }
  398. HSLFTable s = new HSLFTable(numRows,numCols);
  399. // anchor is set in constructor based on numRows/numCols
  400. addShape(s);
  401. return s;
  402. }
  403. }