blob: d437e251ae7a5a17447b183dddeff4f5a2084689 [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
17#include <stdio.h>
18#include <string.h>
19
satok817e5172011-03-04 06:06:45 -080020#define LOG_TAG "LatinIME: proximity_info.cpp"
21
satokd24df432011-07-14 15:43:42 +090022#include "dictionary.h"
satok8fbd5522011-02-22 17:28:55 +090023#include "proximity_info.h"
24
25namespace latinime {
Ken Wakasace9e52a2011-06-18 13:09:55 +090026
satok817e5172011-03-04 06:06:45 -080027ProximityInfo::ProximityInfo(const int maxProximityCharsSize, const int keyboardWidth,
28 const int keyboardHeight, const int gridWidth, const int gridHeight,
29 const uint32_t *proximityCharsArray)
30 : MAX_PROXIMITY_CHARS_SIZE(maxProximityCharsSize), KEYBOARD_WIDTH(keyboardWidth),
31 KEYBOARD_HEIGHT(keyboardHeight), GRID_WIDTH(gridWidth), GRID_HEIGHT(gridHeight),
32 CELL_WIDTH((keyboardWidth + gridWidth - 1) / gridWidth),
33 CELL_HEIGHT((keyboardHeight + gridHeight - 1) / gridHeight) {
34 const int len = GRID_WIDTH * GRID_HEIGHT * MAX_PROXIMITY_CHARS_SIZE;
35 mProximityCharsArray = new uint32_t[len];
36 if (DEBUG_PROXIMITY_INFO) {
37 LOGI("Create proximity info array %d", len);
38 }
39 memcpy(mProximityCharsArray, proximityCharsArray, len * sizeof(mProximityCharsArray[0]));
satok8fbd5522011-02-22 17:28:55 +090040}
41
42ProximityInfo::~ProximityInfo() {
43 delete[] mProximityCharsArray;
44}
satok817e5172011-03-04 06:06:45 -080045
46inline int ProximityInfo::getStartIndexFromCoordinates(const int x, const int y) const {
satok3c4bb772011-03-04 22:50:19 -080047 return ((y / CELL_HEIGHT) * GRID_WIDTH + (x / CELL_WIDTH))
satok817e5172011-03-04 06:06:45 -080048 * MAX_PROXIMITY_CHARS_SIZE;
satok8fbd5522011-02-22 17:28:55 +090049}
satok817e5172011-03-04 06:06:45 -080050
51bool ProximityInfo::hasSpaceProximity(const int x, const int y) const {
52 const int startIndex = getStartIndexFromCoordinates(x, y);
53 if (DEBUG_PROXIMITY_INFO) {
54 LOGI("hasSpaceProximity: index %d", startIndex);
55 }
56 for (int i = 0; i < MAX_PROXIMITY_CHARS_SIZE; ++i) {
57 if (DEBUG_PROXIMITY_INFO) {
58 LOGI("Index: %d", mProximityCharsArray[startIndex + i]);
59 }
60 if (mProximityCharsArray[startIndex + i] == KEYCODE_SPACE) {
61 return true;
62 }
63 }
64 return false;
65}
Ken Wakasace9e52a2011-06-18 13:09:55 +090066
satok1d7eaf82011-07-13 10:32:02 +090067// TODO: Calculate nearby codes here.
68void ProximityInfo::setInputParams(const int* inputCodes, const int inputLength) {
69 mInputCodes = inputCodes;
70 mInputLength = inputLength;
71}
72
satokd24df432011-07-14 15:43:42 +090073inline const int* ProximityInfo::getProximityCharsAt(const int index) const {
satok1d7eaf82011-07-13 10:32:02 +090074 return mInputCodes + (index * MAX_PROXIMITY_CHARS_SIZE);
75}
76
satokd24df432011-07-14 15:43:42 +090077unsigned short ProximityInfo::getPrimaryCharAt(const int index) const {
78 return getProximityCharsAt(index)[0];
79}
80
satok2df30602011-07-15 13:49:00 +090081inline bool ProximityInfo::existsCharInProximityAt(const int index, const int c) const {
satokd24df432011-07-14 15:43:42 +090082 const int *chars = getProximityCharsAt(index);
83 int i = 0;
84 while (chars[i] > 0 && i < MAX_PROXIMITY_CHARS_SIZE) {
85 if (chars[i++] == c) {
86 return true;
87 }
88 }
89 return false;
90}
91
92bool ProximityInfo::existsAdjacentProximityChars(const int index) const {
93 if (index < 0 || index >= mInputLength) return false;
94 const int currentChar = getPrimaryCharAt(index);
95 const int leftIndex = index - 1;
96 if (leftIndex >= 0 && existsCharInProximityAt(leftIndex, currentChar)) {
97 return true;
98 }
99 const int rightIndex = index + 1;
100 if (rightIndex < mInputLength && existsCharInProximityAt(rightIndex, currentChar)) {
101 return true;
102 }
103 return false;
104}
105
106// In the following function, c is the current character of the dictionary word
107// currently examined.
108// currentChars is an array containing the keys close to the character the
109// user actually typed at the same position. We want to see if c is in it: if so,
110// then the word contains at that position a character close to what the user
111// typed.
112// What the user typed is actually the first character of the array.
113// Notice : accented characters do not have a proximity list, so they are alone
114// in their list. The non-accented version of the character should be considered
115// "close", but not the other keys close to the non-accented version.
116ProximityInfo::ProximityType ProximityInfo::getMatchedProximityId(
satok985312e2011-08-05 21:21:01 +0900117 const int index, const unsigned short c, const bool checkProximityChars) const {
satokd24df432011-07-14 15:43:42 +0900118 const int *currentChars = getProximityCharsAt(index);
119 const unsigned short baseLowerC = Dictionary::toBaseLowerCase(c);
120
121 // The first char in the array is what user typed. If it matches right away,
122 // that means the user typed that same char for this pos.
123 if (currentChars[0] == baseLowerC || currentChars[0] == c)
124 return SAME_OR_ACCENTED_OR_CAPITALIZED_CHAR;
125
satok985312e2011-08-05 21:21:01 +0900126 if (!checkProximityChars) return UNRELATED_CHAR;
satokd24df432011-07-14 15:43:42 +0900127
128 // If the non-accented, lowercased version of that first character matches c,
129 // then we have a non-accented version of the accented character the user
130 // typed. Treat it as a close char.
131 if (Dictionary::toBaseLowerCase(currentChars[0]) == baseLowerC)
132 return NEAR_PROXIMITY_CHAR;
133
134 // Not an exact nor an accent-alike match: search the list of close keys
135 int j = 1;
136 while (currentChars[j] > 0 && j < MAX_PROXIMITY_CHARS_SIZE) {
137 const bool matched = (currentChars[j] == baseLowerC || currentChars[j] == c);
138 if (matched) return NEAR_PROXIMITY_CHAR;
139 ++j;
140 }
141
142 // Was not included, signal this as an unrelated character.
143 return UNRELATED_CHAR;
144}
145
satok1d7eaf82011-07-13 10:32:02 +0900146bool ProximityInfo::sameAsTyped(const unsigned short *word, int length) const {
147 if (length != mInputLength) {
148 return false;
149 }
150 const int *inputCodes = mInputCodes;
151 while (length--) {
152 if ((unsigned int) *inputCodes != (unsigned int) *word) {
153 return false;
154 }
155 inputCodes += MAX_PROXIMITY_CHARS_SIZE;
156 word++;
157 }
158 return true;
159}
160
Ken Wakasace9e52a2011-06-18 13:09:55 +0900161} // namespace latinime