blob: 2bfb15d8cc9281a9e39266ca09423d40cebed8f0 [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);
159 for (int i = 0; i < MAX_PROXIMITY_CHARS_SIZE; ++i) {
satok0cb20972012-03-13 22:07:56 +0900160 const int32_t c = mProximityCharsArray[startIndex + i];
satoka70ee6e2012-03-07 15:12:22 +0900161 if (c < KEYCODE_SPACE || c == primaryKey) {
162 continue;
163 }
satok1caff472012-03-14 23:17:12 +0900164 const int keyIndex = getKeyIndex(c);
satok0cb20972012-03-13 22:07:56 +0900165 const bool onKey = isOnKey(keyIndex, x, y);
166 const int distance = squaredDistanceToEdge(keyIndex, x, y);
Jean Chalard081616c2012-03-22 17:39:27 +0900167 if (onKey || distance < MOST_COMMON_KEY_WIDTH_SQUARE) {
satok0cb20972012-03-13 22:07:56 +0900168 inputCodes[insertPos++] = c;
169 if (insertPos >= MAX_PROXIMITY_CHARS_SIZE) {
170 if (DEBUG_DICT) {
171 assert(false);
172 }
173 return;
satoka70ee6e2012-03-07 15:12:22 +0900174 }
175 }
176 }
satok0cb20972012-03-13 22:07:56 +0900177 const int additionalProximitySize =
178 AdditionalProximityChars::getAdditionalCharsSize(&mLocaleStr, primaryKey);
179 if (additionalProximitySize > 0) {
satok1caff472012-03-14 23:17:12 +0900180 inputCodes[insertPos++] = ADDITIONAL_PROXIMITY_CHAR_DELIMITER_CODE;
181 if (insertPos >= MAX_PROXIMITY_CHARS_SIZE) {
182 if (DEBUG_DICT) {
183 assert(false);
184 }
185 return;
186 }
187
satok0cb20972012-03-13 22:07:56 +0900188 const int32_t* additionalProximityChars =
189 AdditionalProximityChars::getAdditionalChars(&mLocaleStr, primaryKey);
satok5eec5742012-03-13 18:26:23 +0900190 for (int j = 0; j < additionalProximitySize; ++j) {
satok0cb20972012-03-13 22:07:56 +0900191 const int32_t ac = additionalProximityChars[j];
satok5eec5742012-03-13 18:26:23 +0900192 int k = 0;
193 for (; k < insertPos; ++k) {
194 if ((int)ac == inputCodes[k]) {
195 break;
196 }
197 }
198 if (k < insertPos) {
199 continue;
200 }
201 inputCodes[insertPos++] = ac;
satok0cb20972012-03-13 22:07:56 +0900202 if (insertPos >= MAX_PROXIMITY_CHARS_SIZE) {
203 if (DEBUG_DICT) {
204 assert(false);
205 }
206 return;
207 }
satok5eec5742012-03-13 18:26:23 +0900208 }
209 }
satok0cb20972012-03-13 22:07:56 +0900210 // Add a delimiter for the proximity characters
satok1caff472012-03-14 23:17:12 +0900211 for (int i = insertPos; i < MAX_PROXIMITY_CHARS_SIZE; ++i) {
212 inputCodes[i] = NOT_A_CODE;
213 }
satoka70ee6e2012-03-07 15:12:22 +0900214}
215
satok1caff472012-03-14 23:17:12 +0900216void ProximityInfo::setInputParams(const int32_t* inputCodes, const int inputLength,
Yusuke Nojima258bfe62011-09-28 12:59:43 +0900217 const int* xCoordinates, const int* yCoordinates) {
satok1caff472012-03-14 23:17:12 +0900218 memset(mInputCodes, 0,
219 MAX_WORD_LENGTH_INTERNAL * MAX_PROXIMITY_CHARS_SIZE * sizeof(mInputCodes[0]));
220
221 for (int i = 0; i < inputLength; ++i) {
222 const int32_t primaryKey = inputCodes[i * MAX_PROXIMITY_CHARS_SIZE];
223 const int x = xCoordinates[i];
224 const int y = yCoordinates[i];
225 int *proximities = &mInputCodes[i * MAX_PROXIMITY_CHARS_SIZE];
226 calculateNearbyKeyCodes(x, y, primaryKey, proximities);
227 }
228
229 if (DEBUG_PROXIMITY_CHARS) {
230 for (int i = 0; i < inputLength; ++i) {
231 AKLOGI("---");
232 for (int j = 0; j < MAX_PROXIMITY_CHARS_SIZE; ++j) {
233 int icc = mInputCodes[i * MAX_PROXIMITY_CHARS_SIZE + j];
234 int icfjc = inputCodes[i * MAX_PROXIMITY_CHARS_SIZE + j];
235 icc+= 0;
236 icfjc += 0;
237 AKLOGI("--- (%d)%c,%c", i, icc, icfjc);
238 AKLOGI("--- A<%d>,B<%d>", icc, icfjc);
239 }
240 }
241 }
242 //Keep for debug, sorry
243 //for (int i = 0; i < MAX_WORD_LENGTH_INTERNAL * MAX_PROXIMITY_CHARS_SIZE; ++i) {
244 //if (i < inputLength * MAX_PROXIMITY_CHARS_SIZE) {
245 //mInputCodes[i] = mInputCodesFromJava[i];
246 //} else {
247 // mInputCodes[i] = 0;
248 // }
249 //}
Yusuke Nojima258bfe62011-09-28 12:59:43 +0900250 mInputXCoordinates = xCoordinates;
251 mInputYCoordinates = yCoordinates;
Yusuke Nojimaa4c1f1c2011-10-06 19:12:20 +0900252 mTouchPositionCorrectionEnabled =
253 HAS_TOUCH_POSITION_CORRECTION_DATA && xCoordinates && yCoordinates;
satok1d7eaf82011-07-13 10:32:02 +0900254 mInputLength = inputLength;
satok0cedd2b2011-08-12 01:05:27 +0900255 for (int i = 0; i < inputLength; ++i) {
256 mPrimaryInputWord[i] = getPrimaryCharAt(i);
257 }
258 mPrimaryInputWord[inputLength] = 0;
satok0cb20972012-03-13 22:07:56 +0900259 if (DEBUG_PROXIMITY_CHARS) {
260 AKLOGI("--- setInputParams");
261 }
Yusuke Nojimac25c7cc2011-10-03 16:44:29 +0900262 for (int i = 0; i < mInputLength; ++i) {
Yusuke Nojimaa4c1f1c2011-10-06 19:12:20 +0900263 const int *proximityChars = getProximityCharsAt(i);
satok0cb20972012-03-13 22:07:56 +0900264 const int primaryKey = proximityChars[0];
265 const int x = xCoordinates[i];
266 const int y = yCoordinates[i];
267 if (DEBUG_PROXIMITY_CHARS) {
268 int a = x + y + primaryKey;
269 a += 0;
270 AKLOGI("--- Primary = %c, x = %d, y = %d", primaryKey, x, y);
271 // Keep debug code just in case
272 //int proximities[50];
273 //for (int m = 0; m < 50; ++m) {
274 //proximities[m] = 0;
275 //}
276 //calculateNearbyKeyCodes(x, y, primaryKey, proximities);
277 //for (int l = 0; l < 50 && proximities[l] > 0; ++l) {
278 //if (DEBUG_PROXIMITY_CHARS) {
279 //AKLOGI("--- native Proximity (%d) = %c", l, proximities[l]);
280 //}
281 //}
282 }
Yusuke Nojimaa4c1f1c2011-10-06 19:12:20 +0900283 for (int j = 0; j < MAX_PROXIMITY_CHARS_SIZE && proximityChars[j] > 0; ++j) {
284 const int currentChar = proximityChars[j];
satok1caff472012-03-14 23:17:12 +0900285 const float squaredDistance = hasInputCoordinates()
286 ? calculateNormalizedSquaredDistance(getKeyIndex(currentChar), i)
287 : NOT_A_DISTANCE_FLOAT;
Yusuke Nojimaa4c1f1c2011-10-06 19:12:20 +0900288 if (squaredDistance >= 0.0f) {
289 mNormalizedSquaredDistances[i * MAX_PROXIMITY_CHARS_SIZE + j] =
290 (int)(squaredDistance * NORMALIZED_SQUARED_DISTANCE_SCALING_FACTOR);
291 } else {
292 mNormalizedSquaredDistances[i * MAX_PROXIMITY_CHARS_SIZE + j] = (j == 0)
293 ? EQUIVALENT_CHAR_WITHOUT_DISTANCE_INFO
294 : PROXIMITY_CHAR_WITHOUT_DISTANCE_INFO;
295 }
satok0cb20972012-03-13 22:07:56 +0900296 if (DEBUG_PROXIMITY_CHARS) {
297 AKLOGI("--- Proximity (%d) = %c", j, currentChar);
298 }
Yusuke Nojimae4ba8222011-10-05 14:55:07 +0900299 }
Yusuke Nojimac25c7cc2011-10-03 16:44:29 +0900300 }
satok1d7eaf82011-07-13 10:32:02 +0900301}
302
Yusuke Nojima16717152011-10-04 17:04:07 +0900303inline float square(const float x) { return x * x; }
304
Yusuke Nojimaa4c1f1c2011-10-06 19:12:20 +0900305float ProximityInfo::calculateNormalizedSquaredDistance(
306 const int keyIndex, const int inputIndex) const {
Jean Chalardbbc25602012-03-23 17:05:03 +0900307 if (keyIndex == NOT_AN_INDEX) {
Yusuke Nojimae4ba8222011-10-05 14:55:07 +0900308 return NOT_A_DISTANCE_FLOAT;
Yusuke Nojimac25c7cc2011-10-03 16:44:29 +0900309 }
Yusuke Nojimaa4c1f1c2011-10-06 19:12:20 +0900310 if (!hasSweetSpotData(keyIndex)) {
Yusuke Nojimae4ba8222011-10-05 14:55:07 +0900311 return NOT_A_DISTANCE_FLOAT;
Yusuke Nojima16717152011-10-04 17:04:07 +0900312 }
Jean Chalard0bfe3592012-01-25 18:11:26 +0900313 if (NOT_A_COORDINATE == mInputXCoordinates[inputIndex]) {
314 return NOT_A_DISTANCE_FLOAT;
315 }
Yusuke Nojimaa4c1f1c2011-10-06 19:12:20 +0900316 const float squaredDistance = calculateSquaredDistanceFromSweetSpotCenter(keyIndex, inputIndex);
317 const float squaredRadius = square(mSweetSpotRadii[keyIndex]);
Yusuke Nojimae4ba8222011-10-05 14:55:07 +0900318 return squaredDistance / squaredRadius;
Yusuke Nojima16717152011-10-04 17:04:07 +0900319}
320
satok1caff472012-03-14 23:17:12 +0900321bool ProximityInfo::hasInputCoordinates() const {
322 return mInputXCoordinates && mInputYCoordinates;
323}
324
Yusuke Nojimaa4c1f1c2011-10-06 19:12:20 +0900325int ProximityInfo::getKeyIndex(const int c) const {
satok1caff472012-03-14 23:17:12 +0900326 if (KEY_COUNT == 0) {
Yusuke Nojimaa4c1f1c2011-10-06 19:12:20 +0900327 // We do not have the coordinate data
Jean Chalardbbc25602012-03-23 17:05:03 +0900328 return NOT_AN_INDEX;
Yusuke Nojimaa4c1f1c2011-10-06 19:12:20 +0900329 }
Tadashi G. Takaoka6e3cb272011-11-11 14:26:13 +0900330 const unsigned short baseLowerC = toBaseLowerCase(c);
Yusuke Nojimaa4c1f1c2011-10-06 19:12:20 +0900331 if (baseLowerC > MAX_CHAR_CODE) {
Jean Chalardbbc25602012-03-23 17:05:03 +0900332 return NOT_AN_INDEX;
Yusuke Nojimaa4c1f1c2011-10-06 19:12:20 +0900333 }
334 return mCodeToKeyIndex[baseLowerC];
335}
336
Yusuke Nojimac25c7cc2011-10-03 16:44:29 +0900337float ProximityInfo::calculateSquaredDistanceFromSweetSpotCenter(
Yusuke Nojimaa4c1f1c2011-10-06 19:12:20 +0900338 const int keyIndex, const int inputIndex) const {
Yusuke Nojimac25c7cc2011-10-03 16:44:29 +0900339 const float sweetSpotCenterX = mSweetSpotCenterXs[keyIndex];
340 const float sweetSpotCenterY = mSweetSpotCenterYs[keyIndex];
341 const float inputX = (float)mInputXCoordinates[inputIndex];
342 const float inputY = (float)mInputYCoordinates[inputIndex];
343 return square(inputX - sweetSpotCenterX) + square(inputY - sweetSpotCenterY);
344}
345
satokd24df432011-07-14 15:43:42 +0900346inline const int* ProximityInfo::getProximityCharsAt(const int index) const {
satok1d7eaf82011-07-13 10:32:02 +0900347 return mInputCodes + (index * MAX_PROXIMITY_CHARS_SIZE);
348}
349
satokd24df432011-07-14 15:43:42 +0900350unsigned short ProximityInfo::getPrimaryCharAt(const int index) const {
351 return getProximityCharsAt(index)[0];
352}
353
satok2df30602011-07-15 13:49:00 +0900354inline bool ProximityInfo::existsCharInProximityAt(const int index, const int c) const {
satokd24df432011-07-14 15:43:42 +0900355 const int *chars = getProximityCharsAt(index);
356 int i = 0;
357 while (chars[i] > 0 && i < MAX_PROXIMITY_CHARS_SIZE) {
358 if (chars[i++] == c) {
359 return true;
360 }
361 }
362 return false;
363}
364
365bool ProximityInfo::existsAdjacentProximityChars(const int index) const {
366 if (index < 0 || index >= mInputLength) return false;
367 const int currentChar = getPrimaryCharAt(index);
368 const int leftIndex = index - 1;
369 if (leftIndex >= 0 && existsCharInProximityAt(leftIndex, currentChar)) {
370 return true;
371 }
372 const int rightIndex = index + 1;
373 if (rightIndex < mInputLength && existsCharInProximityAt(rightIndex, currentChar)) {
374 return true;
375 }
376 return false;
377}
378
379// In the following function, c is the current character of the dictionary word
380// currently examined.
381// currentChars is an array containing the keys close to the character the
382// user actually typed at the same position. We want to see if c is in it: if so,
383// then the word contains at that position a character close to what the user
384// typed.
385// What the user typed is actually the first character of the array.
Yusuke Nojimaa4c1f1c2011-10-06 19:12:20 +0900386// proximityIndex is a pointer to the variable where getMatchedProximityId returns
387// the index of c in the proximity chars of the input index.
satokd24df432011-07-14 15:43:42 +0900388// Notice : accented characters do not have a proximity list, so they are alone
389// in their list. The non-accented version of the character should be considered
390// "close", but not the other keys close to the non-accented version.
Yusuke Nojimaa4c1f1c2011-10-06 19:12:20 +0900391ProximityInfo::ProximityType ProximityInfo::getMatchedProximityId(const int index,
392 const unsigned short c, const bool checkProximityChars, int *proximityIndex) const {
satokd24df432011-07-14 15:43:42 +0900393 const int *currentChars = getProximityCharsAt(index);
Yusuke Nojima258bfe62011-09-28 12:59:43 +0900394 const int firstChar = currentChars[0];
Tadashi G. Takaoka6e3cb272011-11-11 14:26:13 +0900395 const unsigned short baseLowerC = toBaseLowerCase(c);
satokd24df432011-07-14 15:43:42 +0900396
397 // The first char in the array is what user typed. If it matches right away,
398 // that means the user typed that same char for this pos.
Yusuke Nojima258bfe62011-09-28 12:59:43 +0900399 if (firstChar == baseLowerC || firstChar == c) {
Yusuke Nojimae4ba8222011-10-05 14:55:07 +0900400 return EQUIVALENT_CHAR;
Yusuke Nojima258bfe62011-09-28 12:59:43 +0900401 }
satokd24df432011-07-14 15:43:42 +0900402
satok985312e2011-08-05 21:21:01 +0900403 if (!checkProximityChars) return UNRELATED_CHAR;
satokd24df432011-07-14 15:43:42 +0900404
405 // If the non-accented, lowercased version of that first character matches c,
406 // then we have a non-accented version of the accented character the user
407 // typed. Treat it as a close char.
Tadashi G. Takaoka6e3cb272011-11-11 14:26:13 +0900408 if (toBaseLowerCase(firstChar) == baseLowerC)
satokd24df432011-07-14 15:43:42 +0900409 return NEAR_PROXIMITY_CHAR;
410
411 // Not an exact nor an accent-alike match: search the list of close keys
412 int j = 1;
satoke05b3f42012-01-31 17:15:43 +0900413 while (j < MAX_PROXIMITY_CHARS_SIZE
414 && currentChars[j] > ADDITIONAL_PROXIMITY_CHAR_DELIMITER_CODE) {
satokd24df432011-07-14 15:43:42 +0900415 const bool matched = (currentChars[j] == baseLowerC || currentChars[j] == c);
Yusuke Nojimaa4c1f1c2011-10-06 19:12:20 +0900416 if (matched) {
417 if (proximityIndex) {
418 *proximityIndex = j;
419 }
420 return NEAR_PROXIMITY_CHAR;
421 }
satokd24df432011-07-14 15:43:42 +0900422 ++j;
423 }
satoke05b3f42012-01-31 17:15:43 +0900424 if (j < MAX_PROXIMITY_CHARS_SIZE
425 && currentChars[j] == ADDITIONAL_PROXIMITY_CHAR_DELIMITER_CODE) {
426 ++j;
427 while (j < MAX_PROXIMITY_CHARS_SIZE
428 && currentChars[j] > ADDITIONAL_PROXIMITY_CHAR_DELIMITER_CODE) {
429 const bool matched = (currentChars[j] == baseLowerC || currentChars[j] == c);
430 if (matched) {
431 if (proximityIndex) {
432 *proximityIndex = j;
433 }
434 return ADDITIONAL_PROXIMITY_CHAR;
435 }
436 ++j;
437 }
438 }
satokd24df432011-07-14 15:43:42 +0900439
440 // Was not included, signal this as an unrelated character.
441 return UNRELATED_CHAR;
442}
443
satok1d7eaf82011-07-13 10:32:02 +0900444bool ProximityInfo::sameAsTyped(const unsigned short *word, int length) const {
445 if (length != mInputLength) {
446 return false;
447 }
448 const int *inputCodes = mInputCodes;
449 while (length--) {
450 if ((unsigned int) *inputCodes != (unsigned int) *word) {
451 return false;
452 }
453 inputCodes += MAX_PROXIMITY_CHARS_SIZE;
454 word++;
455 }
456 return true;
457}
458
Yusuke Nojimae4ba8222011-10-05 14:55:07 +0900459const int ProximityInfo::NORMALIZED_SQUARED_DISTANCE_SCALING_FACTOR_LOG_2;
460const int ProximityInfo::NORMALIZED_SQUARED_DISTANCE_SCALING_FACTOR;
Yusuke Nojima0e1f6562011-09-21 12:02:47 +0900461const int ProximityInfo::MAX_KEY_COUNT_IN_A_KEYBOARD;
Yusuke Nojimaad358352011-09-29 16:44:54 +0900462const int ProximityInfo::MAX_CHAR_CODE;
Yusuke Nojima0e1f6562011-09-21 12:02:47 +0900463
Ken Wakasace9e52a2011-06-18 13:09:55 +0900464} // namespace latinime