[email protected] | 3528d630 | 2014-02-19 08:13:07 | [diff] [blame] | 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 |
| 3 | // found in the LICENSE file. |
| 4 | |
| 5 | /** |
sammc | d06823a96 | 2014-11-10 04:51:15 | [diff] [blame] | 6 | * Returns the height of the intersection of two rectangles. |
[email protected] | 3528d630 | 2014-02-19 08:13:07 | [diff] [blame] | 7 | * @param {Object} rect1 the first rect |
| 8 | * @param {Object} rect2 the second rect |
sammc | d06823a96 | 2014-11-10 04:51:15 | [diff] [blame] | 9 | * @return {number} the height of the intersection of the rects |
[email protected] | 3528d630 | 2014-02-19 08:13:07 | [diff] [blame] | 10 | */ |
sammc | d06823a96 | 2014-11-10 04:51:15 | [diff] [blame] | 11 | function getIntersectionHeight(rect1, rect2) { |
| 12 | return Math.max(0, |
[email protected] | 3528d630 | 2014-02-19 08:13:07 | [diff] [blame] | 13 | Math.min(rect1.y + rect1.height, rect2.y + rect2.height) - |
| 14 | Math.max(rect1.y, rect2.y)); |
[email protected] | 3528d630 | 2014-02-19 08:13:07 | [diff] [blame] | 15 | } |
| 16 | |
| 17 | /** |
[email protected] | 3528d630 | 2014-02-19 08:13:07 | [diff] [blame] | 18 | * Create a new viewport. |
alexandrec | 9754b9e4 | 2015-01-19 03:41:19 | [diff] [blame] | 19 | * @constructor |
[email protected] | 3528d630 | 2014-02-19 08:13:07 | [diff] [blame] | 20 | * @param {Window} window the window |
| 21 | * @param {Object} sizer is the element which represents the size of the |
| 22 | * document in the viewport |
[email protected] | 3528d630 | 2014-02-19 08:13:07 | [diff] [blame] | 23 | * @param {Function} viewportChangedCallback is run when the viewport changes |
[email protected] | 499e956 | 2014-06-26 05:45:27 | [diff] [blame] | 24 | * @param {Function} beforeZoomCallback is run before a change in zoom |
| 25 | * @param {Function} afterZoomCallback is run after a change in zoom |
[email protected] | 345e7c6 | 2014-05-02 09:52:58 | [diff] [blame] | 26 | * @param {number} scrollbarWidth the width of scrollbars on the page |
sammc | 43d4982f | 2015-04-22 07:46:51 | [diff] [blame] | 27 | * @param {number} defaultZoom The default zoom level. |
raymes | bd82a94 | 2015-08-05 07:05:18 | [diff] [blame] | 28 | * @param {number} topToolbarHeight The number of pixels that should initially |
| 29 | * be left blank above the document for the toolbar. |
[email protected] | 3528d630 | 2014-02-19 08:13:07 | [diff] [blame] | 30 | */ |
| 31 | function Viewport(window, |
| 32 | sizer, |
[email protected] | 345e7c6 | 2014-05-02 09:52:58 | [diff] [blame] | 33 | viewportChangedCallback, |
[email protected] | 499e956 | 2014-06-26 05:45:27 | [diff] [blame] | 34 | beforeZoomCallback, |
| 35 | afterZoomCallback, |
sammc | 43d4982f | 2015-04-22 07:46:51 | [diff] [blame] | 36 | scrollbarWidth, |
raymes | bd82a94 | 2015-08-05 07:05:18 | [diff] [blame] | 37 | defaultZoom, |
| 38 | topToolbarHeight) { |
[email protected] | 3528d630 | 2014-02-19 08:13:07 | [diff] [blame] | 39 | this.window_ = window; |
| 40 | this.sizer_ = sizer; |
[email protected] | 3528d630 | 2014-02-19 08:13:07 | [diff] [blame] | 41 | this.viewportChangedCallback_ = viewportChangedCallback; |
[email protected] | 499e956 | 2014-06-26 05:45:27 | [diff] [blame] | 42 | this.beforeZoomCallback_ = beforeZoomCallback; |
| 43 | this.afterZoomCallback_ = afterZoomCallback; |
| 44 | this.allowedToChangeZoom_ = false; |
[email protected] | 3528d630 | 2014-02-19 08:13:07 | [diff] [blame] | 45 | this.zoom_ = 1; |
[email protected] | 312112c7 | 2014-04-14 01:45:43 | [diff] [blame] | 46 | this.documentDimensions_ = null; |
[email protected] | 3528d630 | 2014-02-19 08:13:07 | [diff] [blame] | 47 | this.pageDimensions_ = []; |
[email protected] | 345e7c6 | 2014-05-02 09:52:58 | [diff] [blame] | 48 | this.scrollbarWidth_ = scrollbarWidth; |
[email protected] | 312112c7 | 2014-04-14 01:45:43 | [diff] [blame] | 49 | this.fittingType_ = Viewport.FittingType.NONE; |
sammc | 43d4982f | 2015-04-22 07:46:51 | [diff] [blame] | 50 | this.defaultZoom_ = defaultZoom; |
raymes | bd82a94 | 2015-08-05 07:05:18 | [diff] [blame] | 51 | this.topToolbarHeight_ = topToolbarHeight; |
[email protected] | 3528d630 | 2014-02-19 08:13:07 | [diff] [blame] | 52 | |
| 53 | window.addEventListener('scroll', this.updateViewport_.bind(this)); |
[email protected] | 312112c7 | 2014-04-14 01:45:43 | [diff] [blame] | 54 | window.addEventListener('resize', this.resize_.bind(this)); |
[email protected] | 3528d630 | 2014-02-19 08:13:07 | [diff] [blame] | 55 | } |
| 56 | |
[email protected] | 312112c7 | 2014-04-14 01:45:43 | [diff] [blame] | 57 | /** |
| 58 | * Enumeration of page fitting types. |
| 59 | * @enum {string} |
| 60 | */ |
| 61 | Viewport.FittingType = { |
| 62 | NONE: 'none', |
| 63 | FIT_TO_PAGE: 'fit-to-page', |
| 64 | FIT_TO_WIDTH: 'fit-to-width' |
| 65 | }; |
| 66 | |
| 67 | /** |
[email protected] | 8dcaa26 | 2014-05-30 13:33:37 | [diff] [blame] | 68 | * The increment to scroll a page by in pixels when up/down/left/right arrow |
| 69 | * keys are pressed. Usually we just let the browser handle scrolling on the |
| 70 | * window when these keys are pressed but in certain cases we need to simulate |
| 71 | * these events. |
| 72 | */ |
| 73 | Viewport.SCROLL_INCREMENT = 40; |
| 74 | |
| 75 | /** |
[email protected] | 312112c7 | 2014-04-14 01:45:43 | [diff] [blame] | 76 | * Predefined zoom factors to be used when zooming in/out. These are in |
bsep | 24e7ec4 | 2016-08-25 02:11:20 | [diff] [blame^] | 77 | * ascending order. This should match the lists in |
| 78 | * components/ui/zoom/page_zoom_constants.h and |
| 79 | * chrome/browser/resources/settings/appearance_page/appearance_page.js |
[email protected] | 312112c7 | 2014-04-14 01:45:43 | [diff] [blame] | 80 | */ |
bsep | 9924b84 | 2016-08-22 18:57:14 | [diff] [blame] | 81 | Viewport.ZOOM_FACTORS = [0.25, 1 / 3, 0.5, 2 / 3, 0.75, 0.8, 0.9, |
| 82 | 1, 1.1, 1.25, 1.5, 1.75, 2, 2.5, 3, 4, 5]; |
[email protected] | 312112c7 | 2014-04-14 01:45:43 | [diff] [blame] | 83 | |
| 84 | /** |
[email protected] | 4271e16b | 2014-08-22 12:18:59 | [diff] [blame] | 85 | * The minimum and maximum range to be used to clip zoom factor. |
| 86 | */ |
| 87 | Viewport.ZOOM_FACTOR_RANGE = { |
| 88 | min: Viewport.ZOOM_FACTORS[0], |
| 89 | max: Viewport.ZOOM_FACTORS[Viewport.ZOOM_FACTORS.length - 1] |
| 90 | }; |
| 91 | |
| 92 | /** |
[email protected] | 312112c7 | 2014-04-14 01:45:43 | [diff] [blame] | 93 | * The width of the page shadow around pages in pixels. |
| 94 | */ |
| 95 | Viewport.PAGE_SHADOW = {top: 3, bottom: 7, left: 5, right: 5}; |
| 96 | |
[email protected] | 3528d630 | 2014-02-19 08:13:07 | [diff] [blame] | 97 | Viewport.prototype = { |
| 98 | /** |
raymes | e6e90c6 | 2015-08-10 06:21:40 | [diff] [blame] | 99 | * Returns the zoomed and rounded document dimensions for the given zoom. |
| 100 | * Rounding is necessary when interacting with the renderer which tends to |
| 101 | * operate in integral values (for example for determining if scrollbars |
| 102 | * should be shown). |
| 103 | * @param {number} zoom The zoom to use to compute the scaled dimensions. |
| 104 | * @return {Object} A dictionary with scaled 'width'/'height' of the document. |
| 105 | * @private |
| 106 | */ |
| 107 | getZoomedDocumentDimensions_: function(zoom) { |
| 108 | if (!this.documentDimensions_) |
| 109 | return null; |
| 110 | return { |
| 111 | width: Math.round(this.documentDimensions_.width * zoom), |
| 112 | height: Math.round(this.documentDimensions_.height * zoom) |
| 113 | }; |
| 114 | }, |
| 115 | |
| 116 | /** |
[email protected] | 3528d630 | 2014-02-19 08:13:07 | [diff] [blame] | 117 | * @private |
| 118 | * Returns true if the document needs scrollbars at the given zoom level. |
| 119 | * @param {number} zoom compute whether scrollbars are needed at this zoom |
[email protected] | 312112c7 | 2014-04-14 01:45:43 | [diff] [blame] | 120 | * @return {Object} with 'horizontal' and 'vertical' keys which map to bool |
| 121 | * values indicating if the horizontal and vertical scrollbars are needed |
[email protected] | 3528d630 | 2014-02-19 08:13:07 | [diff] [blame] | 122 | * respectively. |
| 123 | */ |
| 124 | documentNeedsScrollbars_: function(zoom) { |
raymes | e6e90c6 | 2015-08-10 06:21:40 | [diff] [blame] | 125 | var zoomedDimensions = this.getZoomedDocumentDimensions_(zoom); |
| 126 | if (!zoomedDimensions) { |
[email protected] | 499e956 | 2014-06-26 05:45:27 | [diff] [blame] | 127 | return { |
| 128 | horizontal: false, |
| 129 | vertical: false |
| 130 | }; |
| 131 | } |
sammc | 63334fed | 2014-11-10 03:07:35 | [diff] [blame] | 132 | |
| 133 | // If scrollbars are required for one direction, expand the document in the |
| 134 | // other direction to take the width of the scrollbars into account when |
| 135 | // deciding whether the other direction needs scrollbars. |
raymes | e6e90c6 | 2015-08-10 06:21:40 | [diff] [blame] | 136 | if (zoomedDimensions.width > this.window_.innerWidth) |
| 137 | zoomedDimensions.height += this.scrollbarWidth_; |
| 138 | else if (zoomedDimensions.height > this.window_.innerHeight) |
| 139 | zoomedDimensions.width += this.scrollbarWidth_; |
[email protected] | 3528d630 | 2014-02-19 08:13:07 | [diff] [blame] | 140 | return { |
raymes | e6e90c6 | 2015-08-10 06:21:40 | [diff] [blame] | 141 | horizontal: zoomedDimensions.width > this.window_.innerWidth, |
raymes | 051fb2c | 2015-09-21 04:56:41 | [diff] [blame] | 142 | vertical: zoomedDimensions.height + this.topToolbarHeight_ > |
| 143 | this.window_.innerHeight |
[email protected] | 3528d630 | 2014-02-19 08:13:07 | [diff] [blame] | 144 | }; |
| 145 | }, |
| 146 | |
| 147 | /** |
| 148 | * Returns true if the document needs scrollbars at the current zoom level. |
| 149 | * @return {Object} with 'x' and 'y' keys which map to bool values |
| 150 | * indicating if the horizontal and vertical scrollbars are needed |
| 151 | * respectively. |
| 152 | */ |
| 153 | documentHasScrollbars: function() { |
| 154 | return this.documentNeedsScrollbars_(this.zoom_); |
| 155 | }, |
| 156 | |
| 157 | /** |
| 158 | * @private |
| 159 | * Helper function called when the zoomed document size changes. |
| 160 | */ |
| 161 | contentSizeChanged_: function() { |
raymes | e6e90c6 | 2015-08-10 06:21:40 | [diff] [blame] | 162 | var zoomedDimensions = this.getZoomedDocumentDimensions_(this.zoom_); |
| 163 | if (zoomedDimensions) { |
| 164 | this.sizer_.style.width = zoomedDimensions.width + 'px'; |
| 165 | this.sizer_.style.height = zoomedDimensions.height + |
raymes | bd82a94 | 2015-08-05 07:05:18 | [diff] [blame] | 166 | this.topToolbarHeight_ + 'px'; |
[email protected] | 345e7c6 | 2014-05-02 09:52:58 | [diff] [blame] | 167 | } |
[email protected] | 3528d630 | 2014-02-19 08:13:07 | [diff] [blame] | 168 | }, |
| 169 | |
| 170 | /** |
[email protected] | 312112c7 | 2014-04-14 01:45:43 | [diff] [blame] | 171 | * @private |
| 172 | * Called when the viewport should be updated. |
[email protected] | 3528d630 | 2014-02-19 08:13:07 | [diff] [blame] | 173 | */ |
[email protected] | 312112c7 | 2014-04-14 01:45:43 | [diff] [blame] | 174 | updateViewport_: function() { |
| 175 | this.viewportChangedCallback_(); |
| 176 | }, |
| 177 | |
| 178 | /** |
| 179 | * @private |
| 180 | * Called when the viewport size changes. |
| 181 | */ |
| 182 | resize_: function() { |
| 183 | if (this.fittingType_ == Viewport.FittingType.FIT_TO_PAGE) |
raymes | 161514f | 2015-02-17 05:09:51 | [diff] [blame] | 184 | this.fitToPageInternal_(false); |
[email protected] | 312112c7 | 2014-04-14 01:45:43 | [diff] [blame] | 185 | else if (this.fittingType_ == Viewport.FittingType.FIT_TO_WIDTH) |
| 186 | this.fitToWidth(); |
| 187 | else |
| 188 | this.updateViewport_(); |
| 189 | }, |
| 190 | |
| 191 | /** |
| 192 | * @type {Object} the scroll position of the viewport. |
| 193 | */ |
| 194 | get position() { |
| 195 | return { |
| 196 | x: this.window_.pageXOffset, |
raymes | bd82a94 | 2015-08-05 07:05:18 | [diff] [blame] | 197 | y: this.window_.pageYOffset - this.topToolbarHeight_ |
[email protected] | 312112c7 | 2014-04-14 01:45:43 | [diff] [blame] | 198 | }; |
| 199 | }, |
| 200 | |
| 201 | /** |
| 202 | * Scroll the viewport to the specified position. |
| 203 | * @type {Object} position the position to scroll to. |
| 204 | */ |
| 205 | set position(position) { |
raymes | bd82a94 | 2015-08-05 07:05:18 | [diff] [blame] | 206 | this.window_.scrollTo(position.x, position.y + this.topToolbarHeight_); |
[email protected] | 312112c7 | 2014-04-14 01:45:43 | [diff] [blame] | 207 | }, |
| 208 | |
| 209 | /** |
| 210 | * @type {Object} the size of the viewport excluding scrollbars. |
| 211 | */ |
| 212 | get size() { |
| 213 | var needsScrollbars = this.documentNeedsScrollbars_(this.zoom_); |
| 214 | var scrollbarWidth = needsScrollbars.vertical ? this.scrollbarWidth_ : 0; |
| 215 | var scrollbarHeight = needsScrollbars.horizontal ? this.scrollbarWidth_ : 0; |
| 216 | return { |
| 217 | width: this.window_.innerWidth - scrollbarWidth, |
| 218 | height: this.window_.innerHeight - scrollbarHeight |
| 219 | }; |
| 220 | }, |
| 221 | |
| 222 | /** |
| 223 | * @type {number} the zoom level of the viewport. |
| 224 | */ |
| 225 | get zoom() { |
| 226 | return this.zoom_; |
| 227 | }, |
| 228 | |
| 229 | /** |
| 230 | * @private |
[email protected] | 499e956 | 2014-06-26 05:45:27 | [diff] [blame] | 231 | * Used to wrap a function that might perform zooming on the viewport. This is |
| 232 | * required so that we can notify the plugin that zooming is in progress |
| 233 | * so that while zooming is taking place it can stop reacting to scroll events |
| 234 | * from the viewport. This is to avoid flickering. |
| 235 | */ |
| 236 | mightZoom_: function(f) { |
| 237 | this.beforeZoomCallback_(); |
| 238 | this.allowedToChangeZoom_ = true; |
| 239 | f(); |
| 240 | this.allowedToChangeZoom_ = false; |
| 241 | this.afterZoomCallback_(); |
| 242 | }, |
| 243 | |
| 244 | /** |
| 245 | * @private |
[email protected] | 312112c7 | 2014-04-14 01:45:43 | [diff] [blame] | 246 | * Sets the zoom of the viewport. |
| 247 | * @param {number} newZoom the zoom level to zoom to. |
| 248 | */ |
[email protected] | fbad5bb | 2014-07-18 07:20:36 | [diff] [blame] | 249 | setZoomInternal_: function(newZoom) { |
| 250 | if (!this.allowedToChangeZoom_) { |
| 251 | throw 'Called Viewport.setZoomInternal_ without calling ' + |
| 252 | 'Viewport.mightZoom_.'; |
| 253 | } |
sammc | 5c33b7e | 2014-11-07 04:03:09 | [diff] [blame] | 254 | // Record the scroll position (relative to the top-left of the window). |
raymes | bd82a94 | 2015-08-05 07:05:18 | [diff] [blame] | 255 | var currentScrollPos = { |
| 256 | x: this.position.x / this.zoom_, |
| 257 | y: this.position.y / this.zoom_ |
| 258 | }; |
sammc | 5c33b7e | 2014-11-07 04:03:09 | [diff] [blame] | 259 | this.zoom_ = newZoom; |
[email protected] | 3528d630 | 2014-02-19 08:13:07 | [diff] [blame] | 260 | this.contentSizeChanged_(); |
| 261 | // Scroll to the scaled scroll position. |
raymes | bd82a94 | 2015-08-05 07:05:18 | [diff] [blame] | 262 | this.position = { |
| 263 | x: currentScrollPos.x * newZoom, |
| 264 | y: currentScrollPos.y * newZoom |
| 265 | }; |
[email protected] | 3528d630 | 2014-02-19 08:13:07 | [diff] [blame] | 266 | }, |
| 267 | |
| 268 | /** |
[email protected] | fbad5bb | 2014-07-18 07:20:36 | [diff] [blame] | 269 | * Sets the zoom to the given zoom level. |
| 270 | * @param {number} newZoom the zoom level to zoom to. |
[email protected] | 499e956 | 2014-06-26 05:45:27 | [diff] [blame] | 271 | */ |
[email protected] | fbad5bb | 2014-07-18 07:20:36 | [diff] [blame] | 272 | setZoom: function(newZoom) { |
sammc | 5050705e | 2015-03-27 00:01:27 | [diff] [blame] | 273 | this.fittingType_ = Viewport.FittingType.NONE; |
[email protected] | 4271e16b | 2014-08-22 12:18:59 | [diff] [blame] | 274 | newZoom = Math.max(Viewport.ZOOM_FACTOR_RANGE.min, |
| 275 | Math.min(newZoom, Viewport.ZOOM_FACTOR_RANGE.max)); |
[email protected] | 499e956 | 2014-06-26 05:45:27 | [diff] [blame] | 276 | this.mightZoom_(function() { |
[email protected] | fbad5bb | 2014-07-18 07:20:36 | [diff] [blame] | 277 | this.setZoomInternal_(newZoom); |
| 278 | this.updateViewport_(); |
[email protected] | 499e956 | 2014-06-26 05:45:27 | [diff] [blame] | 279 | }.bind(this)); |
| 280 | }, |
| 281 | |
| 282 | /** |
[email protected] | 312112c7 | 2014-04-14 01:45:43 | [diff] [blame] | 283 | * @type {number} the width of scrollbars in the viewport in pixels. |
[email protected] | 3528d630 | 2014-02-19 08:13:07 | [diff] [blame] | 284 | */ |
[email protected] | 312112c7 | 2014-04-14 01:45:43 | [diff] [blame] | 285 | get scrollbarWidth() { |
| 286 | return this.scrollbarWidth_; |
[email protected] | 3528d630 | 2014-02-19 08:13:07 | [diff] [blame] | 287 | }, |
| 288 | |
| 289 | /** |
[email protected] | 312112c7 | 2014-04-14 01:45:43 | [diff] [blame] | 290 | * @type {Viewport.FittingType} the fitting type the viewport is currently in. |
[email protected] | 3528d630 | 2014-02-19 08:13:07 | [diff] [blame] | 291 | */ |
[email protected] | 312112c7 | 2014-04-14 01:45:43 | [diff] [blame] | 292 | get fittingType() { |
| 293 | return this.fittingType_; |
[email protected] | 3528d630 | 2014-02-19 08:13:07 | [diff] [blame] | 294 | }, |
| 295 | |
| 296 | /** |
[email protected] | 56b7e3c | 2014-02-20 04:31:24 | [diff] [blame] | 297 | * @private |
| 298 | * @param {integer} y the y-coordinate to get the page at. |
| 299 | * @return {integer} the index of a page overlapping the given y-coordinate. |
| 300 | */ |
| 301 | getPageAtY_: function(y) { |
| 302 | var min = 0; |
| 303 | var max = this.pageDimensions_.length - 1; |
| 304 | while (max >= min) { |
| 305 | var page = Math.floor(min + ((max - min) / 2)); |
[email protected] | 13df2a4 | 2014-02-27 03:50:41 | [diff] [blame] | 306 | // There might be a gap between the pages, in which case use the bottom |
| 307 | // of the previous page as the top for finding the page. |
| 308 | var top = 0; |
| 309 | if (page > 0) { |
| 310 | top = this.pageDimensions_[page - 1].y + |
| 311 | this.pageDimensions_[page - 1].height; |
| 312 | } |
| 313 | var bottom = this.pageDimensions_[page].y + |
| 314 | this.pageDimensions_[page].height; |
| 315 | |
[email protected] | 56b7e3c | 2014-02-20 04:31:24 | [diff] [blame] | 316 | if (top <= y && bottom > y) |
| 317 | return page; |
| 318 | else if (top > y) |
| 319 | max = page - 1; |
| 320 | else |
| 321 | min = page + 1; |
| 322 | } |
| 323 | return 0; |
| 324 | }, |
| 325 | |
| 326 | /** |
sammc | d06823a96 | 2014-11-10 04:51:15 | [diff] [blame] | 327 | * Returns the page with the greatest proportion of its height in the current |
| 328 | * viewport. |
[email protected] | 3528d630 | 2014-02-19 08:13:07 | [diff] [blame] | 329 | * @return {int} the index of the most visible page. |
| 330 | */ |
| 331 | getMostVisiblePage: function() { |
[email protected] | 312112c7 | 2014-04-14 01:45:43 | [diff] [blame] | 332 | var firstVisiblePage = this.getPageAtY_(this.position.y / this.zoom_); |
sammc | d06823a96 | 2014-11-10 04:51:15 | [diff] [blame] | 333 | if (firstVisiblePage == this.pageDimensions_.length - 1) |
| 334 | return firstVisiblePage; |
| 335 | |
[email protected] | 345e7c6 | 2014-05-02 09:52:58 | [diff] [blame] | 336 | var viewportRect = { |
| 337 | x: this.position.x / this.zoom_, |
| 338 | y: this.position.y / this.zoom_, |
| 339 | width: this.size.width / this.zoom_, |
| 340 | height: this.size.height / this.zoom_ |
| 341 | }; |
sammc | d06823a96 | 2014-11-10 04:51:15 | [diff] [blame] | 342 | var firstVisiblePageVisibility = getIntersectionHeight( |
| 343 | this.pageDimensions_[firstVisiblePage], viewportRect) / |
| 344 | this.pageDimensions_[firstVisiblePage].height; |
| 345 | var nextPageVisibility = getIntersectionHeight( |
| 346 | this.pageDimensions_[firstVisiblePage + 1], viewportRect) / |
| 347 | this.pageDimensions_[firstVisiblePage + 1].height; |
| 348 | if (nextPageVisibility > firstVisiblePageVisibility) |
| 349 | return firstVisiblePage + 1; |
| 350 | return firstVisiblePage; |
[email protected] | 3528d630 | 2014-02-19 08:13:07 | [diff] [blame] | 351 | }, |
| 352 | |
| 353 | /** |
| 354 | * @private |
| 355 | * Compute the zoom level for fit-to-page or fit-to-width. |pageDimensions| is |
| 356 | * the dimensions for a given page and if |widthOnly| is true, it indicates |
| 357 | * that fit-to-page zoom should be computed rather than fit-to-page. |
| 358 | * @param {Object} pageDimensions the dimensions of a given page |
| 359 | * @param {boolean} widthOnly a bool indicating whether fit-to-page or |
| 360 | * fit-to-width should be computed. |
| 361 | * @return {number} the zoom to use |
| 362 | */ |
| 363 | computeFittingZoom_: function(pageDimensions, widthOnly) { |
| 364 | // First compute the zoom without scrollbars. |
| 365 | var zoomWidth = this.window_.innerWidth / pageDimensions.width; |
| 366 | var zoom; |
alexandrec | 9754b9e4 | 2015-01-19 03:41:19 | [diff] [blame] | 367 | var zoomHeight; |
[email protected] | 3528d630 | 2014-02-19 08:13:07 | [diff] [blame] | 368 | if (widthOnly) { |
| 369 | zoom = zoomWidth; |
| 370 | } else { |
alexandrec | 9754b9e4 | 2015-01-19 03:41:19 | [diff] [blame] | 371 | zoomHeight = this.window_.innerHeight / pageDimensions.height; |
[email protected] | 3528d630 | 2014-02-19 08:13:07 | [diff] [blame] | 372 | zoom = Math.min(zoomWidth, zoomHeight); |
| 373 | } |
| 374 | // Check if there needs to be any scrollbars. |
| 375 | var needsScrollbars = this.documentNeedsScrollbars_(zoom); |
| 376 | |
| 377 | // If the document fits, just return the zoom. |
[email protected] | 312112c7 | 2014-04-14 01:45:43 | [diff] [blame] | 378 | if (!needsScrollbars.horizontal && !needsScrollbars.vertical) |
[email protected] | 3528d630 | 2014-02-19 08:13:07 | [diff] [blame] | 379 | return zoom; |
| 380 | |
raymes | e6e90c6 | 2015-08-10 06:21:40 | [diff] [blame] | 381 | var zoomedDimensions = this.getZoomedDocumentDimensions_(zoom); |
[email protected] | 3528d630 | 2014-02-19 08:13:07 | [diff] [blame] | 382 | |
| 383 | // Check if adding a scrollbar will result in needing the other scrollbar. |
| 384 | var scrollbarWidth = this.scrollbarWidth_; |
[email protected] | 312112c7 | 2014-04-14 01:45:43 | [diff] [blame] | 385 | if (needsScrollbars.horizontal && |
[email protected] | 3528d630 | 2014-02-19 08:13:07 | [diff] [blame] | 386 | zoomedDimensions.height > this.window_.innerHeight - scrollbarWidth) { |
[email protected] | 312112c7 | 2014-04-14 01:45:43 | [diff] [blame] | 387 | needsScrollbars.vertical = true; |
[email protected] | 3528d630 | 2014-02-19 08:13:07 | [diff] [blame] | 388 | } |
[email protected] | 312112c7 | 2014-04-14 01:45:43 | [diff] [blame] | 389 | if (needsScrollbars.vertical && |
[email protected] | 3528d630 | 2014-02-19 08:13:07 | [diff] [blame] | 390 | zoomedDimensions.width > this.window_.innerWidth - scrollbarWidth) { |
[email protected] | 312112c7 | 2014-04-14 01:45:43 | [diff] [blame] | 391 | needsScrollbars.horizontal = true; |
[email protected] | 3528d630 | 2014-02-19 08:13:07 | [diff] [blame] | 392 | } |
| 393 | |
| 394 | // Compute available window space. |
| 395 | var windowWithScrollbars = { |
| 396 | width: this.window_.innerWidth, |
| 397 | height: this.window_.innerHeight |
| 398 | }; |
[email protected] | 312112c7 | 2014-04-14 01:45:43 | [diff] [blame] | 399 | if (needsScrollbars.horizontal) |
[email protected] | 3528d630 | 2014-02-19 08:13:07 | [diff] [blame] | 400 | windowWithScrollbars.height -= scrollbarWidth; |
[email protected] | 312112c7 | 2014-04-14 01:45:43 | [diff] [blame] | 401 | if (needsScrollbars.vertical) |
[email protected] | 3528d630 | 2014-02-19 08:13:07 | [diff] [blame] | 402 | windowWithScrollbars.width -= scrollbarWidth; |
| 403 | |
| 404 | // Recompute the zoom. |
| 405 | zoomWidth = windowWithScrollbars.width / pageDimensions.width; |
| 406 | if (widthOnly) { |
| 407 | zoom = zoomWidth; |
| 408 | } else { |
alexandrec | 9754b9e4 | 2015-01-19 03:41:19 | [diff] [blame] | 409 | zoomHeight = windowWithScrollbars.height / pageDimensions.height; |
[email protected] | 3528d630 | 2014-02-19 08:13:07 | [diff] [blame] | 410 | zoom = Math.min(zoomWidth, zoomHeight); |
| 411 | } |
| 412 | return zoom; |
| 413 | }, |
| 414 | |
| 415 | /** |
| 416 | * Zoom the viewport so that the page-width consumes the entire viewport. |
| 417 | */ |
| 418 | fitToWidth: function() { |
[email protected] | 499e956 | 2014-06-26 05:45:27 | [diff] [blame] | 419 | this.mightZoom_(function() { |
| 420 | this.fittingType_ = Viewport.FittingType.FIT_TO_WIDTH; |
| 421 | if (!this.documentDimensions_) |
| 422 | return; |
[email protected] | 499e956 | 2014-06-26 05:45:27 | [diff] [blame] | 423 | // When computing fit-to-width, the maximum width of a page in the |
| 424 | // document is used, which is equal to the size of the document width. |
[email protected] | fbad5bb | 2014-07-18 07:20:36 | [diff] [blame] | 425 | this.setZoomInternal_(this.computeFittingZoom_(this.documentDimensions_, |
| 426 | true)); |
[email protected] | 499e956 | 2014-06-26 05:45:27 | [diff] [blame] | 427 | var page = this.getMostVisiblePage(); |
[email protected] | 499e956 | 2014-06-26 05:45:27 | [diff] [blame] | 428 | this.updateViewport_(); |
| 429 | }.bind(this)); |
[email protected] | 3528d630 | 2014-02-19 08:13:07 | [diff] [blame] | 430 | }, |
| 431 | |
| 432 | /** |
raymes | 161514f | 2015-02-17 05:09:51 | [diff] [blame] | 433 | * @private |
| 434 | * Zoom the viewport so that a page consumes the entire viewport. |
| 435 | * @param {boolean} scrollToTopOfPage Set to true if the viewport should be |
| 436 | * scrolled to the top of the current page. Set to false if the viewport |
| 437 | * should remain at the current scroll position. |
[email protected] | 3528d630 | 2014-02-19 08:13:07 | [diff] [blame] | 438 | */ |
raymes | 161514f | 2015-02-17 05:09:51 | [diff] [blame] | 439 | fitToPageInternal_: function(scrollToTopOfPage) { |
[email protected] | 499e956 | 2014-06-26 05:45:27 | [diff] [blame] | 440 | this.mightZoom_(function() { |
| 441 | this.fittingType_ = Viewport.FittingType.FIT_TO_PAGE; |
| 442 | if (!this.documentDimensions_) |
| 443 | return; |
| 444 | var page = this.getMostVisiblePage(); |
sammc | 07f53aa | 2014-11-06 06:57:46 | [diff] [blame] | 445 | // Fit to the current page's height and the widest page's width. |
| 446 | var dimensions = { |
| 447 | width: this.documentDimensions_.width, |
| 448 | height: this.pageDimensions_[page].height, |
| 449 | }; |
| 450 | this.setZoomInternal_(this.computeFittingZoom_(dimensions, false)); |
raymes | bd82a94 | 2015-08-05 07:05:18 | [diff] [blame] | 451 | if (scrollToTopOfPage) { |
| 452 | this.position = { |
| 453 | x: 0, |
| 454 | y: this.pageDimensions_[page].y * this.zoom_ |
| 455 | }; |
| 456 | } |
[email protected] | 499e956 | 2014-06-26 05:45:27 | [diff] [blame] | 457 | this.updateViewport_(); |
| 458 | }.bind(this)); |
[email protected] | 3528d630 | 2014-02-19 08:13:07 | [diff] [blame] | 459 | }, |
| 460 | |
| 461 | /** |
raymes | 161514f | 2015-02-17 05:09:51 | [diff] [blame] | 462 | * Zoom the viewport so that a page consumes the entire viewport. Also scrolls |
| 463 | * the viewport to the top of the current page. |
| 464 | */ |
| 465 | fitToPage: function() { |
| 466 | this.fitToPageInternal_(true); |
| 467 | }, |
| 468 | |
| 469 | /** |
[email protected] | 3528d630 | 2014-02-19 08:13:07 | [diff] [blame] | 470 | * Zoom out to the next predefined zoom level. |
| 471 | */ |
| 472 | zoomOut: function() { |
[email protected] | 499e956 | 2014-06-26 05:45:27 | [diff] [blame] | 473 | this.mightZoom_(function() { |
| 474 | this.fittingType_ = Viewport.FittingType.NONE; |
| 475 | var nextZoom = Viewport.ZOOM_FACTORS[0]; |
| 476 | for (var i = 0; i < Viewport.ZOOM_FACTORS.length; i++) { |
| 477 | if (Viewport.ZOOM_FACTORS[i] < this.zoom_) |
| 478 | nextZoom = Viewport.ZOOM_FACTORS[i]; |
| 479 | } |
[email protected] | fbad5bb | 2014-07-18 07:20:36 | [diff] [blame] | 480 | this.setZoomInternal_(nextZoom); |
[email protected] | 499e956 | 2014-06-26 05:45:27 | [diff] [blame] | 481 | this.updateViewport_(); |
| 482 | }.bind(this)); |
[email protected] | 3528d630 | 2014-02-19 08:13:07 | [diff] [blame] | 483 | }, |
| 484 | |
| 485 | /** |
| 486 | * Zoom in to the next predefined zoom level. |
| 487 | */ |
| 488 | zoomIn: function() { |
[email protected] | 499e956 | 2014-06-26 05:45:27 | [diff] [blame] | 489 | this.mightZoom_(function() { |
| 490 | this.fittingType_ = Viewport.FittingType.NONE; |
| 491 | var nextZoom = Viewport.ZOOM_FACTORS[Viewport.ZOOM_FACTORS.length - 1]; |
| 492 | for (var i = Viewport.ZOOM_FACTORS.length - 1; i >= 0; i--) { |
| 493 | if (Viewport.ZOOM_FACTORS[i] > this.zoom_) |
| 494 | nextZoom = Viewport.ZOOM_FACTORS[i]; |
| 495 | } |
[email protected] | fbad5bb | 2014-07-18 07:20:36 | [diff] [blame] | 496 | this.setZoomInternal_(nextZoom); |
[email protected] | 499e956 | 2014-06-26 05:45:27 | [diff] [blame] | 497 | this.updateViewport_(); |
| 498 | }.bind(this)); |
[email protected] | 3528d630 | 2014-02-19 08:13:07 | [diff] [blame] | 499 | }, |
| 500 | |
| 501 | /** |
| 502 | * Go to the given page index. |
[email protected] | f90b9a4 | 2014-08-20 05:37:34 | [diff] [blame] | 503 | * @param {number} page the index of the page to go to. zero-based. |
[email protected] | 3528d630 | 2014-02-19 08:13:07 | [diff] [blame] | 504 | */ |
| 505 | goToPage: function(page) { |
[email protected] | 499e956 | 2014-06-26 05:45:27 | [diff] [blame] | 506 | this.mightZoom_(function() { |
alexandrec | 9754b9e4 | 2015-01-19 03:41:19 | [diff] [blame] | 507 | if (this.pageDimensions_.length === 0) |
[email protected] | 499e956 | 2014-06-26 05:45:27 | [diff] [blame] | 508 | return; |
| 509 | if (page < 0) |
| 510 | page = 0; |
| 511 | if (page >= this.pageDimensions_.length) |
| 512 | page = this.pageDimensions_.length - 1; |
| 513 | var dimensions = this.pageDimensions_[page]; |
raymes | bd82a94 | 2015-08-05 07:05:18 | [diff] [blame] | 514 | var toolbarOffset = 0; |
| 515 | // Unless we're in fit to page mode, scroll above the page by |
| 516 | // |this.topToolbarHeight_| so that the toolbar isn't covering it |
| 517 | // initially. |
| 518 | if (this.fittingType_ != Viewport.FittingType.FIT_TO_PAGE) |
| 519 | toolbarOffset = this.topToolbarHeight_; |
| 520 | this.position = { |
| 521 | x: dimensions.x * this.zoom_, |
| 522 | y: dimensions.y * this.zoom_ - toolbarOffset |
| 523 | }; |
[email protected] | 499e956 | 2014-06-26 05:45:27 | [diff] [blame] | 524 | this.updateViewport_(); |
| 525 | }.bind(this)); |
[email protected] | 3528d630 | 2014-02-19 08:13:07 | [diff] [blame] | 526 | }, |
| 527 | |
| 528 | /** |
| 529 | * Set the dimensions of the document. |
| 530 | * @param {Object} documentDimensions the dimensions of the document |
| 531 | */ |
| 532 | setDocumentDimensions: function(documentDimensions) { |
[email protected] | 499e956 | 2014-06-26 05:45:27 | [diff] [blame] | 533 | this.mightZoom_(function() { |
| 534 | var initialDimensions = !this.documentDimensions_; |
| 535 | this.documentDimensions_ = documentDimensions; |
| 536 | this.pageDimensions_ = this.documentDimensions_.pageDimensions; |
| 537 | if (initialDimensions) { |
sammc | 43d4982f | 2015-04-22 07:46:51 | [diff] [blame] | 538 | this.setZoomInternal_( |
| 539 | Math.min(this.defaultZoom_, |
| 540 | this.computeFittingZoom_(this.documentDimensions_, true))); |
raymes | bd82a94 | 2015-08-05 07:05:18 | [diff] [blame] | 541 | this.position = { |
| 542 | x: 0, |
| 543 | y: -this.topToolbarHeight_ |
| 544 | }; |
[email protected] | 499e956 | 2014-06-26 05:45:27 | [diff] [blame] | 545 | } |
| 546 | this.contentSizeChanged_(); |
| 547 | this.resize_(); |
| 548 | }.bind(this)); |
[email protected] | 312112c7 | 2014-04-14 01:45:43 | [diff] [blame] | 549 | }, |
| 550 | |
| 551 | /** |
| 552 | * Get the coordinates of the page contents (excluding the page shadow) |
| 553 | * relative to the screen. |
| 554 | * @param {number} page the index of the page to get the rect for. |
| 555 | * @return {Object} a rect representing the page in screen coordinates. |
| 556 | */ |
| 557 | getPageScreenRect: function(page) { |
[email protected] | 499e956 | 2014-06-26 05:45:27 | [diff] [blame] | 558 | if (!this.documentDimensions_) { |
| 559 | return { |
| 560 | x: 0, |
| 561 | y: 0, |
| 562 | width: 0, |
| 563 | height: 0 |
| 564 | }; |
| 565 | } |
[email protected] | 312112c7 | 2014-04-14 01:45:43 | [diff] [blame] | 566 | if (page >= this.pageDimensions_.length) |
| 567 | page = this.pageDimensions_.length - 1; |
| 568 | |
| 569 | var pageDimensions = this.pageDimensions_[page]; |
| 570 | |
| 571 | // Compute the page dimensions minus the shadows. |
| 572 | var insetDimensions = { |
| 573 | x: pageDimensions.x + Viewport.PAGE_SHADOW.left, |
| 574 | y: pageDimensions.y + Viewport.PAGE_SHADOW.top, |
| 575 | width: pageDimensions.width - Viewport.PAGE_SHADOW.left - |
| 576 | Viewport.PAGE_SHADOW.right, |
| 577 | height: pageDimensions.height - Viewport.PAGE_SHADOW.top - |
| 578 | Viewport.PAGE_SHADOW.bottom |
| 579 | }; |
| 580 | |
[email protected] | 345e7c6 | 2014-05-02 09:52:58 | [diff] [blame] | 581 | // Compute the x-coordinate of the page within the document. |
| 582 | // TODO(raymes): This should really be set when the PDF plugin passes the |
| 583 | // page coordinates, but it isn't yet. |
| 584 | var x = (this.documentDimensions_.width - pageDimensions.width) / 2 + |
| 585 | Viewport.PAGE_SHADOW.left; |
| 586 | // Compute the space on the left of the document if the document fits |
| 587 | // completely in the screen. |
| 588 | var spaceOnLeft = (this.size.width - |
| 589 | this.documentDimensions_.width * this.zoom_) / 2; |
| 590 | spaceOnLeft = Math.max(spaceOnLeft, 0); |
[email protected] | 312112c7 | 2014-04-14 01:45:43 | [diff] [blame] | 591 | |
| 592 | return { |
[email protected] | 345e7c6 | 2014-05-02 09:52:58 | [diff] [blame] | 593 | x: x * this.zoom_ + spaceOnLeft - this.window_.pageXOffset, |
| 594 | y: insetDimensions.y * this.zoom_ - this.window_.pageYOffset, |
[email protected] | 312112c7 | 2014-04-14 01:45:43 | [diff] [blame] | 595 | width: insetDimensions.width * this.zoom_, |
| 596 | height: insetDimensions.height * this.zoom_ |
| 597 | }; |
[email protected] | 3528d630 | 2014-02-19 08:13:07 | [diff] [blame] | 598 | } |
| 599 | }; |