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