1 /* 2 * Copyright (C) 2014 The Android Open Source Project 3 * Copyright (c) 2000, 2009, Oracle and/or its affiliates. All rights reserved. 4 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 5 * 6 * This code is free software; you can redistribute it and/or modify it 7 * under the terms of the GNU General Public License version 2 only, as 8 * published by the Free Software Foundation. Oracle designates this 9 * particular file as subject to the "Classpath" exception as provided 10 * by Oracle in the LICENSE file that accompanied this code. 11 * 12 * This code is distributed in the hope that it will be useful, but WITHOUT 13 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 14 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 15 * version 2 for more details (a copy is included in the LICENSE file that 16 * accompanied this code). 17 * 18 * You should have received a copy of the GNU General Public License version 19 * 2 along with this work; if not, write to the Free Software Foundation, 20 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 21 * 22 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA 23 * or visit www.oracle.com if you need additional information or have any 24 * questions. 25 */ 26 27 /* 28 * (C) Copyright IBM Corp. 1999-2003 - All Rights Reserved 29 * 30 * The original version of this source code and documentation is 31 * copyrighted and owned by IBM. These materials are provided 32 * under terms of a License Agreement between IBM and Sun. 33 * This technology is protected by multiple US and International 34 * patents. This notice and attribution to IBM may not be removed. 35 */ 36 37 package java.text; 38 39 /** 40 * This class implements the Unicode Bidirectional Algorithm. 41 * <p> 42 * A Bidi object provides information on the bidirectional reordering of the text 43 * used to create it. This is required, for example, to properly display Arabic 44 * or Hebrew text. These languages are inherently mixed directional, as they order 45 * numbers from left-to-right while ordering most other text from right-to-left. 46 * <p> 47 * Once created, a Bidi object can be queried to see if the text it represents is 48 * all left-to-right or all right-to-left. Such objects are very lightweight and 49 * this text is relatively easy to process. 50 * <p> 51 * If there are multiple runs of text, information about the runs can be accessed 52 * by indexing to get the start, limit, and level of a run. The level represents 53 * both the direction and the 'nesting level' of a directional run. Odd levels 54 * are right-to-left, while even levels are left-to-right. So for example level 55 * 0 represents left-to-right text, while level 1 represents right-to-left text, and 56 * level 2 represents left-to-right text embedded in a right-to-left run. 57 * 58 * @since 1.4 59 */ 60 public final class Bidi { 61 62 /** Constant indicating base direction is left-to-right. */ 63 public static final int DIRECTION_LEFT_TO_RIGHT = 0; 64 65 /** Constant indicating base direction is right-to-left. */ 66 public static final int DIRECTION_RIGHT_TO_LEFT = 1; 67 68 /** 69 * Constant indicating that the base direction depends on the first strong 70 * directional character in the text according to the Unicode 71 * Bidirectional Algorithm. If no strong directional character is present, 72 * the base direction is left-to-right. 73 */ 74 public static final int DIRECTION_DEFAULT_LEFT_TO_RIGHT = -2; 75 76 /** 77 * Constant indicating that the base direction depends on the first strong 78 * directional character in the text according to the Unicode 79 * Bidirectional Algorithm. If no strong directional character is present, 80 * the base direction is right-to-left. 81 */ 82 public static final int DIRECTION_DEFAULT_RIGHT_TO_LEFT = -1; 83 translateConstToIcu(int javaInt)84 private static int translateConstToIcu(int javaInt) { 85 switch (javaInt) { 86 case DIRECTION_DEFAULT_LEFT_TO_RIGHT: 87 return android.icu.text.Bidi.DIRECTION_DEFAULT_LEFT_TO_RIGHT; 88 case DIRECTION_DEFAULT_RIGHT_TO_LEFT: 89 return android.icu.text.Bidi.DIRECTION_DEFAULT_RIGHT_TO_LEFT; 90 case DIRECTION_LEFT_TO_RIGHT: 91 return android.icu.text.Bidi.DIRECTION_LEFT_TO_RIGHT; 92 case DIRECTION_RIGHT_TO_LEFT: 93 return android.icu.text.Bidi.DIRECTION_RIGHT_TO_LEFT; 94 // If the parameter was unrecognized use LEFT_TO_RIGHT. 95 default: 96 return android.icu.text.Bidi.DIRECTION_LEFT_TO_RIGHT; 97 } 98 } 99 100 private android.icu.text.Bidi bidiBase; 101 102 /** 103 * Create Bidi from the given paragraph of text and base direction. 104 * @param paragraph a paragraph of text 105 * @param flags a collection of flags that control the algorithm. The 106 * algorithm understands the flags DIRECTION_LEFT_TO_RIGHT, DIRECTION_RIGHT_TO_LEFT, 107 * DIRECTION_DEFAULT_LEFT_TO_RIGHT, and DIRECTION_DEFAULT_RIGHT_TO_LEFT. 108 * Other values are reserved. 109 */ Bidi(String paragraph, int flags)110 public Bidi(String paragraph, int flags) { 111 this((paragraph == null ? null : paragraph.toCharArray()), 0, null, 0, 112 (paragraph == null ? 0 : paragraph.length()), flags); 113 } 114 115 /** 116 * Create Bidi from the given paragraph of text. 117 * <p> 118 * The RUN_DIRECTION attribute in the text, if present, determines the base 119 * direction (left-to-right or right-to-left). If not present, the base 120 * direction is computes using the Unicode Bidirectional Algorithm, defaulting to left-to-right 121 * if there are no strong directional characters in the text. This attribute, if 122 * present, must be applied to all the text in the paragraph. 123 * <p> 124 * The BIDI_EMBEDDING attribute in the text, if present, represents embedding level 125 * information. Negative values from -1 to -62 indicate overrides at the absolute value 126 * of the level. Positive values from 1 to 62 indicate embeddings. Where values are 127 * zero or not defined, the base embedding level as determined by the base direction 128 * is assumed. 129 * <p> 130 * The NUMERIC_SHAPING attribute in the text, if present, converts European digits to 131 * other decimal digits before running the bidi algorithm. This attribute, if present, 132 * must be applied to all the text in the paragraph. 133 * 134 * @param paragraph a paragraph of text with optional character and paragraph attribute information 135 * 136 * @see java.awt.font.TextAttribute#BIDI_EMBEDDING 137 * @see java.awt.font.TextAttribute#NUMERIC_SHAPING 138 * @see java.awt.font.TextAttribute#RUN_DIRECTION 139 */ Bidi(AttributedCharacterIterator paragraph)140 public Bidi(AttributedCharacterIterator paragraph) { 141 if (paragraph == null) { 142 throw new IllegalArgumentException("paragraph is null"); 143 } 144 145 this.bidiBase = new android.icu.text.Bidi(paragraph); 146 } 147 148 /** 149 * Create Bidi from the given text, embedding, and direction information. 150 * The embeddings array may be null. If present, the values represent embedding level 151 * information. Negative values from -1 to -61 indicate overrides at the absolute value 152 * of the level. Positive values from 1 to 61 indicate embeddings. Where values are 153 * zero, the base embedding level as determined by the base direction is assumed. 154 * @param text an array containing the paragraph of text to process. 155 * @param textStart the index into the text array of the start of the paragraph. 156 * @param embeddings an array containing embedding values for each character in the paragraph. 157 * This can be null, in which case it is assumed that there is no external embedding information. 158 * @param embStart the index into the embedding array of the start of the paragraph. 159 * @param paragraphLength the length of the paragraph in the text and embeddings arrays. 160 * @param flags a collection of flags that control the algorithm. The 161 * algorithm understands the flags DIRECTION_LEFT_TO_RIGHT, DIRECTION_RIGHT_TO_LEFT, 162 * DIRECTION_DEFAULT_LEFT_TO_RIGHT, and DIRECTION_DEFAULT_RIGHT_TO_LEFT. 163 * Other values are reserved. 164 */ Bidi(char[] text, int textStart, byte[] embeddings, int embStart, int paragraphLength, int flags)165 public Bidi(char[] text, int textStart, byte[] embeddings, int embStart, int paragraphLength, int flags) { 166 if (text == null) { 167 throw new IllegalArgumentException("text is null"); 168 } 169 if (paragraphLength < 0) { 170 throw new IllegalArgumentException("bad length: " + paragraphLength); 171 } 172 if (textStart < 0 || paragraphLength > text.length - textStart) { 173 throw new IllegalArgumentException("bad range: " + textStart + 174 " length: " + paragraphLength + 175 " for text of length: " + text.length); 176 } 177 if (embeddings != null && (embStart < 0 || paragraphLength > embeddings.length - embStart)) { 178 throw new IllegalArgumentException("bad range: " + embStart + 179 " length: " + paragraphLength + 180 " for embeddings of length: " + text.length); 181 } 182 183 bidiBase = new android.icu.text.Bidi(text, textStart, embeddings, embStart, 184 paragraphLength, translateConstToIcu(flags)); 185 } 186 Bidi(android.icu.text.Bidi bidiBase)187 private Bidi(android.icu.text.Bidi bidiBase) { 188 this.bidiBase = bidiBase; 189 } 190 191 /** 192 * Create a Bidi object representing the bidi information on a line of text within 193 * the paragraph represented by the current Bidi. This call is not required if the 194 * entire paragraph fits on one line. 195 * @param lineStart the offset from the start of the paragraph to the start of the line. 196 * @param lineLimit the offset from the start of the paragraph to the limit of the line. 197 */ createLineBidi(int lineStart, int lineLimit)198 public Bidi createLineBidi(int lineStart, int lineLimit) { 199 if (lineStart < 0 || lineLimit < 0 || lineStart > lineLimit || lineLimit > getLength()) { 200 throw new IllegalArgumentException("Invalid ranges (start=" + lineStart + ", " + 201 "limit=" + lineLimit + ", length=" + getLength() + ")"); 202 } 203 204 // In the special case where the start and end positions are the same, we return a new bidi 205 // instance which is empty. Note that the default constructor for an empty ICU4J bidi 206 // instance is not the same as passing in empty values. This way allows one to call 207 // .getLength() for example and return a correct value instead of an IllegalStateException 208 // being thrown, which happens in the case of using the empty constructor. 209 if (lineStart == lineLimit) { 210 return new Bidi(new android.icu.text.Bidi(new char[] {}, 0, new byte[] {}, 0, 0, 211 translateConstToIcu(DIRECTION_LEFT_TO_RIGHT))); 212 } 213 214 return new Bidi(bidiBase.createLineBidi(lineStart, lineLimit)); 215 } 216 217 /** 218 * Return true if the line is not left-to-right or right-to-left. This means it either has mixed runs of left-to-right 219 * and right-to-left text, or the base direction differs from the direction of the only run of text. 220 * @return true if the line is not left-to-right or right-to-left. 221 */ isMixed()222 public boolean isMixed() { 223 return bidiBase.isMixed(); 224 } 225 226 /** 227 * Return true if the line is all left-to-right text and the base direction is left-to-right. 228 * @return true if the line is all left-to-right text and the base direction is left-to-right 229 */ isLeftToRight()230 public boolean isLeftToRight() { 231 return bidiBase.isLeftToRight(); 232 } 233 234 /** 235 * Return true if the line is all right-to-left text, and the base direction is right-to-left. 236 * @return true if the line is all right-to-left text, and the base direction is right-to-left 237 */ isRightToLeft()238 public boolean isRightToLeft() { 239 return bidiBase.isRightToLeft(); 240 } 241 242 /** 243 * Return the length of text in the line. 244 * @return the length of text in the line 245 */ getLength()246 public int getLength() { 247 return bidiBase.getLength(); 248 } 249 250 /** 251 * Return true if the base direction is left-to-right. 252 * @return true if the base direction is left-to-right 253 */ baseIsLeftToRight()254 public boolean baseIsLeftToRight() { 255 return bidiBase.baseIsLeftToRight(); 256 } 257 258 /** 259 * Return the base level (0 if left-to-right, 1 if right-to-left). 260 * @return the base level 261 */ getBaseLevel()262 public int getBaseLevel() { 263 return bidiBase.getParaLevel(); 264 } 265 266 /** 267 * Return the resolved level of the character at offset. If offset is <0 or >= 268 * the length of the line, return the base direction level. 269 * @param offset the index of the character for which to return the level 270 * @return the resolved level of the character at offset 271 */ getLevelAt(int offset)272 public int getLevelAt(int offset) { 273 try { 274 return bidiBase.getLevelAt(offset); 275 } catch (IllegalArgumentException e) { 276 return getBaseLevel(); 277 } 278 } 279 280 /** 281 * Return the number of level runs. 282 * @return the number of level runs 283 */ getRunCount()284 public int getRunCount() { 285 int runCount = bidiBase.countRuns(); 286 return (runCount == 0 ? 1 : runCount); 287 } 288 289 /** 290 * Return the level of the nth logical run in this line. 291 * @param run the index of the run, between 0 and <code>getRunCount()</code> 292 * @return the level of the run 293 */ getRunLevel(int run)294 public int getRunLevel(int run) { 295 // Paper over a the ICU4J behaviour of strictly enforcing run must be strictly less than 296 // the number of runs. Done to maintain compatibility with previous C implementation. 297 if (run == getRunCount()) { 298 return getBaseLevel(); 299 } 300 return (bidiBase.countRuns() == 0 ? bidiBase.getBaseLevel() : bidiBase.getRunLevel(run)); 301 } 302 303 /** 304 * Return the index of the character at the start of the nth logical run in this line, as 305 * an offset from the start of the line. 306 * @param run the index of the run, between 0 and <code>getRunCount()</code> 307 * @return the start of the run 308 */ getRunStart(int run)309 public int getRunStart(int run) { 310 // Paper over a the ICU4J behaviour of strictly enforcing run must be strictly less than 311 // the number of runs. Done to maintain compatibility with previous C implementation. 312 if (run == getRunCount()) { 313 return getBaseLevel(); 314 } 315 return (bidiBase.countRuns() == 0 ? 0 : bidiBase.getRunStart(run)); 316 } 317 318 /** 319 * Return the index of the character past the end of the nth logical run in this line, as 320 * an offset from the start of the line. For example, this will return the length 321 * of the line for the last run on the line. 322 * @param run the index of the run, between 0 and <code>getRunCount()</code> 323 * @return limit the limit of the run 324 */ getRunLimit(int run)325 public int getRunLimit(int run) { 326 // Paper over a the ICU4J behaviour of strictly enforcing run must be strictly less than 327 // the number of runs. Done to maintain compatibility with previous C implementation. 328 if (run == getRunCount()) { 329 return getBaseLevel(); 330 } 331 return (bidiBase.countRuns() == 0 ? bidiBase.getLength() : bidiBase.getRunLimit(run)); 332 } 333 334 /** 335 * Return true if the specified text requires bidi analysis. If this returns false, 336 * the text will display left-to-right. Clients can then avoid constructing a Bidi object. 337 * Text in the Arabic Presentation Forms area of Unicode is presumed to already be shaped 338 * and ordered for display, and so will not cause this function to return true. 339 * 340 * @param text the text containing the characters to test 341 * @param start the start of the range of characters to test 342 * @param limit the limit of the range of characters to test 343 * @return true if the range of characters requires bidi analysis 344 */ requiresBidi(char[] text, int start, int limit)345 public static boolean requiresBidi(char[] text, int start, int limit) { 346 if (0 > start || start > limit || limit > text.length) { 347 throw new IllegalArgumentException("Value start " + start + 348 " is out of range 0 to " + limit); 349 } 350 return android.icu.text.Bidi.requiresBidi(text, start, limit); 351 } 352 353 /** 354 * Reorder the objects in the array into visual order based on their levels. 355 * This is a utility function to use when you have a collection of objects 356 * representing runs of text in logical order, each run containing text 357 * at a single level. The elements at <code>index</code> from 358 * <code>objectStart</code> up to <code>objectStart + count</code> 359 * in the objects array will be reordered into visual order assuming 360 * each run of text has the level indicated by the corresponding element 361 * in the levels array (at <code>index - objectStart + levelStart</code>). 362 * 363 * @param levels an array representing the bidi level of each object 364 * @param levelStart the start position in the levels array 365 * @param objects the array of objects to be reordered into visual order 366 * @param objectStart the start position in the objects array 367 * @param count the number of objects to reorder 368 */ reorderVisually(byte[] levels, int levelStart, Object[] objects, int objectStart, int count)369 public static void reorderVisually(byte[] levels, int levelStart, Object[] objects, int objectStart, int count) { 370 if (0 > levelStart || levels.length <= levelStart) { 371 throw new IllegalArgumentException("Value levelStart " + 372 levelStart + " is out of range 0 to " + 373 (levels.length-1)); 374 } 375 if (0 > objectStart || objects.length <= objectStart) { 376 throw new IllegalArgumentException("Value objectStart " + 377 levelStart + " is out of range 0 to " + 378 (objects.length-1)); 379 } 380 if (0 > count || objects.length < (objectStart+count)) { 381 throw new IllegalArgumentException("Value count " + 382 levelStart + " is out of range 0 to " + 383 (objects.length - objectStart)); 384 } 385 android.icu.text.Bidi.reorderVisually(levels, levelStart, objects, objectStart, count); 386 } 387 388 /** 389 * Display the bidi internal state, used in debugging. 390 */ toString()391 public String toString() { 392 return getClass().getName() 393 + "[direction: " + bidiBase.getDirection() + " baseLevel: " + bidiBase.getBaseLevel() 394 + " length: " + bidiBase.getLength() + " runs: " + bidiBase.getRunCount() + "]"; 395 } 396 } 397