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.

TextArea.java 6.9KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222
  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. *
  9. * http://www.apache.org/licenses/LICENSE-2.0
  10. *
  11. * Unless required by applicable law or agreed to in writing, software
  12. * distributed under the License is distributed on an "AS IS" BASIS,
  13. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  14. * See the License for the specific language governing permissions and
  15. * limitations under the License.
  16. */
  17. /* $Id$ */
  18. package org.apache.fop.area.inline;
  19. import java.util.Arrays;
  20. import org.apache.fop.util.CharUtilities;
  21. /**
  22. * A text inline area.
  23. */
  24. public class TextArea extends AbstractTextArea {
  25. private static final long serialVersionUID = 7315900267242540809L;
  26. private boolean isHyphenated;
  27. /**
  28. * Create a text inline area
  29. */
  30. public TextArea() {
  31. }
  32. /**
  33. * Constructor with extra parameters:
  34. * create a TextAdjustingInfo object
  35. * @param stretch the available stretch of the text
  36. * @param shrink the available shrink of the text
  37. * @param adj the current total adjustment
  38. */
  39. public TextArea(int stretch, int shrink, int adj) {
  40. super(stretch, shrink, adj);
  41. }
  42. /**
  43. * Remove the old text
  44. */
  45. public void removeText() {
  46. inlines.clear();
  47. }
  48. /**
  49. * Create and add a WordArea child to this TextArea.
  50. *
  51. * @param word the word string
  52. * @param offset the offset for the next area
  53. */
  54. public void addWord(String word, int offset) {
  55. addWord(word, 0, null, null, null, offset);
  56. }
  57. /**
  58. * Create and add a WordArea child to this TextArea.
  59. *
  60. * @param word the word string
  61. * @param offset the offset for the next area
  62. * @param level bidirectional level that applies to entire word
  63. */
  64. public void addWord(String word, int offset, int level) {
  65. addWord(word, 0, null, makeLevels(level, word.length()), null, offset);
  66. }
  67. /**
  68. * Create and add a WordArea child to this TextArea.
  69. *
  70. * @param word the word string
  71. * @param ipd the word's ipd
  72. * @param letterAdjust the letter adjustment array (may be null)
  73. * @param levels array of resolved bidirectional levels of word characters,
  74. * or null if default level
  75. * @param gposAdjustments array of general position adjustments or null if none apply
  76. * @param blockProgressionOffset the offset for the next area
  77. */
  78. public void addWord(String word, int ipd, int[] letterAdjust, int[] levels, int[][] gposAdjustments,
  79. int blockProgressionOffset, boolean nextIsSpace) {
  80. int minWordLevel = findMinLevel(levels, getBidiLevel());
  81. WordArea wordArea = new WordArea(blockProgressionOffset, minWordLevel, word, letterAdjust, levels,
  82. gposAdjustments, false, nextIsSpace);
  83. wordArea.setIPD(ipd);
  84. wordArea.setChangeBarList(getChangeBarList());
  85. addChildArea(wordArea);
  86. wordArea.setParentArea(this);
  87. updateLevel(minWordLevel);
  88. }
  89. public void addWord(String word, int ipd, int[] letterAdjust, int[] levels, int[][] gposAdjustments,
  90. int blockProgressionOffset) {
  91. addWord(word, ipd, letterAdjust, levels, gposAdjustments, blockProgressionOffset, false);
  92. }
  93. /**
  94. * Create and add a SpaceArea child to this TextArea
  95. *
  96. * @param space the space character
  97. * @param ipd the space's ipd
  98. * @param blockProgressionOffset the offset for the next area
  99. * @param adjustable is this space adjustable?
  100. * @param level resolved bidirection level of space character
  101. */
  102. public void addSpace(
  103. char space, int ipd, boolean adjustable, int blockProgressionOffset, int level) {
  104. SpaceArea spaceArea = new SpaceArea(blockProgressionOffset, level, space, adjustable);
  105. spaceArea.setIPD(ipd);
  106. spaceArea.setChangeBarList(getChangeBarList());
  107. addChildArea(spaceArea);
  108. spaceArea.setParentArea(this);
  109. updateLevel(level);
  110. }
  111. /**
  112. * Records that the last word in this text area is hyphenated.
  113. */
  114. public void setHyphenated() {
  115. this.isHyphenated = true;
  116. }
  117. /**
  118. * Returns {@code true} if the last word in this area is hyphenated.
  119. */
  120. public boolean isHyphenated() {
  121. return isHyphenated;
  122. }
  123. /**
  124. * Get the whole text string.
  125. * Renderers whose space adjustment handling is not affected
  126. * by multi-byte characters can use this method to render the
  127. * whole TextArea at once; the other renderers (for example
  128. * PDFRenderer) have to implement renderWord(WordArea) and
  129. * renderSpace(SpaceArea) in order to correctly place each
  130. * text fragment.
  131. *
  132. * @return the text string
  133. */
  134. public String getText() {
  135. StringBuilder text = new StringBuilder();
  136. // assemble the text
  137. for (InlineArea inline : inlines) {
  138. if (inline instanceof WordArea) {
  139. text.append(((WordArea) inline).getWord());
  140. } else {
  141. assert (inline instanceof SpaceArea);
  142. text.append(((SpaceArea) inline).getSpace());
  143. }
  144. }
  145. return text.toString();
  146. }
  147. /** {@inheritDoc} */
  148. @Override
  149. public String toString() {
  150. StringBuffer sb = new StringBuffer(super.toString());
  151. sb.append(" {text=\"");
  152. sb.append(CharUtilities.toNCRefs(getText()));
  153. sb.append("\"");
  154. sb.append("}");
  155. return sb.toString();
  156. }
  157. public void updateLevel(int newLevel) {
  158. if (newLevel >= 0) {
  159. int curLevel = getBidiLevel();
  160. if (curLevel >= 0) {
  161. if (newLevel < curLevel) {
  162. setBidiLevel(newLevel);
  163. }
  164. } else {
  165. setBidiLevel(newLevel);
  166. }
  167. }
  168. }
  169. private static int findMinLevel(int[] levels, int defaultLevel) {
  170. if (levels != null) {
  171. int lMin = Integer.MAX_VALUE;
  172. for (int l : levels) {
  173. if ((l >= 0) && (l < lMin)) {
  174. lMin = l;
  175. }
  176. }
  177. if (lMin == Integer.MAX_VALUE) {
  178. return -1;
  179. } else {
  180. return lMin;
  181. }
  182. } else {
  183. return defaultLevel;
  184. }
  185. }
  186. private int[] makeLevels(int level, int count) {
  187. if (level >= 0) {
  188. int[] levels = new int [ count ];
  189. Arrays.fill(levels, level);
  190. return levels;
  191. } else {
  192. return null;
  193. }
  194. }
  195. public int getEffectiveIPD() {
  196. return getIPD();
  197. }
  198. }