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