1/*
2 * Copyright (C) 2014 The Android Open Source Project
3 * Copyright (c) 2000, 2013, 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
37package 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 */
60public 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
84    // Android-note: Upstream this class delegates to an internal implementation class BidiBase.
85    // For Android that is replaced with android.icu.text.Bidi. BidiBase and ICU Bidi work very
86    // similarly, but differ in some details like level of argument validation and how how exactly
87    // runs are counted. The majority of the changes in this file exist to allow for backwards
88    // compatibility with an earlier ICU4C based Bidi implementation.
89
90    // BEGIN Android-added: translateConstToIcu(int).
91    private static int translateConstToIcu(int javaInt) {
92        switch (javaInt) {
93            case DIRECTION_DEFAULT_LEFT_TO_RIGHT:
94                return android.icu.text.Bidi.DIRECTION_DEFAULT_LEFT_TO_RIGHT;
95            case DIRECTION_DEFAULT_RIGHT_TO_LEFT:
96                return android.icu.text.Bidi.DIRECTION_DEFAULT_RIGHT_TO_LEFT;
97            case DIRECTION_LEFT_TO_RIGHT:
98                return android.icu.text.Bidi.DIRECTION_LEFT_TO_RIGHT;
99            case DIRECTION_RIGHT_TO_LEFT:
100                return android.icu.text.Bidi.DIRECTION_RIGHT_TO_LEFT;
101            // If the parameter was unrecognized use LEFT_TO_RIGHT.
102            default:
103                return android.icu.text.Bidi.DIRECTION_LEFT_TO_RIGHT;
104        }
105    }
106    // END Android-added: translateConstToIcu(int).
107
108    // Android-changed: use ICU Bidi class instead of BidiBase.
109    private final android.icu.text.Bidi bidiBase;
110
111    /**
112     * Create Bidi from the given paragraph of text and base direction.
113     * @param paragraph a paragraph of text
114     * @param flags a collection of flags that control the algorithm.  The
115     * algorithm understands the flags DIRECTION_LEFT_TO_RIGHT, DIRECTION_RIGHT_TO_LEFT,
116     * DIRECTION_DEFAULT_LEFT_TO_RIGHT, and DIRECTION_DEFAULT_RIGHT_TO_LEFT.
117     * Other values are reserved.
118     */
119    public Bidi(String paragraph, int flags) {
120        if (paragraph == null) {
121            throw new IllegalArgumentException("paragraph is null");
122        }
123
124        // Android-changed: use ICU Bidi class instead of BidiBase.
125        bidiBase = new android.icu.text.Bidi(paragraph.toCharArray(), 0, null, 0,
126                                             paragraph.length(), translateConstToIcu(flags));
127    }
128
129    /**
130     * Create Bidi from the given paragraph of text.
131     * <p>
132     * The RUN_DIRECTION attribute in the text, if present, determines the base
133     * direction (left-to-right or right-to-left).  If not present, the base
134     * direction is computes using the Unicode Bidirectional Algorithm, defaulting to left-to-right
135     * if there are no strong directional characters in the text.  This attribute, if
136     * present, must be applied to all the text in the paragraph.
137     * <p>
138     * The BIDI_EMBEDDING attribute in the text, if present, represents embedding level
139     * information.  Negative values from -1 to -62 indicate overrides at the absolute value
140     * of the level.  Positive values from 1 to 62 indicate embeddings.  Where values are
141     * zero or not defined, the base embedding level as determined by the base direction
142     * is assumed.
143     * <p>
144     * The NUMERIC_SHAPING attribute in the text, if present, converts European digits to
145     * other decimal digits before running the bidi algorithm.  This attribute, if present,
146     * must be applied to all the text in the paragraph.
147     *
148     * @param paragraph a paragraph of text with optional character and paragraph attribute information
149     *
150     * @see java.awt.font.TextAttribute#BIDI_EMBEDDING
151     * @see java.awt.font.TextAttribute#NUMERIC_SHAPING
152     * @see java.awt.font.TextAttribute#RUN_DIRECTION
153     */
154    public Bidi(AttributedCharacterIterator paragraph) {
155        if (paragraph == null) {
156            throw new IllegalArgumentException("paragraph is null");
157        }
158
159        // Android-changed: change from BidiBase to ICU Bidi class.
160        this.bidiBase = new android.icu.text.Bidi(paragraph);
161    }
162
163    /**
164     * Create Bidi from the given text, embedding, and direction information.
165     * The embeddings array may be null.  If present, the values represent embedding level
166     * information.  Negative values from -1 to -61 indicate overrides at the absolute value
167     * of the level.  Positive values from 1 to 61 indicate embeddings.  Where values are
168     * zero, the base embedding level as determined by the base direction is assumed.
169     * @param text an array containing the paragraph of text to process.
170     * @param textStart the index into the text array of the start of the paragraph.
171     * @param embeddings an array containing embedding values for each character in the paragraph.
172     * This can be null, in which case it is assumed that there is no external embedding information.
173     * @param embStart the index into the embedding array of the start of the paragraph.
174     * @param paragraphLength the length of the paragraph in the text and embeddings arrays.
175     * @param flags a collection of flags that control the algorithm.  The
176     * algorithm understands the flags DIRECTION_LEFT_TO_RIGHT, DIRECTION_RIGHT_TO_LEFT,
177     * DIRECTION_DEFAULT_LEFT_TO_RIGHT, and DIRECTION_DEFAULT_RIGHT_TO_LEFT.
178     * Other values are reserved.
179     */
180    public Bidi(char[] text, int textStart, byte[] embeddings, int embStart, int paragraphLength, int flags) {
181        if (text == null) {
182            throw new IllegalArgumentException("text is null");
183        }
184        if (paragraphLength < 0) {
185            throw new IllegalArgumentException("bad length: " + paragraphLength);
186        }
187        if (textStart < 0 || paragraphLength > text.length - textStart) {
188            throw new IllegalArgumentException("bad range: " + textStart +
189                                               " length: " + paragraphLength +
190                                               " for text of length: " + text.length);
191        }
192        if (embeddings != null && (embStart < 0 || paragraphLength > embeddings.length - embStart)) {
193            throw new IllegalArgumentException("bad range: " + embStart +
194                                               " length: " + paragraphLength +
195                                               " for embeddings of length: " + text.length);
196        }
197
198        // Android-changed: use ICU Bidi class instead of BidiBase.
199        bidiBase = new android.icu.text.Bidi(text, textStart, embeddings, embStart,
200                                             paragraphLength, translateConstToIcu(flags));
201    }
202
203    // Android-added: private constructor based on ICU Bidi object.
204    private Bidi(android.icu.text.Bidi bidiBase) {
205        this.bidiBase = bidiBase;
206    }
207
208    /**
209     * Create a Bidi object representing the bidi information on a line of text within
210     * the paragraph represented by the current Bidi.  This call is not required if the
211     * entire paragraph fits on one line.
212     *
213     * @param lineStart the offset from the start of the paragraph to the start of the line.
214     * @param lineLimit the offset from the start of the paragraph to the limit of the line.
215     * @return a {@code Bidi} object
216     */
217    public Bidi createLineBidi(int lineStart, int lineLimit) {
218        // BEGIN Android-changed: add explict argument checks and use ICU Bidi class.
219        if (lineStart < 0 || lineLimit < 0 || lineStart > lineLimit || lineLimit > getLength()) {
220            throw new IllegalArgumentException("Invalid ranges (start=" + lineStart + ", " +
221                                               "limit=" + lineLimit + ", length=" + getLength() + ")");
222        }
223
224        // In the special case where the start and end positions are the same, we return a new bidi
225        // instance which is empty. Note that the default constructor for an empty ICU4J bidi
226        // instance is not the same as passing in empty values. This way allows one to call
227        // .getLength() for example and return a correct value instead of an IllegalStateException
228        // being thrown, which happens in the case of using the empty constructor.
229        if (lineStart == lineLimit) {
230            return new Bidi(new android.icu.text.Bidi(new char[] {}, 0, new byte[] {}, 0, 0,
231                                                      translateConstToIcu(DIRECTION_LEFT_TO_RIGHT)));
232         }
233
234        return new Bidi(bidiBase.createLineBidi(lineStart, lineLimit));
235        // END Android-changed: add explict argument checks and use ICU Bidi class.
236    }
237
238    /**
239     * 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
240     * and right-to-left text, or the base direction differs from the direction of the only run of text.
241     *
242     * @return true if the line is not left-to-right or right-to-left.
243     */
244    public boolean isMixed() {
245        return bidiBase.isMixed();
246    }
247
248    /**
249     * Return true if the line is all left-to-right text and the base direction is left-to-right.
250     *
251     * @return true if the line is all left-to-right text and the base direction is left-to-right
252     */
253    public boolean isLeftToRight() {
254        return bidiBase.isLeftToRight();
255    }
256
257    /**
258     * Return true if the line is all right-to-left text, and the base direction is right-to-left.
259     * @return true if the line is all right-to-left text, and the base direction is right-to-left
260     */
261    public boolean isRightToLeft() {
262        return bidiBase.isRightToLeft();
263    }
264
265    /**
266     * Return the length of text in the line.
267     * @return the length of text in the line
268     */
269    public int getLength() {
270        return bidiBase.getLength();
271    }
272
273    /**
274     * Return true if the base direction is left-to-right.
275     * @return true if the base direction is left-to-right
276     */
277    public boolean baseIsLeftToRight() {
278        return bidiBase.baseIsLeftToRight();
279    }
280
281    /**
282     * Return the base level (0 if left-to-right, 1 if right-to-left).
283     * @return the base level
284     */
285    public int getBaseLevel() {
286        return bidiBase.getParaLevel();
287    }
288
289    /**
290     * Return the resolved level of the character at offset.  If offset is
291     * {@literal <} 0 or &ge; the length of the line, return the base direction
292     * level.
293     *
294     * @param offset the index of the character for which to return the level
295     * @return the resolved level of the character at offset
296     */
297    public int getLevelAt(int offset) {
298        // BEGIN Android-changed: return base level on out of range offset argument.
299        try {
300            return bidiBase.getLevelAt(offset);
301        } catch (IllegalArgumentException e) {
302            return getBaseLevel();
303        }
304        // END Android-changed: return base level on out of range offset argument.
305    }
306
307    /**
308     * Return the number of level runs.
309     * @return the number of level runs
310     */
311    public int getRunCount() {
312        // Android-changed: ICU treats the empty string as having 0 runs, we see it as 1 empty run.
313        int runCount = bidiBase.countRuns();
314        return (runCount == 0 ? 1 : runCount);
315    }
316
317    /**
318     * Return the level of the nth logical run in this line.
319     * @param run the index of the run, between 0 and <code>getRunCount()</code>
320     * @return the level of the run
321     */
322    public int getRunLevel(int run) {
323        // Android-added: Tolerate calls with run == getRunCount() for backwards compatibility.
324        if (run == getRunCount()) {
325            return getBaseLevel();
326        }
327        // Android-changed: ICU treats the empty string as having 0 runs, we see it as 1 empty run.
328        return (bidiBase.countRuns() == 0 ? bidiBase.getBaseLevel() : bidiBase.getRunLevel(run));
329    }
330
331    /**
332     * Return the index of the character at the start of the nth logical run in this line, as
333     * an offset from the start of the line.
334     * @param run the index of the run, between 0 and <code>getRunCount()</code>
335     * @return the start of the run
336     */
337    public int getRunStart(int run) {
338        // Android-added: Tolerate calls with run == getRunCount() for backwards compatibility.
339        if (run == getRunCount()) {
340            return getBaseLevel();
341        }
342        // Android-changed: ICU treats the empty string as having 0 runs, we see it as 1 empty run.
343        return (bidiBase.countRuns() == 0 ? 0 : bidiBase.getRunStart(run));
344    }
345
346    /**
347     * Return the index of the character past the end of the nth logical run in this line, as
348     * an offset from the start of the line.  For example, this will return the length
349     * of the line for the last run on the line.
350     * @param run the index of the run, between 0 and <code>getRunCount()</code>
351     * @return limit the limit of the run
352     */
353    public int getRunLimit(int run) {
354        // Android-added: Tolerate calls with run == getRunCount() for backwards compatibility.
355        if (run == getRunCount()) {
356            return getBaseLevel();
357        }
358        // Android-changed: ICU treats the empty string as having 0 runs, we see it as 1 empty run.
359        return (bidiBase.countRuns() == 0 ? bidiBase.getLength() : bidiBase.getRunLimit(run));
360    }
361
362    /**
363     * Return true if the specified text requires bidi analysis.  If this returns false,
364     * the text will display left-to-right.  Clients can then avoid constructing a Bidi object.
365     * Text in the Arabic Presentation Forms area of Unicode is presumed to already be shaped
366     * and ordered for display, and so will not cause this function to return true.
367     *
368     * @param text the text containing the characters to test
369     * @param start the start of the range of characters to test
370     * @param limit the limit of the range of characters to test
371     * @return true if the range of characters requires bidi analysis
372     */
373    public static boolean requiresBidi(char[] text, int start, int limit) {
374        // Android-added: Check arguments to throw correct exception.
375        if (0 > start || start > limit || limit > text.length) {
376            throw new IllegalArgumentException("Value start " + start +
377                                               " is out of range 0 to " + limit);
378        }
379        return android.icu.text.Bidi.requiresBidi(text, start, limit);
380    }
381
382    /**
383     * Reorder the objects in the array into visual order based on their levels.
384     * This is a utility function to use when you have a collection of objects
385     * representing runs of text in logical order, each run containing text
386     * at a single level.  The elements at <code>index</code> from
387     * <code>objectStart</code> up to <code>objectStart + count</code>
388     * in the objects array will be reordered into visual order assuming
389     * each run of text has the level indicated by the corresponding element
390     * in the levels array (at <code>index - objectStart + levelStart</code>).
391     *
392     * @param levels an array representing the bidi level of each object
393     * @param levelStart the start position in the levels array
394     * @param objects the array of objects to be reordered into visual order
395     * @param objectStart the start position in the objects array
396     * @param count the number of objects to reorder
397     */
398    public static void reorderVisually(byte[] levels, int levelStart, Object[] objects, int objectStart, int count) {
399        // BEGIN Android-added: Check arguments to throw correct exception.
400        if (0 > levelStart || levels.length <= levelStart) {
401            throw new IllegalArgumentException("Value levelStart " +
402                      levelStart + " is out of range 0 to " +
403                      (levels.length-1));
404        }
405        if (0 > objectStart || objects.length <= objectStart) {
406            throw new IllegalArgumentException("Value objectStart " +
407                      levelStart + " is out of range 0 to " +
408                      (objects.length-1));
409        }
410        if (0 > count || objects.length < (objectStart+count)) {
411            throw new IllegalArgumentException("Value count " +
412                      levelStart + " is out of range 0 to " +
413                      (objects.length - objectStart));
414        }
415        // END Android-added: Check arguments to throw correct exception.
416
417        // Android-changed: use ICU Bidi class instead of BidiBase.
418        android.icu.text.Bidi.reorderVisually(levels, levelStart, objects, objectStart, count);
419    }
420
421    /**
422     * Display the bidi internal state, used in debugging.
423     */
424    public String toString() {
425        // Android-changed: construct String representation from ICU Bidi object values.
426        return getClass().getName()
427            + "[direction: " + bidiBase.getDirection() + " baseLevel: " + bidiBase.getBaseLevel()
428            + " length: " + bidiBase.getLength() + " runs: " + bidiBase.getRunCount() + "]";
429    }
430
431}
432