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 101 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 112 * of the display content is expanded when translating to braille. | 112 * of the display content is expanded when translating to braille. |
| 113 * (See {@code cvox.ExpandingBrailleTranslator}). | 113 * (See {@code cvox.ExpandingBrailleTranslator}). |
| 114 */ | 114 */ |
| 115 cvox.BrailleDisplayManager.prototype.setContent = function( | 115 cvox.BrailleDisplayManager.prototype.setContent = function( |
| 116 content, expansionType) { | 116 content, expansionType) { |
| 117 this.translateContent_(content, expansionType); | 117 this.translateContent_(content, expansionType); |
| 118 }; | 118 }; |
| 119 | 119 |
| 120 | 120 |
| 121 /** | 121 /** |
| 122 * Takes an image, in the form of a data url, and displays it in braille | |
| 123 * onto the physical braille display and the virtual braille captions display. | |
| 124 * @param {!string} imageUrl The image, in the form of a data url. | |
| 125 */ | |
| 126 cvox.BrailleDisplayManager.prototype.setImageContent = function(imageUrl) { | |
| 127 if (!this.displayState_.available) { | |
| 128 return; | |
| 129 } | |
| 130 | |
| 131 var rows = this.displayState_.textRowCount; | |
| 132 var columns = this.displayState_.textColumnCount; | |
| 133 var imageDataUrl = imageUrl; | |
| 134 var imgElement = document.createElement('img'); | |
| 135 imgElement.src = imageDataUrl; | |
| 136 var canvas1 = document.createElement('canvas'); | |
|
dmazzoni
2016/12/06 07:39:42
Nit: maybe just |canvas| since you don't have more
ultimatedbz
2016/12/07 22:03:48
Done.
| |
| 137 var context = canvas1.getContext('2d'); | |
| 138 canvas1.width = columns * 2; | |
| 139 canvas1.height = rows * 4; | |
| 140 context.drawImage(imgElement, 0, 0, canvas1.width, canvas1.height); | |
| 141 var imageData = context.getImageData(0, 0, canvas1.width, canvas1.height); | |
| 142 var data = imageData.data; | |
| 143 var outputData = []; | |
| 144 | |
| 145 // Convert image to black and white. | |
| 146 for (var i = 0; i < data.length; i += 4) { | |
| 147 // Show if the alpha value is visible (above 20) and the pixel is not | |
| 148 // white (under 600). | |
| 149 var show = (data[i] + | |
| 150 data[i + 1] + | |
| 151 data[i + 2] <= 200 * 3) && | |
| 152 (data[i + 3]) >= 20; | |
| 153 outputData.push(show); | |
| 154 } | |
| 155 | |
| 156 // Convert to Braille. | |
| 157 var buf = new ArrayBuffer(rows * columns); | |
| 158 var view = new Uint8Array(buf); | |
| 159 var coordsToBrailleDot = [0x1, 0x2, 0x4, 0x8, 0x10, 0x20, 0x40, 0x80]; | |
| 160 for (var i = 0; i < rows; i++) { | |
| 161 for (var j = 0; j < columns; j++) { | |
| 162 //Index in braille array | |
| 163 var index = i * columns + j; | |
| 164 view[index] = 0; | |
| 165 for (var x = 0; x < 2; x++) { | |
| 166 for (var y = 0; y < 4; y++) { | |
| 167 if (outputData[((i * columns * 4 + j + y * columns) * 2 + x)] == 1) | |
| 168 view[index] += coordsToBrailleDot[x * 4 + y]; | |
| 169 } | |
| 170 } | |
| 171 } | |
| 172 } | |
| 173 | |
| 174 if (this.realDisplayState_.available) { | |
| 175 chrome.brailleDisplayPrivate.writeDots(buf, buf.byteLength, 1); | |
| 176 } | |
| 177 if (cvox.BrailleCaptionsBackground.isEnabled()) { | |
| 178 cvox.BrailleCaptionsBackground.setImageContent(buf, rows, columns); | |
| 179 } | |
| 180 }; | |
| 181 | |
| 182 | |
| 183 /** | |
| 122 * Sets the command listener. When a command is invoked, the listener will be | 184 * Sets the command listener. When a command is invoked, the listener will be |
| 123 * called with the BrailleKeyEvent corresponding to the command and the content | 185 * called with the BrailleKeyEvent corresponding to the command and the content |
| 124 * that was present on the display when the command was invoked. The content | 186 * that was present on the display when the command was invoked. The content |
| 125 * is guaranteed to be identical to an object previously used as the parameter | 187 * is guaranteed to be identical to an object previously used as the parameter |
| 126 * to cvox.BrailleDisplayManager.setContent, or null if no content was set. | 188 * to cvox.BrailleDisplayManager.setContent, or null if no content was set. |
| 127 * @param {function(!cvox.BrailleKeyEvent, !cvox.NavBraille)} func The listener. | 189 * @param {function(!cvox.BrailleKeyEvent, !cvox.NavBraille)} func The listener. |
| 128 */ | 190 */ |
| 129 cvox.BrailleDisplayManager.prototype.setCommandListener = function(func) { | 191 cvox.BrailleDisplayManager.prototype.setCommandListener = function(func) { |
| 130 this.commandListener_ = func; | 192 this.commandListener_ = func; |
| 131 }; | 193 }; |
| (...skipping 230 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 362 }; | 424 }; |
| 363 | 425 |
| 364 /** | 426 /** |
| 365 * @param {boolean} wordWrap | 427 * @param {boolean} wordWrap |
| 366 * @private | 428 * @private |
| 367 */ | 429 */ |
| 368 cvox.BrailleDisplayManager.prototype.updatePanStrategy_ = function(wordWrap) { | 430 cvox.BrailleDisplayManager.prototype.updatePanStrategy_ = function(wordWrap) { |
| 369 this.panStrategy_.setPanStrategy(wordWrap); | 431 this.panStrategy_.setPanStrategy(wordWrap); |
| 370 this.refresh_(); | 432 this.refresh_(); |
| 371 }; | 433 }; |
| OLD | NEW |