Nelze vybrat více než 25 témat Téma musí začínat písmenem nebo číslem, může obsahovat pomlčky („-“) a může být dlouhé až 35 znaků.

BidiResolver.java 7.9KB

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