Ви не можете вибрати більше 25 тем Теми мають розпочинатися з літери або цифри, можуть містити дефіси (-) і не повинні перевищувати 35 символів.

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