diff --git a/native/jni/com_android_inputmethod_keyboard_ProximityInfo.cpp b/native/jni/com_android_inputmethod_keyboard_ProximityInfo.cpp index e47a64a9e..9eb437c06 100644 --- a/native/jni/com_android_inputmethod_keyboard_ProximityInfo.cpp +++ b/native/jni/com_android_inputmethod_keyboard_ProximityInfo.cpp @@ -50,7 +50,7 @@ static jlong latinime_Keyboard_setProximityInfo(JNIEnv *env, jobject object, ProximityInfo *proximityInfo = new ProximityInfo( localeStr, maxProximityCharsSize, displayWidth, displayHeight, gridWidth, gridHeight, mostCommonkeyWidth, - (const uint32_t*)proximityChars, + (const int32_t*)proximityChars, keyCount, (const int32_t*)keyXCoordinates, (const int32_t*)keyYCoordinates, (const int32_t*)keyWidths, (const int32_t*)keyHeights, (const int32_t*)keyCharCodes, (const float*)sweetSpotCenterXs, (const float*)sweetSpotCenterYs, diff --git a/native/src/additional_proximity_chars.cpp b/native/src/additional_proximity_chars.cpp index 401c85aae..224f020f2 100644 --- a/native/src/additional_proximity_chars.cpp +++ b/native/src/additional_proximity_chars.cpp @@ -19,23 +19,23 @@ namespace latinime { const std::string AdditionalProximityChars::LOCALE_EN_US("en"); -const uint32_t AdditionalProximityChars::EN_US_ADDITIONAL_A[EN_US_ADDITIONAL_A_SIZE] = { +const int32_t AdditionalProximityChars::EN_US_ADDITIONAL_A[EN_US_ADDITIONAL_A_SIZE] = { 'e', 'i', 'o', 'u' }; -const uint32_t AdditionalProximityChars::EN_US_ADDITIONAL_E[EN_US_ADDITIONAL_E_SIZE] = { +const int32_t AdditionalProximityChars::EN_US_ADDITIONAL_E[EN_US_ADDITIONAL_E_SIZE] = { 'a', 'i', 'o', 'u' }; -const uint32_t AdditionalProximityChars::EN_US_ADDITIONAL_I[EN_US_ADDITIONAL_I_SIZE] = { +const int32_t AdditionalProximityChars::EN_US_ADDITIONAL_I[EN_US_ADDITIONAL_I_SIZE] = { 'a', 'e', 'o', 'u' }; -const uint32_t AdditionalProximityChars::EN_US_ADDITIONAL_O[EN_US_ADDITIONAL_O_SIZE] = { +const int32_t AdditionalProximityChars::EN_US_ADDITIONAL_O[EN_US_ADDITIONAL_O_SIZE] = { 'a', 'e', 'i', 'u' }; -const uint32_t AdditionalProximityChars::EN_US_ADDITIONAL_U[EN_US_ADDITIONAL_U_SIZE] = { +const int32_t AdditionalProximityChars::EN_US_ADDITIONAL_U[EN_US_ADDITIONAL_U_SIZE] = { 'a', 'e', 'i', 'o' }; } diff --git a/native/src/additional_proximity_chars.h b/native/src/additional_proximity_chars.h index e0049d949..e0ecc0e1d 100644 --- a/native/src/additional_proximity_chars.h +++ b/native/src/additional_proximity_chars.h @@ -20,29 +20,31 @@ #include #include +#include "defines.h" + namespace latinime { class AdditionalProximityChars { private: static const std::string LOCALE_EN_US; static const int EN_US_ADDITIONAL_A_SIZE = 4; - static const uint32_t EN_US_ADDITIONAL_A[]; + static const int32_t EN_US_ADDITIONAL_A[]; static const int EN_US_ADDITIONAL_E_SIZE = 4; - static const uint32_t EN_US_ADDITIONAL_E[]; + static const int32_t EN_US_ADDITIONAL_E[]; static const int EN_US_ADDITIONAL_I_SIZE = 4; - static const uint32_t EN_US_ADDITIONAL_I[]; + static const int32_t EN_US_ADDITIONAL_I[]; static const int EN_US_ADDITIONAL_O_SIZE = 4; - static const uint32_t EN_US_ADDITIONAL_O[]; + static const int32_t EN_US_ADDITIONAL_O[]; static const int EN_US_ADDITIONAL_U_SIZE = 4; - static const uint32_t EN_US_ADDITIONAL_U[]; + static const int32_t EN_US_ADDITIONAL_U[]; - static bool isEnLocale(const std::string* locale_str) { + static bool isEnLocale(const std::string *locale_str) { return locale_str && locale_str->size() >= LOCALE_EN_US.size() - && locale_str->compare(0, LOCALE_EN_US.size(), LOCALE_EN_US); + && LOCALE_EN_US.compare(0, LOCALE_EN_US.size(), *locale_str); } public: - static int getAdditionalCharsSize(const std::string* locale_str, const uint16_t c) { + static int getAdditionalCharsSize(const std::string* locale_str, const int32_t c) { if (!isEnLocale(locale_str)) { return 0; } @@ -62,7 +64,7 @@ class AdditionalProximityChars { } } - static const uint32_t* getAdditionalChars(const std::string* locale_str, const uint32_t c) { + static const int32_t* getAdditionalChars(const std::string *locale_str, const int32_t c) { if (!isEnLocale(locale_str)) { return 0; } @@ -82,7 +84,7 @@ class AdditionalProximityChars { } } - static bool hasAdditionalChars(const std::string* locale_str, const uint32_t c) { + static bool hasAdditionalChars(const std::string *locale_str, const int32_t c) { return getAdditionalCharsSize(locale_str, c) > 0; } }; diff --git a/native/src/defines.h b/native/src/defines.h index afa1e0404..b0dbffa0f 100644 --- a/native/src/defines.h +++ b/native/src/defines.h @@ -115,6 +115,7 @@ static inline void prof_out(void) { #define DEBUG_NODE DEBUG_DICT_FULL #define DEBUG_TRACE DEBUG_DICT_FULL #define DEBUG_PROXIMITY_INFO false +#define DEBUG_PROXIMITY_CHARS false #define DEBUG_CORRECTION false #define DEBUG_CORRECTION_FREQ false #define DEBUG_WORDS_PRIORITY_QUEUE false @@ -128,6 +129,7 @@ static inline void prof_out(void) { #define DEBUG_NODE false #define DEBUG_TRACE false #define DEBUG_PROXIMITY_INFO false +#define DEBUG_PROXIMITY_CHARS false #define DEBUG_CORRECTION false #define DEBUG_CORRECTION_FREQ false #define DEBUG_WORDS_PRIORITY_QUEUE false diff --git a/native/src/proximity_info.cpp b/native/src/proximity_info.cpp index e590740d2..dd60ed62c 100644 --- a/native/src/proximity_info.cpp +++ b/native/src/proximity_info.cpp @@ -37,7 +37,7 @@ inline void copyOrFillZero(void *to, const void *from, size_t size) { ProximityInfo::ProximityInfo(const std::string localeStr, const int maxProximityCharsSize, const int keyboardWidth, const int keyboardHeight, const int gridWidth, const int gridHeight, const int mostCommonKeyWidth, - const uint32_t *proximityCharsArray, const int keyCount, const int32_t *keyXCoordinates, + const int32_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, const float *sweetSpotCenterXs, const float *sweetSpotCenterYs, const float *sweetSpotRadii) @@ -54,7 +54,7 @@ ProximityInfo::ProximityInfo(const std::string localeStr, const int maxProximity mInputXCoordinates(0), mInputYCoordinates(0), mTouchPositionCorrectionEnabled(false) { const int proximityGridLength = GRID_WIDTH * GRID_HEIGHT * MAX_PROXIMITY_CHARS_SIZE; - mProximityCharsArray = new uint32_t[proximityGridLength]; + mProximityCharsArray = new int32_t[proximityGridLength]; if (DEBUG_PROXIMITY_INFO) { AKLOGI("Create proximity info array %d", proximityGridLength); } @@ -148,35 +148,43 @@ int ProximityInfo::squaredDistanceToEdge(const int keyId, const int x, const int } void ProximityInfo::calculateNearbyKeyCodes( - const int x, const int y, const uint32_t primaryKey, int *inputCodes) { + const int x, const int y, const int32_t primaryKey, int *inputCodes) { int insertPos = 0; inputCodes[insertPos++] = primaryKey; const int startIndex = getStartIndexFromCoordinates(x, y); for (int i = 0; i < MAX_PROXIMITY_CHARS_SIZE; ++i) { - const uint32_t c = mProximityCharsArray[startIndex + i]; + const int32_t c = mProximityCharsArray[startIndex + i]; if (c < KEYCODE_SPACE || c == primaryKey) { continue; } - for (int j = 0; j < KEY_COUNT; ++j) { - const bool onKey = isOnKey(j, x, y); - const int distance = squaredDistanceToEdge(j, x, y); - if (onKey || distance < MOST_COMMON_KEY_WIDTH_SQUARE) { - inputCodes[insertPos++] = c; + int keyIndex = getKeyIndex(c); + const bool onKey = isOnKey(keyIndex, x, y); + const int distance = squaredDistanceToEdge(keyIndex, x, y); + if (onKey || distance < MOST_COMMON_KEY_WIDTH_SQUARE) { + inputCodes[insertPos++] = c; + if (insertPos >= MAX_PROXIMITY_CHARS_SIZE) { + if (DEBUG_DICT) { + assert(false); + } + return; } } } - const int existingProximitySize = insertPos; - for (int i = 0; i < existingProximitySize; ++i) { - const uint32_t c = inputCodes[i]; - const int additionalProximitySize = - AdditionalProximityChars::hasAdditionalChars(&mLocaleStr, c); - if (additionalProximitySize <= 0) { - continue; + inputCodes[insertPos++] = ADDITIONAL_PROXIMITY_CHAR_DELIMITER_CODE; + if (insertPos >= MAX_PROXIMITY_CHARS_SIZE) { + if (DEBUG_DICT) { + assert(false); } - const uint32_t* additionalProximityChars = - AdditionalProximityChars::getAdditionalChars(&mLocaleStr, c); + return; + } + + const int additionalProximitySize = + AdditionalProximityChars::getAdditionalCharsSize(&mLocaleStr, primaryKey); + if (additionalProximitySize > 0) { + const int32_t* additionalProximityChars = + AdditionalProximityChars::getAdditionalChars(&mLocaleStr, primaryKey); for (int j = 0; j < additionalProximitySize; ++j) { - const uint32_t ac = additionalProximityChars[j]; + const int32_t ac = additionalProximityChars[j]; int k = 0; for (; k < insertPos; ++k) { if ((int)ac == inputCodes[k]) { @@ -187,9 +195,16 @@ void ProximityInfo::calculateNearbyKeyCodes( continue; } inputCodes[insertPos++] = ac; + if (insertPos >= MAX_PROXIMITY_CHARS_SIZE) { + if (DEBUG_DICT) { + assert(false); + } + return; + } } } - // TODO: calculate additional chars + // Add a delimiter for the proximity characters + inputCodes[insertPos] = 0; } // TODO: Calculate nearby codes here. @@ -205,8 +220,30 @@ void ProximityInfo::setInputParams(const int* inputCodes, const int inputLength, mPrimaryInputWord[i] = getPrimaryCharAt(i); } mPrimaryInputWord[inputLength] = 0; + if (DEBUG_PROXIMITY_CHARS) { + AKLOGI("--- setInputParams"); + } for (int i = 0; i < mInputLength; ++i) { const int *proximityChars = getProximityCharsAt(i); + const int primaryKey = proximityChars[0]; + const int x = xCoordinates[i]; + const int y = yCoordinates[i]; + if (DEBUG_PROXIMITY_CHARS) { + int a = x + y + primaryKey; + a += 0; + AKLOGI("--- Primary = %c, x = %d, y = %d", primaryKey, x, y); + // Keep debug code just in case + //int proximities[50]; + //for (int m = 0; m < 50; ++m) { + //proximities[m] = 0; + //} + //calculateNearbyKeyCodes(x, y, primaryKey, proximities); + //for (int l = 0; l < 50 && proximities[l] > 0; ++l) { + //if (DEBUG_PROXIMITY_CHARS) { + //AKLOGI("--- native Proximity (%d) = %c", l, proximities[l]); + //} + //} + } for (int j = 0; j < MAX_PROXIMITY_CHARS_SIZE && proximityChars[j] > 0; ++j) { const int currentChar = proximityChars[j]; const int keyIndex = getKeyIndex(currentChar); @@ -219,6 +256,9 @@ void ProximityInfo::setInputParams(const int* inputCodes, const int inputLength, ? EQUIVALENT_CHAR_WITHOUT_DISTANCE_INFO : PROXIMITY_CHAR_WITHOUT_DISTANCE_INFO; } + if (DEBUG_PROXIMITY_CHARS) { + AKLOGI("--- Proximity (%d) = %c", j, currentChar); + } } } } diff --git a/native/src/proximity_info.h b/native/src/proximity_info.h index 8c342e0ef..1a47aff84 100644 --- a/native/src/proximity_info.h +++ b/native/src/proximity_info.h @@ -47,7 +47,7 @@ class ProximityInfo { ProximityInfo(const std::string localeStr, const int maxProximityCharsSize, const int keyboardWidth, const int keybaordHeight, const int gridWidth, const int gridHeight, const int mostCommonkeyWidth, - const uint32_t *proximityCharsArray, const int keyCount, const int32_t *keyXCoordinates, + const int32_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, const float *sweetSpotCenterXs, const float *sweetSpotCenterYs, const float *sweetSpotRadii); @@ -92,7 +92,7 @@ class ProximityInfo { bool isOnKey(const int keyId, const int x, const int y); int squaredDistanceToEdge(const int keyId, const int x, const int y); void calculateNearbyKeyCodes( - const int x, const int y, const uint32_t primaryKey, int *inputCodes); + const int x, const int y, const int32_t primaryKey, int *inputCodes); const int MAX_PROXIMITY_CHARS_SIZE; const int KEYBOARD_WIDTH; @@ -109,7 +109,7 @@ class ProximityInfo { const int *mInputXCoordinates; const int *mInputYCoordinates; bool mTouchPositionCorrectionEnabled; - uint32_t *mProximityCharsArray; + int32_t *mProximityCharsArray; int *mNormalizedSquaredDistances; int32_t mKeyXCoordinates[MAX_KEY_COUNT_IN_A_KEYBOARD]; int32_t mKeyYCoordinates[MAX_KEY_COUNT_IN_A_KEYBOARD];