1 /*
2  * Copyright (C) 2010 The Android Open Source Project
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
17 package android.text;
18 
19 import android.compat.annotation.UnsupportedAppUsage;
20 import android.icu.lang.UCharacter;
21 import android.icu.lang.UCharacterDirection;
22 import android.icu.lang.UProperty;
23 import android.icu.text.Bidi;
24 import android.icu.text.BidiClassifier;
25 import android.text.Layout.Directions;
26 
27 import com.android.internal.annotations.VisibleForTesting;
28 
29 /**
30  * Access the ICU bidi implementation.
31  * @hide
32  */
33 @VisibleForTesting(visibility = VisibleForTesting.Visibility.PACKAGE)
34 public class AndroidBidi {
35 
36     /**
37      * Overrides ICU {@link BidiClassifier} in order to correctly handle character directions for
38      * newest emoji that ICU is not aware of.
39      */
40     public static class EmojiBidiOverride extends BidiClassifier {
EmojiBidiOverride()41         public EmojiBidiOverride() {
42             super(null /* No persisting object needed */);
43         }
44 
45         // Tells ICU to use the standard Unicode value.
46         private static final int NO_OVERRIDE =
47                 UCharacter.getIntPropertyMaxValue(UProperty.BIDI_CLASS) + 1;
48 
49         @Override
classify(int c)50         public int classify(int c) {
51             if (Emoji.isNewEmoji(c)) {
52                 // All new emoji characters in Unicode 10.0 are of the bidi class ON.
53                 return UCharacterDirection.OTHER_NEUTRAL;
54             } else {
55                 return NO_OVERRIDE;
56             }
57         }
58     }
59 
60     private static final EmojiBidiOverride sEmojiBidiOverride = new EmojiBidiOverride();
61 
62     /**
63      * Runs the bidi algorithm on input text.
64      */
65     @UnsupportedAppUsage
bidi(int dir, char[] chs, byte[] chInfo)66     public static int bidi(int dir, char[] chs, byte[] chInfo) {
67         if (chs == null || chInfo == null) {
68             throw new NullPointerException();
69         }
70 
71         final int length = chs.length;
72         if (chInfo.length < length) {
73             throw new IndexOutOfBoundsException();
74         }
75 
76         final byte paraLevel;
77         switch (dir) {
78             case Layout.DIR_REQUEST_LTR: paraLevel = Bidi.LTR; break;
79             case Layout.DIR_REQUEST_RTL: paraLevel = Bidi.RTL; break;
80             case Layout.DIR_REQUEST_DEFAULT_LTR: paraLevel = Bidi.LEVEL_DEFAULT_LTR; break;
81             case Layout.DIR_REQUEST_DEFAULT_RTL: paraLevel = Bidi.LEVEL_DEFAULT_RTL; break;
82             default: paraLevel = Bidi.LTR; break;
83         }
84         final Bidi icuBidi = new Bidi(length /* maxLength */, 0 /* maxRunCount */);
85         icuBidi.setCustomClassifier(sEmojiBidiOverride);
86         icuBidi.setPara(chs, paraLevel, null /* embeddingLevels */);
87         for (int i = 0; i < length; i++) {
88             chInfo[i] = icuBidi.getLevelAt(i);
89         }
90         final byte result = icuBidi.getParaLevel();
91         return (result & 0x1) == 0 ? Layout.DIR_LEFT_TO_RIGHT : Layout.DIR_RIGHT_TO_LEFT;
92     }
93 
94     /**
95      * Returns run direction information for a line within a paragraph.
96      *
97      * @param dir base line direction, either Layout.DIR_LEFT_TO_RIGHT or
98      *     Layout.DIR_RIGHT_TO_LEFT
99      * @param levels levels as returned from {@link #bidi}
100      * @param lstart start of the line in the levels array
101      * @param chars the character array (used to determine whitespace)
102      * @param cstart the start of the line in the chars array
103      * @param len the length of the line
104      * @return the directions
105      */
directions(int dir, byte[] levels, int lstart, char[] chars, int cstart, int len)106     public static Directions directions(int dir, byte[] levels, int lstart,
107             char[] chars, int cstart, int len) {
108         if (len == 0) {
109             return Layout.DIRS_ALL_LEFT_TO_RIGHT;
110         }
111 
112         int baseLevel = dir == Layout.DIR_LEFT_TO_RIGHT ? 0 : 1;
113         int curLevel = levels[lstart];
114         int minLevel = curLevel;
115         int runCount = 1;
116         for (int i = lstart + 1, e = lstart + len; i < e; ++i) {
117             int level = levels[i];
118             if (level != curLevel) {
119                 curLevel = level;
120                 ++runCount;
121             }
122         }
123 
124         // add final run for trailing counter-directional whitespace
125         int visLen = len;
126         if ((curLevel & 1) != (baseLevel & 1)) {
127             // look for visible end
128             while (--visLen >= 0) {
129                 char ch = chars[cstart + visLen];
130 
131                 if (ch == '\n') {
132                     --visLen;
133                     break;
134                 }
135 
136                 if (ch != ' ' && ch != '\t') {
137                     break;
138                 }
139             }
140             ++visLen;
141             if (visLen != len) {
142                 ++runCount;
143             }
144         }
145 
146         if (runCount == 1 && minLevel == baseLevel) {
147             // we're done, only one run on this line
148             if ((minLevel & 1) != 0) {
149                 return Layout.DIRS_ALL_RIGHT_TO_LEFT;
150             }
151             return Layout.DIRS_ALL_LEFT_TO_RIGHT;
152         }
153 
154         int[] ld = new int[runCount * 2];
155         int maxLevel = minLevel;
156         int levelBits = minLevel << Layout.RUN_LEVEL_SHIFT;
157         {
158             // Start of first pair is always 0, we write
159             // length then start at each new run, and the
160             // last run length after we're done.
161             int n = 1;
162             int prev = lstart;
163             curLevel = minLevel;
164             for (int i = lstart, e = lstart + visLen; i < e; ++i) {
165                 int level = levels[i];
166                 if (level != curLevel) {
167                     curLevel = level;
168                     if (level > maxLevel) {
169                         maxLevel = level;
170                     } else if (level < minLevel) {
171                         minLevel = level;
172                     }
173                     // XXX ignore run length limit of 2^RUN_LEVEL_SHIFT
174                     ld[n++] = (i - prev) | levelBits;
175                     ld[n++] = i - lstart;
176                     levelBits = curLevel << Layout.RUN_LEVEL_SHIFT;
177                     prev = i;
178                 }
179             }
180             ld[n] = (lstart + visLen - prev) | levelBits;
181             if (visLen < len) {
182                 ld[++n] = visLen;
183                 ld[++n] = (len - visLen) | (baseLevel << Layout.RUN_LEVEL_SHIFT);
184             }
185         }
186 
187         // See if we need to swap any runs.
188         // If the min level run direction doesn't match the base
189         // direction, we always need to swap (at this point
190         // we have more than one run).
191         // Otherwise, we don't need to swap the lowest level.
192         // Since there are no logically adjacent runs at the same
193         // level, if the max level is the same as the (new) min
194         // level, we have a series of alternating levels that
195         // is already in order, so there's no more to do.
196         //
197         boolean swap;
198         if ((minLevel & 1) == baseLevel) {
199             minLevel += 1;
200             swap = maxLevel > minLevel;
201         } else {
202             swap = runCount > 1;
203         }
204         if (swap) {
205             for (int level = maxLevel - 1; level >= minLevel; --level) {
206                 for (int i = 0; i < ld.length; i += 2) {
207                     if (levels[ld[i]] >= level) {
208                         int e = i + 2;
209                         while (e < ld.length && levels[ld[e]] >= level) {
210                             e += 2;
211                         }
212                         for (int low = i, hi = e - 2; low < hi; low += 2, hi -= 2) {
213                             int x = ld[low]; ld[low] = ld[hi]; ld[hi] = x;
214                             x = ld[low+1]; ld[low+1] = ld[hi+1]; ld[hi+1] = x;
215                         }
216                         i = e + 2;
217                     }
218                 }
219             }
220         }
221         return new Directions(ld);
222     }
223 }