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