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.

HSLFSheet.java 12KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398
  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.*;
  18. import org.apache.poi.ddf.*;
  19. import org.apache.poi.hslf.record.*;
  20. import org.apache.poi.sl.draw.DrawFactory;
  21. import org.apache.poi.sl.draw.Drawable;
  22. import org.apache.poi.sl.usermodel.Sheet;
  23. import org.apache.poi.util.POILogFactory;
  24. import org.apache.poi.util.POILogger;
  25. /**
  26. * This class defines the common format of "Sheets" in a powerpoint
  27. * document. Such sheets could be Slides, Notes, Master etc
  28. *
  29. * @author Nick Burch
  30. * @author Yegor Kozlov
  31. */
  32. public abstract class HSLFSheet implements Sheet<HSLFShape,HSLFSlideShow> {
  33. private static POILogger logger = POILogFactory.getLogger(HSLFSheet.class);
  34. /**
  35. * The <code>SlideShow</code> we belong to
  36. */
  37. private HSLFSlideShow _slideShow;
  38. /**
  39. * Sheet background
  40. */
  41. private HSLFBackground _background;
  42. /**
  43. * Record container that holds sheet data.
  44. * For slides it is org.apache.poi.hslf.record.Slide,
  45. * for notes it is org.apache.poi.hslf.record.Notes,
  46. * for slide masters it is org.apache.poi.hslf.record.SlideMaster, etc.
  47. */
  48. private SheetContainer _container;
  49. private int _sheetNo;
  50. public HSLFSheet(SheetContainer container, int sheetNo) {
  51. _container = container;
  52. _sheetNo = sheetNo;
  53. }
  54. /**
  55. * Returns an array of all the TextRuns in the sheet.
  56. */
  57. public abstract List<List<HSLFTextParagraph>> getTextParagraphs();
  58. /**
  59. * Returns the (internal, RefID based) sheet number, as used
  60. * to in PersistPtr stuff.
  61. */
  62. public int _getSheetRefId() {
  63. return _container.getSheetId();
  64. }
  65. /**
  66. * Returns the (internal, SlideIdentifier based) sheet number, as used
  67. * to reference this sheet from other records.
  68. */
  69. public int _getSheetNumber() {
  70. return _sheetNo;
  71. }
  72. /**
  73. * Fetch the PPDrawing from the underlying record
  74. */
  75. public PPDrawing getPPDrawing() {
  76. return _container.getPPDrawing();
  77. }
  78. /**
  79. * Fetch the SlideShow we're attached to
  80. */
  81. public HSLFSlideShow getSlideShow() {
  82. return _slideShow;
  83. }
  84. /**
  85. * Return record container for this sheet
  86. */
  87. public SheetContainer getSheetContainer() {
  88. return _container;
  89. }
  90. /**
  91. * Set the SlideShow we're attached to.
  92. * Also passes it on to our child RichTextRuns
  93. */
  94. public void setSlideShow(HSLFSlideShow ss) {
  95. _slideShow = ss;
  96. List<List<HSLFTextParagraph>> trs = getTextParagraphs();
  97. if (trs == null) return;
  98. for (List<HSLFTextParagraph> ltp : trs) {
  99. for (HSLFTextParagraph tp : ltp) {
  100. tp.supplySheet(this);
  101. }
  102. }
  103. }
  104. /**
  105. * Returns all shapes contained in this Sheet
  106. *
  107. * @return all shapes contained in this Sheet (Slide or Notes)
  108. */
  109. @Override
  110. public List<HSLFShape> getShapes() {
  111. PPDrawing ppdrawing = getPPDrawing();
  112. EscherContainerRecord dg = (EscherContainerRecord) ppdrawing.getEscherRecords()[0];
  113. EscherContainerRecord spgr = null;
  114. for (Iterator<EscherRecord> it = dg.getChildIterator(); it.hasNext();) {
  115. EscherRecord rec = it.next();
  116. if (rec.getRecordId() == EscherContainerRecord.SPGR_CONTAINER) {
  117. spgr = (EscherContainerRecord) rec;
  118. break;
  119. }
  120. }
  121. if (spgr == null) {
  122. throw new IllegalStateException("spgr not found");
  123. }
  124. List<HSLFShape> shapeList = new ArrayList<HSLFShape>();
  125. Iterator<EscherRecord> it = spgr.getChildIterator();
  126. if (it.hasNext()) {
  127. // skip first item
  128. it.next();
  129. }
  130. for (; it.hasNext();) {
  131. EscherContainerRecord sp = (EscherContainerRecord) it.next();
  132. HSLFShape sh = HSLFShapeFactory.createShape(sp, null);
  133. sh.setSheet(this);
  134. shapeList.add(sh);
  135. }
  136. return shapeList;
  137. }
  138. /**
  139. * Add a new Shape to this Slide
  140. *
  141. * @param shape - the Shape to add
  142. */
  143. public void addShape(HSLFShape shape) {
  144. PPDrawing ppdrawing = getPPDrawing();
  145. EscherContainerRecord dgContainer = (EscherContainerRecord) ppdrawing.getEscherRecords()[0];
  146. EscherContainerRecord spgr = (EscherContainerRecord) HSLFShape.getEscherChild(dgContainer, EscherContainerRecord.SPGR_CONTAINER);
  147. spgr.addChildRecord(shape.getSpContainer());
  148. shape.setSheet(this);
  149. shape.setShapeId(allocateShapeId());
  150. shape.afterInsert(this);
  151. }
  152. /**
  153. * Allocates new shape id for the new drawing group id.
  154. *
  155. * @return a new shape id.
  156. */
  157. public int allocateShapeId()
  158. {
  159. EscherDggRecord dgg = _slideShow.getDocumentRecord().getPPDrawingGroup().getEscherDggRecord();
  160. EscherDgRecord dg = _container.getPPDrawing().getEscherDgRecord();
  161. dgg.setNumShapesSaved( dgg.getNumShapesSaved() + 1 );
  162. // Add to existing cluster if space available
  163. for (int i = 0; i < dgg.getFileIdClusters().length; i++)
  164. {
  165. EscherDggRecord.FileIdCluster c = dgg.getFileIdClusters()[i];
  166. if (c.getDrawingGroupId() == dg.getDrawingGroupId() && c.getNumShapeIdsUsed() != 1024)
  167. {
  168. int result = c.getNumShapeIdsUsed() + (1024 * (i+1));
  169. c.incrementShapeId();
  170. dg.setNumShapes( dg.getNumShapes() + 1 );
  171. dg.setLastMSOSPID( result );
  172. if (result >= dgg.getShapeIdMax())
  173. dgg.setShapeIdMax( result + 1 );
  174. return result;
  175. }
  176. }
  177. // Create new cluster
  178. dgg.addCluster( dg.getDrawingGroupId(), 0, false );
  179. dgg.getFileIdClusters()[dgg.getFileIdClusters().length-1].incrementShapeId();
  180. dg.setNumShapes( dg.getNumShapes() + 1 );
  181. int result = (1024 * dgg.getFileIdClusters().length);
  182. dg.setLastMSOSPID( result );
  183. if (result >= dgg.getShapeIdMax())
  184. dgg.setShapeIdMax( result + 1 );
  185. return result;
  186. }
  187. /**
  188. * Removes the specified shape from this sheet.
  189. *
  190. * @param shape shape to be removed from this sheet, if present.
  191. * @return <tt>true</tt> if the shape was deleted.
  192. */
  193. public boolean removeShape(HSLFShape shape) {
  194. PPDrawing ppdrawing = getPPDrawing();
  195. EscherContainerRecord dg = (EscherContainerRecord) ppdrawing.getEscherRecords()[0];
  196. EscherContainerRecord spgr = null;
  197. for (Iterator<EscherRecord> it = dg.getChildIterator(); it.hasNext();) {
  198. EscherRecord rec = it.next();
  199. if (rec.getRecordId() == EscherContainerRecord.SPGR_CONTAINER) {
  200. spgr = (EscherContainerRecord) rec;
  201. break;
  202. }
  203. }
  204. if(spgr == null) {
  205. return false;
  206. }
  207. List<EscherRecord> lst = spgr.getChildRecords();
  208. boolean result = lst.remove(shape.getSpContainer());
  209. spgr.setChildRecords(lst);
  210. return result;
  211. }
  212. /**
  213. * Called by SlideShow ater a new sheet is created
  214. */
  215. public void onCreate(){
  216. }
  217. /**
  218. * Return the master sheet .
  219. */
  220. public abstract HSLFMasterSheet getMasterSheet();
  221. /**
  222. * Color scheme for this sheet.
  223. */
  224. public ColorSchemeAtom getColorScheme() {
  225. return _container.getColorScheme();
  226. }
  227. /**
  228. * Returns the background shape for this sheet.
  229. *
  230. * @return the background shape for this sheet.
  231. */
  232. public HSLFBackground getBackground() {
  233. if (_background == null) {
  234. PPDrawing ppdrawing = getPPDrawing();
  235. EscherContainerRecord dg = (EscherContainerRecord) ppdrawing.getEscherRecords()[0];
  236. EscherContainerRecord spContainer = null;
  237. for (Iterator<EscherRecord> it = dg.getChildIterator(); it.hasNext();) {
  238. EscherRecord rec = it.next();
  239. if (rec.getRecordId() == EscherContainerRecord.SP_CONTAINER) {
  240. spContainer = (EscherContainerRecord) rec;
  241. break;
  242. }
  243. }
  244. _background = new HSLFBackground(spContainer, null);
  245. _background.setSheet(this);
  246. }
  247. return _background;
  248. }
  249. @Override
  250. public void draw(Graphics2D graphics) {
  251. DrawFactory drawFact = DrawFactory.getInstance(graphics);
  252. Drawable draw = drawFact.getDrawable(this);
  253. draw.draw(graphics);
  254. }
  255. /**
  256. * Subclasses should call this method and update the array of text runs
  257. * when a text shape is added
  258. *
  259. * @param shape
  260. */
  261. protected void onAddTextShape(HSLFTextShape shape) {
  262. }
  263. /**
  264. * Return placeholder by text type
  265. *
  266. * @param type type of text, See {@link org.apache.poi.hslf.record.TextHeaderAtom}
  267. * @return <code>TextShape</code> or <code>null</code>
  268. */
  269. public HSLFTextShape getPlaceholderByTextType(int type){
  270. for (HSLFShape shape : getShapes()) {
  271. if(shape instanceof HSLFTextShape){
  272. HSLFTextShape tx = (HSLFTextShape)shape;
  273. if (tx != null && tx.getRunType() == type) {
  274. return tx;
  275. }
  276. }
  277. }
  278. return null;
  279. }
  280. /**
  281. * Search text placeholer by its type
  282. *
  283. * @param type type of placeholder to search. See {@link org.apache.poi.hslf.record.OEPlaceholderAtom}
  284. * @return <code>TextShape</code> or <code>null</code>
  285. */
  286. public HSLFTextShape getPlaceholder(int type){
  287. for (HSLFShape shape : getShapes()) {
  288. if(shape instanceof HSLFTextShape){
  289. HSLFTextShape tx = (HSLFTextShape)shape;
  290. int placeholderId = 0;
  291. OEPlaceholderAtom oep = tx.getPlaceholderAtom();
  292. if(oep != null) {
  293. placeholderId = oep.getPlaceholderId();
  294. } else {
  295. //special case for files saved in Office 2007
  296. RoundTripHFPlaceholder12 hldr = tx.getClientDataRecord(RecordTypes.RoundTripHFPlaceholder12.typeID);
  297. if(hldr != null) placeholderId = hldr.getPlaceholderId();
  298. }
  299. if(placeholderId == type){
  300. return tx;
  301. }
  302. }
  303. }
  304. return null;
  305. }
  306. /**
  307. * Return programmable tag associated with this sheet, e.g. <code>___PPT12</code>.
  308. *
  309. * @return programmable tag associated with this sheet.
  310. */
  311. public String getProgrammableTag(){
  312. String tag = null;
  313. RecordContainer progTags = (RecordContainer)
  314. getSheetContainer().findFirstOfType(
  315. RecordTypes.ProgTags.typeID
  316. );
  317. if(progTags != null) {
  318. RecordContainer progBinaryTag = (RecordContainer)
  319. progTags.findFirstOfType(
  320. RecordTypes.ProgBinaryTag.typeID
  321. );
  322. if(progBinaryTag != null) {
  323. CString binaryTag = (CString)
  324. progBinaryTag.findFirstOfType(
  325. RecordTypes.CString.typeID
  326. );
  327. if(binaryTag != null) tag = binaryTag.getText();
  328. }
  329. }
  330. return tag;
  331. }
  332. public Iterator<HSLFShape> iterator() {
  333. return getShapes().iterator();
  334. }
  335. /**
  336. * @return whether shapes on the master sheet should be shown. By default master graphics is turned off.
  337. * Sheets that support the notion of master (slide, slideLayout) should override it and
  338. * check this setting
  339. */
  340. public boolean getFollowMasterGraphics() {
  341. return false;
  342. }
  343. }