blob: dd60ed62cd68ae342fd96340ddd9f0c6c9e4186f [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];
satok817e5172011-03-04 06:06:45 -080058 if (DEBUG_PROXIMITY_INFO) {
satok9fb6f472012-01-13 18:01:22 +090059 AKLOGI("Create proximity info array %d", proximityGridLength);
satok817e5172011-03-04 06:06:45 -080060 }
Jean Chalard8c8ca592011-11-10 12:07:30 +090061 memcpy(mProximityCharsArray, proximityCharsArray,
62 proximityGridLength * sizeof(mProximityCharsArray[0]));
63 const int normalizedSquaredDistancesLength =
64 MAX_PROXIMITY_CHARS_SIZE * MAX_WORD_LENGTH_INTERNAL;
65 mNormalizedSquaredDistances = new int[normalizedSquaredDistancesLength];
66 for (int i = 0; i < normalizedSquaredDistancesLength; ++i) {
Yusuke Nojimaa4c1f1c2011-10-06 19:12:20 +090067 mNormalizedSquaredDistances[i] = NOT_A_DISTANCE;
68 }
Yusuke Nojima0e1f6562011-09-21 12:02:47 +090069
Yusuke Nojimade2f8422011-09-27 11:15:18 +090070 copyOrFillZero(mKeyXCoordinates, keyXCoordinates, KEY_COUNT * sizeof(mKeyXCoordinates[0]));
71 copyOrFillZero(mKeyYCoordinates, keyYCoordinates, KEY_COUNT * sizeof(mKeyYCoordinates[0]));
72 copyOrFillZero(mKeyWidths, keyWidths, KEY_COUNT * sizeof(mKeyWidths[0]));
73 copyOrFillZero(mKeyHeights, keyHeights, KEY_COUNT * sizeof(mKeyHeights[0]));
74 copyOrFillZero(mKeyCharCodes, keyCharCodes, KEY_COUNT * sizeof(mKeyCharCodes[0]));
Yusuke Nojimaad358352011-09-29 16:44:54 +090075 copyOrFillZero(mSweetSpotCenterXs, sweetSpotCenterXs,
76 KEY_COUNT * sizeof(mSweetSpotCenterXs[0]));
77 copyOrFillZero(mSweetSpotCenterYs, sweetSpotCenterYs,
78 KEY_COUNT * sizeof(mSweetSpotCenterYs[0]));
79 copyOrFillZero(mSweetSpotRadii, sweetSpotRadii, KEY_COUNT * sizeof(mSweetSpotRadii[0]));
Yusuke Nojima0e1f6562011-09-21 12:02:47 +090080
Yusuke Nojima0e1f6562011-09-21 12:02:47 +090081 initializeCodeToKeyIndex();
82}
83
Yusuke Nojima0e1f6562011-09-21 12:02:47 +090084// Build the reversed look up table from the char code to the index in mKeyXCoordinates,
85// mKeyYCoordinates, mKeyWidths, mKeyHeights, mKeyCharCodes.
86void ProximityInfo::initializeCodeToKeyIndex() {
Yusuke Nojimaad358352011-09-29 16:44:54 +090087 memset(mCodeToKeyIndex, -1, (MAX_CHAR_CODE + 1) * sizeof(mCodeToKeyIndex[0]));
Yusuke Nojima0e1f6562011-09-21 12:02:47 +090088 for (int i = 0; i < KEY_COUNT; ++i) {
89 const int code = mKeyCharCodes[i];
Yusuke Nojimaad358352011-09-29 16:44:54 +090090 if (0 <= code && code <= MAX_CHAR_CODE) {
Yusuke Nojima0e1f6562011-09-21 12:02:47 +090091 mCodeToKeyIndex[code] = i;
Yusuke Nojimaad358352011-09-29 16:44:54 +090092 }
Yusuke Nojima0e1f6562011-09-21 12:02:47 +090093 }
satok8fbd5522011-02-22 17:28:55 +090094}
95
96ProximityInfo::~ProximityInfo() {
Yusuke Nojimaa4c1f1c2011-10-06 19:12:20 +090097 delete[] mNormalizedSquaredDistances;
satok8fbd5522011-02-22 17:28:55 +090098 delete[] mProximityCharsArray;
99}
satok817e5172011-03-04 06:06:45 -0800100
101inline int ProximityInfo::getStartIndexFromCoordinates(const int x, const int y) const {
satok3c4bb772011-03-04 22:50:19 -0800102 return ((y / CELL_HEIGHT) * GRID_WIDTH + (x / CELL_WIDTH))
satok817e5172011-03-04 06:06:45 -0800103 * MAX_PROXIMITY_CHARS_SIZE;
satok8fbd5522011-02-22 17:28:55 +0900104}
satok817e5172011-03-04 06:06:45 -0800105
106bool ProximityInfo::hasSpaceProximity(const int x, const int y) const {
satok744dab62011-12-15 22:29:05 +0900107 if (x < 0 || y < 0) {
108 if (DEBUG_DICT) {
satok9fb6f472012-01-13 18:01:22 +0900109 AKLOGI("HasSpaceProximity: Illegal coordinates (%d, %d)", x, y);
satok1a6da632011-12-16 23:15:06 +0900110 assert(false);
satok744dab62011-12-15 22:29:05 +0900111 }
112 return false;
113 }
114
satok817e5172011-03-04 06:06:45 -0800115 const int startIndex = getStartIndexFromCoordinates(x, y);
116 if (DEBUG_PROXIMITY_INFO) {
satok9fb6f472012-01-13 18:01:22 +0900117 AKLOGI("hasSpaceProximity: index %d, %d, %d", startIndex, x, y);
satok817e5172011-03-04 06:06:45 -0800118 }
119 for (int i = 0; i < MAX_PROXIMITY_CHARS_SIZE; ++i) {
120 if (DEBUG_PROXIMITY_INFO) {
satok9fb6f472012-01-13 18:01:22 +0900121 AKLOGI("Index: %d", mProximityCharsArray[startIndex + i]);
satok817e5172011-03-04 06:06:45 -0800122 }
123 if (mProximityCharsArray[startIndex + i] == KEYCODE_SPACE) {
124 return true;
125 }
126 }
127 return false;
128}
Ken Wakasace9e52a2011-06-18 13:09:55 +0900129
satoka70ee6e2012-03-07 15:12:22 +0900130bool ProximityInfo::isOnKey(const int keyId, const int x, const int y) {
131 const int left = mKeyXCoordinates[keyId];
132 const int top = mKeyYCoordinates[keyId];
133 const int right = left + mKeyWidths[keyId] + 1;
134 const int bottom = top + mKeyHeights[keyId];
135 return left < right && top < bottom && x >= left && x < right && y >= top && y < bottom;
136}
137
138int ProximityInfo::squaredDistanceToEdge(const int keyId, const int x, const int y) {
139 const int left = mKeyXCoordinates[keyId];
140 const int top = mKeyYCoordinates[keyId];
141 const int right = left + mKeyWidths[keyId] + 1;
142 const int bottom = top + mKeyHeights[keyId];
143 const int edgeX = x < left ? left : (x > right ? right : x);
144 const int edgeY = y < top ? top : (y > bottom ? bottom : y);
145 const int dx = x - edgeX;
146 const int dy = y - edgeY;
147 return dx * dx + dy * dy;
148}
149
150void ProximityInfo::calculateNearbyKeyCodes(
satok0cb20972012-03-13 22:07:56 +0900151 const int x, const int y, const int32_t primaryKey, int *inputCodes) {
satoka70ee6e2012-03-07 15:12:22 +0900152 int insertPos = 0;
153 inputCodes[insertPos++] = primaryKey;
154 const int startIndex = getStartIndexFromCoordinates(x, y);
155 for (int i = 0; i < MAX_PROXIMITY_CHARS_SIZE; ++i) {
satok0cb20972012-03-13 22:07:56 +0900156 const int32_t c = mProximityCharsArray[startIndex + i];
satoka70ee6e2012-03-07 15:12:22 +0900157 if (c < KEYCODE_SPACE || c == primaryKey) {
158 continue;
159 }
satok0cb20972012-03-13 22:07:56 +0900160 int keyIndex = getKeyIndex(c);
161 const bool onKey = isOnKey(keyIndex, x, y);
162 const int distance = squaredDistanceToEdge(keyIndex, x, y);
163 if (onKey || distance < MOST_COMMON_KEY_WIDTH_SQUARE) {
164 inputCodes[insertPos++] = c;
165 if (insertPos >= MAX_PROXIMITY_CHARS_SIZE) {
166 if (DEBUG_DICT) {
167 assert(false);
168 }
169 return;
satoka70ee6e2012-03-07 15:12:22 +0900170 }
171 }
172 }
satok0cb20972012-03-13 22:07:56 +0900173 inputCodes[insertPos++] = ADDITIONAL_PROXIMITY_CHAR_DELIMITER_CODE;
174 if (insertPos >= MAX_PROXIMITY_CHARS_SIZE) {
175 if (DEBUG_DICT) {
176 assert(false);
satok5eec5742012-03-13 18:26:23 +0900177 }
satok0cb20972012-03-13 22:07:56 +0900178 return;
179 }
180
181 const int additionalProximitySize =
182 AdditionalProximityChars::getAdditionalCharsSize(&mLocaleStr, primaryKey);
183 if (additionalProximitySize > 0) {
184 const int32_t* additionalProximityChars =
185 AdditionalProximityChars::getAdditionalChars(&mLocaleStr, primaryKey);
satok5eec5742012-03-13 18:26:23 +0900186 for (int j = 0; j < additionalProximitySize; ++j) {
satok0cb20972012-03-13 22:07:56 +0900187 const int32_t ac = additionalProximityChars[j];
satok5eec5742012-03-13 18:26:23 +0900188 int k = 0;
189 for (; k < insertPos; ++k) {
190 if ((int)ac == inputCodes[k]) {
191 break;
192 }
193 }
194 if (k < insertPos) {
195 continue;
196 }
197 inputCodes[insertPos++] = ac;
satok0cb20972012-03-13 22:07:56 +0900198 if (insertPos >= MAX_PROXIMITY_CHARS_SIZE) {
199 if (DEBUG_DICT) {
200 assert(false);
201 }
202 return;
203 }
satok5eec5742012-03-13 18:26:23 +0900204 }
205 }
satok0cb20972012-03-13 22:07:56 +0900206 // Add a delimiter for the proximity characters
207 inputCodes[insertPos] = 0;
satoka70ee6e2012-03-07 15:12:22 +0900208}
209
satok1d7eaf82011-07-13 10:32:02 +0900210// TODO: Calculate nearby codes here.
Yusuke Nojima258bfe62011-09-28 12:59:43 +0900211void ProximityInfo::setInputParams(const int* inputCodes, const int inputLength,
212 const int* xCoordinates, const int* yCoordinates) {
satok1d7eaf82011-07-13 10:32:02 +0900213 mInputCodes = inputCodes;
Yusuke Nojima258bfe62011-09-28 12:59:43 +0900214 mInputXCoordinates = xCoordinates;
215 mInputYCoordinates = yCoordinates;
Yusuke Nojimaa4c1f1c2011-10-06 19:12:20 +0900216 mTouchPositionCorrectionEnabled =
217 HAS_TOUCH_POSITION_CORRECTION_DATA && xCoordinates && yCoordinates;
satok1d7eaf82011-07-13 10:32:02 +0900218 mInputLength = inputLength;
satok0cedd2b2011-08-12 01:05:27 +0900219 for (int i = 0; i < inputLength; ++i) {
220 mPrimaryInputWord[i] = getPrimaryCharAt(i);
221 }
222 mPrimaryInputWord[inputLength] = 0;
satok0cb20972012-03-13 22:07:56 +0900223 if (DEBUG_PROXIMITY_CHARS) {
224 AKLOGI("--- setInputParams");
225 }
Yusuke Nojimac25c7cc2011-10-03 16:44:29 +0900226 for (int i = 0; i < mInputLength; ++i) {
Yusuke Nojimaa4c1f1c2011-10-06 19:12:20 +0900227 const int *proximityChars = getProximityCharsAt(i);
satok0cb20972012-03-13 22:07:56 +0900228 const int primaryKey = proximityChars[0];
229 const int x = xCoordinates[i];
230 const int y = yCoordinates[i];
231 if (DEBUG_PROXIMITY_CHARS) {
232 int a = x + y + primaryKey;
233 a += 0;
234 AKLOGI("--- Primary = %c, x = %d, y = %d", primaryKey, x, y);
235 // Keep debug code just in case
236 //int proximities[50];
237 //for (int m = 0; m < 50; ++m) {
238 //proximities[m] = 0;
239 //}
240 //calculateNearbyKeyCodes(x, y, primaryKey, proximities);
241 //for (int l = 0; l < 50 && proximities[l] > 0; ++l) {
242 //if (DEBUG_PROXIMITY_CHARS) {
243 //AKLOGI("--- native Proximity (%d) = %c", l, proximities[l]);
244 //}
245 //}
246 }
Yusuke Nojimaa4c1f1c2011-10-06 19:12:20 +0900247 for (int j = 0; j < MAX_PROXIMITY_CHARS_SIZE && proximityChars[j] > 0; ++j) {
248 const int currentChar = proximityChars[j];
249 const int keyIndex = getKeyIndex(currentChar);
250 const float squaredDistance = calculateNormalizedSquaredDistance(keyIndex, i);
251 if (squaredDistance >= 0.0f) {
252 mNormalizedSquaredDistances[i * MAX_PROXIMITY_CHARS_SIZE + j] =
253 (int)(squaredDistance * NORMALIZED_SQUARED_DISTANCE_SCALING_FACTOR);
254 } else {
255 mNormalizedSquaredDistances[i * MAX_PROXIMITY_CHARS_SIZE + j] = (j == 0)
256 ? EQUIVALENT_CHAR_WITHOUT_DISTANCE_INFO
257 : PROXIMITY_CHAR_WITHOUT_DISTANCE_INFO;
258 }
satok0cb20972012-03-13 22:07:56 +0900259 if (DEBUG_PROXIMITY_CHARS) {
260 AKLOGI("--- Proximity (%d) = %c", j, currentChar);
261 }
Yusuke Nojimae4ba8222011-10-05 14:55:07 +0900262 }
Yusuke Nojimac25c7cc2011-10-03 16:44:29 +0900263 }
satok1d7eaf82011-07-13 10:32:02 +0900264}
265
Yusuke Nojima16717152011-10-04 17:04:07 +0900266inline float square(const float x) { return x * x; }
267
Yusuke Nojimaa4c1f1c2011-10-06 19:12:20 +0900268float ProximityInfo::calculateNormalizedSquaredDistance(
269 const int keyIndex, const int inputIndex) const {
Yusuke Nojimae4ba8222011-10-05 14:55:07 +0900270 static const float NOT_A_DISTANCE_FLOAT = -1.0f;
Yusuke Nojimaa4c1f1c2011-10-06 19:12:20 +0900271 if (keyIndex == NOT_A_INDEX) {
Yusuke Nojimae4ba8222011-10-05 14:55:07 +0900272 return NOT_A_DISTANCE_FLOAT;
Yusuke Nojimac25c7cc2011-10-03 16:44:29 +0900273 }
Yusuke Nojimaa4c1f1c2011-10-06 19:12:20 +0900274 if (!hasSweetSpotData(keyIndex)) {
Yusuke Nojimae4ba8222011-10-05 14:55:07 +0900275 return NOT_A_DISTANCE_FLOAT;
Yusuke Nojima16717152011-10-04 17:04:07 +0900276 }
Jean Chalard0bfe3592012-01-25 18:11:26 +0900277 if (NOT_A_COORDINATE == mInputXCoordinates[inputIndex]) {
278 return NOT_A_DISTANCE_FLOAT;
279 }
Yusuke Nojimaa4c1f1c2011-10-06 19:12:20 +0900280 const float squaredDistance = calculateSquaredDistanceFromSweetSpotCenter(keyIndex, inputIndex);
281 const float squaredRadius = square(mSweetSpotRadii[keyIndex]);
Yusuke Nojimae4ba8222011-10-05 14:55:07 +0900282 return squaredDistance / squaredRadius;
Yusuke Nojima16717152011-10-04 17:04:07 +0900283}
284
Yusuke Nojimaa4c1f1c2011-10-06 19:12:20 +0900285int ProximityInfo::getKeyIndex(const int c) const {
286 if (KEY_COUNT == 0 || !mInputXCoordinates || !mInputYCoordinates) {
287 // We do not have the coordinate data
288 return NOT_A_INDEX;
289 }
Tadashi G. Takaoka6e3cb272011-11-11 14:26:13 +0900290 const unsigned short baseLowerC = toBaseLowerCase(c);
Yusuke Nojimaa4c1f1c2011-10-06 19:12:20 +0900291 if (baseLowerC > MAX_CHAR_CODE) {
292 return NOT_A_INDEX;
293 }
294 return mCodeToKeyIndex[baseLowerC];
295}
296
Yusuke Nojimac25c7cc2011-10-03 16:44:29 +0900297float ProximityInfo::calculateSquaredDistanceFromSweetSpotCenter(
Yusuke Nojimaa4c1f1c2011-10-06 19:12:20 +0900298 const int keyIndex, const int inputIndex) const {
Yusuke Nojimac25c7cc2011-10-03 16:44:29 +0900299 const float sweetSpotCenterX = mSweetSpotCenterXs[keyIndex];
300 const float sweetSpotCenterY = mSweetSpotCenterYs[keyIndex];
301 const float inputX = (float)mInputXCoordinates[inputIndex];
302 const float inputY = (float)mInputYCoordinates[inputIndex];
303 return square(inputX - sweetSpotCenterX) + square(inputY - sweetSpotCenterY);
304}
305
satokd24df432011-07-14 15:43:42 +0900306inline const int* ProximityInfo::getProximityCharsAt(const int index) const {
satok1d7eaf82011-07-13 10:32:02 +0900307 return mInputCodes + (index * MAX_PROXIMITY_CHARS_SIZE);
308}
309
satokd24df432011-07-14 15:43:42 +0900310unsigned short ProximityInfo::getPrimaryCharAt(const int index) const {
311 return getProximityCharsAt(index)[0];
312}
313
satok2df30602011-07-15 13:49:00 +0900314inline bool ProximityInfo::existsCharInProximityAt(const int index, const int c) const {
satokd24df432011-07-14 15:43:42 +0900315 const int *chars = getProximityCharsAt(index);
316 int i = 0;
317 while (chars[i] > 0 && i < MAX_PROXIMITY_CHARS_SIZE) {
318 if (chars[i++] == c) {
319 return true;
320 }
321 }
322 return false;
323}
324
325bool ProximityInfo::existsAdjacentProximityChars(const int index) const {
326 if (index < 0 || index >= mInputLength) return false;
327 const int currentChar = getPrimaryCharAt(index);
328 const int leftIndex = index - 1;
329 if (leftIndex >= 0 && existsCharInProximityAt(leftIndex, currentChar)) {
330 return true;
331 }
332 const int rightIndex = index + 1;
333 if (rightIndex < mInputLength && existsCharInProximityAt(rightIndex, currentChar)) {
334 return true;
335 }
336 return false;
337}
338
339// In the following function, c is the current character of the dictionary word
340// currently examined.
341// currentChars is an array containing the keys close to the character the
342// user actually typed at the same position. We want to see if c is in it: if so,
343// then the word contains at that position a character close to what the user
344// typed.
345// What the user typed is actually the first character of the array.
Yusuke Nojimaa4c1f1c2011-10-06 19:12:20 +0900346// proximityIndex is a pointer to the variable where getMatchedProximityId returns
347// the index of c in the proximity chars of the input index.
satokd24df432011-07-14 15:43:42 +0900348// Notice : accented characters do not have a proximity list, so they are alone
349// in their list. The non-accented version of the character should be considered
350// "close", but not the other keys close to the non-accented version.
Yusuke Nojimaa4c1f1c2011-10-06 19:12:20 +0900351ProximityInfo::ProximityType ProximityInfo::getMatchedProximityId(const int index,
352 const unsigned short c, const bool checkProximityChars, int *proximityIndex) const {
satokd24df432011-07-14 15:43:42 +0900353 const int *currentChars = getProximityCharsAt(index);
Yusuke Nojima258bfe62011-09-28 12:59:43 +0900354 const int firstChar = currentChars[0];
Tadashi G. Takaoka6e3cb272011-11-11 14:26:13 +0900355 const unsigned short baseLowerC = toBaseLowerCase(c);
satokd24df432011-07-14 15:43:42 +0900356
357 // The first char in the array is what user typed. If it matches right away,
358 // that means the user typed that same char for this pos.
Yusuke Nojima258bfe62011-09-28 12:59:43 +0900359 if (firstChar == baseLowerC || firstChar == c) {
Yusuke Nojimae4ba8222011-10-05 14:55:07 +0900360 return EQUIVALENT_CHAR;
Yusuke Nojima258bfe62011-09-28 12:59:43 +0900361 }
satokd24df432011-07-14 15:43:42 +0900362
satok985312e2011-08-05 21:21:01 +0900363 if (!checkProximityChars) return UNRELATED_CHAR;
satokd24df432011-07-14 15:43:42 +0900364
365 // If the non-accented, lowercased version of that first character matches c,
366 // then we have a non-accented version of the accented character the user
367 // typed. Treat it as a close char.
Tadashi G. Takaoka6e3cb272011-11-11 14:26:13 +0900368 if (toBaseLowerCase(firstChar) == baseLowerC)
satokd24df432011-07-14 15:43:42 +0900369 return NEAR_PROXIMITY_CHAR;
370
371 // Not an exact nor an accent-alike match: search the list of close keys
372 int j = 1;
satoke05b3f42012-01-31 17:15:43 +0900373 while (j < MAX_PROXIMITY_CHARS_SIZE
374 && currentChars[j] > ADDITIONAL_PROXIMITY_CHAR_DELIMITER_CODE) {
satokd24df432011-07-14 15:43:42 +0900375 const bool matched = (currentChars[j] == baseLowerC || currentChars[j] == c);
Yusuke Nojimaa4c1f1c2011-10-06 19:12:20 +0900376 if (matched) {
377 if (proximityIndex) {
378 *proximityIndex = j;
379 }
380 return NEAR_PROXIMITY_CHAR;
381 }
satokd24df432011-07-14 15:43:42 +0900382 ++j;
383 }
satoke05b3f42012-01-31 17:15:43 +0900384 if (j < MAX_PROXIMITY_CHARS_SIZE
385 && currentChars[j] == ADDITIONAL_PROXIMITY_CHAR_DELIMITER_CODE) {
386 ++j;
387 while (j < MAX_PROXIMITY_CHARS_SIZE
388 && currentChars[j] > ADDITIONAL_PROXIMITY_CHAR_DELIMITER_CODE) {
389 const bool matched = (currentChars[j] == baseLowerC || currentChars[j] == c);
390 if (matched) {
391 if (proximityIndex) {
392 *proximityIndex = j;
393 }
394 return ADDITIONAL_PROXIMITY_CHAR;
395 }
396 ++j;
397 }
398 }
satokd24df432011-07-14 15:43:42 +0900399
400 // Was not included, signal this as an unrelated character.
401 return UNRELATED_CHAR;
402}
403
satok1d7eaf82011-07-13 10:32:02 +0900404bool ProximityInfo::sameAsTyped(const unsigned short *word, int length) const {
405 if (length != mInputLength) {
406 return false;
407 }
408 const int *inputCodes = mInputCodes;
409 while (length--) {
410 if ((unsigned int) *inputCodes != (unsigned int) *word) {
411 return false;
412 }
413 inputCodes += MAX_PROXIMITY_CHARS_SIZE;
414 word++;
415 }
416 return true;
417}
418
Yusuke Nojimae4ba8222011-10-05 14:55:07 +0900419const int ProximityInfo::NORMALIZED_SQUARED_DISTANCE_SCALING_FACTOR_LOG_2;
420const int ProximityInfo::NORMALIZED_SQUARED_DISTANCE_SCALING_FACTOR;
Yusuke Nojima0e1f6562011-09-21 12:02:47 +0900421const int ProximityInfo::MAX_KEY_COUNT_IN_A_KEYBOARD;
Yusuke Nojimaad358352011-09-29 16:44:54 +0900422const int ProximityInfo::MAX_CHAR_CODE;
Yusuke Nojima0e1f6562011-09-21 12:02:47 +0900423
Ken Wakasace9e52a2011-06-18 13:09:55 +0900424} // namespace latinime