blob: 58842b92faf4645b7bb0093fdcbc2af5b2078ffc [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
Yusuke Nojima0e1f6562011-09-21 12:02:47 +090023#include "defines_touch_position_correction.h"
satokd24df432011-07-14 15:43:42 +090024#include "dictionary.h"
satok8fbd5522011-02-22 17:28:55 +090025#include "proximity_info.h"
26
27namespace latinime {
Ken Wakasace9e52a2011-06-18 13:09:55 +090028
Yusuke Nojimade2f8422011-09-27 11:15:18 +090029inline void copyOrFillZero(void *to, const void *from, size_t size) {
30 if (from) {
31 memcpy(to, from, size);
32 } else {
33 memset(to, 0, size);
34 }
35}
36
satok817e5172011-03-04 06:06:45 -080037ProximityInfo::ProximityInfo(const int maxProximityCharsSize, const int keyboardWidth,
38 const int keyboardHeight, const int gridWidth, const int gridHeight,
Yusuke Nojima0e1f6562011-09-21 12:02:47 +090039 const uint32_t *proximityCharsArray, const int keyCount, const int32_t *keyXCoordinates,
40 const int32_t *keyYCoordinates, const int32_t *keyWidths, const int32_t *keyHeights,
Yusuke Nojima1c923d82011-09-28 11:38:34 +090041 const int32_t *keyCharCodes, int themeId)
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 Nojima1c923d82011-09-28 11:38:34 +090046 KEY_COUNT(min(keyCount, MAX_KEY_COUNT_IN_A_KEYBOARD)), THEME_ID(themeId) {
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 Nojima0e1f6562011-09-21 12:02:47 +090059
60 initializeCodeToGroup();
61 initializeCodeToKeyIndex();
62}
63
64// Build the reversed look up table from the char code to the index in its group.
65// see TOUCH_POSITION_CORRECTION_GROUPS
66void ProximityInfo::initializeCodeToGroup() {
67 memset(mCodeToGroup, -1, (MAX_GROUPED_CHAR_CODE + 1) * sizeof(mCodeToGroup[0]));
68 for (int i = 0; i < CORRECTION_GROUP_COUNT; ++i) {
69 const char *group = TOUCH_POSITION_CORRECTION_GROUPS[i];
70 for (int j = 0; group[j]; ++j) {
71 const int code = group[j];
72 if (0 <= code && code <= MAX_GROUPED_CHAR_CODE)
73 mCodeToGroup[code] = i;
74 }
75 }
76}
77
78// Build the reversed look up table from the char code to the index in mKeyXCoordinates,
79// mKeyYCoordinates, mKeyWidths, mKeyHeights, mKeyCharCodes.
80void ProximityInfo::initializeCodeToKeyIndex() {
81 memset(mCodeToKeyIndex, -1, (MAX_GROUPED_CHAR_CODE + 1) * sizeof(mCodeToKeyIndex[0]));
82 for (int i = 0; i < KEY_COUNT; ++i) {
83 const int code = mKeyCharCodes[i];
84 if (0 <= code && code <= MAX_GROUPED_CHAR_CODE)
85 mCodeToKeyIndex[code] = i;
86 }
satok8fbd5522011-02-22 17:28:55 +090087}
88
89ProximityInfo::~ProximityInfo() {
90 delete[] mProximityCharsArray;
91}
satok817e5172011-03-04 06:06:45 -080092
93inline int ProximityInfo::getStartIndexFromCoordinates(const int x, const int y) const {
satok3c4bb772011-03-04 22:50:19 -080094 return ((y / CELL_HEIGHT) * GRID_WIDTH + (x / CELL_WIDTH))
satok817e5172011-03-04 06:06:45 -080095 * MAX_PROXIMITY_CHARS_SIZE;
satok8fbd5522011-02-22 17:28:55 +090096}
satok817e5172011-03-04 06:06:45 -080097
98bool ProximityInfo::hasSpaceProximity(const int x, const int y) const {
99 const int startIndex = getStartIndexFromCoordinates(x, y);
100 if (DEBUG_PROXIMITY_INFO) {
101 LOGI("hasSpaceProximity: index %d", startIndex);
102 }
103 for (int i = 0; i < MAX_PROXIMITY_CHARS_SIZE; ++i) {
104 if (DEBUG_PROXIMITY_INFO) {
105 LOGI("Index: %d", mProximityCharsArray[startIndex + i]);
106 }
107 if (mProximityCharsArray[startIndex + i] == KEYCODE_SPACE) {
108 return true;
109 }
110 }
111 return false;
112}
Ken Wakasace9e52a2011-06-18 13:09:55 +0900113
satok1d7eaf82011-07-13 10:32:02 +0900114// TODO: Calculate nearby codes here.
115void ProximityInfo::setInputParams(const int* inputCodes, const int inputLength) {
116 mInputCodes = inputCodes;
117 mInputLength = inputLength;
satok0cedd2b2011-08-12 01:05:27 +0900118 for (int i = 0; i < inputLength; ++i) {
119 mPrimaryInputWord[i] = getPrimaryCharAt(i);
120 }
121 mPrimaryInputWord[inputLength] = 0;
satok1d7eaf82011-07-13 10:32:02 +0900122}
123
satokd24df432011-07-14 15:43:42 +0900124inline const int* ProximityInfo::getProximityCharsAt(const int index) const {
satok1d7eaf82011-07-13 10:32:02 +0900125 return mInputCodes + (index * MAX_PROXIMITY_CHARS_SIZE);
126}
127
satokd24df432011-07-14 15:43:42 +0900128unsigned short ProximityInfo::getPrimaryCharAt(const int index) const {
129 return getProximityCharsAt(index)[0];
130}
131
satok2df30602011-07-15 13:49:00 +0900132inline bool ProximityInfo::existsCharInProximityAt(const int index, const int c) const {
satokd24df432011-07-14 15:43:42 +0900133 const int *chars = getProximityCharsAt(index);
134 int i = 0;
135 while (chars[i] > 0 && i < MAX_PROXIMITY_CHARS_SIZE) {
136 if (chars[i++] == c) {
137 return true;
138 }
139 }
140 return false;
141}
142
143bool ProximityInfo::existsAdjacentProximityChars(const int index) const {
144 if (index < 0 || index >= mInputLength) return false;
145 const int currentChar = getPrimaryCharAt(index);
146 const int leftIndex = index - 1;
147 if (leftIndex >= 0 && existsCharInProximityAt(leftIndex, currentChar)) {
148 return true;
149 }
150 const int rightIndex = index + 1;
151 if (rightIndex < mInputLength && existsCharInProximityAt(rightIndex, currentChar)) {
152 return true;
153 }
154 return false;
155}
156
157// In the following function, c is the current character of the dictionary word
158// currently examined.
159// currentChars is an array containing the keys close to the character the
160// user actually typed at the same position. We want to see if c is in it: if so,
161// then the word contains at that position a character close to what the user
162// typed.
163// What the user typed is actually the first character of the array.
164// Notice : accented characters do not have a proximity list, so they are alone
165// in their list. The non-accented version of the character should be considered
166// "close", but not the other keys close to the non-accented version.
167ProximityInfo::ProximityType ProximityInfo::getMatchedProximityId(
satok985312e2011-08-05 21:21:01 +0900168 const int index, const unsigned short c, const bool checkProximityChars) const {
satokd24df432011-07-14 15:43:42 +0900169 const int *currentChars = getProximityCharsAt(index);
170 const unsigned short baseLowerC = Dictionary::toBaseLowerCase(c);
171
172 // The first char in the array is what user typed. If it matches right away,
173 // that means the user typed that same char for this pos.
174 if (currentChars[0] == baseLowerC || currentChars[0] == c)
175 return SAME_OR_ACCENTED_OR_CAPITALIZED_CHAR;
176
satok985312e2011-08-05 21:21:01 +0900177 if (!checkProximityChars) return UNRELATED_CHAR;
satokd24df432011-07-14 15:43:42 +0900178
179 // If the non-accented, lowercased version of that first character matches c,
180 // then we have a non-accented version of the accented character the user
181 // typed. Treat it as a close char.
182 if (Dictionary::toBaseLowerCase(currentChars[0]) == baseLowerC)
183 return NEAR_PROXIMITY_CHAR;
184
185 // Not an exact nor an accent-alike match: search the list of close keys
186 int j = 1;
187 while (currentChars[j] > 0 && j < MAX_PROXIMITY_CHARS_SIZE) {
188 const bool matched = (currentChars[j] == baseLowerC || currentChars[j] == c);
189 if (matched) return NEAR_PROXIMITY_CHAR;
190 ++j;
191 }
192
193 // Was not included, signal this as an unrelated character.
194 return UNRELATED_CHAR;
195}
196
satok1d7eaf82011-07-13 10:32:02 +0900197bool ProximityInfo::sameAsTyped(const unsigned short *word, int length) const {
198 if (length != mInputLength) {
199 return false;
200 }
201 const int *inputCodes = mInputCodes;
202 while (length--) {
203 if ((unsigned int) *inputCodes != (unsigned int) *word) {
204 return false;
205 }
206 inputCodes += MAX_PROXIMITY_CHARS_SIZE;
207 word++;
208 }
209 return true;
210}
211
Yusuke Nojima0e1f6562011-09-21 12:02:47 +0900212const int ProximityInfo::MAX_KEY_COUNT_IN_A_KEYBOARD;
213const int ProximityInfo::MAX_GROUPED_CHAR_CODE;
214
Ken Wakasace9e52a2011-06-18 13:09:55 +0900215} // namespace latinime