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.

TabStopPropCollection.java 4.2KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  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.textproperties;
  16. import java.util.ArrayList;
  17. import java.util.List;
  18. import org.apache.poi.util.LittleEndian;
  19. import org.apache.poi.util.LittleEndianConsts;
  20. /**
  21. * Container for tabstop lists
  22. */
  23. public class TabStopPropCollection extends TextProp {
  24. public enum TabStopType {
  25. LEFT(0), CENTER(1), RIGHT(2), DECIMAL(3);
  26. private final int val;
  27. TabStopType(int val) {
  28. this.val = val;
  29. }
  30. public static TabStopType fromRecordVal(int val) {
  31. for (TabStopType tst : values()) {
  32. if (tst.val == val) return tst;
  33. }
  34. return LEFT;
  35. }
  36. }
  37. public static class TabStop {
  38. /**
  39. * If the TextPFException record that contains this TabStop structure also contains a
  40. * leftMargin, then the value of position is relative to the left margin of the paragraph;
  41. * otherwise, the value is relative to the left side of the paragraph.
  42. *
  43. * If a TextRuler record contains this TabStop structure, the value is relative to the
  44. * left side of the text ruler.
  45. */
  46. private int position;
  47. /**
  48. * A enumeration that specifies how text aligns at the tab stop.
  49. */
  50. private TabStopType type;
  51. public TabStop(int position, TabStopType type) {
  52. this.position = position;
  53. this.type = type;
  54. }
  55. public int getPosition() {
  56. return position;
  57. }
  58. public void setPosition(int position) {
  59. this.position = position;
  60. }
  61. public TabStopType getType() {
  62. return type;
  63. }
  64. public void setType(TabStopType type) {
  65. this.type = type;
  66. }
  67. }
  68. private List<TabStop> tabStops = new ArrayList<TabStop>();
  69. public TabStopPropCollection() {
  70. super(0, 0x100000, "tabStops");
  71. }
  72. /**
  73. * Parses the tabstops from TxMasterStyle record
  74. *
  75. * @param data the data stream
  76. * @param offset the offset within the data
  77. */
  78. public void parseProperty(byte data[], int offset) {
  79. int count = LittleEndian.getUShort(data, offset);
  80. int off = offset + LittleEndianConsts.SHORT_SIZE;
  81. for (int i=0; i<count; i++) {
  82. int position = LittleEndian.getShort(data, off);
  83. off += LittleEndianConsts.SHORT_SIZE;
  84. int recVal = LittleEndian.getShort(data, off);
  85. TabStopType type = TabStopType.fromRecordVal(recVal);
  86. off += LittleEndianConsts.SHORT_SIZE;
  87. tabStops.add(new TabStop(position, type));
  88. }
  89. }
  90. @Override
  91. public int getSize() {
  92. return LittleEndianConsts.SHORT_SIZE + tabStops.size()*LittleEndianConsts.INT_SIZE;
  93. }
  94. @Override
  95. public TabStopPropCollection clone() {
  96. TabStopPropCollection other = (TabStopPropCollection)super.clone();
  97. other.tabStops = new ArrayList<TabStop>();
  98. for (TabStop ts : tabStops) {
  99. TabStop tso = new TabStop(ts.getPosition(), ts.getType());
  100. other.tabStops.add(tso);
  101. }
  102. return other;
  103. }
  104. }