Upstream version 11.40.271.0
[platform/framework/web/crosswalk.git] / src / third_party / google_input_tools / src / chrome / os / inputview / util.js
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
7 //
8 //      http://www.apache.org/licenses/LICENSE-2.0
9 //
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");
13 //
14 goog.provide('i18n.input.chrome.inputview.util');
15
16 goog.require('goog.string');
17 goog.require('goog.style');
18
19
20 goog.scope(function() {
21 var util = i18n.input.chrome.inputview.util;
22
23
24 /**
25  * The mapping between the real character and its replacement for display.
26  *
27  * @type {!Object.<string, string>}
28  */
29 util.DISPLAY_MAPPING = {
30   '\u0300' : '\u0060',
31   '\u0301' : '\u00B4',
32   '\u0302' : '\u02C6',
33   '\u0303' : '\u02DC',
34   '\u0304' : '\u02c9',
35   '\u0305' : '\u00AF',
36   '\u0306' : '\u02D8',
37   '\u0307' : '\u02D9',
38   '\u0308' : '\u00a8',
39   '\u0309' : '\u02C0',
40   '\u030A' : '\u02DA',
41   '\u030B' : '\u02DD',
42   '\u030C' : '\u02C7',
43   '\u030D' : '\u02C8',
44   '\u030E' : '\u0022',
45   '\u0327' : '\u00B8',
46   '\u0328' : '\u02DB',
47   '\u0345' : '\u037A',
48   '\u030F' : '\u030F\u0020',
49   '\u031B' : '\u031B\u0020',
50   '\u0323' : '\u0323\u0020'
51 };
52
53
54 /**
55  * The keysets using US keyboard layouts.
56  *
57  * @type {!Array.<string>}
58  */
59 util.KEYSETS_USE_US = [
60   'array',
61   'cangjie',
62   'dayi',
63   'jp_us',
64   'pinyin-zh-CN',
65   'pinyin-zh-TW',
66   'quick',
67   't13n',
68   'wubi',
69   'zhuyin'
70 ];
71
72
73 /**
74  * The keysets that have en switcher key.
75  *
76  * @type {!Array.<string>}
77  */
78 util.KEYSETS_HAVE_EN_SWTICHER = [
79   // When other keysets that use us add the enswitcher key,
80   // should move them to this array.
81   'array',
82   'cangjie',
83   'dayi',
84   'pinyin-zh-CN',
85   'pinyin-zh-TW',
86   'quick',
87   'wubi',
88   'zhuyin'
89 ];
90
91
92 /**
93  * The keysets that have compact keyset.
94  *
95  * @type {!Array.<string>}
96  */
97 util.KEYSETS_HAVE_COMPACT = [
98   'be',
99   'ca',
100   'ca-eng',
101   'de',
102   'dk',
103   'fi',
104   'fr',
105   'gb-extd',
106   'ie',
107   'is',
108   'no',
109   'pinyin-zh-CN',
110   'se',
111   'us',
112   'zhuyin'
113 ];
114
115
116 /**
117  * A regular expression for the end of a sentence.
118  *
119  * @private {!RegExp}
120  */
121 util.END_SENTENCE_REGEX_ = /[\.\?!] +$/;
122
123
124 /**
125  * The regex of characters support dead key.
126  *
127  * @type {!RegExp}
128  * @private
129  */
130 util.REGEX_CHARACTER_SUPPORT_DEADKEY_ =
131     /^[a-zA-ZæÆœŒΑΕΗΙΟΥΩαεηιυοωϒ]+$/;
132
133
134 /**
135  * The regex of characters supported in language module.
136  *
137  * @type {!RegExp}
138  */
139 util.REGEX_LANGUAGE_MODEL_CHARACTERS =
140     /(?=[^\u00d7\u00f7])[a-z\-\'\u00c0-\u017F]/i;
141
142
143 /**
144  * Splits a value to pieces according to the weights.
145  *
146  * @param {!Array.<number>} weightArray The weight array.
147  * @param {number} totalValue The total value.
148  * @return {!Array.<number>} The splitted values.
149  */
150 util.splitValue = function(weightArray, totalValue) {
151   if (weightArray.length == 0) {
152     return [];
153   }
154
155   if (weightArray.length == 1) {
156     return [totalValue];
157   }
158
159   var totalWeight = 0;
160   for (var i = 0; i < weightArray.length; i++) {
161     totalWeight += weightArray[i];
162   }
163   var tmp = totalValue / totalWeight;
164   var values = [];
165   var totalFlooredValue = 0;
166   var diffs = [];
167   for (var i = 0; i < weightArray.length; i++) {
168     var result = weightArray[i] * tmp;
169     values.push(result);
170     diffs.push(result - Math.floor(result));
171     totalFlooredValue += Math.floor(result);
172   }
173   var diff = totalValue - totalFlooredValue;
174
175   // Distributes the rest pixels to values who lose most.
176   for (var i = 0; i < diff; i++) {
177     var max = 0;
178     var index = 0;
179     for (var j = 0; j < diffs.length; j++) {
180       if (diffs[j] > max) {
181         max = diffs[j];
182         index = j;
183       }
184     }
185     values[index] += 1;
186     diffs[index] = 0;
187   }
188   for (var i = 0; i < values.length; i++) {
189     values[i] = Math.floor(values[i]);
190   }
191   return values;
192 };
193
194
195 /**
196  * Gets the value of a property.
197  *
198  * @param {Element} elem The element.
199  * @param {string} property The property name.
200  * @return {number} The value.
201  */
202 util.getPropertyValue = function(elem, property) {
203   var value = goog.style.getComputedStyle(elem, property);
204   if (value) {
205     return parseInt(value.replace('px', ''), 10);
206   }
207   return 0;
208 };
209
210
211 /**
212  * To upper case.
213  *
214  * @param {string} character The character.
215  * @return {string} The uppercase of the character.
216  */
217 util.toUpper = function(character) {
218   if (character == '\u00b5') {
219     return '\u00b5';
220   } else {
221     return character.toUpperCase();
222   }
223 };
224
225
226 /**
227  * To lower case.
228  *
229  * @param {string} character The character.
230  * @return {string} The lower case of the character.
231  */
232 util.toLower = function(character) {
233   if (character == '\u0049') {
234     return '\u0131';
235   }
236   return character.toLowerCase();
237 };
238
239
240 /**
241  * Is this character trigger commit.
242  *
243  * @param {string} character The character.
244  * @return {boolean} True to trigger commit.
245  */
246 util.isCommitCharacter = function(character) {
247   if (util.DISPLAY_MAPPING[character] ||
248       util.REGEX_LANGUAGE_MODEL_CHARACTERS.test(
249           character)) {
250     return false;
251   }
252
253   return true;
254 };
255
256
257 /**
258  * Some unicode character can't be shown in the web page, use a replacement
259  *     instead.
260  *
261  * @param {string} invisibleCharacter The character can't be shown.
262  * @return {string} The replacement.
263  */
264 util.getVisibleCharacter = function(invisibleCharacter) {
265   var map = util.DISPLAY_MAPPING;
266   if (map[invisibleCharacter]) {
267     return map[invisibleCharacter];
268   }
269   return invisibleCharacter;
270 };
271
272
273 /**
274  * Whether this is a letter key.
275  *
276  * @param {!Array.<string>} characters The characters.
277  * @return {boolean} True if this is a letter key.
278  */
279 util.isLetterKey = function(characters) {
280   if (characters[1] == util.toUpper(
281       characters[0]) || characters[1] == util.
282           toLower(characters[0])) {
283     return true;
284   }
285   return false;
286 };
287
288
289 /**
290  * True if this character supports dead key combination.
291  *
292  * @param {string} character The character.
293  * @return {boolean} True if supports the dead key combination.
294  */
295 util.supportDeadKey = function(character) {
296   return util.REGEX_CHARACTER_SUPPORT_DEADKEY_.
297       test(character);
298 };
299
300
301 /**
302  * True if we need to do the auto-capitalize.
303  *
304  * @param {string} text .
305  * @return {boolean} .
306  */
307 util.needAutoCap = function(text) {
308   if (goog.string.isEmpty(text)) {
309     return false;
310   } else {
311     return util.END_SENTENCE_REGEX_.test(text);
312   }
313 };
314
315
316 /**
317  * Returns the configuration file name from the keyboard code.
318  *
319  * @param {string} keyboardCode The keyboard code.
320  * @return {string} The config file name which contains the keyset.
321  */
322 util.getConfigName = function(keyboardCode) {
323   // Strips out all the suffixes in the keyboard code.
324   return keyboardCode.replace(/\..*$/, '');
325 };
326
327 });  // goog.scope