Chromium Code Reviews| 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 Puts text on a braille display. | 6 * @fileoverview Puts text on a braille display. |
| 7 * | 7 * |
| 8 */ | 8 */ |
| 9 | 9 |
| 10 goog.provide('cvox.BrailleDisplayManager'); | 10 goog.provide('cvox.BrailleDisplayManager'); |
| (...skipping 69 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 80 * @type {!Array<number>} | 80 * @type {!Array<number>} |
| 81 * @private | 81 * @private |
| 82 */ | 82 */ |
| 83 this.textToBraille_ = []; | 83 this.textToBraille_ = []; |
| 84 /** | 84 /** |
| 85 * @type {!Array<number>} | 85 * @type {!Array<number>} |
| 86 * @private | 86 * @private |
| 87 */ | 87 */ |
| 88 this.brailleToText_ = []; | 88 this.brailleToText_ = []; |
| 89 | 89 |
| 90 this.displayingImage_ = false; | |
| 91 | |
| 90 translatorManager.addChangeListener(function() { | 92 translatorManager.addChangeListener(function() { |
| 91 this.translateContent_(this.content_, this.expansionType_); | 93 this.translateContent_(this.content_, this.expansionType_); |
| 92 }.bind(this)); | 94 }.bind(this)); |
| 93 | 95 |
| 94 chrome.storage.onChanged.addListener(function(changes, area) { | 96 chrome.storage.onChanged.addListener(function(changes, area) { |
| 95 if (area == 'local' && 'brailleWordWrap' in changes) { | 97 if (area == 'local' && 'brailleWordWrap' in changes) { |
| 96 this.updatePanStrategy_(changes.brailleWordWrap.newValue); | 98 this.updatePanStrategy_(changes.brailleWordWrap.newValue); |
| 97 } | 99 } |
| 98 }.bind(this)); | 100 }.bind(this)); |
| 99 chrome.storage.local.get({brailleWordWrap: true}, function(items) { | 101 chrome.storage.local.get({brailleWordWrap: true}, function(items) { |
| (...skipping 27 matching lines...) Expand all Loading... | |
| 127 | 129 |
| 128 /** | 130 /** |
| 129 * @param {!cvox.NavBraille} content Content to send to the braille display. | 131 * @param {!cvox.NavBraille} content Content to send to the braille display. |
| 130 * @param {!cvox.ExpandingBrailleTranslator.ExpansionType} expansionType | 132 * @param {!cvox.ExpandingBrailleTranslator.ExpansionType} expansionType |
| 131 * If the text has a {@code ValueSpan}, this indicates how that part | 133 * If the text has a {@code ValueSpan}, this indicates how that part |
| 132 * of the display content is expanded when translating to braille. | 134 * of the display content is expanded when translating to braille. |
| 133 * (See {@code cvox.ExpandingBrailleTranslator}). | 135 * (See {@code cvox.ExpandingBrailleTranslator}). |
| 134 */ | 136 */ |
| 135 cvox.BrailleDisplayManager.prototype.setContent = function( | 137 cvox.BrailleDisplayManager.prototype.setContent = function( |
| 136 content, expansionType) { | 138 content, expansionType) { |
| 139 this.displayingImage_ = false; | |
| 137 this.translateContent_(content, expansionType); | 140 this.translateContent_(content, expansionType); |
| 138 }; | 141 }; |
| 139 | 142 |
| 140 | 143 |
| 141 /** | 144 /** |
| 145 * Takes an image, in the form of a data url, and displays it in braille | |
| 146 * onto the physical braille display and the virtual braille captions display. | |
| 147 * @param {!string} imageUrl The image, in the form of a data url. | |
|
David Tseng
2016/12/08 16:44:22
primitive type doesn't require nullable i.e. @para
ultimatedbz
2016/12/08 19:29:05
Done.
| |
| 148 */ | |
| 149 cvox.BrailleDisplayManager.prototype.setImageContent = function(imageUrl) { | |
| 150 this.displayingImage_ = true; | |
| 151 if (!this.displayState_.available) { | |
| 152 return; | |
| 153 } | |
| 154 | |
| 155 var rows = this.displayState_.textRowCount; | |
| 156 var columns = this.displayState_.textColumnCount; | |
| 157 var imageDataUrl = imageUrl; | |
| 158 var imgElement = document.createElement('img'); | |
| 159 imgElement.src = imageDataUrl; | |
| 160 var canvas = document.createElement('canvas'); | |
| 161 var context = canvas.getContext('2d'); | |
| 162 canvas.width = columns * 2; | |
| 163 canvas.height = rows * 4; | |
| 164 context.drawImage(imgElement, 0, 0, canvas.width, canvas.height); | |
| 165 var imageData = context.getImageData(0, 0, canvas.width, canvas.height); | |
| 166 var data = imageData.data; | |
| 167 var outputData = []; | |
| 168 | |
| 169 // Convert image to black and white. | |
| 170 for (var i = 0; i < data.length; i += 4) { | |
| 171 // Show if the alpha value is visible (above 20) and the pixel is not | |
| 172 // white (under 600). | |
| 173 var show = (data[i] + | |
| 174 data[i + 1] + | |
| 175 data[i + 2] <= 200 * 3) && | |
| 176 (data[i + 3]) == 255; | |
| 177 outputData.push(show); | |
| 178 } | |
| 179 | |
| 180 // Convert to Braille. | |
| 181 var buf = new ArrayBuffer(rows * columns); | |
| 182 var view = new Uint8Array(buf); | |
| 183 var coordsToBrailleDot = [0x1, 0x2, 0x4, 0x40, 0x8, 0x10, 0x20, 0x80]; | |
| 184 for (var i = 0; i < rows; i++) { | |
| 185 for (var j = 0; j < columns; j++) { | |
| 186 //Index in braille array | |
|
David Tseng
2016/12/08 16:44:22
nit: period at end.
ultimatedbz
2016/12/08 19:29:05
Done.
| |
| 187 var index = i * columns + j; | |
| 188 view[index] = 0; | |
| 189 for (var x = 0; x < 2; x++) { | |
| 190 for (var y = 0; y < 4; y++) { | |
| 191 if (outputData[((i * columns * 4 + j + y * columns) * 2 + x)]) | |
| 192 view[index] += coordsToBrailleDot[x * 4 + y]; | |
| 193 } | |
| 194 } | |
| 195 } | |
| 196 } | |
| 197 | |
| 198 if (this.realDisplayState_.available) { | |
| 199 chrome.brailleDisplayPrivate.writeDots(buf, buf.byteLength, 1); | |
| 200 } | |
| 201 if (cvox.BrailleCaptionsBackground.isEnabled()) { | |
| 202 cvox.BrailleCaptionsBackground.setImageContent(buf, rows, columns); | |
| 203 } | |
| 204 }; | |
| 205 | |
| 206 | |
| 207 /** | |
| 142 * Sets the command listener. When a command is invoked, the listener will be | 208 * Sets the command listener. When a command is invoked, the listener will be |
| 143 * called with the BrailleKeyEvent corresponding to the command and the content | 209 * called with the BrailleKeyEvent corresponding to the command and the content |
| 144 * that was present on the display when the command was invoked. The content | 210 * that was present on the display when the command was invoked. The content |
| 145 * is guaranteed to be identical to an object previously used as the parameter | 211 * is guaranteed to be identical to an object previously used as the parameter |
| 146 * to cvox.BrailleDisplayManager.setContent, or null if no content was set. | 212 * to cvox.BrailleDisplayManager.setContent, or null if no content was set. |
| 147 * @param {function(!cvox.BrailleKeyEvent, !cvox.NavBraille)} func The listener. | 213 * @param {function(!cvox.BrailleKeyEvent, !cvox.NavBraille)} func The listener. |
| 148 */ | 214 */ |
| 149 cvox.BrailleDisplayManager.prototype.setCommandListener = function(func) { | 215 cvox.BrailleDisplayManager.prototype.setCommandListener = function(func) { |
| 150 this.commandListener_ = func; | 216 this.commandListener_ = func; |
| 151 }; | 217 }; |
| (...skipping 31 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 183 cvox.BrailleDisplayManager.prototype.onCaptionsStateChanged_ = function() { | 249 cvox.BrailleDisplayManager.prototype.onCaptionsStateChanged_ = function() { |
| 184 // Force reevaluation of the display state based on our stored real | 250 // Force reevaluation of the display state based on our stored real |
| 185 // hardware display state, meaning that if a real display is connected, | 251 // hardware display state, meaning that if a real display is connected, |
| 186 // that takes precedence over the state from the captions 'virtual' display. | 252 // that takes precedence over the state from the captions 'virtual' display. |
| 187 this.refreshDisplayState_(this.realDisplayState_); | 253 this.refreshDisplayState_(this.realDisplayState_); |
| 188 }; | 254 }; |
| 189 | 255 |
| 190 | 256 |
| 191 /** @private */ | 257 /** @private */ |
| 192 cvox.BrailleDisplayManager.prototype.refresh_ = function() { | 258 cvox.BrailleDisplayManager.prototype.refresh_ = function() { |
| 193 if (!this.displayState_.available) { | 259 if (!this.displayState_.available || this.displayingImage_) { |
|
David Tseng
2016/12/08 16:44:22
Panning support in v2? TODO(dmazzoni) perhaps? You
| |
| 194 return; | 260 return; |
| 195 } | 261 } |
| 196 var viewPort = this.panStrategy_.viewPort; | 262 var viewPort = this.panStrategy_.viewPort; |
| 197 var buf = this.displayedContent_.slice(viewPort.start, viewPort.end); | 263 var buf = this.displayedContent_.slice(viewPort.start, viewPort.end); |
| 198 if (this.realDisplayState_.available) { | 264 if (this.realDisplayState_.available) { |
| 199 chrome.brailleDisplayPrivate.writeDots(buf, buf.byteLength, 1); | 265 chrome.brailleDisplayPrivate.writeDots(buf, buf.byteLength, 1); |
| 200 } | 266 } |
| 201 if (cvox.BrailleCaptionsBackground.isEnabled()) { | 267 if (cvox.BrailleCaptionsBackground.isEnabled()) { |
| 202 var start = this.brailleToTextPosition_(viewPort.start); | 268 var start = this.brailleToTextPosition_(viewPort.start); |
| 203 var end = this.brailleToTextPosition_(viewPort.end); | 269 var end = this.brailleToTextPosition_(viewPort.end); |
| (...skipping 186 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 390 */ | 456 */ |
| 391 cvox.BrailleDisplayManager.prototype.updatePanStrategy_ = function(wordWrap) { | 457 cvox.BrailleDisplayManager.prototype.updatePanStrategy_ = function(wordWrap) { |
| 392 var newStrategy = wordWrap ? new cvox.WrappingPanStrategy() : | 458 var newStrategy = wordWrap ? new cvox.WrappingPanStrategy() : |
| 393 new cvox.FixedPanStrategy(); | 459 new cvox.FixedPanStrategy(); |
| 394 newStrategy.setDisplaySize(this.displayState_.textColumnCount || 0); | 460 newStrategy.setDisplaySize(this.displayState_.textColumnCount || 0); |
| 395 newStrategy.setContent(this.translatedContent_, | 461 newStrategy.setContent(this.translatedContent_, |
| 396 this.panStrategy_.viewPort.start); | 462 this.panStrategy_.viewPort.start); |
| 397 this.panStrategy_ = newStrategy; | 463 this.panStrategy_ = newStrategy; |
| 398 this.refresh_(); | 464 this.refresh_(); |
| 399 }; | 465 }; |
| OLD | NEW |