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 14KB

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