summaryrefslogtreecommitdiff
path: root/chromium/chrome/browser/resources/chromeos/chromevox/braille/pan_strategy.js
blob: 9984b263efff43d017cd7e99aeb440cd876d7940 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
// Copyright 2015 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

/** @fileoverview Logic for panning a braille display within a line of braille
 * content that might not fit on a single display.
 */

goog.provide('cvox.PanStrategy');

/**
 * @constructor
 *
 * A stateful class that keeps track of the current 'viewport' of a braille
 * display in a line of content.
 */
cvox.PanStrategy = function() {
  /**
   * @type {number}
   * @private
   */
  this.displaySize_ = 0;
  /**
   * @type {number}
   * @private
   */
  this.contentLength_ = 0;
  /**
   * Points before which it is desirable to break content if it doesn't fit
   * on the display.
   * @type {!Array<number>}
   * @private
   */
  this.breakPoints_ = [];
  /**
   * @type {!cvox.PanStrategy.Range}
   * @private
   */
  this.viewPort_ = {start: 0, end: 0};
};

/**
 * A range used to represent the viewport with inclusive start and xclusive
 * end position.
 * @typedef {{start: number, end: number}}
 */
cvox.PanStrategy.Range;

cvox.PanStrategy.prototype = {
  /**
   * Gets the current viewport which is never larger than the current
   * display size and whose end points are always within the limits of
   * the current content.
   * @type {!cvox.PanStrategy.Range}
   */
  get viewPort() {
    return this.viewPort_;
  },

  /**
   * Sets the display size.  This call may update the viewport.
   * @param {number} size the new display size, or {@code 0} if no display is
   *     present.
   */
  setDisplaySize: function(size) {
    this.displaySize_ = size;
    this.panToPosition_(this.viewPort_.start);
  },

  /**
   * Sets the current content that panning should happen within.  This call may
   * change the viewport.
   * @param {!ArrayBuffer} translatedContent The new content.
   * @param {number} targetPosition Target position.  The viewport is changed
   *     to overlap this position.
   */
  setContent: function(translatedContent, targetPosition) {
    this.breakPoints_ = this.calculateBreakPoints_(translatedContent);
    this.contentLength_ = translatedContent.byteLength;
    this.panToPosition_(targetPosition);
  },

  /**
   * If possible, changes the viewport to a part of the line that follows
   * the current viewport.
   * @return {boolean} {@code true} if the viewport was changed.
   */
  next: function() {
    var newStart = this.viewPort_.end;
    var newEnd;
    if (newStart + this.displaySize_ < this.contentLength_) {
      newEnd = this.extendRight_(newStart);
    } else {
      newEnd = this.contentLength_;
    }
    if (newEnd > newStart) {
      this.viewPort_ = {start: newStart, end: newEnd};
      return true;
    }
    return false;
  },

  /**
   * If possible, changes the viewport to a part of the line that precedes
   * the current viewport.
   * @return {boolean} {@code true} if the viewport was changed.
   */
  previous: function() {
    if (this.viewPort_.start > 0) {
      var newStart, newEnd;
      if (this.viewPort_.start <= this.displaySize_) {
        newStart = 0;
        newEnd = this.extendRight_(newStart);
      } else {
        newEnd = this.viewPort_.start;
        var limit = newEnd - this.displaySize_;
        newStart = limit;
        var pos = 0;
        while (pos < this.breakPoints_.length &&
            this.breakPoints_[pos] < limit) {
          pos++;
        }
        if (pos < this.breakPoints_.length &&
            this.breakPoints_[pos] < newEnd) {
          newStart = this.breakPoints_[pos];
        }
      }
      if (newStart < newEnd) {
        this.viewPort_ = {start: newStart, end: newEnd};
        return true;
      }
    }
    return false;
  },

  /**
   * Finds the end position for a new viewport start position, considering
   * current breakpoints as well as display size and content length.
   * @param {number} from Start of the region to extend.
   * @return {number}
   * @private
   */
  extendRight_: function(from) {
    var limit = Math.min(from + this.displaySize_, this.contentLength_);
    var pos = 0;
    var result = limit;
    while (pos < this.breakPoints_.length && this.breakPoints_[pos] <= from) {
      pos++;
    }
    while (pos < this.breakPoints_.length && this.breakPoints_[pos] <= limit) {
      result = this.breakPoints_[pos];
      pos++;
    }
    return result;
  },

  /**
   * Overridden by subclasses to provide breakpoints given translated
   * braille cell content.
   * @param {!ArrayBuffer} content New display content.
   * @return {!Array<number>} The points before which it is desirable to break
   *     content if needed or the empty array if no points are more desirable
   *     than any position.
   * @private
   */
  calculateBreakPoints_: function(content) {return [];},

  /**
   * Moves the viewport so that it overlaps a target position without taking
   * the current viewport position into consideration.
   * @param {number} position Target position.
   */
  panToPosition_: function(position) {
    if (this.displaySize_ > 0) {
      this.viewPort_ = {start: 0, end: 0};
      while (this.next() && this.viewPort_.end <= position) {
        // Nothing to do.
      }
    } else {
      this.viewPort_ = {start: position, end: position};
    }
  },
};

/**
 * A pan strategy that fits as much content on the display as possible, that
 * is, it doesn't do any wrapping.
 * @constructor
 * @extends {cvox.PanStrategy}
 */
cvox.FixedPanStrategy = cvox.PanStrategy;
/**
 * A pan strategy that tries to wrap 'words' when breaking content.
 * A 'word' in this context is just a chunk of non-blank braille cells
 * delimited by blank cells.
 * @constructor
 * @extends {cvox.PanStrategy}
 */
cvox.WrappingPanStrategy = function() {
  cvox.PanStrategy.call(this);
};

cvox.WrappingPanStrategy.prototype = {
  __proto__: cvox.PanStrategy.prototype,

  /** @override */
  calculateBreakPoints_: function(content) {
    var view = new Uint8Array(content);
    var newContentLength = view.length;
    var result = [];
    var lastCellWasBlank = false;
    for (var pos = 0; pos < view.length; ++pos) {
      if (lastCellWasBlank && view[pos] != 0) {
        result.push(pos);
      }
      lastCellWasBlank = (view[pos] == 0);
    }
    return result;
  },
};