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