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.6KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218
  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(
  79. String word, int ipd, int[] letterAdjust, int[] levels,
  80. int[][] gposAdjustments, int blockProgressionOffset) {
  81. int minWordLevel = findMinLevel(levels, getBidiLevel());
  82. WordArea wordArea = new WordArea(
  83. blockProgressionOffset, minWordLevel, word, letterAdjust, levels, gposAdjustments);
  84. wordArea.setIPD(ipd);
  85. wordArea.setChangeBarList(getChangeBarList());
  86. addChildArea(wordArea);
  87. wordArea.setParentArea(this);
  88. updateLevel(minWordLevel);
  89. }
  90. /**
  91. * Create and add a SpaceArea child to this TextArea
  92. *
  93. * @param space the space character
  94. * @param ipd the space's ipd
  95. * @param blockProgressionOffset the offset for the next area
  96. * @param adjustable is this space adjustable?
  97. * @param level resolved bidirection level of space character
  98. */
  99. public void addSpace(
  100. char space, int ipd, boolean adjustable, int blockProgressionOffset, int level) {
  101. SpaceArea spaceArea = new SpaceArea(blockProgressionOffset, level, space, adjustable);
  102. spaceArea.setIPD(ipd);
  103. spaceArea.setChangeBarList(getChangeBarList());
  104. addChildArea(spaceArea);
  105. spaceArea.setParentArea(this);
  106. updateLevel(level);
  107. }
  108. /**
  109. * Records that the last word in this text area is hyphenated.
  110. */
  111. public void setHyphenated() {
  112. this.isHyphenated = true;
  113. }
  114. /**
  115. * Returns {@code true} if the last word in this area is hyphenated.
  116. */
  117. public boolean isHyphenated() {
  118. return isHyphenated;
  119. }
  120. /**
  121. * Get the whole text string.
  122. * Renderers whose space adjustment handling is not affected
  123. * by multi-byte characters can use this method to render the
  124. * whole TextArea at once; the other renderers (for example
  125. * PDFRenderer) have to implement renderWord(WordArea) and
  126. * renderSpace(SpaceArea) in order to correctly place each
  127. * text fragment.
  128. *
  129. * @return the text string
  130. */
  131. public String getText() {
  132. StringBuilder text = new StringBuilder();
  133. // assemble the text
  134. for (InlineArea inline : inlines) {
  135. if (inline instanceof WordArea) {
  136. text.append(((WordArea) inline).getWord());
  137. } else {
  138. assert (inline instanceof SpaceArea);
  139. text.append(((SpaceArea) inline).getSpace());
  140. }
  141. }
  142. return text.toString();
  143. }
  144. /** {@inheritDoc} */
  145. @Override
  146. public String toString() {
  147. StringBuffer sb = new StringBuffer(super.toString());
  148. sb.append(" {text=\"");
  149. sb.append(CharUtilities.toNCRefs(getText()));
  150. sb.append("\"");
  151. sb.append("}");
  152. return sb.toString();
  153. }
  154. public void updateLevel(int newLevel) {
  155. if (newLevel >= 0) {
  156. int curLevel = getBidiLevel();
  157. if (curLevel >= 0) {
  158. if (newLevel < curLevel) {
  159. setBidiLevel(newLevel);
  160. }
  161. } else {
  162. setBidiLevel(newLevel);
  163. }
  164. }
  165. }
  166. private static int findMinLevel(int[] levels, int defaultLevel) {
  167. if (levels != null) {
  168. int lMin = Integer.MAX_VALUE;
  169. for (int l : levels) {
  170. if ((l >= 0) && (l < lMin)) {
  171. lMin = l;
  172. }
  173. }
  174. if (lMin == Integer.MAX_VALUE) {
  175. return -1;
  176. } else {
  177. return lMin;
  178. }
  179. } else {
  180. return defaultLevel;
  181. }
  182. }
  183. private int[] makeLevels(int level, int count) {
  184. if (level >= 0) {
  185. int[] levels = new int [ count ];
  186. Arrays.fill(levels, level);
  187. return levels;
  188. } else {
  189. return null;
  190. }
  191. }
  192. public int getEffectiveIPD() {
  193. return getIPD();
  194. }
  195. }