| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 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 |
|
| 28 |
|
| 29 |
|
| 30 |
@VisibleForTesting(visibility = VisibleForTesting.Visibility.PACKAGE) |
| 31 |
public class AndroidBidi { |
| 32 |
|
| 33 |
|
| 34 |
|
| 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 , 0 ); |
| 56 |
icuBidi.setPara(chs, paraLevel, null ); |
| 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 |
|
| 66 |
|
| 67 |
|
| 68 |
|
| 69 |
|
| 70 |
|
| 71 |
|
| 72 |
|
| 73 |
|
| 74 |
|
| 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 |
|
| 95 |
int visLen = len; |
| 96 |
if ((curLevel & 1) != (baseLevel & 1)) { |
| 97 |
|
| 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 |
|
| 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 |
|
| 129 |
|
| 130 |
|
| 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 |
|
| 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 |
|
| 158 |
|
| 159 |
|
| 160 |
|
| 161 |
|
| 162 |
|
| 163 |
|
| 164 |
|
| 165 |
|
| 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 |
|