Chromium Code Reviews| OLD | NEW |
|---|---|
| 1 // Copyright 2013 The Chromium Authors. All rights reserved. | 1 // Copyright 2013 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 * Predefined zoom factors to be used when zooming in/out. These are in | 6 * Predefined zoom factors to be used when zooming in/out. These are in |
| 7 * ascending order. | 7 * ascending order. |
| 8 */ | 8 */ |
| 9 var ZOOM_FACTORS = [0.25, 0.333, 0.5, 0.666, 0.75, 0.9, 1.0, | 9 var ZOOM_FACTORS = [0.25, 0.333, 0.5, 0.666, 0.75, 0.9, 1.0, |
| 10 1.1, 1.25, 1.5, 1.75, 2.0, 2.5, 3.0, 4.0, 5.0]; | 10 1.1, 1.25, 1.5, 1.75, 2.0, 2.5, 3.0, 4.0, 5.0]; |
| (...skipping 136 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 147 getCurrentViewportRect_: function() { | 147 getCurrentViewportRect_: function() { |
| 148 return { | 148 return { |
| 149 x: this.window_.pageXOffset / this.zoom_, | 149 x: this.window_.pageXOffset / this.zoom_, |
| 150 y: this.window_.pageYOffset / this.zoom_, | 150 y: this.window_.pageYOffset / this.zoom_, |
| 151 width: this.window_.innerWidth / this.zoom_, | 151 width: this.window_.innerWidth / this.zoom_, |
| 152 height: this.window_.innerHeight / this.zoom_, | 152 height: this.window_.innerHeight / this.zoom_, |
| 153 }; | 153 }; |
| 154 }, | 154 }, |
| 155 | 155 |
| 156 /** | 156 /** |
| 157 * @private | |
| 158 * @param {integer} y the y-cordinate to get the page at. | |
| 159 * @return {integer} the index of a page overlapping the given y-coordinate. | |
| 160 */ | |
| 161 getPageAtY_: function(y) { | |
| 162 var min = 0; | |
| 163 var max = this.pageDimensions_.length - 1; | |
| 164 while (max >= min) { | |
| 165 var page = Math.floor(min + ((max - min) / 2)); | |
| 166 var top = this.pageDimensions_[page].y; | |
| 167 var bottom = top + this.pageDimensions_[page].height; | |
| 168 if (top <= y && bottom > y) | |
| 169 return page; | |
| 170 else if (top > y) | |
| 171 max = page - 1; | |
| 172 else | |
| 173 min = page + 1; | |
| 174 } | |
| 175 return 0; | |
| 176 }, | |
| 177 | |
| 178 /** | |
| 157 * Returns the page with the most pixels in the current viewport. | 179 * Returns the page with the most pixels in the current viewport. |
| 158 * @return {int} the index of the most visible page. | 180 * @return {int} the index of the most visible page. |
| 159 */ | 181 */ |
| 160 getMostVisiblePage: function() { | 182 getMostVisiblePage: function() { |
| 161 // TODO(raymes): Do a binary search here. | 183 var firstVisiblePage = this.getPageAtY_(this.getCurrentViewportRect_().y); |
| 162 var mostVisiblePage = {'number': 0, 'area': 0}; | 184 var mostVisiblePage = {'number': 0, 'area': 0}; |
| 163 for (var i = 0; i < this.pageDimensions_.length; i++) { | 185 for (var i = firstVisiblePage; i < this.pageDimensions_.length; i++) { |
| 164 var area = getIntersectionArea(this.pageDimensions_[i], | 186 var area = getIntersectionArea(this.pageDimensions_[i], |
| 165 this.getCurrentViewportRect_()); | 187 this.getCurrentViewportRect_()); |
| 166 if (area > mostVisiblePage.area) { | 188 if (area > mostVisiblePage.area) { |
| 167 mostVisiblePage.area = area; | 189 mostVisiblePage.area = area; |
| 168 mostVisiblePage.number = i; | 190 mostVisiblePage.number = i; |
| 169 } | 191 } |
| 192 if (area == 0) | |
|
koz (OOO until 15th September)
2014/02/18 07:04:34
nit: Maybe move this before the if statement and a
raymes
2014/02/19 05:39:37
Done.
| |
| 193 break; | |
| 170 } | 194 } |
| 171 return mostVisiblePage.number; | 195 return mostVisiblePage.number; |
| 172 }, | 196 }, |
| 173 | 197 |
| 174 /** | 198 /** |
| 175 * @private | 199 * @private |
| 176 * Compute the zoom level for fit-to-page or fit-to-width. |pageDimensions| is | 200 * Compute the zoom level for fit-to-page or fit-to-width. |pageDimensions| is |
| 177 * the dimensions for a given page and if |widthOnly| is true, it indicates | 201 * the dimensions for a given page and if |widthOnly| is true, it indicates |
| 178 * that fit-to-page zoom should be computed rather than fit-to-page. | 202 * that fit-to-page zoom should be computed rather than fit-to-page. |
| 179 * @param {Object} pageDimensions the dimensions of a given page | 203 * @param {Object} pageDimensions the dimensions of a given page |
| (...skipping 119 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 299 * Set the dimensions of the document. | 323 * Set the dimensions of the document. |
| 300 * @param {Object} documentDimensions the dimensions of the document | 324 * @param {Object} documentDimensions the dimensions of the document |
| 301 */ | 325 */ |
| 302 setDocumentDimensions: function(documentDimensions) { | 326 setDocumentDimensions: function(documentDimensions) { |
| 303 this.documentDimensions_ = documentDimensions; | 327 this.documentDimensions_ = documentDimensions; |
| 304 this.pageDimensions_ = this.documentDimensions_.pageDimensions; | 328 this.pageDimensions_ = this.documentDimensions_.pageDimensions; |
| 305 this.contentSizeChanged_(); | 329 this.contentSizeChanged_(); |
| 306 this.updateViewport_(); | 330 this.updateViewport_(); |
| 307 } | 331 } |
| 308 }; | 332 }; |
| OLD | NEW |