blob: 4d03f3274017df2d343ca196ffbbd9dda7b04ff3 [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) {
133 const int left = mKeyXCoordinates[keyId];
134 const int top = mKeyYCoordinates[keyId];
135 const int right = left + mKeyWidths[keyId] + 1;
136 const int bottom = top + mKeyHeights[keyId];
137 return left < right && top < bottom && x >= left && x < right && y >= top && y < bottom;
138}
139
140int ProximityInfo::squaredDistanceToEdge(const int keyId, const int x, const int y) {
141 const int left = mKeyXCoordinates[keyId];
142 const int top = mKeyYCoordinates[keyId];
satok1caff472012-03-14 23:17:12 +0900143 const int right = left + mKeyWidths[keyId];
satoka70ee6e2012-03-07 15:12:22 +0900144 const int bottom = top + mKeyHeights[keyId];
145 const int edgeX = x < left ? left : (x > right ? right : x);
146 const int edgeY = y < top ? top : (y > bottom ? bottom : y);
147 const int dx = x - edgeX;
148 const int dy = y - edgeY;
149 return dx * dx + dy * dy;
150}
151
152void ProximityInfo::calculateNearbyKeyCodes(
satok0cb20972012-03-13 22:07:56 +0900153 const int x, const int y, const int32_t primaryKey, int *inputCodes) {
satoka70ee6e2012-03-07 15:12:22 +0900154 int insertPos = 0;
155 inputCodes[insertPos++] = primaryKey;
156 const int startIndex = getStartIndexFromCoordinates(x, y);
157 for (int i = 0; i < MAX_PROXIMITY_CHARS_SIZE; ++i) {
satok0cb20972012-03-13 22:07:56 +0900158 const int32_t c = mProximityCharsArray[startIndex + i];
satoka70ee6e2012-03-07 15:12:22 +0900159 if (c < KEYCODE_SPACE || c == primaryKey) {
160 continue;
161 }
satok1caff472012-03-14 23:17:12 +0900162 const int keyIndex = getKeyIndex(c);
satok0cb20972012-03-13 22:07:56 +0900163 const bool onKey = isOnKey(keyIndex, x, y);
164 const int distance = squaredDistanceToEdge(keyIndex, x, y);
satok1caff472012-03-14 23:17:12 +0900165 if (c >= KEYCODE_SPACE && (onKey || distance < MOST_COMMON_KEY_WIDTH_SQUARE)) {
satok0cb20972012-03-13 22:07:56 +0900166 inputCodes[insertPos++] = c;
167 if (insertPos >= MAX_PROXIMITY_CHARS_SIZE) {
168 if (DEBUG_DICT) {
169 assert(false);
170 }
171 return;
satoka70ee6e2012-03-07 15:12:22 +0900172 }
173 }
174 }
satok0cb20972012-03-13 22:07:56 +0900175 const int additionalProximitySize =
176 AdditionalProximityChars::getAdditionalCharsSize(&mLocaleStr, primaryKey);
177 if (additionalProximitySize > 0) {
satok1caff472012-03-14 23:17:12 +0900178 inputCodes[insertPos++] = ADDITIONAL_PROXIMITY_CHAR_DELIMITER_CODE;
179 if (insertPos >= MAX_PROXIMITY_CHARS_SIZE) {
180 if (DEBUG_DICT) {
181 assert(false);
182 }
183 return;
184 }
185
satok0cb20972012-03-13 22:07:56 +0900186 const int32_t* additionalProximityChars =
187 AdditionalProximityChars::getAdditionalChars(&mLocaleStr, primaryKey);
satok5eec5742012-03-13 18:26:23 +0900188 for (int j = 0; j < additionalProximitySize; ++j) {
satok0cb20972012-03-13 22:07:56 +0900189 const int32_t ac = additionalProximityChars[j];
satok5eec5742012-03-13 18:26:23 +0900190 int k = 0;
191 for (; k < insertPos; ++k) {
192 if ((int)ac == inputCodes[k]) {
193 break;
194 }
195 }
196 if (k < insertPos) {
197 continue;
198 }
199 inputCodes[insertPos++] = ac;
satok0cb20972012-03-13 22:07:56 +0900200 if (insertPos >= MAX_PROXIMITY_CHARS_SIZE) {
201 if (DEBUG_DICT) {
202 assert(false);
203 }
204 return;
205 }
satok5eec5742012-03-13 18:26:23 +0900206 }
207 }
satok0cb20972012-03-13 22:07:56 +0900208 // Add a delimiter for the proximity characters
satok1caff472012-03-14 23:17:12 +0900209 for (int i = insertPos; i < MAX_PROXIMITY_CHARS_SIZE; ++i) {
210 inputCodes[i] = NOT_A_CODE;
211 }
satoka70ee6e2012-03-07 15:12:22 +0900212}
213
satok1d7eaf82011-07-13 10:32:02 +0900214// TODO: Calculate nearby codes here.
satok1caff472012-03-14 23:17:12 +0900215void ProximityInfo::setInputParams(const int32_t* inputCodes, const int inputLength,
Yusuke Nojima258bfe62011-09-28 12:59:43 +0900216 const int* xCoordinates, const int* yCoordinates) {
satok1caff472012-03-14 23:17:12 +0900217 memset(mInputCodes, 0,
218 MAX_WORD_LENGTH_INTERNAL * MAX_PROXIMITY_CHARS_SIZE * sizeof(mInputCodes[0]));
219
220 for (int i = 0; i < inputLength; ++i) {
221 const int32_t primaryKey = inputCodes[i * MAX_PROXIMITY_CHARS_SIZE];
222 const int x = xCoordinates[i];
223 const int y = yCoordinates[i];
224 int *proximities = &mInputCodes[i * MAX_PROXIMITY_CHARS_SIZE];
225 calculateNearbyKeyCodes(x, y, primaryKey, proximities);
226 }
227
228 if (DEBUG_PROXIMITY_CHARS) {
229 for (int i = 0; i < inputLength; ++i) {
230 AKLOGI("---");
231 for (int j = 0; j < MAX_PROXIMITY_CHARS_SIZE; ++j) {
232 int icc = mInputCodes[i * MAX_PROXIMITY_CHARS_SIZE + j];
233 int icfjc = inputCodes[i * MAX_PROXIMITY_CHARS_SIZE + j];
234 icc+= 0;
235 icfjc += 0;
236 AKLOGI("--- (%d)%c,%c", i, icc, icfjc);
237 AKLOGI("--- A<%d>,B<%d>", icc, icfjc);
238 }
239 }
240 }
241 //Keep for debug, sorry
242 //for (int i = 0; i < MAX_WORD_LENGTH_INTERNAL * MAX_PROXIMITY_CHARS_SIZE; ++i) {
243 //if (i < inputLength * MAX_PROXIMITY_CHARS_SIZE) {
244 //mInputCodes[i] = mInputCodesFromJava[i];
245 //} else {
246 // mInputCodes[i] = 0;
247 // }
248 //}
Yusuke Nojima258bfe62011-09-28 12:59:43 +0900249 mInputXCoordinates = xCoordinates;
250 mInputYCoordinates = yCoordinates;
Yusuke Nojimaa4c1f1c2011-10-06 19:12:20 +0900251 mTouchPositionCorrectionEnabled =
252 HAS_TOUCH_POSITION_CORRECTION_DATA && xCoordinates && yCoordinates;
satok1d7eaf82011-07-13 10:32:02 +0900253 mInputLength = inputLength;
satok0cedd2b2011-08-12 01:05:27 +0900254 for (int i = 0; i < inputLength; ++i) {
255 mPrimaryInputWord[i] = getPrimaryCharAt(i);
256 }
257 mPrimaryInputWord[inputLength] = 0;
satok0cb20972012-03-13 22:07:56 +0900258 if (DEBUG_PROXIMITY_CHARS) {
259 AKLOGI("--- setInputParams");
260 }
Yusuke Nojimac25c7cc2011-10-03 16:44:29 +0900261 for (int i = 0; i < mInputLength; ++i) {
Yusuke Nojimaa4c1f1c2011-10-06 19:12:20 +0900262 const int *proximityChars = getProximityCharsAt(i);
satok0cb20972012-03-13 22:07:56 +0900263 const int primaryKey = proximityChars[0];
264 const int x = xCoordinates[i];
265 const int y = yCoordinates[i];
266 if (DEBUG_PROXIMITY_CHARS) {
267 int a = x + y + primaryKey;
268 a += 0;
269 AKLOGI("--- Primary = %c, x = %d, y = %d", primaryKey, x, y);
270 // Keep debug code just in case
271 //int proximities[50];
272 //for (int m = 0; m < 50; ++m) {
273 //proximities[m] = 0;
274 //}
275 //calculateNearbyKeyCodes(x, y, primaryKey, proximities);
276 //for (int l = 0; l < 50 && proximities[l] > 0; ++l) {
277 //if (DEBUG_PROXIMITY_CHARS) {
278 //AKLOGI("--- native Proximity (%d) = %c", l, proximities[l]);
279 //}
280 //}
281 }
Yusuke Nojimaa4c1f1c2011-10-06 19:12:20 +0900282 for (int j = 0; j < MAX_PROXIMITY_CHARS_SIZE && proximityChars[j] > 0; ++j) {
283 const int currentChar = proximityChars[j];
satok1caff472012-03-14 23:17:12 +0900284 const float squaredDistance = hasInputCoordinates()
285 ? calculateNormalizedSquaredDistance(getKeyIndex(currentChar), i)
286 : NOT_A_DISTANCE_FLOAT;
Yusuke Nojimaa4c1f1c2011-10-06 19:12:20 +0900287 if (squaredDistance >= 0.0f) {
288 mNormalizedSquaredDistances[i * MAX_PROXIMITY_CHARS_SIZE + j] =
289 (int)(squaredDistance * NORMALIZED_SQUARED_DISTANCE_SCALING_FACTOR);
290 } else {
291 mNormalizedSquaredDistances[i * MAX_PROXIMITY_CHARS_SIZE + j] = (j == 0)
292 ? EQUIVALENT_CHAR_WITHOUT_DISTANCE_INFO
293 : PROXIMITY_CHAR_WITHOUT_DISTANCE_INFO;
294 }
satok0cb20972012-03-13 22:07:56 +0900295 if (DEBUG_PROXIMITY_CHARS) {
296 AKLOGI("--- Proximity (%d) = %c", j, currentChar);
297 }
Yusuke Nojimae4ba8222011-10-05 14:55:07 +0900298 }
Yusuke Nojimac25c7cc2011-10-03 16:44:29 +0900299 }
satok1d7eaf82011-07-13 10:32:02 +0900300}
301
Yusuke Nojima16717152011-10-04 17:04:07 +0900302inline float square(const float x) { return x * x; }
303
Yusuke Nojimaa4c1f1c2011-10-06 19:12:20 +0900304float ProximityInfo::calculateNormalizedSquaredDistance(
305 const int keyIndex, const int inputIndex) const {
Yusuke Nojimaa4c1f1c2011-10-06 19:12:20 +0900306 if (keyIndex == NOT_A_INDEX) {
Yusuke Nojimae4ba8222011-10-05 14:55:07 +0900307 return NOT_A_DISTANCE_FLOAT;
Yusuke Nojimac25c7cc2011-10-03 16:44:29 +0900308 }
Yusuke Nojimaa4c1f1c2011-10-06 19:12:20 +0900309 if (!hasSweetSpotData(keyIndex)) {
Yusuke Nojimae4ba8222011-10-05 14:55:07 +0900310 return NOT_A_DISTANCE_FLOAT;
Yusuke Nojima16717152011-10-04 17:04:07 +0900311 }
Jean Chalard0bfe3592012-01-25 18:11:26 +0900312 if (NOT_A_COORDINATE == mInputXCoordinates[inputIndex]) {
313 return NOT_A_DISTANCE_FLOAT;
314 }
Yusuke Nojimaa4c1f1c2011-10-06 19:12:20 +0900315 const float squaredDistance = calculateSquaredDistanceFromSweetSpotCenter(keyIndex, inputIndex);
316 const float squaredRadius = square(mSweetSpotRadii[keyIndex]);
Yusuke Nojimae4ba8222011-10-05 14:55:07 +0900317 return squaredDistance / squaredRadius;
Yusuke Nojima16717152011-10-04 17:04:07 +0900318}
319
satok1caff472012-03-14 23:17:12 +0900320bool ProximityInfo::hasInputCoordinates() const {
321 return mInputXCoordinates && mInputYCoordinates;
322}
323
Yusuke Nojimaa4c1f1c2011-10-06 19:12:20 +0900324int ProximityInfo::getKeyIndex(const int c) const {
satok1caff472012-03-14 23:17:12 +0900325 if (KEY_COUNT == 0) {
Yusuke Nojimaa4c1f1c2011-10-06 19:12:20 +0900326 // We do not have the coordinate data
327 return NOT_A_INDEX;
328 }
Tadashi G. Takaoka6e3cb272011-11-11 14:26:13 +0900329 const unsigned short baseLowerC = toBaseLowerCase(c);
Yusuke Nojimaa4c1f1c2011-10-06 19:12:20 +0900330 if (baseLowerC > MAX_CHAR_CODE) {
331 return NOT_A_INDEX;
332 }
333 return mCodeToKeyIndex[baseLowerC];
334}
335
Yusuke Nojimac25c7cc2011-10-03 16:44:29 +0900336float ProximityInfo::calculateSquaredDistanceFromSweetSpotCenter(
Yusuke Nojimaa4c1f1c2011-10-06 19:12:20 +0900337 const int keyIndex, const int inputIndex) const {
Yusuke Nojimac25c7cc2011-10-03 16:44:29 +0900338 const float sweetSpotCenterX = mSweetSpotCenterXs[keyIndex];
339 const float sweetSpotCenterY = mSweetSpotCenterYs[keyIndex];
340 const float inputX = (float)mInputXCoordinates[inputIndex];
341 const float inputY = (float)mInputYCoordinates[inputIndex];
342 return square(inputX - sweetSpotCenterX) + square(inputY - sweetSpotCenterY);
343}
344
satokd24df432011-07-14 15:43:42 +0900345inline const int* ProximityInfo::getProximityCharsAt(const int index) const {
satok1d7eaf82011-07-13 10:32:02 +0900346 return mInputCodes + (index * MAX_PROXIMITY_CHARS_SIZE);
347}
348
satokd24df432011-07-14 15:43:42 +0900349unsigned short ProximityInfo::getPrimaryCharAt(const int index) const {
350 return getProximityCharsAt(index)[0];
351}
352
satok2df30602011-07-15 13:49:00 +0900353inline bool ProximityInfo::existsCharInProximityAt(const int index, const int c) const {
satokd24df432011-07-14 15:43:42 +0900354 const int *chars = getProximityCharsAt(index);
355 int i = 0;
356 while (chars[i] > 0 && i < MAX_PROXIMITY_CHARS_SIZE) {
357 if (chars[i++] == c) {
358 return true;
359 }
360 }
361 return false;
362}
363
364bool ProximityInfo::existsAdjacentProximityChars(const int index) const {
365 if (index < 0 || index >= mInputLength) return false;
366 const int currentChar = getPrimaryCharAt(index);
367 const int leftIndex = index - 1;
368 if (leftIndex >= 0 && existsCharInProximityAt(leftIndex, currentChar)) {
369 return true;
370 }
371 const int rightIndex = index + 1;
372 if (rightIndex < mInputLength && existsCharInProximityAt(rightIndex, currentChar)) {
373 return true;
374 }
375 return false;
376}
377
378// In the following function, c is the current character of the dictionary word
379// currently examined.
380// currentChars is an array containing the keys close to the character the
381// user actually typed at the same position. We want to see if c is in it: if so,
382// then the word contains at that position a character close to what the user
383// typed.
384// What the user typed is actually the first character of the array.
Yusuke Nojimaa4c1f1c2011-10-06 19:12:20 +0900385// proximityIndex is a pointer to the variable where getMatchedProximityId returns
386// the index of c in the proximity chars of the input index.
satokd24df432011-07-14 15:43:42 +0900387// Notice : accented characters do not have a proximity list, so they are alone
388// in their list. The non-accented version of the character should be considered
389// "close", but not the other keys close to the non-accented version.
Yusuke Nojimaa4c1f1c2011-10-06 19:12:20 +0900390ProximityInfo::ProximityType ProximityInfo::getMatchedProximityId(const int index,
391 const unsigned short c, const bool checkProximityChars, int *proximityIndex) const {
satokd24df432011-07-14 15:43:42 +0900392 const int *currentChars = getProximityCharsAt(index);
Yusuke Nojima258bfe62011-09-28 12:59:43 +0900393 const int firstChar = currentChars[0];
Tadashi G. Takaoka6e3cb272011-11-11 14:26:13 +0900394 const unsigned short baseLowerC = toBaseLowerCase(c);
satokd24df432011-07-14 15:43:42 +0900395
396 // The first char in the array is what user typed. If it matches right away,
397 // that means the user typed that same char for this pos.
Yusuke Nojima258bfe62011-09-28 12:59:43 +0900398 if (firstChar == baseLowerC || firstChar == c) {
Yusuke Nojimae4ba8222011-10-05 14:55:07 +0900399 return EQUIVALENT_CHAR;
Yusuke Nojima258bfe62011-09-28 12:59:43 +0900400 }
satokd24df432011-07-14 15:43:42 +0900401
satok985312e2011-08-05 21:21:01 +0900402 if (!checkProximityChars) return UNRELATED_CHAR;
satokd24df432011-07-14 15:43:42 +0900403
404 // If the non-accented, lowercased version of that first character matches c,
405 // then we have a non-accented version of the accented character the user
406 // typed. Treat it as a close char.
Tadashi G. Takaoka6e3cb272011-11-11 14:26:13 +0900407 if (toBaseLowerCase(firstChar) == baseLowerC)
satokd24df432011-07-14 15:43:42 +0900408 return NEAR_PROXIMITY_CHAR;
409
410 // Not an exact nor an accent-alike match: search the list of close keys
411 int j = 1;
satoke05b3f42012-01-31 17:15:43 +0900412 while (j < MAX_PROXIMITY_CHARS_SIZE
413 && currentChars[j] > ADDITIONAL_PROXIMITY_CHAR_DELIMITER_CODE) {
satokd24df432011-07-14 15:43:42 +0900414 const bool matched = (currentChars[j] == baseLowerC || currentChars[j] == c);
Yusuke Nojimaa4c1f1c2011-10-06 19:12:20 +0900415 if (matched) {
416 if (proximityIndex) {
417 *proximityIndex = j;
418 }
419 return NEAR_PROXIMITY_CHAR;
420 }
satokd24df432011-07-14 15:43:42 +0900421 ++j;
422 }
satoke05b3f42012-01-31 17:15:43 +0900423 if (j < MAX_PROXIMITY_CHARS_SIZE
424 && currentChars[j] == ADDITIONAL_PROXIMITY_CHAR_DELIMITER_CODE) {
425 ++j;
426 while (j < MAX_PROXIMITY_CHARS_SIZE
427 && currentChars[j] > ADDITIONAL_PROXIMITY_CHAR_DELIMITER_CODE) {
428 const bool matched = (currentChars[j] == baseLowerC || currentChars[j] == c);
429 if (matched) {
430 if (proximityIndex) {
431 *proximityIndex = j;
432 }
433 return ADDITIONAL_PROXIMITY_CHAR;
434 }
435 ++j;
436 }
437 }
satokd24df432011-07-14 15:43:42 +0900438
439 // Was not included, signal this as an unrelated character.
440 return UNRELATED_CHAR;
441}
442
satok1d7eaf82011-07-13 10:32:02 +0900443bool ProximityInfo::sameAsTyped(const unsigned short *word, int length) const {
444 if (length != mInputLength) {
445 return false;
446 }
447 const int *inputCodes = mInputCodes;
448 while (length--) {
449 if ((unsigned int) *inputCodes != (unsigned int) *word) {
450 return false;
451 }
452 inputCodes += MAX_PROXIMITY_CHARS_SIZE;
453 word++;
454 }
455 return true;
456}
457
Yusuke Nojimae4ba8222011-10-05 14:55:07 +0900458const int ProximityInfo::NORMALIZED_SQUARED_DISTANCE_SCALING_FACTOR_LOG_2;
459const int ProximityInfo::NORMALIZED_SQUARED_DISTANCE_SCALING_FACTOR;
Yusuke Nojima0e1f6562011-09-21 12:02:47 +0900460const int ProximityInfo::MAX_KEY_COUNT_IN_A_KEYBOARD;
Yusuke Nojimaad358352011-09-29 16:44:54 +0900461const int ProximityInfo::MAX_CHAR_CODE;
Yusuke Nojima0e1f6562011-09-21 12:02:47 +0900462
Ken Wakasace9e52a2011-06-18 13:09:55 +0900463} // namespace latinime