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.

Slide.java 16KB

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