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