2014-06-26 04:07:49 +00:00
|
|
|
/*
|
|
|
|
* Copyright (C) 2012 The Android Open Source Project
|
|
|
|
*
|
|
|
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
* you may not use this file except in compliance with the License.
|
|
|
|
* You may obtain a copy of the License at
|
|
|
|
*
|
|
|
|
* http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
*
|
|
|
|
* Unless required by applicable law or agreed to in writing, software
|
|
|
|
* distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
* See the License for the specific language governing permissions and
|
|
|
|
* limitations under the License.
|
|
|
|
*/
|
|
|
|
|
|
|
|
package com.android.inputmethod.latin.utils;
|
|
|
|
|
|
|
|
import java.util.Locale;
|
|
|
|
import java.util.TreeMap;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* A class to help with handling different writing scripts.
|
|
|
|
*/
|
|
|
|
public class ScriptUtils {
|
2015-02-12 19:00:06 +00:00
|
|
|
|
2014-06-30 11:59:18 +00:00
|
|
|
// Used for hardware keyboards
|
|
|
|
public static final int SCRIPT_UNKNOWN = -1;
|
2015-02-12 19:00:06 +00:00
|
|
|
|
2014-07-30 06:56:56 +00:00
|
|
|
public static final int SCRIPT_ARABIC = 0;
|
|
|
|
public static final int SCRIPT_ARMENIAN = 1;
|
|
|
|
public static final int SCRIPT_BENGALI = 2;
|
|
|
|
public static final int SCRIPT_CYRILLIC = 3;
|
|
|
|
public static final int SCRIPT_DEVANAGARI = 4;
|
|
|
|
public static final int SCRIPT_GEORGIAN = 5;
|
|
|
|
public static final int SCRIPT_GREEK = 6;
|
|
|
|
public static final int SCRIPT_HEBREW = 7;
|
|
|
|
public static final int SCRIPT_KANNADA = 8;
|
|
|
|
public static final int SCRIPT_KHMER = 9;
|
|
|
|
public static final int SCRIPT_LAO = 10;
|
|
|
|
public static final int SCRIPT_LATIN = 11;
|
|
|
|
public static final int SCRIPT_MALAYALAM = 12;
|
|
|
|
public static final int SCRIPT_MYANMAR = 13;
|
|
|
|
public static final int SCRIPT_SINHALA = 14;
|
2014-07-30 04:01:44 +00:00
|
|
|
public static final int SCRIPT_TAMIL = 15;
|
2014-07-30 06:56:56 +00:00
|
|
|
public static final int SCRIPT_TELUGU = 16;
|
|
|
|
public static final int SCRIPT_THAI = 17;
|
2015-02-12 19:00:06 +00:00
|
|
|
|
|
|
|
private static final TreeMap<String, Integer> mIso15924toImeScriptCode;
|
|
|
|
|
2014-06-26 04:07:49 +00:00
|
|
|
static {
|
2015-02-12 19:00:06 +00:00
|
|
|
mIso15924toImeScriptCode = new TreeMap<>();
|
|
|
|
mIso15924toImeScriptCode.put("Arab", SCRIPT_ARABIC);
|
|
|
|
mIso15924toImeScriptCode.put("Armn", SCRIPT_ARMENIAN);
|
|
|
|
mIso15924toImeScriptCode.put("Beng", SCRIPT_BENGALI);
|
|
|
|
mIso15924toImeScriptCode.put("Cyrl", SCRIPT_CYRILLIC);
|
|
|
|
mIso15924toImeScriptCode.put("Deva", SCRIPT_DEVANAGARI);
|
|
|
|
mIso15924toImeScriptCode.put("Geor", SCRIPT_GEORGIAN);
|
|
|
|
mIso15924toImeScriptCode.put("Grek", SCRIPT_GREEK);
|
|
|
|
mIso15924toImeScriptCode.put("Hebr", SCRIPT_HEBREW);
|
|
|
|
mIso15924toImeScriptCode.put("Knda", SCRIPT_KANNADA);
|
|
|
|
mIso15924toImeScriptCode.put("Khmr", SCRIPT_KHMER);
|
|
|
|
mIso15924toImeScriptCode.put("Laoo", SCRIPT_LAO);
|
|
|
|
mIso15924toImeScriptCode.put("Latn", SCRIPT_LATIN);
|
|
|
|
mIso15924toImeScriptCode.put("Mlym", SCRIPT_MALAYALAM);
|
|
|
|
mIso15924toImeScriptCode.put("Mymr", SCRIPT_MYANMAR);
|
|
|
|
mIso15924toImeScriptCode.put("Sinh", SCRIPT_SINHALA);
|
|
|
|
mIso15924toImeScriptCode.put("Taml", SCRIPT_TAMIL);
|
|
|
|
mIso15924toImeScriptCode.put("Telu", SCRIPT_TELUGU);
|
|
|
|
mIso15924toImeScriptCode.put("Thai", SCRIPT_THAI);
|
2014-06-26 04:07:49 +00:00
|
|
|
}
|
|
|
|
/*
|
|
|
|
* Returns whether the code point is a letter that makes sense for the specified
|
|
|
|
* locale for this spell checker.
|
|
|
|
* The dictionaries supported by Latin IME are described in res/xml/spellchecker.xml
|
|
|
|
* and is limited to EFIGS languages and Russian.
|
|
|
|
* Hence at the moment this explicitly tests for Cyrillic characters or Latin characters
|
|
|
|
* as appropriate, and explicitly excludes CJK, Arabic and Hebrew characters.
|
|
|
|
*/
|
2014-06-27 13:44:24 +00:00
|
|
|
public static boolean isLetterPartOfScript(final int codePoint, final int scriptId) {
|
|
|
|
switch (scriptId) {
|
|
|
|
case SCRIPT_ARABIC:
|
|
|
|
// Arabic letters can be in any of the following blocks:
|
|
|
|
// Arabic U+0600..U+06FF
|
2014-07-30 04:01:44 +00:00
|
|
|
// Arabic Supplement, Thaana U+0750..U+077F, U+0780..U+07BF
|
2014-06-27 13:44:24 +00:00
|
|
|
// Arabic Extended-A U+08A0..U+08FF
|
|
|
|
// Arabic Presentation Forms-A U+FB50..U+FDFF
|
|
|
|
// Arabic Presentation Forms-B U+FE70..U+FEFF
|
|
|
|
return (codePoint >= 0x600 && codePoint <= 0x6FF)
|
2014-07-30 04:01:44 +00:00
|
|
|
|| (codePoint >= 0x750 && codePoint <= 0x7BF)
|
2014-06-27 13:44:24 +00:00
|
|
|
|| (codePoint >= 0x8A0 && codePoint <= 0x8FF)
|
|
|
|
|| (codePoint >= 0xFB50 && codePoint <= 0xFDFF)
|
|
|
|
|| (codePoint >= 0xFE70 && codePoint <= 0xFEFF);
|
2014-06-30 09:25:56 +00:00
|
|
|
case SCRIPT_ARMENIAN:
|
|
|
|
// Armenian letters are in the Armenian unicode block, U+0530..U+058F and
|
|
|
|
// Alphabetic Presentation Forms block, U+FB00..U+FB4F, but only in the Armenian part
|
|
|
|
// of that block, which is U+FB13..U+FB17.
|
|
|
|
return (codePoint >= 0x530 && codePoint <= 0x58F
|
|
|
|
|| codePoint >= 0xFB13 && codePoint <= 0xFB17);
|
2014-07-30 06:56:56 +00:00
|
|
|
case SCRIPT_BENGALI:
|
|
|
|
// Bengali unicode block is U+0980..U+09FF
|
|
|
|
return (codePoint >= 0x980 && codePoint <= 0x9FF);
|
|
|
|
case SCRIPT_CYRILLIC:
|
|
|
|
// All Cyrillic characters are in the 400~52F block. There are some in the upper
|
|
|
|
// Unicode range, but they are archaic characters that are not used in modern
|
|
|
|
// Russian and are not used by our dictionary.
|
|
|
|
return codePoint >= 0x400 && codePoint <= 0x52F && Character.isLetter(codePoint);
|
|
|
|
case SCRIPT_DEVANAGARI:
|
|
|
|
// Devanagari unicode block is +0900..U+097F
|
|
|
|
return (codePoint >= 0x900 && codePoint <= 0x97F);
|
2014-06-30 09:25:56 +00:00
|
|
|
case SCRIPT_GEORGIAN:
|
|
|
|
// Georgian letters are in the Georgian unicode block, U+10A0..U+10FF,
|
|
|
|
// or Georgian supplement block, U+2D00..U+2D2F
|
|
|
|
return (codePoint >= 0x10A0 && codePoint <= 0x10FF
|
|
|
|
|| codePoint >= 0x2D00 && codePoint <= 0x2D2F);
|
2014-07-30 06:56:56 +00:00
|
|
|
case SCRIPT_GREEK:
|
|
|
|
// Greek letters are either in the 370~3FF range (Greek & Coptic), or in the
|
|
|
|
// 1F00~1FFF range (Greek extended). Our dictionary contains both sort of characters.
|
|
|
|
// Our dictionary also contains a few words with 0xF2; it would be best to check
|
|
|
|
// if that's correct, but a web search does return results for these words so
|
|
|
|
// they are probably okay.
|
|
|
|
return (codePoint >= 0x370 && codePoint <= 0x3FF)
|
|
|
|
|| (codePoint >= 0x1F00 && codePoint <= 0x1FFF)
|
|
|
|
|| codePoint == 0xF2;
|
|
|
|
case SCRIPT_HEBREW:
|
|
|
|
// Hebrew letters are in the Hebrew unicode block, which spans from U+0590 to U+05FF,
|
|
|
|
// or in the Alphabetic Presentation Forms block, U+FB00..U+FB4F, but only in the
|
|
|
|
// Hebrew part of that block, which is U+FB1D..U+FB4F.
|
|
|
|
return (codePoint >= 0x590 && codePoint <= 0x5FF
|
|
|
|
|| codePoint >= 0xFB1D && codePoint <= 0xFB4F);
|
|
|
|
case SCRIPT_KANNADA:
|
|
|
|
// Kannada unicode block is U+0C80..U+0CFF
|
|
|
|
return (codePoint >= 0xC80 && codePoint <= 0xCFF);
|
2014-07-30 03:01:53 +00:00
|
|
|
case SCRIPT_KHMER:
|
|
|
|
// Khmer letters are in unicode block U+1780..U+17FF, and the Khmer symbols block
|
|
|
|
// is U+19E0..U+19FF
|
|
|
|
return (codePoint >= 0x1780 && codePoint <= 0x17FF
|
|
|
|
|| codePoint >= 0x19E0 && codePoint <= 0x19FF);
|
|
|
|
case SCRIPT_LAO:
|
|
|
|
// The Lao block is U+0E80..U+0EFF
|
|
|
|
return (codePoint >= 0xE80 && codePoint <= 0xEFF);
|
2014-07-30 06:56:56 +00:00
|
|
|
case SCRIPT_LATIN:
|
|
|
|
// Our supported latin script dictionaries (EFIGS) at the moment only include
|
|
|
|
// characters in the C0, C1, Latin Extended A and B, IPA extensions unicode
|
|
|
|
// blocks. As it happens, those are back-to-back in the code range 0x40 to 0x2AF,
|
|
|
|
// so the below is a very efficient way to test for it. As for the 0-0x3F, it's
|
|
|
|
// excluded from isLetter anyway.
|
|
|
|
return codePoint <= 0x2AF && Character.isLetter(codePoint);
|
|
|
|
case SCRIPT_MALAYALAM:
|
|
|
|
// Malayalam unicode block is U+0D00..U+0D7F
|
|
|
|
return (codePoint >= 0xD00 && codePoint <= 0xD7F);
|
2014-07-30 03:31:29 +00:00
|
|
|
case SCRIPT_MYANMAR:
|
|
|
|
// Myanmar has three unicode blocks :
|
|
|
|
// Myanmar U+1000..U+109F
|
|
|
|
// Myanmar extended-A U+AA60..U+AA7F
|
|
|
|
// Myanmar extended-B U+A9E0..U+A9FF
|
|
|
|
return (codePoint >= 0x1000 && codePoint <= 0x109F
|
|
|
|
|| codePoint >= 0xAA60 && codePoint <= 0xAA7F
|
|
|
|
|| codePoint >= 0xA9E0 && codePoint <= 0xA9FF);
|
|
|
|
case SCRIPT_SINHALA:
|
|
|
|
// Sinhala unicode block is U+0D80..U+0DFF
|
|
|
|
return (codePoint >= 0xD80 && codePoint <= 0xDFF);
|
2014-07-30 04:01:44 +00:00
|
|
|
case SCRIPT_TAMIL:
|
|
|
|
// Tamil unicode block is U+0B80..U+0BFF
|
|
|
|
return (codePoint >= 0xB80 && codePoint <= 0xBFF);
|
2014-07-30 06:56:56 +00:00
|
|
|
case SCRIPT_TELUGU:
|
|
|
|
// Telugu unicode block is U+0C00..U+0C7F
|
|
|
|
return (codePoint >= 0xC00 && codePoint <= 0xC7F);
|
|
|
|
case SCRIPT_THAI:
|
|
|
|
// Thai unicode block is U+0E00..U+0E7F
|
|
|
|
return (codePoint >= 0xE00 && codePoint <= 0xE7F);
|
2014-06-30 11:59:18 +00:00
|
|
|
case SCRIPT_UNKNOWN:
|
|
|
|
return true;
|
2014-06-26 04:07:49 +00:00
|
|
|
default:
|
|
|
|
// Should never come here
|
2014-06-27 13:44:24 +00:00
|
|
|
throw new RuntimeException("Impossible value of script: " + scriptId);
|
2014-06-26 04:07:49 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-02-12 19:00:06 +00:00
|
|
|
/**
|
|
|
|
* @param locale spell checker locale
|
|
|
|
* @return internal Latin IME script code that maps to an ISO 15924 script code
|
|
|
|
* {@see http://unicode.org/iso15924/iso15924-codes.html}
|
|
|
|
*/
|
2014-06-30 09:25:56 +00:00
|
|
|
public static int getScriptFromSpellCheckerLocale(final Locale locale) {
|
2015-02-12 19:00:06 +00:00
|
|
|
String isoScriptCode = locale.getScript();
|
|
|
|
Integer imeScriptCode = mIso15924toImeScriptCode.get(isoScriptCode);
|
|
|
|
if (imeScriptCode == null) {
|
|
|
|
throw new RuntimeException("Unsupported ISO 15924 code: " + isoScriptCode);
|
2014-06-26 04:07:49 +00:00
|
|
|
}
|
2015-02-12 19:00:06 +00:00
|
|
|
return imeScriptCode;
|
2014-06-26 04:07:49 +00:00
|
|
|
}
|
|
|
|
}
|