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