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