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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541
  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.EscherRecordTypes;
  23. import org.apache.poi.ddf.EscherSpRecord;
  24. import org.apache.poi.hslf.exceptions.HSLFException;
  25. import org.apache.poi.hslf.model.HeadersFooters;
  26. import org.apache.poi.hslf.record.CString;
  27. import org.apache.poi.hslf.record.ColorSchemeAtom;
  28. import org.apache.poi.hslf.record.Comment2000;
  29. import org.apache.poi.hslf.record.EscherTextboxWrapper;
  30. import org.apache.poi.hslf.record.HeadersFootersContainer;
  31. import org.apache.poi.hslf.record.RecordContainer;
  32. import org.apache.poi.hslf.record.RecordTypes;
  33. import org.apache.poi.hslf.record.SSSlideInfoAtom;
  34. import org.apache.poi.hslf.record.SlideAtom;
  35. import org.apache.poi.hslf.record.SlideAtomLayout.SlideLayoutType;
  36. import org.apache.poi.hslf.record.SlideListWithText.SlideAtomsSet;
  37. import org.apache.poi.hslf.record.StyleTextProp9Atom;
  38. import org.apache.poi.hslf.record.TextHeaderAtom;
  39. import org.apache.poi.sl.draw.DrawFactory;
  40. import org.apache.poi.sl.draw.Drawable;
  41. import org.apache.poi.sl.usermodel.Notes;
  42. import org.apache.poi.sl.usermodel.Placeholder;
  43. import org.apache.poi.sl.usermodel.ShapeType;
  44. import org.apache.poi.sl.usermodel.SimpleShape;
  45. import org.apache.poi.sl.usermodel.Slide;
  46. import org.apache.poi.sl.usermodel.TextShape.TextPlaceholder;
  47. /**
  48. * This class represents a slide in a PowerPoint Document. It allows
  49. * access to the text within, and the layout. For now, it only does
  50. * the text side of things though
  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. * <ul>
  137. * <li> set id of the drawing group.</li>
  138. * <li> set shapeId for the container descriptor and background</li>
  139. * </ul>
  140. */
  141. @Override
  142. public void onCreate(){
  143. //initialize drawing group id
  144. EscherDggRecord dgg = getSlideShow().getDocumentRecord().getPPDrawingGroup().getEscherDggRecord();
  145. EscherContainerRecord dgContainer = getSheetContainer().getPPDrawing().getDgContainer();
  146. EscherDgRecord dg = HSLFShape.getEscherChild(dgContainer, EscherDgRecord.RECORD_ID);
  147. int dgId = dgg.getMaxDrawingGroupId() + 1;
  148. dg.setOptions((short)(dgId << 4));
  149. dgg.setDrawingsSaved(dgg.getDrawingsSaved() + 1);
  150. for (EscherContainerRecord c : dgContainer.getChildContainers()) {
  151. EscherSpRecord spr = null;
  152. switch(EscherRecordTypes.forTypeID(c.getRecordId())){
  153. case SPGR_CONTAINER:
  154. EscherContainerRecord dc = (EscherContainerRecord)c.getChild(0);
  155. spr = dc.getChildById(EscherSpRecord.RECORD_ID);
  156. break;
  157. case SP_CONTAINER:
  158. spr = c.getChildById(EscherSpRecord.RECORD_ID);
  159. break;
  160. default:
  161. break;
  162. }
  163. if(spr != null) {
  164. spr.setShapeId(allocateShapeId());
  165. }
  166. }
  167. //PPT doen't increment the number of saved shapes for group descriptor and background
  168. dg.setNumShapes(1);
  169. }
  170. /**
  171. * Create a {@code TextBox} object that represents the slide's title.
  172. *
  173. * @return {@code TextBox} object that represents the slide's title.
  174. */
  175. public HSLFTextBox addTitle() {
  176. HSLFPlaceholder pl = new HSLFPlaceholder();
  177. pl.setShapeType(ShapeType.RECT);
  178. pl.setPlaceholder(Placeholder.TITLE);
  179. pl.setRunType(TextPlaceholder.TITLE.nativeId);
  180. pl.setText("Click to edit title");
  181. pl.setAnchor(new java.awt.Rectangle(54, 48, 612, 90));
  182. addShape(pl);
  183. return pl;
  184. }
  185. // Complex Accesser methods follow
  186. /**
  187. * <p>
  188. * The title is a run of text of type {@code TextHeaderAtom.CENTER_TITLE_TYPE} or
  189. * {@code TextHeaderAtom.TITLE_TYPE}
  190. * </p>
  191. *
  192. * @see TextHeaderAtom
  193. */
  194. @Override
  195. public String getTitle(){
  196. for (List<HSLFTextParagraph> tp : getTextParagraphs()) {
  197. if (tp.isEmpty()) {
  198. continue;
  199. }
  200. int type = tp.get(0).getRunType();
  201. if (TextPlaceholder.isTitle(type)) {
  202. String str = HSLFTextParagraph.getRawText(tp);
  203. return HSLFTextParagraph.toExternalString(str, type);
  204. }
  205. }
  206. return null;
  207. }
  208. @Override
  209. public String getSlideName() {
  210. final CString name = (CString)getSlideRecord().findFirstOfType(RecordTypes.CString.typeID);
  211. return name != null ? name.getText() : "Slide"+getSlideNumber();
  212. }
  213. /**
  214. * Returns an array of all the TextRuns found
  215. */
  216. @Override
  217. public List<List<HSLFTextParagraph>> getTextParagraphs() { return _paragraphs; }
  218. /**
  219. * Returns the (public facing) page number of this slide
  220. */
  221. @Override
  222. public int getSlideNumber() { return _slideNo; }
  223. /**
  224. * Returns the underlying slide record
  225. */
  226. public org.apache.poi.hslf.record.Slide getSlideRecord() {
  227. return (org.apache.poi.hslf.record.Slide)getSheetContainer();
  228. }
  229. /**
  230. * @return set of records inside {@code SlideListWithtext} container
  231. * which hold text data for this slide (typically for placeholders).
  232. */
  233. public SlideAtomsSet getSlideAtomsSet() { return _atomSet; }
  234. /**
  235. * Returns master sheet associated with this slide.
  236. * It can be either SlideMaster or TitleMaster objects.
  237. *
  238. * @return the master sheet associated with this slide.
  239. */
  240. @Override
  241. public HSLFMasterSheet getMasterSheet(){
  242. int masterId = getSlideRecord().getSlideAtom().getMasterID();
  243. for (HSLFSlideMaster sm : getSlideShow().getSlideMasters()) {
  244. if (masterId == sm._getSheetNumber()) {
  245. return sm;
  246. }
  247. }
  248. for (HSLFTitleMaster tm : getSlideShow().getTitleMasters()) {
  249. if (masterId == tm._getSheetNumber()) {
  250. return tm;
  251. }
  252. }
  253. return null;
  254. }
  255. /**
  256. * Change Master of this slide.
  257. */
  258. public void setMasterSheet(HSLFMasterSheet master){
  259. SlideAtom sa = getSlideRecord().getSlideAtom();
  260. int sheetNo = master._getSheetNumber();
  261. sa.setMasterID(sheetNo);
  262. }
  263. /**
  264. * Sets whether this slide follows master background
  265. *
  266. * @param flag {@code true} if the slide follows master,
  267. * {@code false} otherwise
  268. */
  269. @Override
  270. public void setFollowMasterBackground(boolean flag){
  271. SlideAtom sa = getSlideRecord().getSlideAtom();
  272. sa.setFollowMasterBackground(flag);
  273. }
  274. /**
  275. * Whether this slide follows master sheet background
  276. *
  277. * @return {@code true} if the slide follows master background,
  278. * {@code false} otherwise
  279. */
  280. @Override
  281. public boolean getFollowMasterBackground(){
  282. SlideAtom sa = getSlideRecord().getSlideAtom();
  283. return sa.getFollowMasterBackground();
  284. }
  285. /**
  286. * Sets whether this slide draws master sheet objects
  287. *
  288. * @param flag {@code true} if the slide draws master sheet objects,
  289. * {@code false} otherwise
  290. */
  291. @Override
  292. public void setFollowMasterObjects(boolean flag){
  293. SlideAtom sa = getSlideRecord().getSlideAtom();
  294. sa.setFollowMasterObjects(flag);
  295. }
  296. /**
  297. * Whether this slide follows master color scheme
  298. *
  299. * @return {@code true} if the slide follows master color scheme,
  300. * {@code false} otherwise
  301. */
  302. public boolean getFollowMasterScheme(){
  303. SlideAtom sa = getSlideRecord().getSlideAtom();
  304. return sa.getFollowMasterScheme();
  305. }
  306. /**
  307. * Sets whether this slide draws master color scheme
  308. *
  309. * @param flag {@code true} if the slide draws master color scheme,
  310. * {@code false} otherwise
  311. */
  312. public void setFollowMasterScheme(boolean flag){
  313. SlideAtom sa = getSlideRecord().getSlideAtom();
  314. sa.setFollowMasterScheme(flag);
  315. }
  316. /**
  317. * Whether this slide draws master sheet objects
  318. *
  319. * @return {@code true} if the slide draws master sheet objects,
  320. * {@code false} otherwise
  321. */
  322. @Override
  323. public boolean getFollowMasterObjects(){
  324. SlideAtom sa = getSlideRecord().getSlideAtom();
  325. return sa.getFollowMasterObjects();
  326. }
  327. /**
  328. * Background for this slide.
  329. */
  330. @Override
  331. public HSLFBackground getBackground() {
  332. if (getFollowMasterBackground()) {
  333. final HSLFMasterSheet ms = getMasterSheet();
  334. return (ms == null) ? null : ms.getBackground();
  335. }
  336. return super.getBackground();
  337. }
  338. /**
  339. * Color scheme for this slide.
  340. */
  341. @Override
  342. public ColorSchemeAtom getColorScheme() {
  343. if (getFollowMasterScheme()) {
  344. final HSLFMasterSheet ms = getMasterSheet();
  345. return (ms == null) ? null : ms.getColorScheme();
  346. }
  347. return super.getColorScheme();
  348. }
  349. private static RecordContainer selectContainer(final RecordContainer root, final int index, final RecordTypes... path) {
  350. if (root == null || index >= path.length) {
  351. return root;
  352. }
  353. final RecordContainer newRoot = (RecordContainer) root.findFirstOfType(path[index].typeID);
  354. return selectContainer(newRoot, index+1, path);
  355. }
  356. /**
  357. * Get the comment(s) for this slide.
  358. * Note - for now, only works on PPT 2000 and
  359. * PPT 2003 files. Doesn't work for PPT 97
  360. * ones, as they do their comments oddly.
  361. */
  362. @Override
  363. public List<HSLFComment> getComments() {
  364. final List<HSLFComment> comments = new ArrayList<>();
  365. // If there are any, they're in
  366. // ProgTags -> ProgBinaryTag -> BinaryTagData
  367. final RecordContainer binaryTags =
  368. selectContainer(getSheetContainer(), 0,
  369. RecordTypes.ProgTags, RecordTypes.ProgBinaryTag, RecordTypes.BinaryTagData);
  370. if (binaryTags != null) {
  371. for (final org.apache.poi.hslf.record.Record record : binaryTags.getChildRecords()) {
  372. if (record instanceof Comment2000) {
  373. comments.add(new HSLFComment((Comment2000)record));
  374. }
  375. }
  376. }
  377. return comments;
  378. }
  379. /**
  380. * Header / Footer settings for this slide.
  381. *
  382. * @return Header / Footer settings for this slide
  383. */
  384. @Override
  385. public HeadersFooters getHeadersFooters(){
  386. return new HeadersFooters(this, HeadersFootersContainer.SlideHeadersFootersContainer);
  387. }
  388. @Override
  389. protected void onAddTextShape(HSLFTextShape shape) {
  390. List<HSLFTextParagraph> newParas = shape.getTextParagraphs();
  391. _paragraphs.add(newParas);
  392. }
  393. /** This will return an atom per TextBox, so if the page has two text boxes the method should return two atoms. */
  394. public StyleTextProp9Atom[] getNumberedListInfo() {
  395. return this.getPPDrawing().getNumberedListInfo();
  396. }
  397. public EscherTextboxWrapper[] getTextboxWrappers() {
  398. return this.getPPDrawing().getTextboxWrappers();
  399. }
  400. @Override
  401. public void setHidden(boolean hidden) {
  402. org.apache.poi.hslf.record.Slide cont = getSlideRecord();
  403. SSSlideInfoAtom slideInfo =
  404. (SSSlideInfoAtom)cont.findFirstOfType(RecordTypes.SSSlideInfoAtom.typeID);
  405. if (slideInfo == null) {
  406. slideInfo = new SSSlideInfoAtom();
  407. cont.addChildAfter(slideInfo, cont.findFirstOfType(RecordTypes.SlideAtom.typeID));
  408. }
  409. slideInfo.setEffectTransitionFlagByBit(SSSlideInfoAtom.HIDDEN_BIT, hidden);
  410. }
  411. @Override
  412. public boolean isHidden() {
  413. SSSlideInfoAtom slideInfo =
  414. (SSSlideInfoAtom)getSlideRecord().findFirstOfType(RecordTypes.SSSlideInfoAtom.typeID);
  415. return (slideInfo != null) && slideInfo.getEffectTransitionFlagByBit(SSSlideInfoAtom.HIDDEN_BIT);
  416. }
  417. @Override
  418. public void draw(Graphics2D graphics) {
  419. DrawFactory drawFact = DrawFactory.getInstance(graphics);
  420. Drawable draw = drawFact.getDrawable(this);
  421. draw.draw(graphics);
  422. }
  423. @Override
  424. public boolean getFollowMasterColourScheme() {
  425. return false;
  426. }
  427. @Override
  428. public void setFollowMasterColourScheme(boolean follow) {
  429. }
  430. @Override
  431. public boolean getFollowMasterGraphics() {
  432. return getFollowMasterObjects();
  433. }
  434. @Override
  435. public boolean getDisplayPlaceholder(final Placeholder placeholder) {
  436. final HeadersFooters hf = getHeadersFooters();
  437. final SlideLayoutType slt = getSlideRecord().getSlideAtom().getSSlideLayoutAtom().getGeometryType();
  438. final boolean isTitle =
  439. (slt == SlideLayoutType.TITLE_SLIDE || slt == SlideLayoutType.TITLE_ONLY || slt == SlideLayoutType.MASTER_TITLE);
  440. switch (placeholder) {
  441. case DATETIME:
  442. return (hf.isDateTimeVisible() && (hf.isTodayDateVisible() || (hf.isUserDateVisible() && hf.getUserDateAtom() != null))) && !isTitle;
  443. case SLIDE_NUMBER:
  444. return hf.isSlideNumberVisible() && !isTitle;
  445. case HEADER:
  446. return hf.isHeaderVisible() && hf.getHeaderAtom() != null && !isTitle;
  447. case FOOTER:
  448. return hf.isFooterVisible() && hf.getFooterAtom() != null && !isTitle;
  449. default:
  450. return false;
  451. }
  452. }
  453. @Override
  454. public boolean getDisplayPlaceholder(final SimpleShape<?,?> placeholderRef) {
  455. Placeholder placeholder = placeholderRef.getPlaceholder();
  456. if (placeholder == null) {
  457. return false;
  458. }
  459. final HeadersFooters hf = getHeadersFooters();
  460. final SlideLayoutType slt = getSlideRecord().getSlideAtom().getSSlideLayoutAtom().getGeometryType();
  461. final boolean isTitle =
  462. (slt == SlideLayoutType.TITLE_SLIDE || slt == SlideLayoutType.TITLE_ONLY || slt == SlideLayoutType.MASTER_TITLE);
  463. switch (placeholder) {
  464. case HEADER:
  465. return hf.isHeaderVisible() && hf.getHeaderAtom() != null && !isTitle;
  466. case FOOTER:
  467. return hf.isFooterVisible() && hf.getFooterAtom() != null && !isTitle;
  468. case DATETIME:
  469. case SLIDE_NUMBER:
  470. default:
  471. return false;
  472. }
  473. }
  474. @Override
  475. public HSLFMasterSheet getSlideLayout(){
  476. // TODO: find out how we can find the mastersheet base on the slide layout type, i.e.
  477. // getSlideRecord().getSlideAtom().getSSlideLayoutAtom().getGeometryType()
  478. return getMasterSheet();
  479. }
  480. }