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.

TextObjectRecord.java 13KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346
  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.hssf.record;
  16. import java.util.Collections;
  17. import java.util.LinkedHashMap;
  18. import java.util.Map;
  19. import java.util.function.Supplier;
  20. import org.apache.poi.hssf.record.cont.ContinuableRecord;
  21. import org.apache.poi.hssf.record.cont.ContinuableRecordOutput;
  22. import org.apache.poi.hssf.usermodel.HSSFRichTextString;
  23. import org.apache.poi.ss.formula.ptg.OperandPtg;
  24. import org.apache.poi.ss.formula.ptg.Ptg;
  25. import org.apache.poi.util.BitField;
  26. import org.apache.poi.util.BitFieldFactory;
  27. import org.apache.poi.util.RecordFormatException;
  28. /**
  29. * The TXO record (0x01B6) is used to define the properties of a text box. It is
  30. * followed by two or more continue records unless there is no actual text. The
  31. * first continue records contain the text data and the last continue record
  32. * contains the formatting runs.
  33. */
  34. public final class TextObjectRecord extends ContinuableRecord {
  35. public static final short sid = 0x01B6;
  36. private static final int FORMAT_RUN_ENCODED_SIZE = 8; // 2 shorts and 4 bytes reserved
  37. private static final BitField HorizontalTextAlignment = BitFieldFactory.getInstance(0x000E);
  38. private static final BitField VerticalTextAlignment = BitFieldFactory.getInstance(0x0070);
  39. private static final BitField textLocked = BitFieldFactory.getInstance(0x0200);
  40. public static final short HORIZONTAL_TEXT_ALIGNMENT_LEFT_ALIGNED = 1;
  41. public static final short HORIZONTAL_TEXT_ALIGNMENT_CENTERED = 2;
  42. public static final short HORIZONTAL_TEXT_ALIGNMENT_RIGHT_ALIGNED = 3;
  43. public static final short HORIZONTAL_TEXT_ALIGNMENT_JUSTIFIED = 4;
  44. public static final short VERTICAL_TEXT_ALIGNMENT_TOP = 1;
  45. public static final short VERTICAL_TEXT_ALIGNMENT_CENTER = 2;
  46. public static final short VERTICAL_TEXT_ALIGNMENT_BOTTOM = 3;
  47. public static final short VERTICAL_TEXT_ALIGNMENT_JUSTIFY = 4;
  48. public static final short TEXT_ORIENTATION_NONE = 0;
  49. public static final short TEXT_ORIENTATION_TOP_TO_BOTTOM = 1;
  50. public static final short TEXT_ORIENTATION_ROT_RIGHT = 2;
  51. public static final short TEXT_ORIENTATION_ROT_LEFT = 3;
  52. private int field_1_options;
  53. private int field_2_textOrientation;
  54. private int field_3_reserved4;
  55. private int field_4_reserved5;
  56. private int field_5_reserved6;
  57. private int field_8_reserved7;
  58. private HSSFRichTextString _text;
  59. /*
  60. * Note - the next three fields are very similar to those on
  61. * EmbededObjectRefSubRecord(ftPictFmla 0x0009)
  62. *
  63. * some observed values for the 4 bytes preceding the formula: C0 5E 86 03
  64. * C0 11 AC 02 80 F1 8A 03 D4 F0 8A 03
  65. */
  66. private int _unknownPreFormulaInt;
  67. /** expect tRef, tRef3D, tArea, tArea3D or tName */
  68. private OperandPtg _linkRefPtg;
  69. /**
  70. * Not clear if needed . Excel seems to be OK if this byte is not present.
  71. * Value is often the same as the earlier firstColumn byte. */
  72. private Byte _unknownPostFormulaByte;
  73. public TextObjectRecord() {}
  74. public TextObjectRecord(TextObjectRecord other) {
  75. super(other);
  76. field_1_options = other.field_1_options;
  77. field_2_textOrientation = other.field_2_textOrientation;
  78. field_3_reserved4 = other.field_3_reserved4;
  79. field_4_reserved5 = other.field_4_reserved5;
  80. field_5_reserved6 = other.field_5_reserved6;
  81. field_8_reserved7 = other.field_8_reserved7;
  82. _text = other._text;
  83. if (other._linkRefPtg != null) {
  84. _unknownPreFormulaInt = other._unknownPreFormulaInt;
  85. _linkRefPtg = other._linkRefPtg.copy();
  86. _unknownPostFormulaByte = other._unknownPostFormulaByte;
  87. }
  88. }
  89. public TextObjectRecord(RecordInputStream in) {
  90. field_1_options = in.readUShort();
  91. field_2_textOrientation = in.readUShort();
  92. field_3_reserved4 = in.readUShort();
  93. field_4_reserved5 = in.readUShort();
  94. field_5_reserved6 = in.readUShort();
  95. int field_6_textLength = in.readUShort();
  96. int field_7_formattingDataLength = in.readUShort();
  97. field_8_reserved7 = in.readInt();
  98. if (in.remaining() > 0) {
  99. // Text Objects can have simple reference formulas
  100. // (This bit not mentioned in the MS document)
  101. if (in.remaining() < 11) {
  102. throw new RecordFormatException("Not enough remaining data for a link formula");
  103. }
  104. int formulaSize = in.readUShort();
  105. _unknownPreFormulaInt = in.readInt();
  106. Ptg[] ptgs = Ptg.readTokens(formulaSize, in);
  107. if (ptgs.length != 1) {
  108. throw new RecordFormatException("Read " + ptgs.length
  109. + " tokens but expected exactly 1");
  110. }
  111. if (!(ptgs[0] instanceof OperandPtg)) {
  112. throw new IllegalArgumentException("Had unexpected type of ptg at index 0: " + ptgs[0].getClass());
  113. }
  114. _linkRefPtg = (OperandPtg) ptgs[0];
  115. _unknownPostFormulaByte = in.remaining() > 0 ? in.readByte() : null;
  116. } else {
  117. _linkRefPtg = null;
  118. }
  119. if (in.remaining() > 0) {
  120. throw new RecordFormatException("Unused " + in.remaining() + " bytes at end of record");
  121. }
  122. String text;
  123. if (field_6_textLength > 0) {
  124. text = readRawString(in, field_6_textLength);
  125. } else {
  126. text = "";
  127. }
  128. _text = new HSSFRichTextString(text);
  129. if (field_7_formattingDataLength > 0) {
  130. processFontRuns(in, _text, field_7_formattingDataLength);
  131. }
  132. }
  133. private static String readRawString(RecordInputStream in, int textLength) {
  134. byte compressByte = in.readByte();
  135. boolean isCompressed = (compressByte & 0x01) == 0;
  136. if (isCompressed) {
  137. return in.readCompressedUnicode(textLength);
  138. }
  139. return in.readUnicodeLEString(textLength);
  140. }
  141. private static void processFontRuns(RecordInputStream in, HSSFRichTextString str,
  142. int formattingRunDataLength) {
  143. if (formattingRunDataLength % FORMAT_RUN_ENCODED_SIZE != 0) {
  144. throw new RecordFormatException("Bad format run data length " + formattingRunDataLength
  145. + ")");
  146. }
  147. int nRuns = formattingRunDataLength / FORMAT_RUN_ENCODED_SIZE;
  148. for (int i = 0; i < nRuns; i++) {
  149. short index = in.readShort();
  150. short iFont = in.readShort();
  151. in.readInt(); // skip reserved.
  152. str.applyFont(index, str.length(), iFont);
  153. }
  154. }
  155. public short getSid() {
  156. return sid;
  157. }
  158. private void serializeTXORecord(ContinuableRecordOutput out) {
  159. out.writeShort(field_1_options);
  160. out.writeShort(field_2_textOrientation);
  161. out.writeShort(field_3_reserved4);
  162. out.writeShort(field_4_reserved5);
  163. out.writeShort(field_5_reserved6);
  164. out.writeShort(_text.length());
  165. out.writeShort(getFormattingDataLength());
  166. out.writeInt(field_8_reserved7);
  167. if (_linkRefPtg != null) {
  168. int formulaSize = _linkRefPtg.getSize();
  169. out.writeShort(formulaSize);
  170. out.writeInt(_unknownPreFormulaInt);
  171. _linkRefPtg.write(out);
  172. if (_unknownPostFormulaByte != null) {
  173. out.writeByte(_unknownPostFormulaByte);
  174. }
  175. }
  176. }
  177. private void serializeTrailingRecords(ContinuableRecordOutput out) {
  178. out.writeContinue();
  179. out.writeStringData(_text.getString());
  180. out.writeContinue();
  181. writeFormatData(out, _text);
  182. }
  183. protected void serialize(ContinuableRecordOutput out) {
  184. serializeTXORecord(out);
  185. if (_text.getString().length() > 0) {
  186. serializeTrailingRecords(out);
  187. }
  188. }
  189. private int getFormattingDataLength() {
  190. if (_text.length() < 1) {
  191. // important - no formatting data if text is empty
  192. return 0;
  193. }
  194. return (_text.numFormattingRuns() + 1) * FORMAT_RUN_ENCODED_SIZE;
  195. }
  196. private static void writeFormatData(ContinuableRecordOutput out , HSSFRichTextString str) {
  197. int nRuns = str.numFormattingRuns();
  198. for (int i = 0; i < nRuns; i++) {
  199. out.writeShort(str.getIndexOfFormattingRun(i));
  200. int fontIndex = str.getFontOfFormattingRun(i);
  201. out.writeShort(fontIndex == HSSFRichTextString.NO_FONT ? 0 : fontIndex);
  202. out.writeInt(0); // skip reserved
  203. }
  204. out.writeShort(str.length());
  205. out.writeShort(0);
  206. out.writeInt(0); // skip reserved
  207. }
  208. /**
  209. * Sets the Horizontal text alignment field value.
  210. *
  211. * @param value The horizontal alignment, use one of the HORIZONTAL_TEXT_ALIGNMENT_... constants in this class
  212. */
  213. public void setHorizontalTextAlignment(int value) {
  214. field_1_options = HorizontalTextAlignment.setValue(field_1_options, value);
  215. }
  216. /**
  217. * @return the Horizontal text alignment field value.
  218. */
  219. public int getHorizontalTextAlignment() {
  220. return HorizontalTextAlignment.getValue(field_1_options);
  221. }
  222. /**
  223. * Sets the Vertical text alignment field value.
  224. *
  225. * @param value The vertical alignment, use one of the VERTIUCAL_TEST_ALIGNMENT_... constants in this class
  226. */
  227. public void setVerticalTextAlignment(int value) {
  228. field_1_options = VerticalTextAlignment.setValue(field_1_options, value);
  229. }
  230. /**
  231. * @return the Vertical text alignment field value.
  232. */
  233. public int getVerticalTextAlignment() {
  234. return VerticalTextAlignment.getValue(field_1_options);
  235. }
  236. /**
  237. * Sets the text locked field value.
  238. *
  239. * @param value If the text should be locked
  240. */
  241. public void setTextLocked(boolean value) {
  242. field_1_options = textLocked.setBoolean(field_1_options, value);
  243. }
  244. /**
  245. * @return the text locked field value.
  246. */
  247. public boolean isTextLocked() {
  248. return textLocked.isSet(field_1_options);
  249. }
  250. /**
  251. * Get the text orientation field for the TextObjectBase record.
  252. *
  253. * @return One of TEXT_ORIENTATION_NONE TEXT_ORIENTATION_TOP_TO_BOTTOM
  254. * TEXT_ORIENTATION_ROT_RIGHT TEXT_ORIENTATION_ROT_LEFT
  255. */
  256. public int getTextOrientation() {
  257. return field_2_textOrientation;
  258. }
  259. /**
  260. * Set the text orientation field for the TextObjectBase record.
  261. *
  262. * @param textOrientation
  263. * One of TEXT_ORIENTATION_NONE TEXT_ORIENTATION_TOP_TO_BOTTOM
  264. * TEXT_ORIENTATION_ROT_RIGHT TEXT_ORIENTATION_ROT_LEFT
  265. */
  266. public void setTextOrientation(int textOrientation) {
  267. this.field_2_textOrientation = textOrientation;
  268. }
  269. public HSSFRichTextString getStr() {
  270. return _text;
  271. }
  272. public void setStr(HSSFRichTextString str) {
  273. _text = str;
  274. }
  275. public Ptg getLinkRefPtg() {
  276. return _linkRefPtg;
  277. }
  278. @Override
  279. public TextObjectRecord copy() {
  280. return new TextObjectRecord(this);
  281. }
  282. @Override
  283. public HSSFRecordTypes getGenericRecordType() {
  284. return HSSFRecordTypes.TEXT_OBJECT;
  285. }
  286. @Override
  287. public Map<String, Supplier<?>> getGenericProperties() {
  288. final Map<String,Supplier<?>> m = new LinkedHashMap<>();
  289. m.put("isHorizontal", this::getHorizontalTextAlignment);
  290. m.put("isVertical", this::getVerticalTextAlignment);
  291. m.put("textLocked", this::isTextLocked);
  292. m.put("textOrientation", this::getTextOrientation);
  293. m.put("string", this::getStr);
  294. m.put("reserved4", () -> field_3_reserved4);
  295. m.put("reserved5", () -> field_4_reserved5);
  296. m.put("reserved6", () -> field_5_reserved6);
  297. m.put("reserved7", () -> field_8_reserved7);
  298. return Collections.unmodifiableMap(m);
  299. }
  300. }