Fabrice Di Meglio | 4b60c30 | 2011-08-17 16:56:55 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2011 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 | */ |
Doug Felt | cb379120 | 2011-07-07 11:57:48 -0700 | [diff] [blame] | 16 | |
| 17 | package android.text; |
| 18 | |
| 19 | |
Fabrice Di Meglio | 3fb824b | 2012-02-28 17:58:31 -0800 | [diff] [blame] | 20 | import android.view.View; |
Fabrice Di Meglio | 7810b5f | 2011-08-24 18:26:14 -0700 | [diff] [blame] | 21 | |
Fabrice Di Meglio | 57a8574 | 2013-01-31 13:29:36 -0800 | [diff] [blame^] | 22 | import java.nio.CharBuffer; |
| 23 | |
Doug Felt | cb379120 | 2011-07-07 11:57:48 -0700 | [diff] [blame] | 24 | /** |
| 25 | * Some objects that implement TextDirectionHeuristic. |
Gilles Debunne | cefb4bc | 2012-05-02 18:14:56 -0700 | [diff] [blame] | 26 | * |
Doug Felt | cb379120 | 2011-07-07 11:57:48 -0700 | [diff] [blame] | 27 | */ |
| 28 | public class TextDirectionHeuristics { |
| 29 | |
Fabrice Di Meglio | 57a8574 | 2013-01-31 13:29:36 -0800 | [diff] [blame^] | 30 | /** |
| 31 | * Always decides that the direction is left to right. |
| 32 | */ |
Doug Felt | cb379120 | 2011-07-07 11:57:48 -0700 | [diff] [blame] | 33 | public static final TextDirectionHeuristic LTR = |
| 34 | new TextDirectionHeuristicInternal(null /* no algorithm */, false); |
| 35 | |
Fabrice Di Meglio | 57a8574 | 2013-01-31 13:29:36 -0800 | [diff] [blame^] | 36 | /** |
| 37 | * Always decides that the direction is right to left. |
| 38 | */ |
Doug Felt | cb379120 | 2011-07-07 11:57:48 -0700 | [diff] [blame] | 39 | public static final TextDirectionHeuristic RTL = |
| 40 | new TextDirectionHeuristicInternal(null /* no algorithm */, true); |
| 41 | |
| 42 | /** |
Fabrice Di Meglio | 57a8574 | 2013-01-31 13:29:36 -0800 | [diff] [blame^] | 43 | * Determines the direction based on the first strong directional character, including bidi |
| 44 | * format chars, falling back to left to right if it finds none. This is the default behavior |
| 45 | * of the Unicode Bidirectional Algorithm. |
Doug Felt | cb379120 | 2011-07-07 11:57:48 -0700 | [diff] [blame] | 46 | */ |
| 47 | public static final TextDirectionHeuristic FIRSTSTRONG_LTR = |
| 48 | new TextDirectionHeuristicInternal(FirstStrong.INSTANCE, false); |
| 49 | |
| 50 | /** |
Fabrice Di Meglio | 57a8574 | 2013-01-31 13:29:36 -0800 | [diff] [blame^] | 51 | * Determines the direction based on the first strong directional character, including bidi |
| 52 | * format chars, falling back to right to left if it finds none. This is similar to the default |
| 53 | * behavior of the Unicode Bidirectional Algorithm, just with different fallback behavior. |
Doug Felt | cb379120 | 2011-07-07 11:57:48 -0700 | [diff] [blame] | 54 | */ |
| 55 | public static final TextDirectionHeuristic FIRSTSTRONG_RTL = |
| 56 | new TextDirectionHeuristicInternal(FirstStrong.INSTANCE, true); |
| 57 | |
| 58 | /** |
Fabrice Di Meglio | 57a8574 | 2013-01-31 13:29:36 -0800 | [diff] [blame^] | 59 | * If the text contains any strong right to left non-format character, determines that the |
| 60 | * direction is right to left, falling back to left to right if it finds none. |
Fabrice Di Meglio | 4b60c30 | 2011-08-17 16:56:55 -0700 | [diff] [blame] | 61 | */ |
| 62 | public static final TextDirectionHeuristic ANYRTL_LTR = |
| 63 | new TextDirectionHeuristicInternal(AnyStrong.INSTANCE_RTL, false); |
| 64 | |
| 65 | /** |
Fabrice Di Meglio | 7810b5f | 2011-08-24 18:26:14 -0700 | [diff] [blame] | 66 | * Force the paragraph direction to the Locale direction. Falls back to left to right. |
| 67 | */ |
| 68 | public static final TextDirectionHeuristic LOCALE = TextDirectionHeuristicLocale.INSTANCE; |
| 69 | |
Fabrice Di Meglio | 57a8574 | 2013-01-31 13:29:36 -0800 | [diff] [blame^] | 70 | /** |
| 71 | * State constants for taking care about true / false / unknown |
| 72 | */ |
| 73 | private static final int STATE_TRUE = 0; |
| 74 | private static final int STATE_FALSE = 1; |
| 75 | private static final int STATE_UNKNOWN = 2; |
| 76 | |
| 77 | private static int isRtlText(int directionality) { |
| 78 | switch (directionality) { |
| 79 | case Character.DIRECTIONALITY_LEFT_TO_RIGHT: |
| 80 | return STATE_FALSE; |
| 81 | case Character.DIRECTIONALITY_RIGHT_TO_LEFT: |
| 82 | case Character.DIRECTIONALITY_RIGHT_TO_LEFT_ARABIC: |
| 83 | return STATE_TRUE; |
| 84 | default: |
| 85 | return STATE_UNKNOWN; |
| 86 | } |
| 87 | } |
| 88 | |
| 89 | private static int isRtlTextOrFormat(int directionality) { |
| 90 | switch (directionality) { |
| 91 | case Character.DIRECTIONALITY_LEFT_TO_RIGHT: |
| 92 | case Character.DIRECTIONALITY_LEFT_TO_RIGHT_EMBEDDING: |
| 93 | case Character.DIRECTIONALITY_LEFT_TO_RIGHT_OVERRIDE: |
| 94 | return STATE_FALSE; |
| 95 | case Character.DIRECTIONALITY_RIGHT_TO_LEFT: |
| 96 | case Character.DIRECTIONALITY_RIGHT_TO_LEFT_ARABIC: |
| 97 | case Character.DIRECTIONALITY_RIGHT_TO_LEFT_EMBEDDING: |
| 98 | case Character.DIRECTIONALITY_RIGHT_TO_LEFT_OVERRIDE: |
| 99 | return STATE_TRUE; |
| 100 | default: |
| 101 | return STATE_UNKNOWN; |
| 102 | } |
Doug Felt | cb379120 | 2011-07-07 11:57:48 -0700 | [diff] [blame] | 103 | } |
| 104 | |
| 105 | /** |
| 106 | * Computes the text direction based on an algorithm. Subclasses implement |
| 107 | * {@link #defaultIsRtl} to handle cases where the algorithm cannot determine the |
| 108 | * direction from the text alone. |
Doug Felt | cb379120 | 2011-07-07 11:57:48 -0700 | [diff] [blame] | 109 | */ |
Fabrice Di Meglio | e7beae3 | 2012-02-13 15:23:57 -0800 | [diff] [blame] | 110 | private static abstract class TextDirectionHeuristicImpl implements TextDirectionHeuristic { |
Doug Felt | cb379120 | 2011-07-07 11:57:48 -0700 | [diff] [blame] | 111 | private final TextDirectionAlgorithm mAlgorithm; |
| 112 | |
| 113 | public TextDirectionHeuristicImpl(TextDirectionAlgorithm algorithm) { |
| 114 | mAlgorithm = algorithm; |
| 115 | } |
| 116 | |
| 117 | /** |
| 118 | * Return true if the default text direction is rtl. |
| 119 | */ |
| 120 | abstract protected boolean defaultIsRtl(); |
| 121 | |
| 122 | @Override |
Fabrice Di Meglio | 57a8574 | 2013-01-31 13:29:36 -0800 | [diff] [blame^] | 123 | public boolean isRtl(char[] array, int start, int count) { |
| 124 | return isRtl(CharBuffer.wrap(array), start, count); |
| 125 | } |
| 126 | |
| 127 | @Override |
| 128 | public boolean isRtl(CharSequence cs, int start, int count) { |
| 129 | if (cs == null || start < 0 || count < 0 || cs.length() - count < start) { |
Doug Felt | cb379120 | 2011-07-07 11:57:48 -0700 | [diff] [blame] | 130 | throw new IllegalArgumentException(); |
| 131 | } |
| 132 | if (mAlgorithm == null) { |
| 133 | return defaultIsRtl(); |
| 134 | } |
Fabrice Di Meglio | 57a8574 | 2013-01-31 13:29:36 -0800 | [diff] [blame^] | 135 | return doCheck(cs, start, count); |
Doug Felt | cb379120 | 2011-07-07 11:57:48 -0700 | [diff] [blame] | 136 | } |
| 137 | |
Fabrice Di Meglio | 57a8574 | 2013-01-31 13:29:36 -0800 | [diff] [blame^] | 138 | private boolean doCheck(CharSequence cs, int start, int count) { |
| 139 | switch(mAlgorithm.checkRtl(cs, start, count)) { |
| 140 | case STATE_TRUE: |
Doug Felt | cb379120 | 2011-07-07 11:57:48 -0700 | [diff] [blame] | 141 | return true; |
Fabrice Di Meglio | 57a8574 | 2013-01-31 13:29:36 -0800 | [diff] [blame^] | 142 | case STATE_FALSE: |
Doug Felt | cb379120 | 2011-07-07 11:57:48 -0700 | [diff] [blame] | 143 | return false; |
| 144 | default: |
| 145 | return defaultIsRtl(); |
| 146 | } |
| 147 | } |
| 148 | } |
| 149 | |
| 150 | private static class TextDirectionHeuristicInternal extends TextDirectionHeuristicImpl { |
| 151 | private final boolean mDefaultIsRtl; |
| 152 | |
| 153 | private TextDirectionHeuristicInternal(TextDirectionAlgorithm algorithm, |
| 154 | boolean defaultIsRtl) { |
| 155 | super(algorithm); |
| 156 | mDefaultIsRtl = defaultIsRtl; |
| 157 | } |
| 158 | |
| 159 | @Override |
| 160 | protected boolean defaultIsRtl() { |
| 161 | return mDefaultIsRtl; |
| 162 | } |
| 163 | } |
| 164 | |
Doug Felt | cb379120 | 2011-07-07 11:57:48 -0700 | [diff] [blame] | 165 | /** |
| 166 | * Interface for an algorithm to guess the direction of a paragraph of text. |
Doug Felt | cb379120 | 2011-07-07 11:57:48 -0700 | [diff] [blame] | 167 | */ |
Fabrice Di Meglio | e7beae3 | 2012-02-13 15:23:57 -0800 | [diff] [blame] | 168 | private static interface TextDirectionAlgorithm { |
Doug Felt | cb379120 | 2011-07-07 11:57:48 -0700 | [diff] [blame] | 169 | /** |
| 170 | * Returns whether the range of text is RTL according to the algorithm. |
Doug Felt | cb379120 | 2011-07-07 11:57:48 -0700 | [diff] [blame] | 171 | */ |
Fabrice Di Meglio | 57a8574 | 2013-01-31 13:29:36 -0800 | [diff] [blame^] | 172 | int checkRtl(CharSequence cs, int start, int count); |
Doug Felt | cb379120 | 2011-07-07 11:57:48 -0700 | [diff] [blame] | 173 | } |
| 174 | |
| 175 | /** |
Fabrice Di Meglio | 57a8574 | 2013-01-31 13:29:36 -0800 | [diff] [blame^] | 176 | * Algorithm that uses the first strong directional character to determine the paragraph |
| 177 | * direction. This is the standard Unicode Bidirectional algorithm. |
Doug Felt | cb379120 | 2011-07-07 11:57:48 -0700 | [diff] [blame] | 178 | */ |
Fabrice Di Meglio | e7beae3 | 2012-02-13 15:23:57 -0800 | [diff] [blame] | 179 | private static class FirstStrong implements TextDirectionAlgorithm { |
Doug Felt | cb379120 | 2011-07-07 11:57:48 -0700 | [diff] [blame] | 180 | @Override |
Fabrice Di Meglio | 57a8574 | 2013-01-31 13:29:36 -0800 | [diff] [blame^] | 181 | public int checkRtl(CharSequence cs, int start, int count) { |
| 182 | int result = STATE_UNKNOWN; |
| 183 | for (int i = start, e = start + count; i < e && result == STATE_UNKNOWN; ++i) { |
| 184 | result = isRtlTextOrFormat(Character.getDirectionality(cs.charAt(i))); |
Doug Felt | cb379120 | 2011-07-07 11:57:48 -0700 | [diff] [blame] | 185 | } |
| 186 | return result; |
| 187 | } |
| 188 | |
| 189 | private FirstStrong() { |
| 190 | } |
| 191 | |
| 192 | public static final FirstStrong INSTANCE = new FirstStrong(); |
| 193 | } |
| 194 | |
| 195 | /** |
| 196 | * Algorithm that uses the presence of any strong directional non-format |
| 197 | * character (e.g. excludes LRE, LRO, RLE, RLO) to determine the |
| 198 | * direction of text. |
Doug Felt | cb379120 | 2011-07-07 11:57:48 -0700 | [diff] [blame] | 199 | */ |
Fabrice Di Meglio | e7beae3 | 2012-02-13 15:23:57 -0800 | [diff] [blame] | 200 | private static class AnyStrong implements TextDirectionAlgorithm { |
Doug Felt | cb379120 | 2011-07-07 11:57:48 -0700 | [diff] [blame] | 201 | private final boolean mLookForRtl; |
| 202 | |
| 203 | @Override |
Fabrice Di Meglio | 57a8574 | 2013-01-31 13:29:36 -0800 | [diff] [blame^] | 204 | public int checkRtl(CharSequence cs, int start, int count) { |
Doug Felt | cb379120 | 2011-07-07 11:57:48 -0700 | [diff] [blame] | 205 | boolean haveUnlookedFor = false; |
| 206 | for (int i = start, e = start + count; i < e; ++i) { |
Fabrice Di Meglio | 57a8574 | 2013-01-31 13:29:36 -0800 | [diff] [blame^] | 207 | switch (isRtlText(Character.getDirectionality(cs.charAt(i)))) { |
| 208 | case STATE_TRUE: |
Doug Felt | cb379120 | 2011-07-07 11:57:48 -0700 | [diff] [blame] | 209 | if (mLookForRtl) { |
Fabrice Di Meglio | 57a8574 | 2013-01-31 13:29:36 -0800 | [diff] [blame^] | 210 | return STATE_TRUE; |
Doug Felt | cb379120 | 2011-07-07 11:57:48 -0700 | [diff] [blame] | 211 | } |
| 212 | haveUnlookedFor = true; |
| 213 | break; |
Fabrice Di Meglio | 57a8574 | 2013-01-31 13:29:36 -0800 | [diff] [blame^] | 214 | case STATE_FALSE: |
Doug Felt | cb379120 | 2011-07-07 11:57:48 -0700 | [diff] [blame] | 215 | if (!mLookForRtl) { |
Fabrice Di Meglio | 57a8574 | 2013-01-31 13:29:36 -0800 | [diff] [blame^] | 216 | return STATE_FALSE; |
Doug Felt | cb379120 | 2011-07-07 11:57:48 -0700 | [diff] [blame] | 217 | } |
| 218 | haveUnlookedFor = true; |
| 219 | break; |
| 220 | default: |
| 221 | break; |
| 222 | } |
| 223 | } |
| 224 | if (haveUnlookedFor) { |
Fabrice Di Meglio | 57a8574 | 2013-01-31 13:29:36 -0800 | [diff] [blame^] | 225 | return mLookForRtl ? STATE_FALSE : STATE_TRUE; |
Doug Felt | cb379120 | 2011-07-07 11:57:48 -0700 | [diff] [blame] | 226 | } |
Fabrice Di Meglio | 57a8574 | 2013-01-31 13:29:36 -0800 | [diff] [blame^] | 227 | return STATE_UNKNOWN; |
Doug Felt | cb379120 | 2011-07-07 11:57:48 -0700 | [diff] [blame] | 228 | } |
| 229 | |
| 230 | private AnyStrong(boolean lookForRtl) { |
| 231 | this.mLookForRtl = lookForRtl; |
| 232 | } |
| 233 | |
| 234 | public static final AnyStrong INSTANCE_RTL = new AnyStrong(true); |
| 235 | public static final AnyStrong INSTANCE_LTR = new AnyStrong(false); |
| 236 | } |
| 237 | |
| 238 | /** |
Fabrice Di Meglio | 7810b5f | 2011-08-24 18:26:14 -0700 | [diff] [blame] | 239 | * Algorithm that uses the Locale direction to force the direction of a paragraph. |
| 240 | */ |
Fabrice Di Meglio | e7beae3 | 2012-02-13 15:23:57 -0800 | [diff] [blame] | 241 | private static class TextDirectionHeuristicLocale extends TextDirectionHeuristicImpl { |
Fabrice Di Meglio | 7810b5f | 2011-08-24 18:26:14 -0700 | [diff] [blame] | 242 | |
| 243 | public TextDirectionHeuristicLocale() { |
| 244 | super(null); |
| 245 | } |
| 246 | |
| 247 | @Override |
| 248 | protected boolean defaultIsRtl() { |
Fabrice Di Meglio | d3d9f3f | 2012-09-18 12:55:32 -0700 | [diff] [blame] | 249 | final int dir = TextUtils.getLayoutDirectionFromLocale(java.util.Locale.getDefault()); |
Fabrice Di Meglio | 3fb824b | 2012-02-28 17:58:31 -0800 | [diff] [blame] | 250 | return (dir == View.LAYOUT_DIRECTION_RTL); |
Fabrice Di Meglio | 7810b5f | 2011-08-24 18:26:14 -0700 | [diff] [blame] | 251 | } |
| 252 | |
| 253 | public static final TextDirectionHeuristicLocale INSTANCE = |
| 254 | new TextDirectionHeuristicLocale(); |
| 255 | } |
Doug Felt | cb379120 | 2011-07-07 11:57:48 -0700 | [diff] [blame] | 256 | } |