diff options
Diffstat (limited to 'native')
-rw-r--r-- | native/jni/com_android_inputmethod_keyboard_ProximityInfo.cpp | 25 | ||||
-rw-r--r-- | native/jni/jni_common.h | 18 | ||||
-rw-r--r-- | native/src/correction.h | 3 | ||||
-rw-r--r-- | native/src/defines.h | 2 | ||||
-rw-r--r-- | native/src/defines_touch_position_correction.h | 61 | ||||
-rw-r--r-- | native/src/proximity_info.cpp | 54 | ||||
-rw-r--r-- | native/src/proximity_info.h | 20 |
7 files changed, 172 insertions, 11 deletions
diff --git a/native/jni/com_android_inputmethod_keyboard_ProximityInfo.cpp b/native/jni/com_android_inputmethod_keyboard_ProximityInfo.cpp index f3e2a7e60..07cee4053 100644 --- a/native/jni/com_android_inputmethod_keyboard_ProximityInfo.cpp +++ b/native/jni/com_android_inputmethod_keyboard_ProximityInfo.cpp @@ -30,10 +30,25 @@ namespace latinime { static jint latinime_Keyboard_setProximityInfo(JNIEnv *env, jobject object, jint maxProximityCharsSize, jint displayWidth, jint displayHeight, jint gridWidth, - jint gridHeight, jintArray proximityCharsArray) { - jint* proximityChars = env->GetIntArrayElements(proximityCharsArray, NULL); + jint gridHeight, jintArray proximityCharsArray, jint keyCount, + jintArray keyXCoordinateArray, jintArray keyYCoordinateArray, jintArray keyWidthArray, + jintArray keyHeightArray, jintArray keyCharCodeArray, jint themeId) { + jint *proximityChars = env->GetIntArrayElements(proximityCharsArray, NULL); + jint *keyXCoordinates = safeGetIntArrayElements(env, keyXCoordinateArray); + jint *keyYCoordinates = safeGetIntArrayElements(env, keyYCoordinateArray); + jint *keyWidths = safeGetIntArrayElements(env, keyWidthArray); + jint *keyHeights = safeGetIntArrayElements(env, keyHeightArray); + jint *keyCharCodes = safeGetIntArrayElements(env, keyCharCodeArray); ProximityInfo *proximityInfo = new ProximityInfo(maxProximityCharsSize, displayWidth, - displayHeight, gridWidth, gridHeight, (const uint32_t *)proximityChars); + displayHeight, gridWidth, gridHeight, (const uint32_t*)proximityChars, + keyCount, (const int32_t*)keyXCoordinates, (const int32_t*)keyYCoordinates, + (const int32_t*)keyWidths, (const int32_t*)keyHeights, (const int32_t*)keyCharCodes, + themeId); + safeReleaseIntArrayElements(env, keyCharCodeArray, keyCharCodes); + safeReleaseIntArrayElements(env, keyHeightArray, keyHeights); + safeReleaseIntArrayElements(env, keyWidthArray, keyWidths); + safeReleaseIntArrayElements(env, keyYCoordinateArray, keyYCoordinates); + safeReleaseIntArrayElements(env, keyXCoordinateArray, keyXCoordinates); env->ReleaseIntArrayElements(proximityCharsArray, proximityChars, 0); return (jint)proximityInfo; } @@ -45,12 +60,12 @@ static void latinime_Keyboard_release(JNIEnv *env, jobject object, jint proximit } static JNINativeMethod sKeyboardMethods[] = { - {"setProximityInfoNative", "(IIIII[I)I", (void*)latinime_Keyboard_setProximityInfo}, + {"setProximityInfoNative", "(IIIII[II[I[I[I[I[II)I", (void*)latinime_Keyboard_setProximityInfo}, {"releaseProximityInfoNative", "(I)V", (void*)latinime_Keyboard_release} }; int register_ProximityInfo(JNIEnv *env) { - const char* const kClassPathName = "com/android/inputmethod/keyboard/ProximityInfo"; + const char *const kClassPathName = "com/android/inputmethod/keyboard/ProximityInfo"; return registerNativeMethods(env, kClassPathName, sKeyboardMethods, sizeof(sKeyboardMethods) / sizeof(sKeyboardMethods[0])); } diff --git a/native/jni/jni_common.h b/native/jni/jni_common.h index c502fa3a8..dbf6d3e19 100644 --- a/native/jni/jni_common.h +++ b/native/jni/jni_common.h @@ -18,13 +18,29 @@ #ifndef LATINIME_JNI_COMMON_H #define LATINIME_JNI_COMMON_H +#include <stdlib.h> + #include "jni.h" namespace latinime { -int registerNativeMethods(JNIEnv* env, const char* className, JNINativeMethod* methods, +int registerNativeMethods(JNIEnv *env, const char *className, JNINativeMethod *methods, int numMethods); +inline jint *safeGetIntArrayElements(JNIEnv *env, jintArray jArray) { + if (jArray) { + return env->GetIntArrayElements(jArray, NULL); + } else { + return NULL; + } +} + +inline void safeReleaseIntArrayElements(JNIEnv *env, jintArray jArray, jint *cArray) { + if (jArray) { + env->ReleaseIntArrayElements(jArray, cArray, 0); + } +} + } // namespace latinime #endif // LATINIME_JNI_COMMON_H diff --git a/native/src/correction.h b/native/src/correction.h index f3194b788..41130ad77 100644 --- a/native/src/correction.h +++ b/native/src/correction.h @@ -119,8 +119,9 @@ private: int mTerminalInputIndex; int mTerminalOutputIndex; unsigned short mWord[MAX_WORD_LENGTH_INTERNAL]; + // Edit distance calculation requires a buffer with (N+1)^2 length for the input length N. // Caveat: Do not create multiple tables per thread as this table eats up RAM a lot. - int mEditDistanceTable[MAX_WORD_LENGTH_INTERNAL * MAX_WORD_LENGTH_INTERNAL]; + int mEditDistanceTable[(MAX_WORD_LENGTH_INTERNAL + 1) * (MAX_WORD_LENGTH_INTERNAL + 1)]; CorrectionState mCorrectionStates[MAX_WORD_LENGTH_INTERNAL]; diff --git a/native/src/defines.h b/native/src/defines.h index 009d0ad3d..55469a788 100644 --- a/native/src/defines.h +++ b/native/src/defines.h @@ -131,7 +131,7 @@ static void dumpWord(const unsigned short* word, const int length) { #endif // FLAG_DBG #ifndef U_SHORT_MAX -#define U_SHORT_MAX 1 << 16 +#define U_SHORT_MAX 65535 // ((1 << 16) - 1) #endif #ifndef S_INT_MAX #define S_INT_MAX 2147483647 // ((1 << 31) - 1) diff --git a/native/src/defines_touch_position_correction.h b/native/src/defines_touch_position_correction.h new file mode 100644 index 000000000..122906804 --- /dev/null +++ b/native/src/defines_touch_position_correction.h @@ -0,0 +1,61 @@ +/* +** +** Copyright 2011, 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. +*/ + +#ifndef LATINIME_DEFINES_TOUCH_POSITION_CORRECTION_H +#define LATINIME_DEFINES_TOUCH_POSITION_CORRECTION_H + +#define OUTER_SWEET_SPOT_RADIUS_RATIO 1.3 + +static const char* TOUCH_POSITION_CORRECTION_GROUPS[] = { + "qwertyuiop", + "a", + "sdfghjk", + "l", + "zxcvbnm", +}; + +// (center X) / (key width) +static const float RELATIVE_TOUCH_CENTER_X[] = { + 0, // qwertyuiop + -0.26871, // a + 0, // sdfghjk + 0.028050, // l + 0, // zxcvbnm +}; + +// (center Y) / (key height) +static const float RELATIVE_TOUCH_CENTER_Y[] = { + 0.192088, // qwertyuiop + 0.214100, // a + 0.216640, // sdfghjk + 0.233288, // l + 0.286598, // zxcvbnm +}; + +// (sweet spot radius) / ((key width) + (key height)) +static const float SWEET_SPOT_RADIUS[] = { + 0.148955, // qwertyuiop + 0.185437, // a + 0.145522, // sdfghjk + 0.156882, // l + 0.144211, // zxcvbnm +}; + +#define CORRECTION_GROUP_COUNT \ + ((int)(sizeof(TOUCH_POSITION_CORRECTION_GROUPS) / sizeof(TOUCH_POSITION_CORRECTION_GROUPS[0]))) + +#endif // LATINIME_DEFINES_TOUCH_POSITION_CORRECTION_H diff --git a/native/src/proximity_info.cpp b/native/src/proximity_info.cpp index 361bdacbf..58842b92f 100644 --- a/native/src/proximity_info.cpp +++ b/native/src/proximity_info.cpp @@ -14,29 +14,76 @@ * limitations under the License. */ +#include <assert.h> #include <stdio.h> #include <string.h> #define LOG_TAG "LatinIME: proximity_info.cpp" +#include "defines_touch_position_correction.h" #include "dictionary.h" #include "proximity_info.h" namespace latinime { +inline void copyOrFillZero(void *to, const void *from, size_t size) { + if (from) { + memcpy(to, from, size); + } else { + memset(to, 0, size); + } +} + ProximityInfo::ProximityInfo(const int maxProximityCharsSize, const int keyboardWidth, const int keyboardHeight, const int gridWidth, const int gridHeight, - const uint32_t *proximityCharsArray) + const uint32_t *proximityCharsArray, const int keyCount, const int32_t *keyXCoordinates, + const int32_t *keyYCoordinates, const int32_t *keyWidths, const int32_t *keyHeights, + const int32_t *keyCharCodes, int themeId) : MAX_PROXIMITY_CHARS_SIZE(maxProximityCharsSize), KEYBOARD_WIDTH(keyboardWidth), KEYBOARD_HEIGHT(keyboardHeight), GRID_WIDTH(gridWidth), GRID_HEIGHT(gridHeight), CELL_WIDTH((keyboardWidth + gridWidth - 1) / gridWidth), - CELL_HEIGHT((keyboardHeight + gridHeight - 1) / gridHeight) { + CELL_HEIGHT((keyboardHeight + gridHeight - 1) / gridHeight), + KEY_COUNT(min(keyCount, MAX_KEY_COUNT_IN_A_KEYBOARD)), THEME_ID(themeId) { const int len = GRID_WIDTH * GRID_HEIGHT * MAX_PROXIMITY_CHARS_SIZE; mProximityCharsArray = new uint32_t[len]; if (DEBUG_PROXIMITY_INFO) { LOGI("Create proximity info array %d", len); } memcpy(mProximityCharsArray, proximityCharsArray, len * sizeof(mProximityCharsArray[0])); + + copyOrFillZero(mKeyXCoordinates, keyXCoordinates, KEY_COUNT * sizeof(mKeyXCoordinates[0])); + copyOrFillZero(mKeyYCoordinates, keyYCoordinates, KEY_COUNT * sizeof(mKeyYCoordinates[0])); + copyOrFillZero(mKeyWidths, keyWidths, KEY_COUNT * sizeof(mKeyWidths[0])); + copyOrFillZero(mKeyHeights, keyHeights, KEY_COUNT * sizeof(mKeyHeights[0])); + copyOrFillZero(mKeyCharCodes, keyCharCodes, KEY_COUNT * sizeof(mKeyCharCodes[0])); + + initializeCodeToGroup(); + initializeCodeToKeyIndex(); +} + +// Build the reversed look up table from the char code to the index in its group. +// see TOUCH_POSITION_CORRECTION_GROUPS +void ProximityInfo::initializeCodeToGroup() { + memset(mCodeToGroup, -1, (MAX_GROUPED_CHAR_CODE + 1) * sizeof(mCodeToGroup[0])); + for (int i = 0; i < CORRECTION_GROUP_COUNT; ++i) { + const char *group = TOUCH_POSITION_CORRECTION_GROUPS[i]; + for (int j = 0; group[j]; ++j) { + const int code = group[j]; + if (0 <= code && code <= MAX_GROUPED_CHAR_CODE) + mCodeToGroup[code] = i; + } + } +} + +// Build the reversed look up table from the char code to the index in mKeyXCoordinates, +// mKeyYCoordinates, mKeyWidths, mKeyHeights, mKeyCharCodes. +void ProximityInfo::initializeCodeToKeyIndex() { + memset(mCodeToKeyIndex, -1, (MAX_GROUPED_CHAR_CODE + 1) * sizeof(mCodeToKeyIndex[0])); + for (int i = 0; i < KEY_COUNT; ++i) { + const int code = mKeyCharCodes[i]; + if (0 <= code && code <= MAX_GROUPED_CHAR_CODE) + mCodeToKeyIndex[code] = i; + } } ProximityInfo::~ProximityInfo() { @@ -162,4 +209,7 @@ bool ProximityInfo::sameAsTyped(const unsigned short *word, int length) const { return true; } +const int ProximityInfo::MAX_KEY_COUNT_IN_A_KEYBOARD; +const int ProximityInfo::MAX_GROUPED_CHAR_CODE; + } // namespace latinime diff --git a/native/src/proximity_info.h b/native/src/proximity_info.h index 75fc8fb63..3190e73ef 100644 --- a/native/src/proximity_info.h +++ b/native/src/proximity_info.h @@ -35,7 +35,9 @@ public: ProximityInfo(const int maxProximityCharsSize, const int keyboardWidth, const int keybaordHeight, const int gridWidth, const int gridHeight, - const uint32_t *proximityCharsArray); + const uint32_t *proximityCharsArray, const int keyCount, const int32_t *keyXCoordinates, + const int32_t *keyYCoordinates, const int32_t *keyWidths, const int32_t *keyHeights, + const int32_t *keyCharCodes, int themeId); ~ProximityInfo(); bool hasSpaceProximity(const int x, const int y) const; void setInputParams(const int* inputCodes, const int inputLength); @@ -51,7 +53,14 @@ public: } private: + // The max number of the keys in one keyboard layout + static const int MAX_KEY_COUNT_IN_A_KEYBOARD = 64; + // The upper limit of the char code in TOUCH_POSITION_CORRECTION_GROUP + static const int MAX_GROUPED_CHAR_CODE = 127; + int getStartIndexFromCoordinates(const int x, const int y) const; + void initializeCodeToGroup(); + void initializeCodeToKeyIndex(); const int MAX_PROXIMITY_CHARS_SIZE; const int KEYBOARD_WIDTH; const int KEYBOARD_HEIGHT; @@ -59,10 +68,19 @@ private: const int GRID_HEIGHT; const int CELL_WIDTH; const int CELL_HEIGHT; + const int KEY_COUNT; + const int THEME_ID; const int *mInputCodes; uint32_t *mProximityCharsArray; + int32_t mKeyXCoordinates[MAX_KEY_COUNT_IN_A_KEYBOARD]; + int32_t mKeyYCoordinates[MAX_KEY_COUNT_IN_A_KEYBOARD]; + int32_t mKeyWidths[MAX_KEY_COUNT_IN_A_KEYBOARD]; + int32_t mKeyHeights[MAX_KEY_COUNT_IN_A_KEYBOARD]; + int32_t mKeyCharCodes[MAX_KEY_COUNT_IN_A_KEYBOARD]; int mInputLength; unsigned short mPrimaryInputWord[MAX_WORD_LENGTH_INTERNAL]; + int mCodeToGroup[MAX_GROUPED_CHAR_CODE + 1]; + int mCodeToKeyIndex[MAX_GROUPED_CHAR_CODE + 1]; }; } // namespace latinime |