blob: 39b91d76fc382994e5b3814b1d0c1c958023504d [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"
Satoshi Kataoka3e8c58f2012-06-05 17:55:52 +090027#include "proximity_info_state.h"
satok8fbd5522011-02-22 17:28:55 +090028
29namespace latinime {
Ken Wakasace9e52a2011-06-18 13:09:55 +090030
Yusuke Nojimade2f8422011-09-27 11:15:18 +090031inline void copyOrFillZero(void *to, const void *from, size_t size) {
32 if (from) {
33 memcpy(to, from, size);
34 } else {
35 memset(to, 0, size);
36 }
37}
38
satok552c3c22012-03-13 16:33:47 +090039ProximityInfo::ProximityInfo(const std::string localeStr, const int maxProximityCharsSize,
40 const int keyboardWidth, const int keyboardHeight, const int gridWidth,
41 const int gridHeight, const int mostCommonKeyWidth,
satok0cb20972012-03-13 22:07:56 +090042 const int32_t *proximityCharsArray, const int keyCount, const int32_t *keyXCoordinates,
Yusuke Nojima0e1f6562011-09-21 12:02:47 +090043 const int32_t *keyYCoordinates, const int32_t *keyWidths, const int32_t *keyHeights,
Yusuke Nojimaad358352011-09-29 16:44:54 +090044 const int32_t *keyCharCodes, const float *sweetSpotCenterXs, const float *sweetSpotCenterYs,
45 const float *sweetSpotRadii)
satok817e5172011-03-04 06:06:45 -080046 : MAX_PROXIMITY_CHARS_SIZE(maxProximityCharsSize), KEYBOARD_WIDTH(keyboardWidth),
47 KEYBOARD_HEIGHT(keyboardHeight), GRID_WIDTH(gridWidth), GRID_HEIGHT(gridHeight),
satoka70ee6e2012-03-07 15:12:22 +090048 MOST_COMMON_KEY_WIDTH_SQUARE(mostCommonKeyWidth * mostCommonKeyWidth),
satok817e5172011-03-04 06:06:45 -080049 CELL_WIDTH((keyboardWidth + gridWidth - 1) / gridWidth),
Yusuke Nojima0e1f6562011-09-21 12:02:47 +090050 CELL_HEIGHT((keyboardHeight + gridHeight - 1) / gridHeight),
Yusuke Nojima258bfe62011-09-28 12:59:43 +090051 KEY_COUNT(min(keyCount, MAX_KEY_COUNT_IN_A_KEYBOARD)),
Yusuke Nojimaa4c1f1c2011-10-06 19:12:20 +090052 HAS_TOUCH_POSITION_CORRECTION_DATA(keyCount > 0 && keyXCoordinates && keyYCoordinates
53 && keyWidths && keyHeights && keyCharCodes && sweetSpotCenterXs
54 && sweetSpotCenterYs && sweetSpotRadii),
Satoshi Kataoka3e8c58f2012-06-05 17:55:52 +090055 mLocaleStr(localeStr) {
satok817e5172011-03-04 06:06:45 -080056 if (DEBUG_PROXIMITY_INFO) {
satok9fb6f472012-01-13 18:01:22 +090057 AKLOGI("Create proximity info array %d", proximityGridLength);
satok817e5172011-03-04 06:06:45 -080058 }
Satoshi Kataoka3e8c58f2012-06-05 17:55:52 +090059 const int proximityGridLength = GRID_WIDTH * GRID_HEIGHT * MAX_PROXIMITY_CHARS_SIZE;
60 mProximityCharsArray = new int32_t[proximityGridLength];
Jean Chalard8c8ca592011-11-10 12:07:30 +090061 memcpy(mProximityCharsArray, proximityCharsArray,
62 proximityGridLength * sizeof(mProximityCharsArray[0]));
Yusuke Nojima0e1f6562011-09-21 12:02:47 +090063
Yusuke Nojimade2f8422011-09-27 11:15:18 +090064 copyOrFillZero(mKeyXCoordinates, keyXCoordinates, KEY_COUNT * sizeof(mKeyXCoordinates[0]));
65 copyOrFillZero(mKeyYCoordinates, keyYCoordinates, KEY_COUNT * sizeof(mKeyYCoordinates[0]));
66 copyOrFillZero(mKeyWidths, keyWidths, KEY_COUNT * sizeof(mKeyWidths[0]));
67 copyOrFillZero(mKeyHeights, keyHeights, KEY_COUNT * sizeof(mKeyHeights[0]));
68 copyOrFillZero(mKeyCharCodes, keyCharCodes, KEY_COUNT * sizeof(mKeyCharCodes[0]));
Yusuke Nojimaad358352011-09-29 16:44:54 +090069 copyOrFillZero(mSweetSpotCenterXs, sweetSpotCenterXs,
70 KEY_COUNT * sizeof(mSweetSpotCenterXs[0]));
71 copyOrFillZero(mSweetSpotCenterYs, sweetSpotCenterYs,
72 KEY_COUNT * sizeof(mSweetSpotCenterYs[0]));
73 copyOrFillZero(mSweetSpotRadii, sweetSpotRadii, KEY_COUNT * sizeof(mSweetSpotRadii[0]));
Yusuke Nojima0e1f6562011-09-21 12:02:47 +090074
Yusuke Nojima0e1f6562011-09-21 12:02:47 +090075 initializeCodeToKeyIndex();
Satoshi Kataoka3e8c58f2012-06-05 17:55:52 +090076 mProximityInfoState = new ProximityInfoState(this, MAX_PROXIMITY_CHARS_SIZE,
77 HAS_TOUCH_POSITION_CORRECTION_DATA, MOST_COMMON_KEY_WIDTH_SQUARE, mLocaleStr,
78 KEY_COUNT, CELL_HEIGHT, CELL_WIDTH, GRID_WIDTH, GRID_HEIGHT);
Yusuke Nojima0e1f6562011-09-21 12:02:47 +090079}
80
Yusuke Nojima0e1f6562011-09-21 12:02:47 +090081// Build the reversed look up table from the char code to the index in mKeyXCoordinates,
82// mKeyYCoordinates, mKeyWidths, mKeyHeights, mKeyCharCodes.
83void ProximityInfo::initializeCodeToKeyIndex() {
Yusuke Nojimaad358352011-09-29 16:44:54 +090084 memset(mCodeToKeyIndex, -1, (MAX_CHAR_CODE + 1) * sizeof(mCodeToKeyIndex[0]));
Yusuke Nojima0e1f6562011-09-21 12:02:47 +090085 for (int i = 0; i < KEY_COUNT; ++i) {
86 const int code = mKeyCharCodes[i];
Yusuke Nojimaad358352011-09-29 16:44:54 +090087 if (0 <= code && code <= MAX_CHAR_CODE) {
Yusuke Nojima0e1f6562011-09-21 12:02:47 +090088 mCodeToKeyIndex[code] = i;
Yusuke Nojimaad358352011-09-29 16:44:54 +090089 }
Yusuke Nojima0e1f6562011-09-21 12:02:47 +090090 }
satok8fbd5522011-02-22 17:28:55 +090091}
92
93ProximityInfo::~ProximityInfo() {
94 delete[] mProximityCharsArray;
Satoshi Kataoka3e8c58f2012-06-05 17:55:52 +090095 delete mProximityInfoState;
satok8fbd5522011-02-22 17:28:55 +090096}
satok817e5172011-03-04 06:06:45 -080097
98inline int ProximityInfo::getStartIndexFromCoordinates(const int x, const int y) const {
satok3c4bb772011-03-04 22:50:19 -080099 return ((y / CELL_HEIGHT) * GRID_WIDTH + (x / CELL_WIDTH))
satok817e5172011-03-04 06:06:45 -0800100 * MAX_PROXIMITY_CHARS_SIZE;
satok8fbd5522011-02-22 17:28:55 +0900101}
satok817e5172011-03-04 06:06:45 -0800102
103bool ProximityInfo::hasSpaceProximity(const int x, const int y) const {
satok744dab62011-12-15 22:29:05 +0900104 if (x < 0 || y < 0) {
105 if (DEBUG_DICT) {
satok9fb6f472012-01-13 18:01:22 +0900106 AKLOGI("HasSpaceProximity: Illegal coordinates (%d, %d)", x, y);
satok1a6da632011-12-16 23:15:06 +0900107 assert(false);
satok744dab62011-12-15 22:29:05 +0900108 }
109 return false;
110 }
111
satok817e5172011-03-04 06:06:45 -0800112 const int startIndex = getStartIndexFromCoordinates(x, y);
113 if (DEBUG_PROXIMITY_INFO) {
satok9fb6f472012-01-13 18:01:22 +0900114 AKLOGI("hasSpaceProximity: index %d, %d, %d", startIndex, x, y);
satok817e5172011-03-04 06:06:45 -0800115 }
Satoshi Kataoka3e8c58f2012-06-05 17:55:52 +0900116 int32_t* proximityCharsArray = mProximityCharsArray;
satok817e5172011-03-04 06:06:45 -0800117 for (int i = 0; i < MAX_PROXIMITY_CHARS_SIZE; ++i) {
118 if (DEBUG_PROXIMITY_INFO) {
satok9fb6f472012-01-13 18:01:22 +0900119 AKLOGI("Index: %d", mProximityCharsArray[startIndex + i]);
satok817e5172011-03-04 06:06:45 -0800120 }
Satoshi Kataoka3e8c58f2012-06-05 17:55:52 +0900121 if (proximityCharsArray[startIndex + i] == KEYCODE_SPACE) {
satok817e5172011-03-04 06:06:45 -0800122 return true;
123 }
124 }
125 return false;
126}
Ken Wakasace9e52a2011-06-18 13:09:55 +0900127
satok9df4a452012-03-23 16:05:18 +0900128int ProximityInfo::squaredDistanceToEdge(const int keyId, const int x, const int y) const {
Jean Chalard081616c2012-03-22 17:39:27 +0900129 if (keyId < 0) return true; // NOT_A_ID is -1, but return whenever < 0 just in case
satoka70ee6e2012-03-07 15:12:22 +0900130 const int left = mKeyXCoordinates[keyId];
131 const int top = mKeyYCoordinates[keyId];
satok1caff472012-03-14 23:17:12 +0900132 const int right = left + mKeyWidths[keyId];
satoka70ee6e2012-03-07 15:12:22 +0900133 const int bottom = top + mKeyHeights[keyId];
134 const int edgeX = x < left ? left : (x > right ? right : x);
135 const int edgeY = y < top ? top : (y > bottom ? bottom : y);
136 const int dx = x - edgeX;
137 const int dy = y - edgeY;
138 return dx * dx + dy * dy;
139}
140
141void ProximityInfo::calculateNearbyKeyCodes(
satok9df4a452012-03-23 16:05:18 +0900142 const int x, const int y, const int32_t primaryKey, int *inputCodes) const {
Satoshi Kataoka3e8c58f2012-06-05 17:55:52 +0900143 int32_t *proximityCharsArray = mProximityCharsArray;
satoka70ee6e2012-03-07 15:12:22 +0900144 int insertPos = 0;
145 inputCodes[insertPos++] = primaryKey;
146 const int startIndex = getStartIndexFromCoordinates(x, y);
Jean Chalard52612a02012-03-23 19:38:23 +0900147 if (startIndex >= 0) {
Jean Chalard88ec1252012-03-23 19:25:10 +0900148 for (int i = 0; i < MAX_PROXIMITY_CHARS_SIZE; ++i) {
Satoshi Kataoka3e8c58f2012-06-05 17:55:52 +0900149 const int32_t c = proximityCharsArray[startIndex + i];
Jean Chalard88ec1252012-03-23 19:25:10 +0900150 if (c < KEYCODE_SPACE || c == primaryKey) {
151 continue;
152 }
153 const int keyIndex = getKeyIndex(c);
154 const bool onKey = isOnKey(keyIndex, x, y);
155 const int distance = squaredDistanceToEdge(keyIndex, x, y);
156 if (onKey || distance < MOST_COMMON_KEY_WIDTH_SQUARE) {
157 inputCodes[insertPos++] = c;
158 if (insertPos >= MAX_PROXIMITY_CHARS_SIZE) {
159 if (DEBUG_DICT) {
160 assert(false);
161 }
162 return;
satok0cb20972012-03-13 22:07:56 +0900163 }
satoka70ee6e2012-03-07 15:12:22 +0900164 }
165 }
Jean Chalard88ec1252012-03-23 19:25:10 +0900166 const int additionalProximitySize =
167 AdditionalProximityChars::getAdditionalCharsSize(&mLocaleStr, primaryKey);
168 if (additionalProximitySize > 0) {
169 inputCodes[insertPos++] = ADDITIONAL_PROXIMITY_CHAR_DELIMITER_CODE;
170 if (insertPos >= MAX_PROXIMITY_CHARS_SIZE) {
Jean Chalard3094d122012-03-23 19:36:07 +0900171 if (DEBUG_DICT) {
172 assert(false);
173 }
174 return;
satok1caff472012-03-14 23:17:12 +0900175 }
satok1caff472012-03-14 23:17:12 +0900176
Jean Chalard88ec1252012-03-23 19:25:10 +0900177 const int32_t* additionalProximityChars =
178 AdditionalProximityChars::getAdditionalChars(&mLocaleStr, primaryKey);
179 for (int j = 0; j < additionalProximitySize; ++j) {
180 const int32_t ac = additionalProximityChars[j];
181 int k = 0;
182 for (; k < insertPos; ++k) {
183 if ((int)ac == inputCodes[k]) {
184 break;
185 }
satok5eec5742012-03-13 18:26:23 +0900186 }
Jean Chalard88ec1252012-03-23 19:25:10 +0900187 if (k < insertPos) {
188 continue;
satok0cb20972012-03-13 22:07:56 +0900189 }
Jean Chalard88ec1252012-03-23 19:25:10 +0900190 inputCodes[insertPos++] = ac;
191 if (insertPos >= MAX_PROXIMITY_CHARS_SIZE) {
192 if (DEBUG_DICT) {
193 assert(false);
194 }
195 return;
Jean Chalard3094d122012-03-23 19:36:07 +0900196 }
satok0cb20972012-03-13 22:07:56 +0900197 }
satok5eec5742012-03-13 18:26:23 +0900198 }
Jean Chalard52612a02012-03-23 19:38:23 +0900199 }
satok0cb20972012-03-13 22:07:56 +0900200 // Add a delimiter for the proximity characters
satok1caff472012-03-14 23:17:12 +0900201 for (int i = insertPos; i < MAX_PROXIMITY_CHARS_SIZE; ++i) {
202 inputCodes[i] = NOT_A_CODE;
203 }
satoka70ee6e2012-03-07 15:12:22 +0900204}
205
Satoshi Kataoka3e8c58f2012-06-05 17:55:52 +0900206// TODO: remove
207void ProximityInfo::initInputParams(const int32_t *inputCodes, const int inputLength,
208 const int *xCoordinates, const int *yCoordinates) {
209 mProximityInfoState->initInputParams(inputCodes, inputLength, xCoordinates, yCoordinates);
satok1caff472012-03-14 23:17:12 +0900210}
211
Yusuke Nojimaa4c1f1c2011-10-06 19:12:20 +0900212int ProximityInfo::getKeyIndex(const int c) const {
satok1caff472012-03-14 23:17:12 +0900213 if (KEY_COUNT == 0) {
Yusuke Nojimaa4c1f1c2011-10-06 19:12:20 +0900214 // We do not have the coordinate data
Jean Chalardbbc25602012-03-23 17:05:03 +0900215 return NOT_AN_INDEX;
Yusuke Nojimaa4c1f1c2011-10-06 19:12:20 +0900216 }
Tadashi G. Takaoka6e3cb272011-11-11 14:26:13 +0900217 const unsigned short baseLowerC = toBaseLowerCase(c);
Yusuke Nojimaa4c1f1c2011-10-06 19:12:20 +0900218 if (baseLowerC > MAX_CHAR_CODE) {
Jean Chalardbbc25602012-03-23 17:05:03 +0900219 return NOT_AN_INDEX;
Yusuke Nojimaa4c1f1c2011-10-06 19:12:20 +0900220 }
221 return mCodeToKeyIndex[baseLowerC];
222}
223
Satoshi Kataoka3e8c58f2012-06-05 17:55:52 +0900224// TODO: remove
satokd24df432011-07-14 15:43:42 +0900225inline const int* ProximityInfo::getProximityCharsAt(const int index) const {
Satoshi Kataoka3e8c58f2012-06-05 17:55:52 +0900226 return mProximityInfoState->getProximityCharsAt(index);
satok1d7eaf82011-07-13 10:32:02 +0900227}
228
Satoshi Kataoka3e8c58f2012-06-05 17:55:52 +0900229// TODO: remove
satokd24df432011-07-14 15:43:42 +0900230unsigned short ProximityInfo::getPrimaryCharAt(const int index) const {
Satoshi Kataoka3e8c58f2012-06-05 17:55:52 +0900231 return mProximityInfoState->getPrimaryCharAt(index);
satokd24df432011-07-14 15:43:42 +0900232}
233
Satoshi Kataoka3e8c58f2012-06-05 17:55:52 +0900234// TODO: remove
235bool ProximityInfo::existsCharInProximityAt(const int index, const int c) const {
236 return mProximityInfoState->existsCharInProximityAt(index, c);
satokd24df432011-07-14 15:43:42 +0900237}
238
Satoshi Kataoka3e8c58f2012-06-05 17:55:52 +0900239// TODO: remove
satokd24df432011-07-14 15:43:42 +0900240bool ProximityInfo::existsAdjacentProximityChars(const int index) const {
Satoshi Kataoka3e8c58f2012-06-05 17:55:52 +0900241 return mProximityInfoState->existsAdjacentProximityChars(index);
satokd24df432011-07-14 15:43:42 +0900242}
243
Satoshi Kataoka3e8c58f2012-06-05 17:55:52 +0900244// TODO: remove
245ProximityType ProximityInfo::getMatchedProximityId(const int index,
Yusuke Nojimaa4c1f1c2011-10-06 19:12:20 +0900246 const unsigned short c, const bool checkProximityChars, int *proximityIndex) const {
Satoshi Kataoka3e8c58f2012-06-05 17:55:52 +0900247 return mProximityInfoState->getMatchedProximityId(
248 index, c, checkProximityChars, proximityIndex);
satokd24df432011-07-14 15:43:42 +0900249}
250
Satoshi Kataoka3e8c58f2012-06-05 17:55:52 +0900251// TODO: remove
252int ProximityInfo::getNormalizedSquaredDistance(
253 const int inputIndex, const int proximityIndex) const {
254 return mProximityInfoState->getNormalizedSquaredDistance(inputIndex, proximityIndex);
satok1d7eaf82011-07-13 10:32:02 +0900255}
256
Satoshi Kataoka3e8c58f2012-06-05 17:55:52 +0900257// TODO: remove
258const unsigned short* ProximityInfo::getPrimaryInputWord() const {
259 return mProximityInfoState->getPrimaryInputWord();
260}
Yusuke Nojima0e1f6562011-09-21 12:02:47 +0900261
Satoshi Kataoka3e8c58f2012-06-05 17:55:52 +0900262// TODO: remove
263bool ProximityInfo::touchPositionCorrectionEnabled() const {
264 return mProximityInfoState->touchPositionCorrectionEnabled();
265}
Ken Wakasace9e52a2011-06-18 13:09:55 +0900266} // namespace latinime