blob: 94cccc3822e94ce3ed1355cd921d3ad4385e4584 [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
satok817e5172011-03-04 06:06:45 -080029ProximityInfo::ProximityInfo(const int maxProximityCharsSize, const int keyboardWidth,
30 const int keyboardHeight, const int gridWidth, const int gridHeight,
Yusuke Nojima0e1f6562011-09-21 12:02:47 +090031 const uint32_t *proximityCharsArray, const int keyCount, const int32_t *keyXCoordinates,
32 const int32_t *keyYCoordinates, const int32_t *keyWidths, const int32_t *keyHeights,
33 const int32_t *keyCharCodes)
satok817e5172011-03-04 06:06:45 -080034 : MAX_PROXIMITY_CHARS_SIZE(maxProximityCharsSize), KEYBOARD_WIDTH(keyboardWidth),
35 KEYBOARD_HEIGHT(keyboardHeight), GRID_WIDTH(gridWidth), GRID_HEIGHT(gridHeight),
36 CELL_WIDTH((keyboardWidth + gridWidth - 1) / gridWidth),
Yusuke Nojima0e1f6562011-09-21 12:02:47 +090037 CELL_HEIGHT((keyboardHeight + gridHeight - 1) / gridHeight),
38 KEY_COUNT(min(keyCount, MAX_KEY_COUNT_IN_A_KEYBOARD)) {
satok817e5172011-03-04 06:06:45 -080039 const int len = GRID_WIDTH * GRID_HEIGHT * MAX_PROXIMITY_CHARS_SIZE;
40 mProximityCharsArray = new uint32_t[len];
41 if (DEBUG_PROXIMITY_INFO) {
42 LOGI("Create proximity info array %d", len);
43 }
44 memcpy(mProximityCharsArray, proximityCharsArray, len * sizeof(mProximityCharsArray[0]));
Yusuke Nojima0e1f6562011-09-21 12:02:47 +090045
46 if (KEY_COUNT > 0) {
47 if (DEBUG_PROXIMITY_INFO) {
48 LOGI("Create key coordinate array %d", keyCount);
49 }
50 memcpy(mKeyXCoordinates, keyXCoordinates, KEY_COUNT * sizeof(mKeyXCoordinates[0]));
51 memcpy(mKeyYCoordinates, keyYCoordinates, KEY_COUNT * sizeof(mKeyYCoordinates[0]));
52 memcpy(mKeyWidths, keyWidths, KEY_COUNT * sizeof(mKeyWidths[0]));
53 memcpy(mKeyHeights, keyHeights, KEY_COUNT * sizeof(mKeyHeights[0]));
54 memcpy(mKeyCharCodes, keyCharCodes, KEY_COUNT * sizeof(mKeyCharCodes[0]));
55 }
56
57 initializeCodeToGroup();
58 initializeCodeToKeyIndex();
59}
60
61// Build the reversed look up table from the char code to the index in its group.
62// see TOUCH_POSITION_CORRECTION_GROUPS
63void ProximityInfo::initializeCodeToGroup() {
64 memset(mCodeToGroup, -1, (MAX_GROUPED_CHAR_CODE + 1) * sizeof(mCodeToGroup[0]));
65 for (int i = 0; i < CORRECTION_GROUP_COUNT; ++i) {
66 const char *group = TOUCH_POSITION_CORRECTION_GROUPS[i];
67 for (int j = 0; group[j]; ++j) {
68 const int code = group[j];
69 if (0 <= code && code <= MAX_GROUPED_CHAR_CODE)
70 mCodeToGroup[code] = i;
71 }
72 }
73}
74
75// Build the reversed look up table from the char code to the index in mKeyXCoordinates,
76// mKeyYCoordinates, mKeyWidths, mKeyHeights, mKeyCharCodes.
77void ProximityInfo::initializeCodeToKeyIndex() {
78 memset(mCodeToKeyIndex, -1, (MAX_GROUPED_CHAR_CODE + 1) * sizeof(mCodeToKeyIndex[0]));
79 for (int i = 0; i < KEY_COUNT; ++i) {
80 const int code = mKeyCharCodes[i];
81 if (0 <= code && code <= MAX_GROUPED_CHAR_CODE)
82 mCodeToKeyIndex[code] = i;
83 }
satok8fbd5522011-02-22 17:28:55 +090084}
85
86ProximityInfo::~ProximityInfo() {
87 delete[] mProximityCharsArray;
88}
satok817e5172011-03-04 06:06:45 -080089
90inline int ProximityInfo::getStartIndexFromCoordinates(const int x, const int y) const {
satok3c4bb772011-03-04 22:50:19 -080091 return ((y / CELL_HEIGHT) * GRID_WIDTH + (x / CELL_WIDTH))
satok817e5172011-03-04 06:06:45 -080092 * MAX_PROXIMITY_CHARS_SIZE;
satok8fbd5522011-02-22 17:28:55 +090093}
satok817e5172011-03-04 06:06:45 -080094
95bool ProximityInfo::hasSpaceProximity(const int x, const int y) const {
96 const int startIndex = getStartIndexFromCoordinates(x, y);
97 if (DEBUG_PROXIMITY_INFO) {
98 LOGI("hasSpaceProximity: index %d", startIndex);
99 }
100 for (int i = 0; i < MAX_PROXIMITY_CHARS_SIZE; ++i) {
101 if (DEBUG_PROXIMITY_INFO) {
102 LOGI("Index: %d", mProximityCharsArray[startIndex + i]);
103 }
104 if (mProximityCharsArray[startIndex + i] == KEYCODE_SPACE) {
105 return true;
106 }
107 }
108 return false;
109}
Ken Wakasace9e52a2011-06-18 13:09:55 +0900110
satok1d7eaf82011-07-13 10:32:02 +0900111// TODO: Calculate nearby codes here.
112void ProximityInfo::setInputParams(const int* inputCodes, const int inputLength) {
113 mInputCodes = inputCodes;
114 mInputLength = inputLength;
satok0cedd2b2011-08-12 01:05:27 +0900115 for (int i = 0; i < inputLength; ++i) {
116 mPrimaryInputWord[i] = getPrimaryCharAt(i);
117 }
118 mPrimaryInputWord[inputLength] = 0;
satok1d7eaf82011-07-13 10:32:02 +0900119}
120
satokd24df432011-07-14 15:43:42 +0900121inline const int* ProximityInfo::getProximityCharsAt(const int index) const {
satok1d7eaf82011-07-13 10:32:02 +0900122 return mInputCodes + (index * MAX_PROXIMITY_CHARS_SIZE);
123}
124
satokd24df432011-07-14 15:43:42 +0900125unsigned short ProximityInfo::getPrimaryCharAt(const int index) const {
126 return getProximityCharsAt(index)[0];
127}
128
satok2df30602011-07-15 13:49:00 +0900129inline bool ProximityInfo::existsCharInProximityAt(const int index, const int c) const {
satokd24df432011-07-14 15:43:42 +0900130 const int *chars = getProximityCharsAt(index);
131 int i = 0;
132 while (chars[i] > 0 && i < MAX_PROXIMITY_CHARS_SIZE) {
133 if (chars[i++] == c) {
134 return true;
135 }
136 }
137 return false;
138}
139
140bool ProximityInfo::existsAdjacentProximityChars(const int index) const {
141 if (index < 0 || index >= mInputLength) return false;
142 const int currentChar = getPrimaryCharAt(index);
143 const int leftIndex = index - 1;
144 if (leftIndex >= 0 && existsCharInProximityAt(leftIndex, currentChar)) {
145 return true;
146 }
147 const int rightIndex = index + 1;
148 if (rightIndex < mInputLength && existsCharInProximityAt(rightIndex, currentChar)) {
149 return true;
150 }
151 return false;
152}
153
154// In the following function, c is the current character of the dictionary word
155// currently examined.
156// currentChars is an array containing the keys close to the character the
157// user actually typed at the same position. We want to see if c is in it: if so,
158// then the word contains at that position a character close to what the user
159// typed.
160// What the user typed is actually the first character of the array.
161// Notice : accented characters do not have a proximity list, so they are alone
162// in their list. The non-accented version of the character should be considered
163// "close", but not the other keys close to the non-accented version.
164ProximityInfo::ProximityType ProximityInfo::getMatchedProximityId(
satok985312e2011-08-05 21:21:01 +0900165 const int index, const unsigned short c, const bool checkProximityChars) const {
satokd24df432011-07-14 15:43:42 +0900166 const int *currentChars = getProximityCharsAt(index);
167 const unsigned short baseLowerC = Dictionary::toBaseLowerCase(c);
168
169 // The first char in the array is what user typed. If it matches right away,
170 // that means the user typed that same char for this pos.
171 if (currentChars[0] == baseLowerC || currentChars[0] == c)
172 return SAME_OR_ACCENTED_OR_CAPITALIZED_CHAR;
173
satok985312e2011-08-05 21:21:01 +0900174 if (!checkProximityChars) return UNRELATED_CHAR;
satokd24df432011-07-14 15:43:42 +0900175
176 // If the non-accented, lowercased version of that first character matches c,
177 // then we have a non-accented version of the accented character the user
178 // typed. Treat it as a close char.
179 if (Dictionary::toBaseLowerCase(currentChars[0]) == baseLowerC)
180 return NEAR_PROXIMITY_CHAR;
181
182 // Not an exact nor an accent-alike match: search the list of close keys
183 int j = 1;
184 while (currentChars[j] > 0 && j < MAX_PROXIMITY_CHARS_SIZE) {
185 const bool matched = (currentChars[j] == baseLowerC || currentChars[j] == c);
186 if (matched) return NEAR_PROXIMITY_CHAR;
187 ++j;
188 }
189
190 // Was not included, signal this as an unrelated character.
191 return UNRELATED_CHAR;
192}
193
satok1d7eaf82011-07-13 10:32:02 +0900194bool ProximityInfo::sameAsTyped(const unsigned short *word, int length) const {
195 if (length != mInputLength) {
196 return false;
197 }
198 const int *inputCodes = mInputCodes;
199 while (length--) {
200 if ((unsigned int) *inputCodes != (unsigned int) *word) {
201 return false;
202 }
203 inputCodes += MAX_PROXIMITY_CHARS_SIZE;
204 word++;
205 }
206 return true;
207}
208
Yusuke Nojima0e1f6562011-09-21 12:02:47 +0900209const int ProximityInfo::MAX_KEY_COUNT_IN_A_KEYBOARD;
210const int ProximityInfo::MAX_GROUPED_CHAR_CODE;
211
Ken Wakasace9e52a2011-06-18 13:09:55 +0900212} // namespace latinime