CapsModeUtils.java revision 60afa7000f14f8f8ca890236f636d45a2b59b61e
1/* 2 * Copyright (C) 2013 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 17package com.android.inputmethod.latin.utils; 18 19import android.text.InputType; 20import android.text.TextUtils; 21 22import com.android.inputmethod.latin.Constants; 23import com.android.inputmethod.latin.WordComposer; 24import com.android.inputmethod.latin.settings.SpacingAndPunctuations; 25 26import java.util.Locale; 27 28public final class CapsModeUtils { 29 private CapsModeUtils() { 30 // This utility class is not publicly instantiable. 31 } 32 33 /** 34 * Apply an auto-caps mode to a string. 35 * 36 * This intentionally does NOT apply manual caps mode. It only changes the capitalization if 37 * the mode is one of the auto-caps modes. 38 * @param s The string to capitalize. 39 * @param capitalizeMode The mode in which to capitalize. 40 * @param locale The locale for capitalizing. 41 * @return The capitalized string. 42 */ 43 public static String applyAutoCapsMode(final String s, final int capitalizeMode, 44 final Locale locale) { 45 if (WordComposer.CAPS_MODE_AUTO_SHIFT_LOCKED == capitalizeMode) { 46 return s.toUpperCase(locale); 47 } else if (WordComposer.CAPS_MODE_AUTO_SHIFTED == capitalizeMode) { 48 return StringUtils.capitalizeFirstCodePoint(s, locale); 49 } else { 50 return s; 51 } 52 } 53 54 /** 55 * Return whether a constant represents an auto-caps mode (either auto-shift or auto-shift-lock) 56 * @param mode The mode to test for 57 * @return true if this represents an auto-caps mode, false otherwise 58 */ 59 public static boolean isAutoCapsMode(final int mode) { 60 return WordComposer.CAPS_MODE_AUTO_SHIFTED == mode 61 || WordComposer.CAPS_MODE_AUTO_SHIFT_LOCKED == mode; 62 } 63 64 /** 65 * Determine what caps mode should be in effect at the current offset in 66 * the text. Only the mode bits set in <var>reqModes</var> will be 67 * checked. Note that the caps mode flags here are explicitly defined 68 * to match those in {@link InputType}. 69 * 70 * This code is a straight copy of TextUtils.getCapsMode (modulo namespace and formatting 71 * issues). This will change in the future as we simplify the code for our use and fix bugs. 72 * 73 * @param cs The text that should be checked for caps modes. 74 * @param reqModes The modes to be checked: may be any combination of 75 * {@link TextUtils#CAP_MODE_CHARACTERS}, {@link TextUtils#CAP_MODE_WORDS}, and 76 * {@link TextUtils#CAP_MODE_SENTENCES}. 77 * @param spacingAndPunctuations The current spacing and punctuations settings. 78 * @param hasSpaceBefore Whether we should consider there is a space inserted at the end of cs 79 * 80 * @return Returns the actual capitalization modes that can be in effect 81 * at the current position, which is any combination of 82 * {@link TextUtils#CAP_MODE_CHARACTERS}, {@link TextUtils#CAP_MODE_WORDS}, and 83 * {@link TextUtils#CAP_MODE_SENTENCES}. 84 */ 85 public static int getCapsMode(final CharSequence cs, final int reqModes, 86 final SpacingAndPunctuations spacingAndPunctuations, final boolean hasSpaceBefore) { 87 // Quick description of what we want to do: 88 // CAP_MODE_CHARACTERS is always on. 89 // CAP_MODE_WORDS is on if there is some whitespace before the cursor. 90 // CAP_MODE_SENTENCES is on if there is some whitespace before the cursor, and the end 91 // of a sentence just before that. 92 // We ignore opening parentheses and the like just before the cursor for purposes of 93 // finding whitespace for WORDS and SENTENCES modes. 94 // The end of a sentence ends with a period, question mark or exclamation mark. If it's 95 // a period, it also needs not to be an abbreviation, which means it also needs to either 96 // be immediately preceded by punctuation, or by a string of only letters with single 97 // periods interleaved. 98 99 // Step 1 : check for cap MODE_CHARACTERS. If it's looked for, it's always on. 100 if ((reqModes & (TextUtils.CAP_MODE_WORDS | TextUtils.CAP_MODE_SENTENCES)) == 0) { 101 // Here we are not looking for MODE_WORDS or MODE_SENTENCES, so since we already 102 // evaluated MODE_CHARACTERS, we can return. 103 return TextUtils.CAP_MODE_CHARACTERS & reqModes; 104 } 105 106 // Step 2 : Skip (ignore at the end of input) any opening punctuation. This includes 107 // opening parentheses, brackets, opening quotes, everything that *opens* a span of 108 // text in the linguistic sense. In RTL languages, this is still an opening sign, although 109 // it may look like a right parenthesis for example. We also include double quote and 110 // single quote since they aren't start punctuation in the unicode sense, but should still 111 // be skipped for English. TODO: does this depend on the language? 112 int i; 113 if (hasSpaceBefore) { 114 i = cs.length() + 1; 115 } else { 116 for (i = cs.length(); i > 0; i--) { 117 final char c = cs.charAt(i - 1); 118 if (c != Constants.CODE_DOUBLE_QUOTE && c != Constants.CODE_SINGLE_QUOTE 119 && Character.getType(c) != Character.START_PUNCTUATION) { 120 break; 121 } 122 } 123 } 124 125 // We are now on the character that precedes any starting punctuation, so in the most 126 // frequent case this will be whitespace or a letter, although it may occasionally be a 127 // start of line, or some symbol. 128 129 // Step 3 : Search for the start of a paragraph. From the starting point computed in step 2, 130 // we go back over any space or tab char sitting there. We find the start of a paragraph 131 // if the first char that's not a space or tab is a start of line (as in \n, start of text, 132 // or some other similar characters). 133 int j = i; 134 char prevChar = Constants.CODE_SPACE; 135 if (hasSpaceBefore) --j; 136 while (j > 0) { 137 prevChar = cs.charAt(j - 1); 138 if (!Character.isSpaceChar(prevChar) && prevChar != Constants.CODE_TAB) break; 139 j--; 140 } 141 if (j <= 0 || Character.isWhitespace(prevChar)) { 142 if (spacingAndPunctuations.mUsesGermanRules) { 143 // In German typography rules, there is a specific case that the first character 144 // of a new line should not be capitalized if the previous line ends in a comma. 145 boolean hasNewLine = false; 146 while (--j >= 0 && Character.isWhitespace(prevChar)) { 147 if (Constants.CODE_ENTER == prevChar) { 148 hasNewLine = true; 149 } 150 prevChar = cs.charAt(j); 151 } 152 if (Constants.CODE_COMMA == prevChar && hasNewLine) { 153 return (TextUtils.CAP_MODE_CHARACTERS | TextUtils.CAP_MODE_WORDS) & reqModes; 154 } 155 } 156 // There are only spacing chars between the start of the paragraph and the cursor, 157 // defined as a isWhitespace() char that is neither a isSpaceChar() nor a tab. Both 158 // MODE_WORDS and MODE_SENTENCES should be active. 159 return (TextUtils.CAP_MODE_CHARACTERS | TextUtils.CAP_MODE_WORDS 160 | TextUtils.CAP_MODE_SENTENCES) & reqModes; 161 } 162 if (i == j) { 163 // If we don't have whitespace before index i, it means neither MODE_WORDS 164 // nor mode sentences should be on so we can return right away. 165 return TextUtils.CAP_MODE_CHARACTERS & reqModes; 166 } 167 if ((reqModes & TextUtils.CAP_MODE_SENTENCES) == 0) { 168 // Here we know we have whitespace before the cursor (if not, we returned in the above 169 // if i == j clause), so we need MODE_WORDS to be on. And we don't need to evaluate 170 // MODE_SENTENCES so we can return right away. 171 return (TextUtils.CAP_MODE_CHARACTERS | TextUtils.CAP_MODE_WORDS) & reqModes; 172 } 173 // Please note that because of the reqModes & CAP_MODE_SENTENCES test a few lines above, 174 // we know that MODE_SENTENCES is being requested. 175 176 // Step 4 : Search for MODE_SENTENCES. 177 // English is a special case in that "American typography" rules, which are the most common 178 // in English, state that a sentence terminator immediately following a quotation mark 179 // should be swapped with it and de-duplicated (included in the quotation mark), 180 // e.g. <<Did he say, "let's go home?">> 181 // No other language has such a rule as far as I know, instead putting inside the quotation 182 // mark as the exact thing quoted and handling the surrounding punctuation independently, 183 // e.g. <<Did he say, "let's go home"?>> 184 if (spacingAndPunctuations.mUsesAmericanTypography) { 185 for (; j > 0; j--) { 186 // Here we look to go over any closing punctuation. This is because in dominant 187 // variants of English, the final period is placed within double quotes and maybe 188 // other closing punctuation signs. This is generally not true in other languages. 189 final char c = cs.charAt(j - 1); 190 if (c != Constants.CODE_DOUBLE_QUOTE && c != Constants.CODE_SINGLE_QUOTE 191 && Character.getType(c) != Character.END_PUNCTUATION) { 192 break; 193 } 194 } 195 } 196 197 if (j <= 0) return TextUtils.CAP_MODE_CHARACTERS & reqModes; 198 char c = cs.charAt(--j); 199 200 // We found the next interesting chunk of text ; next we need to determine if it's the 201 // end of a sentence. If we have a question mark or an exclamation mark, it's the end of 202 // a sentence. If it's neither, the only remaining case is the period so we get the opposite 203 // case out of the way. 204 if (c == Constants.CODE_QUESTION_MARK || c == Constants.CODE_EXCLAMATION_MARK) { 205 return (TextUtils.CAP_MODE_CHARACTERS | TextUtils.CAP_MODE_SENTENCES) & reqModes; 206 } 207 if (!spacingAndPunctuations.isSentenceSeparator(c) || j <= 0) { 208 return (TextUtils.CAP_MODE_CHARACTERS | TextUtils.CAP_MODE_WORDS) & reqModes; 209 } 210 211 // We found out that we have a period. We need to determine if this is a full stop or 212 // otherwise sentence-ending period, or an abbreviation like "e.g.". An abbreviation 213 // looks like (\w\.){2,} 214 // To find out, we will have a simple state machine with the following states : 215 // START, WORD, PERIOD, ABBREVIATION 216 // On START : (just before the first period) 217 // letter => WORD 218 // whitespace => end with no caps (it was a stand-alone period) 219 // otherwise => end with caps (several periods/symbols in a row) 220 // On WORD : (within the word just before the first period) 221 // letter => WORD 222 // period => PERIOD 223 // otherwise => end with caps (it was a word with a full stop at the end) 224 // On PERIOD : (period within a potential abbreviation) 225 // letter => LETTER 226 // otherwise => end with caps (it was not an abbreviation) 227 // On LETTER : (letter within a potential abbreviation) 228 // letter => LETTER 229 // period => PERIOD 230 // otherwise => end with no caps (it was an abbreviation) 231 // "Not an abbreviation" in the above chart essentially covers cases like "...yes.". This 232 // should capitalize. 233 234 final int START = 0; 235 final int WORD = 1; 236 final int PERIOD = 2; 237 final int LETTER = 3; 238 final int caps = (TextUtils.CAP_MODE_CHARACTERS | TextUtils.CAP_MODE_WORDS 239 | TextUtils.CAP_MODE_SENTENCES) & reqModes; 240 final int noCaps = (TextUtils.CAP_MODE_CHARACTERS | TextUtils.CAP_MODE_WORDS) & reqModes; 241 int state = START; 242 while (j > 0) { 243 c = cs.charAt(--j); 244 switch (state) { 245 case START: 246 if (Character.isLetter(c)) { 247 state = WORD; 248 } else if (Character.isWhitespace(c)) { 249 return noCaps; 250 } else { 251 return caps; 252 } 253 break; 254 case WORD: 255 if (Character.isLetter(c)) { 256 state = WORD; 257 } else if (spacingAndPunctuations.isSentenceSeparator(c)) { 258 state = PERIOD; 259 } else { 260 return caps; 261 } 262 break; 263 case PERIOD: 264 if (Character.isLetter(c)) { 265 state = LETTER; 266 } else { 267 return caps; 268 } 269 break; 270 case LETTER: 271 if (Character.isLetter(c)) { 272 state = LETTER; 273 } else if (spacingAndPunctuations.isSentenceSeparator(c)) { 274 state = PERIOD; 275 } else { 276 return noCaps; 277 } 278 } 279 } 280 // Here we arrived at the start of the line. This should behave exactly like whitespace. 281 return (START == state || LETTER == state) ? noCaps : caps; 282 } 283} 284