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.

BidiResolver.java 7.6KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238
  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.ArrayList;
  20. import java.util.Iterator;
  21. import java.util.List;
  22. import java.util.Stack;
  23. import java.util.Vector;
  24. import org.apache.commons.logging.Log;
  25. import org.apache.commons.logging.LogFactory;
  26. import org.apache.fop.area.LineArea;
  27. import org.apache.fop.area.inline.InlineArea;
  28. import org.apache.fop.fo.pagination.PageSequence;
  29. // CSOFF: LineLengthCheck
  30. /**
  31. * <p>A utility class for performing bidirectional resolution processing.</p>
  32. *
  33. * <p>This work was originally authored by Glenn Adams (gadams@apache.org).</p>
  34. */
  35. public final class BidiResolver {
  36. /**
  37. * logging instance
  38. */
  39. private static final Log log = LogFactory.getLog(BidiResolver.class);
  40. private BidiResolver() {
  41. }
  42. /**
  43. * Resolve inline directionality.
  44. * @param ps a page sequence FO instance
  45. */
  46. public static void resolveInlineDirectionality(PageSequence ps) {
  47. if (log.isDebugEnabled()) {
  48. log.debug("BD: RESOLVE: " + ps);
  49. }
  50. List ranges = pruneEmptyRanges(ps.collectDelimitedTextRanges(new Stack()));
  51. resolveInlineDirectionality(ranges);
  52. }
  53. /**
  54. * Reorder line area.
  55. * @param la a line area instance
  56. */
  57. public static void reorder(LineArea la) {
  58. // 1. collect inline levels
  59. List runs = collectRuns(la.getInlineAreas(), new Vector());
  60. if (log.isDebugEnabled()) {
  61. dumpRuns("BD: REORDER: INPUT:", runs);
  62. }
  63. // 2. split heterogeneous inlines
  64. runs = splitRuns(runs);
  65. if (log.isDebugEnabled()) {
  66. dumpRuns("BD: REORDER: SPLIT INLINES:", runs);
  67. }
  68. // 3. determine minimum and maximum levels
  69. int[] mm = computeMinMaxLevel(runs, null);
  70. if (log.isDebugEnabled()) {
  71. log.debug("BD: REORDER: { min = " + mm[0] + ", max = " + mm[1] + "}");
  72. }
  73. // 4. reorder from maximum level to minimum odd level
  74. int mn = mm[0];
  75. int mx = mm[1];
  76. if (mx > 0) {
  77. for (int l1 = mx, l2 = ((mn & 1) == 0) ? (mn + 1) : mn; l1 >= l2; l1--) {
  78. runs = reorderRuns(runs, l1);
  79. }
  80. }
  81. if (log.isDebugEnabled()) {
  82. dumpRuns("BD: REORDER: REORDERED RUNS:", runs);
  83. }
  84. // 5. reverse word consituents (characters and glyphs) while mirroring
  85. boolean mirror = true;
  86. reverseWords(runs, mirror);
  87. if (log.isDebugEnabled()) {
  88. dumpRuns("BD: REORDER: REORDERED WORDS:", runs);
  89. }
  90. // 6. replace line area's inline areas with reordered runs' inline areas
  91. replaceInlines(la, replicateSplitWords(runs));
  92. }
  93. private static void resolveInlineDirectionality(List ranges) {
  94. for (Iterator it = ranges.iterator(); it.hasNext(); ) {
  95. DelimitedTextRange r = (DelimitedTextRange) it.next();
  96. r.resolve();
  97. if (log.isDebugEnabled()) {
  98. log.debug(r);
  99. }
  100. }
  101. }
  102. private static List collectRuns(List inlines, List runs) {
  103. for (Iterator it = inlines.iterator(); it.hasNext(); ) {
  104. InlineArea ia = (InlineArea) it.next();
  105. runs = ia.collectInlineRuns(runs);
  106. }
  107. return runs;
  108. }
  109. private static List splitRuns(List runs) {
  110. List runsNew = new Vector();
  111. for (Iterator it = runs.iterator(); it.hasNext(); ) {
  112. InlineRun ir = (InlineRun) it.next();
  113. if (ir.isHomogenous()) {
  114. runsNew.add(ir);
  115. } else {
  116. runsNew.addAll(ir.split());
  117. }
  118. }
  119. if (!runsNew.equals(runs)) {
  120. runs = runsNew;
  121. }
  122. return runs;
  123. }
  124. private static int[] computeMinMaxLevel(List runs, int[] mm) {
  125. if (mm == null) {
  126. mm = new int[] {Integer.MAX_VALUE, Integer.MIN_VALUE};
  127. }
  128. for (Iterator it = runs.iterator(); it.hasNext(); ) {
  129. InlineRun ir = (InlineRun) it.next();
  130. ir.updateMinMax(mm);
  131. }
  132. return mm;
  133. }
  134. private static List reorderRuns(List runs, int level) {
  135. assert level >= 0;
  136. List runsNew = new Vector();
  137. for (int i = 0, n = runs.size(); i < n; i++) {
  138. InlineRun iri = (InlineRun) runs.get(i);
  139. if (iri.getMinLevel() < level) {
  140. runsNew.add(iri);
  141. } else {
  142. int s = i;
  143. int e = s;
  144. while (e < n) {
  145. InlineRun ire = (InlineRun) runs.get(e);
  146. if (ire.getMinLevel() < level) {
  147. break;
  148. } else {
  149. e++;
  150. }
  151. }
  152. if (s < e) {
  153. runsNew.addAll(reverseRuns(runs, s, e));
  154. }
  155. i = e - 1;
  156. }
  157. }
  158. if (!runsNew.equals(runs)) {
  159. runs = runsNew;
  160. }
  161. return runs;
  162. }
  163. private static List reverseRuns(List runs, int s, int e) {
  164. int n = e - s;
  165. Vector runsNew = new Vector(n);
  166. if (n > 0) {
  167. for (int i = 0; i < n; i++) {
  168. int k = (n - i - 1);
  169. InlineRun ir = (InlineRun) runs.get(s + k);
  170. ir.reverse();
  171. runsNew.add(ir);
  172. }
  173. }
  174. return runsNew;
  175. }
  176. private static void reverseWords(List runs, boolean mirror) {
  177. for (Iterator it = runs.iterator(); it.hasNext(); ) {
  178. InlineRun ir = (InlineRun) it.next();
  179. ir.maybeReverseWord(mirror);
  180. }
  181. }
  182. private static List replicateSplitWords(List runs) {
  183. // [TBD] for each run which inline word area appears multiple times in
  184. // runs, replicate that word
  185. return runs;
  186. }
  187. private static void replaceInlines(LineArea la, List runs) {
  188. List<InlineArea> inlines = new ArrayList<InlineArea>();
  189. for (Iterator it = runs.iterator(); it.hasNext(); ) {
  190. InlineRun ir = (InlineRun) it.next();
  191. inlines.add(ir.getInline());
  192. }
  193. la.setInlineAreas(unflattenInlines(inlines));
  194. }
  195. private static List unflattenInlines(List<InlineArea> inlines) {
  196. return new UnflattenProcessor(inlines) .unflatten();
  197. }
  198. private static void dumpRuns(String header, List runs) {
  199. log.debug(header);
  200. for (Iterator it = runs.iterator(); it.hasNext(); ) {
  201. InlineRun ir = (InlineRun) it.next();
  202. log.debug(ir);
  203. }
  204. }
  205. private static List pruneEmptyRanges(Stack ranges) {
  206. Vector rv = new Vector();
  207. for (Iterator it = ranges.iterator(); it.hasNext(); ) {
  208. DelimitedTextRange r = (DelimitedTextRange) it.next();
  209. if (!r.isEmpty()) {
  210. rv.add(r);
  211. }
  212. }
  213. return rv;
  214. }
  215. }