blob: 4ff6e0ac07241a76fa43d54b1e9a41ca6f4877cb [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>
19#include <string.h>
20
satok817e5172011-03-04 06:06:45 -080021#define LOG_TAG "LatinIME: proximity_info.cpp"
22
satokd24df432011-07-14 15:43:42 +090023#include "dictionary.h"
satok8fbd5522011-02-22 17:28:55 +090024#include "proximity_info.h"
25
26namespace latinime {
Ken Wakasace9e52a2011-06-18 13:09:55 +090027
Yusuke Nojimade2f8422011-09-27 11:15:18 +090028inline void copyOrFillZero(void *to, const void *from, size_t size) {
29 if (from) {
30 memcpy(to, from, size);
31 } else {
32 memset(to, 0, size);
33 }
34}
35
satok817e5172011-03-04 06:06:45 -080036ProximityInfo::ProximityInfo(const int maxProximityCharsSize, const int keyboardWidth,
37 const int keyboardHeight, const int gridWidth, const int gridHeight,
Yusuke Nojima0e1f6562011-09-21 12:02:47 +090038 const uint32_t *proximityCharsArray, const int keyCount, const int32_t *keyXCoordinates,
39 const int32_t *keyYCoordinates, const int32_t *keyWidths, const int32_t *keyHeights,
Yusuke Nojimaad358352011-09-29 16:44:54 +090040 const int32_t *keyCharCodes, const float *sweetSpotCenterXs, const float *sweetSpotCenterYs,
41 const float *sweetSpotRadii)
satok817e5172011-03-04 06:06:45 -080042 : MAX_PROXIMITY_CHARS_SIZE(maxProximityCharsSize), KEYBOARD_WIDTH(keyboardWidth),
43 KEYBOARD_HEIGHT(keyboardHeight), GRID_WIDTH(gridWidth), GRID_HEIGHT(gridHeight),
44 CELL_WIDTH((keyboardWidth + gridWidth - 1) / gridWidth),
Yusuke Nojima0e1f6562011-09-21 12:02:47 +090045 CELL_HEIGHT((keyboardHeight + gridHeight - 1) / gridHeight),
Yusuke Nojimaad358352011-09-29 16:44:54 +090046 KEY_COUNT(min(keyCount, MAX_KEY_COUNT_IN_A_KEYBOARD)) {
satok817e5172011-03-04 06:06:45 -080047 const int len = GRID_WIDTH * GRID_HEIGHT * MAX_PROXIMITY_CHARS_SIZE;
48 mProximityCharsArray = new uint32_t[len];
49 if (DEBUG_PROXIMITY_INFO) {
50 LOGI("Create proximity info array %d", len);
51 }
52 memcpy(mProximityCharsArray, proximityCharsArray, len * sizeof(mProximityCharsArray[0]));
Yusuke Nojima0e1f6562011-09-21 12:02:47 +090053
Yusuke Nojimade2f8422011-09-27 11:15:18 +090054 copyOrFillZero(mKeyXCoordinates, keyXCoordinates, KEY_COUNT * sizeof(mKeyXCoordinates[0]));
55 copyOrFillZero(mKeyYCoordinates, keyYCoordinates, KEY_COUNT * sizeof(mKeyYCoordinates[0]));
56 copyOrFillZero(mKeyWidths, keyWidths, KEY_COUNT * sizeof(mKeyWidths[0]));
57 copyOrFillZero(mKeyHeights, keyHeights, KEY_COUNT * sizeof(mKeyHeights[0]));
58 copyOrFillZero(mKeyCharCodes, keyCharCodes, KEY_COUNT * sizeof(mKeyCharCodes[0]));
Yusuke Nojimaad358352011-09-29 16:44:54 +090059 copyOrFillZero(mSweetSpotCenterXs, sweetSpotCenterXs,
60 KEY_COUNT * sizeof(mSweetSpotCenterXs[0]));
61 copyOrFillZero(mSweetSpotCenterYs, sweetSpotCenterYs,
62 KEY_COUNT * sizeof(mSweetSpotCenterYs[0]));
63 copyOrFillZero(mSweetSpotRadii, sweetSpotRadii, KEY_COUNT * sizeof(mSweetSpotRadii[0]));
Yusuke Nojima0e1f6562011-09-21 12:02:47 +090064
Yusuke Nojima0e1f6562011-09-21 12:02:47 +090065 initializeCodeToKeyIndex();
66}
67
Yusuke Nojima0e1f6562011-09-21 12:02:47 +090068// Build the reversed look up table from the char code to the index in mKeyXCoordinates,
69// mKeyYCoordinates, mKeyWidths, mKeyHeights, mKeyCharCodes.
70void ProximityInfo::initializeCodeToKeyIndex() {
Yusuke Nojimaad358352011-09-29 16:44:54 +090071 memset(mCodeToKeyIndex, -1, (MAX_CHAR_CODE + 1) * sizeof(mCodeToKeyIndex[0]));
Yusuke Nojima0e1f6562011-09-21 12:02:47 +090072 for (int i = 0; i < KEY_COUNT; ++i) {
73 const int code = mKeyCharCodes[i];
Yusuke Nojimaad358352011-09-29 16:44:54 +090074 if (0 <= code && code <= MAX_CHAR_CODE) {
Yusuke Nojima0e1f6562011-09-21 12:02:47 +090075 mCodeToKeyIndex[code] = i;
Yusuke Nojimaad358352011-09-29 16:44:54 +090076 }
Yusuke Nojima0e1f6562011-09-21 12:02:47 +090077 }
satok8fbd5522011-02-22 17:28:55 +090078}
79
80ProximityInfo::~ProximityInfo() {
81 delete[] mProximityCharsArray;
82}
satok817e5172011-03-04 06:06:45 -080083
84inline int ProximityInfo::getStartIndexFromCoordinates(const int x, const int y) const {
satok3c4bb772011-03-04 22:50:19 -080085 return ((y / CELL_HEIGHT) * GRID_WIDTH + (x / CELL_WIDTH))
satok817e5172011-03-04 06:06:45 -080086 * MAX_PROXIMITY_CHARS_SIZE;
satok8fbd5522011-02-22 17:28:55 +090087}
satok817e5172011-03-04 06:06:45 -080088
89bool ProximityInfo::hasSpaceProximity(const int x, const int y) const {
90 const int startIndex = getStartIndexFromCoordinates(x, y);
91 if (DEBUG_PROXIMITY_INFO) {
92 LOGI("hasSpaceProximity: index %d", startIndex);
93 }
94 for (int i = 0; i < MAX_PROXIMITY_CHARS_SIZE; ++i) {
95 if (DEBUG_PROXIMITY_INFO) {
96 LOGI("Index: %d", mProximityCharsArray[startIndex + i]);
97 }
98 if (mProximityCharsArray[startIndex + i] == KEYCODE_SPACE) {
99 return true;
100 }
101 }
102 return false;
103}
Ken Wakasace9e52a2011-06-18 13:09:55 +0900104
satok1d7eaf82011-07-13 10:32:02 +0900105// TODO: Calculate nearby codes here.
106void ProximityInfo::setInputParams(const int* inputCodes, const int inputLength) {
107 mInputCodes = inputCodes;
108 mInputLength = inputLength;
satok0cedd2b2011-08-12 01:05:27 +0900109 for (int i = 0; i < inputLength; ++i) {
110 mPrimaryInputWord[i] = getPrimaryCharAt(i);
111 }
112 mPrimaryInputWord[inputLength] = 0;
satok1d7eaf82011-07-13 10:32:02 +0900113}
114
satokd24df432011-07-14 15:43:42 +0900115inline const int* ProximityInfo::getProximityCharsAt(const int index) const {
satok1d7eaf82011-07-13 10:32:02 +0900116 return mInputCodes + (index * MAX_PROXIMITY_CHARS_SIZE);
117}
118
satokd24df432011-07-14 15:43:42 +0900119unsigned short ProximityInfo::getPrimaryCharAt(const int index) const {
120 return getProximityCharsAt(index)[0];
121}
122
satok2df30602011-07-15 13:49:00 +0900123inline bool ProximityInfo::existsCharInProximityAt(const int index, const int c) const {
satokd24df432011-07-14 15:43:42 +0900124 const int *chars = getProximityCharsAt(index);
125 int i = 0;
126 while (chars[i] > 0 && i < MAX_PROXIMITY_CHARS_SIZE) {
127 if (chars[i++] == c) {
128 return true;
129 }
130 }
131 return false;
132}
133
134bool ProximityInfo::existsAdjacentProximityChars(const int index) const {
135 if (index < 0 || index >= mInputLength) return false;
136 const int currentChar = getPrimaryCharAt(index);
137 const int leftIndex = index - 1;
138 if (leftIndex >= 0 && existsCharInProximityAt(leftIndex, currentChar)) {
139 return true;
140 }
141 const int rightIndex = index + 1;
142 if (rightIndex < mInputLength && existsCharInProximityAt(rightIndex, currentChar)) {
143 return true;
144 }
145 return false;
146}
147
148// In the following function, c is the current character of the dictionary word
149// currently examined.
150// currentChars is an array containing the keys close to the character the
151// user actually typed at the same position. We want to see if c is in it: if so,
152// then the word contains at that position a character close to what the user
153// typed.
154// What the user typed is actually the first character of the array.
155// Notice : accented characters do not have a proximity list, so they are alone
156// in their list. The non-accented version of the character should be considered
157// "close", but not the other keys close to the non-accented version.
158ProximityInfo::ProximityType ProximityInfo::getMatchedProximityId(
satok985312e2011-08-05 21:21:01 +0900159 const int index, const unsigned short c, const bool checkProximityChars) const {
satokd24df432011-07-14 15:43:42 +0900160 const int *currentChars = getProximityCharsAt(index);
161 const unsigned short baseLowerC = Dictionary::toBaseLowerCase(c);
162
163 // The first char in the array is what user typed. If it matches right away,
164 // that means the user typed that same char for this pos.
165 if (currentChars[0] == baseLowerC || currentChars[0] == c)
166 return SAME_OR_ACCENTED_OR_CAPITALIZED_CHAR;
167
satok985312e2011-08-05 21:21:01 +0900168 if (!checkProximityChars) return UNRELATED_CHAR;
satokd24df432011-07-14 15:43:42 +0900169
170 // If the non-accented, lowercased version of that first character matches c,
171 // then we have a non-accented version of the accented character the user
172 // typed. Treat it as a close char.
173 if (Dictionary::toBaseLowerCase(currentChars[0]) == baseLowerC)
174 return NEAR_PROXIMITY_CHAR;
175
176 // Not an exact nor an accent-alike match: search the list of close keys
177 int j = 1;
178 while (currentChars[j] > 0 && j < MAX_PROXIMITY_CHARS_SIZE) {
179 const bool matched = (currentChars[j] == baseLowerC || currentChars[j] == c);
180 if (matched) return NEAR_PROXIMITY_CHAR;
181 ++j;
182 }
183
184 // Was not included, signal this as an unrelated character.
185 return UNRELATED_CHAR;
186}
187
satok1d7eaf82011-07-13 10:32:02 +0900188bool ProximityInfo::sameAsTyped(const unsigned short *word, int length) const {
189 if (length != mInputLength) {
190 return false;
191 }
192 const int *inputCodes = mInputCodes;
193 while (length--) {
194 if ((unsigned int) *inputCodes != (unsigned int) *word) {
195 return false;
196 }
197 inputCodes += MAX_PROXIMITY_CHARS_SIZE;
198 word++;
199 }
200 return true;
201}
202
Yusuke Nojima0e1f6562011-09-21 12:02:47 +0900203const int ProximityInfo::MAX_KEY_COUNT_IN_A_KEYBOARD;
Yusuke Nojimaad358352011-09-29 16:44:54 +0900204const int ProximityInfo::MAX_CHAR_CODE;
Yusuke Nojima0e1f6562011-09-21 12:02:47 +0900205
Ken Wakasace9e52a2011-06-18 13:09:55 +0900206} // namespace latinime