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

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