blob: 2ba244a7c9470fc459fd3e13f1bfe6cbf9128fc3 [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) {
satok1035bc92012-06-13 16:07:54 -070056 const int proximityGridLength = GRID_WIDTH * GRID_HEIGHT * MAX_PROXIMITY_CHARS_SIZE;
satok817e5172011-03-04 06:06:45 -080057 if (DEBUG_PROXIMITY_INFO) {
satok9fb6f472012-01-13 18:01:22 +090058 AKLOGI("Create proximity info array %d", proximityGridLength);
satok817e5172011-03-04 06:06:45 -080059 }
Satoshi Kataoka3e8c58f2012-06-05 17:55:52 +090060 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();
76}
77
Yusuke Nojima0e1f6562011-09-21 12:02:47 +090078// Build the reversed look up table from the char code to the index in mKeyXCoordinates,
79// mKeyYCoordinates, mKeyWidths, mKeyHeights, mKeyCharCodes.
80void ProximityInfo::initializeCodeToKeyIndex() {
Yusuke Nojimaad358352011-09-29 16:44:54 +090081 memset(mCodeToKeyIndex, -1, (MAX_CHAR_CODE + 1) * sizeof(mCodeToKeyIndex[0]));
Yusuke Nojima0e1f6562011-09-21 12:02:47 +090082 for (int i = 0; i < KEY_COUNT; ++i) {
83 const int code = mKeyCharCodes[i];
Yusuke Nojimaad358352011-09-29 16:44:54 +090084 if (0 <= code && code <= MAX_CHAR_CODE) {
Yusuke Nojima0e1f6562011-09-21 12:02:47 +090085 mCodeToKeyIndex[code] = i;
Yusuke Nojimaad358352011-09-29 16:44:54 +090086 }
Yusuke Nojima0e1f6562011-09-21 12:02:47 +090087 }
satok8fbd5522011-02-22 17:28:55 +090088}
89
90ProximityInfo::~ProximityInfo() {
91 delete[] mProximityCharsArray;
92}
satok817e5172011-03-04 06:06:45 -080093
94inline int ProximityInfo::getStartIndexFromCoordinates(const int x, const int y) const {
satok3c4bb772011-03-04 22:50:19 -080095 return ((y / CELL_HEIGHT) * GRID_WIDTH + (x / CELL_WIDTH))
satok817e5172011-03-04 06:06:45 -080096 * MAX_PROXIMITY_CHARS_SIZE;
satok8fbd5522011-02-22 17:28:55 +090097}
satok817e5172011-03-04 06:06:45 -080098
99bool ProximityInfo::hasSpaceProximity(const int x, const int y) const {
satok744dab62011-12-15 22:29:05 +0900100 if (x < 0 || y < 0) {
101 if (DEBUG_DICT) {
satok9fb6f472012-01-13 18:01:22 +0900102 AKLOGI("HasSpaceProximity: Illegal coordinates (%d, %d)", x, y);
satok1a6da632011-12-16 23:15:06 +0900103 assert(false);
satok744dab62011-12-15 22:29:05 +0900104 }
105 return false;
106 }
107
satok817e5172011-03-04 06:06:45 -0800108 const int startIndex = getStartIndexFromCoordinates(x, y);
109 if (DEBUG_PROXIMITY_INFO) {
satok9fb6f472012-01-13 18:01:22 +0900110 AKLOGI("hasSpaceProximity: index %d, %d, %d", startIndex, x, y);
satok817e5172011-03-04 06:06:45 -0800111 }
Satoshi Kataoka3e8c58f2012-06-05 17:55:52 +0900112 int32_t* proximityCharsArray = mProximityCharsArray;
satok817e5172011-03-04 06:06:45 -0800113 for (int i = 0; i < MAX_PROXIMITY_CHARS_SIZE; ++i) {
114 if (DEBUG_PROXIMITY_INFO) {
satok9fb6f472012-01-13 18:01:22 +0900115 AKLOGI("Index: %d", mProximityCharsArray[startIndex + i]);
satok817e5172011-03-04 06:06:45 -0800116 }
Satoshi Kataoka3e8c58f2012-06-05 17:55:52 +0900117 if (proximityCharsArray[startIndex + i] == KEYCODE_SPACE) {
satok817e5172011-03-04 06:06:45 -0800118 return true;
119 }
120 }
121 return false;
122}
Ken Wakasace9e52a2011-06-18 13:09:55 +0900123
satok9df4a452012-03-23 16:05:18 +0900124int ProximityInfo::squaredDistanceToEdge(const int keyId, const int x, const int y) const {
Jean Chalard081616c2012-03-22 17:39:27 +0900125 if (keyId < 0) return true; // NOT_A_ID is -1, but return whenever < 0 just in case
satoka70ee6e2012-03-07 15:12:22 +0900126 const int left = mKeyXCoordinates[keyId];
127 const int top = mKeyYCoordinates[keyId];
satok1caff472012-03-14 23:17:12 +0900128 const int right = left + mKeyWidths[keyId];
satoka70ee6e2012-03-07 15:12:22 +0900129 const int bottom = top + mKeyHeights[keyId];
130 const int edgeX = x < left ? left : (x > right ? right : x);
131 const int edgeY = y < top ? top : (y > bottom ? bottom : y);
132 const int dx = x - edgeX;
133 const int dy = y - edgeY;
134 return dx * dx + dy * dy;
135}
136
137void ProximityInfo::calculateNearbyKeyCodes(
satok9df4a452012-03-23 16:05:18 +0900138 const int x, const int y, const int32_t primaryKey, int *inputCodes) const {
Satoshi Kataoka3e8c58f2012-06-05 17:55:52 +0900139 int32_t *proximityCharsArray = mProximityCharsArray;
satoka70ee6e2012-03-07 15:12:22 +0900140 int insertPos = 0;
141 inputCodes[insertPos++] = primaryKey;
142 const int startIndex = getStartIndexFromCoordinates(x, y);
Jean Chalard52612a02012-03-23 19:38:23 +0900143 if (startIndex >= 0) {
Jean Chalard88ec1252012-03-23 19:25:10 +0900144 for (int i = 0; i < MAX_PROXIMITY_CHARS_SIZE; ++i) {
Satoshi Kataoka3e8c58f2012-06-05 17:55:52 +0900145 const int32_t c = proximityCharsArray[startIndex + i];
Jean Chalard88ec1252012-03-23 19:25:10 +0900146 if (c < KEYCODE_SPACE || c == primaryKey) {
147 continue;
148 }
149 const int keyIndex = getKeyIndex(c);
150 const bool onKey = isOnKey(keyIndex, x, y);
151 const int distance = squaredDistanceToEdge(keyIndex, x, y);
152 if (onKey || distance < MOST_COMMON_KEY_WIDTH_SQUARE) {
153 inputCodes[insertPos++] = c;
154 if (insertPos >= MAX_PROXIMITY_CHARS_SIZE) {
155 if (DEBUG_DICT) {
156 assert(false);
157 }
158 return;
satok0cb20972012-03-13 22:07:56 +0900159 }
satoka70ee6e2012-03-07 15:12:22 +0900160 }
161 }
Jean Chalard88ec1252012-03-23 19:25:10 +0900162 const int additionalProximitySize =
163 AdditionalProximityChars::getAdditionalCharsSize(&mLocaleStr, primaryKey);
164 if (additionalProximitySize > 0) {
165 inputCodes[insertPos++] = ADDITIONAL_PROXIMITY_CHAR_DELIMITER_CODE;
166 if (insertPos >= MAX_PROXIMITY_CHARS_SIZE) {
Jean Chalard3094d122012-03-23 19:36:07 +0900167 if (DEBUG_DICT) {
168 assert(false);
169 }
170 return;
satok1caff472012-03-14 23:17:12 +0900171 }
satok1caff472012-03-14 23:17:12 +0900172
Jean Chalard88ec1252012-03-23 19:25:10 +0900173 const int32_t* additionalProximityChars =
174 AdditionalProximityChars::getAdditionalChars(&mLocaleStr, primaryKey);
175 for (int j = 0; j < additionalProximitySize; ++j) {
176 const int32_t ac = additionalProximityChars[j];
177 int k = 0;
178 for (; k < insertPos; ++k) {
179 if ((int)ac == inputCodes[k]) {
180 break;
181 }
satok5eec5742012-03-13 18:26:23 +0900182 }
Jean Chalard88ec1252012-03-23 19:25:10 +0900183 if (k < insertPos) {
184 continue;
satok0cb20972012-03-13 22:07:56 +0900185 }
Jean Chalard88ec1252012-03-23 19:25:10 +0900186 inputCodes[insertPos++] = ac;
187 if (insertPos >= MAX_PROXIMITY_CHARS_SIZE) {
188 if (DEBUG_DICT) {
189 assert(false);
190 }
191 return;
Jean Chalard3094d122012-03-23 19:36:07 +0900192 }
satok0cb20972012-03-13 22:07:56 +0900193 }
satok5eec5742012-03-13 18:26:23 +0900194 }
Jean Chalard52612a02012-03-23 19:38:23 +0900195 }
satok0cb20972012-03-13 22:07:56 +0900196 // Add a delimiter for the proximity characters
satok1caff472012-03-14 23:17:12 +0900197 for (int i = insertPos; i < MAX_PROXIMITY_CHARS_SIZE; ++i) {
198 inputCodes[i] = NOT_A_CODE;
199 }
satoka70ee6e2012-03-07 15:12:22 +0900200}
201
Yusuke Nojimaa4c1f1c2011-10-06 19:12:20 +0900202int ProximityInfo::getKeyIndex(const int c) const {
satok1caff472012-03-14 23:17:12 +0900203 if (KEY_COUNT == 0) {
Yusuke Nojimaa4c1f1c2011-10-06 19:12:20 +0900204 // We do not have the coordinate data
Jean Chalardbbc25602012-03-23 17:05:03 +0900205 return NOT_AN_INDEX;
Yusuke Nojimaa4c1f1c2011-10-06 19:12:20 +0900206 }
Tadashi G. Takaoka6e3cb272011-11-11 14:26:13 +0900207 const unsigned short baseLowerC = toBaseLowerCase(c);
Yusuke Nojimaa4c1f1c2011-10-06 19:12:20 +0900208 if (baseLowerC > MAX_CHAR_CODE) {
Jean Chalardbbc25602012-03-23 17:05:03 +0900209 return NOT_AN_INDEX;
Yusuke Nojimaa4c1f1c2011-10-06 19:12:20 +0900210 }
211 return mCodeToKeyIndex[baseLowerC];
212}
Ken Wakasace9e52a2011-06-18 13:09:55 +0900213} // namespace latinime