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