blob: 3c4480d863b0b441fb0db26e4947dc5bb762da51 [file] [log] [blame]
Ria Runjie Jiang1e6163c2015-08-06 13:06:25 -07001// Copyright 2015 The Chromium Authors. All rights reserved.
2// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
4
5#include <algorithm>
6#include <cmath>
7#include <new>
8#include <vector>
9
10#include "services/prediction/proximity_info_factory.h"
11#include "services/prediction/touch_position_correction.h"
12
13// NOTE: This class has been translated to C++ and modified from the Android
14// Open Source Project. Specifically from some functions of the following file:
15// https://android.googlesource.com/platform/packages/inputmethods/LatinIME/+/
16// android-5.1.1_r8/java/src/com/android/inputmethod/keyboard/ProximityInfo.java
17
18namespace prediction {
19
20const float ProximityInfoFactory::SEARCH_DISTANCE = 1.2f;
21
22const float ProximityInfoFactory::DEFAULT_TOUCH_POSITION_CORRECTION_RADIUS =
23 0.15f;
24
25// Hardcoded qwerty keyboard proximity settings
26ProximityInfoFactory::ProximityInfoFactory() {
27 plocale_ = "en";
28 pgrid_width_ = 32;
29 pgrid_height_ = 16;
30 pgrid_size_ = pgrid_width_ * pgrid_height_;
31 pcell_width_ = (348 + pgrid_width_ - 1) / pgrid_width_;
32 pcell_height_ = (174 + pgrid_height_ - 1) / pgrid_height_;
33 pkeyboard_min_width_ = 348;
34 pkeyboard_height_ = 174;
35 pmost_common_key_height_ = 29;
36 pmost_common_key_width_ = 58;
37}
38
39ProximityInfoFactory::~ProximityInfoFactory() {
40}
41
42latinime::ProximityInfo* ProximityInfoFactory::GetNativeProximityInfo() {
43 const int default_width = pmost_common_key_width_;
44 const int threshold = (int)(default_width * SEARCH_DISTANCE);
45 const int threshold_squared = threshold * threshold;
46 const int last_pixel_x_coordinate = pgrid_width_ * pcell_width_ - 1;
47 const int last_pixel_y_coordinate = pgrid_height_ * pcell_height_ - 1;
48
49 std::vector<Key> pgrid_neighbors[32 * 16 /*pgrid_size_*/];
50 int neighbor_count_per_cell[pgrid_size_];
51 std::fill_n(neighbor_count_per_cell, pgrid_size_, 0);
52 Key neighbors_flat_buffer[32 * 16 * 26 /*pgrid_size_ * keyset::key_count*/];
53
54 const int half_cell_width = pcell_width_ / 2;
55 const int half_cell_height = pcell_height_ / 2;
56 for (int i = 0; i < keyset::key_count; i++) {
57 const Key key = keyset::key_set[i];
58
59 const int key_x = key.kx;
60 const int key_y = key.ky;
61 const int top_pixel_within_threshold = key_y - threshold;
62 const int y_delta_to_grid = top_pixel_within_threshold % pcell_height_;
63 const int y_middle_of_top_cell =
64 top_pixel_within_threshold - y_delta_to_grid + half_cell_height;
65 const int y_start =
66 std::max(half_cell_height,
67 y_middle_of_top_cell +
68 (y_delta_to_grid <= half_cell_height ? 0 : pcell_height_));
69 const int y_end =
70 std::min(last_pixel_y_coordinate, key_y + key.kheight + threshold);
71
72 const int left_pixel_within_threshold = key_x - threshold;
73 const int x_delta_to_grid = left_pixel_within_threshold % pcell_width_;
74 const int x_middle_of_left_cell =
75 left_pixel_within_threshold - x_delta_to_grid + half_cell_width;
76 const int x_start =
77 std::max(half_cell_width,
78 x_middle_of_left_cell +
79 (x_delta_to_grid <= half_cell_width ? 0 : pcell_width_));
80 const int x_end =
81 std::min(last_pixel_x_coordinate, key_x + key.kwidth + threshold);
82
83 int base_index_of_current_row =
84 (y_start / pcell_height_) * pgrid_width_ + (x_start / pcell_width_);
85 for (int center_y = y_start; center_y <= y_end; center_y += pcell_height_) {
86 int index = base_index_of_current_row;
87 for (int center_x = x_start; center_x <= x_end;
88 center_x += pcell_width_) {
89 if (SquaredDistanceToEdge(center_x, center_y, key) <
90 threshold_squared) {
91 neighbors_flat_buffer[index * keyset::key_count +
92 neighbor_count_per_cell[index]] =
93 keyset::key_set[i];
94 ++neighbor_count_per_cell[index];
95 }
96 ++index;
97 }
98 base_index_of_current_row += pgrid_width_;
99 }
100 }
101
102 for (int i = 0; i < pgrid_size_; ++i) {
103 const int index_start = i * keyset::key_count;
104 const int index_end = index_start + neighbor_count_per_cell[i];
105 for (int index = index_start; index < index_end; index++) {
106 pgrid_neighbors[i].push_back(neighbors_flat_buffer[index]);
107 }
108 }
109
110 int proximity_chars_array[pgrid_size_ * MAX_PROXIMITY_CHARS_SIZE];
111 for (int i = 0; i < pgrid_size_; i++) {
112 int info_index = i * MAX_PROXIMITY_CHARS_SIZE;
113 for (int j = 0; j < neighbor_count_per_cell[i]; j++) {
114 Key neighbor_key = pgrid_neighbors[i][j];
115 proximity_chars_array[info_index] = neighbor_key.kcode;
116 info_index++;
117 }
118 }
119
120 int key_x_coordinates[keyset::key_count];
121 int key_y_coordinates[keyset::key_count];
122 int key_widths[keyset::key_count];
123 int key_heights[keyset::key_count];
124 int key_char_codes[keyset::key_count];
125 float sweet_spot_center_xs[keyset::key_count];
126 float sweet_spot_center_ys[keyset::key_count];
127 float sweet_spot_radii[keyset::key_count];
128
129 for (int key_index = 0; key_index < keyset::key_count; key_index++) {
130 Key key = keyset::key_set[key_index];
131 key_x_coordinates[key_index] = key.kx;
132 key_y_coordinates[key_index] = key.ky;
133 key_widths[key_index] = key.kwidth;
134 key_heights[key_index] = key.kheight;
135 key_char_codes[key_index] = key.kcode;
136 }
137
138 TouchPositionCorrection touch_position_correction;
139 if (touch_position_correction.IsValid()) {
140 const int rows = touch_position_correction.GetRows();
141 const float default_radius =
142 DEFAULT_TOUCH_POSITION_CORRECTION_RADIUS *
143 (float)std::hypot(pmost_common_key_width_, pmost_common_key_height_);
144 for (int key_index = 0; key_index < keyset::key_count; key_index++) {
145 Key key = keyset::key_set[key_index];
146 sweet_spot_center_xs[key_index] =
147 (key.khit_box_left + key.khit_box_right) * 0.5f;
148 sweet_spot_center_ys[key_index] =
149 (key.khit_box_top + key.khit_box_bottom) * 0.5f;
150 sweet_spot_radii[key_index] = default_radius;
151 const int row = key.khit_box_top / pmost_common_key_height_;
152 if (row < rows) {
153 const int hit_box_width = key.khit_box_right - key.khit_box_left;
154 const int hit_box_height = key.khit_box_bottom - key.khit_box_top;
155 const float hit_box_diagonal =
156 (float)std::hypot(hit_box_width, hit_box_height);
157 sweet_spot_center_xs[key_index] +=
158 touch_position_correction.GetX(row) * hit_box_width;
159 sweet_spot_center_ys[key_index] +=
160 touch_position_correction.GetY(row) * hit_box_height;
161 sweet_spot_radii[key_index] =
162 touch_position_correction.GetRadius(row) * hit_box_diagonal;
163 }
164 }
165 }
166
167 latinime::ProximityInfo* proximity_info = new latinime::ProximityInfo(
168 plocale_, pkeyboard_min_width_, pkeyboard_height_, pgrid_width_,
169 pgrid_height_, pmost_common_key_width_, pmost_common_key_height_,
170 proximity_chars_array, pgrid_size_ * MAX_PROXIMITY_CHARS_SIZE,
171 keyset::key_count, key_x_coordinates, key_y_coordinates, key_widths,
172 key_heights, key_char_codes, sweet_spot_center_xs, sweet_spot_center_ys,
173 sweet_spot_radii);
174
175 return proximity_info;
176}
177
178int ProximityInfoFactory::SquaredDistanceToEdge(int x, int y, Key k) {
179 const int left = k.kx;
180 const int right = left + k.kwidth;
181 const int top = k.ky;
182 const int bottom = top + k.kheight;
183 const int edge_x = x < left ? left : (x > right ? right : x);
184 const int edge_y = y < top ? top : (y > bottom ? bottom : y);
185 const int dx = x - edge_x;
186 const int dy = y - edge_y;
187 return dx * dx + dy * dy;
188}
189
James Robinson04362ac2015-09-18 18:24:02 -0700190} // namespace prediction