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.

DelimitedTextRange.java 8.5KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225
  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.complexscripts.bidi;
  19. import java.util.Iterator;
  20. import java.util.List;
  21. import java.util.Vector;
  22. import org.apache.commons.logging.Log;
  23. import org.apache.commons.logging.LogFactory;
  24. import org.apache.fop.fo.CharIterator;
  25. import org.apache.fop.fo.FONode;
  26. import org.apache.fop.fo.FObj;
  27. import org.apache.fop.traits.Direction;
  28. import org.apache.fop.traits.WritingModeTraits;
  29. import org.apache.fop.traits.WritingModeTraitsGetter;
  30. import org.apache.fop.util.CharUtilities;
  31. // CSOFF: LineLengthCheck
  32. /**
  33. * The <code>DelimitedTextRange</code> class implements the "delimited text range" as described
  34. * by XML-FO 1.1 §5.8, which contains a flattened sequence of characters. Any FO that generates
  35. * block areas serves as a delimiter.
  36. *
  37. * <p>This work was originally authored by Glenn Adams (gadams@apache.org).</p>
  38. */
  39. public class DelimitedTextRange {
  40. private FONode fn; // node that generates this text range
  41. private StringBuffer buffer; // flattened character sequence of generating FO nodes
  42. private List intervals; // list of intervals over buffer of generating FO nodes
  43. /**
  44. * Primary constructor.
  45. * @param fn node that generates this text range
  46. */
  47. public DelimitedTextRange(FONode fn) {
  48. this.fn = fn;
  49. this.buffer = new StringBuffer();
  50. this.intervals = new Vector();
  51. }
  52. /**
  53. * Obtain node that generated this text range.
  54. * @return node that generated this text range
  55. */
  56. public FONode getNode() {
  57. return fn;
  58. }
  59. /**
  60. * Append interval using characters from character iterator IT.
  61. * @param it character iterator
  62. * @param fn node that generates interval being appended
  63. */
  64. public void append(CharIterator it, FONode fn) {
  65. if (it != null) {
  66. int s = buffer.length();
  67. int e = s;
  68. while (it.hasNext()) {
  69. char c = it.nextChar();
  70. buffer.append(c);
  71. e++;
  72. }
  73. intervals.add(new TextInterval(fn, s, e));
  74. }
  75. }
  76. /**
  77. * Append interval using character C.
  78. * @param c character
  79. * @param fn node that generates interval being appended
  80. */
  81. public void append(char c, FONode fn) {
  82. if (c != 0) {
  83. int s = buffer.length();
  84. int e = s + 1;
  85. buffer.append(c);
  86. intervals.add(new TextInterval(fn, s, e));
  87. }
  88. }
  89. /**
  90. * Determine if range is empty.
  91. * @return true if range is empty
  92. */
  93. public boolean isEmpty() {
  94. return buffer.length() == 0;
  95. }
  96. /**
  97. * Resolve bidirectional levels for this range.
  98. */
  99. public void resolve() {
  100. WritingModeTraitsGetter tg;
  101. if ((tg = WritingModeTraits.getWritingModeTraitsGetter(getNode())) != null) {
  102. resolve(tg.getInlineProgressionDirection());
  103. }
  104. }
  105. @Override
  106. public String toString() {
  107. StringBuffer sb = new StringBuffer("DR: " + fn.getLocalName() + " { <" + CharUtilities.toNCRefs(buffer.toString()) + ">");
  108. sb.append(", intervals <");
  109. boolean first = true;
  110. for (Iterator it = intervals.iterator(); it.hasNext(); ) {
  111. TextInterval ti = (TextInterval) it.next();
  112. if (first) {
  113. first = false;
  114. } else {
  115. sb.append(',');
  116. }
  117. sb.append(ti.toString());
  118. }
  119. sb.append("> }");
  120. return sb.toString();
  121. }
  122. private void resolve(Direction paragraphEmbeddingLevel) {
  123. int [] levels;
  124. if ((levels = UnicodeBidiAlgorithm.resolveLevels(buffer, paragraphEmbeddingLevel)) != null) {
  125. assignLevels(levels);
  126. assignBlockLevel(paragraphEmbeddingLevel);
  127. assignTextLevels();
  128. }
  129. }
  130. /**
  131. * <p>Assign resolved levels to all text intervals of this delimited text range.</p>
  132. * <p>Has a possible side effect of replacing the intervals array with a new array
  133. * containing new text intervals, such that each text interval is associated with
  134. * a single level run.</p>
  135. * @param levels array of levels each corresponding to each index of the delimited
  136. * text range
  137. */
  138. private void assignLevels(int[] levels) {
  139. Vector intervalsNew = new Vector(intervals.size());
  140. for (Iterator it = intervals.iterator(); it.hasNext(); ) {
  141. TextInterval ti = (TextInterval) it.next();
  142. intervalsNew.addAll(assignLevels(ti, levels));
  143. }
  144. if (!intervalsNew.equals(intervals)) {
  145. intervals = intervalsNew;
  146. }
  147. }
  148. /**
  149. * <p>Assign resolved levels to a specified text interval over this delimited text
  150. * range.</p>
  151. * <p>Returns a list of text intervals containing either (1) the single, input text
  152. * interval or (2) two or more new text intervals obtained from sub-dividing the input
  153. * text range into level runs, i.e., runs of text assigned to a single level.</p>
  154. * @param ti a text interval to which levels are to be assigned
  155. * @param levels array of levels each corresponding to each index of the delimited
  156. * text range
  157. * @return a list of text intervals as described above
  158. */
  159. private static final Log log = LogFactory.getLog(BidiResolver.class);
  160. private List assignLevels(TextInterval ti, int[] levels) {
  161. Vector tiv = new Vector();
  162. FONode fn = ti.getNode();
  163. int fnStart = ti.getStart(); // start of node's text in delimited text range
  164. for (int i = fnStart, n = ti.getEnd(); i < n; ) {
  165. int s = i; // inclusive start index of interval in delimited text range
  166. int e = s; // exclusive end index of interval in delimited text range
  167. int l = levels [ s ]; // current run level
  168. while (e < n) { // skip to end of run level or end of interval
  169. if (levels [ e ] != l) {
  170. break;
  171. } else {
  172. e++;
  173. }
  174. }
  175. if ((ti.getStart() == s) && (ti.getEnd() == e)) {
  176. ti.setLevel(l); // reuse interval, assigning it single level
  177. } else {
  178. ti = new TextInterval(fn, fnStart, s, e, l); // subdivide interval
  179. }
  180. if (log.isDebugEnabled()) {
  181. log.debug("AL(" + l + "): " + ti);
  182. }
  183. tiv.add(ti);
  184. i = e;
  185. }
  186. return tiv;
  187. }
  188. /**
  189. * <p>Assign resolved levels for each interval to source #PCDATA in the associated FOText.</p>
  190. */
  191. private void assignTextLevels() {
  192. for (Iterator it = intervals.iterator(); it.hasNext(); ) {
  193. TextInterval ti = (TextInterval) it.next();
  194. ti.assignTextLevels();
  195. }
  196. }
  197. private void assignBlockLevel(Direction paragraphEmbeddingLevel) {
  198. int defaultLevel = (paragraphEmbeddingLevel == Direction.RL) ? 1 : 0;
  199. for (Iterator it = intervals.iterator(); it.hasNext(); ) {
  200. TextInterval ti = (TextInterval) it.next();
  201. assignBlockLevel(ti.getNode(), defaultLevel);
  202. }
  203. }
  204. private void assignBlockLevel(FONode node, int defaultLevel) {
  205. for (FONode fn = node; fn != null; fn = fn.getParent()) {
  206. if (fn instanceof FObj) {
  207. FObj fo = (FObj) fn;
  208. if (fo.isBidiRangeBlockItem()) {
  209. if (fo.getBidiLevel() < 0) {
  210. fo.setBidiLevel(defaultLevel);
  211. }
  212. break;
  213. }
  214. }
  215. }
  216. }
  217. }