Вы не можете выбрать более 25 тем Темы должны начинаться с буквы или цифры, могут содержать дефисы(-) и должны содержать не более 35 символов.

BidiResolver.java 8.2KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254
  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. // 1. collect delimited text ranges
  51. List ranges = ps.collectDelimitedTextRanges(new Stack());
  52. if (log.isDebugEnabled()) {
  53. dumpRanges("BD: RESOLVE: RANGES:", ranges);
  54. }
  55. // 2. prune empty ranges
  56. ranges = pruneEmptyRanges(ranges);
  57. if (log.isDebugEnabled()) {
  58. dumpRanges("BD: RESOLVE: PRUNED RANGES:", ranges);
  59. }
  60. // 3. resolve inline directionaly of unpruned ranges
  61. resolveInlineDirectionality(ranges);
  62. }
  63. /**
  64. * Reorder line area.
  65. * @param la a line area instance
  66. */
  67. public static void reorder(LineArea la) {
  68. // 1. collect inline levels
  69. List runs = collectRuns(la.getInlineAreas(), new Vector());
  70. if (log.isDebugEnabled()) {
  71. dumpRuns("BD: REORDER: INPUT:", runs);
  72. }
  73. // 2. split heterogeneous inlines
  74. runs = splitRuns(runs);
  75. if (log.isDebugEnabled()) {
  76. dumpRuns("BD: REORDER: SPLIT INLINES:", runs);
  77. }
  78. // 3. determine minimum and maximum levels
  79. int[] mm = computeMinMaxLevel(runs, null);
  80. if (log.isDebugEnabled()) {
  81. log.debug("BD: REORDER: { min = " + mm[0] + ", max = " + mm[1] + "}");
  82. }
  83. // 4. reorder from maximum level to minimum odd level
  84. int mn = mm[0];
  85. int mx = mm[1];
  86. if (mx > 0) {
  87. for (int l1 = mx, l2 = ((mn & 1) == 0) ? (mn + 1) : mn; l1 >= l2; l1--) {
  88. runs = reorderRuns(runs, l1);
  89. }
  90. }
  91. if (log.isDebugEnabled()) {
  92. dumpRuns("BD: REORDER: REORDERED RUNS:", runs);
  93. }
  94. // 5. reverse word consituents (characters and glyphs) while mirroring
  95. boolean mirror = true;
  96. reverseWords(runs, mirror);
  97. if (log.isDebugEnabled()) {
  98. dumpRuns("BD: REORDER: REORDERED WORDS:", runs);
  99. }
  100. // 6. replace line area's inline areas with reordered runs' inline areas
  101. replaceInlines(la, replicateSplitWords(runs));
  102. }
  103. private static void resolveInlineDirectionality(List ranges) {
  104. for (Iterator it = ranges.iterator(); it.hasNext(); ) {
  105. DelimitedTextRange r = (DelimitedTextRange) it.next();
  106. r.resolve();
  107. if (log.isDebugEnabled()) {
  108. log.debug(r);
  109. }
  110. }
  111. }
  112. private static List collectRuns(List inlines, List runs) {
  113. for (Iterator it = inlines.iterator(); it.hasNext(); ) {
  114. InlineArea ia = (InlineArea) it.next();
  115. runs = ia.collectInlineRuns(runs);
  116. }
  117. return runs;
  118. }
  119. private static List splitRuns(List runs) {
  120. List runsNew = new Vector();
  121. for (Iterator it = runs.iterator(); it.hasNext(); ) {
  122. InlineRun ir = (InlineRun) it.next();
  123. if (ir.isHomogenous()) {
  124. runsNew.add(ir);
  125. } else {
  126. runsNew.addAll(ir.split());
  127. }
  128. }
  129. if (!runsNew.equals(runs)) {
  130. runs = runsNew;
  131. }
  132. return runs;
  133. }
  134. private static int[] computeMinMaxLevel(List runs, int[] mm) {
  135. if (mm == null) {
  136. mm = new int[] {Integer.MAX_VALUE, Integer.MIN_VALUE};
  137. }
  138. for (Iterator it = runs.iterator(); it.hasNext(); ) {
  139. InlineRun ir = (InlineRun) it.next();
  140. ir.updateMinMax(mm);
  141. }
  142. return mm;
  143. }
  144. private static List reorderRuns(List runs, int level) {
  145. assert level >= 0;
  146. List runsNew = new Vector();
  147. for (int i = 0, n = runs.size(); i < n; i++) {
  148. InlineRun iri = (InlineRun) runs.get(i);
  149. if (iri.getMinLevel() < level) {
  150. runsNew.add(iri);
  151. } else {
  152. int s = i;
  153. int e = s;
  154. while (e < n) {
  155. InlineRun ire = (InlineRun) runs.get(e);
  156. if (ire.getMinLevel() < level) {
  157. break;
  158. } else {
  159. e++;
  160. }
  161. }
  162. if (s < e) {
  163. runsNew.addAll(reverseRuns(runs, s, e));
  164. }
  165. i = e - 1;
  166. }
  167. }
  168. if (!runsNew.equals(runs)) {
  169. runs = runsNew;
  170. }
  171. return runs;
  172. }
  173. private static List reverseRuns(List runs, int s, int e) {
  174. int n = e - s;
  175. Vector runsNew = new Vector(n);
  176. if (n > 0) {
  177. for (int i = 0; i < n; i++) {
  178. int k = (n - i - 1);
  179. InlineRun ir = (InlineRun) runs.get(s + k);
  180. ir.reverse();
  181. runsNew.add(ir);
  182. }
  183. }
  184. return runsNew;
  185. }
  186. private static void reverseWords(List runs, boolean mirror) {
  187. for (Iterator it = runs.iterator(); it.hasNext(); ) {
  188. InlineRun ir = (InlineRun) it.next();
  189. ir.maybeReverseWord(mirror);
  190. }
  191. }
  192. private static List replicateSplitWords(List runs) {
  193. // [TBD] for each run which inline word area appears multiple times in
  194. // runs, replicate that word
  195. return runs;
  196. }
  197. private static void replaceInlines(LineArea la, List runs) {
  198. List<InlineArea> inlines = new ArrayList<InlineArea>();
  199. for (Iterator it = runs.iterator(); it.hasNext(); ) {
  200. InlineRun ir = (InlineRun) it.next();
  201. inlines.add(ir.getInline());
  202. }
  203. la.setInlineAreas(unflattenInlines(inlines));
  204. }
  205. private static List unflattenInlines(List<InlineArea> inlines) {
  206. return new UnflattenProcessor(inlines) .unflatten();
  207. }
  208. private static void dumpRuns(String header, List runs) {
  209. log.debug(header);
  210. for (Iterator it = runs.iterator(); it.hasNext(); ) {
  211. InlineRun ir = (InlineRun) it.next();
  212. log.debug(ir);
  213. }
  214. }
  215. private static void dumpRanges(String header, List ranges) {
  216. log.debug(header);
  217. for (Iterator it = ranges.iterator(); it.hasNext(); ) {
  218. DelimitedTextRange r = (DelimitedTextRange) it.next();
  219. log.debug(r);
  220. }
  221. }
  222. private static List pruneEmptyRanges(List ranges) {
  223. Vector rv = new Vector();
  224. for (Iterator it = ranges.iterator(); it.hasNext(); ) {
  225. DelimitedTextRange r = (DelimitedTextRange) it.next();
  226. if (!r.isEmpty()) {
  227. rv.add(r);
  228. }
  229. }
  230. return rv;
  231. }
  232. }