ScriptUtils.java revision 292deb632cbab232334190e68d29184094d6d51b
1/* 2 * Copyright (C) 2012 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 java.util.Locale; 20import java.util.TreeMap; 21 22/** 23 * A class to help with handling different writing scripts. 24 */ 25public class ScriptUtils { 26 // Used for hardware keyboards 27 public static final int SCRIPT_UNKNOWN = -1; 28 // TODO: should we use ISO 15924 identifiers instead? 29 public static final int SCRIPT_LATIN = 0; 30 public static final int SCRIPT_CYRILLIC = 1; 31 public static final int SCRIPT_GREEK = 2; 32 public static final int SCRIPT_ARABIC = 3; 33 public static final int SCRIPT_HEBREW = 4; 34 public static final TreeMap<String, Integer> mLanguageToScript; 35 static { 36 // List of the supported languages and their associated script. We won't check 37 // words written in another script than the selected script, because we know we 38 // don't have those in our dictionary so we will underline everything and we 39 // will never have any suggestions, so it makes no sense checking them, and this 40 // is done in {@link #shouldFilterOut}. Also, the script is used to choose which 41 // proximity to pass to the dictionary descent algorithm. 42 // IMPORTANT: this only contains languages - do not write countries in there. 43 // Only the language is searched from the map. 44 mLanguageToScript = new TreeMap<>(); 45 mLanguageToScript.put("cs", SCRIPT_LATIN); 46 mLanguageToScript.put("da", SCRIPT_LATIN); 47 mLanguageToScript.put("de", SCRIPT_LATIN); 48 mLanguageToScript.put("el", SCRIPT_GREEK); 49 mLanguageToScript.put("en", SCRIPT_LATIN); 50 mLanguageToScript.put("es", SCRIPT_LATIN); 51 mLanguageToScript.put("fi", SCRIPT_LATIN); 52 mLanguageToScript.put("fr", SCRIPT_LATIN); 53 mLanguageToScript.put("hr", SCRIPT_LATIN); 54 mLanguageToScript.put("it", SCRIPT_LATIN); 55 mLanguageToScript.put("lt", SCRIPT_LATIN); 56 mLanguageToScript.put("lv", SCRIPT_LATIN); 57 mLanguageToScript.put("nb", SCRIPT_LATIN); 58 mLanguageToScript.put("nl", SCRIPT_LATIN); 59 mLanguageToScript.put("pt", SCRIPT_LATIN); 60 mLanguageToScript.put("sl", SCRIPT_LATIN); 61 mLanguageToScript.put("ru", SCRIPT_CYRILLIC); 62 } 63 /* 64 * Returns whether the code point is a letter that makes sense for the specified 65 * locale for this spell checker. 66 * The dictionaries supported by Latin IME are described in res/xml/spellchecker.xml 67 * and is limited to EFIGS languages and Russian. 68 * Hence at the moment this explicitly tests for Cyrillic characters or Latin characters 69 * as appropriate, and explicitly excludes CJK, Arabic and Hebrew characters. 70 */ 71 public static boolean isLetterPartOfScript(final int codePoint, final int scriptId) { 72 switch (scriptId) { 73 case SCRIPT_LATIN: 74 // Our supported latin script dictionaries (EFIGS) at the moment only include 75 // characters in the C0, C1, Latin Extended A and B, IPA extensions unicode 76 // blocks. As it happens, those are back-to-back in the code range 0x40 to 0x2AF, 77 // so the below is a very efficient way to test for it. As for the 0-0x3F, it's 78 // excluded from isLetter anyway. 79 return codePoint <= 0x2AF && Character.isLetter(codePoint); 80 case SCRIPT_CYRILLIC: 81 // All Cyrillic characters are in the 400~52F block. There are some in the upper 82 // Unicode range, but they are archaic characters that are not used in modern 83 // Russian and are not used by our dictionary. 84 return codePoint >= 0x400 && codePoint <= 0x52F && Character.isLetter(codePoint); 85 case SCRIPT_GREEK: 86 // Greek letters are either in the 370~3FF range (Greek & Coptic), or in the 87 // 1F00~1FFF range (Greek extended). Our dictionary contains both sort of characters. 88 // Our dictionary also contains a few words with 0xF2; it would be best to check 89 // if that's correct, but a web search does return results for these words so 90 // they are probably okay. 91 return (codePoint >= 0x370 && codePoint <= 0x3FF) 92 || (codePoint >= 0x1F00 && codePoint <= 0x1FFF) 93 || codePoint == 0xF2; 94 case SCRIPT_ARABIC: 95 // Arabic letters can be in any of the following blocks: 96 // Arabic U+0600..U+06FF 97 // Arabic Supplement U+0750..U+077F 98 // Arabic Extended-A U+08A0..U+08FF 99 // Arabic Presentation Forms-A U+FB50..U+FDFF 100 // Arabic Presentation Forms-B U+FE70..U+FEFF 101 return (codePoint >= 0x600 && codePoint <= 0x6FF) 102 || (codePoint >= 0x750 && codePoint <= 0x77F) 103 || (codePoint >= 0x8A0 && codePoint <= 0x8FF) 104 || (codePoint >= 0xFB50 && codePoint <= 0xFDFF) 105 || (codePoint >= 0xFE70 && codePoint <= 0xFEFF); 106 case SCRIPT_HEBREW: 107 // Hebrew letters are in the Hebrew unicode block, which spans from U+0590 to U+05FF, 108 // or in the Alphabetic Presentation Forms block, U+FB00..U+FB4F, but only in the 109 // Hebrew part of that block, which is U+FB1D..U+FB4F. 110 return (codePoint >= 0x590 && codePoint <= 0x5FF 111 || codePoint >= 0xFB1D && codePoint <= 0xFB4F); 112 case SCRIPT_UNKNOWN: 113 return true; 114 default: 115 // Should never come here 116 throw new RuntimeException("Impossible value of script: " + scriptId); 117 } 118 } 119 120 public static int getScriptFromLocale(final Locale locale) { 121 final Integer script = mLanguageToScript.get(locale.getLanguage()); 122 if (null == script) { 123 throw new RuntimeException("We have been called with an unsupported language: \"" 124 + locale.getLanguage() + "\". Framework bug?"); 125 } 126 return script; 127 } 128} 129