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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518
  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.CString;
  26. import org.apache.poi.hslf.record.ColorSchemeAtom;
  27. import org.apache.poi.hslf.record.Comment2000;
  28. import org.apache.poi.hslf.record.EscherTextboxWrapper;
  29. import org.apache.poi.hslf.record.HeadersFootersContainer;
  30. import org.apache.poi.hslf.record.Record;
  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.Slide;
  45. /**
  46. * This class represents a slide in a PowerPoint Document. It allows
  47. * access to the text within, and the layout. For now, it only does
  48. * the text side of things though
  49. *
  50. * @author Nick Burch
  51. * @author Yegor Kozlov
  52. */
  53. public final class HSLFSlide extends HSLFSheet implements Slide<HSLFShape,HSLFTextParagraph> {
  54. private int _slideNo;
  55. private SlideAtomsSet _atomSet;
  56. private final List<List<HSLFTextParagraph>> _paragraphs = new ArrayList<>();
  57. private HSLFNotes _notes; // usermodel needs to set this
  58. /**
  59. * Constructs a Slide from the Slide record, and the SlideAtomsSet
  60. * containing the text.
  61. * Initializes TextRuns, to provide easier access to the text
  62. *
  63. * @param slide the Slide record we're based on
  64. * @param notes the Notes sheet attached to us
  65. * @param atomSet the SlideAtomsSet to get the text from
  66. */
  67. public HSLFSlide(org.apache.poi.hslf.record.Slide slide, HSLFNotes notes, SlideAtomsSet atomSet, int slideIdentifier, int slideNumber) {
  68. super(slide, slideIdentifier);
  69. _notes = notes;
  70. _atomSet = atomSet;
  71. _slideNo = slideNumber;
  72. // For the text coming in from the SlideAtomsSet:
  73. // Build up TextRuns from pairs of TextHeaderAtom and
  74. // one of TextBytesAtom or TextCharsAtom
  75. if (_atomSet != null && _atomSet.getSlideRecords().length > 0) {
  76. // Grab text from SlideListWithTexts entries
  77. _paragraphs.addAll(HSLFTextParagraph.findTextParagraphs(_atomSet.getSlideRecords()));
  78. if (_paragraphs.isEmpty()) {
  79. throw new HSLFException("No text records found for slide");
  80. }
  81. }
  82. // Grab text from slide's PPDrawing
  83. for (List<HSLFTextParagraph> l : HSLFTextParagraph.findTextParagraphs(getPPDrawing(), this)) {
  84. if (!_paragraphs.contains(l)) {
  85. _paragraphs.add(l);
  86. }
  87. }
  88. }
  89. /**
  90. * Create a new Slide instance
  91. * @param sheetNumber The internal number of the sheet, as used by PersistPtrHolder
  92. * @param slideNumber The user facing number of the sheet
  93. */
  94. public HSLFSlide(int sheetNumber, int sheetRefId, int slideNumber){
  95. super(new org.apache.poi.hslf.record.Slide(), sheetNumber);
  96. _slideNo = slideNumber;
  97. getSheetContainer().setSheetId(sheetRefId);
  98. }
  99. /**
  100. * Returns the Notes Sheet for this slide, or null if there isn't one
  101. */
  102. @Override
  103. public HSLFNotes getNotes() {
  104. return _notes;
  105. }
  106. /**
  107. * Sets the Notes that are associated with this. Updates the
  108. * references in the records to point to the new ID
  109. */
  110. @Override
  111. public void setNotes(Notes<HSLFShape,HSLFTextParagraph> notes) {
  112. if (notes != null && !(notes instanceof HSLFNotes)) {
  113. throw new IllegalArgumentException("notes needs to be of type HSLFNotes");
  114. }
  115. _notes = (HSLFNotes)notes;
  116. // Update the Slide Atom's ID of where to point to
  117. SlideAtom sa = getSlideRecord().getSlideAtom();
  118. if(_notes == null) {
  119. // Set to 0
  120. sa.setNotesID(0);
  121. } else {
  122. // Set to the value from the notes' sheet id
  123. sa.setNotesID(_notes._getSheetNumber());
  124. }
  125. }
  126. /**
  127. * Changes the Slide's (external facing) page number.
  128. * @see org.apache.poi.hslf.usermodel.HSLFSlideShow#reorderSlide(int, int)
  129. */
  130. public void setSlideNumber(int newSlideNumber) {
  131. _slideNo = newSlideNumber;
  132. }
  133. /**
  134. * Called by SlideShow ater a new slide is created.
  135. * <p>
  136. * For Slide we need to do the following:
  137. * <li> set id of the drawing group.
  138. * <li> set shapeId for the container descriptor and background
  139. * </p>
  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(c.getRecordId()){
  153. case EscherContainerRecord.SPGR_CONTAINER:
  154. EscherContainerRecord dc = (EscherContainerRecord)c.getChild(0);
  155. spr = dc.getChildById(EscherSpRecord.RECORD_ID);
  156. break;
  157. case EscherContainerRecord.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</code> object that represents the slide's title.
  172. *
  173. * @return <code>TextBox</code> 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(TextHeaderAtom.TITLE_TYPE);
  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</code> or
  189. * <code>TextHeaderAtom.TITLE_TYPE</code>
  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. switch (type) {
  202. case TextHeaderAtom.CENTER_TITLE_TYPE:
  203. case TextHeaderAtom.TITLE_TYPE:
  204. String str = HSLFTextParagraph.getRawText(tp);
  205. return HSLFTextParagraph.toExternalString(str, type);
  206. }
  207. }
  208. return null;
  209. }
  210. @Override
  211. public String getSlideName() {
  212. final CString name = (CString)getSlideRecord().findFirstOfType(RecordTypes.CString.typeID);
  213. return name != null ? name.getText() : "Slide"+getSlideNumber();
  214. }
  215. /**
  216. * Returns an array of all the TextRuns found
  217. */
  218. @Override
  219. public List<List<HSLFTextParagraph>> getTextParagraphs() { return _paragraphs; }
  220. /**
  221. * Returns the (public facing) page number of this slide
  222. */
  223. @Override
  224. public int getSlideNumber() { return _slideNo; }
  225. /**
  226. * Returns the underlying slide record
  227. */
  228. public org.apache.poi.hslf.record.Slide getSlideRecord() {
  229. return (org.apache.poi.hslf.record.Slide)getSheetContainer();
  230. }
  231. /**
  232. * @return set of records inside <code>SlideListWithtext</code> container
  233. * which hold text data for this slide (typically for placeholders).
  234. */
  235. protected SlideAtomsSet getSlideAtomsSet() { return _atomSet; }
  236. /**
  237. * Returns master sheet associated with this slide.
  238. * It can be either SlideMaster or TitleMaster objects.
  239. *
  240. * @return the master sheet associated with this slide.
  241. */
  242. @Override
  243. public HSLFMasterSheet getMasterSheet(){
  244. int masterId = getSlideRecord().getSlideAtom().getMasterID();
  245. for (HSLFSlideMaster sm : getSlideShow().getSlideMasters()) {
  246. if (masterId == sm._getSheetNumber()) {
  247. return sm;
  248. }
  249. }
  250. for (HSLFTitleMaster tm : getSlideShow().getTitleMasters()) {
  251. if (masterId == tm._getSheetNumber()) {
  252. return tm;
  253. }
  254. }
  255. return null;
  256. }
  257. /**
  258. * Change Master of this slide.
  259. */
  260. public void setMasterSheet(HSLFMasterSheet master){
  261. SlideAtom sa = getSlideRecord().getSlideAtom();
  262. int sheetNo = master._getSheetNumber();
  263. sa.setMasterID(sheetNo);
  264. }
  265. /**
  266. * Sets whether this slide follows master background
  267. *
  268. * @param flag <code>true</code> if the slide follows master,
  269. * <code>false</code> otherwise
  270. */
  271. @Override
  272. public void setFollowMasterBackground(boolean flag){
  273. SlideAtom sa = getSlideRecord().getSlideAtom();
  274. sa.setFollowMasterBackground(flag);
  275. }
  276. /**
  277. * Whether this slide follows master sheet background
  278. *
  279. * @return <code>true</code> if the slide follows master background,
  280. * <code>false</code> otherwise
  281. */
  282. @Override
  283. public boolean getFollowMasterBackground(){
  284. SlideAtom sa = getSlideRecord().getSlideAtom();
  285. return sa.getFollowMasterBackground();
  286. }
  287. /**
  288. * Sets whether this slide draws master sheet objects
  289. *
  290. * @param flag <code>true</code> if the slide draws master sheet objects,
  291. * <code>false</code> otherwise
  292. */
  293. @Override
  294. public void setFollowMasterObjects(boolean flag){
  295. SlideAtom sa = getSlideRecord().getSlideAtom();
  296. sa.setFollowMasterObjects(flag);
  297. }
  298. /**
  299. * Whether this slide follows master color scheme
  300. *
  301. * @return <code>true</code> if the slide follows master color scheme,
  302. * <code>false</code> otherwise
  303. */
  304. public boolean getFollowMasterScheme(){
  305. SlideAtom sa = getSlideRecord().getSlideAtom();
  306. return sa.getFollowMasterScheme();
  307. }
  308. /**
  309. * Sets whether this slide draws master color scheme
  310. *
  311. * @param flag <code>true</code> if the slide draws master color scheme,
  312. * <code>false</code> otherwise
  313. */
  314. public void setFollowMasterScheme(boolean flag){
  315. SlideAtom sa = getSlideRecord().getSlideAtom();
  316. sa.setFollowMasterScheme(flag);
  317. }
  318. /**
  319. * Whether this slide draws master sheet objects
  320. *
  321. * @return <code>true</code> if the slide draws master sheet objects,
  322. * <code>false</code> otherwise
  323. */
  324. @Override
  325. public boolean getFollowMasterObjects(){
  326. SlideAtom sa = getSlideRecord().getSlideAtom();
  327. return sa.getFollowMasterObjects();
  328. }
  329. /**
  330. * Background for this slide.
  331. */
  332. @Override
  333. public HSLFBackground getBackground() {
  334. if (getFollowMasterBackground()) {
  335. final HSLFMasterSheet ms = getMasterSheet();
  336. return (ms == null) ? null : ms.getBackground();
  337. }
  338. return super.getBackground();
  339. }
  340. /**
  341. * Color scheme for this slide.
  342. */
  343. @Override
  344. public ColorSchemeAtom getColorScheme() {
  345. if (getFollowMasterScheme()) {
  346. final HSLFMasterSheet ms = getMasterSheet();
  347. return (ms == null) ? null : ms.getColorScheme();
  348. }
  349. return super.getColorScheme();
  350. }
  351. private static RecordContainer selectContainer(final RecordContainer root, final int index, final RecordTypes... path) {
  352. if (root == null || index >= path.length) {
  353. return root;
  354. }
  355. final RecordContainer newRoot = (RecordContainer) root.findFirstOfType(path[index].typeID);
  356. return selectContainer(newRoot, index+1, path);
  357. }
  358. /**
  359. * Get the comment(s) for this slide.
  360. * Note - for now, only works on PPT 2000 and
  361. * PPT 2003 files. Doesn't work for PPT 97
  362. * ones, as they do their comments oddly.
  363. */
  364. public List<HSLFComment> getComments() {
  365. final List<HSLFComment> comments = new ArrayList<>();
  366. // If there are any, they're in
  367. // ProgTags -> ProgBinaryTag -> BinaryTagData
  368. final RecordContainer binaryTags =
  369. selectContainer(getSheetContainer(), 0,
  370. RecordTypes.ProgTags, RecordTypes.ProgBinaryTag, RecordTypes.BinaryTagData);
  371. if (binaryTags != null) {
  372. for (final Record record : binaryTags.getChildRecords()) {
  373. if (record instanceof Comment2000) {
  374. comments.add(new HSLFComment((Comment2000)record));
  375. }
  376. }
  377. }
  378. return comments;
  379. }
  380. /**
  381. * Header / Footer settings for this slide.
  382. *
  383. * @return Header / Footer settings for this slide
  384. */
  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() && !isTitle;
  443. case SLIDE_NUMBER:
  444. return hf.isSlideNumberVisible() && !isTitle;
  445. case HEADER:
  446. return hf.isHeaderVisible() && !isTitle;
  447. case FOOTER:
  448. return hf.isFooterVisible() && !isTitle;
  449. default:
  450. return false;
  451. }
  452. }
  453. @Override
  454. public HSLFMasterSheet getSlideLayout(){
  455. // TODO: find out how we can find the mastersheet base on the slide layout type, i.e.
  456. // getSlideRecord().getSlideAtom().getSSlideLayoutAtom().getGeometryType()
  457. return getMasterSheet();
  458. }
  459. }