• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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"
23 
24 namespace latinime {
25 
26 class Correction;
27 
28 class ProximityInfo {
29 public:
30     static const int NORMALIZED_SQUARED_DISTANCE_SCALING_FACTOR_LOG_2 = 10;
31     static const int NORMALIZED_SQUARED_DISTANCE_SCALING_FACTOR =
32             1 << NORMALIZED_SQUARED_DISTANCE_SCALING_FACTOR_LOG_2;
33 
34     // Used as a return value for character comparison
35     typedef enum {
36         // Same char, possibly with different case or accent
37         EQUIVALENT_CHAR,
38         // It is a char located nearby on the keyboard
39         NEAR_PROXIMITY_CHAR,
40         // It is an unrelated char
41         UNRELATED_CHAR
42     } ProximityType;
43 
44     ProximityInfo(const int maxProximityCharsSize, const int keyboardWidth,
45             const int keybaordHeight, const int gridWidth, const int gridHeight,
46             const uint32_t *proximityCharsArray, const int keyCount, const int32_t *keyXCoordinates,
47             const int32_t *keyYCoordinates, const int32_t *keyWidths, const int32_t *keyHeights,
48             const int32_t *keyCharCodes, const float *sweetSpotCenterXs,
49             const float *sweetSpotCenterYs, const float *sweetSpotRadii);
50     ~ProximityInfo();
51     bool hasSpaceProximity(const int x, const int y) const;
52     void setInputParams(const int* inputCodes, const int inputLength,
53             const int *xCoordinates, const int *yCoordinates);
54     const int* getProximityCharsAt(const int index) const;
55     unsigned short getPrimaryCharAt(const int index) const;
56     bool existsCharInProximityAt(const int index, const int c) const;
57     bool existsAdjacentProximityChars(const int index) const;
58     ProximityType getMatchedProximityId(const int index, const unsigned short c,
59             const bool checkProximityChars, int *proximityIndex = NULL) const;
getNormalizedSquaredDistance(const int inputIndex,const int proximityIndex)60     int getNormalizedSquaredDistance(const int inputIndex, const int proximityIndex) const {
61         return mNormalizedSquaredDistances[inputIndex * MAX_PROXIMITY_CHARS_SIZE + proximityIndex];
62     }
63     bool sameAsTyped(const unsigned short *word, int length) const;
getPrimaryInputWord()64     const unsigned short* getPrimaryInputWord() const {
65         return mPrimaryInputWord;
66     }
touchPositionCorrectionEnabled()67     bool touchPositionCorrectionEnabled() const {
68         return mTouchPositionCorrectionEnabled;
69     }
70 
71 private:
72     // The max number of the keys in one keyboard layout
73     static const int MAX_KEY_COUNT_IN_A_KEYBOARD = 64;
74     // The upper limit of the char code in mCodeToKeyIndex
75     static const int MAX_CHAR_CODE = 127;
76 
77     int getStartIndexFromCoordinates(const int x, const int y) const;
78     void initializeCodeToKeyIndex();
79     float calculateNormalizedSquaredDistance(const int keyIndex, const int inputIndex) const;
80     float calculateSquaredDistanceFromSweetSpotCenter(
81             const int keyIndex, const int inputIndex) const;
82     int getKeyIndex(const int c) const;
hasSweetSpotData(const int keyIndex)83     bool hasSweetSpotData(const int keyIndex) const {
84         // When there are no calibration data for a key,
85         // the radius of the key is assigned to zero.
86         return mSweetSpotRadii[keyIndex] > 0.0;
87     }
88 
89     const int MAX_PROXIMITY_CHARS_SIZE;
90     const int KEYBOARD_WIDTH;
91     const int KEYBOARD_HEIGHT;
92     const int GRID_WIDTH;
93     const int GRID_HEIGHT;
94     const int CELL_WIDTH;
95     const int CELL_HEIGHT;
96     const int KEY_COUNT;
97     const bool HAS_TOUCH_POSITION_CORRECTION_DATA;
98     const int *mInputCodes;
99     const int *mInputXCoordinates;
100     const int *mInputYCoordinates;
101     bool mTouchPositionCorrectionEnabled;
102     uint32_t *mProximityCharsArray;
103     int *mNormalizedSquaredDistances;
104     int32_t mKeyXCoordinates[MAX_KEY_COUNT_IN_A_KEYBOARD];
105     int32_t mKeyYCoordinates[MAX_KEY_COUNT_IN_A_KEYBOARD];
106     int32_t mKeyWidths[MAX_KEY_COUNT_IN_A_KEYBOARD];
107     int32_t mKeyHeights[MAX_KEY_COUNT_IN_A_KEYBOARD];
108     int32_t mKeyCharCodes[MAX_KEY_COUNT_IN_A_KEYBOARD];
109     float mSweetSpotCenterXs[MAX_KEY_COUNT_IN_A_KEYBOARD];
110     float mSweetSpotCenterYs[MAX_KEY_COUNT_IN_A_KEYBOARD];
111     float mSweetSpotRadii[MAX_KEY_COUNT_IN_A_KEYBOARD];
112     int mInputLength;
113     unsigned short mPrimaryInputWord[MAX_WORD_LENGTH_INTERNAL];
114     int mCodeToKeyIndex[MAX_CHAR_CODE + 1];
115 };
116 
117 } // namespace latinime
118 
119 #endif // LATINIME_PROXIMITY_INFO_H
120