Debian Codegraph @

core/java/android/text/AndroidBidi.java

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