Home | History | Annotate | Download | only in layout
      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 
     25 namespace latinime {
     26 
     27 class ProximityInfo {
     28  public:
     29     ProximityInfo(JNIEnv *env, const jstring localeJStr,
     30             const int keyboardWidth, const int keyboardHeight, const int gridWidth,
     31             const int gridHeight, const int mostCommonKeyWidth, const int mostCommonKeyHeight,
     32             const jintArray proximityChars, const int keyCount, const jintArray keyXCoordinates,
     33             const jintArray keyYCoordinates, const jintArray keyWidths, const jintArray keyHeights,
     34             const jintArray keyCharCodes, const jfloatArray sweetSpotCenterXs,
     35             const jfloatArray sweetSpotCenterYs, const jfloatArray sweetSpotRadii);
     36     ~ProximityInfo();
     37     bool hasSpaceProximity(const int x, const int y) const;
     38     int getNormalizedSquaredDistance(const int inputIndex, const int proximityIndex) 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     bool hasSweetSpotData(const int keyIndex) const {
     43         // When there are no calibration data for a key,
     44         // the radius of the key is assigned to zero.
     45         return mSweetSpotRadii[keyIndex] > 0.0f;
     46     }
     47     float getSweetSpotRadiiAt(int keyIndex) const { return mSweetSpotRadii[keyIndex]; }
     48     float getSweetSpotCenterXAt(int keyIndex) const { return mSweetSpotCenterXs[keyIndex]; }
     49     float getSweetSpotCenterYAt(int keyIndex) const { return mSweetSpotCenterYs[keyIndex]; }
     50     void calculateNearbyKeyCodes(
     51             const int x, const int y, const int primaryKey, int *inputCodes) const;
     52     bool hasTouchPositionCorrectionData() const { return HAS_TOUCH_POSITION_CORRECTION_DATA; }
     53     int getMostCommonKeyWidth() const { return MOST_COMMON_KEY_WIDTH; }
     54     int getMostCommonKeyWidthSquare() const { return MOST_COMMON_KEY_WIDTH_SQUARE; }
     55     float getNormalizedSquaredMostCommonKeyHypotenuse() const {
     56         return NORMALIZED_SQUARED_MOST_COMMON_KEY_HYPOTENUSE;
     57     }
     58     int getKeyCount() const { return KEY_COUNT; }
     59     int getCellHeight() const { return CELL_HEIGHT; }
     60     int getCellWidth() const { return CELL_WIDTH; }
     61     int getGridWidth() const { return GRID_WIDTH; }
     62     int getGridHeight() const { return GRID_HEIGHT; }
     63     int getKeyboardWidth() const { return KEYBOARD_WIDTH; }
     64     int getKeyboardHeight() const { return KEYBOARD_HEIGHT; }
     65     float getKeyboardHypotenuse() const { return KEYBOARD_HYPOTENUSE; }
     66 
     67     int getKeyCenterXOfKeyIdG(
     68             const int keyId, const int referencePointX, const bool isGeometric) const;
     69     int getKeyCenterYOfKeyIdG(
     70             const int keyId, const int referencePointY, const bool isGeometric) const;
     71     int getKeyKeyDistanceG(int keyId0, int keyId1) const;
     72 
     73     AK_FORCE_INLINE void initializeProximities(const int *const inputCodes,
     74             const int *const inputXCoordinates, const int *const inputYCoordinates,
     75             const int inputSize, int *allInputCodes) const {
     76         ProximityInfoUtils::initializeProximities(inputCodes, inputXCoordinates, inputYCoordinates,
     77                 inputSize, mKeyXCoordinates, mKeyYCoordinates, mKeyWidths, mKeyHeights,
     78                 mProximityCharsArray, CELL_HEIGHT, CELL_WIDTH, GRID_WIDTH, MOST_COMMON_KEY_WIDTH,
     79                 KEY_COUNT, mLocaleStr, &mCodeToKeyMap, allInputCodes);
     80     }
     81 
     82     AK_FORCE_INLINE int getKeyIndexOf(const int c) const {
     83         return ProximityInfoUtils::getKeyIndexOf(KEY_COUNT, c, &mCodeToKeyMap);
     84     }
     85 
     86     AK_FORCE_INLINE bool isCodePointOnKeyboard(const int codePoint) const {
     87         return getKeyIndexOf(codePoint) != NOT_AN_INDEX;
     88     }
     89 
     90  private:
     91     DISALLOW_IMPLICIT_CONSTRUCTORS(ProximityInfo);
     92 
     93     void initializeG();
     94 
     95     const int GRID_WIDTH;
     96     const int GRID_HEIGHT;
     97     const int MOST_COMMON_KEY_WIDTH;
     98     const int MOST_COMMON_KEY_WIDTH_SQUARE;
     99     const int MOST_COMMON_KEY_HEIGHT;
    100     const float NORMALIZED_SQUARED_MOST_COMMON_KEY_HYPOTENUSE;
    101     const int CELL_WIDTH;
    102     const int CELL_HEIGHT;
    103     const int KEY_COUNT;
    104     const int KEYBOARD_WIDTH;
    105     const int KEYBOARD_HEIGHT;
    106     const float KEYBOARD_HYPOTENUSE;
    107     const bool HAS_TOUCH_POSITION_CORRECTION_DATA;
    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     hash_map_compat<int, int> mCodeToKeyMap;
    121 
    122     int mKeyIndexToCodePointG[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     // TODO: move to correction.h
    127 };
    128 } // namespace latinime
    129 #endif // LATINIME_PROXIMITY_INFO_H
    130