blob: 960d401196633dc4d051ec30f9ca21c680d6f909 [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"
Ken Wakasa3b088a22012-05-16 23:05:32 +090024#include "defines.h"
satokd24df432011-07-14 15:43:42 +090025#include "dictionary.h"
satok8fbd5522011-02-22 17:28:55 +090026#include "proximity_info.h"
27
28namespace latinime {
Ken Wakasace9e52a2011-06-18 13:09:55 +090029
Yusuke Nojimade2f8422011-09-27 11:15:18 +090030inline void copyOrFillZero(void *to, const void *from, size_t size) {
31 if (from) {
32 memcpy(to, from, size);
33 } else {
34 memset(to, 0, size);
35 }
36}
37
satok552c3c22012-03-13 16:33:47 +090038ProximityInfo::ProximityInfo(const std::string localeStr, const int maxProximityCharsSize,
39 const int keyboardWidth, const int keyboardHeight, const int gridWidth,
40 const int gridHeight, const int mostCommonKeyWidth,
satok0cb20972012-03-13 22:07:56 +090041 const int32_t *proximityCharsArray, const int keyCount, const int32_t *keyXCoordinates,
Yusuke Nojima0e1f6562011-09-21 12:02:47 +090042 const int32_t *keyYCoordinates, const int32_t *keyWidths, const int32_t *keyHeights,
Yusuke Nojimaad358352011-09-29 16:44:54 +090043 const int32_t *keyCharCodes, const float *sweetSpotCenterXs, const float *sweetSpotCenterYs,
44 const float *sweetSpotRadii)
satok817e5172011-03-04 06:06:45 -080045 : MAX_PROXIMITY_CHARS_SIZE(maxProximityCharsSize), KEYBOARD_WIDTH(keyboardWidth),
46 KEYBOARD_HEIGHT(keyboardHeight), GRID_WIDTH(gridWidth), GRID_HEIGHT(gridHeight),
satoka70ee6e2012-03-07 15:12:22 +090047 MOST_COMMON_KEY_WIDTH_SQUARE(mostCommonKeyWidth * mostCommonKeyWidth),
satok817e5172011-03-04 06:06:45 -080048 CELL_WIDTH((keyboardWidth + gridWidth - 1) / gridWidth),
Yusuke Nojima0e1f6562011-09-21 12:02:47 +090049 CELL_HEIGHT((keyboardHeight + gridHeight - 1) / gridHeight),
Yusuke Nojima258bfe62011-09-28 12:59:43 +090050 KEY_COUNT(min(keyCount, MAX_KEY_COUNT_IN_A_KEYBOARD)),
Yusuke Nojimaa4c1f1c2011-10-06 19:12:20 +090051 HAS_TOUCH_POSITION_CORRECTION_DATA(keyCount > 0 && keyXCoordinates && keyYCoordinates
52 && keyWidths && keyHeights && keyCharCodes && sweetSpotCenterXs
53 && sweetSpotCenterYs && sweetSpotRadii),
satok5eec5742012-03-13 18:26:23 +090054 mLocaleStr(localeStr),
Tadashi G. Takaoka0e971482011-10-28 17:02:09 +090055 mInputXCoordinates(0), mInputYCoordinates(0),
Yusuke Nojimaa4c1f1c2011-10-06 19:12:20 +090056 mTouchPositionCorrectionEnabled(false) {
Jean Chalard8c8ca592011-11-10 12:07:30 +090057 const int proximityGridLength = GRID_WIDTH * GRID_HEIGHT * MAX_PROXIMITY_CHARS_SIZE;
satok0cb20972012-03-13 22:07:56 +090058 mProximityCharsArray = new int32_t[proximityGridLength];
satok1caff472012-03-14 23:17:12 +090059 mInputCodes = new int32_t[MAX_PROXIMITY_CHARS_SIZE * MAX_WORD_LENGTH_INTERNAL];
satok817e5172011-03-04 06:06:45 -080060 if (DEBUG_PROXIMITY_INFO) {
satok9fb6f472012-01-13 18:01:22 +090061 AKLOGI("Create proximity info array %d", proximityGridLength);
satok817e5172011-03-04 06:06:45 -080062 }
Jean Chalard8c8ca592011-11-10 12:07:30 +090063 memcpy(mProximityCharsArray, proximityCharsArray,
64 proximityGridLength * sizeof(mProximityCharsArray[0]));
65 const int normalizedSquaredDistancesLength =
66 MAX_PROXIMITY_CHARS_SIZE * MAX_WORD_LENGTH_INTERNAL;
67 mNormalizedSquaredDistances = new int[normalizedSquaredDistancesLength];
68 for (int i = 0; i < normalizedSquaredDistancesLength; ++i) {
Yusuke Nojimaa4c1f1c2011-10-06 19:12:20 +090069 mNormalizedSquaredDistances[i] = NOT_A_DISTANCE;
70 }
Yusuke Nojima0e1f6562011-09-21 12:02:47 +090071
Yusuke Nojimade2f8422011-09-27 11:15:18 +090072 copyOrFillZero(mKeyXCoordinates, keyXCoordinates, KEY_COUNT * sizeof(mKeyXCoordinates[0]));
73 copyOrFillZero(mKeyYCoordinates, keyYCoordinates, KEY_COUNT * sizeof(mKeyYCoordinates[0]));
74 copyOrFillZero(mKeyWidths, keyWidths, KEY_COUNT * sizeof(mKeyWidths[0]));
75 copyOrFillZero(mKeyHeights, keyHeights, KEY_COUNT * sizeof(mKeyHeights[0]));
76 copyOrFillZero(mKeyCharCodes, keyCharCodes, KEY_COUNT * sizeof(mKeyCharCodes[0]));
Yusuke Nojimaad358352011-09-29 16:44:54 +090077 copyOrFillZero(mSweetSpotCenterXs, sweetSpotCenterXs,
78 KEY_COUNT * sizeof(mSweetSpotCenterXs[0]));
79 copyOrFillZero(mSweetSpotCenterYs, sweetSpotCenterYs,
80 KEY_COUNT * sizeof(mSweetSpotCenterYs[0]));
81 copyOrFillZero(mSweetSpotRadii, sweetSpotRadii, KEY_COUNT * sizeof(mSweetSpotRadii[0]));
Yusuke Nojima0e1f6562011-09-21 12:02:47 +090082
Yusuke Nojima0e1f6562011-09-21 12:02:47 +090083 initializeCodeToKeyIndex();
84}
85
Yusuke Nojima0e1f6562011-09-21 12:02:47 +090086// Build the reversed look up table from the char code to the index in mKeyXCoordinates,
87// mKeyYCoordinates, mKeyWidths, mKeyHeights, mKeyCharCodes.
88void ProximityInfo::initializeCodeToKeyIndex() {
Yusuke Nojimaad358352011-09-29 16:44:54 +090089 memset(mCodeToKeyIndex, -1, (MAX_CHAR_CODE + 1) * sizeof(mCodeToKeyIndex[0]));
Yusuke Nojima0e1f6562011-09-21 12:02:47 +090090 for (int i = 0; i < KEY_COUNT; ++i) {
91 const int code = mKeyCharCodes[i];
Yusuke Nojimaad358352011-09-29 16:44:54 +090092 if (0 <= code && code <= MAX_CHAR_CODE) {
Yusuke Nojima0e1f6562011-09-21 12:02:47 +090093 mCodeToKeyIndex[code] = i;
Yusuke Nojimaad358352011-09-29 16:44:54 +090094 }
Yusuke Nojima0e1f6562011-09-21 12:02:47 +090095 }
satok8fbd5522011-02-22 17:28:55 +090096}
97
98ProximityInfo::~ProximityInfo() {
Yusuke Nojimaa4c1f1c2011-10-06 19:12:20 +090099 delete[] mNormalizedSquaredDistances;
satok8fbd5522011-02-22 17:28:55 +0900100 delete[] mProximityCharsArray;
satok1caff472012-03-14 23:17:12 +0900101 delete[] mInputCodes;
satok8fbd5522011-02-22 17:28:55 +0900102}
satok817e5172011-03-04 06:06:45 -0800103
104inline int ProximityInfo::getStartIndexFromCoordinates(const int x, const int y) const {
satok3c4bb772011-03-04 22:50:19 -0800105 return ((y / CELL_HEIGHT) * GRID_WIDTH + (x / CELL_WIDTH))
satok817e5172011-03-04 06:06:45 -0800106 * MAX_PROXIMITY_CHARS_SIZE;
satok8fbd5522011-02-22 17:28:55 +0900107}
satok817e5172011-03-04 06:06:45 -0800108
109bool ProximityInfo::hasSpaceProximity(const int x, const int y) const {
satok744dab62011-12-15 22:29:05 +0900110 if (x < 0 || y < 0) {
111 if (DEBUG_DICT) {
satok9fb6f472012-01-13 18:01:22 +0900112 AKLOGI("HasSpaceProximity: Illegal coordinates (%d, %d)", x, y);
satok1a6da632011-12-16 23:15:06 +0900113 assert(false);
satok744dab62011-12-15 22:29:05 +0900114 }
115 return false;
116 }
117
satok817e5172011-03-04 06:06:45 -0800118 const int startIndex = getStartIndexFromCoordinates(x, y);
119 if (DEBUG_PROXIMITY_INFO) {
satok9fb6f472012-01-13 18:01:22 +0900120 AKLOGI("hasSpaceProximity: index %d, %d, %d", startIndex, x, y);
satok817e5172011-03-04 06:06:45 -0800121 }
122 for (int i = 0; i < MAX_PROXIMITY_CHARS_SIZE; ++i) {
123 if (DEBUG_PROXIMITY_INFO) {
satok9fb6f472012-01-13 18:01:22 +0900124 AKLOGI("Index: %d", mProximityCharsArray[startIndex + i]);
satok817e5172011-03-04 06:06:45 -0800125 }
126 if (mProximityCharsArray[startIndex + i] == KEYCODE_SPACE) {
127 return true;
128 }
129 }
130 return false;
131}
Ken Wakasace9e52a2011-06-18 13:09:55 +0900132
satok9df4a452012-03-23 16:05:18 +0900133bool ProximityInfo::isOnKey(const int keyId, const int x, const int y) const {
Jean Chalard081616c2012-03-22 17:39:27 +0900134 if (keyId < 0) return true; // NOT_A_ID is -1, but return whenever < 0 just in case
satoka70ee6e2012-03-07 15:12:22 +0900135 const int left = mKeyXCoordinates[keyId];
136 const int top = mKeyYCoordinates[keyId];
137 const int right = left + mKeyWidths[keyId] + 1;
138 const int bottom = top + mKeyHeights[keyId];
139 return left < right && top < bottom && x >= left && x < right && y >= top && y < bottom;
140}
141
satok9df4a452012-03-23 16:05:18 +0900142int ProximityInfo::squaredDistanceToEdge(const int keyId, const int x, const int y) const {
Jean Chalard081616c2012-03-22 17:39:27 +0900143 if (keyId < 0) return true; // NOT_A_ID is -1, but return whenever < 0 just in case
satoka70ee6e2012-03-07 15:12:22 +0900144 const int left = mKeyXCoordinates[keyId];
145 const int top = mKeyYCoordinates[keyId];
satok1caff472012-03-14 23:17:12 +0900146 const int right = left + mKeyWidths[keyId];
satoka70ee6e2012-03-07 15:12:22 +0900147 const int bottom = top + mKeyHeights[keyId];
148 const int edgeX = x < left ? left : (x > right ? right : x);
149 const int edgeY = y < top ? top : (y > bottom ? bottom : y);
150 const int dx = x - edgeX;
151 const int dy = y - edgeY;
152 return dx * dx + dy * dy;
153}
154
155void ProximityInfo::calculateNearbyKeyCodes(
satok9df4a452012-03-23 16:05:18 +0900156 const int x, const int y, const int32_t primaryKey, int *inputCodes) const {
satoka70ee6e2012-03-07 15:12:22 +0900157 int insertPos = 0;
158 inputCodes[insertPos++] = primaryKey;
159 const int startIndex = getStartIndexFromCoordinates(x, y);
Jean Chalard52612a02012-03-23 19:38:23 +0900160 if (startIndex >= 0) {
Jean Chalard88ec1252012-03-23 19:25:10 +0900161 for (int i = 0; i < MAX_PROXIMITY_CHARS_SIZE; ++i) {
162 const int32_t c = mProximityCharsArray[startIndex + i];
163 if (c < KEYCODE_SPACE || c == primaryKey) {
164 continue;
165 }
166 const int keyIndex = getKeyIndex(c);
167 const bool onKey = isOnKey(keyIndex, x, y);
168 const int distance = squaredDistanceToEdge(keyIndex, x, y);
169 if (onKey || distance < MOST_COMMON_KEY_WIDTH_SQUARE) {
170 inputCodes[insertPos++] = c;
171 if (insertPos >= MAX_PROXIMITY_CHARS_SIZE) {
172 if (DEBUG_DICT) {
173 assert(false);
174 }
175 return;
satok0cb20972012-03-13 22:07:56 +0900176 }
satoka70ee6e2012-03-07 15:12:22 +0900177 }
178 }
Jean Chalard88ec1252012-03-23 19:25:10 +0900179 const int additionalProximitySize =
180 AdditionalProximityChars::getAdditionalCharsSize(&mLocaleStr, primaryKey);
181 if (additionalProximitySize > 0) {
182 inputCodes[insertPos++] = ADDITIONAL_PROXIMITY_CHAR_DELIMITER_CODE;
183 if (insertPos >= MAX_PROXIMITY_CHARS_SIZE) {
Jean Chalard3094d122012-03-23 19:36:07 +0900184 if (DEBUG_DICT) {
185 assert(false);
186 }
187 return;
satok1caff472012-03-14 23:17:12 +0900188 }
satok1caff472012-03-14 23:17:12 +0900189
Jean Chalard88ec1252012-03-23 19:25:10 +0900190 const int32_t* additionalProximityChars =
191 AdditionalProximityChars::getAdditionalChars(&mLocaleStr, primaryKey);
192 for (int j = 0; j < additionalProximitySize; ++j) {
193 const int32_t ac = additionalProximityChars[j];
194 int k = 0;
195 for (; k < insertPos; ++k) {
196 if ((int)ac == inputCodes[k]) {
197 break;
198 }
satok5eec5742012-03-13 18:26:23 +0900199 }
Jean Chalard88ec1252012-03-23 19:25:10 +0900200 if (k < insertPos) {
201 continue;
satok0cb20972012-03-13 22:07:56 +0900202 }
Jean Chalard88ec1252012-03-23 19:25:10 +0900203 inputCodes[insertPos++] = ac;
204 if (insertPos >= MAX_PROXIMITY_CHARS_SIZE) {
205 if (DEBUG_DICT) {
206 assert(false);
207 }
208 return;
Jean Chalard3094d122012-03-23 19:36:07 +0900209 }
satok0cb20972012-03-13 22:07:56 +0900210 }
satok5eec5742012-03-13 18:26:23 +0900211 }
Jean Chalard52612a02012-03-23 19:38:23 +0900212 }
satok0cb20972012-03-13 22:07:56 +0900213 // Add a delimiter for the proximity characters
satok1caff472012-03-14 23:17:12 +0900214 for (int i = insertPos; i < MAX_PROXIMITY_CHARS_SIZE; ++i) {
215 inputCodes[i] = NOT_A_CODE;
216 }
satoka70ee6e2012-03-07 15:12:22 +0900217}
218
satok1caff472012-03-14 23:17:12 +0900219void ProximityInfo::setInputParams(const int32_t* inputCodes, const int inputLength,
Yusuke Nojima258bfe62011-09-28 12:59:43 +0900220 const int* xCoordinates, const int* yCoordinates) {
satok1caff472012-03-14 23:17:12 +0900221 memset(mInputCodes, 0,
222 MAX_WORD_LENGTH_INTERNAL * MAX_PROXIMITY_CHARS_SIZE * sizeof(mInputCodes[0]));
223
224 for (int i = 0; i < inputLength; ++i) {
satok9df4a452012-03-23 16:05:18 +0900225 const int32_t primaryKey = inputCodes[i];
satok1caff472012-03-14 23:17:12 +0900226 const int x = xCoordinates[i];
227 const int y = yCoordinates[i];
228 int *proximities = &mInputCodes[i * MAX_PROXIMITY_CHARS_SIZE];
229 calculateNearbyKeyCodes(x, y, primaryKey, proximities);
230 }
231
232 if (DEBUG_PROXIMITY_CHARS) {
233 for (int i = 0; i < inputLength; ++i) {
234 AKLOGI("---");
235 for (int j = 0; j < MAX_PROXIMITY_CHARS_SIZE; ++j) {
236 int icc = mInputCodes[i * MAX_PROXIMITY_CHARS_SIZE + j];
237 int icfjc = inputCodes[i * MAX_PROXIMITY_CHARS_SIZE + j];
238 icc+= 0;
239 icfjc += 0;
240 AKLOGI("--- (%d)%c,%c", i, icc, icfjc);
241 AKLOGI("--- A<%d>,B<%d>", icc, icfjc);
242 }
243 }
244 }
245 //Keep for debug, sorry
246 //for (int i = 0; i < MAX_WORD_LENGTH_INTERNAL * MAX_PROXIMITY_CHARS_SIZE; ++i) {
247 //if (i < inputLength * MAX_PROXIMITY_CHARS_SIZE) {
248 //mInputCodes[i] = mInputCodesFromJava[i];
249 //} else {
250 // mInputCodes[i] = 0;
251 // }
252 //}
Yusuke Nojima258bfe62011-09-28 12:59:43 +0900253 mInputXCoordinates = xCoordinates;
254 mInputYCoordinates = yCoordinates;
Yusuke Nojimaa4c1f1c2011-10-06 19:12:20 +0900255 mTouchPositionCorrectionEnabled =
256 HAS_TOUCH_POSITION_CORRECTION_DATA && xCoordinates && yCoordinates;
satok1d7eaf82011-07-13 10:32:02 +0900257 mInputLength = inputLength;
satok0cedd2b2011-08-12 01:05:27 +0900258 for (int i = 0; i < inputLength; ++i) {
259 mPrimaryInputWord[i] = getPrimaryCharAt(i);
260 }
261 mPrimaryInputWord[inputLength] = 0;
satok0cb20972012-03-13 22:07:56 +0900262 if (DEBUG_PROXIMITY_CHARS) {
263 AKLOGI("--- setInputParams");
264 }
Yusuke Nojimac25c7cc2011-10-03 16:44:29 +0900265 for (int i = 0; i < mInputLength; ++i) {
Yusuke Nojimaa4c1f1c2011-10-06 19:12:20 +0900266 const int *proximityChars = getProximityCharsAt(i);
satok0cb20972012-03-13 22:07:56 +0900267 const int primaryKey = proximityChars[0];
268 const int x = xCoordinates[i];
269 const int y = yCoordinates[i];
270 if (DEBUG_PROXIMITY_CHARS) {
271 int a = x + y + primaryKey;
272 a += 0;
273 AKLOGI("--- Primary = %c, x = %d, y = %d", primaryKey, x, y);
274 // Keep debug code just in case
275 //int proximities[50];
276 //for (int m = 0; m < 50; ++m) {
277 //proximities[m] = 0;
278 //}
279 //calculateNearbyKeyCodes(x, y, primaryKey, proximities);
280 //for (int l = 0; l < 50 && proximities[l] > 0; ++l) {
281 //if (DEBUG_PROXIMITY_CHARS) {
282 //AKLOGI("--- native Proximity (%d) = %c", l, proximities[l]);
283 //}
284 //}
285 }
Yusuke Nojimaa4c1f1c2011-10-06 19:12:20 +0900286 for (int j = 0; j < MAX_PROXIMITY_CHARS_SIZE && proximityChars[j] > 0; ++j) {
287 const int currentChar = proximityChars[j];
satok1caff472012-03-14 23:17:12 +0900288 const float squaredDistance = hasInputCoordinates()
289 ? calculateNormalizedSquaredDistance(getKeyIndex(currentChar), i)
290 : NOT_A_DISTANCE_FLOAT;
Yusuke Nojimaa4c1f1c2011-10-06 19:12:20 +0900291 if (squaredDistance >= 0.0f) {
292 mNormalizedSquaredDistances[i * MAX_PROXIMITY_CHARS_SIZE + j] =
293 (int)(squaredDistance * NORMALIZED_SQUARED_DISTANCE_SCALING_FACTOR);
294 } else {
295 mNormalizedSquaredDistances[i * MAX_PROXIMITY_CHARS_SIZE + j] = (j == 0)
296 ? EQUIVALENT_CHAR_WITHOUT_DISTANCE_INFO
297 : PROXIMITY_CHAR_WITHOUT_DISTANCE_INFO;
298 }
satok0cb20972012-03-13 22:07:56 +0900299 if (DEBUG_PROXIMITY_CHARS) {
300 AKLOGI("--- Proximity (%d) = %c", j, currentChar);
301 }
Yusuke Nojimae4ba8222011-10-05 14:55:07 +0900302 }
Yusuke Nojimac25c7cc2011-10-03 16:44:29 +0900303 }
satok1d7eaf82011-07-13 10:32:02 +0900304}
305
Yusuke Nojima16717152011-10-04 17:04:07 +0900306inline float square(const float x) { return x * x; }
307
Yusuke Nojimaa4c1f1c2011-10-06 19:12:20 +0900308float ProximityInfo::calculateNormalizedSquaredDistance(
309 const int keyIndex, const int inputIndex) const {
Jean Chalardbbc25602012-03-23 17:05:03 +0900310 if (keyIndex == NOT_AN_INDEX) {
Yusuke Nojimae4ba8222011-10-05 14:55:07 +0900311 return NOT_A_DISTANCE_FLOAT;
Yusuke Nojimac25c7cc2011-10-03 16:44:29 +0900312 }
Yusuke Nojimaa4c1f1c2011-10-06 19:12:20 +0900313 if (!hasSweetSpotData(keyIndex)) {
Yusuke Nojimae4ba8222011-10-05 14:55:07 +0900314 return NOT_A_DISTANCE_FLOAT;
Yusuke Nojima16717152011-10-04 17:04:07 +0900315 }
Jean Chalard0bfe3592012-01-25 18:11:26 +0900316 if (NOT_A_COORDINATE == mInputXCoordinates[inputIndex]) {
317 return NOT_A_DISTANCE_FLOAT;
318 }
Yusuke Nojimaa4c1f1c2011-10-06 19:12:20 +0900319 const float squaredDistance = calculateSquaredDistanceFromSweetSpotCenter(keyIndex, inputIndex);
320 const float squaredRadius = square(mSweetSpotRadii[keyIndex]);
Yusuke Nojimae4ba8222011-10-05 14:55:07 +0900321 return squaredDistance / squaredRadius;
Yusuke Nojima16717152011-10-04 17:04:07 +0900322}
323
satok1caff472012-03-14 23:17:12 +0900324bool ProximityInfo::hasInputCoordinates() const {
325 return mInputXCoordinates && mInputYCoordinates;
326}
327
Yusuke Nojimaa4c1f1c2011-10-06 19:12:20 +0900328int ProximityInfo::getKeyIndex(const int c) const {
satok1caff472012-03-14 23:17:12 +0900329 if (KEY_COUNT == 0) {
Yusuke Nojimaa4c1f1c2011-10-06 19:12:20 +0900330 // We do not have the coordinate data
Jean Chalardbbc25602012-03-23 17:05:03 +0900331 return NOT_AN_INDEX;
Yusuke Nojimaa4c1f1c2011-10-06 19:12:20 +0900332 }
Tadashi G. Takaoka6e3cb272011-11-11 14:26:13 +0900333 const unsigned short baseLowerC = toBaseLowerCase(c);
Yusuke Nojimaa4c1f1c2011-10-06 19:12:20 +0900334 if (baseLowerC > MAX_CHAR_CODE) {
Jean Chalardbbc25602012-03-23 17:05:03 +0900335 return NOT_AN_INDEX;
Yusuke Nojimaa4c1f1c2011-10-06 19:12:20 +0900336 }
337 return mCodeToKeyIndex[baseLowerC];
338}
339
Yusuke Nojimac25c7cc2011-10-03 16:44:29 +0900340float ProximityInfo::calculateSquaredDistanceFromSweetSpotCenter(
Yusuke Nojimaa4c1f1c2011-10-06 19:12:20 +0900341 const int keyIndex, const int inputIndex) const {
Yusuke Nojimac25c7cc2011-10-03 16:44:29 +0900342 const float sweetSpotCenterX = mSweetSpotCenterXs[keyIndex];
343 const float sweetSpotCenterY = mSweetSpotCenterYs[keyIndex];
344 const float inputX = (float)mInputXCoordinates[inputIndex];
345 const float inputY = (float)mInputYCoordinates[inputIndex];
346 return square(inputX - sweetSpotCenterX) + square(inputY - sweetSpotCenterY);
347}
348
satokd24df432011-07-14 15:43:42 +0900349inline const int* ProximityInfo::getProximityCharsAt(const int index) const {
satok1d7eaf82011-07-13 10:32:02 +0900350 return mInputCodes + (index * MAX_PROXIMITY_CHARS_SIZE);
351}
352
satokd24df432011-07-14 15:43:42 +0900353unsigned short ProximityInfo::getPrimaryCharAt(const int index) const {
354 return getProximityCharsAt(index)[0];
355}
356
satok2df30602011-07-15 13:49:00 +0900357inline bool ProximityInfo::existsCharInProximityAt(const int index, const int c) const {
satokd24df432011-07-14 15:43:42 +0900358 const int *chars = getProximityCharsAt(index);
359 int i = 0;
360 while (chars[i] > 0 && i < MAX_PROXIMITY_CHARS_SIZE) {
361 if (chars[i++] == c) {
362 return true;
363 }
364 }
365 return false;
366}
367
368bool ProximityInfo::existsAdjacentProximityChars(const int index) const {
369 if (index < 0 || index >= mInputLength) return false;
370 const int currentChar = getPrimaryCharAt(index);
371 const int leftIndex = index - 1;
372 if (leftIndex >= 0 && existsCharInProximityAt(leftIndex, currentChar)) {
373 return true;
374 }
375 const int rightIndex = index + 1;
376 if (rightIndex < mInputLength && existsCharInProximityAt(rightIndex, currentChar)) {
377 return true;
378 }
379 return false;
380}
381
382// In the following function, c is the current character of the dictionary word
383// currently examined.
384// currentChars is an array containing the keys close to the character the
385// user actually typed at the same position. We want to see if c is in it: if so,
386// then the word contains at that position a character close to what the user
387// typed.
388// What the user typed is actually the first character of the array.
Yusuke Nojimaa4c1f1c2011-10-06 19:12:20 +0900389// proximityIndex is a pointer to the variable where getMatchedProximityId returns
390// the index of c in the proximity chars of the input index.
satokd24df432011-07-14 15:43:42 +0900391// Notice : accented characters do not have a proximity list, so they are alone
392// in their list. The non-accented version of the character should be considered
393// "close", but not the other keys close to the non-accented version.
Yusuke Nojimaa4c1f1c2011-10-06 19:12:20 +0900394ProximityInfo::ProximityType ProximityInfo::getMatchedProximityId(const int index,
395 const unsigned short c, const bool checkProximityChars, int *proximityIndex) const {
satokd24df432011-07-14 15:43:42 +0900396 const int *currentChars = getProximityCharsAt(index);
Yusuke Nojima258bfe62011-09-28 12:59:43 +0900397 const int firstChar = currentChars[0];
Tadashi G. Takaoka6e3cb272011-11-11 14:26:13 +0900398 const unsigned short baseLowerC = toBaseLowerCase(c);
satokd24df432011-07-14 15:43:42 +0900399
400 // The first char in the array is what user typed. If it matches right away,
401 // that means the user typed that same char for this pos.
Yusuke Nojima258bfe62011-09-28 12:59:43 +0900402 if (firstChar == baseLowerC || firstChar == c) {
Yusuke Nojimae4ba8222011-10-05 14:55:07 +0900403 return EQUIVALENT_CHAR;
Yusuke Nojima258bfe62011-09-28 12:59:43 +0900404 }
satokd24df432011-07-14 15:43:42 +0900405
satok985312e2011-08-05 21:21:01 +0900406 if (!checkProximityChars) return UNRELATED_CHAR;
satokd24df432011-07-14 15:43:42 +0900407
408 // If the non-accented, lowercased version of that first character matches c,
409 // then we have a non-accented version of the accented character the user
410 // typed. Treat it as a close char.
Tadashi G. Takaoka6e3cb272011-11-11 14:26:13 +0900411 if (toBaseLowerCase(firstChar) == baseLowerC)
satokd24df432011-07-14 15:43:42 +0900412 return NEAR_PROXIMITY_CHAR;
413
414 // Not an exact nor an accent-alike match: search the list of close keys
415 int j = 1;
satoke05b3f42012-01-31 17:15:43 +0900416 while (j < MAX_PROXIMITY_CHARS_SIZE
417 && currentChars[j] > ADDITIONAL_PROXIMITY_CHAR_DELIMITER_CODE) {
satokd24df432011-07-14 15:43:42 +0900418 const bool matched = (currentChars[j] == baseLowerC || currentChars[j] == c);
Yusuke Nojimaa4c1f1c2011-10-06 19:12:20 +0900419 if (matched) {
420 if (proximityIndex) {
421 *proximityIndex = j;
422 }
423 return NEAR_PROXIMITY_CHAR;
424 }
satokd24df432011-07-14 15:43:42 +0900425 ++j;
426 }
satoke05b3f42012-01-31 17:15:43 +0900427 if (j < MAX_PROXIMITY_CHARS_SIZE
428 && currentChars[j] == ADDITIONAL_PROXIMITY_CHAR_DELIMITER_CODE) {
429 ++j;
430 while (j < MAX_PROXIMITY_CHARS_SIZE
431 && currentChars[j] > ADDITIONAL_PROXIMITY_CHAR_DELIMITER_CODE) {
432 const bool matched = (currentChars[j] == baseLowerC || currentChars[j] == c);
433 if (matched) {
434 if (proximityIndex) {
435 *proximityIndex = j;
436 }
437 return ADDITIONAL_PROXIMITY_CHAR;
438 }
439 ++j;
440 }
441 }
satokd24df432011-07-14 15:43:42 +0900442
443 // Was not included, signal this as an unrelated character.
444 return UNRELATED_CHAR;
445}
446
satok1d7eaf82011-07-13 10:32:02 +0900447bool ProximityInfo::sameAsTyped(const unsigned short *word, int length) const {
448 if (length != mInputLength) {
449 return false;
450 }
451 const int *inputCodes = mInputCodes;
452 while (length--) {
453 if ((unsigned int) *inputCodes != (unsigned int) *word) {
454 return false;
455 }
456 inputCodes += MAX_PROXIMITY_CHARS_SIZE;
457 word++;
458 }
459 return true;
460}
461
Yusuke Nojimae4ba8222011-10-05 14:55:07 +0900462const int ProximityInfo::NORMALIZED_SQUARED_DISTANCE_SCALING_FACTOR_LOG_2;
463const int ProximityInfo::NORMALIZED_SQUARED_DISTANCE_SCALING_FACTOR;
Yusuke Nojima0e1f6562011-09-21 12:02:47 +0900464const int ProximityInfo::MAX_KEY_COUNT_IN_A_KEYBOARD;
Yusuke Nojimaad358352011-09-29 16:44:54 +0900465const int ProximityInfo::MAX_CHAR_CODE;
Yusuke Nojima0e1f6562011-09-21 12:02:47 +0900466
Ken Wakasace9e52a2011-06-18 13:09:55 +0900467} // namespace latinime