proximity_info.h revision addea83bad5751308fef508d79c6989b8872f050
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 "defines.h"
21#include "jni.h"
22#include "suggest/core/layout/proximity_info_utils.h"
23#include "utils/hash_map_compat.h"
24
25namespace latinime {
26
27class Correction;
28
29class ProximityInfo {
30 public:
31    ProximityInfo(JNIEnv *env, const jstring localeJStr,
32            const int keyboardWidth, const int keyboardHeight, const int gridWidth,
33            const int gridHeight, 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    int getNormalizedSquaredDistance(const int inputIndex, const int proximityIndex) const;
41    float getNormalizedSquaredDistanceFromCenterFloatG(
42            const int keyId, const int x, const int y,
43            const float verticalScale) const;
44    bool sameAsTyped(const unsigned short *word, int length) const;
45    int getCodePointOf(const int keyIndex) const;
46    bool hasSweetSpotData(const int keyIndex) const {
47        // When there are no calibration data for a key,
48        // the radius of the key is assigned to zero.
49        return mSweetSpotRadii[keyIndex] > 0.0f;
50    }
51    float getSweetSpotRadiiAt(int keyIndex) const { return mSweetSpotRadii[keyIndex]; }
52    float getSweetSpotCenterXAt(int keyIndex) const { return mSweetSpotCenterXs[keyIndex]; }
53    float getSweetSpotCenterYAt(int keyIndex) const { return mSweetSpotCenterYs[keyIndex]; }
54    void calculateNearbyKeyCodes(
55            const int x, const int y, const int primaryKey, int *inputCodes) const;
56    bool hasTouchPositionCorrectionData() const { return HAS_TOUCH_POSITION_CORRECTION_DATA; }
57    int getMostCommonKeyWidth() const { return MOST_COMMON_KEY_WIDTH; }
58    int getMostCommonKeyWidthSquare() const { return MOST_COMMON_KEY_WIDTH_SQUARE; }
59    float getNormalizedSquaredMostCommonKeyHypotenuse() const {
60        return NORMALIZED_SQUARED_MOST_COMMON_KEY_HYPOTENUSE;
61    }
62    int getKeyCount() const { return KEY_COUNT; }
63    int getCellHeight() const { return CELL_HEIGHT; }
64    int getCellWidth() const { return CELL_WIDTH; }
65    int getGridWidth() const { return GRID_WIDTH; }
66    int getGridHeight() const { return GRID_HEIGHT; }
67    int getKeyboardWidth() const { return KEYBOARD_WIDTH; }
68    int getKeyboardHeight() const { return KEYBOARD_HEIGHT; }
69    float getKeyboardHypotenuse() const { return KEYBOARD_HYPOTENUSE; }
70
71    int getKeyCenterXOfCodePointG(int charCode) const;
72    int getKeyCenterYOfCodePointG(int charCode) const;
73    int getKeyCenterXOfKeyIdG(int keyId) const;
74    int getKeyCenterYOfKeyIdG(int keyId) const;
75    int getKeyKeyDistanceG(int keyId0, int keyId1) const;
76
77    AK_FORCE_INLINE void initializeProximities(const int *const inputCodes,
78            const int *const inputXCoordinates, const int *const inputYCoordinates,
79            const int inputSize, int *allInputCodes) const {
80        ProximityInfoUtils::initializeProximities(inputCodes, inputXCoordinates, inputYCoordinates,
81                inputSize, mKeyXCoordinates, mKeyYCoordinates, mKeyWidths, mKeyHeights,
82                mProximityCharsArray, CELL_HEIGHT, CELL_WIDTH, GRID_WIDTH, MOST_COMMON_KEY_WIDTH,
83                KEY_COUNT, mLocaleStr, &mCodeToKeyMap, allInputCodes);
84    }
85
86    AK_FORCE_INLINE int getKeyIndexOf(const int c) const {
87        return ProximityInfoUtils::getKeyIndexOf(KEY_COUNT, c, &mCodeToKeyMap);
88    }
89
90    AK_FORCE_INLINE bool isCodePointOnKeyboard(const int codePoint) const {
91        return getKeyIndexOf(codePoint) != NOT_AN_INDEX;
92    }
93
94 private:
95    DISALLOW_IMPLICIT_CONSTRUCTORS(ProximityInfo);
96
97    void initializeG();
98    float calculateNormalizedSquaredDistance(const int keyIndex, const int inputIndex) const;
99    bool hasInputCoordinates() const;
100
101    const int GRID_WIDTH;
102    const int GRID_HEIGHT;
103    const int MOST_COMMON_KEY_WIDTH;
104    const int MOST_COMMON_KEY_WIDTH_SQUARE;
105    const int MOST_COMMON_KEY_HEIGHT;
106    const float NORMALIZED_SQUARED_MOST_COMMON_KEY_HYPOTENUSE;
107    const int CELL_WIDTH;
108    const int CELL_HEIGHT;
109    const int KEY_COUNT;
110    const int KEYBOARD_WIDTH;
111    const int KEYBOARD_HEIGHT;
112    const float KEYBOARD_HYPOTENUSE;
113    const bool HAS_TOUCH_POSITION_CORRECTION_DATA;
114    char mLocaleStr[MAX_LOCALE_STRING_LENGTH];
115    int *mProximityCharsArray;
116    int mKeyXCoordinates[MAX_KEY_COUNT_IN_A_KEYBOARD];
117    int mKeyYCoordinates[MAX_KEY_COUNT_IN_A_KEYBOARD];
118    int mKeyWidths[MAX_KEY_COUNT_IN_A_KEYBOARD];
119    int mKeyHeights[MAX_KEY_COUNT_IN_A_KEYBOARD];
120    int mKeyCodePoints[MAX_KEY_COUNT_IN_A_KEYBOARD];
121    float mSweetSpotCenterXs[MAX_KEY_COUNT_IN_A_KEYBOARD];
122    float mSweetSpotCenterYs[MAX_KEY_COUNT_IN_A_KEYBOARD];
123    float mSweetSpotRadii[MAX_KEY_COUNT_IN_A_KEYBOARD];
124    hash_map_compat<int, int> mCodeToKeyMap;
125
126    int mKeyIndexToCodePointG[MAX_KEY_COUNT_IN_A_KEYBOARD];
127    int mCenterXsG[MAX_KEY_COUNT_IN_A_KEYBOARD];
128    int mCenterYsG[MAX_KEY_COUNT_IN_A_KEYBOARD];
129    int mKeyKeyDistancesG[MAX_KEY_COUNT_IN_A_KEYBOARD][MAX_KEY_COUNT_IN_A_KEYBOARD];
130    // TODO: move to correction.h
131};
132} // namespace latinime
133#endif // LATINIME_PROXIMITY_INFO_H
134