blob: a5ed57d1a4f8b7a8c9715694d6e784399ce5e562 [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#ifndef LATINIME_PROXIMITY_INFO_H
18#define LATINIME_PROXIMITY_INFO_H
19
20#include <stdint.h>
satok552c3c22012-03-13 16:33:47 +090021#include <string>
satok8fbd5522011-02-22 17:28:55 +090022
23#include "defines.h"
24
25namespace latinime {
26
satokcfca3c62011-08-10 14:30:10 +090027class Correction;
Satoshi Kataoka3e8c58f2012-06-05 17:55:52 +090028class ProximityInfoState;
satok2df30602011-07-15 13:49:00 +090029
satok8fbd5522011-02-22 17:28:55 +090030class ProximityInfo {
Ken Wakasae12e9b52012-01-06 12:24:38 +090031 public:
satokd24df432011-07-14 15:43:42 +090032
satok552c3c22012-03-13 16:33:47 +090033 ProximityInfo(const std::string localeStr, const int maxProximityCharsSize,
Tadashi G. Takaokaa58ebc72012-04-18 15:44:34 +090034 const int keyboardWidth, const int keyboardHeight, const int gridWidth,
satok552c3c22012-03-13 16:33:47 +090035 const int gridHeight, const int mostCommonkeyWidth,
satok0cb20972012-03-13 22:07:56 +090036 const int32_t *proximityCharsArray, const int keyCount, const int32_t *keyXCoordinates,
Yusuke Nojima0e1f6562011-09-21 12:02:47 +090037 const int32_t *keyYCoordinates, const int32_t *keyWidths, const int32_t *keyHeights,
Yusuke Nojimaad358352011-09-29 16:44:54 +090038 const int32_t *keyCharCodes, const float *sweetSpotCenterXs,
39 const float *sweetSpotCenterYs, const float *sweetSpotRadii);
satok8fbd5522011-02-22 17:28:55 +090040 ~ProximityInfo();
satok817e5172011-03-04 06:06:45 -080041 bool hasSpaceProximity(const int x, const int y) const;
Satoshi Kataoka3e8c58f2012-06-05 17:55:52 +090042 int getNormalizedSquaredDistance(const int inputIndex, const int proximityIndex) const;
43 bool sameAsTyped(const unsigned short *word, int length) const;
44 int squaredDistanceToEdge(const int keyId, const int x, const int y) const;
45 bool isOnKey(const int keyId, const int x, const int y) const {
46 if (keyId < 0) return true; // NOT_A_ID is -1, but return whenever < 0 just in case
47 const int left = mKeyXCoordinates[keyId];
48 const int top = mKeyYCoordinates[keyId];
49 const int right = left + mKeyWidths[keyId] + 1;
50 const int bottom = top + mKeyHeights[keyId];
51 return left < right && top < bottom && x >= left && x < right && y >= top && y < bottom;
52 }
53 int getKeyIndex(const int c) const;
54 bool hasSweetSpotData(const int keyIndex) const {
55 // When there are no calibration data for a key,
56 // the radius of the key is assigned to zero.
57 return mSweetSpotRadii[keyIndex] > 0.0;
58 }
59 float getSweetSpotRadiiAt(int keyIndex) const {
60 return mSweetSpotRadii[keyIndex];
61 }
62 float getSweetSpotCenterXAt(int keyIndex) const {
63 return mSweetSpotCenterXs[keyIndex];
64 }
65 float getSweetSpotCenterYAt(int keyIndex) const {
66 return mSweetSpotCenterYs[keyIndex];
67 }
68 void calculateNearbyKeyCodes(
69 const int x, const int y, const int32_t primaryKey, int *inputCodes) const;
70
71 ////////////////////////////////////
72 // Access to proximity info state //
73 // TODO: remove //
74 ////////////////////////////////////
75 void initInputParams(const int32_t *inputCodes, const int inputLength,
Yusuke Nojima258bfe62011-09-28 12:59:43 +090076 const int *xCoordinates, const int *yCoordinates);
satok1d7eaf82011-07-13 10:32:02 +090077 const int* getProximityCharsAt(const int index) const;
satokd24df432011-07-14 15:43:42 +090078 unsigned short getPrimaryCharAt(const int index) const;
79 bool existsCharInProximityAt(const int index, const int c) const;
80 bool existsAdjacentProximityChars(const int index) const;
Yusuke Nojimaa4c1f1c2011-10-06 19:12:20 +090081 ProximityType getMatchedProximityId(const int index, const unsigned short c,
Tadashi G. Takaoka0e971482011-10-28 17:02:09 +090082 const bool checkProximityChars, int *proximityIndex = 0) const;
Satoshi Kataoka3e8c58f2012-06-05 17:55:52 +090083 const unsigned short* getPrimaryInputWord() const;
84 bool touchPositionCorrectionEnabled() const;
85 ////////////////////////////////////
satok635f68e2011-08-10 22:19:33 +090086
Ken Wakasae12e9b52012-01-06 12:24:38 +090087 private:
Yusuke Nojima0e1f6562011-09-21 12:02:47 +090088 // The max number of the keys in one keyboard layout
89 static const int MAX_KEY_COUNT_IN_A_KEYBOARD = 64;
Yusuke Nojimaad358352011-09-29 16:44:54 +090090 // The upper limit of the char code in mCodeToKeyIndex
91 static const int MAX_CHAR_CODE = 127;
satok1caff472012-03-14 23:17:12 +090092 static const float NOT_A_DISTANCE_FLOAT = -1.0f;
93 static const int NOT_A_CODE = -1;
Yusuke Nojima0e1f6562011-09-21 12:02:47 +090094
satok817e5172011-03-04 06:06:45 -080095 int getStartIndexFromCoordinates(const int x, const int y) const;
Yusuke Nojima0e1f6562011-09-21 12:02:47 +090096 void initializeCodeToKeyIndex();
Yusuke Nojimaa4c1f1c2011-10-06 19:12:20 +090097 float calculateNormalizedSquaredDistance(const int keyIndex, const int inputIndex) const;
98 float calculateSquaredDistanceFromSweetSpotCenter(
99 const int keyIndex, const int inputIndex) const;
satok1caff472012-03-14 23:17:12 +0900100 bool hasInputCoordinates() const;
Yusuke Nojimac25c7cc2011-10-03 16:44:29 +0900101
Ken Wakasade3070a2011-03-19 09:16:42 +0900102 const int MAX_PROXIMITY_CHARS_SIZE;
satok817e5172011-03-04 06:06:45 -0800103 const int KEYBOARD_WIDTH;
104 const int KEYBOARD_HEIGHT;
satok8fbd5522011-02-22 17:28:55 +0900105 const int GRID_WIDTH;
106 const int GRID_HEIGHT;
satoka70ee6e2012-03-07 15:12:22 +0900107 const int MOST_COMMON_KEY_WIDTH_SQUARE;
Ken Wakasade3070a2011-03-19 09:16:42 +0900108 const int CELL_WIDTH;
109 const int CELL_HEIGHT;
Yusuke Nojima0e1f6562011-09-21 12:02:47 +0900110 const int KEY_COUNT;
Yusuke Nojimaa4c1f1c2011-10-06 19:12:20 +0900111 const bool HAS_TOUCH_POSITION_CORRECTION_DATA;
satok5eec5742012-03-13 18:26:23 +0900112 const std::string mLocaleStr;
satok0cb20972012-03-13 22:07:56 +0900113 int32_t *mProximityCharsArray;
Yusuke Nojima0e1f6562011-09-21 12:02:47 +0900114 int32_t mKeyXCoordinates[MAX_KEY_COUNT_IN_A_KEYBOARD];
115 int32_t mKeyYCoordinates[MAX_KEY_COUNT_IN_A_KEYBOARD];
116 int32_t mKeyWidths[MAX_KEY_COUNT_IN_A_KEYBOARD];
117 int32_t mKeyHeights[MAX_KEY_COUNT_IN_A_KEYBOARD];
118 int32_t mKeyCharCodes[MAX_KEY_COUNT_IN_A_KEYBOARD];
Yusuke Nojimaad358352011-09-29 16:44:54 +0900119 float mSweetSpotCenterXs[MAX_KEY_COUNT_IN_A_KEYBOARD];
120 float mSweetSpotCenterYs[MAX_KEY_COUNT_IN_A_KEYBOARD];
121 float mSweetSpotRadii[MAX_KEY_COUNT_IN_A_KEYBOARD];
Yusuke Nojimaad358352011-09-29 16:44:54 +0900122 int mCodeToKeyIndex[MAX_CHAR_CODE + 1];
Satoshi Kataoka3e8c58f2012-06-05 17:55:52 +0900123 // TODO: move to correction.h
124 ProximityInfoState *mProximityInfoState;
satok8fbd5522011-02-22 17:28:55 +0900125};
Ken Wakasace9e52a2011-06-18 13:09:55 +0900126
127} // namespace latinime
128
satok8fbd5522011-02-22 17:28:55 +0900129#endif // LATINIME_PROXIMITY_INFO_H