blob: 2f5f81ccba8a28374aefc06a1ef6e7e3ac1f5b14 [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
Yusuke Nojima0e1f6562011-09-21 12:02:47 +090017#include <assert.h>
satok8fbd5522011-02-22 17:28:55 +090018#include <stdio.h>
satok552c3c22012-03-13 16:33:47 +090019#include <string>
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"
satokd24df432011-07-14 15:43:42 +090024#include "dictionary.h"
satok8fbd5522011-02-22 17:28:55 +090025#include "proximity_info.h"
26
27namespace latinime {
Ken Wakasace9e52a2011-06-18 13:09:55 +090028
Yusuke Nojimade2f8422011-09-27 11:15:18 +090029inline void copyOrFillZero(void *to, const void *from, size_t size) {
30 if (from) {
31 memcpy(to, from, size);
32 } else {
33 memset(to, 0, size);
34 }
35}
36
satok552c3c22012-03-13 16:33:47 +090037ProximityInfo::ProximityInfo(const std::string localeStr, const int maxProximityCharsSize,
38 const int keyboardWidth, const int keyboardHeight, const int gridWidth,
39 const int gridHeight, const int mostCommonKeyWidth,
satok0cb20972012-03-13 22:07:56 +090040 const int32_t *proximityCharsArray, const int keyCount, const int32_t *keyXCoordinates,
Yusuke Nojima0e1f6562011-09-21 12:02:47 +090041 const int32_t *keyYCoordinates, const int32_t *keyWidths, const int32_t *keyHeights,
Yusuke Nojimaad358352011-09-29 16:44:54 +090042 const int32_t *keyCharCodes, const float *sweetSpotCenterXs, const float *sweetSpotCenterYs,
43 const float *sweetSpotRadii)
satok817e5172011-03-04 06:06:45 -080044 : MAX_PROXIMITY_CHARS_SIZE(maxProximityCharsSize), KEYBOARD_WIDTH(keyboardWidth),
45 KEYBOARD_HEIGHT(keyboardHeight), GRID_WIDTH(gridWidth), GRID_HEIGHT(gridHeight),
satoka70ee6e2012-03-07 15:12:22 +090046 MOST_COMMON_KEY_WIDTH_SQUARE(mostCommonKeyWidth * mostCommonKeyWidth),
satok817e5172011-03-04 06:06:45 -080047 CELL_WIDTH((keyboardWidth + gridWidth - 1) / gridWidth),
Yusuke Nojima0e1f6562011-09-21 12:02:47 +090048 CELL_HEIGHT((keyboardHeight + gridHeight - 1) / gridHeight),
Yusuke Nojima258bfe62011-09-28 12:59:43 +090049 KEY_COUNT(min(keyCount, MAX_KEY_COUNT_IN_A_KEYBOARD)),
Yusuke Nojimaa4c1f1c2011-10-06 19:12:20 +090050 HAS_TOUCH_POSITION_CORRECTION_DATA(keyCount > 0 && keyXCoordinates && keyYCoordinates
51 && keyWidths && keyHeights && keyCharCodes && sweetSpotCenterXs
52 && sweetSpotCenterYs && sweetSpotRadii),
satok5eec5742012-03-13 18:26:23 +090053 mLocaleStr(localeStr),
Tadashi G. Takaoka0e971482011-10-28 17:02:09 +090054 mInputXCoordinates(0), mInputYCoordinates(0),
Yusuke Nojimaa4c1f1c2011-10-06 19:12:20 +090055 mTouchPositionCorrectionEnabled(false) {
Jean Chalard8c8ca592011-11-10 12:07:30 +090056 const int proximityGridLength = GRID_WIDTH * GRID_HEIGHT * MAX_PROXIMITY_CHARS_SIZE;
satok0cb20972012-03-13 22:07:56 +090057 mProximityCharsArray = new int32_t[proximityGridLength];
satok1caff472012-03-14 23:17:12 +090058 mInputCodes = new int32_t[MAX_PROXIMITY_CHARS_SIZE * MAX_WORD_LENGTH_INTERNAL];
satok817e5172011-03-04 06:06:45 -080059 if (DEBUG_PROXIMITY_INFO) {
satok9fb6f472012-01-13 18:01:22 +090060 AKLOGI("Create proximity info array %d", proximityGridLength);
satok817e5172011-03-04 06:06:45 -080061 }
Jean Chalard8c8ca592011-11-10 12:07:30 +090062 memcpy(mProximityCharsArray, proximityCharsArray,
63 proximityGridLength * sizeof(mProximityCharsArray[0]));
64 const int normalizedSquaredDistancesLength =
65 MAX_PROXIMITY_CHARS_SIZE * MAX_WORD_LENGTH_INTERNAL;
66 mNormalizedSquaredDistances = new int[normalizedSquaredDistancesLength];
67 for (int i = 0; i < normalizedSquaredDistancesLength; ++i) {
Yusuke Nojimaa4c1f1c2011-10-06 19:12:20 +090068 mNormalizedSquaredDistances[i] = NOT_A_DISTANCE;
69 }
Yusuke Nojima0e1f6562011-09-21 12:02:47 +090070
Yusuke Nojimade2f8422011-09-27 11:15:18 +090071 copyOrFillZero(mKeyXCoordinates, keyXCoordinates, KEY_COUNT * sizeof(mKeyXCoordinates[0]));
72 copyOrFillZero(mKeyYCoordinates, keyYCoordinates, KEY_COUNT * sizeof(mKeyYCoordinates[0]));
73 copyOrFillZero(mKeyWidths, keyWidths, KEY_COUNT * sizeof(mKeyWidths[0]));
74 copyOrFillZero(mKeyHeights, keyHeights, KEY_COUNT * sizeof(mKeyHeights[0]));
75 copyOrFillZero(mKeyCharCodes, keyCharCodes, KEY_COUNT * sizeof(mKeyCharCodes[0]));
Yusuke Nojimaad358352011-09-29 16:44:54 +090076 copyOrFillZero(mSweetSpotCenterXs, sweetSpotCenterXs,
77 KEY_COUNT * sizeof(mSweetSpotCenterXs[0]));
78 copyOrFillZero(mSweetSpotCenterYs, sweetSpotCenterYs,
79 KEY_COUNT * sizeof(mSweetSpotCenterYs[0]));
80 copyOrFillZero(mSweetSpotRadii, sweetSpotRadii, KEY_COUNT * sizeof(mSweetSpotRadii[0]));
Yusuke Nojima0e1f6562011-09-21 12:02:47 +090081
Yusuke Nojima0e1f6562011-09-21 12:02:47 +090082 initializeCodeToKeyIndex();
83}
84
Yusuke Nojima0e1f6562011-09-21 12:02:47 +090085// Build the reversed look up table from the char code to the index in mKeyXCoordinates,
86// mKeyYCoordinates, mKeyWidths, mKeyHeights, mKeyCharCodes.
87void ProximityInfo::initializeCodeToKeyIndex() {
Yusuke Nojimaad358352011-09-29 16:44:54 +090088 memset(mCodeToKeyIndex, -1, (MAX_CHAR_CODE + 1) * sizeof(mCodeToKeyIndex[0]));
Yusuke Nojima0e1f6562011-09-21 12:02:47 +090089 for (int i = 0; i < KEY_COUNT; ++i) {
90 const int code = mKeyCharCodes[i];
Yusuke Nojimaad358352011-09-29 16:44:54 +090091 if (0 <= code && code <= MAX_CHAR_CODE) {
Yusuke Nojima0e1f6562011-09-21 12:02:47 +090092 mCodeToKeyIndex[code] = i;
Yusuke Nojimaad358352011-09-29 16:44:54 +090093 }
Yusuke Nojima0e1f6562011-09-21 12:02:47 +090094 }
satok8fbd5522011-02-22 17:28:55 +090095}
96
97ProximityInfo::~ProximityInfo() {
Yusuke Nojimaa4c1f1c2011-10-06 19:12:20 +090098 delete[] mNormalizedSquaredDistances;
satok8fbd5522011-02-22 17:28:55 +090099 delete[] mProximityCharsArray;
satok1caff472012-03-14 23:17:12 +0900100 delete[] mInputCodes;
satok8fbd5522011-02-22 17:28:55 +0900101}
satok817e5172011-03-04 06:06:45 -0800102
103inline int ProximityInfo::getStartIndexFromCoordinates(const int x, const int y) const {
satok3c4bb772011-03-04 22:50:19 -0800104 return ((y / CELL_HEIGHT) * GRID_WIDTH + (x / CELL_WIDTH))
satok817e5172011-03-04 06:06:45 -0800105 * MAX_PROXIMITY_CHARS_SIZE;
satok8fbd5522011-02-22 17:28:55 +0900106}
satok817e5172011-03-04 06:06:45 -0800107
108bool ProximityInfo::hasSpaceProximity(const int x, const int y) const {
satok744dab62011-12-15 22:29:05 +0900109 if (x < 0 || y < 0) {
110 if (DEBUG_DICT) {
satok9fb6f472012-01-13 18:01:22 +0900111 AKLOGI("HasSpaceProximity: Illegal coordinates (%d, %d)", x, y);
satok1a6da632011-12-16 23:15:06 +0900112 assert(false);
satok744dab62011-12-15 22:29:05 +0900113 }
114 return false;
115 }
116
satok817e5172011-03-04 06:06:45 -0800117 const int startIndex = getStartIndexFromCoordinates(x, y);
118 if (DEBUG_PROXIMITY_INFO) {
satok9fb6f472012-01-13 18:01:22 +0900119 AKLOGI("hasSpaceProximity: index %d, %d, %d", startIndex, x, y);
satok817e5172011-03-04 06:06:45 -0800120 }
121 for (int i = 0; i < MAX_PROXIMITY_CHARS_SIZE; ++i) {
122 if (DEBUG_PROXIMITY_INFO) {
satok9fb6f472012-01-13 18:01:22 +0900123 AKLOGI("Index: %d", mProximityCharsArray[startIndex + i]);
satok817e5172011-03-04 06:06:45 -0800124 }
125 if (mProximityCharsArray[startIndex + i] == KEYCODE_SPACE) {
126 return true;
127 }
128 }
129 return false;
130}
Ken Wakasace9e52a2011-06-18 13:09:55 +0900131
satoka70ee6e2012-03-07 15:12:22 +0900132bool ProximityInfo::isOnKey(const int keyId, const int x, const int y) {
Jean Chalard081616c2012-03-22 17:39:27 +0900133 if (keyId < 0) return true; // NOT_A_ID is -1, but return whenever < 0 just in case
satoka70ee6e2012-03-07 15:12:22 +0900134 const int left = mKeyXCoordinates[keyId];
135 const int top = mKeyYCoordinates[keyId];
136 const int right = left + mKeyWidths[keyId] + 1;
137 const int bottom = top + mKeyHeights[keyId];
138 return left < right && top < bottom && x >= left && x < right && y >= top && y < bottom;
139}
140
141int ProximityInfo::squaredDistanceToEdge(const int keyId, const int x, const int y) {
Jean Chalard081616c2012-03-22 17:39:27 +0900142 if (keyId < 0) return true; // NOT_A_ID is -1, but return whenever < 0 just in case
satoka70ee6e2012-03-07 15:12:22 +0900143 const int left = mKeyXCoordinates[keyId];
144 const int top = mKeyYCoordinates[keyId];
satok1caff472012-03-14 23:17:12 +0900145 const int right = left + mKeyWidths[keyId];
satoka70ee6e2012-03-07 15:12:22 +0900146 const int bottom = top + mKeyHeights[keyId];
147 const int edgeX = x < left ? left : (x > right ? right : x);
148 const int edgeY = y < top ? top : (y > bottom ? bottom : y);
149 const int dx = x - edgeX;
150 const int dy = y - edgeY;
151 return dx * dx + dy * dy;
152}
153
154void ProximityInfo::calculateNearbyKeyCodes(
satok0cb20972012-03-13 22:07:56 +0900155 const int x, const int y, const int32_t primaryKey, int *inputCodes) {
satoka70ee6e2012-03-07 15:12:22 +0900156 int insertPos = 0;
157 inputCodes[insertPos++] = primaryKey;
158 const int startIndex = getStartIndexFromCoordinates(x, y);
Jean Chalard88ec1252012-03-23 19:25:10 +0900159
160 for (int i = 0; i < MAX_PROXIMITY_CHARS_SIZE; ++i) {
161 const int32_t c = mProximityCharsArray[startIndex + i];
162 if (c < KEYCODE_SPACE || c == primaryKey) {
163 continue;
164 }
165 const int keyIndex = getKeyIndex(c);
166 const bool onKey = isOnKey(keyIndex, x, y);
167 const int distance = squaredDistanceToEdge(keyIndex, x, y);
168 if (onKey || distance < MOST_COMMON_KEY_WIDTH_SQUARE) {
169 inputCodes[insertPos++] = c;
170 if (insertPos >= MAX_PROXIMITY_CHARS_SIZE) {
171 if (DEBUG_DICT) {
172 assert(false);
173 }
174 return;
satok0cb20972012-03-13 22:07:56 +0900175 }
satoka70ee6e2012-03-07 15:12:22 +0900176 }
177 }
Jean Chalard88ec1252012-03-23 19:25:10 +0900178 const int additionalProximitySize =
179 AdditionalProximityChars::getAdditionalCharsSize(&mLocaleStr, primaryKey);
180 if (additionalProximitySize > 0) {
181 inputCodes[insertPos++] = ADDITIONAL_PROXIMITY_CHAR_DELIMITER_CODE;
182 if (insertPos >= MAX_PROXIMITY_CHARS_SIZE) {
satok1caff472012-03-14 23:17:12 +0900183 if (DEBUG_DICT) {
184 assert(false);
185 }
186 return;
187 }
188
Jean Chalard88ec1252012-03-23 19:25:10 +0900189 const int32_t* additionalProximityChars =
190 AdditionalProximityChars::getAdditionalChars(&mLocaleStr, primaryKey);
191 for (int j = 0; j < additionalProximitySize; ++j) {
192 const int32_t ac = additionalProximityChars[j];
193 int k = 0;
194 for (; k < insertPos; ++k) {
195 if ((int)ac == inputCodes[k]) {
196 break;
197 }
satok5eec5742012-03-13 18:26:23 +0900198 }
Jean Chalard88ec1252012-03-23 19:25:10 +0900199 if (k < insertPos) {
200 continue;
satok0cb20972012-03-13 22:07:56 +0900201 }
Jean Chalard88ec1252012-03-23 19:25:10 +0900202 inputCodes[insertPos++] = ac;
203 if (insertPos >= MAX_PROXIMITY_CHARS_SIZE) {
204 if (DEBUG_DICT) {
205 assert(false);
206 }
207 return;
satok0cb20972012-03-13 22:07:56 +0900208 }
satok5eec5742012-03-13 18:26:23 +0900209 }
210 }
Jean Chalard88ec1252012-03-23 19:25:10 +0900211
satok0cb20972012-03-13 22:07:56 +0900212 // Add a delimiter for the proximity characters
satok1caff472012-03-14 23:17:12 +0900213 for (int i = insertPos; i < MAX_PROXIMITY_CHARS_SIZE; ++i) {
214 inputCodes[i] = NOT_A_CODE;
215 }
satoka70ee6e2012-03-07 15:12:22 +0900216}
217
satok1caff472012-03-14 23:17:12 +0900218void ProximityInfo::setInputParams(const int32_t* inputCodes, const int inputLength,
Yusuke Nojima258bfe62011-09-28 12:59:43 +0900219 const int* xCoordinates, const int* yCoordinates) {
satok1caff472012-03-14 23:17:12 +0900220 memset(mInputCodes, 0,
221 MAX_WORD_LENGTH_INTERNAL * MAX_PROXIMITY_CHARS_SIZE * sizeof(mInputCodes[0]));
222
223 for (int i = 0; i < inputLength; ++i) {
224 const int32_t primaryKey = inputCodes[i * MAX_PROXIMITY_CHARS_SIZE];
225 const int x = xCoordinates[i];
226 const int y = yCoordinates[i];
227 int *proximities = &mInputCodes[i * MAX_PROXIMITY_CHARS_SIZE];
228 calculateNearbyKeyCodes(x, y, primaryKey, proximities);
229 }
230
231 if (DEBUG_PROXIMITY_CHARS) {
232 for (int i = 0; i < inputLength; ++i) {
233 AKLOGI("---");
234 for (int j = 0; j < MAX_PROXIMITY_CHARS_SIZE; ++j) {
235 int icc = mInputCodes[i * MAX_PROXIMITY_CHARS_SIZE + j];
236 int icfjc = inputCodes[i * MAX_PROXIMITY_CHARS_SIZE + j];
237 icc+= 0;
238 icfjc += 0;
239 AKLOGI("--- (%d)%c,%c", i, icc, icfjc);
240 AKLOGI("--- A<%d>,B<%d>", icc, icfjc);
241 }
242 }
243 }
244 //Keep for debug, sorry
245 //for (int i = 0; i < MAX_WORD_LENGTH_INTERNAL * MAX_PROXIMITY_CHARS_SIZE; ++i) {
246 //if (i < inputLength * MAX_PROXIMITY_CHARS_SIZE) {
247 //mInputCodes[i] = mInputCodesFromJava[i];
248 //} else {
249 // mInputCodes[i] = 0;
250 // }
251 //}
Yusuke Nojima258bfe62011-09-28 12:59:43 +0900252 mInputXCoordinates = xCoordinates;
253 mInputYCoordinates = yCoordinates;
Yusuke Nojimaa4c1f1c2011-10-06 19:12:20 +0900254 mTouchPositionCorrectionEnabled =
255 HAS_TOUCH_POSITION_CORRECTION_DATA && xCoordinates && yCoordinates;
satok1d7eaf82011-07-13 10:32:02 +0900256 mInputLength = inputLength;
satok0cedd2b2011-08-12 01:05:27 +0900257 for (int i = 0; i < inputLength; ++i) {
258 mPrimaryInputWord[i] = getPrimaryCharAt(i);
259 }
260 mPrimaryInputWord[inputLength] = 0;
satok0cb20972012-03-13 22:07:56 +0900261 if (DEBUG_PROXIMITY_CHARS) {
262 AKLOGI("--- setInputParams");
263 }
Yusuke Nojimac25c7cc2011-10-03 16:44:29 +0900264 for (int i = 0; i < mInputLength; ++i) {
Yusuke Nojimaa4c1f1c2011-10-06 19:12:20 +0900265 const int *proximityChars = getProximityCharsAt(i);
satok0cb20972012-03-13 22:07:56 +0900266 const int primaryKey = proximityChars[0];
267 const int x = xCoordinates[i];
268 const int y = yCoordinates[i];
269 if (DEBUG_PROXIMITY_CHARS) {
270 int a = x + y + primaryKey;
271 a += 0;
272 AKLOGI("--- Primary = %c, x = %d, y = %d", primaryKey, x, y);
273 // Keep debug code just in case
274 //int proximities[50];
275 //for (int m = 0; m < 50; ++m) {
276 //proximities[m] = 0;
277 //}
278 //calculateNearbyKeyCodes(x, y, primaryKey, proximities);
279 //for (int l = 0; l < 50 && proximities[l] > 0; ++l) {
280 //if (DEBUG_PROXIMITY_CHARS) {
281 //AKLOGI("--- native Proximity (%d) = %c", l, proximities[l]);
282 //}
283 //}
284 }
Yusuke Nojimaa4c1f1c2011-10-06 19:12:20 +0900285 for (int j = 0; j < MAX_PROXIMITY_CHARS_SIZE && proximityChars[j] > 0; ++j) {
286 const int currentChar = proximityChars[j];
satok1caff472012-03-14 23:17:12 +0900287 const float squaredDistance = hasInputCoordinates()
288 ? calculateNormalizedSquaredDistance(getKeyIndex(currentChar), i)
289 : NOT_A_DISTANCE_FLOAT;
Yusuke Nojimaa4c1f1c2011-10-06 19:12:20 +0900290 if (squaredDistance >= 0.0f) {
291 mNormalizedSquaredDistances[i * MAX_PROXIMITY_CHARS_SIZE + j] =
292 (int)(squaredDistance * NORMALIZED_SQUARED_DISTANCE_SCALING_FACTOR);
293 } else {
294 mNormalizedSquaredDistances[i * MAX_PROXIMITY_CHARS_SIZE + j] = (j == 0)
295 ? EQUIVALENT_CHAR_WITHOUT_DISTANCE_INFO
296 : PROXIMITY_CHAR_WITHOUT_DISTANCE_INFO;
297 }
satok0cb20972012-03-13 22:07:56 +0900298 if (DEBUG_PROXIMITY_CHARS) {
299 AKLOGI("--- Proximity (%d) = %c", j, currentChar);
300 }
Yusuke Nojimae4ba8222011-10-05 14:55:07 +0900301 }
Yusuke Nojimac25c7cc2011-10-03 16:44:29 +0900302 }
satok1d7eaf82011-07-13 10:32:02 +0900303}
304
Yusuke Nojima16717152011-10-04 17:04:07 +0900305inline float square(const float x) { return x * x; }
306
Yusuke Nojimaa4c1f1c2011-10-06 19:12:20 +0900307float ProximityInfo::calculateNormalizedSquaredDistance(
308 const int keyIndex, const int inputIndex) const {
Jean Chalardbbc25602012-03-23 17:05:03 +0900309 if (keyIndex == NOT_AN_INDEX) {
Yusuke Nojimae4ba8222011-10-05 14:55:07 +0900310 return NOT_A_DISTANCE_FLOAT;
Yusuke Nojimac25c7cc2011-10-03 16:44:29 +0900311 }
Yusuke Nojimaa4c1f1c2011-10-06 19:12:20 +0900312 if (!hasSweetSpotData(keyIndex)) {
Yusuke Nojimae4ba8222011-10-05 14:55:07 +0900313 return NOT_A_DISTANCE_FLOAT;
Yusuke Nojima16717152011-10-04 17:04:07 +0900314 }
Jean Chalard0bfe3592012-01-25 18:11:26 +0900315 if (NOT_A_COORDINATE == mInputXCoordinates[inputIndex]) {
316 return NOT_A_DISTANCE_FLOAT;
317 }
Yusuke Nojimaa4c1f1c2011-10-06 19:12:20 +0900318 const float squaredDistance = calculateSquaredDistanceFromSweetSpotCenter(keyIndex, inputIndex);
319 const float squaredRadius = square(mSweetSpotRadii[keyIndex]);
Yusuke Nojimae4ba8222011-10-05 14:55:07 +0900320 return squaredDistance / squaredRadius;
Yusuke Nojima16717152011-10-04 17:04:07 +0900321}
322
satok1caff472012-03-14 23:17:12 +0900323bool ProximityInfo::hasInputCoordinates() const {
324 return mInputXCoordinates && mInputYCoordinates;
325}
326
Yusuke Nojimaa4c1f1c2011-10-06 19:12:20 +0900327int ProximityInfo::getKeyIndex(const int c) const {
satok1caff472012-03-14 23:17:12 +0900328 if (KEY_COUNT == 0) {
Yusuke Nojimaa4c1f1c2011-10-06 19:12:20 +0900329 // We do not have the coordinate data
Jean Chalardbbc25602012-03-23 17:05:03 +0900330 return NOT_AN_INDEX;
Yusuke Nojimaa4c1f1c2011-10-06 19:12:20 +0900331 }
Tadashi G. Takaoka6e3cb272011-11-11 14:26:13 +0900332 const unsigned short baseLowerC = toBaseLowerCase(c);
Yusuke Nojimaa4c1f1c2011-10-06 19:12:20 +0900333 if (baseLowerC > MAX_CHAR_CODE) {
Jean Chalardbbc25602012-03-23 17:05:03 +0900334 return NOT_AN_INDEX;
Yusuke Nojimaa4c1f1c2011-10-06 19:12:20 +0900335 }
336 return mCodeToKeyIndex[baseLowerC];
337}
338
Yusuke Nojimac25c7cc2011-10-03 16:44:29 +0900339float ProximityInfo::calculateSquaredDistanceFromSweetSpotCenter(
Yusuke Nojimaa4c1f1c2011-10-06 19:12:20 +0900340 const int keyIndex, const int inputIndex) const {
Yusuke Nojimac25c7cc2011-10-03 16:44:29 +0900341 const float sweetSpotCenterX = mSweetSpotCenterXs[keyIndex];
342 const float sweetSpotCenterY = mSweetSpotCenterYs[keyIndex];
343 const float inputX = (float)mInputXCoordinates[inputIndex];
344 const float inputY = (float)mInputYCoordinates[inputIndex];
345 return square(inputX - sweetSpotCenterX) + square(inputY - sweetSpotCenterY);
346}
347
satokd24df432011-07-14 15:43:42 +0900348inline const int* ProximityInfo::getProximityCharsAt(const int index) const {
satok1d7eaf82011-07-13 10:32:02 +0900349 return mInputCodes + (index * MAX_PROXIMITY_CHARS_SIZE);
350}
351
satokd24df432011-07-14 15:43:42 +0900352unsigned short ProximityInfo::getPrimaryCharAt(const int index) const {
353 return getProximityCharsAt(index)[0];
354}
355
satok2df30602011-07-15 13:49:00 +0900356inline bool ProximityInfo::existsCharInProximityAt(const int index, const int c) const {
satokd24df432011-07-14 15:43:42 +0900357 const int *chars = getProximityCharsAt(index);
358 int i = 0;
359 while (chars[i] > 0 && i < MAX_PROXIMITY_CHARS_SIZE) {
360 if (chars[i++] == c) {
361 return true;
362 }
363 }
364 return false;
365}
366
367bool ProximityInfo::existsAdjacentProximityChars(const int index) const {
368 if (index < 0 || index >= mInputLength) return false;
369 const int currentChar = getPrimaryCharAt(index);
370 const int leftIndex = index - 1;
371 if (leftIndex >= 0 && existsCharInProximityAt(leftIndex, currentChar)) {
372 return true;
373 }
374 const int rightIndex = index + 1;
375 if (rightIndex < mInputLength && existsCharInProximityAt(rightIndex, currentChar)) {
376 return true;
377 }
378 return false;
379}
380
381// In the following function, c is the current character of the dictionary word
382// currently examined.
383// currentChars is an array containing the keys close to the character the
384// user actually typed at the same position. We want to see if c is in it: if so,
385// then the word contains at that position a character close to what the user
386// typed.
387// What the user typed is actually the first character of the array.
Yusuke Nojimaa4c1f1c2011-10-06 19:12:20 +0900388// proximityIndex is a pointer to the variable where getMatchedProximityId returns
389// the index of c in the proximity chars of the input index.
satokd24df432011-07-14 15:43:42 +0900390// Notice : accented characters do not have a proximity list, so they are alone
391// in their list. The non-accented version of the character should be considered
392// "close", but not the other keys close to the non-accented version.
Yusuke Nojimaa4c1f1c2011-10-06 19:12:20 +0900393ProximityInfo::ProximityType ProximityInfo::getMatchedProximityId(const int index,
394 const unsigned short c, const bool checkProximityChars, int *proximityIndex) const {
satokd24df432011-07-14 15:43:42 +0900395 const int *currentChars = getProximityCharsAt(index);
Yusuke Nojima258bfe62011-09-28 12:59:43 +0900396 const int firstChar = currentChars[0];
Tadashi G. Takaoka6e3cb272011-11-11 14:26:13 +0900397 const unsigned short baseLowerC = toBaseLowerCase(c);
satokd24df432011-07-14 15:43:42 +0900398
399 // The first char in the array is what user typed. If it matches right away,
400 // that means the user typed that same char for this pos.
Yusuke Nojima258bfe62011-09-28 12:59:43 +0900401 if (firstChar == baseLowerC || firstChar == c) {
Yusuke Nojimae4ba8222011-10-05 14:55:07 +0900402 return EQUIVALENT_CHAR;
Yusuke Nojima258bfe62011-09-28 12:59:43 +0900403 }
satokd24df432011-07-14 15:43:42 +0900404
satok985312e2011-08-05 21:21:01 +0900405 if (!checkProximityChars) return UNRELATED_CHAR;
satokd24df432011-07-14 15:43:42 +0900406
407 // If the non-accented, lowercased version of that first character matches c,
408 // then we have a non-accented version of the accented character the user
409 // typed. Treat it as a close char.
Tadashi G. Takaoka6e3cb272011-11-11 14:26:13 +0900410 if (toBaseLowerCase(firstChar) == baseLowerC)
satokd24df432011-07-14 15:43:42 +0900411 return NEAR_PROXIMITY_CHAR;
412
413 // Not an exact nor an accent-alike match: search the list of close keys
414 int j = 1;
satoke05b3f42012-01-31 17:15:43 +0900415 while (j < MAX_PROXIMITY_CHARS_SIZE
416 && currentChars[j] > ADDITIONAL_PROXIMITY_CHAR_DELIMITER_CODE) {
satokd24df432011-07-14 15:43:42 +0900417 const bool matched = (currentChars[j] == baseLowerC || currentChars[j] == c);
Yusuke Nojimaa4c1f1c2011-10-06 19:12:20 +0900418 if (matched) {
419 if (proximityIndex) {
420 *proximityIndex = j;
421 }
422 return NEAR_PROXIMITY_CHAR;
423 }
satokd24df432011-07-14 15:43:42 +0900424 ++j;
425 }
satoke05b3f42012-01-31 17:15:43 +0900426 if (j < MAX_PROXIMITY_CHARS_SIZE
427 && currentChars[j] == ADDITIONAL_PROXIMITY_CHAR_DELIMITER_CODE) {
428 ++j;
429 while (j < MAX_PROXIMITY_CHARS_SIZE
430 && currentChars[j] > ADDITIONAL_PROXIMITY_CHAR_DELIMITER_CODE) {
431 const bool matched = (currentChars[j] == baseLowerC || currentChars[j] == c);
432 if (matched) {
433 if (proximityIndex) {
434 *proximityIndex = j;
435 }
436 return ADDITIONAL_PROXIMITY_CHAR;
437 }
438 ++j;
439 }
440 }
satokd24df432011-07-14 15:43:42 +0900441
442 // Was not included, signal this as an unrelated character.
443 return UNRELATED_CHAR;
444}
445
satok1d7eaf82011-07-13 10:32:02 +0900446bool ProximityInfo::sameAsTyped(const unsigned short *word, int length) const {
447 if (length != mInputLength) {
448 return false;
449 }
450 const int *inputCodes = mInputCodes;
451 while (length--) {
452 if ((unsigned int) *inputCodes != (unsigned int) *word) {
453 return false;
454 }
455 inputCodes += MAX_PROXIMITY_CHARS_SIZE;
456 word++;
457 }
458 return true;
459}
460
Yusuke Nojimae4ba8222011-10-05 14:55:07 +0900461const int ProximityInfo::NORMALIZED_SQUARED_DISTANCE_SCALING_FACTOR_LOG_2;
462const int ProximityInfo::NORMALIZED_SQUARED_DISTANCE_SCALING_FACTOR;
Yusuke Nojima0e1f6562011-09-21 12:02:47 +0900463const int ProximityInfo::MAX_KEY_COUNT_IN_A_KEYBOARD;
Yusuke Nojimaad358352011-09-29 16:44:54 +0900464const int ProximityInfo::MAX_CHAR_CODE;
Yusuke Nojima0e1f6562011-09-21 12:02:47 +0900465
Ken Wakasace9e52a2011-06-18 13:09:55 +0900466} // namespace latinime