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.

HSLFSlideMaster.java 5.8KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157
  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.util.ArrayList;
  17. import java.util.List;
  18. import org.apache.poi.hslf.exceptions.HSLFException;
  19. import org.apache.poi.hslf.model.textproperties.TextProp;
  20. import org.apache.poi.hslf.model.textproperties.TextPropCollection;
  21. import org.apache.poi.hslf.record.*;
  22. import org.apache.poi.util.Internal;
  23. /**
  24. * SlideMaster determines the graphics, layout, and formatting for all the slides in a given presentation.
  25. * It stores information about default font styles, placeholder sizes and positions,
  26. * background design, and color schemes.
  27. *
  28. * @author Yegor Kozlov
  29. */
  30. public final class HSLFSlideMaster extends HSLFMasterSheet {
  31. private final List<List<HSLFTextParagraph>> _paragraphs = new ArrayList<List<HSLFTextParagraph>>();
  32. /**
  33. * all TxMasterStyleAtoms available in this master
  34. */
  35. private TxMasterStyleAtom[] _txmaster;
  36. /**
  37. * Constructs a SlideMaster from the MainMaster record,
  38. *
  39. */
  40. public HSLFSlideMaster(MainMaster record, int sheetNo) {
  41. super(record, sheetNo);
  42. for (List<HSLFTextParagraph> l : HSLFTextParagraph.findTextParagraphs(getPPDrawing(), this)) {
  43. if (!_paragraphs.contains(l)) _paragraphs.add(l);
  44. }
  45. }
  46. /**
  47. * Returns an array of all the TextRuns found
  48. */
  49. public List<List<HSLFTextParagraph>> getTextParagraphs() {
  50. return _paragraphs;
  51. }
  52. /**
  53. * Returns <code>null</code> since SlideMasters doen't have master sheet.
  54. */
  55. public HSLFMasterSheet getMasterSheet() {
  56. return null;
  57. }
  58. /**
  59. * Pickup a style attribute from the master.
  60. * This is the "workhorse" which returns the default style attributes.
  61. */
  62. public TextProp getStyleAttribute(int txtype, int level, String name, boolean isCharacter) {
  63. if (_txmaster.length <= txtype) return null;
  64. TxMasterStyleAtom t = _txmaster[txtype];
  65. List<TextPropCollection> styles = isCharacter ? t.getCharacterStyles() : t.getParagraphStyles();
  66. TextProp prop = null;
  67. for (int i = Math.min(level, styles.size()-1); prop == null && i >= 0; i--) {
  68. prop = styles.get(i).findByName(name);
  69. }
  70. if (prop != null) return prop;
  71. switch (txtype) {
  72. case TextHeaderAtom.CENTRE_BODY_TYPE:
  73. case TextHeaderAtom.HALF_BODY_TYPE:
  74. case TextHeaderAtom.QUARTER_BODY_TYPE:
  75. txtype = TextHeaderAtom.BODY_TYPE;
  76. break;
  77. case TextHeaderAtom.CENTER_TITLE_TYPE:
  78. txtype = TextHeaderAtom.TITLE_TYPE;
  79. break;
  80. default:
  81. return null;
  82. }
  83. return getStyleAttribute(txtype, level, name, isCharacter);
  84. }
  85. /**
  86. * Assign SlideShow for this slide master.
  87. */
  88. @Internal
  89. @Override
  90. protected void setSlideShow(HSLFSlideShow ss) {
  91. super.setSlideShow(ss);
  92. //after the slide show is assigned collect all available style records
  93. assert (_txmaster == null);
  94. _txmaster = new TxMasterStyleAtom[9];
  95. TxMasterStyleAtom txdoc = getSlideShow().getDocumentRecord().getEnvironment().getTxMasterStyleAtom();
  96. _txmaster[txdoc.getTextType()] = txdoc;
  97. TxMasterStyleAtom[] txrec = ((MainMaster)getSheetContainer()).getTxMasterStyleAtoms();
  98. for (int i = 0; i < txrec.length; i++) {
  99. int txType = txrec[i].getTextType();
  100. if (txType < _txmaster.length && _txmaster[txType] == null) {
  101. _txmaster[txType] = txrec[i];
  102. }
  103. }
  104. for (List<HSLFTextParagraph> paras : getTextParagraphs()) {
  105. for (HSLFTextParagraph htp : paras) {
  106. int txType = htp.getRunType();
  107. if (txType >= _txmaster.length || _txmaster[txType] == null) {
  108. throw new HSLFException("Master styles not initialized");
  109. }
  110. int level = htp.getIndentLevel();
  111. List<TextPropCollection> charStyles = _txmaster[txType].getCharacterStyles();
  112. List<TextPropCollection> paragraphStyles = _txmaster[txType].getParagraphStyles();
  113. if (charStyles == null || paragraphStyles == null ||
  114. charStyles.size() <= level || paragraphStyles.size() <= level) {
  115. throw new HSLFException("Master styles not initialized");
  116. }
  117. htp.setMasterStyleReference(paragraphStyles.get(level));
  118. for (HSLFTextRun htr : htp.getTextRuns()) {
  119. htr.setMasterStyleReference(charStyles.get(level));
  120. }
  121. }
  122. }
  123. }
  124. protected void onAddTextShape(HSLFTextShape shape) {
  125. List<HSLFTextParagraph> runs = shape.getTextParagraphs();
  126. _paragraphs.add(runs);
  127. }
  128. public TxMasterStyleAtom[] getTxMasterStyleAtoms(){
  129. return _txmaster;
  130. }
  131. }