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.

HSLFSlide.java 17KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504
  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.util.ArrayList;
  18. import java.util.List;
  19. import org.apache.poi.ddf.EscherContainerRecord;
  20. import org.apache.poi.ddf.EscherDgRecord;
  21. import org.apache.poi.ddf.EscherDggRecord;
  22. import org.apache.poi.ddf.EscherSpRecord;
  23. import org.apache.poi.hslf.exceptions.HSLFException;
  24. import org.apache.poi.hslf.model.HeadersFooters;
  25. import org.apache.poi.hslf.record.ColorSchemeAtom;
  26. import org.apache.poi.hslf.record.Comment2000;
  27. import org.apache.poi.hslf.record.EscherTextboxWrapper;
  28. import org.apache.poi.hslf.record.HeadersFootersContainer;
  29. import org.apache.poi.hslf.record.Record;
  30. import org.apache.poi.hslf.record.RecordContainer;
  31. import org.apache.poi.hslf.record.RecordTypes;
  32. import org.apache.poi.hslf.record.SSSlideInfoAtom;
  33. import org.apache.poi.hslf.record.SlideAtom;
  34. import org.apache.poi.hslf.record.SlideAtomLayout.SlideLayoutType;
  35. import org.apache.poi.hslf.record.SlideListWithText.SlideAtomsSet;
  36. import org.apache.poi.hslf.record.StyleTextProp9Atom;
  37. import org.apache.poi.hslf.record.TextHeaderAtom;
  38. import org.apache.poi.sl.draw.DrawFactory;
  39. import org.apache.poi.sl.draw.Drawable;
  40. import org.apache.poi.sl.usermodel.Notes;
  41. import org.apache.poi.sl.usermodel.Placeholder;
  42. import org.apache.poi.sl.usermodel.ShapeType;
  43. import org.apache.poi.sl.usermodel.Slide;
  44. /**
  45. * This class represents a slide in a PowerPoint Document. It allows
  46. * access to the text within, and the layout. For now, it only does
  47. * the text side of things though
  48. *
  49. * @author Nick Burch
  50. * @author Yegor Kozlov
  51. */
  52. public final class HSLFSlide extends HSLFSheet implements Slide<HSLFShape,HSLFTextParagraph> {
  53. private int _slideNo;
  54. private SlideAtomsSet _atomSet;
  55. private final List<List<HSLFTextParagraph>> _paragraphs = new ArrayList<>();
  56. private HSLFNotes _notes; // usermodel needs to set this
  57. /**
  58. * Constructs a Slide from the Slide record, and the SlideAtomsSet
  59. * containing the text.
  60. * Initializes TextRuns, to provide easier access to the text
  61. *
  62. * @param slide the Slide record we're based on
  63. * @param notes the Notes sheet attached to us
  64. * @param atomSet the SlideAtomsSet to get the text from
  65. */
  66. public HSLFSlide(org.apache.poi.hslf.record.Slide slide, HSLFNotes notes, SlideAtomsSet atomSet, int slideIdentifier, int slideNumber) {
  67. super(slide, slideIdentifier);
  68. _notes = notes;
  69. _atomSet = atomSet;
  70. _slideNo = slideNumber;
  71. // For the text coming in from the SlideAtomsSet:
  72. // Build up TextRuns from pairs of TextHeaderAtom and
  73. // one of TextBytesAtom or TextCharsAtom
  74. if (_atomSet != null && _atomSet.getSlideRecords().length > 0) {
  75. // Grab text from SlideListWithTexts entries
  76. _paragraphs.addAll(HSLFTextParagraph.findTextParagraphs(_atomSet.getSlideRecords()));
  77. if (_paragraphs.isEmpty()) {
  78. throw new HSLFException("No text records found for slide");
  79. }
  80. }
  81. // Grab text from slide's PPDrawing
  82. for (List<HSLFTextParagraph> l : HSLFTextParagraph.findTextParagraphs(getPPDrawing(), this)) {
  83. if (!_paragraphs.contains(l)) {
  84. _paragraphs.add(l);
  85. }
  86. }
  87. }
  88. /**
  89. * Create a new Slide instance
  90. * @param sheetNumber The internal number of the sheet, as used by PersistPtrHolder
  91. * @param slideNumber The user facing number of the sheet
  92. */
  93. public HSLFSlide(int sheetNumber, int sheetRefId, int slideNumber){
  94. super(new org.apache.poi.hslf.record.Slide(), sheetNumber);
  95. _slideNo = slideNumber;
  96. getSheetContainer().setSheetId(sheetRefId);
  97. }
  98. /**
  99. * Returns the Notes Sheet for this slide, or null if there isn't one
  100. */
  101. @Override
  102. public HSLFNotes getNotes() {
  103. return _notes;
  104. }
  105. /**
  106. * Sets the Notes that are associated with this. Updates the
  107. * references in the records to point to the new ID
  108. */
  109. @Override
  110. public void setNotes(Notes<HSLFShape,HSLFTextParagraph> notes) {
  111. if (notes != null && !(notes instanceof HSLFNotes)) {
  112. throw new IllegalArgumentException("notes needs to be of type HSLFNotes");
  113. }
  114. _notes = (HSLFNotes)notes;
  115. // Update the Slide Atom's ID of where to point to
  116. SlideAtom sa = getSlideRecord().getSlideAtom();
  117. if(_notes == null) {
  118. // Set to 0
  119. sa.setNotesID(0);
  120. } else {
  121. // Set to the value from the notes' sheet id
  122. sa.setNotesID(_notes._getSheetNumber());
  123. }
  124. }
  125. /**
  126. * Changes the Slide's (external facing) page number.
  127. * @see org.apache.poi.hslf.usermodel.HSLFSlideShow#reorderSlide(int, int)
  128. */
  129. public void setSlideNumber(int newSlideNumber) {
  130. _slideNo = newSlideNumber;
  131. }
  132. /**
  133. * Called by SlideShow ater a new slide is created.
  134. * <p>
  135. * For Slide we need to do the following:
  136. * <li> set id of the drawing group.
  137. * <li> set shapeId for the container descriptor and background
  138. * </p>
  139. */
  140. @Override
  141. public void onCreate(){
  142. //initialize drawing group id
  143. EscherDggRecord dgg = getSlideShow().getDocumentRecord().getPPDrawingGroup().getEscherDggRecord();
  144. EscherContainerRecord dgContainer = getSheetContainer().getPPDrawing().getDgContainer();
  145. EscherDgRecord dg = HSLFShape.getEscherChild(dgContainer, EscherDgRecord.RECORD_ID);
  146. int dgId = dgg.getMaxDrawingGroupId() + 1;
  147. dg.setOptions((short)(dgId << 4));
  148. dgg.setDrawingsSaved(dgg.getDrawingsSaved() + 1);
  149. for (EscherContainerRecord c : dgContainer.getChildContainers()) {
  150. EscherSpRecord spr = null;
  151. switch(c.getRecordId()){
  152. case EscherContainerRecord.SPGR_CONTAINER:
  153. EscherContainerRecord dc = (EscherContainerRecord)c.getChild(0);
  154. spr = dc.getChildById(EscherSpRecord.RECORD_ID);
  155. break;
  156. case EscherContainerRecord.SP_CONTAINER:
  157. spr = c.getChildById(EscherSpRecord.RECORD_ID);
  158. break;
  159. default:
  160. break;
  161. }
  162. if(spr != null) {
  163. spr.setShapeId(allocateShapeId());
  164. }
  165. }
  166. //PPT doen't increment the number of saved shapes for group descriptor and background
  167. dg.setNumShapes(1);
  168. }
  169. /**
  170. * Create a <code>TextBox</code> object that represents the slide's title.
  171. *
  172. * @return <code>TextBox</code> object that represents the slide's title.
  173. */
  174. public HSLFTextBox addTitle() {
  175. HSLFPlaceholder pl = new HSLFPlaceholder();
  176. pl.setShapeType(ShapeType.RECT);
  177. pl.setPlaceholder(Placeholder.TITLE);
  178. pl.setRunType(TextHeaderAtom.TITLE_TYPE);
  179. pl.setText("Click to edit title");
  180. pl.setAnchor(new java.awt.Rectangle(54, 48, 612, 90));
  181. addShape(pl);
  182. return pl;
  183. }
  184. // Complex Accesser methods follow
  185. /**
  186. * <p>
  187. * The title is a run of text of type <code>TextHeaderAtom.CENTER_TITLE_TYPE</code> or
  188. * <code>TextHeaderAtom.TITLE_TYPE</code>
  189. * </p>
  190. *
  191. * @see TextHeaderAtom
  192. */
  193. @Override
  194. public String getTitle(){
  195. for (List<HSLFTextParagraph> tp : getTextParagraphs()) {
  196. if (tp.isEmpty()) {
  197. continue;
  198. }
  199. int type = tp.get(0).getRunType();
  200. switch (type) {
  201. case TextHeaderAtom.CENTER_TITLE_TYPE:
  202. case TextHeaderAtom.TITLE_TYPE:
  203. String str = HSLFTextParagraph.getRawText(tp);
  204. return HSLFTextParagraph.toExternalString(str, type);
  205. }
  206. }
  207. return null;
  208. }
  209. // Simple Accesser methods follow
  210. /**
  211. * Returns an array of all the TextRuns found
  212. */
  213. @Override
  214. public List<List<HSLFTextParagraph>> getTextParagraphs() { return _paragraphs; }
  215. /**
  216. * Returns the (public facing) page number of this slide
  217. */
  218. @Override
  219. public int getSlideNumber() { return _slideNo; }
  220. /**
  221. * Returns the underlying slide record
  222. */
  223. public org.apache.poi.hslf.record.Slide getSlideRecord() {
  224. return (org.apache.poi.hslf.record.Slide)getSheetContainer();
  225. }
  226. /**
  227. * @return set of records inside <code>SlideListWithtext</code> container
  228. * which hold text data for this slide (typically for placeholders).
  229. */
  230. protected SlideAtomsSet getSlideAtomsSet() { return _atomSet; }
  231. /**
  232. * Returns master sheet associated with this slide.
  233. * It can be either SlideMaster or TitleMaster objects.
  234. *
  235. * @return the master sheet associated with this slide.
  236. */
  237. @Override
  238. public HSLFMasterSheet getMasterSheet(){
  239. int masterId = getSlideRecord().getSlideAtom().getMasterID();
  240. for (HSLFSlideMaster sm : getSlideShow().getSlideMasters()) {
  241. if (masterId == sm._getSheetNumber()) {
  242. return sm;
  243. }
  244. }
  245. for (HSLFTitleMaster tm : getSlideShow().getTitleMasters()) {
  246. if (masterId == tm._getSheetNumber()) {
  247. return tm;
  248. }
  249. }
  250. return null;
  251. }
  252. /**
  253. * Change Master of this slide.
  254. */
  255. public void setMasterSheet(HSLFMasterSheet master){
  256. SlideAtom sa = getSlideRecord().getSlideAtom();
  257. int sheetNo = master._getSheetNumber();
  258. sa.setMasterID(sheetNo);
  259. }
  260. /**
  261. * Sets whether this slide follows master background
  262. *
  263. * @param flag <code>true</code> if the slide follows master,
  264. * <code>false</code> otherwise
  265. */
  266. @Override
  267. public void setFollowMasterBackground(boolean flag){
  268. SlideAtom sa = getSlideRecord().getSlideAtom();
  269. sa.setFollowMasterBackground(flag);
  270. }
  271. /**
  272. * Whether this slide follows master sheet background
  273. *
  274. * @return <code>true</code> if the slide follows master background,
  275. * <code>false</code> otherwise
  276. */
  277. @Override
  278. public boolean getFollowMasterBackground(){
  279. SlideAtom sa = getSlideRecord().getSlideAtom();
  280. return sa.getFollowMasterBackground();
  281. }
  282. /**
  283. * Sets whether this slide draws master sheet objects
  284. *
  285. * @param flag <code>true</code> if the slide draws master sheet objects,
  286. * <code>false</code> otherwise
  287. */
  288. @Override
  289. public void setFollowMasterObjects(boolean flag){
  290. SlideAtom sa = getSlideRecord().getSlideAtom();
  291. sa.setFollowMasterObjects(flag);
  292. }
  293. /**
  294. * Whether this slide follows master color scheme
  295. *
  296. * @return <code>true</code> if the slide follows master color scheme,
  297. * <code>false</code> otherwise
  298. */
  299. public boolean getFollowMasterScheme(){
  300. SlideAtom sa = getSlideRecord().getSlideAtom();
  301. return sa.getFollowMasterScheme();
  302. }
  303. /**
  304. * Sets whether this slide draws master color scheme
  305. *
  306. * @param flag <code>true</code> if the slide draws master color scheme,
  307. * <code>false</code> otherwise
  308. */
  309. public void setFollowMasterScheme(boolean flag){
  310. SlideAtom sa = getSlideRecord().getSlideAtom();
  311. sa.setFollowMasterScheme(flag);
  312. }
  313. /**
  314. * Whether this slide draws master sheet objects
  315. *
  316. * @return <code>true</code> if the slide draws master sheet objects,
  317. * <code>false</code> otherwise
  318. */
  319. @Override
  320. public boolean getFollowMasterObjects(){
  321. SlideAtom sa = getSlideRecord().getSlideAtom();
  322. return sa.getFollowMasterObjects();
  323. }
  324. /**
  325. * Background for this slide.
  326. */
  327. @Override
  328. public HSLFBackground getBackground() {
  329. if (getFollowMasterBackground()) {
  330. final HSLFMasterSheet ms = getMasterSheet();
  331. return (ms == null) ? null : ms.getBackground();
  332. }
  333. return super.getBackground();
  334. }
  335. /**
  336. * Color scheme for this slide.
  337. */
  338. @Override
  339. public ColorSchemeAtom getColorScheme() {
  340. if (getFollowMasterScheme()) {
  341. final HSLFMasterSheet ms = getMasterSheet();
  342. return (ms == null) ? null : ms.getColorScheme();
  343. }
  344. return super.getColorScheme();
  345. }
  346. private static RecordContainer selectContainer(final RecordContainer root, final int index, final RecordTypes... path) {
  347. if (root == null || index >= path.length) {
  348. return root;
  349. }
  350. final RecordContainer newRoot = (RecordContainer) root.findFirstOfType(path[index].typeID);
  351. return selectContainer(newRoot, index+1, path);
  352. }
  353. /**
  354. * Get the comment(s) for this slide.
  355. * Note - for now, only works on PPT 2000 and
  356. * PPT 2003 files. Doesn't work for PPT 97
  357. * ones, as they do their comments oddly.
  358. */
  359. public List<HSLFComment> getComments() {
  360. final List<HSLFComment> comments = new ArrayList<>();
  361. // If there are any, they're in
  362. // ProgTags -> ProgBinaryTag -> BinaryTagData
  363. final RecordContainer binaryTags =
  364. selectContainer(getSheetContainer(), 0,
  365. RecordTypes.ProgTags, RecordTypes.ProgBinaryTag, RecordTypes.BinaryTagData);
  366. if (binaryTags != null) {
  367. for (final Record record : binaryTags.getChildRecords()) {
  368. if (record instanceof Comment2000) {
  369. comments.add(new HSLFComment((Comment2000)record));
  370. }
  371. }
  372. }
  373. return comments;
  374. }
  375. /**
  376. * Header / Footer settings for this slide.
  377. *
  378. * @return Header / Footer settings for this slide
  379. */
  380. public HeadersFooters getHeadersFooters(){
  381. return new HeadersFooters(this, HeadersFootersContainer.SlideHeadersFootersContainer);
  382. }
  383. @Override
  384. protected void onAddTextShape(HSLFTextShape shape) {
  385. List<HSLFTextParagraph> newParas = shape.getTextParagraphs();
  386. _paragraphs.add(newParas);
  387. }
  388. /** This will return an atom per TextBox, so if the page has two text boxes the method should return two atoms. */
  389. public StyleTextProp9Atom[] getNumberedListInfo() {
  390. return this.getPPDrawing().getNumberedListInfo();
  391. }
  392. public EscherTextboxWrapper[] getTextboxWrappers() {
  393. return this.getPPDrawing().getTextboxWrappers();
  394. }
  395. @Override
  396. public void setHidden(boolean hidden) {
  397. org.apache.poi.hslf.record.Slide cont = getSlideRecord();
  398. SSSlideInfoAtom slideInfo =
  399. (SSSlideInfoAtom)cont.findFirstOfType(RecordTypes.SSSlideInfoAtom.typeID);
  400. if (slideInfo == null) {
  401. slideInfo = new SSSlideInfoAtom();
  402. cont.addChildAfter(slideInfo, cont.findFirstOfType(RecordTypes.SlideAtom.typeID));
  403. }
  404. slideInfo.setEffectTransitionFlagByBit(SSSlideInfoAtom.HIDDEN_BIT, hidden);
  405. }
  406. @Override
  407. public boolean isHidden() {
  408. SSSlideInfoAtom slideInfo =
  409. (SSSlideInfoAtom)getSlideRecord().findFirstOfType(RecordTypes.SSSlideInfoAtom.typeID);
  410. return (slideInfo != null) && slideInfo.getEffectTransitionFlagByBit(SSSlideInfoAtom.HIDDEN_BIT);
  411. }
  412. @Override
  413. public void draw(Graphics2D graphics) {
  414. DrawFactory drawFact = DrawFactory.getInstance(graphics);
  415. Drawable draw = drawFact.getDrawable(this);
  416. draw.draw(graphics);
  417. }
  418. @Override
  419. public boolean getFollowMasterColourScheme() {
  420. return false;
  421. }
  422. @Override
  423. public void setFollowMasterColourScheme(boolean follow) {
  424. }
  425. @Override
  426. public boolean getFollowMasterGraphics() {
  427. return getFollowMasterObjects();
  428. }
  429. @Override
  430. public boolean getDisplayPlaceholder(final Placeholder placeholder) {
  431. final HeadersFooters hf = getHeadersFooters();
  432. final SlideLayoutType slt = getSlideRecord().getSlideAtom().getSSlideLayoutAtom().getGeometryType();
  433. final boolean isTitle =
  434. (slt == SlideLayoutType.TITLE_SLIDE || slt == SlideLayoutType.TITLE_ONLY || slt == SlideLayoutType.MASTER_TITLE);
  435. switch (placeholder) {
  436. case DATETIME:
  437. return hf.isDateTimeVisible() && !isTitle;
  438. case SLIDE_NUMBER:
  439. return hf.isSlideNumberVisible() && !isTitle;
  440. case HEADER:
  441. return hf.isHeaderVisible() && !isTitle;
  442. case FOOTER:
  443. return hf.isFooterVisible() && !isTitle;
  444. default:
  445. return false;
  446. }
  447. }
  448. }