OLD | NEW |
1 // Copyright 2014 The Chromium Authors. All rights reserved. | 1 // Copyright 2014 The Chromium Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 /** | 5 /** |
6 * @fileoverview JavaScript shim for the liblouis Native Client wrapper. | 6 * @fileoverview JavaScript shim for the liblouis Native Client wrapper. |
7 */ | 7 */ |
8 | 8 |
9 goog.provide('cvox.LibLouis'); | 9 goog.provide('cvox.LibLouis'); |
10 | 10 |
(...skipping 114 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
125 * Determines whether the Native Client instance is attached. | 125 * Determines whether the Native Client instance is attached. |
126 * @return {boolean} {@code true} if the <embed> element is attached to the DOM. | 126 * @return {boolean} {@code true} if the <embed> element is attached to the DOM. |
127 */ | 127 */ |
128 cvox.LibLouis.prototype.isAttached = function() { | 128 cvox.LibLouis.prototype.isAttached = function() { |
129 return this.embedElement_ !== null; | 129 return this.embedElement_ !== null; |
130 }; | 130 }; |
131 | 131 |
132 | 132 |
133 /** | 133 /** |
134 * Returns a translator for the desired table, asynchronously. | 134 * Returns a translator for the desired table, asynchronously. |
135 * @param {string} tableName Braille table name for liblouis. | 135 * @param {string} tableNames Comma separated list of braille table names for |
| 136 * liblouis. |
136 * @param {function(cvox.LibLouis.Translator)} callback | 137 * @param {function(cvox.LibLouis.Translator)} callback |
137 * Callback which will receive the translator, or {@code null} on failure. | 138 * Callback which will receive the translator, or {@code null} on failure. |
138 */ | 139 */ |
139 cvox.LibLouis.prototype.getTranslator = | 140 cvox.LibLouis.prototype.getTranslator = |
140 function(tableName, callback) { | 141 function(tableNames, callback) { |
141 switch (this.instanceState_) { | 142 switch (this.instanceState_) { |
142 case cvox.LibLouis.InstanceState.NOT_LOADED: | 143 case cvox.LibLouis.InstanceState.NOT_LOADED: |
143 case cvox.LibLouis.InstanceState.LOADING: | 144 case cvox.LibLouis.InstanceState.LOADING: |
144 this.pendingTranslators_.push( | 145 this.pendingTranslators_.push( |
145 { tableName: tableName, callback: callback }); | 146 { tableNames: tableNames, callback: callback }); |
146 return; | 147 return; |
147 case cvox.LibLouis.InstanceState.ERROR: | 148 case cvox.LibLouis.InstanceState.ERROR: |
148 callback(null /* translator */); | 149 callback(null /* translator */); |
149 return; | 150 return; |
150 case cvox.LibLouis.InstanceState.LOADED: | 151 case cvox.LibLouis.InstanceState.LOADED: |
151 this.rpc_('CheckTable', { 'table_name': tableName }, | 152 this.rpc_('CheckTable', { 'table_names': tableNames }, |
152 goog.bind(function(reply) { | 153 goog.bind(function(reply) { |
153 if (reply['success']) { | 154 if (reply['success']) { |
154 var translator = new cvox.LibLouis.Translator( | 155 var translator = new cvox.LibLouis.Translator( |
155 this, tableName); | 156 this, tableNames); |
156 callback(translator); | 157 callback(translator); |
157 } else { | 158 } else { |
158 callback(null /* translator */); | 159 callback(null /* translator */); |
159 } | 160 } |
160 }, this)); | 161 }, this)); |
161 return; | 162 return; |
162 } | 163 } |
163 }; | 164 }; |
164 | 165 |
165 | 166 |
(...skipping 25 matching lines...) Expand all Loading... |
191 | 192 |
192 /** | 193 /** |
193 * Invoked when the Native Client instance successfully loads. | 194 * Invoked when the Native Client instance successfully loads. |
194 * @param {Event} e Event dispatched after loading. | 195 * @param {Event} e Event dispatched after loading. |
195 * @private | 196 * @private |
196 */ | 197 */ |
197 cvox.LibLouis.prototype.onInstanceLoad_ = function(e) { | 198 cvox.LibLouis.prototype.onInstanceLoad_ = function(e) { |
198 window.console.info('loaded liblouis Native Client instance'); | 199 window.console.info('loaded liblouis Native Client instance'); |
199 this.instanceState_ = cvox.LibLouis.InstanceState.LOADED; | 200 this.instanceState_ = cvox.LibLouis.InstanceState.LOADED; |
200 this.pendingTranslators_.forEach(goog.bind(function(record) { | 201 this.pendingTranslators_.forEach(goog.bind(function(record) { |
201 this.getTranslator(record.tableName, record.callback); | 202 this.getTranslator(record.tableNames, record.callback); |
202 }, this)); | 203 }, this)); |
203 this.pendingTranslators_.length = 0; | 204 this.pendingTranslators_.length = 0; |
204 }; | 205 }; |
205 | 206 |
206 | 207 |
207 /** | 208 /** |
208 * Invoked when the Native Client instance fails to load. | 209 * Invoked when the Native Client instance fails to load. |
209 * @param {Event} e Event dispatched after loading failure. | 210 * @param {Event} e Event dispatched after loading failure. |
210 * @private | 211 * @private |
211 */ | 212 */ |
212 cvox.LibLouis.prototype.onInstanceError_ = function(e) { | 213 cvox.LibLouis.prototype.onInstanceError_ = function(e) { |
213 window.console.error('failed to load liblouis Native Client instance'); | 214 window.console.error('failed to load liblouis Native Client instance'); |
214 this.instanceState_ = cvox.LibLouis.InstanceState.ERROR; | 215 this.instanceState_ = cvox.LibLouis.InstanceState.ERROR; |
215 this.pendingTranslators_.forEach(goog.bind(function(record) { | 216 this.pendingTranslators_.forEach(goog.bind(function(record) { |
216 this.getTranslator(record.tableName, record.callback); | 217 this.getTranslator(record.tableNames, record.callback); |
217 }, this)); | 218 }, this)); |
218 this.pendingTranslators_.length = 0; | 219 this.pendingTranslators_.length = 0; |
219 }; | 220 }; |
220 | 221 |
221 | 222 |
222 /** | 223 /** |
223 * Invoked when the Native Client instance posts a message. | 224 * Invoked when the Native Client instance posts a message. |
224 * @param {Event} e Event dispatched after the message was posted. | 225 * @param {Event} e Event dispatched after the message was posted. |
225 * @private | 226 * @private |
226 */ | 227 */ |
(...skipping 16 matching lines...) Expand all Loading... |
243 delete this.pendingRpcCallbacks_[messageId]; | 244 delete this.pendingRpcCallbacks_[messageId]; |
244 callback(message); | 245 callback(message); |
245 } | 246 } |
246 }; | 247 }; |
247 | 248 |
248 | 249 |
249 /** | 250 /** |
250 * Braille translator which uses a Native Client instance of liblouis. | 251 * Braille translator which uses a Native Client instance of liblouis. |
251 * @constructor | 252 * @constructor |
252 * @param {!cvox.LibLouis} instance The instance wrapper. | 253 * @param {!cvox.LibLouis} instance The instance wrapper. |
253 * @param {string} tableName The table name to be passed to liblouis. | 254 * @param {string} tableNames Comma separated list of Table names to be passed |
| 255 * to liblouis. |
254 */ | 256 */ |
255 cvox.LibLouis.Translator = function(instance, tableName) { | 257 cvox.LibLouis.Translator = function(instance, tableNames) { |
256 /** | 258 /** |
257 * The instance wrapper. | 259 * The instance wrapper. |
258 * @private {!cvox.LibLouis} | 260 * @private {!cvox.LibLouis} |
259 */ | 261 */ |
260 this.instance_ = instance; | 262 this.instance_ = instance; |
261 | 263 |
262 /** | 264 /** |
263 * The table name. | 265 * The table name. |
264 * @private {string} | 266 * @private {string} |
265 */ | 267 */ |
266 this.tableName_ = tableName; | 268 this.tableNames_ = tableNames; |
267 }; | 269 }; |
268 | 270 |
269 | 271 |
270 /** | 272 /** |
271 * Translates text into braille cells. | 273 * Translates text into braille cells. |
272 * @param {string} text Text to be translated. | 274 * @param {string} text Text to be translated. |
273 * @param {function(ArrayBuffer, Array.<number>, Array.<number>)} callback | 275 * @param {function(ArrayBuffer, Array.<number>, Array.<number>)} callback |
274 * Callback for result. Takes 3 parameters: the resulting cells, | 276 * Callback for result. Takes 3 parameters: the resulting cells, |
275 * mapping from text to braille positions and mapping from braille to | 277 * mapping from text to braille positions and mapping from braille to |
276 * text positions. If translation fails for any reason, all parameters are | 278 * text positions. If translation fails for any reason, all parameters are |
277 * {@code null}. | 279 * {@code null}. |
278 */ | 280 */ |
279 cvox.LibLouis.Translator.prototype.translate = function(text, callback) { | 281 cvox.LibLouis.Translator.prototype.translate = function(text, callback) { |
280 var message = { 'table_name': this.tableName_, 'text': text }; | 282 var message = { 'table_names': this.tableNames_, 'text': text }; |
281 this.instance_.rpc_('Translate', message, function(reply) { | 283 this.instance_.rpc_('Translate', message, function(reply) { |
282 var cells = null; | 284 var cells = null; |
283 var textToBraille = null; | 285 var textToBraille = null; |
284 var brailleToText = null; | 286 var brailleToText = null; |
285 if (reply['success'] && goog.isString(reply['cells'])) { | 287 if (reply['success'] && goog.isString(reply['cells'])) { |
286 cells = cvox.LibLouis.Translator.decodeHexString_(reply['cells']); | 288 cells = cvox.LibLouis.Translator.decodeHexString_(reply['cells']); |
287 if (goog.isDef(reply['text_to_braille'])) { | 289 if (goog.isDef(reply['text_to_braille'])) { |
288 textToBraille = reply['text_to_braille']; | 290 textToBraille = reply['text_to_braille']; |
289 } | 291 } |
290 if (goog.isDef(reply['braille_to_text'])) { | 292 if (goog.isDef(reply['braille_to_text'])) { |
(...skipping 16 matching lines...) Expand all Loading... |
307 */ | 309 */ |
308 cvox.LibLouis.Translator.prototype.backTranslate = | 310 cvox.LibLouis.Translator.prototype.backTranslate = |
309 function(cells, callback) { | 311 function(cells, callback) { |
310 if (cells.byteLength == 0) { | 312 if (cells.byteLength == 0) { |
311 // liblouis doesn't handle empty input, so handle that trivially | 313 // liblouis doesn't handle empty input, so handle that trivially |
312 // here. | 314 // here. |
313 callback(''); | 315 callback(''); |
314 return; | 316 return; |
315 } | 317 } |
316 var message = { | 318 var message = { |
317 'table_name': this.tableName_, | 319 'table_names': this.tableNames_, |
318 'cells': cvox.LibLouis.Translator.encodeHexString_(cells) | 320 'cells': cvox.LibLouis.Translator.encodeHexString_(cells) |
319 }; | 321 }; |
320 this.instance_.rpc_('BackTranslate', message, function(reply) { | 322 this.instance_.rpc_('BackTranslate', message, function(reply) { |
321 if (reply['success'] && goog.isString(reply['text'])) { | 323 if (reply['success'] && goog.isString(reply['text'])) { |
322 callback(reply['text']); | 324 callback(reply['text']); |
323 } else { | 325 } else { |
324 callback(null /* text */); | 326 callback(null /* text */); |
325 } | 327 } |
326 }); | 328 }); |
327 }; | 329 }; |
(...skipping 26 matching lines...) Expand all Loading... |
354 */ | 356 */ |
355 cvox.LibLouis.Translator.encodeHexString_ = function(arrayBuffer) { | 357 cvox.LibLouis.Translator.encodeHexString_ = function(arrayBuffer) { |
356 var array = new Uint8Array(arrayBuffer); | 358 var array = new Uint8Array(arrayBuffer); |
357 var hex = ''; | 359 var hex = ''; |
358 for (var i = 0; i < array.length; i++) { | 360 for (var i = 0; i < array.length; i++) { |
359 var b = array[i]; | 361 var b = array[i]; |
360 hex += (b < 0x10 ? '0' : '') + b.toString(16); | 362 hex += (b < 0x10 ? '0' : '') + b.toString(16); |
361 } | 363 } |
362 return hex; | 364 return hex; |
363 }; | 365 }; |
OLD | NEW |