blob: 0d8c6a3ca00f3c97cdcb943c870910ca73dc94be [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>
21
22#include "defines.h"
Tom Ouyang13216852012-09-03 12:50:21 -070023#include "hash_map_compat.h"
Ken Wakasabb005f72012-08-08 20:43:47 +090024#include "jni.h"
satok8fbd5522011-02-22 17:28:55 +090025
26namespace latinime {
27
satokcfca3c62011-08-10 14:30:10 +090028class Correction;
satok2df30602011-07-15 13:49:00 +090029
Keisuke Kuroyanagi1cd7ca92012-09-14 18:03:10 +090030inline bool isSkippableChar(const uint16_t character) {
31 // TODO: Do not hardcode here
32 return character == '\'' || character == '-';
33}
34
satok8fbd5522011-02-22 17:28:55 +090035class ProximityInfo {
Ken Wakasae12e9b52012-01-06 12:24:38 +090036 public:
Ken Wakasa01511452012-08-09 15:58:15 +090037 ProximityInfo(JNIEnv *env, const jstring localeJStr, const int maxProximityCharsSize,
Tadashi G. Takaokaa58ebc72012-04-18 15:44:34 +090038 const int keyboardWidth, const int keyboardHeight, const int gridWidth,
Ken Wakasabb005f72012-08-08 20:43:47 +090039 const int gridHeight, const int mostCommonKeyWidth, const jintArray proximityChars,
40 const int keyCount, const jintArray keyXCoordinates, const jintArray keyYCoordinates,
41 const jintArray keyWidths, const jintArray keyHeights, const jintArray keyCharCodes,
42 const jfloatArray sweetSpotCenterXs, const jfloatArray sweetSpotCenterYs,
43 const jfloatArray sweetSpotRadii);
satok8fbd5522011-02-22 17:28:55 +090044 ~ProximityInfo();
satok817e5172011-03-04 06:06:45 -080045 bool hasSpaceProximity(const int x, const int y) const;
Satoshi Kataoka3e8c58f2012-06-05 17:55:52 +090046 int getNormalizedSquaredDistance(const int inputIndex, const int proximityIndex) const;
Satoshi Kataokae7398cd2012-08-13 20:20:04 +090047 float getNormalizedSquaredDistanceFromCenterFloat(
48 const int keyId, const int x, const int y) const;
Satoshi Kataoka3e8c58f2012-06-05 17:55:52 +090049 bool sameAsTyped(const unsigned short *word, int length) const;
Ken Wakasaf2789812012-09-04 12:49:46 +090050 int getKeyIndexOf(const int c) const;
51 int getCodePointOf(const int keyIndex) const;
Satoshi Kataoka3e8c58f2012-06-05 17:55:52 +090052 bool hasSweetSpotData(const int keyIndex) const {
53 // When there are no calibration data for a key,
54 // the radius of the key is assigned to zero.
Ken Wakasafee0ac62012-08-16 19:34:02 +090055 return mSweetSpotRadii[keyIndex] > 0.0f;
Satoshi Kataoka3e8c58f2012-06-05 17:55:52 +090056 }
57 float getSweetSpotRadiiAt(int keyIndex) const {
58 return mSweetSpotRadii[keyIndex];
59 }
60 float getSweetSpotCenterXAt(int keyIndex) const {
61 return mSweetSpotCenterXs[keyIndex];
62 }
63 float getSweetSpotCenterYAt(int keyIndex) const {
64 return mSweetSpotCenterYs[keyIndex];
65 }
66 void calculateNearbyKeyCodes(
67 const int x, const int y, const int32_t primaryKey, int *inputCodes) const;
68
Satoshi Kataoka4a3db702012-06-08 15:29:44 +090069 bool hasTouchPositionCorrectionData() const {
70 return HAS_TOUCH_POSITION_CORRECTION_DATA;
71 }
72
Satoshi Kataoka6b4a1d72012-08-10 15:42:56 +090073 int getMostCommonKeyWidth() const {
74 return MOST_COMMON_KEY_WIDTH;
75 }
76
Satoshi Kataoka4a3db702012-06-08 15:29:44 +090077 int getMostCommonKeyWidthSquare() const {
78 return MOST_COMMON_KEY_WIDTH_SQUARE;
79 }
80
Ken Wakasa9e0c7112012-08-09 22:26:58 +090081 const char *getLocaleStr() const {
82 return mLocaleStr;
Satoshi Kataoka4a3db702012-06-08 15:29:44 +090083 }
84
85 int getKeyCount() const {
86 return KEY_COUNT;
87 }
88
89 int getCellHeight() const {
90 return CELL_HEIGHT;
91 }
92
93 int getCellWidth() const {
94 return CELL_WIDTH;
95 }
96
97 int getGridWidth() const {
98 return GRID_WIDTH;
99 }
100
101 int getGridHeight() const {
102 return GRID_HEIGHT;
103 }
satok635f68e2011-08-10 22:19:33 +0900104
Keisuke Kuroyanagi95a49a52012-09-04 17:00:24 +0900105 int getKeyboardWidth() const {
106 return KEYBOARD_WIDTH;
107 }
108
109 int getKeyboardHeight() const {
110 return KEYBOARD_HEIGHT;
111 }
112
Ken Wakasa5964d4e2012-09-10 16:49:36 +0900113 int getKeyCenterXOfCodePointG(int charCode) const;
114 int getKeyCenterYOfCodePointG(int charCode) const;
115 int getKeyCenterXOfKeyIdG(int keyId) const;
116 int getKeyCenterYOfKeyIdG(int keyId) const;
Satoshi Kataoka6b4a1d72012-08-10 15:42:56 +0900117 int getKeyKeyDistanceG(int key0, int key1) const;
118
Ken Wakasae12e9b52012-01-06 12:24:38 +0900119 private:
satok1bc038c2012-06-14 11:25:50 -0700120 DISALLOW_IMPLICIT_CONSTRUCTORS(ProximityInfo);
Ken Wakasa162c2112012-08-24 14:51:15 +0900121 static const float NOT_A_DISTANCE_FLOAT;
Yusuke Nojima0e1f6562011-09-21 12:02:47 +0900122
satok817e5172011-03-04 06:06:45 -0800123 int getStartIndexFromCoordinates(const int x, const int y) const;
Satoshi Kataoka6b4a1d72012-08-10 15:42:56 +0900124 void initializeG();
Yusuke Nojimaa4c1f1c2011-10-06 19:12:20 +0900125 float calculateNormalizedSquaredDistance(const int keyIndex, const int inputIndex) const;
126 float calculateSquaredDistanceFromSweetSpotCenter(
127 const int keyIndex, const int inputIndex) const;
satok1caff472012-03-14 23:17:12 +0900128 bool hasInputCoordinates() const;
Ken Wakasafee0ac62012-08-16 19:34:02 +0900129 int squaredDistanceToEdge(const int keyId, const int x, const int y) const;
130 bool isOnKey(const int keyId, const int x, const int y) const {
131 if (keyId < 0) return true; // NOT_A_ID is -1, but return whenever < 0 just in case
132 const int left = mKeyXCoordinates[keyId];
133 const int top = mKeyYCoordinates[keyId];
134 const int right = left + mKeyWidths[keyId] + 1;
135 const int bottom = top + mKeyHeights[keyId];
136 return left < right && top < bottom && x >= left && x < right && y >= top && y < bottom;
137 }
Yusuke Nojimac25c7cc2011-10-03 16:44:29 +0900138
Ken Wakasade3070a2011-03-19 09:16:42 +0900139 const int MAX_PROXIMITY_CHARS_SIZE;
satok8fbd5522011-02-22 17:28:55 +0900140 const int GRID_WIDTH;
141 const int GRID_HEIGHT;
Satoshi Kataoka6b4a1d72012-08-10 15:42:56 +0900142 const int MOST_COMMON_KEY_WIDTH;
satoka70ee6e2012-03-07 15:12:22 +0900143 const int MOST_COMMON_KEY_WIDTH_SQUARE;
Ken Wakasade3070a2011-03-19 09:16:42 +0900144 const int CELL_WIDTH;
145 const int CELL_HEIGHT;
Yusuke Nojima0e1f6562011-09-21 12:02:47 +0900146 const int KEY_COUNT;
Keisuke Kuroyanagi95a49a52012-09-04 17:00:24 +0900147 const int KEYBOARD_WIDTH;
148 const int KEYBOARD_HEIGHT;
Yusuke Nojimaa4c1f1c2011-10-06 19:12:20 +0900149 const bool HAS_TOUCH_POSITION_CORRECTION_DATA;
Ken Wakasa9e0c7112012-08-09 22:26:58 +0900150 char mLocaleStr[MAX_LOCALE_STRING_LENGTH];
satok0cb20972012-03-13 22:07:56 +0900151 int32_t *mProximityCharsArray;
Yusuke Nojima0e1f6562011-09-21 12:02:47 +0900152 int32_t mKeyXCoordinates[MAX_KEY_COUNT_IN_A_KEYBOARD];
153 int32_t mKeyYCoordinates[MAX_KEY_COUNT_IN_A_KEYBOARD];
154 int32_t mKeyWidths[MAX_KEY_COUNT_IN_A_KEYBOARD];
155 int32_t mKeyHeights[MAX_KEY_COUNT_IN_A_KEYBOARD];
Ken Wakasaf2789812012-09-04 12:49:46 +0900156 int32_t mKeyCodePoints[MAX_KEY_COUNT_IN_A_KEYBOARD];
Yusuke Nojimaad358352011-09-29 16:44:54 +0900157 float mSweetSpotCenterXs[MAX_KEY_COUNT_IN_A_KEYBOARD];
158 float mSweetSpotCenterYs[MAX_KEY_COUNT_IN_A_KEYBOARD];
159 float mSweetSpotRadii[MAX_KEY_COUNT_IN_A_KEYBOARD];
Tom Ouyang13216852012-09-03 12:50:21 -0700160 hash_map_compat<int, int> mCodeToKeyMap;
Satoshi Kataoka6b4a1d72012-08-10 15:42:56 +0900161
Ken Wakasaf2789812012-09-04 12:49:46 +0900162 int mKeyIndexToCodePointG[MAX_KEY_COUNT_IN_A_KEYBOARD];
Satoshi Kataoka6b4a1d72012-08-10 15:42:56 +0900163 int mCenterXsG[MAX_KEY_COUNT_IN_A_KEYBOARD];
164 int mCenterYsG[MAX_KEY_COUNT_IN_A_KEYBOARD];
165 int mKeyKeyDistancesG[MAX_KEY_COUNT_IN_A_KEYBOARD][MAX_KEY_COUNT_IN_A_KEYBOARD];
Satoshi Kataoka3e8c58f2012-06-05 17:55:52 +0900166 // TODO: move to correction.h
satok8fbd5522011-02-22 17:28:55 +0900167};
Ken Wakasace9e52a2011-06-18 13:09:55 +0900168} // namespace latinime
satok8fbd5522011-02-22 17:28:55 +0900169#endif // LATINIME_PROXIMITY_INFO_H