OLD | NEW |
(Empty) | |
| 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 'pinyin-zh-CN' |
| 82 ]; |
| 83 |
| 84 |
| 85 /** |
| 86 * A regular expression for the end of a sentence. |
| 87 * |
| 88 * @private {!RegExp} |
| 89 */ |
| 90 util.END_SENTENCE_REGEX_ = /[\.\?!] +$/; |
| 91 |
| 92 |
| 93 /** |
| 94 * The regex of characters support dead key. |
| 95 * |
| 96 * @type {!RegExp} |
| 97 * @private |
| 98 */ |
| 99 util.REGEX_CHARACTER_SUPPORT_DEADKEY_ = |
| 100 /^[a-zA-ZæÆœŒΑΕΗΙΟΥΩαεηιυοωϒ]+$/; |
| 101 |
| 102 |
| 103 /** |
| 104 * The regex of characters supported in language module. |
| 105 * |
| 106 * @type {!RegExp} |
| 107 */ |
| 108 util.REGEX_LANGUAGE_MODEL_CHARACTERS = |
| 109 /(?=[^\u00d7\u00f7])[a-z\-\'\u00c0-\u017F]/i; |
| 110 |
| 111 |
| 112 /** |
| 113 * Splits a value to pieces according to the weights. |
| 114 * |
| 115 * @param {!Array.<number>} weightArray The weight array. |
| 116 * @param {number} totalValue The total value. |
| 117 * @return {!Array.<number>} The splitted values. |
| 118 */ |
| 119 util.splitValue = function(weightArray, totalValue) { |
| 120 if (weightArray.length == 0) { |
| 121 return []; |
| 122 } |
| 123 |
| 124 if (weightArray.length == 1) { |
| 125 return [totalValue]; |
| 126 } |
| 127 |
| 128 var totalWeight = 0; |
| 129 for (var i = 0; i < weightArray.length; i++) { |
| 130 totalWeight += weightArray[i]; |
| 131 } |
| 132 var tmp = totalValue / totalWeight; |
| 133 var values = []; |
| 134 var totalFlooredValue = 0; |
| 135 var diffs = []; |
| 136 for (var i = 0; i < weightArray.length; i++) { |
| 137 var result = weightArray[i] * tmp; |
| 138 values.push(result); |
| 139 diffs.push(result - Math.floor(result)); |
| 140 totalFlooredValue += Math.floor(result); |
| 141 } |
| 142 var diff = totalValue - totalFlooredValue; |
| 143 |
| 144 // Distributes the rest pixels to values who lose most. |
| 145 for (var i = 0; i < diff; i++) { |
| 146 var max = 0; |
| 147 var index = 0; |
| 148 for (var j = 0; j < diffs.length; j++) { |
| 149 if (diffs[j] > max) { |
| 150 max = diffs[j]; |
| 151 index = j; |
| 152 } |
| 153 } |
| 154 values[index] += 1; |
| 155 diffs[index] = 0; |
| 156 } |
| 157 for (var i = 0; i < values.length; i++) { |
| 158 values[i] = Math.floor(values[i]); |
| 159 } |
| 160 return values; |
| 161 }; |
| 162 |
| 163 |
| 164 /** |
| 165 * Gets the value of a property. |
| 166 * |
| 167 * @param {Element} elem The element. |
| 168 * @param {string} property The property name. |
| 169 * @return {number} The value. |
| 170 */ |
| 171 util.getPropertyValue = function(elem, property) { |
| 172 var value = goog.style.getComputedStyle(elem, property); |
| 173 if (value) { |
| 174 return parseInt(value.replace('px', ''), 10); |
| 175 } |
| 176 return 0; |
| 177 }; |
| 178 |
| 179 |
| 180 /** |
| 181 * To upper case. |
| 182 * |
| 183 * @param {string} character The character. |
| 184 * @return {string} The uppercase of the character. |
| 185 */ |
| 186 util.toUpper = function(character) { |
| 187 if (character == '\u00b5') { |
| 188 return '\u005b5'; |
| 189 } else { |
| 190 return character.toUpperCase(); |
| 191 } |
| 192 }; |
| 193 |
| 194 |
| 195 /** |
| 196 * To lower case. |
| 197 * |
| 198 * @param {string} character The character. |
| 199 * @return {string} The lower case of the character. |
| 200 */ |
| 201 util.toLower = function(character) { |
| 202 if (character == '\u0049') { |
| 203 return '\u0131'; |
| 204 } |
| 205 return character.toLowerCase(); |
| 206 }; |
| 207 |
| 208 |
| 209 /** |
| 210 * Is this character trigger commit. |
| 211 * |
| 212 * @param {string} character The character. |
| 213 * @return {boolean} True to trigger commit. |
| 214 */ |
| 215 util.isCommitCharacter = function(character) { |
| 216 if (util.DISPLAY_MAPPING[character] || |
| 217 util.REGEX_LANGUAGE_MODEL_CHARACTERS.test( |
| 218 character)) { |
| 219 return false; |
| 220 } |
| 221 |
| 222 return true; |
| 223 }; |
| 224 |
| 225 |
| 226 /** |
| 227 * Some unicode character can't be shown in the web page, use a replacement |
| 228 * instead. |
| 229 * |
| 230 * @param {string} invisibleCharacter The character can't be shown. |
| 231 * @return {string} The replacement. |
| 232 */ |
| 233 util.getVisibleCharacter = function(invisibleCharacter) { |
| 234 var map = util.DISPLAY_MAPPING; |
| 235 if (map[invisibleCharacter]) { |
| 236 return map[invisibleCharacter]; |
| 237 } |
| 238 return invisibleCharacter; |
| 239 }; |
| 240 |
| 241 |
| 242 /** |
| 243 * Whether this is a letter key. |
| 244 * |
| 245 * @param {!Array.<string>} characters The characters. |
| 246 * @return {boolean} True if this is a letter key. |
| 247 */ |
| 248 util.isLetterKey = function(characters) { |
| 249 if (characters[1] == util.toUpper( |
| 250 characters[0]) || characters[1] == util. |
| 251 toLower(characters[0])) { |
| 252 return true; |
| 253 } |
| 254 return false; |
| 255 }; |
| 256 |
| 257 |
| 258 /** |
| 259 * True if this character supports dead key combination. |
| 260 * |
| 261 * @param {string} character The character. |
| 262 * @return {boolean} True if supports the dead key combination. |
| 263 */ |
| 264 util.supportDeadKey = function(character) { |
| 265 return util.REGEX_CHARACTER_SUPPORT_DEADKEY_. |
| 266 test(character); |
| 267 }; |
| 268 |
| 269 |
| 270 /** |
| 271 * True if we need to do the auto-capitalize. |
| 272 * |
| 273 * @param {string} text . |
| 274 * @return {boolean} . |
| 275 */ |
| 276 util.needAutoCap = function(text) { |
| 277 if (goog.string.isEmpty(text)) { |
| 278 return false; |
| 279 } else { |
| 280 return util.END_SENTENCE_REGEX_.test(text); |
| 281 } |
| 282 }; |
| 283 |
| 284 |
| 285 /** |
| 286 * Returns the configuration file name from the keyboard code. |
| 287 * |
| 288 * @param {string} keyboardCode The keyboard code. |
| 289 * @return {string} The config file name which contains the keyset. |
| 290 */ |
| 291 util.getConfigName = function(keyboardCode) { |
| 292 // Strips out all the suffixes in the keyboard code. |
| 293 return keyboardCode.replace(/\..*$/, ''); |
| 294 }; |
| 295 |
| 296 }); // goog.scope |
OLD | NEW |