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.

TxMasterStyleAtom.java 6.9KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209
  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.record;
  16. import java.io.ByteArrayOutputStream;
  17. import java.io.IOException;
  18. import java.io.OutputStream;
  19. import java.util.ArrayList;
  20. import java.util.List;
  21. import org.apache.poi.hslf.exceptions.HSLFException;
  22. import org.apache.poi.hslf.model.textproperties.TextPropCollection;
  23. import org.apache.poi.hslf.model.textproperties.TextPropCollection.TextPropType;
  24. import org.apache.poi.util.LittleEndian;
  25. import org.apache.poi.util.LittleEndianOutputStream;
  26. /**
  27. * TxMasterStyleAtom atom (4003).
  28. * <p>
  29. * Stores default character and paragraph styles.
  30. * The atom instance value is the text type and is encoded like the txstyle field in
  31. * TextHeaderAtom. The text styles are located in the MainMaster container,
  32. * except for the "other" style, which is in the Document.Environment container.
  33. * </p>
  34. * <p>
  35. * This atom can store up to 5 pairs of paragraph+character styles,
  36. * each pair describes an indent level. The first pair describes
  37. * first-level paragraph with no indentation.
  38. * </p>
  39. *
  40. * @author Yegor Kozlov
  41. */
  42. public final class TxMasterStyleAtom extends RecordAtom {
  43. /**
  44. * Maximum number of indentation levels allowed in PowerPoint documents
  45. */
  46. public static final int MAX_INDENT = 5;
  47. private byte[] _header;
  48. private static long _type = 4003;
  49. private byte[] _data;
  50. private List<TextPropCollection> paragraphStyles;
  51. private List<TextPropCollection> charStyles;
  52. protected TxMasterStyleAtom(byte[] source, int start, int len) {
  53. _header = new byte[8];
  54. System.arraycopy(source,start,_header,0,8);
  55. _data = new byte[len-8];
  56. System.arraycopy(source,start+8,_data,0,_data.length);
  57. //read available styles
  58. try {
  59. init();
  60. } catch (Exception e){
  61. e.printStackTrace();
  62. }
  63. }
  64. /**
  65. * We are of type 4003
  66. *
  67. * @return type of this record
  68. * @see RecordTypes#TxMasterStyleAtom
  69. */
  70. public long getRecordType() {
  71. return _type;
  72. }
  73. /**
  74. * Write the contents of the record back, so it can be written
  75. * to disk
  76. */
  77. public void writeOut(OutputStream out) throws IOException {
  78. // Write out the (new) header
  79. out.write(_header);
  80. // Write out the record data
  81. out.write(_data);
  82. }
  83. /**
  84. * Returns array of character styles defined in this record.
  85. *
  86. * @return character styles defined in this record
  87. */
  88. public List<TextPropCollection> getCharacterStyles(){
  89. return charStyles;
  90. }
  91. /**
  92. * Returns array of paragraph styles defined in this record.
  93. *
  94. * @return paragraph styles defined in this record
  95. */
  96. public List<TextPropCollection> getParagraphStyles(){
  97. return paragraphStyles;
  98. }
  99. /**
  100. * Return type of the text.
  101. * Must be a constant defined in <code>TextHeaderAtom</code>
  102. *
  103. * @return type of the text
  104. * @see TextHeaderAtom
  105. */
  106. public int getTextType(){
  107. //The atom instance value is the text type
  108. return LittleEndian.getShort(_header, 0) >> 4;
  109. }
  110. /**
  111. * parse the record data and initialize styles
  112. */
  113. protected void init(){
  114. //type of the text
  115. int type = getTextType();
  116. int head;
  117. int pos = 0;
  118. //number of indentation levels
  119. short levels = LittleEndian.getShort(_data, 0);
  120. pos += LittleEndian.SHORT_SIZE;
  121. paragraphStyles = new ArrayList<TextPropCollection>(levels);
  122. charStyles = new ArrayList<TextPropCollection>(levels);
  123. for(short i = 0; i < levels; i++) {
  124. TextPropCollection prprops = new TextPropCollection(0, TextPropType.paragraph);
  125. if (type >= TextHeaderAtom.CENTRE_BODY_TYPE) {
  126. // Fetch the 2 byte value, that is safe to ignore for some types of text
  127. short indentLevel = LittleEndian.getShort(_data, pos);
  128. prprops.setIndentLevel(indentLevel);
  129. pos += LittleEndian.SHORT_SIZE;
  130. } else {
  131. prprops.setIndentLevel((short)-1);
  132. }
  133. head = LittleEndian.getInt(_data, pos);
  134. pos += LittleEndian.INT_SIZE;
  135. pos += prprops.buildTextPropList( head, _data, pos);
  136. paragraphStyles.add(prprops);
  137. head = LittleEndian.getInt(_data, pos);
  138. pos += LittleEndian.INT_SIZE;
  139. TextPropCollection chprops = new TextPropCollection(0, TextPropType.character);
  140. pos += chprops.buildTextPropList( head, _data, pos);
  141. charStyles.add(chprops);
  142. }
  143. }
  144. /**
  145. * Updates the rawdata from the modified paragraph/character styles
  146. *
  147. * @since 3.14-beta1
  148. */
  149. public void updateStyles() {
  150. int type = getTextType();
  151. try {
  152. ByteArrayOutputStream bos = new ByteArrayOutputStream();
  153. LittleEndianOutputStream leos = new LittleEndianOutputStream(bos);
  154. int levels = paragraphStyles.size();
  155. leos.writeShort(levels);
  156. TextPropCollection prdummy = new TextPropCollection(0, TextPropType.paragraph);
  157. TextPropCollection chdummy = new TextPropCollection(0, TextPropType.character);
  158. for (int i=0; i<levels; i++) {
  159. prdummy.copy(paragraphStyles.get(i));
  160. chdummy.copy(charStyles.get(i));
  161. if (type >= TextHeaderAtom.CENTRE_BODY_TYPE) {
  162. leos.writeShort(prdummy.getIndentLevel());
  163. }
  164. // Indent level is not written for master styles
  165. prdummy.setIndentLevel((short)-1);
  166. prdummy.writeOut(bos, true);
  167. chdummy.writeOut(bos, true);
  168. }
  169. _data = bos.toByteArray();
  170. leos.close();
  171. LittleEndian.putInt(_header, 4, _data.length);
  172. } catch (IOException e) {
  173. throw new HSLFException("error in updating master style properties", e);
  174. }
  175. }
  176. }