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