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 <unordered_map>
21 #include <vector>
22 
23 #include "defines.h"
24 #include "jni.h"
25 #include "suggest/core/layout/proximity_info_utils.h"
26 
27 namespace latinime {
28 
29 class ProximityInfo {
30  public:
31     ProximityInfo(JNIEnv *env, const int keyboardWidth, const int keyboardHeight,
32             const int gridWidth, const int gridHeight,
33             const int mostCommonKeyWidth, const int mostCommonKeyHeight,
34             const jintArray proximityChars, const int keyCount, const jintArray keyXCoordinates,
35             const jintArray keyYCoordinates, const jintArray keyWidths, const jintArray keyHeights,
36             const jintArray keyCharCodes, const jfloatArray sweetSpotCenterXs,
37             const jfloatArray sweetSpotCenterYs, const jfloatArray sweetSpotRadii);
38     ~ProximityInfo();
39     bool hasSpaceProximity(const int x, const int y) const;
40     float getNormalizedSquaredDistanceFromCenterFloatG(
41             const int keyId, const int x, const int y, const bool isGeometric) const;
42     int getCodePointOf(const int keyIndex) const;
43     int getOriginalCodePointOf(const int keyIndex) const;
hasSweetSpotData(const int keyIndex)44     bool hasSweetSpotData(const int keyIndex) const {
45         // When there are no calibration data for a key,
46         // the radius of the key is assigned to zero.
47         return mSweetSpotRadii[keyIndex] > 0.0f;
48     }
getSweetSpotRadiiAt(int keyIndex)49     float getSweetSpotRadiiAt(int keyIndex) const { return mSweetSpotRadii[keyIndex]; }
getSweetSpotCenterXAt(int keyIndex)50     float getSweetSpotCenterXAt(int keyIndex) const { return mSweetSpotCenterXs[keyIndex]; }
getSweetSpotCenterYAt(int keyIndex)51     float getSweetSpotCenterYAt(int keyIndex) const { return mSweetSpotCenterYs[keyIndex]; }
hasTouchPositionCorrectionData()52     bool hasTouchPositionCorrectionData() const { return HAS_TOUCH_POSITION_CORRECTION_DATA; }
getMostCommonKeyWidth()53     int getMostCommonKeyWidth() const { return MOST_COMMON_KEY_WIDTH; }
getMostCommonKeyWidthSquare()54     int getMostCommonKeyWidthSquare() const { return MOST_COMMON_KEY_WIDTH_SQUARE; }
getNormalizedSquaredMostCommonKeyHypotenuse()55     float getNormalizedSquaredMostCommonKeyHypotenuse() const {
56         return NORMALIZED_SQUARED_MOST_COMMON_KEY_HYPOTENUSE;
57     }
getKeyCount()58     int getKeyCount() const { return KEY_COUNT; }
getCellHeight()59     int getCellHeight() const { return CELL_HEIGHT; }
getCellWidth()60     int getCellWidth() const { return CELL_WIDTH; }
getGridWidth()61     int getGridWidth() const { return GRID_WIDTH; }
getGridHeight()62     int getGridHeight() const { return GRID_HEIGHT; }
getKeyboardWidth()63     int getKeyboardWidth() const { return KEYBOARD_WIDTH; }
getKeyboardHeight()64     int getKeyboardHeight() const { return KEYBOARD_HEIGHT; }
getKeyboardHypotenuse()65     float getKeyboardHypotenuse() const { return KEYBOARD_HYPOTENUSE; }
66 
67     int getKeyCenterXOfKeyIdG(
68             const int keyId, const int referencePointX, const bool isGeometric) const;
69     int getKeyCenterYOfKeyIdG(
70             const int keyId, const int referencePointY, const bool isGeometric) const;
71     int getKeyKeyDistanceG(int keyId0, int keyId1) const;
72 
initializeProximities(const int * const inputCodes,const int * const inputXCoordinates,const int * const inputYCoordinates,const int inputSize,int * allInputCodes,const std::vector<int> * locale)73     AK_FORCE_INLINE void initializeProximities(const int *const inputCodes,
74             const int *const inputXCoordinates, const int *const inputYCoordinates,
75             const int inputSize, int *allInputCodes, const std::vector<int> *locale) const {
76         ProximityInfoUtils::initializeProximities(inputCodes, inputXCoordinates, inputYCoordinates,
77                 inputSize, mKeyXCoordinates, mKeyYCoordinates, mKeyWidths, mKeyHeights,
78                 mProximityCharsArray, CELL_HEIGHT, CELL_WIDTH, GRID_WIDTH, MOST_COMMON_KEY_WIDTH,
79                 KEY_COUNT, locale, &mLowerCodePointToKeyMap, allInputCodes);
80     }
81 
getKeyIndexOf(const int c)82     AK_FORCE_INLINE int getKeyIndexOf(const int c) const {
83         return ProximityInfoUtils::getKeyIndexOf(KEY_COUNT, c, &mLowerCodePointToKeyMap);
84     }
85 
isCodePointOnKeyboard(const int codePoint)86     AK_FORCE_INLINE bool isCodePointOnKeyboard(const int codePoint) const {
87         return getKeyIndexOf(codePoint) != NOT_AN_INDEX;
88     }
89 
90  private:
91     DISALLOW_IMPLICIT_CONSTRUCTORS(ProximityInfo);
92 
93     void initializeG();
94 
95     const int GRID_WIDTH;
96     const int GRID_HEIGHT;
97     const int MOST_COMMON_KEY_WIDTH;
98     const int MOST_COMMON_KEY_WIDTH_SQUARE;
99     const float NORMALIZED_SQUARED_MOST_COMMON_KEY_HYPOTENUSE;
100     const int CELL_WIDTH;
101     const int CELL_HEIGHT;
102     const int KEY_COUNT;
103     const int KEYBOARD_WIDTH;
104     const int KEYBOARD_HEIGHT;
105     const float KEYBOARD_HYPOTENUSE;
106     const bool HAS_TOUCH_POSITION_CORRECTION_DATA;
107     int *mProximityCharsArray;
108     int mKeyXCoordinates[MAX_KEY_COUNT_IN_A_KEYBOARD];
109     int mKeyYCoordinates[MAX_KEY_COUNT_IN_A_KEYBOARD];
110     int mKeyWidths[MAX_KEY_COUNT_IN_A_KEYBOARD];
111     int mKeyHeights[MAX_KEY_COUNT_IN_A_KEYBOARD];
112     int mKeyCodePoints[MAX_KEY_COUNT_IN_A_KEYBOARD];
113     float mSweetSpotCenterXs[MAX_KEY_COUNT_IN_A_KEYBOARD];
114     float mSweetSpotCenterYs[MAX_KEY_COUNT_IN_A_KEYBOARD];
115     // Sweet spots for geometric input. Note that we have extra sweet spots only for Y coordinates.
116     float mSweetSpotCenterYsG[MAX_KEY_COUNT_IN_A_KEYBOARD];
117     float mSweetSpotRadii[MAX_KEY_COUNT_IN_A_KEYBOARD];
118     std::unordered_map<int, int> mLowerCodePointToKeyMap;
119     int mKeyIndexToOriginalCodePoint[MAX_KEY_COUNT_IN_A_KEYBOARD];
120     int mKeyIndexToLowerCodePointG[MAX_KEY_COUNT_IN_A_KEYBOARD];
121     int mCenterXsG[MAX_KEY_COUNT_IN_A_KEYBOARD];
122     int mCenterYsG[MAX_KEY_COUNT_IN_A_KEYBOARD];
123     int mKeyKeyDistancesG[MAX_KEY_COUNT_IN_A_KEYBOARD][MAX_KEY_COUNT_IN_A_KEYBOARD];
124 };
125 } // namespace latinime
126 #endif // LATINIME_PROXIMITY_INFO_H
127