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 
     22 #include "defines.h"
     23 #include "hash_map_compat.h"
     24 #include "jni.h"
     25 
     26 namespace latinime {
     27 
     28 class Correction;
     29 
     30 class ProximityInfo {
     31  public:
     32     ProximityInfo(JNIEnv *env, const jstring localeJStr, const int maxProximityCharsSize,
     33             const int keyboardWidth, const int keyboardHeight, const int gridWidth,
     34             const int gridHeight, const int mostCommonKeyWidth, const jintArray proximityChars,
     35             const int keyCount, const jintArray keyXCoordinates, const jintArray keyYCoordinates,
     36             const jintArray keyWidths, const jintArray keyHeights, const jintArray keyCharCodes,
     37             const jfloatArray sweetSpotCenterXs, const jfloatArray sweetSpotCenterYs,
     38             const jfloatArray sweetSpotRadii);
     39     ~ProximityInfo();
     40     bool hasSpaceProximity(const int x, const int y) const;
     41     int getNormalizedSquaredDistance(const int inputIndex, const int proximityIndex) const;
     42     float getNormalizedSquaredDistanceFromCenterFloatG(
     43             const int keyId, const int x, const int y) const;
     44     bool sameAsTyped(const unsigned short *word, int length) const;
     45     int getKeyIndexOf(const int c) const;
     46     int getCodePointOf(const int keyIndex) const;
     47     bool hasSweetSpotData(const int keyIndex) const {
     48         // When there are no calibration data for a key,
     49         // the radius of the key is assigned to zero.
     50         return mSweetSpotRadii[keyIndex] > 0.0f;
     51     }
     52     float getSweetSpotRadiiAt(int keyIndex) const {
     53         return mSweetSpotRadii[keyIndex];
     54     }
     55     float getSweetSpotCenterXAt(int keyIndex) const {
     56         return mSweetSpotCenterXs[keyIndex];
     57     }
     58     float getSweetSpotCenterYAt(int keyIndex) const {
     59         return mSweetSpotCenterYs[keyIndex];
     60     }
     61     void calculateNearbyKeyCodes(
     62             const int x, const int y, const int32_t primaryKey, int *inputCodes) const;
     63 
     64     bool hasTouchPositionCorrectionData() const {
     65         return HAS_TOUCH_POSITION_CORRECTION_DATA;
     66     }
     67 
     68     int getMostCommonKeyWidth() const {
     69         return MOST_COMMON_KEY_WIDTH;
     70     }
     71 
     72     int getMostCommonKeyWidthSquare() const {
     73         return MOST_COMMON_KEY_WIDTH_SQUARE;
     74     }
     75 
     76     const char *getLocaleStr() const {
     77         return mLocaleStr;
     78     }
     79 
     80     int getKeyCount() const {
     81         return KEY_COUNT;
     82     }
     83 
     84     int getCellHeight() const {
     85         return CELL_HEIGHT;
     86     }
     87 
     88     int getCellWidth() const {
     89         return CELL_WIDTH;
     90     }
     91 
     92     int getGridWidth() const {
     93         return GRID_WIDTH;
     94     }
     95 
     96     int getGridHeight() const {
     97         return GRID_HEIGHT;
     98     }
     99 
    100     int getKeyboardWidth() const {
    101         return KEYBOARD_WIDTH;
    102     }
    103 
    104     int getKeyboardHeight() const {
    105         return KEYBOARD_HEIGHT;
    106     }
    107 
    108     int getKeyCenterXOfCodePointG(int charCode) const;
    109     int getKeyCenterYOfCodePointG(int charCode) const;
    110     int getKeyCenterXOfKeyIdG(int keyId) const;
    111     int getKeyCenterYOfKeyIdG(int keyId) const;
    112     int getKeyKeyDistanceG(int key0, int key1) const;
    113 
    114  private:
    115     DISALLOW_IMPLICIT_CONSTRUCTORS(ProximityInfo);
    116     static const float NOT_A_DISTANCE_FLOAT;
    117 
    118     int getStartIndexFromCoordinates(const int x, const int y) const;
    119     void initializeG();
    120     float calculateNormalizedSquaredDistance(const int keyIndex, const int inputIndex) const;
    121     bool hasInputCoordinates() const;
    122     int squaredDistanceToEdge(const int keyId, const int x, const int y) const;
    123     bool isOnKey(const int keyId, const int x, const int y) const {
    124         if (keyId < 0) return true; // NOT_A_ID is -1, but return whenever < 0 just in case
    125         const int left = mKeyXCoordinates[keyId];
    126         const int top = mKeyYCoordinates[keyId];
    127         const int right = left + mKeyWidths[keyId] + 1;
    128         const int bottom = top + mKeyHeights[keyId];
    129         return left < right && top < bottom && x >= left && x < right && y >= top && y < bottom;
    130     }
    131 
    132     const int MAX_PROXIMITY_CHARS_SIZE;
    133     const int GRID_WIDTH;
    134     const int GRID_HEIGHT;
    135     const int MOST_COMMON_KEY_WIDTH;
    136     const int MOST_COMMON_KEY_WIDTH_SQUARE;
    137     const int CELL_WIDTH;
    138     const int CELL_HEIGHT;
    139     const int KEY_COUNT;
    140     const int KEYBOARD_WIDTH;
    141     const int KEYBOARD_HEIGHT;
    142     const bool HAS_TOUCH_POSITION_CORRECTION_DATA;
    143     char mLocaleStr[MAX_LOCALE_STRING_LENGTH];
    144     int32_t *mProximityCharsArray;
    145     int32_t mKeyXCoordinates[MAX_KEY_COUNT_IN_A_KEYBOARD];
    146     int32_t mKeyYCoordinates[MAX_KEY_COUNT_IN_A_KEYBOARD];
    147     int32_t mKeyWidths[MAX_KEY_COUNT_IN_A_KEYBOARD];
    148     int32_t mKeyHeights[MAX_KEY_COUNT_IN_A_KEYBOARD];
    149     int32_t mKeyCodePoints[MAX_KEY_COUNT_IN_A_KEYBOARD];
    150     float mSweetSpotCenterXs[MAX_KEY_COUNT_IN_A_KEYBOARD];
    151     float mSweetSpotCenterYs[MAX_KEY_COUNT_IN_A_KEYBOARD];
    152     float mSweetSpotRadii[MAX_KEY_COUNT_IN_A_KEYBOARD];
    153     hash_map_compat<int, int> mCodeToKeyMap;
    154 
    155     int mKeyIndexToCodePointG[MAX_KEY_COUNT_IN_A_KEYBOARD];
    156     int mCenterXsG[MAX_KEY_COUNT_IN_A_KEYBOARD];
    157     int mCenterYsG[MAX_KEY_COUNT_IN_A_KEYBOARD];
    158     int mKeyKeyDistancesG[MAX_KEY_COUNT_IN_A_KEYBOARD][MAX_KEY_COUNT_IN_A_KEYBOARD];
    159     // TODO: move to correction.h
    160 };
    161 } // namespace latinime
    162 #endif // LATINIME_PROXIMITY_INFO_H
    163