1 // Copyright 2014 The ChromeOS IME Authors. All Rights Reserved.
2 // limitations under the License.
3 // See the License for the specific language governing permissions and
4 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
5 // distributed under the License is distributed on an "AS-IS" BASIS,
6 // Unless required by applicable law or agreed to in writing, software
8 // http://www.apache.org/licenses/LICENSE-2.0
10 // You may obtain a copy of the License at
11 // you may not use this file except in compliance with the License.
12 // Licensed under the Apache License, Version 2.0 (the "License");
14 goog.provide('i18n.input.chrome.inputview.util');
16 goog.require('goog.string');
17 goog.require('goog.style');
20 goog.scope(function() {
21 var util = i18n.input.chrome.inputview.util;
25 * The mapping between the real character and its replacement for display.
27 * @type {!Object.<string, string>}
29 util.DISPLAY_MAPPING = {
48 '\u030F' : '\u030F\u0020',
49 '\u031B' : '\u031B\u0020',
50 '\u0323' : '\u0323\u0020'
55 * The keysets using US keyboard layouts.
57 * @type {!Array.<string>}
59 util.KEYSETS_USE_US = [
74 * The keysets that have en switcher key.
76 * @type {!Array.<string>}
78 util.KEYSETS_HAVE_EN_SWTICHER = [
79 // When other keysets that use us add the enswitcher key,
80 // should move them to this array.
93 * The keysets that have compact keyset.
95 * @type {!Array.<string>}
97 util.KEYSETS_HAVE_COMPACT = [
117 * A regular expression for the end of a sentence.
121 util.END_SENTENCE_REGEX_ = /[\.\?!] +$/;
125 * The regex of characters support dead key.
130 util.REGEX_CHARACTER_SUPPORT_DEADKEY_ =
131 /^[a-zA-ZæÆœŒΑΕΗΙΟΥΩαεηιυοωϒ]+$/;
135 * The regex of characters supported in language module.
139 util.REGEX_LANGUAGE_MODEL_CHARACTERS =
140 /(?=[^\u00d7\u00f7])[a-z\-\'\u00c0-\u017F]/i;
144 * Splits a value to pieces according to the weights.
146 * @param {!Array.<number>} weightArray The weight array.
147 * @param {number} totalValue The total value.
148 * @return {!Array.<number>} The splitted values.
150 util.splitValue = function(weightArray, totalValue) {
151 if (weightArray.length == 0) {
155 if (weightArray.length == 1) {
160 for (var i = 0; i < weightArray.length; i++) {
161 totalWeight += weightArray[i];
163 var tmp = totalValue / totalWeight;
165 var totalFlooredValue = 0;
167 for (var i = 0; i < weightArray.length; i++) {
168 var result = weightArray[i] * tmp;
170 diffs.push(result - Math.floor(result));
171 totalFlooredValue += Math.floor(result);
173 var diff = totalValue - totalFlooredValue;
175 // Distributes the rest pixels to values who lose most.
176 for (var i = 0; i < diff; i++) {
179 for (var j = 0; j < diffs.length; j++) {
180 if (diffs[j] > max) {
188 for (var i = 0; i < values.length; i++) {
189 values[i] = Math.floor(values[i]);
196 * Gets the value of a property.
198 * @param {Element} elem The element.
199 * @param {string} property The property name.
200 * @return {number} The value.
202 util.getPropertyValue = function(elem, property) {
203 var value = goog.style.getComputedStyle(elem, property);
205 return parseInt(value.replace('px', ''), 10);
214 * @param {string} character The character.
215 * @return {string} The uppercase of the character.
217 util.toUpper = function(character) {
218 if (character == '\u00b5') {
221 return character.toUpperCase();
229 * @param {string} character The character.
230 * @return {string} The lower case of the character.
232 util.toLower = function(character) {
233 if (character == '\u0049') {
236 return character.toLowerCase();
241 * Is this character trigger commit.
243 * @param {string} character The character.
244 * @return {boolean} True to trigger commit.
246 util.isCommitCharacter = function(character) {
247 if (util.DISPLAY_MAPPING[character] ||
248 util.REGEX_LANGUAGE_MODEL_CHARACTERS.test(
258 * Some unicode character can't be shown in the web page, use a replacement
261 * @param {string} invisibleCharacter The character can't be shown.
262 * @return {string} The replacement.
264 util.getVisibleCharacter = function(invisibleCharacter) {
265 var map = util.DISPLAY_MAPPING;
266 if (map[invisibleCharacter]) {
267 return map[invisibleCharacter];
269 return invisibleCharacter;
274 * Whether this is a letter key.
276 * @param {!Array.<string>} characters The characters.
277 * @return {boolean} True if this is a letter key.
279 util.isLetterKey = function(characters) {
280 if (characters[1] == util.toUpper(
281 characters[0]) || characters[1] == util.
282 toLower(characters[0])) {
290 * True if this character supports dead key combination.
292 * @param {string} character The character.
293 * @return {boolean} True if supports the dead key combination.
295 util.supportDeadKey = function(character) {
296 return util.REGEX_CHARACTER_SUPPORT_DEADKEY_.
302 * True if we need to do the auto-capitalize.
304 * @param {string} text .
305 * @return {boolean} .
307 util.needAutoCap = function(text) {
308 if (goog.string.isEmpty(text)) {
311 return util.END_SENTENCE_REGEX_.test(text);
317 * Returns the configuration file name from the keyboard code.
319 * @param {string} keyboardCode The keyboard code.
320 * @return {string} The config file name which contains the keyset.
322 util.getConfigName = function(keyboardCode) {
323 // Strips out all the suffixes in the keyboard code.
324 return keyboardCode.replace(/\..*$/, '');