blob: e681f6f9734d02878012fcd4b511f94b817abfab [file] [log] [blame]
satok8fbd5522011-02-22 17:28:55 +09001/*
2 * Copyright (C) 2011 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
Ken Wakasaf1008c52012-07-31 17:56:40 +090017#include <cassert>
18#include <cmath>
19#include <cstring>
satok8fbd5522011-02-22 17:28:55 +090020
satok817e5172011-03-04 06:06:45 -080021#define LOG_TAG "LatinIME: proximity_info.cpp"
22
satok552c3c22012-03-13 16:33:47 +090023#include "additional_proximity_chars.h"
Ken Wakasa77e8e812012-08-02 19:48:08 +090024#include "char_utils.h"
Ken Wakasa3b088a22012-05-16 23:05:32 +090025#include "defines.h"
Satoshi Kataoka6b4a1d72012-08-10 15:42:56 +090026#include "geometry_utils.h"
Ken Wakasabb005f72012-08-08 20:43:47 +090027#include "jni.h"
satok8fbd5522011-02-22 17:28:55 +090028#include "proximity_info.h"
29
30namespace latinime {
Ken Wakasace9e52a2011-06-18 13:09:55 +090031
Ken Wakasa162c2112012-08-24 14:51:15 +090032/* static */ const int ProximityInfo::NOT_A_CODE = -1;
33/* static */ const float ProximityInfo::NOT_A_DISTANCE_FLOAT = -1.0f;
34
Ken Wakasabb005f72012-08-08 20:43:47 +090035static inline void safeGetOrFillZeroIntArrayRegion(JNIEnv *env, jintArray jArray, jsize len,
36 jint *buffer) {
37 if (jArray && buffer) {
38 env->GetIntArrayRegion(jArray, 0, len, buffer);
39 } else if (buffer) {
Ken Wakasa063c3e22012-08-10 22:10:04 +090040 memset(buffer, 0, len * sizeof(jint));
Yusuke Nojimade2f8422011-09-27 11:15:18 +090041 }
42}
43
Ken Wakasabb005f72012-08-08 20:43:47 +090044static inline void safeGetOrFillZeroFloatArrayRegion(JNIEnv *env, jfloatArray jArray, jsize len,
45 jfloat *buffer) {
46 if (jArray && buffer) {
47 env->GetFloatArrayRegion(jArray, 0, len, buffer);
48 } else if (buffer) {
Ken Wakasa063c3e22012-08-10 22:10:04 +090049 memset(buffer, 0, len * sizeof(jfloat));
Ken Wakasabb005f72012-08-08 20:43:47 +090050 }
51}
52
Ken Wakasa01511452012-08-09 15:58:15 +090053ProximityInfo::ProximityInfo(JNIEnv *env, const jstring localeJStr, const int maxProximityCharsSize,
satok552c3c22012-03-13 16:33:47 +090054 const int keyboardWidth, const int keyboardHeight, const int gridWidth,
Ken Wakasabb005f72012-08-08 20:43:47 +090055 const int gridHeight, const int mostCommonKeyWidth, const jintArray proximityChars,
56 const int keyCount, const jintArray keyXCoordinates, const jintArray keyYCoordinates,
57 const jintArray keyWidths, const jintArray keyHeights, const jintArray keyCharCodes,
58 const jfloatArray sweetSpotCenterXs, const jfloatArray sweetSpotCenterYs,
59 const jfloatArray sweetSpotRadii)
Ken Wakasa162c2112012-08-24 14:51:15 +090060 : MAX_PROXIMITY_CHARS_SIZE(maxProximityCharsSize), GRID_WIDTH(gridWidth),
61 GRID_HEIGHT(gridHeight), MOST_COMMON_KEY_WIDTH(mostCommonKeyWidth),
satoka70ee6e2012-03-07 15:12:22 +090062 MOST_COMMON_KEY_WIDTH_SQUARE(mostCommonKeyWidth * mostCommonKeyWidth),
satok817e5172011-03-04 06:06:45 -080063 CELL_WIDTH((keyboardWidth + gridWidth - 1) / gridWidth),
Yusuke Nojima0e1f6562011-09-21 12:02:47 +090064 CELL_HEIGHT((keyboardHeight + gridHeight - 1) / gridHeight),
Yusuke Nojima258bfe62011-09-28 12:59:43 +090065 KEY_COUNT(min(keyCount, MAX_KEY_COUNT_IN_A_KEYBOARD)),
Yusuke Nojimaa4c1f1c2011-10-06 19:12:20 +090066 HAS_TOUCH_POSITION_CORRECTION_DATA(keyCount > 0 && keyXCoordinates && keyYCoordinates
67 && keyWidths && keyHeights && keyCharCodes && sweetSpotCenterXs
Ken Wakasa162c2112012-08-24 14:51:15 +090068 && sweetSpotCenterYs && sweetSpotRadii),
69 mProximityCharsArray(new int32_t[GRID_WIDTH * GRID_HEIGHT * MAX_PROXIMITY_CHARS_SIZE
70 /* proximityGridLength */]) {
satok1035bc92012-06-13 16:07:54 -070071 const int proximityGridLength = GRID_WIDTH * GRID_HEIGHT * MAX_PROXIMITY_CHARS_SIZE;
satok817e5172011-03-04 06:06:45 -080072 if (DEBUG_PROXIMITY_INFO) {
satok9fb6f472012-01-13 18:01:22 +090073 AKLOGI("Create proximity info array %d", proximityGridLength);
satok817e5172011-03-04 06:06:45 -080074 }
Ken Wakasa01511452012-08-09 15:58:15 +090075 const jsize localeCStrUtf8Length = env->GetStringUTFLength(localeJStr);
Ken Wakasa9e0c7112012-08-09 22:26:58 +090076 if (localeCStrUtf8Length >= MAX_LOCALE_STRING_LENGTH) {
77 AKLOGI("Locale string length too long: length=%d", localeCStrUtf8Length);
78 assert(false);
79 }
80 memset(mLocaleStr, 0, sizeof(mLocaleStr));
81 env->GetStringUTFRegion(localeJStr, 0, env->GetStringLength(localeJStr), mLocaleStr);
Ken Wakasabb005f72012-08-08 20:43:47 +090082 safeGetOrFillZeroIntArrayRegion(env, proximityChars, proximityGridLength, mProximityCharsArray);
83 safeGetOrFillZeroIntArrayRegion(env, keyXCoordinates, KEY_COUNT, mKeyXCoordinates);
84 safeGetOrFillZeroIntArrayRegion(env, keyYCoordinates, KEY_COUNT, mKeyYCoordinates);
85 safeGetOrFillZeroIntArrayRegion(env, keyWidths, KEY_COUNT, mKeyWidths);
86 safeGetOrFillZeroIntArrayRegion(env, keyHeights, KEY_COUNT, mKeyHeights);
87 safeGetOrFillZeroIntArrayRegion(env, keyCharCodes, KEY_COUNT, mKeyCharCodes);
88 safeGetOrFillZeroFloatArrayRegion(env, sweetSpotCenterXs, KEY_COUNT, mSweetSpotCenterXs);
89 safeGetOrFillZeroFloatArrayRegion(env, sweetSpotCenterYs, KEY_COUNT, mSweetSpotCenterYs);
90 safeGetOrFillZeroFloatArrayRegion(env, sweetSpotRadii, KEY_COUNT, mSweetSpotRadii);
Yusuke Nojima0e1f6562011-09-21 12:02:47 +090091 initializeCodeToKeyIndex();
Satoshi Kataoka6b4a1d72012-08-10 15:42:56 +090092 initializeG();
Yusuke Nojima0e1f6562011-09-21 12:02:47 +090093}
94
Yusuke Nojima0e1f6562011-09-21 12:02:47 +090095// Build the reversed look up table from the char code to the index in mKeyXCoordinates,
96// mKeyYCoordinates, mKeyWidths, mKeyHeights, mKeyCharCodes.
97void ProximityInfo::initializeCodeToKeyIndex() {
Yusuke Nojimaad358352011-09-29 16:44:54 +090098 memset(mCodeToKeyIndex, -1, (MAX_CHAR_CODE + 1) * sizeof(mCodeToKeyIndex[0]));
Yusuke Nojima0e1f6562011-09-21 12:02:47 +090099 for (int i = 0; i < KEY_COUNT; ++i) {
100 const int code = mKeyCharCodes[i];
Yusuke Nojimaad358352011-09-29 16:44:54 +0900101 if (0 <= code && code <= MAX_CHAR_CODE) {
Yusuke Nojima0e1f6562011-09-21 12:02:47 +0900102 mCodeToKeyIndex[code] = i;
Yusuke Nojimaad358352011-09-29 16:44:54 +0900103 }
Yusuke Nojima0e1f6562011-09-21 12:02:47 +0900104 }
satok8fbd5522011-02-22 17:28:55 +0900105}
106
107ProximityInfo::~ProximityInfo() {
108 delete[] mProximityCharsArray;
109}
satok817e5172011-03-04 06:06:45 -0800110
111inline int ProximityInfo::getStartIndexFromCoordinates(const int x, const int y) const {
satok3c4bb772011-03-04 22:50:19 -0800112 return ((y / CELL_HEIGHT) * GRID_WIDTH + (x / CELL_WIDTH))
satok817e5172011-03-04 06:06:45 -0800113 * MAX_PROXIMITY_CHARS_SIZE;
satok8fbd5522011-02-22 17:28:55 +0900114}
satok817e5172011-03-04 06:06:45 -0800115
116bool ProximityInfo::hasSpaceProximity(const int x, const int y) const {
satok744dab62011-12-15 22:29:05 +0900117 if (x < 0 || y < 0) {
118 if (DEBUG_DICT) {
satok9fb6f472012-01-13 18:01:22 +0900119 AKLOGI("HasSpaceProximity: Illegal coordinates (%d, %d)", x, y);
satok1a6da632011-12-16 23:15:06 +0900120 assert(false);
satok744dab62011-12-15 22:29:05 +0900121 }
122 return false;
123 }
124
satok817e5172011-03-04 06:06:45 -0800125 const int startIndex = getStartIndexFromCoordinates(x, y);
126 if (DEBUG_PROXIMITY_INFO) {
satok9fb6f472012-01-13 18:01:22 +0900127 AKLOGI("hasSpaceProximity: index %d, %d, %d", startIndex, x, y);
satok817e5172011-03-04 06:06:45 -0800128 }
Ken Wakasa0bbb9172012-07-25 17:51:43 +0900129 int32_t *proximityCharsArray = mProximityCharsArray;
satok817e5172011-03-04 06:06:45 -0800130 for (int i = 0; i < MAX_PROXIMITY_CHARS_SIZE; ++i) {
131 if (DEBUG_PROXIMITY_INFO) {
satok9fb6f472012-01-13 18:01:22 +0900132 AKLOGI("Index: %d", mProximityCharsArray[startIndex + i]);
satok817e5172011-03-04 06:06:45 -0800133 }
Satoshi Kataoka3e8c58f2012-06-05 17:55:52 +0900134 if (proximityCharsArray[startIndex + i] == KEYCODE_SPACE) {
satok817e5172011-03-04 06:06:45 -0800135 return true;
136 }
137 }
138 return false;
139}
Ken Wakasace9e52a2011-06-18 13:09:55 +0900140
Satoshi Kataokae7398cd2012-08-13 20:20:04 +0900141static inline float getNormalizedSquaredDistanceFloat(float x1, float y1, float x2, float y2,
142 float scale) {
143 return squareFloat((x1 - x2) / scale) + squareFloat((y1 - y2) / scale);
144}
145
146float ProximityInfo::getNormalizedSquaredDistanceFromCenterFloat(
147 const int keyId, const int x, const int y) const {
148 const float centerX = static_cast<float>(getKeyCenterXOfIdG(keyId));
149 const float centerY = static_cast<float>(getKeyCenterYOfIdG(keyId));
150 const float touchX = static_cast<float>(x);
151 const float touchY = static_cast<float>(y);
152 const float keyWidth = static_cast<float>(getMostCommonKeyWidth());
153 return getNormalizedSquaredDistanceFloat(centerX, centerY, touchX, touchY, keyWidth);
154}
155
satok9df4a452012-03-23 16:05:18 +0900156int ProximityInfo::squaredDistanceToEdge(const int keyId, const int x, const int y) const {
Jean Chalard081616c2012-03-22 17:39:27 +0900157 if (keyId < 0) return true; // NOT_A_ID is -1, but return whenever < 0 just in case
satoka70ee6e2012-03-07 15:12:22 +0900158 const int left = mKeyXCoordinates[keyId];
159 const int top = mKeyYCoordinates[keyId];
satok1caff472012-03-14 23:17:12 +0900160 const int right = left + mKeyWidths[keyId];
satoka70ee6e2012-03-07 15:12:22 +0900161 const int bottom = top + mKeyHeights[keyId];
162 const int edgeX = x < left ? left : (x > right ? right : x);
163 const int edgeY = y < top ? top : (y > bottom ? bottom : y);
164 const int dx = x - edgeX;
165 const int dy = y - edgeY;
166 return dx * dx + dy * dy;
167}
168
169void ProximityInfo::calculateNearbyKeyCodes(
satok9df4a452012-03-23 16:05:18 +0900170 const int x, const int y, const int32_t primaryKey, int *inputCodes) const {
Satoshi Kataoka3e8c58f2012-06-05 17:55:52 +0900171 int32_t *proximityCharsArray = mProximityCharsArray;
satoka70ee6e2012-03-07 15:12:22 +0900172 int insertPos = 0;
173 inputCodes[insertPos++] = primaryKey;
174 const int startIndex = getStartIndexFromCoordinates(x, y);
Jean Chalard52612a02012-03-23 19:38:23 +0900175 if (startIndex >= 0) {
Jean Chalard88ec1252012-03-23 19:25:10 +0900176 for (int i = 0; i < MAX_PROXIMITY_CHARS_SIZE; ++i) {
Satoshi Kataoka3e8c58f2012-06-05 17:55:52 +0900177 const int32_t c = proximityCharsArray[startIndex + i];
Jean Chalard88ec1252012-03-23 19:25:10 +0900178 if (c < KEYCODE_SPACE || c == primaryKey) {
179 continue;
180 }
181 const int keyIndex = getKeyIndex(c);
182 const bool onKey = isOnKey(keyIndex, x, y);
183 const int distance = squaredDistanceToEdge(keyIndex, x, y);
184 if (onKey || distance < MOST_COMMON_KEY_WIDTH_SQUARE) {
185 inputCodes[insertPos++] = c;
186 if (insertPos >= MAX_PROXIMITY_CHARS_SIZE) {
187 if (DEBUG_DICT) {
188 assert(false);
189 }
190 return;
satok0cb20972012-03-13 22:07:56 +0900191 }
satoka70ee6e2012-03-07 15:12:22 +0900192 }
193 }
Jean Chalard88ec1252012-03-23 19:25:10 +0900194 const int additionalProximitySize =
Ken Wakasa01511452012-08-09 15:58:15 +0900195 AdditionalProximityChars::getAdditionalCharsSize(mLocaleStr, primaryKey);
Jean Chalard88ec1252012-03-23 19:25:10 +0900196 if (additionalProximitySize > 0) {
197 inputCodes[insertPos++] = ADDITIONAL_PROXIMITY_CHAR_DELIMITER_CODE;
198 if (insertPos >= MAX_PROXIMITY_CHARS_SIZE) {
Jean Chalard3094d122012-03-23 19:36:07 +0900199 if (DEBUG_DICT) {
200 assert(false);
201 }
202 return;
satok1caff472012-03-14 23:17:12 +0900203 }
satok1caff472012-03-14 23:17:12 +0900204
Ken Wakasa0bbb9172012-07-25 17:51:43 +0900205 const int32_t *additionalProximityChars =
Ken Wakasa01511452012-08-09 15:58:15 +0900206 AdditionalProximityChars::getAdditionalChars(mLocaleStr, primaryKey);
Jean Chalard88ec1252012-03-23 19:25:10 +0900207 for (int j = 0; j < additionalProximitySize; ++j) {
208 const int32_t ac = additionalProximityChars[j];
209 int k = 0;
210 for (; k < insertPos; ++k) {
211 if ((int)ac == inputCodes[k]) {
212 break;
213 }
satok5eec5742012-03-13 18:26:23 +0900214 }
Jean Chalard88ec1252012-03-23 19:25:10 +0900215 if (k < insertPos) {
216 continue;
satok0cb20972012-03-13 22:07:56 +0900217 }
Jean Chalard88ec1252012-03-23 19:25:10 +0900218 inputCodes[insertPos++] = ac;
219 if (insertPos >= MAX_PROXIMITY_CHARS_SIZE) {
220 if (DEBUG_DICT) {
221 assert(false);
222 }
223 return;
Jean Chalard3094d122012-03-23 19:36:07 +0900224 }
satok0cb20972012-03-13 22:07:56 +0900225 }
satok5eec5742012-03-13 18:26:23 +0900226 }
Jean Chalard52612a02012-03-23 19:38:23 +0900227 }
satok0cb20972012-03-13 22:07:56 +0900228 // Add a delimiter for the proximity characters
satok1caff472012-03-14 23:17:12 +0900229 for (int i = insertPos; i < MAX_PROXIMITY_CHARS_SIZE; ++i) {
230 inputCodes[i] = NOT_A_CODE;
231 }
satoka70ee6e2012-03-07 15:12:22 +0900232}
233
Yusuke Nojimaa4c1f1c2011-10-06 19:12:20 +0900234int ProximityInfo::getKeyIndex(const int c) const {
satok1caff472012-03-14 23:17:12 +0900235 if (KEY_COUNT == 0) {
Yusuke Nojimaa4c1f1c2011-10-06 19:12:20 +0900236 // We do not have the coordinate data
Jean Chalardbbc25602012-03-23 17:05:03 +0900237 return NOT_AN_INDEX;
Yusuke Nojimaa4c1f1c2011-10-06 19:12:20 +0900238 }
Tadashi G. Takaoka6e3cb272011-11-11 14:26:13 +0900239 const unsigned short baseLowerC = toBaseLowerCase(c);
Yusuke Nojimaa4c1f1c2011-10-06 19:12:20 +0900240 if (baseLowerC > MAX_CHAR_CODE) {
Jean Chalardbbc25602012-03-23 17:05:03 +0900241 return NOT_AN_INDEX;
Yusuke Nojimaa4c1f1c2011-10-06 19:12:20 +0900242 }
243 return mCodeToKeyIndex[baseLowerC];
244}
Satoshi Kataokaefb63242012-06-27 14:52:40 +0900245
Satoshi Kataoka6b4a1d72012-08-10 15:42:56 +0900246int ProximityInfo::getKeyCode(const int keyIndex) const {
247 if (keyIndex < 0 || keyIndex >= KEY_COUNT) {
248 return NOT_AN_INDEX;
249 }
250 return mKeyToCodeIndexG[keyIndex];
251}
252
253void ProximityInfo::initializeG() {
Satoshi Kataokae7398cd2012-08-13 20:20:04 +0900254 // TODO: Optimize
Satoshi Kataoka6b4a1d72012-08-10 15:42:56 +0900255 for (int i = 0; i < KEY_COUNT; ++i) {
256 const int code = mKeyCharCodes[i];
257 const int lowerCode = toBaseLowerCase(code);
258 mCenterXsG[i] = mKeyXCoordinates[i] + mKeyWidths[i] / 2;
259 mCenterYsG[i] = mKeyYCoordinates[i] + mKeyHeights[i] / 2;
260 if (code != lowerCode && lowerCode >= 0 && lowerCode <= MAX_CHAR_CODE) {
261 mCodeToKeyIndex[lowerCode] = i;
262 mKeyToCodeIndexG[i] = lowerCode;
263 } else {
264 mKeyToCodeIndexG[i] = code;
265 }
266 }
267 for (int i = 0; i < KEY_COUNT; i++) {
268 mKeyKeyDistancesG[i][i] = 0;
269 for (int j = i + 1; j < KEY_COUNT; j++) {
Ken Wakasabcec82d2012-08-12 11:10:48 +0900270 mKeyKeyDistancesG[i][j] = getDistanceInt(
Satoshi Kataoka6b4a1d72012-08-10 15:42:56 +0900271 mCenterXsG[i], mCenterYsG[i], mCenterXsG[j], mCenterYsG[j]);
272 mKeyKeyDistancesG[j][i] = mKeyKeyDistancesG[i][j];
273 }
274 }
275}
276
277float ProximityInfo::getKeyCenterXOfCharG(int charCode) const {
278 return getKeyCenterXOfIdG(getKeyIndex(charCode));
279}
280
281float ProximityInfo::getKeyCenterYOfCharG(int charCode) const {
282 return getKeyCenterYOfIdG(getKeyIndex(charCode));
283}
284
285float ProximityInfo::getKeyCenterXOfIdG(int keyId) const {
286 if (keyId >= 0) {
287 return mCenterXsG[keyId];
288 }
289 return 0;
290}
291
292float ProximityInfo::getKeyCenterYOfIdG(int keyId) const {
293 if (keyId >= 0) {
294 return mCenterYsG[keyId];
295 }
296 return 0;
297}
298
299int ProximityInfo::getKeyKeyDistanceG(int key0, int key1) const {
300 const int keyId0 = getKeyIndex(key0);
301 const int keyId1 = getKeyIndex(key1);
302 if (keyId0 >= 0 && keyId1 >= 0) {
303 return mKeyKeyDistancesG[keyId0][keyId1];
304 }
305 return 0;
306}
Ken Wakasace9e52a2011-06-18 13:09:55 +0900307} // namespace latinime