blob: a4a641160b6d6f7e0e75e06c955390e8bcfbedae [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>
Satoshi Kataokaefb63242012-06-27 14:52:40 +090018#include <math.h>
satok8fbd5522011-02-22 17:28:55 +090019#include <stdio.h>
satok552c3c22012-03-13 16:33:47 +090020#include <string>
satok8fbd5522011-02-22 17:28:55 +090021
satok817e5172011-03-04 06:06:45 -080022#define LOG_TAG "LatinIME: proximity_info.cpp"
23
satok552c3c22012-03-13 16:33:47 +090024#include "additional_proximity_chars.h"
Ken Wakasa3b088a22012-05-16 23:05:32 +090025#include "defines.h"
satokd24df432011-07-14 15:43:42 +090026#include "dictionary.h"
satok8fbd5522011-02-22 17:28:55 +090027#include "proximity_info.h"
Satoshi Kataoka3e8c58f2012-06-05 17:55:52 +090028#include "proximity_info_state.h"
satok8fbd5522011-02-22 17:28:55 +090029
30namespace latinime {
Ken Wakasace9e52a2011-06-18 13:09:55 +090031
Yusuke Nojimade2f8422011-09-27 11:15:18 +090032inline void copyOrFillZero(void *to, const void *from, size_t size) {
33 if (from) {
34 memcpy(to, from, size);
35 } else {
36 memset(to, 0, size);
37 }
38}
39
satok552c3c22012-03-13 16:33:47 +090040ProximityInfo::ProximityInfo(const std::string localeStr, const int maxProximityCharsSize,
41 const int keyboardWidth, const int keyboardHeight, const int gridWidth,
42 const int gridHeight, const int mostCommonKeyWidth,
satok0cb20972012-03-13 22:07:56 +090043 const int32_t *proximityCharsArray, const int keyCount, const int32_t *keyXCoordinates,
Yusuke Nojima0e1f6562011-09-21 12:02:47 +090044 const int32_t *keyYCoordinates, const int32_t *keyWidths, const int32_t *keyHeights,
Yusuke Nojimaad358352011-09-29 16:44:54 +090045 const int32_t *keyCharCodes, const float *sweetSpotCenterXs, const float *sweetSpotCenterYs,
46 const float *sweetSpotRadii)
satok817e5172011-03-04 06:06:45 -080047 : MAX_PROXIMITY_CHARS_SIZE(maxProximityCharsSize), KEYBOARD_WIDTH(keyboardWidth),
48 KEYBOARD_HEIGHT(keyboardHeight), GRID_WIDTH(gridWidth), GRID_HEIGHT(gridHeight),
satoka70ee6e2012-03-07 15:12:22 +090049 MOST_COMMON_KEY_WIDTH_SQUARE(mostCommonKeyWidth * mostCommonKeyWidth),
satok817e5172011-03-04 06:06:45 -080050 CELL_WIDTH((keyboardWidth + gridWidth - 1) / gridWidth),
Yusuke Nojima0e1f6562011-09-21 12:02:47 +090051 CELL_HEIGHT((keyboardHeight + gridHeight - 1) / gridHeight),
Yusuke Nojima258bfe62011-09-28 12:59:43 +090052 KEY_COUNT(min(keyCount, MAX_KEY_COUNT_IN_A_KEYBOARD)),
Yusuke Nojimaa4c1f1c2011-10-06 19:12:20 +090053 HAS_TOUCH_POSITION_CORRECTION_DATA(keyCount > 0 && keyXCoordinates && keyYCoordinates
54 && keyWidths && keyHeights && keyCharCodes && sweetSpotCenterXs
55 && sweetSpotCenterYs && sweetSpotRadii),
Satoshi Kataoka3e8c58f2012-06-05 17:55:52 +090056 mLocaleStr(localeStr) {
satok1035bc92012-06-13 16:07:54 -070057 const int proximityGridLength = GRID_WIDTH * GRID_HEIGHT * MAX_PROXIMITY_CHARS_SIZE;
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 }
Satoshi Kataoka3e8c58f2012-06-05 17:55:52 +090061 mProximityCharsArray = new int32_t[proximityGridLength];
Jean Chalard8c8ca592011-11-10 12:07:30 +090062 memcpy(mProximityCharsArray, proximityCharsArray,
63 proximityGridLength * sizeof(mProximityCharsArray[0]));
Yusuke Nojima0e1f6562011-09-21 12:02:47 +090064
Yusuke Nojimade2f8422011-09-27 11:15:18 +090065 copyOrFillZero(mKeyXCoordinates, keyXCoordinates, KEY_COUNT * sizeof(mKeyXCoordinates[0]));
66 copyOrFillZero(mKeyYCoordinates, keyYCoordinates, KEY_COUNT * sizeof(mKeyYCoordinates[0]));
67 copyOrFillZero(mKeyWidths, keyWidths, KEY_COUNT * sizeof(mKeyWidths[0]));
68 copyOrFillZero(mKeyHeights, keyHeights, KEY_COUNT * sizeof(mKeyHeights[0]));
69 copyOrFillZero(mKeyCharCodes, keyCharCodes, KEY_COUNT * sizeof(mKeyCharCodes[0]));
Yusuke Nojimaad358352011-09-29 16:44:54 +090070 copyOrFillZero(mSweetSpotCenterXs, sweetSpotCenterXs,
71 KEY_COUNT * sizeof(mSweetSpotCenterXs[0]));
72 copyOrFillZero(mSweetSpotCenterYs, sweetSpotCenterYs,
73 KEY_COUNT * sizeof(mSweetSpotCenterYs[0]));
74 copyOrFillZero(mSweetSpotRadii, sweetSpotRadii, KEY_COUNT * sizeof(mSweetSpotRadii[0]));
Yusuke Nojima0e1f6562011-09-21 12:02:47 +090075
Yusuke Nojima0e1f6562011-09-21 12:02:47 +090076 initializeCodeToKeyIndex();
77}
78
Yusuke Nojima0e1f6562011-09-21 12:02:47 +090079// Build the reversed look up table from the char code to the index in mKeyXCoordinates,
80// mKeyYCoordinates, mKeyWidths, mKeyHeights, mKeyCharCodes.
81void ProximityInfo::initializeCodeToKeyIndex() {
Yusuke Nojimaad358352011-09-29 16:44:54 +090082 memset(mCodeToKeyIndex, -1, (MAX_CHAR_CODE + 1) * sizeof(mCodeToKeyIndex[0]));
Yusuke Nojima0e1f6562011-09-21 12:02:47 +090083 for (int i = 0; i < KEY_COUNT; ++i) {
84 const int code = mKeyCharCodes[i];
Yusuke Nojimaad358352011-09-29 16:44:54 +090085 if (0 <= code && code <= MAX_CHAR_CODE) {
Yusuke Nojima0e1f6562011-09-21 12:02:47 +090086 mCodeToKeyIndex[code] = i;
Yusuke Nojimaad358352011-09-29 16:44:54 +090087 }
Yusuke Nojima0e1f6562011-09-21 12:02:47 +090088 }
satok8fbd5522011-02-22 17:28:55 +090089}
90
91ProximityInfo::~ProximityInfo() {
92 delete[] mProximityCharsArray;
93}
satok817e5172011-03-04 06:06:45 -080094
95inline int ProximityInfo::getStartIndexFromCoordinates(const int x, const int y) const {
satok3c4bb772011-03-04 22:50:19 -080096 return ((y / CELL_HEIGHT) * GRID_WIDTH + (x / CELL_WIDTH))
satok817e5172011-03-04 06:06:45 -080097 * MAX_PROXIMITY_CHARS_SIZE;
satok8fbd5522011-02-22 17:28:55 +090098}
satok817e5172011-03-04 06:06:45 -080099
100bool ProximityInfo::hasSpaceProximity(const int x, const int y) const {
satok744dab62011-12-15 22:29:05 +0900101 if (x < 0 || y < 0) {
102 if (DEBUG_DICT) {
satok9fb6f472012-01-13 18:01:22 +0900103 AKLOGI("HasSpaceProximity: Illegal coordinates (%d, %d)", x, y);
satok1a6da632011-12-16 23:15:06 +0900104 assert(false);
satok744dab62011-12-15 22:29:05 +0900105 }
106 return false;
107 }
108
satok817e5172011-03-04 06:06:45 -0800109 const int startIndex = getStartIndexFromCoordinates(x, y);
110 if (DEBUG_PROXIMITY_INFO) {
satok9fb6f472012-01-13 18:01:22 +0900111 AKLOGI("hasSpaceProximity: index %d, %d, %d", startIndex, x, y);
satok817e5172011-03-04 06:06:45 -0800112 }
Satoshi Kataoka3e8c58f2012-06-05 17:55:52 +0900113 int32_t* proximityCharsArray = mProximityCharsArray;
satok817e5172011-03-04 06:06:45 -0800114 for (int i = 0; i < MAX_PROXIMITY_CHARS_SIZE; ++i) {
115 if (DEBUG_PROXIMITY_INFO) {
satok9fb6f472012-01-13 18:01:22 +0900116 AKLOGI("Index: %d", mProximityCharsArray[startIndex + i]);
satok817e5172011-03-04 06:06:45 -0800117 }
Satoshi Kataoka3e8c58f2012-06-05 17:55:52 +0900118 if (proximityCharsArray[startIndex + i] == KEYCODE_SPACE) {
satok817e5172011-03-04 06:06:45 -0800119 return true;
120 }
121 }
122 return false;
123}
Ken Wakasace9e52a2011-06-18 13:09:55 +0900124
satok9df4a452012-03-23 16:05:18 +0900125int ProximityInfo::squaredDistanceToEdge(const int keyId, const int x, const int y) const {
Jean Chalard081616c2012-03-22 17:39:27 +0900126 if (keyId < 0) return true; // NOT_A_ID is -1, but return whenever < 0 just in case
satoka70ee6e2012-03-07 15:12:22 +0900127 const int left = mKeyXCoordinates[keyId];
128 const int top = mKeyYCoordinates[keyId];
satok1caff472012-03-14 23:17:12 +0900129 const int right = left + mKeyWidths[keyId];
satoka70ee6e2012-03-07 15:12:22 +0900130 const int bottom = top + mKeyHeights[keyId];
131 const int edgeX = x < left ? left : (x > right ? right : x);
132 const int edgeY = y < top ? top : (y > bottom ? bottom : y);
133 const int dx = x - edgeX;
134 const int dy = y - edgeY;
135 return dx * dx + dy * dy;
136}
137
138void ProximityInfo::calculateNearbyKeyCodes(
satok9df4a452012-03-23 16:05:18 +0900139 const int x, const int y, const int32_t primaryKey, int *inputCodes) const {
Satoshi Kataoka3e8c58f2012-06-05 17:55:52 +0900140 int32_t *proximityCharsArray = mProximityCharsArray;
satoka70ee6e2012-03-07 15:12:22 +0900141 int insertPos = 0;
142 inputCodes[insertPos++] = primaryKey;
143 const int startIndex = getStartIndexFromCoordinates(x, y);
Jean Chalard52612a02012-03-23 19:38:23 +0900144 if (startIndex >= 0) {
Jean Chalard88ec1252012-03-23 19:25:10 +0900145 for (int i = 0; i < MAX_PROXIMITY_CHARS_SIZE; ++i) {
Satoshi Kataoka3e8c58f2012-06-05 17:55:52 +0900146 const int32_t c = proximityCharsArray[startIndex + i];
Jean Chalard88ec1252012-03-23 19:25:10 +0900147 if (c < KEYCODE_SPACE || c == primaryKey) {
148 continue;
149 }
150 const int keyIndex = getKeyIndex(c);
151 const bool onKey = isOnKey(keyIndex, x, y);
152 const int distance = squaredDistanceToEdge(keyIndex, x, y);
153 if (onKey || distance < MOST_COMMON_KEY_WIDTH_SQUARE) {
154 inputCodes[insertPos++] = c;
155 if (insertPos >= MAX_PROXIMITY_CHARS_SIZE) {
156 if (DEBUG_DICT) {
157 assert(false);
158 }
159 return;
satok0cb20972012-03-13 22:07:56 +0900160 }
satoka70ee6e2012-03-07 15:12:22 +0900161 }
162 }
Jean Chalard88ec1252012-03-23 19:25:10 +0900163 const int additionalProximitySize =
164 AdditionalProximityChars::getAdditionalCharsSize(&mLocaleStr, primaryKey);
165 if (additionalProximitySize > 0) {
166 inputCodes[insertPos++] = ADDITIONAL_PROXIMITY_CHAR_DELIMITER_CODE;
167 if (insertPos >= MAX_PROXIMITY_CHARS_SIZE) {
Jean Chalard3094d122012-03-23 19:36:07 +0900168 if (DEBUG_DICT) {
169 assert(false);
170 }
171 return;
satok1caff472012-03-14 23:17:12 +0900172 }
satok1caff472012-03-14 23:17:12 +0900173
Jean Chalard88ec1252012-03-23 19:25:10 +0900174 const int32_t* additionalProximityChars =
175 AdditionalProximityChars::getAdditionalChars(&mLocaleStr, primaryKey);
176 for (int j = 0; j < additionalProximitySize; ++j) {
177 const int32_t ac = additionalProximityChars[j];
178 int k = 0;
179 for (; k < insertPos; ++k) {
180 if ((int)ac == inputCodes[k]) {
181 break;
182 }
satok5eec5742012-03-13 18:26:23 +0900183 }
Jean Chalard88ec1252012-03-23 19:25:10 +0900184 if (k < insertPos) {
185 continue;
satok0cb20972012-03-13 22:07:56 +0900186 }
Jean Chalard88ec1252012-03-23 19:25:10 +0900187 inputCodes[insertPos++] = ac;
188 if (insertPos >= MAX_PROXIMITY_CHARS_SIZE) {
189 if (DEBUG_DICT) {
190 assert(false);
191 }
192 return;
Jean Chalard3094d122012-03-23 19:36:07 +0900193 }
satok0cb20972012-03-13 22:07:56 +0900194 }
satok5eec5742012-03-13 18:26:23 +0900195 }
Jean Chalard52612a02012-03-23 19:38:23 +0900196 }
satok0cb20972012-03-13 22:07:56 +0900197 // Add a delimiter for the proximity characters
satok1caff472012-03-14 23:17:12 +0900198 for (int i = insertPos; i < MAX_PROXIMITY_CHARS_SIZE; ++i) {
199 inputCodes[i] = NOT_A_CODE;
200 }
satoka70ee6e2012-03-07 15:12:22 +0900201}
202
Yusuke Nojimaa4c1f1c2011-10-06 19:12:20 +0900203int ProximityInfo::getKeyIndex(const int c) const {
satok1caff472012-03-14 23:17:12 +0900204 if (KEY_COUNT == 0) {
Yusuke Nojimaa4c1f1c2011-10-06 19:12:20 +0900205 // We do not have the coordinate data
Jean Chalardbbc25602012-03-23 17:05:03 +0900206 return NOT_AN_INDEX;
Yusuke Nojimaa4c1f1c2011-10-06 19:12:20 +0900207 }
Tadashi G. Takaoka6e3cb272011-11-11 14:26:13 +0900208 const unsigned short baseLowerC = toBaseLowerCase(c);
Yusuke Nojimaa4c1f1c2011-10-06 19:12:20 +0900209 if (baseLowerC > MAX_CHAR_CODE) {
Jean Chalardbbc25602012-03-23 17:05:03 +0900210 return NOT_AN_INDEX;
Yusuke Nojimaa4c1f1c2011-10-06 19:12:20 +0900211 }
212 return mCodeToKeyIndex[baseLowerC];
213}
Satoshi Kataokaefb63242012-06-27 14:52:40 +0900214
215// TODO: [Staging] Optimize
216void ProximityInfo::getCenters(int *centerXs, int *centerYs, int *codeToKeyIndex,
217 int *keyToCodeIndex, int *keyCount, int *keyWidth) const {
218 *keyCount = KEY_COUNT;
219 *keyWidth = sqrt((float)MOST_COMMON_KEY_WIDTH_SQUARE);
220
221 for (int i = 0; i < KEY_COUNT; ++i) {
222 const int code = mKeyCharCodes[i];
223 const int lowerCode = toBaseLowerCase(code);
224 centerXs[i] = mKeyXCoordinates[i] + mKeyWidths[i] / 2;
225 centerYs[i] = mKeyYCoordinates[i] + mKeyHeights[i] / 2;
226 codeToKeyIndex[code] = i;
227 if (code != lowerCode && lowerCode >= 0 && lowerCode <= MAX_CHAR_CODE) {
228 codeToKeyIndex[lowerCode] = i;
229 keyToCodeIndex[i] = lowerCode;
230 } else {
231 keyToCodeIndex[i] = code;
232 }
233 }
234}
Ken Wakasace9e52a2011-06-18 13:09:55 +0900235} // namespace latinime