satok | 8fbd552 | 2011-02-22 17:28:55 +0900 | [diff] [blame] | 1 | /* |
| 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 Ouyang | 1321685 | 2012-09-03 12:50:21 -0700 | [diff] [blame] | 23 | #include "hash_map_compat.h" |
Ken Wakasa | bb005f7 | 2012-08-08 20:43:47 +0900 | [diff] [blame] | 24 | #include "jni.h" |
satok | 8fbd552 | 2011-02-22 17:28:55 +0900 | [diff] [blame] | 25 | |
| 26 | namespace latinime { |
| 27 | |
satok | cfca3c6 | 2011-08-10 14:30:10 +0900 | [diff] [blame] | 28 | class Correction; |
satok | 2df3060 | 2011-07-15 13:49:00 +0900 | [diff] [blame] | 29 | |
Keisuke Kuroyanagi | 1cd7ca9 | 2012-09-14 18:03:10 +0900 | [diff] [blame] | 30 | inline bool isSkippableChar(const uint16_t character) { |
| 31 | // TODO: Do not hardcode here |
| 32 | return character == '\'' || character == '-'; |
| 33 | } |
| 34 | |
satok | 8fbd552 | 2011-02-22 17:28:55 +0900 | [diff] [blame] | 35 | class ProximityInfo { |
Ken Wakasa | e12e9b5 | 2012-01-06 12:24:38 +0900 | [diff] [blame] | 36 | public: |
Ken Wakasa | 0151145 | 2012-08-09 15:58:15 +0900 | [diff] [blame] | 37 | ProximityInfo(JNIEnv *env, const jstring localeJStr, const int maxProximityCharsSize, |
Tadashi G. Takaoka | a58ebc7 | 2012-04-18 15:44:34 +0900 | [diff] [blame] | 38 | const int keyboardWidth, const int keyboardHeight, const int gridWidth, |
Ken Wakasa | bb005f7 | 2012-08-08 20:43:47 +0900 | [diff] [blame] | 39 | 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); |
satok | 8fbd552 | 2011-02-22 17:28:55 +0900 | [diff] [blame] | 44 | ~ProximityInfo(); |
satok | 817e517 | 2011-03-04 06:06:45 -0800 | [diff] [blame] | 45 | bool hasSpaceProximity(const int x, const int y) const; |
Satoshi Kataoka | 3e8c58f | 2012-06-05 17:55:52 +0900 | [diff] [blame] | 46 | int getNormalizedSquaredDistance(const int inputIndex, const int proximityIndex) const; |
Satoshi Kataoka | 0edab9d | 2012-09-24 18:29:31 +0900 | [diff] [blame^] | 47 | float getNormalizedSquaredDistanceFromCenterFloatG( |
Satoshi Kataoka | e7398cd | 2012-08-13 20:20:04 +0900 | [diff] [blame] | 48 | const int keyId, const int x, const int y) const; |
Satoshi Kataoka | 3e8c58f | 2012-06-05 17:55:52 +0900 | [diff] [blame] | 49 | bool sameAsTyped(const unsigned short *word, int length) const; |
Ken Wakasa | f278981 | 2012-09-04 12:49:46 +0900 | [diff] [blame] | 50 | int getKeyIndexOf(const int c) const; |
| 51 | int getCodePointOf(const int keyIndex) const; |
Satoshi Kataoka | 3e8c58f | 2012-06-05 17:55:52 +0900 | [diff] [blame] | 52 | 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 Wakasa | fee0ac6 | 2012-08-16 19:34:02 +0900 | [diff] [blame] | 55 | return mSweetSpotRadii[keyIndex] > 0.0f; |
Satoshi Kataoka | 3e8c58f | 2012-06-05 17:55:52 +0900 | [diff] [blame] | 56 | } |
| 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 Kataoka | 4a3db70 | 2012-06-08 15:29:44 +0900 | [diff] [blame] | 69 | bool hasTouchPositionCorrectionData() const { |
| 70 | return HAS_TOUCH_POSITION_CORRECTION_DATA; |
| 71 | } |
| 72 | |
Satoshi Kataoka | 6b4a1d7 | 2012-08-10 15:42:56 +0900 | [diff] [blame] | 73 | int getMostCommonKeyWidth() const { |
| 74 | return MOST_COMMON_KEY_WIDTH; |
| 75 | } |
| 76 | |
Satoshi Kataoka | 4a3db70 | 2012-06-08 15:29:44 +0900 | [diff] [blame] | 77 | int getMostCommonKeyWidthSquare() const { |
| 78 | return MOST_COMMON_KEY_WIDTH_SQUARE; |
| 79 | } |
| 80 | |
Ken Wakasa | 9e0c711 | 2012-08-09 22:26:58 +0900 | [diff] [blame] | 81 | const char *getLocaleStr() const { |
| 82 | return mLocaleStr; |
Satoshi Kataoka | 4a3db70 | 2012-06-08 15:29:44 +0900 | [diff] [blame] | 83 | } |
| 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 | } |
satok | 635f68e | 2011-08-10 22:19:33 +0900 | [diff] [blame] | 104 | |
Keisuke Kuroyanagi | 95a49a5 | 2012-09-04 17:00:24 +0900 | [diff] [blame] | 105 | int getKeyboardWidth() const { |
| 106 | return KEYBOARD_WIDTH; |
| 107 | } |
| 108 | |
| 109 | int getKeyboardHeight() const { |
| 110 | return KEYBOARD_HEIGHT; |
| 111 | } |
| 112 | |
Ken Wakasa | 5964d4e | 2012-09-10 16:49:36 +0900 | [diff] [blame] | 113 | int getKeyCenterXOfCodePointG(int charCode) const; |
| 114 | int getKeyCenterYOfCodePointG(int charCode) const; |
| 115 | int getKeyCenterXOfKeyIdG(int keyId) const; |
| 116 | int getKeyCenterYOfKeyIdG(int keyId) const; |
Satoshi Kataoka | 6b4a1d7 | 2012-08-10 15:42:56 +0900 | [diff] [blame] | 117 | int getKeyKeyDistanceG(int key0, int key1) const; |
| 118 | |
Ken Wakasa | e12e9b5 | 2012-01-06 12:24:38 +0900 | [diff] [blame] | 119 | private: |
satok | 1bc038c | 2012-06-14 11:25:50 -0700 | [diff] [blame] | 120 | DISALLOW_IMPLICIT_CONSTRUCTORS(ProximityInfo); |
Ken Wakasa | 162c211 | 2012-08-24 14:51:15 +0900 | [diff] [blame] | 121 | static const float NOT_A_DISTANCE_FLOAT; |
Yusuke Nojima | 0e1f656 | 2011-09-21 12:02:47 +0900 | [diff] [blame] | 122 | |
satok | 817e517 | 2011-03-04 06:06:45 -0800 | [diff] [blame] | 123 | int getStartIndexFromCoordinates(const int x, const int y) const; |
Satoshi Kataoka | 6b4a1d7 | 2012-08-10 15:42:56 +0900 | [diff] [blame] | 124 | void initializeG(); |
Yusuke Nojima | a4c1f1c | 2011-10-06 19:12:20 +0900 | [diff] [blame] | 125 | float calculateNormalizedSquaredDistance(const int keyIndex, const int inputIndex) const; |
satok | 1caff47 | 2012-03-14 23:17:12 +0900 | [diff] [blame] | 126 | bool hasInputCoordinates() const; |
Ken Wakasa | fee0ac6 | 2012-08-16 19:34:02 +0900 | [diff] [blame] | 127 | int squaredDistanceToEdge(const int keyId, const int x, const int y) const; |
| 128 | bool isOnKey(const int keyId, const int x, const int y) const { |
| 129 | if (keyId < 0) return true; // NOT_A_ID is -1, but return whenever < 0 just in case |
| 130 | const int left = mKeyXCoordinates[keyId]; |
| 131 | const int top = mKeyYCoordinates[keyId]; |
| 132 | const int right = left + mKeyWidths[keyId] + 1; |
| 133 | const int bottom = top + mKeyHeights[keyId]; |
| 134 | return left < right && top < bottom && x >= left && x < right && y >= top && y < bottom; |
| 135 | } |
Yusuke Nojima | c25c7cc | 2011-10-03 16:44:29 +0900 | [diff] [blame] | 136 | |
Ken Wakasa | de3070a | 2011-03-19 09:16:42 +0900 | [diff] [blame] | 137 | const int MAX_PROXIMITY_CHARS_SIZE; |
satok | 8fbd552 | 2011-02-22 17:28:55 +0900 | [diff] [blame] | 138 | const int GRID_WIDTH; |
| 139 | const int GRID_HEIGHT; |
Satoshi Kataoka | 6b4a1d7 | 2012-08-10 15:42:56 +0900 | [diff] [blame] | 140 | const int MOST_COMMON_KEY_WIDTH; |
satok | a70ee6e | 2012-03-07 15:12:22 +0900 | [diff] [blame] | 141 | const int MOST_COMMON_KEY_WIDTH_SQUARE; |
Ken Wakasa | de3070a | 2011-03-19 09:16:42 +0900 | [diff] [blame] | 142 | const int CELL_WIDTH; |
| 143 | const int CELL_HEIGHT; |
Yusuke Nojima | 0e1f656 | 2011-09-21 12:02:47 +0900 | [diff] [blame] | 144 | const int KEY_COUNT; |
Keisuke Kuroyanagi | 95a49a5 | 2012-09-04 17:00:24 +0900 | [diff] [blame] | 145 | const int KEYBOARD_WIDTH; |
| 146 | const int KEYBOARD_HEIGHT; |
Yusuke Nojima | a4c1f1c | 2011-10-06 19:12:20 +0900 | [diff] [blame] | 147 | const bool HAS_TOUCH_POSITION_CORRECTION_DATA; |
Ken Wakasa | 9e0c711 | 2012-08-09 22:26:58 +0900 | [diff] [blame] | 148 | char mLocaleStr[MAX_LOCALE_STRING_LENGTH]; |
satok | 0cb2097 | 2012-03-13 22:07:56 +0900 | [diff] [blame] | 149 | int32_t *mProximityCharsArray; |
Yusuke Nojima | 0e1f656 | 2011-09-21 12:02:47 +0900 | [diff] [blame] | 150 | int32_t mKeyXCoordinates[MAX_KEY_COUNT_IN_A_KEYBOARD]; |
| 151 | int32_t mKeyYCoordinates[MAX_KEY_COUNT_IN_A_KEYBOARD]; |
| 152 | int32_t mKeyWidths[MAX_KEY_COUNT_IN_A_KEYBOARD]; |
| 153 | int32_t mKeyHeights[MAX_KEY_COUNT_IN_A_KEYBOARD]; |
Ken Wakasa | f278981 | 2012-09-04 12:49:46 +0900 | [diff] [blame] | 154 | int32_t mKeyCodePoints[MAX_KEY_COUNT_IN_A_KEYBOARD]; |
Yusuke Nojima | ad35835 | 2011-09-29 16:44:54 +0900 | [diff] [blame] | 155 | float mSweetSpotCenterXs[MAX_KEY_COUNT_IN_A_KEYBOARD]; |
| 156 | float mSweetSpotCenterYs[MAX_KEY_COUNT_IN_A_KEYBOARD]; |
| 157 | float mSweetSpotRadii[MAX_KEY_COUNT_IN_A_KEYBOARD]; |
Tom Ouyang | 1321685 | 2012-09-03 12:50:21 -0700 | [diff] [blame] | 158 | hash_map_compat<int, int> mCodeToKeyMap; |
Satoshi Kataoka | 6b4a1d7 | 2012-08-10 15:42:56 +0900 | [diff] [blame] | 159 | |
Ken Wakasa | f278981 | 2012-09-04 12:49:46 +0900 | [diff] [blame] | 160 | int mKeyIndexToCodePointG[MAX_KEY_COUNT_IN_A_KEYBOARD]; |
Satoshi Kataoka | 6b4a1d7 | 2012-08-10 15:42:56 +0900 | [diff] [blame] | 161 | int mCenterXsG[MAX_KEY_COUNT_IN_A_KEYBOARD]; |
| 162 | int mCenterYsG[MAX_KEY_COUNT_IN_A_KEYBOARD]; |
| 163 | int mKeyKeyDistancesG[MAX_KEY_COUNT_IN_A_KEYBOARD][MAX_KEY_COUNT_IN_A_KEYBOARD]; |
Satoshi Kataoka | 3e8c58f | 2012-06-05 17:55:52 +0900 | [diff] [blame] | 164 | // TODO: move to correction.h |
satok | 8fbd552 | 2011-02-22 17:28:55 +0900 | [diff] [blame] | 165 | }; |
Ken Wakasa | ce9e52a | 2011-06-18 13:09:55 +0900 | [diff] [blame] | 166 | } // namespace latinime |
satok | 8fbd552 | 2011-02-22 17:28:55 +0900 | [diff] [blame] | 167 | #endif // LATINIME_PROXIMITY_INFO_H |