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

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