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 17 package com.android.inputmethod.latin.utils; 18 19 import java.util.Locale; 20 import java.util.TreeMap; 21 22 /** 23 * A class to help with handling different writing scripts. 24 */ 25 public 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_ARABIC = 0; 30 public static final int SCRIPT_ARMENIAN = 1; 31 public static final int SCRIPT_BENGALI = 2; 32 public static final int SCRIPT_CYRILLIC = 3; 33 public static final int SCRIPT_DEVANAGARI = 4; 34 public static final int SCRIPT_GEORGIAN = 5; 35 public static final int SCRIPT_GREEK = 6; 36 public static final int SCRIPT_HEBREW = 7; 37 public static final int SCRIPT_KANNADA = 8; 38 public static final int SCRIPT_KHMER = 9; 39 public static final int SCRIPT_LAO = 10; 40 public static final int SCRIPT_LATIN = 11; 41 public static final int SCRIPT_MALAYALAM = 12; 42 public static final int SCRIPT_MYANMAR = 13; 43 public static final int SCRIPT_SINHALA = 14; 44 public static final int SCRIPT_TAMIL = 15; 45 public static final int SCRIPT_TELUGU = 16; 46 public static final int SCRIPT_THAI = 17; 47 public static final TreeMap<String, Integer> mSpellCheckerLanguageToScript; 48 static { 49 // List of the supported languages and their associated script. We won't check 50 // words written in another script than the selected script, because we know we 51 // don't have those in our dictionary so we will underline everything and we 52 // will never have any suggestions, so it makes no sense checking them, and this 53 // is done in {@link #shouldFilterOut}. Also, the script is used to choose which 54 // proximity to pass to the dictionary descent algorithm. 55 // IMPORTANT: this only contains languages - do not write countries in there. 56 // Only the language is searched from the map. 57 mSpellCheckerLanguageToScript = new TreeMap<>(); 58 mSpellCheckerLanguageToScript.put("cs", SCRIPT_LATIN); 59 mSpellCheckerLanguageToScript.put("da", SCRIPT_LATIN); 60 mSpellCheckerLanguageToScript.put("de", SCRIPT_LATIN); 61 mSpellCheckerLanguageToScript.put("el", SCRIPT_GREEK); 62 mSpellCheckerLanguageToScript.put("en", SCRIPT_LATIN); 63 mSpellCheckerLanguageToScript.put("es", SCRIPT_LATIN); 64 mSpellCheckerLanguageToScript.put("fi", SCRIPT_LATIN); 65 mSpellCheckerLanguageToScript.put("fr", SCRIPT_LATIN); 66 mSpellCheckerLanguageToScript.put("hr", SCRIPT_LATIN); 67 mSpellCheckerLanguageToScript.put("it", SCRIPT_LATIN); 68 mSpellCheckerLanguageToScript.put("lt", SCRIPT_LATIN); 69 mSpellCheckerLanguageToScript.put("lv", SCRIPT_LATIN); 70 mSpellCheckerLanguageToScript.put("nb", SCRIPT_LATIN); 71 mSpellCheckerLanguageToScript.put("nl", SCRIPT_LATIN); 72 mSpellCheckerLanguageToScript.put("pt", SCRIPT_LATIN); 73 mSpellCheckerLanguageToScript.put("sl", SCRIPT_LATIN); 74 mSpellCheckerLanguageToScript.put("ru", SCRIPT_CYRILLIC); 75 } 76 /* 77 * Returns whether the code point is a letter that makes sense for the specified 78 * locale for this spell checker. 79 * The dictionaries supported by Latin IME are described in res/xml/spellchecker.xml 80 * and is limited to EFIGS languages and Russian. 81 * Hence at the moment this explicitly tests for Cyrillic characters or Latin characters 82 * as appropriate, and explicitly excludes CJK, Arabic and Hebrew characters. 83 */ isLetterPartOfScript(final int codePoint, final int scriptId)84 public static boolean isLetterPartOfScript(final int codePoint, final int scriptId) { 85 switch (scriptId) { 86 case SCRIPT_ARABIC: 87 // Arabic letters can be in any of the following blocks: 88 // Arabic U+0600..U+06FF 89 // Arabic Supplement, Thaana U+0750..U+077F, U+0780..U+07BF 90 // Arabic Extended-A U+08A0..U+08FF 91 // Arabic Presentation Forms-A U+FB50..U+FDFF 92 // Arabic Presentation Forms-B U+FE70..U+FEFF 93 return (codePoint >= 0x600 && codePoint <= 0x6FF) 94 || (codePoint >= 0x750 && codePoint <= 0x7BF) 95 || (codePoint >= 0x8A0 && codePoint <= 0x8FF) 96 || (codePoint >= 0xFB50 && codePoint <= 0xFDFF) 97 || (codePoint >= 0xFE70 && codePoint <= 0xFEFF); 98 case SCRIPT_ARMENIAN: 99 // Armenian letters are in the Armenian unicode block, U+0530..U+058F and 100 // Alphabetic Presentation Forms block, U+FB00..U+FB4F, but only in the Armenian part 101 // of that block, which is U+FB13..U+FB17. 102 return (codePoint >= 0x530 && codePoint <= 0x58F 103 || codePoint >= 0xFB13 && codePoint <= 0xFB17); 104 case SCRIPT_BENGALI: 105 // Bengali unicode block is U+0980..U+09FF 106 return (codePoint >= 0x980 && codePoint <= 0x9FF); 107 case SCRIPT_CYRILLIC: 108 // All Cyrillic characters are in the 400~52F block. There are some in the upper 109 // Unicode range, but they are archaic characters that are not used in modern 110 // Russian and are not used by our dictionary. 111 return codePoint >= 0x400 && codePoint <= 0x52F && Character.isLetter(codePoint); 112 case SCRIPT_DEVANAGARI: 113 // Devanagari unicode block is +0900..U+097F 114 return (codePoint >= 0x900 && codePoint <= 0x97F); 115 case SCRIPT_GEORGIAN: 116 // Georgian letters are in the Georgian unicode block, U+10A0..U+10FF, 117 // or Georgian supplement block, U+2D00..U+2D2F 118 return (codePoint >= 0x10A0 && codePoint <= 0x10FF 119 || codePoint >= 0x2D00 && codePoint <= 0x2D2F); 120 case SCRIPT_GREEK: 121 // Greek letters are either in the 370~3FF range (Greek & Coptic), or in the 122 // 1F00~1FFF range (Greek extended). Our dictionary contains both sort of characters. 123 // Our dictionary also contains a few words with 0xF2; it would be best to check 124 // if that's correct, but a web search does return results for these words so 125 // they are probably okay. 126 return (codePoint >= 0x370 && codePoint <= 0x3FF) 127 || (codePoint >= 0x1F00 && codePoint <= 0x1FFF) 128 || codePoint == 0xF2; 129 case SCRIPT_HEBREW: 130 // Hebrew letters are in the Hebrew unicode block, which spans from U+0590 to U+05FF, 131 // or in the Alphabetic Presentation Forms block, U+FB00..U+FB4F, but only in the 132 // Hebrew part of that block, which is U+FB1D..U+FB4F. 133 return (codePoint >= 0x590 && codePoint <= 0x5FF 134 || codePoint >= 0xFB1D && codePoint <= 0xFB4F); 135 case SCRIPT_KANNADA: 136 // Kannada unicode block is U+0C80..U+0CFF 137 return (codePoint >= 0xC80 && codePoint <= 0xCFF); 138 case SCRIPT_KHMER: 139 // Khmer letters are in unicode block U+1780..U+17FF, and the Khmer symbols block 140 // is U+19E0..U+19FF 141 return (codePoint >= 0x1780 && codePoint <= 0x17FF 142 || codePoint >= 0x19E0 && codePoint <= 0x19FF); 143 case SCRIPT_LAO: 144 // The Lao block is U+0E80..U+0EFF 145 return (codePoint >= 0xE80 && codePoint <= 0xEFF); 146 case SCRIPT_LATIN: 147 // Our supported latin script dictionaries (EFIGS) at the moment only include 148 // characters in the C0, C1, Latin Extended A and B, IPA extensions unicode 149 // blocks. As it happens, those are back-to-back in the code range 0x40 to 0x2AF, 150 // so the below is a very efficient way to test for it. As for the 0-0x3F, it's 151 // excluded from isLetter anyway. 152 return codePoint <= 0x2AF && Character.isLetter(codePoint); 153 case SCRIPT_MALAYALAM: 154 // Malayalam unicode block is U+0D00..U+0D7F 155 return (codePoint >= 0xD00 && codePoint <= 0xD7F); 156 case SCRIPT_MYANMAR: 157 // Myanmar has three unicode blocks : 158 // Myanmar U+1000..U+109F 159 // Myanmar extended-A U+AA60..U+AA7F 160 // Myanmar extended-B U+A9E0..U+A9FF 161 return (codePoint >= 0x1000 && codePoint <= 0x109F 162 || codePoint >= 0xAA60 && codePoint <= 0xAA7F 163 || codePoint >= 0xA9E0 && codePoint <= 0xA9FF); 164 case SCRIPT_SINHALA: 165 // Sinhala unicode block is U+0D80..U+0DFF 166 return (codePoint >= 0xD80 && codePoint <= 0xDFF); 167 case SCRIPT_TAMIL: 168 // Tamil unicode block is U+0B80..U+0BFF 169 return (codePoint >= 0xB80 && codePoint <= 0xBFF); 170 case SCRIPT_TELUGU: 171 // Telugu unicode block is U+0C00..U+0C7F 172 return (codePoint >= 0xC00 && codePoint <= 0xC7F); 173 case SCRIPT_THAI: 174 // Thai unicode block is U+0E00..U+0E7F 175 return (codePoint >= 0xE00 && codePoint <= 0xE7F); 176 case SCRIPT_UNKNOWN: 177 return true; 178 default: 179 // Should never come here 180 throw new RuntimeException("Impossible value of script: " + scriptId); 181 } 182 } 183 getScriptFromSpellCheckerLocale(final Locale locale)184 public static int getScriptFromSpellCheckerLocale(final Locale locale) { 185 final Integer script = mSpellCheckerLanguageToScript.get(locale.getLanguage()); 186 if (null == script) { 187 throw new RuntimeException("We have been called with an unsupported language: \"" 188 + locale.getLanguage() + "\". Framework bug?"); 189 } 190 return script; 191 } 192 } 193