Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(386)

Side by Side Diff: chrome/browser/resources/chromeos/chromevox/braille/braille_display_manager.js

Issue 2544203004: Display images in multiline Braille
Patch Set: Minor Nits, added TODO comments Created 4 years ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
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
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
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.
148 */
149 cvox.BrailleDisplayManager.prototype.setImageContent = function(imageUrl) {
150 // TODO(dmazzoni) We are only supporting this for multiline displays for now.
151 // We'd like to support single line displays with panning.
152 if (this.displayState_.textRowCount <= 1) {
153 return;
154 }
155 this.displayingImage_ = true;
156 if (!this.displayState_.available) {
157 return;
158 }
159
160 var rows = this.displayState_.textRowCount;
161 var columns = this.displayState_.textColumnCount;
162 var imageDataUrl = imageUrl;
163 var imgElement = document.createElement('img');
164 imgElement.src = imageDataUrl;
165 var canvas = document.createElement('canvas');
166 var context = canvas.getContext('2d');
167 canvas.width = columns * 2;
168 canvas.height = rows * 4;
169 context.drawImage(imgElement, 0, 0, canvas.width, canvas.height);
170 var imageData = context.getImageData(0, 0, canvas.width, canvas.height);
171 var data = imageData.data;
172 var outputData = [];
173
174 // Convert image to black and white.
175 for (var i = 0; i < data.length; i += 4) {
176 // Show if the alpha value is visible (above 20) and the pixel is not
177 // white (under 600).
178 var show = (data[i] +
179 data[i + 1] +
180 data[i + 2] <= 200 * 3) &&
181 (data[i + 3]) == 255;
182 outputData.push(show);
183 }
184
185 // Convert to Braille.
186 var buf = new ArrayBuffer(rows * columns);
187 var view = new Uint8Array(buf);
188 var coordsToBrailleDot = [0x1, 0x2, 0x4, 0x40, 0x8, 0x10, 0x20, 0x80];
189 for (var i = 0; i < rows; i++) {
190 for (var j = 0; j < columns; j++) {
191 // Index in braille array.
192 var index = i * columns + j;
193 view[index] = 0;
194 for (var x = 0; x < 2; x++) {
195 for (var y = 0; y < 4; y++) {
196 if (outputData[((i * columns * 4 + j + y * columns) * 2 + x)])
197 view[index] += coordsToBrailleDot[x * 4 + y];
198 }
199 }
200 }
201 }
202
203 if (this.realDisplayState_.available) {
204 chrome.brailleDisplayPrivate.writeDots(buf, buf.byteLength, 1);
205 }
206 if (cvox.BrailleCaptionsBackground.isEnabled()) {
207 cvox.BrailleCaptionsBackground.setImageContent(buf, rows, columns);
208 }
209 };
210
211
212 /**
142 * Sets the command listener. When a command is invoked, the listener will be 213 * 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 214 * 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 215 * 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 216 * 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. 217 * to cvox.BrailleDisplayManager.setContent, or null if no content was set.
147 * @param {function(!cvox.BrailleKeyEvent, !cvox.NavBraille)} func The listener. 218 * @param {function(!cvox.BrailleKeyEvent, !cvox.NavBraille)} func The listener.
148 */ 219 */
149 cvox.BrailleDisplayManager.prototype.setCommandListener = function(func) { 220 cvox.BrailleDisplayManager.prototype.setCommandListener = function(func) {
150 this.commandListener_ = func; 221 this.commandListener_ = func;
151 }; 222 };
(...skipping 31 matching lines...) Expand 10 before | Expand all | Expand 10 after
183 cvox.BrailleDisplayManager.prototype.onCaptionsStateChanged_ = function() { 254 cvox.BrailleDisplayManager.prototype.onCaptionsStateChanged_ = function() {
184 // Force reevaluation of the display state based on our stored real 255 // Force reevaluation of the display state based on our stored real
185 // hardware display state, meaning that if a real display is connected, 256 // hardware display state, meaning that if a real display is connected,
186 // that takes precedence over the state from the captions 'virtual' display. 257 // that takes precedence over the state from the captions 'virtual' display.
187 this.refreshDisplayState_(this.realDisplayState_); 258 this.refreshDisplayState_(this.realDisplayState_);
188 }; 259 };
189 260
190 261
191 /** @private */ 262 /** @private */
192 cvox.BrailleDisplayManager.prototype.refresh_ = function() { 263 cvox.BrailleDisplayManager.prototype.refresh_ = function() {
193 if (!this.displayState_.available) { 264 // TODO(dmazzoni) Implement Panning support for images so refresh works with
265 // images as well.
266 if (!this.displayState_.available || this.displayingImage_) {
194 return; 267 return;
195 } 268 }
196 var viewPort = this.panStrategy_.viewPort; 269 var viewPort = this.panStrategy_.viewPort;
197 var buf = this.displayedContent_.slice(viewPort.start, viewPort.end); 270 var buf = this.displayedContent_.slice(viewPort.start, viewPort.end);
198 if (this.realDisplayState_.available) { 271 if (this.realDisplayState_.available) {
199 chrome.brailleDisplayPrivate.writeDots(buf, buf.byteLength, 1); 272 chrome.brailleDisplayPrivate.writeDots(buf, buf.byteLength, 1);
200 } 273 }
201 if (cvox.BrailleCaptionsBackground.isEnabled()) { 274 if (cvox.BrailleCaptionsBackground.isEnabled()) {
202 var start = this.brailleToTextPosition_(viewPort.start); 275 var start = this.brailleToTextPosition_(viewPort.start);
203 var end = this.brailleToTextPosition_(viewPort.end); 276 var end = this.brailleToTextPosition_(viewPort.end);
(...skipping 186 matching lines...) Expand 10 before | Expand all | Expand 10 after
390 */ 463 */
391 cvox.BrailleDisplayManager.prototype.updatePanStrategy_ = function(wordWrap) { 464 cvox.BrailleDisplayManager.prototype.updatePanStrategy_ = function(wordWrap) {
392 var newStrategy = wordWrap ? new cvox.WrappingPanStrategy() : 465 var newStrategy = wordWrap ? new cvox.WrappingPanStrategy() :
393 new cvox.FixedPanStrategy(); 466 new cvox.FixedPanStrategy();
394 newStrategy.setDisplaySize(this.displayState_.textColumnCount || 0); 467 newStrategy.setDisplaySize(this.displayState_.textColumnCount || 0);
395 newStrategy.setContent(this.translatedContent_, 468 newStrategy.setContent(this.translatedContent_,
396 this.panStrategy_.viewPort.start); 469 this.panStrategy_.viewPort.start);
397 this.panStrategy_ = newStrategy; 470 this.panStrategy_ = newStrategy;
398 this.refresh_(); 471 this.refresh_();
399 }; 472 };
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698