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