[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 | /** |
Henrique Nakashima | 585c7af0 | 2018-03-27 04:55:21 | [diff] [blame] | 6 | * @typedef {{ |
| 7 | * x: number, |
| 8 | * y: number |
| 9 | * }} |
| 10 | */ |
| 11 | let Point; |
| 12 | |
| 13 | /** |
| 14 | * @typedef {{ |
| 15 | * x: number | undefined, |
| 16 | * y: number | undefined |
| 17 | * }} |
| 18 | */ |
| 19 | let PartialPoint; |
| 20 | |
| 21 | /** |
sammc | d06823a96 | 2014-11-10 04:51:15 | [diff] [blame] | 22 | * Returns the height of the intersection of two rectangles. |
Henrique Nakashima | d5de6d0d | 2018-04-13 18:02:42 | [diff] [blame] | 23 | * |
[email protected] | 3528d630 | 2014-02-19 08:13:07 | [diff] [blame] | 24 | * @param {Object} rect1 the first rect |
| 25 | * @param {Object} rect2 the second rect |
sammc | d06823a96 | 2014-11-10 04:51:15 | [diff] [blame] | 26 | * @return {number} the height of the intersection of the rects |
[email protected] | 3528d630 | 2014-02-19 08:13:07 | [diff] [blame] | 27 | */ |
sammc | d06823a96 | 2014-11-10 04:51:15 | [diff] [blame] | 28 | function getIntersectionHeight(rect1, rect2) { |
dbeam | 70db0fb | 2017-06-19 17:09:27 | [diff] [blame] | 29 | return Math.max( |
| 30 | 0, |
[email protected] | 3528d630 | 2014-02-19 08:13:07 | [diff] [blame] | 31 | Math.min(rect1.y + rect1.height, rect2.y + rect2.height) - |
dbeam | 70db0fb | 2017-06-19 17:09:27 | [diff] [blame] | 32 | Math.max(rect1.y, rect2.y)); |
[email protected] | 3528d630 | 2014-02-19 08:13:07 | [diff] [blame] | 33 | } |
| 34 | |
| 35 | /** |
mcnee | 6e1abbf | 2016-11-09 18:05:31 | [diff] [blame] | 36 | * Computes vector between two points. |
Henrique Nakashima | d5de6d0d | 2018-04-13 18:02:42 | [diff] [blame] | 37 | * |
mcnee | 6e1abbf | 2016-11-09 18:05:31 | [diff] [blame] | 38 | * @param {!Object} p1 The first point. |
| 39 | * @param {!Object} p2 The second point. |
| 40 | * @return {!Object} The vector. |
| 41 | */ |
| 42 | function vectorDelta(p1, p2) { |
dbeam | 70db0fb | 2017-06-19 17:09:27 | [diff] [blame] | 43 | return {x: p2.x - p1.x, y: p2.y - p1.y}; |
mcnee | 6e1abbf | 2016-11-09 18:05:31 | [diff] [blame] | 44 | } |
| 45 | |
| 46 | function frameToPluginCoordinate(coordinateInFrame) { |
| 47 | var container = $('plugin'); |
| 48 | return { |
| 49 | x: coordinateInFrame.x - container.getBoundingClientRect().left, |
| 50 | y: coordinateInFrame.y - container.getBoundingClientRect().top |
| 51 | }; |
| 52 | } |
| 53 | |
| 54 | /** |
[email protected] | 3528d630 | 2014-02-19 08:13:07 | [diff] [blame] | 55 | * Create a new viewport. |
Henrique Nakashima | d5de6d0d | 2018-04-13 18:02:42 | [diff] [blame] | 56 | * |
[email protected] | 3528d630 | 2014-02-19 08:13:07 | [diff] [blame] | 57 | * @param {Window} window the window |
| 58 | * @param {Object} sizer is the element which represents the size of the |
| 59 | * document in the viewport |
[email protected] | 3528d630 | 2014-02-19 08:13:07 | [diff] [blame] | 60 | * @param {Function} viewportChangedCallback is run when the viewport changes |
[email protected] | 499e956 | 2014-06-26 05:45:27 | [diff] [blame] | 61 | * @param {Function} beforeZoomCallback is run before a change in zoom |
| 62 | * @param {Function} afterZoomCallback is run after a change in zoom |
rbpotter | a82dacc | 2017-09-08 19:39:39 | [diff] [blame] | 63 | * @param {Function} setUserInitiatedCallback is run to indicate whether a zoom |
| 64 | * event is user initiated. |
[email protected] | 345e7c6 | 2014-05-02 09:52:58 | [diff] [blame] | 65 | * @param {number} scrollbarWidth the width of scrollbars on the page |
sammc | 43d4982f | 2015-04-22 07:46:51 | [diff] [blame] | 66 | * @param {number} defaultZoom The default zoom level. |
raymes | bd82a94 | 2015-08-05 07:05:18 | [diff] [blame] | 67 | * @param {number} topToolbarHeight The number of pixels that should initially |
| 68 | * be left blank above the document for the toolbar. |
Henrique Nakashima | d5de6d0d | 2018-04-13 18:02:42 | [diff] [blame] | 69 | * @constructor |
[email protected] | 3528d630 | 2014-02-19 08:13:07 | [diff] [blame] | 70 | */ |
dbeam | 70db0fb | 2017-06-19 17:09:27 | [diff] [blame] | 71 | function Viewport( |
| 72 | window, sizer, viewportChangedCallback, beforeZoomCallback, |
rbpotter | a82dacc | 2017-09-08 19:39:39 | [diff] [blame] | 73 | afterZoomCallback, setUserInitiatedCallback, scrollbarWidth, defaultZoom, |
| 74 | topToolbarHeight) { |
[email protected] | 3528d630 | 2014-02-19 08:13:07 | [diff] [blame] | 75 | this.window_ = window; |
| 76 | this.sizer_ = sizer; |
[email protected] | 3528d630 | 2014-02-19 08:13:07 | [diff] [blame] | 77 | this.viewportChangedCallback_ = viewportChangedCallback; |
[email protected] | 499e956 | 2014-06-26 05:45:27 | [diff] [blame] | 78 | this.beforeZoomCallback_ = beforeZoomCallback; |
| 79 | this.afterZoomCallback_ = afterZoomCallback; |
rbpotter | a82dacc | 2017-09-08 19:39:39 | [diff] [blame] | 80 | this.setUserInitiatedCallback_ = setUserInitiatedCallback; |
[email protected] | 499e956 | 2014-06-26 05:45:27 | [diff] [blame] | 81 | this.allowedToChangeZoom_ = false; |
mcnee | 2999413a | 2016-12-06 20:29:25 | [diff] [blame] | 82 | this.internalZoom_ = 1; |
| 83 | this.zoomManager_ = new InactiveZoomManager(this, 1); |
[email protected] | 312112c7 | 2014-04-14 01:45:43 | [diff] [blame] | 84 | this.documentDimensions_ = null; |
[email protected] | 3528d630 | 2014-02-19 08:13:07 | [diff] [blame] | 85 | this.pageDimensions_ = []; |
[email protected] | 345e7c6 | 2014-05-02 09:52:58 | [diff] [blame] | 86 | this.scrollbarWidth_ = scrollbarWidth; |
Henrique Nakashima | 50b18e0 | 2017-11-21 23:29:57 | [diff] [blame] | 87 | this.fittingType_ = FittingType.NONE; |
sammc | 43d4982f | 2015-04-22 07:46:51 | [diff] [blame] | 88 | this.defaultZoom_ = defaultZoom; |
raymes | bd82a94 | 2015-08-05 07:05:18 | [diff] [blame] | 89 | this.topToolbarHeight_ = topToolbarHeight; |
mcnee | 6e1abbf | 2016-11-09 18:05:31 | [diff] [blame] | 90 | this.prevScale_ = 1; |
| 91 | this.pinchPhase_ = Viewport.PinchPhase.PINCH_NONE; |
| 92 | this.pinchPanVector_ = null; |
| 93 | this.pinchCenter_ = null; |
| 94 | this.firstPinchCenterInFrame_ = null; |
[email protected] | 3528d630 | 2014-02-19 08:13:07 | [diff] [blame] | 95 | |
| 96 | window.addEventListener('scroll', this.updateViewport_.bind(this)); |
rbpotter | a82dacc | 2017-09-08 19:39:39 | [diff] [blame] | 97 | window.addEventListener('resize', this.resizeWrapper_.bind(this)); |
[email protected] | 3528d630 | 2014-02-19 08:13:07 | [diff] [blame] | 98 | } |
| 99 | |
[email protected] | 312112c7 | 2014-04-14 01:45:43 | [diff] [blame] | 100 | /** |
mcnee | 6e1abbf | 2016-11-09 18:05:31 | [diff] [blame] | 101 | * Enumeration of pinch states. |
| 102 | * This should match PinchPhase enum in pdf/out_of_process_instance.h |
| 103 | * @enum {number} |
| 104 | */ |
| 105 | Viewport.PinchPhase = { |
| 106 | PINCH_NONE: 0, |
| 107 | PINCH_START: 1, |
| 108 | PINCH_UPDATE_ZOOM_OUT: 2, |
| 109 | PINCH_UPDATE_ZOOM_IN: 3, |
| 110 | PINCH_END: 4 |
| 111 | }; |
| 112 | |
| 113 | /** |
[email protected] | 8dcaa26 | 2014-05-30 13:33:37 | [diff] [blame] | 114 | * The increment to scroll a page by in pixels when up/down/left/right arrow |
| 115 | * keys are pressed. Usually we just let the browser handle scrolling on the |
| 116 | * window when these keys are pressed but in certain cases we need to simulate |
| 117 | * these events. |
| 118 | */ |
| 119 | Viewport.SCROLL_INCREMENT = 40; |
| 120 | |
| 121 | /** |
[email protected] | 312112c7 | 2014-04-14 01:45:43 | [diff] [blame] | 122 | * Predefined zoom factors to be used when zooming in/out. These are in |
bsep | 24e7ec4 | 2016-08-25 02:11:20 | [diff] [blame] | 123 | * ascending order. This should match the lists in |
| 124 | * components/ui/zoom/page_zoom_constants.h and |
| 125 | * chrome/browser/resources/settings/appearance_page/appearance_page.js |
[email protected] | 312112c7 | 2014-04-14 01:45:43 | [diff] [blame] | 126 | */ |
dbeam | 70db0fb | 2017-06-19 17:09:27 | [diff] [blame] | 127 | Viewport.ZOOM_FACTORS = [ |
| 128 | 0.25, 1 / 3, 0.5, 2 / 3, 0.75, 0.8, 0.9, 1, 1.1, 1.25, 1.5, 1.75, 2, 2.5, 3, |
| 129 | 4, 5 |
| 130 | ]; |
[email protected] | 312112c7 | 2014-04-14 01:45:43 | [diff] [blame] | 131 | |
| 132 | /** |
[email protected] | 4271e16b | 2014-08-22 12:18:59 | [diff] [blame] | 133 | * The minimum and maximum range to be used to clip zoom factor. |
| 134 | */ |
| 135 | Viewport.ZOOM_FACTOR_RANGE = { |
| 136 | min: Viewport.ZOOM_FACTORS[0], |
| 137 | max: Viewport.ZOOM_FACTORS[Viewport.ZOOM_FACTORS.length - 1] |
| 138 | }; |
| 139 | |
| 140 | /** |
Kevin McNee | 4e78edc | 2017-10-26 19:55:54 | [diff] [blame] | 141 | * Clamps the zoom factor (or page scale factor) to be within the limits. |
Henrique Nakashima | d5de6d0d | 2018-04-13 18:02:42 | [diff] [blame] | 142 | * |
Kevin McNee | 4e78edc | 2017-10-26 19:55:54 | [diff] [blame] | 143 | * @param {number} factor The zoom/scale factor. |
| 144 | * @return {number} The factor clamped within the limits. |
| 145 | */ |
| 146 | Viewport.clampZoom = function(factor) { |
| 147 | return Math.max( |
| 148 | Viewport.ZOOM_FACTOR_RANGE.min, |
| 149 | Math.min(factor, Viewport.ZOOM_FACTOR_RANGE.max)); |
| 150 | }; |
| 151 | |
| 152 | /** |
[email protected] | 312112c7 | 2014-04-14 01:45:43 | [diff] [blame] | 153 | * The width of the page shadow around pages in pixels. |
| 154 | */ |
dbeam | 70db0fb | 2017-06-19 17:09:27 | [diff] [blame] | 155 | Viewport.PAGE_SHADOW = { |
| 156 | top: 3, |
| 157 | bottom: 7, |
| 158 | left: 5, |
| 159 | right: 5 |
| 160 | }; |
[email protected] | 312112c7 | 2014-04-14 01:45:43 | [diff] [blame] | 161 | |
[email protected] | 3528d630 | 2014-02-19 08:13:07 | [diff] [blame] | 162 | Viewport.prototype = { |
| 163 | /** |
raymes | e6e90c6 | 2015-08-10 06:21:40 | [diff] [blame] | 164 | * Returns the zoomed and rounded document dimensions for the given zoom. |
| 165 | * Rounding is necessary when interacting with the renderer which tends to |
| 166 | * operate in integral values (for example for determining if scrollbars |
| 167 | * should be shown). |
Henrique Nakashima | d5de6d0d | 2018-04-13 18:02:42 | [diff] [blame] | 168 | * |
raymes | e6e90c6 | 2015-08-10 06:21:40 | [diff] [blame] | 169 | * @param {number} zoom The zoom to use to compute the scaled dimensions. |
| 170 | * @return {Object} A dictionary with scaled 'width'/'height' of the document. |
| 171 | * @private |
| 172 | */ |
| 173 | getZoomedDocumentDimensions_: function(zoom) { |
| 174 | if (!this.documentDimensions_) |
| 175 | return null; |
| 176 | return { |
| 177 | width: Math.round(this.documentDimensions_.width * zoom), |
| 178 | height: Math.round(this.documentDimensions_.height * zoom) |
| 179 | }; |
| 180 | }, |
| 181 | |
| 182 | /** |
[email protected] | 3528d630 | 2014-02-19 08:13:07 | [diff] [blame] | 183 | * Returns true if the document needs scrollbars at the given zoom level. |
Henrique Nakashima | d5de6d0d | 2018-04-13 18:02:42 | [diff] [blame] | 184 | * |
[email protected] | 3528d630 | 2014-02-19 08:13:07 | [diff] [blame] | 185 | * @param {number} zoom compute whether scrollbars are needed at this zoom |
[email protected] | 312112c7 | 2014-04-14 01:45:43 | [diff] [blame] | 186 | * @return {Object} with 'horizontal' and 'vertical' keys which map to bool |
| 187 | * values indicating if the horizontal and vertical scrollbars are needed |
[email protected] | 3528d630 | 2014-02-19 08:13:07 | [diff] [blame] | 188 | * respectively. |
Henrique Nakashima | d5de6d0d | 2018-04-13 18:02:42 | [diff] [blame] | 189 | * @private |
[email protected] | 3528d630 | 2014-02-19 08:13:07 | [diff] [blame] | 190 | */ |
| 191 | documentNeedsScrollbars_: function(zoom) { |
raymes | e6e90c6 | 2015-08-10 06:21:40 | [diff] [blame] | 192 | var zoomedDimensions = this.getZoomedDocumentDimensions_(zoom); |
| 193 | if (!zoomedDimensions) { |
dbeam | 70db0fb | 2017-06-19 17:09:27 | [diff] [blame] | 194 | return {horizontal: false, vertical: false}; |
[email protected] | 499e956 | 2014-06-26 05:45:27 | [diff] [blame] | 195 | } |
sammc | 63334fed | 2014-11-10 03:07:35 | [diff] [blame] | 196 | |
| 197 | // If scrollbars are required for one direction, expand the document in the |
| 198 | // other direction to take the width of the scrollbars into account when |
| 199 | // deciding whether the other direction needs scrollbars. |
raymes | e6e90c6 | 2015-08-10 06:21:40 | [diff] [blame] | 200 | if (zoomedDimensions.width > this.window_.innerWidth) |
| 201 | zoomedDimensions.height += this.scrollbarWidth_; |
| 202 | else if (zoomedDimensions.height > this.window_.innerHeight) |
| 203 | zoomedDimensions.width += this.scrollbarWidth_; |
[email protected] | 3528d630 | 2014-02-19 08:13:07 | [diff] [blame] | 204 | return { |
raymes | e6e90c6 | 2015-08-10 06:21:40 | [diff] [blame] | 205 | horizontal: zoomedDimensions.width > this.window_.innerWidth, |
raymes | 051fb2c | 2015-09-21 04:56:41 | [diff] [blame] | 206 | vertical: zoomedDimensions.height + this.topToolbarHeight_ > |
| 207 | this.window_.innerHeight |
[email protected] | 3528d630 | 2014-02-19 08:13:07 | [diff] [blame] | 208 | }; |
| 209 | }, |
| 210 | |
| 211 | /** |
| 212 | * Returns true if the document needs scrollbars at the current zoom level. |
Henrique Nakashima | d5de6d0d | 2018-04-13 18:02:42 | [diff] [blame] | 213 | * |
[email protected] | 3528d630 | 2014-02-19 08:13:07 | [diff] [blame] | 214 | * @return {Object} with 'x' and 'y' keys which map to bool values |
| 215 | * indicating if the horizontal and vertical scrollbars are needed |
| 216 | * respectively. |
| 217 | */ |
| 218 | documentHasScrollbars: function() { |
mcnee | 2999413a | 2016-12-06 20:29:25 | [diff] [blame] | 219 | return this.documentNeedsScrollbars_(this.zoom); |
[email protected] | 3528d630 | 2014-02-19 08:13:07 | [diff] [blame] | 220 | }, |
| 221 | |
| 222 | /** |
[email protected] | 3528d630 | 2014-02-19 08:13:07 | [diff] [blame] | 223 | * Helper function called when the zoomed document size changes. |
Henrique Nakashima | d5de6d0d | 2018-04-13 18:02:42 | [diff] [blame] | 224 | * |
| 225 | * @private |
[email protected] | 3528d630 | 2014-02-19 08:13:07 | [diff] [blame] | 226 | */ |
| 227 | contentSizeChanged_: function() { |
mcnee | 2999413a | 2016-12-06 20:29:25 | [diff] [blame] | 228 | var zoomedDimensions = this.getZoomedDocumentDimensions_(this.zoom); |
raymes | e6e90c6 | 2015-08-10 06:21:40 | [diff] [blame] | 229 | if (zoomedDimensions) { |
| 230 | this.sizer_.style.width = zoomedDimensions.width + 'px'; |
dbeam | 70db0fb | 2017-06-19 17:09:27 | [diff] [blame] | 231 | this.sizer_.style.height = |
| 232 | zoomedDimensions.height + this.topToolbarHeight_ + 'px'; |
[email protected] | 345e7c6 | 2014-05-02 09:52:58 | [diff] [blame] | 233 | } |
[email protected] | 3528d630 | 2014-02-19 08:13:07 | [diff] [blame] | 234 | }, |
| 235 | |
| 236 | /** |
[email protected] | 312112c7 | 2014-04-14 01:45:43 | [diff] [blame] | 237 | * Called when the viewport should be updated. |
Henrique Nakashima | d5de6d0d | 2018-04-13 18:02:42 | [diff] [blame] | 238 | * |
| 239 | * @private |
[email protected] | 3528d630 | 2014-02-19 08:13:07 | [diff] [blame] | 240 | */ |
[email protected] | 312112c7 | 2014-04-14 01:45:43 | [diff] [blame] | 241 | updateViewport_: function() { |
| 242 | this.viewportChangedCallback_(); |
| 243 | }, |
| 244 | |
| 245 | /** |
rbpotter | a82dacc | 2017-09-08 19:39:39 | [diff] [blame] | 246 | * Called when the browser window size changes. |
Henrique Nakashima | d5de6d0d | 2018-04-13 18:02:42 | [diff] [blame] | 247 | * |
| 248 | * @private |
rbpotter | a82dacc | 2017-09-08 19:39:39 | [diff] [blame] | 249 | */ |
| 250 | resizeWrapper_: function() { |
| 251 | this.setUserInitiatedCallback_(false); |
| 252 | this.resize_(); |
| 253 | this.setUserInitiatedCallback_(true); |
| 254 | }, |
| 255 | |
| 256 | /** |
[email protected] | 312112c7 | 2014-04-14 01:45:43 | [diff] [blame] | 257 | * Called when the viewport size changes. |
Henrique Nakashima | d5de6d0d | 2018-04-13 18:02:42 | [diff] [blame] | 258 | * |
| 259 | * @private |
[email protected] | 312112c7 | 2014-04-14 01:45:43 | [diff] [blame] | 260 | */ |
| 261 | resize_: function() { |
Henrique Nakashima | 50b18e0 | 2017-11-21 23:29:57 | [diff] [blame] | 262 | if (this.fittingType_ == FittingType.FIT_TO_PAGE) |
raymes | 161514f | 2015-02-17 05:09:51 | [diff] [blame] | 263 | this.fitToPageInternal_(false); |
Henrique Nakashima | 50b18e0 | 2017-11-21 23:29:57 | [diff] [blame] | 264 | else if (this.fittingType_ == FittingType.FIT_TO_WIDTH) |
[email protected] | 312112c7 | 2014-04-14 01:45:43 | [diff] [blame] | 265 | this.fitToWidth(); |
Henrique Nakashima | 50b18e0 | 2017-11-21 23:29:57 | [diff] [blame] | 266 | else if (this.fittingType_ == FittingType.FIT_TO_HEIGHT) |
| 267 | this.fitToHeightInternal_(false); |
Henrique Nakashima | 4cf9ed4 | 2018-09-07 21:02:03 | [diff] [blame^] | 268 | else if (this.internalZoom_ == 0) |
| 269 | this.fitToNone(); |
[email protected] | 312112c7 | 2014-04-14 01:45:43 | [diff] [blame] | 270 | else |
| 271 | this.updateViewport_(); |
| 272 | }, |
| 273 | |
| 274 | /** |
| 275 | * @type {Object} the scroll position of the viewport. |
| 276 | */ |
| 277 | get position() { |
| 278 | return { |
| 279 | x: this.window_.pageXOffset, |
raymes | bd82a94 | 2015-08-05 07:05:18 | [diff] [blame] | 280 | y: this.window_.pageYOffset - this.topToolbarHeight_ |
[email protected] | 312112c7 | 2014-04-14 01:45:43 | [diff] [blame] | 281 | }; |
| 282 | }, |
| 283 | |
| 284 | /** |
| 285 | * Scroll the viewport to the specified position. |
Henrique Nakashima | d5de6d0d | 2018-04-13 18:02:42 | [diff] [blame] | 286 | * |
Henrique Nakashima | 585c7af0 | 2018-03-27 04:55:21 | [diff] [blame] | 287 | * @type {Object} position The position to scroll to. |
[email protected] | 312112c7 | 2014-04-14 01:45:43 | [diff] [blame] | 288 | */ |
| 289 | set position(position) { |
raymes | bd82a94 | 2015-08-05 07:05:18 | [diff] [blame] | 290 | this.window_.scrollTo(position.x, position.y + this.topToolbarHeight_); |
[email protected] | 312112c7 | 2014-04-14 01:45:43 | [diff] [blame] | 291 | }, |
| 292 | |
| 293 | /** |
| 294 | * @type {Object} the size of the viewport excluding scrollbars. |
| 295 | */ |
| 296 | get size() { |
mcnee | 2999413a | 2016-12-06 20:29:25 | [diff] [blame] | 297 | var needsScrollbars = this.documentNeedsScrollbars_(this.zoom); |
[email protected] | 312112c7 | 2014-04-14 01:45:43 | [diff] [blame] | 298 | var scrollbarWidth = needsScrollbars.vertical ? this.scrollbarWidth_ : 0; |
| 299 | var scrollbarHeight = needsScrollbars.horizontal ? this.scrollbarWidth_ : 0; |
| 300 | return { |
| 301 | width: this.window_.innerWidth - scrollbarWidth, |
| 302 | height: this.window_.innerHeight - scrollbarHeight |
| 303 | }; |
| 304 | }, |
| 305 | |
| 306 | /** |
| 307 | * @type {number} the zoom level of the viewport. |
| 308 | */ |
| 309 | get zoom() { |
mcnee | 2999413a | 2016-12-06 20:29:25 | [diff] [blame] | 310 | return this.zoomManager_.applyBrowserZoom(this.internalZoom_); |
| 311 | }, |
| 312 | |
| 313 | /** |
| 314 | * Set the zoom manager. |
Henrique Nakashima | d5de6d0d | 2018-04-13 18:02:42 | [diff] [blame] | 315 | * |
mcnee | 2999413a | 2016-12-06 20:29:25 | [diff] [blame] | 316 | * @type {ZoomManager} manager the zoom manager to set. |
| 317 | */ |
| 318 | set zoomManager(manager) { |
| 319 | this.zoomManager_ = manager; |
[email protected] | 312112c7 | 2014-04-14 01:45:43 | [diff] [blame] | 320 | }, |
| 321 | |
| 322 | /** |
mcnee | 6e1abbf | 2016-11-09 18:05:31 | [diff] [blame] | 323 | * @type {Viewport.PinchPhase} The phase of the current pinch gesture for |
| 324 | * the viewport. |
| 325 | */ |
| 326 | get pinchPhase() { |
| 327 | return this.pinchPhase_; |
| 328 | }, |
| 329 | |
| 330 | /** |
| 331 | * @type {Object} The panning caused by the current pinch gesture (as |
| 332 | * the deltas of the x and y coordinates). |
| 333 | */ |
| 334 | get pinchPanVector() { |
| 335 | return this.pinchPanVector_; |
| 336 | }, |
| 337 | |
| 338 | /** |
| 339 | * @type {Object} The coordinates of the center of the current pinch gesture. |
| 340 | */ |
| 341 | get pinchCenter() { |
| 342 | return this.pinchCenter_; |
| 343 | }, |
| 344 | |
| 345 | /** |
[email protected] | 499e956 | 2014-06-26 05:45:27 | [diff] [blame] | 346 | * Used to wrap a function that might perform zooming on the viewport. This is |
| 347 | * required so that we can notify the plugin that zooming is in progress |
| 348 | * so that while zooming is taking place it can stop reacting to scroll events |
| 349 | * from the viewport. This is to avoid flickering. |
Henrique Nakashima | d5de6d0d | 2018-04-13 18:02:42 | [diff] [blame] | 350 | * |
| 351 | * @param {function} f Function to wrap |
| 352 | * @private |
[email protected] | 499e956 | 2014-06-26 05:45:27 | [diff] [blame] | 353 | */ |
| 354 | mightZoom_: function(f) { |
| 355 | this.beforeZoomCallback_(); |
| 356 | this.allowedToChangeZoom_ = true; |
| 357 | f(); |
| 358 | this.allowedToChangeZoom_ = false; |
| 359 | this.afterZoomCallback_(); |
| 360 | }, |
| 361 | |
| 362 | /** |
[email protected] | 312112c7 | 2014-04-14 01:45:43 | [diff] [blame] | 363 | * Sets the zoom of the viewport. |
Henrique Nakashima | d5de6d0d | 2018-04-13 18:02:42 | [diff] [blame] | 364 | * |
[email protected] | 312112c7 | 2014-04-14 01:45:43 | [diff] [blame] | 365 | * @param {number} newZoom the zoom level to zoom to. |
Henrique Nakashima | d5de6d0d | 2018-04-13 18:02:42 | [diff] [blame] | 366 | * @private |
[email protected] | 312112c7 | 2014-04-14 01:45:43 | [diff] [blame] | 367 | */ |
[email protected] | fbad5bb | 2014-07-18 07:20:36 | [diff] [blame] | 368 | setZoomInternal_: function(newZoom) { |
| 369 | if (!this.allowedToChangeZoom_) { |
| 370 | throw 'Called Viewport.setZoomInternal_ without calling ' + |
dbeam | 70db0fb | 2017-06-19 17:09:27 | [diff] [blame] | 371 | 'Viewport.mightZoom_.'; |
[email protected] | fbad5bb | 2014-07-18 07:20:36 | [diff] [blame] | 372 | } |
sammc | 5c33b7e | 2014-11-07 04:03:09 | [diff] [blame] | 373 | // Record the scroll position (relative to the top-left of the window). |
raymes | bd82a94 | 2015-08-05 07:05:18 | [diff] [blame] | 374 | var currentScrollPos = { |
mcnee | 2999413a | 2016-12-06 20:29:25 | [diff] [blame] | 375 | x: this.position.x / this.zoom, |
| 376 | y: this.position.y / this.zoom |
raymes | bd82a94 | 2015-08-05 07:05:18 | [diff] [blame] | 377 | }; |
Henrique Nakashima | 4cf9ed4 | 2018-09-07 21:02:03 | [diff] [blame^] | 378 | |
mcnee | 2999413a | 2016-12-06 20:29:25 | [diff] [blame] | 379 | this.internalZoom_ = newZoom; |
[email protected] | 3528d630 | 2014-02-19 08:13:07 | [diff] [blame] | 380 | this.contentSizeChanged_(); |
| 381 | // Scroll to the scaled scroll position. |
raymes | bd82a94 | 2015-08-05 07:05:18 | [diff] [blame] | 382 | this.position = { |
mcnee | 2999413a | 2016-12-06 20:29:25 | [diff] [blame] | 383 | x: currentScrollPos.x * this.zoom, |
| 384 | y: currentScrollPos.y * this.zoom |
raymes | bd82a94 | 2015-08-05 07:05:18 | [diff] [blame] | 385 | }; |
[email protected] | 3528d630 | 2014-02-19 08:13:07 | [diff] [blame] | 386 | }, |
| 387 | |
| 388 | /** |
mcnee | 6e1abbf | 2016-11-09 18:05:31 | [diff] [blame] | 389 | * Sets the zoom of the viewport. |
| 390 | * Same as setZoomInternal_ but for pinch zoom we have some more operations. |
Henrique Nakashima | d5de6d0d | 2018-04-13 18:02:42 | [diff] [blame] | 391 | * |
mcnee | 6e1abbf | 2016-11-09 18:05:31 | [diff] [blame] | 392 | * @param {number} scaleDelta The zoom delta. |
| 393 | * @param {!Object} center The pinch center in content coordinates. |
Henrique Nakashima | d5de6d0d | 2018-04-13 18:02:42 | [diff] [blame] | 394 | * @private |
mcnee | 6e1abbf | 2016-11-09 18:05:31 | [diff] [blame] | 395 | */ |
| 396 | setPinchZoomInternal_: function(scaleDelta, center) { |
dbeam | 70db0fb | 2017-06-19 17:09:27 | [diff] [blame] | 397 | assert( |
| 398 | this.allowedToChangeZoom_, |
mcnee | 6e1abbf | 2016-11-09 18:05:31 | [diff] [blame] | 399 | 'Called Viewport.setPinchZoomInternal_ without calling ' + |
dbeam | 70db0fb | 2017-06-19 17:09:27 | [diff] [blame] | 400 | 'Viewport.mightZoom_.'); |
Kevin McNee | 4e78edc | 2017-10-26 19:55:54 | [diff] [blame] | 401 | this.internalZoom_ = Viewport.clampZoom(this.internalZoom_ * scaleDelta); |
mcnee | 6e1abbf | 2016-11-09 18:05:31 | [diff] [blame] | 402 | |
| 403 | var newCenterInContent = this.frameToContent(center); |
| 404 | var delta = { |
| 405 | x: (newCenterInContent.x - this.oldCenterInContent.x), |
| 406 | y: (newCenterInContent.y - this.oldCenterInContent.y) |
| 407 | }; |
| 408 | |
| 409 | // Record the scroll position (relative to the pinch center). |
| 410 | var currentScrollPos = { |
mcnee | 2999413a | 2016-12-06 20:29:25 | [diff] [blame] | 411 | x: this.position.x - delta.x * this.zoom, |
| 412 | y: this.position.y - delta.y * this.zoom |
mcnee | 6e1abbf | 2016-11-09 18:05:31 | [diff] [blame] | 413 | }; |
| 414 | |
| 415 | this.contentSizeChanged_(); |
| 416 | // Scroll to the scaled scroll position. |
dbeam | 70db0fb | 2017-06-19 17:09:27 | [diff] [blame] | 417 | this.position = {x: currentScrollPos.x, y: currentScrollPos.y}; |
mcnee | 6e1abbf | 2016-11-09 18:05:31 | [diff] [blame] | 418 | }, |
| 419 | |
| 420 | /** |
mcnee | 6e1abbf | 2016-11-09 18:05:31 | [diff] [blame] | 421 | * Converts a point from frame to content coordinates. |
Henrique Nakashima | d5de6d0d | 2018-04-13 18:02:42 | [diff] [blame] | 422 | * |
mcnee | 6e1abbf | 2016-11-09 18:05:31 | [diff] [blame] | 423 | * @param {!Object} framePoint The frame coordinates. |
| 424 | * @return {!Object} The content coordinates. |
Henrique Nakashima | d5de6d0d | 2018-04-13 18:02:42 | [diff] [blame] | 425 | * @private |
mcnee | 6e1abbf | 2016-11-09 18:05:31 | [diff] [blame] | 426 | */ |
| 427 | frameToContent: function(framePoint) { |
| 428 | // TODO(mcnee) Add a helper Point class to avoid duplicating operations |
| 429 | // on plain {x,y} objects. |
| 430 | return { |
mcnee | 2999413a | 2016-12-06 20:29:25 | [diff] [blame] | 431 | x: (framePoint.x + this.position.x) / this.zoom, |
| 432 | y: (framePoint.y + this.position.y) / this.zoom |
mcnee | 6e1abbf | 2016-11-09 18:05:31 | [diff] [blame] | 433 | }; |
| 434 | }, |
| 435 | |
| 436 | /** |
[email protected] | fbad5bb | 2014-07-18 07:20:36 | [diff] [blame] | 437 | * Sets the zoom to the given zoom level. |
Henrique Nakashima | d5de6d0d | 2018-04-13 18:02:42 | [diff] [blame] | 438 | * |
[email protected] | fbad5bb | 2014-07-18 07:20:36 | [diff] [blame] | 439 | * @param {number} newZoom the zoom level to zoom to. |
[email protected] | 499e956 | 2014-06-26 05:45:27 | [diff] [blame] | 440 | */ |
[email protected] | fbad5bb | 2014-07-18 07:20:36 | [diff] [blame] | 441 | setZoom: function(newZoom) { |
Henrique Nakashima | 50b18e0 | 2017-11-21 23:29:57 | [diff] [blame] | 442 | this.fittingType_ = FittingType.NONE; |
dpapad | 9afc280 | 2017-08-09 22:01:43 | [diff] [blame] | 443 | this.mightZoom_(() => { |
Kevin McNee | 4e78edc | 2017-10-26 19:55:54 | [diff] [blame] | 444 | this.setZoomInternal_(Viewport.clampZoom(newZoom)); |
[email protected] | fbad5bb | 2014-07-18 07:20:36 | [diff] [blame] | 445 | this.updateViewport_(); |
dpapad | 9afc280 | 2017-08-09 22:01:43 | [diff] [blame] | 446 | }); |
[email protected] | 499e956 | 2014-06-26 05:45:27 | [diff] [blame] | 447 | }, |
| 448 | |
| 449 | /** |
mcnee | 2999413a | 2016-12-06 20:29:25 | [diff] [blame] | 450 | * Gets notified of the browser zoom changing seperately from the |
| 451 | * internal zoom. |
Henrique Nakashima | d5de6d0d | 2018-04-13 18:02:42 | [diff] [blame] | 452 | * |
mcnee | 2999413a | 2016-12-06 20:29:25 | [diff] [blame] | 453 | * @param {number} oldBrowserZoom the previous value of the browser zoom. |
| 454 | */ |
| 455 | updateZoomFromBrowserChange: function(oldBrowserZoom) { |
dpapad | 9afc280 | 2017-08-09 22:01:43 | [diff] [blame] | 456 | this.mightZoom_(() => { |
mcnee | 2999413a | 2016-12-06 20:29:25 | [diff] [blame] | 457 | // Record the scroll position (relative to the top-left of the window). |
| 458 | var oldZoom = oldBrowserZoom * this.internalZoom_; |
| 459 | var currentScrollPos = { |
| 460 | x: this.position.x / oldZoom, |
| 461 | y: this.position.y / oldZoom |
| 462 | }; |
| 463 | this.contentSizeChanged_(); |
| 464 | // Scroll to the scaled scroll position. |
| 465 | this.position = { |
| 466 | x: currentScrollPos.x * this.zoom, |
| 467 | y: currentScrollPos.y * this.zoom |
| 468 | }; |
| 469 | this.updateViewport_(); |
dpapad | 9afc280 | 2017-08-09 22:01:43 | [diff] [blame] | 470 | }); |
mcnee | 2999413a | 2016-12-06 20:29:25 | [diff] [blame] | 471 | }, |
| 472 | |
| 473 | /** |
[email protected] | 312112c7 | 2014-04-14 01:45:43 | [diff] [blame] | 474 | * @type {number} the width of scrollbars in the viewport in pixels. |
[email protected] | 3528d630 | 2014-02-19 08:13:07 | [diff] [blame] | 475 | */ |
[email protected] | 312112c7 | 2014-04-14 01:45:43 | [diff] [blame] | 476 | get scrollbarWidth() { |
| 477 | return this.scrollbarWidth_; |
[email protected] | 3528d630 | 2014-02-19 08:13:07 | [diff] [blame] | 478 | }, |
| 479 | |
| 480 | /** |
Henrique Nakashima | 50b18e0 | 2017-11-21 23:29:57 | [diff] [blame] | 481 | * @type {FittingType} the fitting type the viewport is currently in. |
[email protected] | 3528d630 | 2014-02-19 08:13:07 | [diff] [blame] | 482 | */ |
[email protected] | 312112c7 | 2014-04-14 01:45:43 | [diff] [blame] | 483 | get fittingType() { |
| 484 | return this.fittingType_; |
[email protected] | 3528d630 | 2014-02-19 08:13:07 | [diff] [blame] | 485 | }, |
| 486 | |
| 487 | /** |
Henrique Nakashima | d5de6d0d | 2018-04-13 18:02:42 | [diff] [blame] | 488 | * Get the which page is at a given y position. |
| 489 | * |
dpapad | d2d6477 | 2017-05-19 02:30:38 | [diff] [blame] | 490 | * @param {number} y the y-coordinate to get the page at. |
| 491 | * @return {number} the index of a page overlapping the given y-coordinate. |
Henrique Nakashima | d5de6d0d | 2018-04-13 18:02:42 | [diff] [blame] | 492 | * @private |
[email protected] | 56b7e3c | 2014-02-20 04:31:24 | [diff] [blame] | 493 | */ |
| 494 | getPageAtY_: function(y) { |
| 495 | var min = 0; |
| 496 | var max = this.pageDimensions_.length - 1; |
| 497 | while (max >= min) { |
| 498 | var page = Math.floor(min + ((max - min) / 2)); |
[email protected] | 13df2a4 | 2014-02-27 03:50:41 | [diff] [blame] | 499 | // There might be a gap between the pages, in which case use the bottom |
| 500 | // of the previous page as the top for finding the page. |
| 501 | var top = 0; |
| 502 | if (page > 0) { |
| 503 | top = this.pageDimensions_[page - 1].y + |
| 504 | this.pageDimensions_[page - 1].height; |
| 505 | } |
dbeam | 70db0fb | 2017-06-19 17:09:27 | [diff] [blame] | 506 | var bottom = |
| 507 | this.pageDimensions_[page].y + this.pageDimensions_[page].height; |
[email protected] | 13df2a4 | 2014-02-27 03:50:41 | [diff] [blame] | 508 | |
[email protected] | 56b7e3c | 2014-02-20 04:31:24 | [diff] [blame] | 509 | if (top <= y && bottom > y) |
| 510 | return page; |
Lei Zhang | ddc0ef03 | 2017-11-22 02:14:24 | [diff] [blame] | 511 | |
| 512 | if (top > y) |
[email protected] | 56b7e3c | 2014-02-20 04:31:24 | [diff] [blame] | 513 | max = page - 1; |
| 514 | else |
| 515 | min = page + 1; |
| 516 | } |
| 517 | return 0; |
| 518 | }, |
| 519 | |
| 520 | /** |
sammc | d06823a96 | 2014-11-10 04:51:15 | [diff] [blame] | 521 | * Returns the page with the greatest proportion of its height in the current |
| 522 | * viewport. |
Henrique Nakashima | d5de6d0d | 2018-04-13 18:02:42 | [diff] [blame] | 523 | * |
dpapad | d2d6477 | 2017-05-19 02:30:38 | [diff] [blame] | 524 | * @return {number} the index of the most visible page. |
[email protected] | 3528d630 | 2014-02-19 08:13:07 | [diff] [blame] | 525 | */ |
| 526 | getMostVisiblePage: function() { |
mcnee | 2999413a | 2016-12-06 20:29:25 | [diff] [blame] | 527 | var firstVisiblePage = this.getPageAtY_(this.position.y / this.zoom); |
sammc | d06823a96 | 2014-11-10 04:51:15 | [diff] [blame] | 528 | if (firstVisiblePage == this.pageDimensions_.length - 1) |
| 529 | return firstVisiblePage; |
| 530 | |
[email protected] | 345e7c6 | 2014-05-02 09:52:58 | [diff] [blame] | 531 | var viewportRect = { |
mcnee | 2999413a | 2016-12-06 20:29:25 | [diff] [blame] | 532 | x: this.position.x / this.zoom, |
| 533 | y: this.position.y / this.zoom, |
| 534 | width: this.size.width / this.zoom, |
| 535 | height: this.size.height / this.zoom |
[email protected] | 345e7c6 | 2014-05-02 09:52:58 | [diff] [blame] | 536 | }; |
dbeam | 70db0fb | 2017-06-19 17:09:27 | [diff] [blame] | 537 | var firstVisiblePageVisibility = |
| 538 | getIntersectionHeight( |
| 539 | this.pageDimensions_[firstVisiblePage], viewportRect) / |
sammc | d06823a96 | 2014-11-10 04:51:15 | [diff] [blame] | 540 | this.pageDimensions_[firstVisiblePage].height; |
dbeam | 70db0fb | 2017-06-19 17:09:27 | [diff] [blame] | 541 | var nextPageVisibility = |
| 542 | getIntersectionHeight( |
| 543 | this.pageDimensions_[firstVisiblePage + 1], viewportRect) / |
sammc | d06823a96 | 2014-11-10 04:51:15 | [diff] [blame] | 544 | this.pageDimensions_[firstVisiblePage + 1].height; |
| 545 | if (nextPageVisibility > firstVisiblePageVisibility) |
| 546 | return firstVisiblePage + 1; |
| 547 | return firstVisiblePage; |
[email protected] | 3528d630 | 2014-02-19 08:13:07 | [diff] [blame] | 548 | }, |
| 549 | |
| 550 | /** |
Henrique Nakashima | 50b18e0 | 2017-11-21 23:29:57 | [diff] [blame] | 551 | * Compute the zoom level for fit-to-page, fit-to-width or fit-to-height. |
| 552 | * |
| 553 | * At least one of {fitWidth, fitHeight} must be true. |
| 554 | * |
| 555 | * @param {Object} pageDimensions the dimensions of a given page in px. |
| 556 | * @param {boolean} fitWidth a bool indicating whether the whole width of the |
| 557 | * page needs to be in the viewport. |
| 558 | * @param {boolean} fitHeight a bool indicating whether the whole height of |
| 559 | * the page needs to be in the viewport. |
mcnee | 2999413a | 2016-12-06 20:29:25 | [diff] [blame] | 560 | * @return {number} the internal zoom to set |
Henrique Nakashima | d5de6d0d | 2018-04-13 18:02:42 | [diff] [blame] | 561 | * @private |
[email protected] | 3528d630 | 2014-02-19 08:13:07 | [diff] [blame] | 562 | */ |
Henrique Nakashima | 50b18e0 | 2017-11-21 23:29:57 | [diff] [blame] | 563 | computeFittingZoom_: function(pageDimensions, fitWidth, fitHeight) { |
| 564 | assert( |
| 565 | fitWidth || fitHeight, |
| 566 | 'Invalid parameters. At least one of fitWidth and fitHeight must be ' + |
| 567 | 'true.'); |
| 568 | |
[email protected] | 3528d630 | 2014-02-19 08:13:07 | [diff] [blame] | 569 | // First compute the zoom without scrollbars. |
Henrique Nakashima | 50b18e0 | 2017-11-21 23:29:57 | [diff] [blame] | 570 | var zoom = this.computeFittingZoomGivenDimensions_( |
| 571 | fitWidth, fitHeight, this.window_.innerWidth, this.window_.innerHeight, |
| 572 | pageDimensions.width, pageDimensions.height); |
| 573 | |
[email protected] | 3528d630 | 2014-02-19 08:13:07 | [diff] [blame] | 574 | // Check if there needs to be any scrollbars. |
| 575 | var needsScrollbars = this.documentNeedsScrollbars_(zoom); |
| 576 | |
| 577 | // If the document fits, just return the zoom. |
[email protected] | 312112c7 | 2014-04-14 01:45:43 | [diff] [blame] | 578 | if (!needsScrollbars.horizontal && !needsScrollbars.vertical) |
[email protected] | 3528d630 | 2014-02-19 08:13:07 | [diff] [blame] | 579 | return zoom; |
| 580 | |
raymes | e6e90c6 | 2015-08-10 06:21:40 | [diff] [blame] | 581 | var zoomedDimensions = this.getZoomedDocumentDimensions_(zoom); |
[email protected] | 3528d630 | 2014-02-19 08:13:07 | [diff] [blame] | 582 | |
| 583 | // Check if adding a scrollbar will result in needing the other scrollbar. |
| 584 | var scrollbarWidth = this.scrollbarWidth_; |
[email protected] | 312112c7 | 2014-04-14 01:45:43 | [diff] [blame] | 585 | if (needsScrollbars.horizontal && |
[email protected] | 3528d630 | 2014-02-19 08:13:07 | [diff] [blame] | 586 | zoomedDimensions.height > this.window_.innerHeight - scrollbarWidth) { |
[email protected] | 312112c7 | 2014-04-14 01:45:43 | [diff] [blame] | 587 | needsScrollbars.vertical = true; |
[email protected] | 3528d630 | 2014-02-19 08:13:07 | [diff] [blame] | 588 | } |
[email protected] | 312112c7 | 2014-04-14 01:45:43 | [diff] [blame] | 589 | if (needsScrollbars.vertical && |
[email protected] | 3528d630 | 2014-02-19 08:13:07 | [diff] [blame] | 590 | zoomedDimensions.width > this.window_.innerWidth - scrollbarWidth) { |
[email protected] | 312112c7 | 2014-04-14 01:45:43 | [diff] [blame] | 591 | needsScrollbars.horizontal = true; |
[email protected] | 3528d630 | 2014-02-19 08:13:07 | [diff] [blame] | 592 | } |
| 593 | |
| 594 | // Compute available window space. |
| 595 | var windowWithScrollbars = { |
| 596 | width: this.window_.innerWidth, |
| 597 | height: this.window_.innerHeight |
| 598 | }; |
[email protected] | 312112c7 | 2014-04-14 01:45:43 | [diff] [blame] | 599 | if (needsScrollbars.horizontal) |
[email protected] | 3528d630 | 2014-02-19 08:13:07 | [diff] [blame] | 600 | windowWithScrollbars.height -= scrollbarWidth; |
[email protected] | 312112c7 | 2014-04-14 01:45:43 | [diff] [blame] | 601 | if (needsScrollbars.vertical) |
[email protected] | 3528d630 | 2014-02-19 08:13:07 | [diff] [blame] | 602 | windowWithScrollbars.width -= scrollbarWidth; |
| 603 | |
| 604 | // Recompute the zoom. |
Henrique Nakashima | 50b18e0 | 2017-11-21 23:29:57 | [diff] [blame] | 605 | zoom = this.computeFittingZoomGivenDimensions_( |
| 606 | fitWidth, fitHeight, windowWithScrollbars.width, |
| 607 | windowWithScrollbars.height, pageDimensions.width, |
| 608 | pageDimensions.height); |
| 609 | |
mcnee | 2999413a | 2016-12-06 20:29:25 | [diff] [blame] | 610 | return this.zoomManager_.internalZoomComponent(zoom); |
[email protected] | 3528d630 | 2014-02-19 08:13:07 | [diff] [blame] | 611 | }, |
| 612 | |
| 613 | /** |
Henrique Nakashima | 50b18e0 | 2017-11-21 23:29:57 | [diff] [blame] | 614 | * Compute a zoom level given the dimensions to fit and the actual numbers |
| 615 | * in those dimensions. |
| 616 | * |
| 617 | * @param {boolean} fitWidth make sure the page width is totally contained in |
| 618 | * the window. |
| 619 | * @param {boolean} fitHeight make sure the page height is totally contained |
| 620 | * in the window. |
| 621 | * @param {number} windowWidth the width of the window in px. |
| 622 | * @param {number} windowHeight the height of the window in px. |
| 623 | * @param {number} pageWidth the width of the page in px. |
| 624 | * @param {number} pageHeight the height of the page in px. |
| 625 | * @return {number} the internal zoom to set |
Henrique Nakashima | d5de6d0d | 2018-04-13 18:02:42 | [diff] [blame] | 626 | * @private |
Henrique Nakashima | 50b18e0 | 2017-11-21 23:29:57 | [diff] [blame] | 627 | */ |
| 628 | computeFittingZoomGivenDimensions_: function( |
| 629 | fitWidth, fitHeight, windowWidth, windowHeight, pageWidth, pageHeight) { |
| 630 | // Assumes at least one of {fitWidth, fitHeight} is set. |
| 631 | var zoomWidth; |
| 632 | var zoomHeight; |
| 633 | |
| 634 | if (fitWidth) |
| 635 | zoomWidth = windowWidth / pageWidth; |
| 636 | |
| 637 | if (fitHeight) |
| 638 | zoomHeight = windowHeight / pageHeight; |
| 639 | |
Henrique Nakashima | 4cf9ed4 | 2018-09-07 21:02:03 | [diff] [blame^] | 640 | var zoom; |
| 641 | if (!fitWidth && fitHeight) { |
| 642 | zoom = zoomHeight; |
| 643 | } else if (fitWidth && !fitHeight) { |
| 644 | zoom = zoomWidth; |
| 645 | } else { |
| 646 | // Assume fitWidth && fitHeight |
| 647 | zoom = Math.min(zoomWidth, zoomHeight); |
| 648 | } |
Henrique Nakashima | 50b18e0 | 2017-11-21 23:29:57 | [diff] [blame] | 649 | |
Henrique Nakashima | 4cf9ed4 | 2018-09-07 21:02:03 | [diff] [blame^] | 650 | return Math.max(zoom, 0); |
Henrique Nakashima | 50b18e0 | 2017-11-21 23:29:57 | [diff] [blame] | 651 | }, |
| 652 | |
| 653 | /** |
| 654 | * Zoom the viewport so that the page width consumes the entire viewport. |
[email protected] | 3528d630 | 2014-02-19 08:13:07 | [diff] [blame] | 655 | */ |
| 656 | fitToWidth: function() { |
dpapad | 9afc280 | 2017-08-09 22:01:43 | [diff] [blame] | 657 | this.mightZoom_(() => { |
Henrique Nakashima | 50b18e0 | 2017-11-21 23:29:57 | [diff] [blame] | 658 | this.fittingType_ = FittingType.FIT_TO_WIDTH; |
[email protected] | 499e956 | 2014-06-26 05:45:27 | [diff] [blame] | 659 | if (!this.documentDimensions_) |
| 660 | return; |
[email protected] | 499e956 | 2014-06-26 05:45:27 | [diff] [blame] | 661 | // When computing fit-to-width, the maximum width of a page in the |
| 662 | // document is used, which is equal to the size of the document width. |
dbeam | 70db0fb | 2017-06-19 17:09:27 | [diff] [blame] | 663 | this.setZoomInternal_( |
Henrique Nakashima | 50b18e0 | 2017-11-21 23:29:57 | [diff] [blame] | 664 | this.computeFittingZoom_(this.documentDimensions_, true, false)); |
[email protected] | 499e956 | 2014-06-26 05:45:27 | [diff] [blame] | 665 | this.updateViewport_(); |
dpapad | 9afc280 | 2017-08-09 22:01:43 | [diff] [blame] | 666 | }); |
[email protected] | 3528d630 | 2014-02-19 08:13:07 | [diff] [blame] | 667 | }, |
| 668 | |
| 669 | /** |
Henrique Nakashima | 50b18e0 | 2017-11-21 23:29:57 | [diff] [blame] | 670 | * Zoom the viewport so that the page height consumes the entire viewport. |
Henrique Nakashima | d5de6d0d | 2018-04-13 18:02:42 | [diff] [blame] | 671 | * |
Henrique Nakashima | 50b18e0 | 2017-11-21 23:29:57 | [diff] [blame] | 672 | * @param {boolean} scrollToTopOfPage Set to true if the viewport should be |
| 673 | * scrolled to the top of the current page. Set to false if the viewport |
| 674 | * should remain at the current scroll position. |
Henrique Nakashima | d5de6d0d | 2018-04-13 18:02:42 | [diff] [blame] | 675 | * @private |
Henrique Nakashima | 50b18e0 | 2017-11-21 23:29:57 | [diff] [blame] | 676 | */ |
| 677 | fitToHeightInternal_: function(scrollToTopOfPage) { |
| 678 | this.mightZoom_(() => { |
| 679 | this.fittingType_ = FittingType.FIT_TO_HEIGHT; |
| 680 | if (!this.documentDimensions_) |
| 681 | return; |
| 682 | var page = this.getMostVisiblePage(); |
| 683 | // When computing fit-to-height, the maximum height of the current page |
| 684 | // is used. |
| 685 | var dimensions = { |
| 686 | width: 0, |
| 687 | height: this.pageDimensions_[page].height, |
| 688 | }; |
| 689 | this.setZoomInternal_(this.computeFittingZoom_(dimensions, false, true)); |
| 690 | if (scrollToTopOfPage) { |
| 691 | this.position = {x: 0, y: this.pageDimensions_[page].y * this.zoom}; |
| 692 | } |
| 693 | this.updateViewport_(); |
| 694 | }); |
| 695 | }, |
| 696 | |
| 697 | /** |
| 698 | * Zoom the viewport so that the page height consumes the entire viewport. |
| 699 | */ |
| 700 | fitToHeight: function() { |
| 701 | this.fitToHeightInternal_(true); |
| 702 | }, |
| 703 | |
| 704 | /** |
Henrique Nakashima | 50b18e0 | 2017-11-21 23:29:57 | [diff] [blame] | 705 | * Zoom the viewport so that a page consumes as much as possible of the it. |
Henrique Nakashima | d5de6d0d | 2018-04-13 18:02:42 | [diff] [blame] | 706 | * |
raymes | 161514f | 2015-02-17 05:09:51 | [diff] [blame] | 707 | * @param {boolean} scrollToTopOfPage Set to true if the viewport should be |
| 708 | * scrolled to the top of the current page. Set to false if the viewport |
| 709 | * should remain at the current scroll position. |
Henrique Nakashima | d5de6d0d | 2018-04-13 18:02:42 | [diff] [blame] | 710 | * @private |
[email protected] | 3528d630 | 2014-02-19 08:13:07 | [diff] [blame] | 711 | */ |
raymes | 161514f | 2015-02-17 05:09:51 | [diff] [blame] | 712 | fitToPageInternal_: function(scrollToTopOfPage) { |
dpapad | 9afc280 | 2017-08-09 22:01:43 | [diff] [blame] | 713 | this.mightZoom_(() => { |
Henrique Nakashima | 50b18e0 | 2017-11-21 23:29:57 | [diff] [blame] | 714 | this.fittingType_ = FittingType.FIT_TO_PAGE; |
[email protected] | 499e956 | 2014-06-26 05:45:27 | [diff] [blame] | 715 | if (!this.documentDimensions_) |
| 716 | return; |
| 717 | var page = this.getMostVisiblePage(); |
sammc | 07f53aa | 2014-11-06 06:57:46 | [diff] [blame] | 718 | // Fit to the current page's height and the widest page's width. |
| 719 | var dimensions = { |
| 720 | width: this.documentDimensions_.width, |
| 721 | height: this.pageDimensions_[page].height, |
| 722 | }; |
Henrique Nakashima | 50b18e0 | 2017-11-21 23:29:57 | [diff] [blame] | 723 | this.setZoomInternal_(this.computeFittingZoom_(dimensions, true, true)); |
raymes | bd82a94 | 2015-08-05 07:05:18 | [diff] [blame] | 724 | if (scrollToTopOfPage) { |
dbeam | 70db0fb | 2017-06-19 17:09:27 | [diff] [blame] | 725 | this.position = {x: 0, y: this.pageDimensions_[page].y * this.zoom}; |
raymes | bd82a94 | 2015-08-05 07:05:18 | [diff] [blame] | 726 | } |
[email protected] | 499e956 | 2014-06-26 05:45:27 | [diff] [blame] | 727 | this.updateViewport_(); |
dpapad | 9afc280 | 2017-08-09 22:01:43 | [diff] [blame] | 728 | }); |
[email protected] | 3528d630 | 2014-02-19 08:13:07 | [diff] [blame] | 729 | }, |
| 730 | |
| 731 | /** |
raymes | 161514f | 2015-02-17 05:09:51 | [diff] [blame] | 732 | * Zoom the viewport so that a page consumes the entire viewport. Also scrolls |
| 733 | * the viewport to the top of the current page. |
| 734 | */ |
| 735 | fitToPage: function() { |
| 736 | this.fitToPageInternal_(true); |
| 737 | }, |
| 738 | |
| 739 | /** |
Henrique Nakashima | 4cf9ed4 | 2018-09-07 21:02:03 | [diff] [blame^] | 740 | * Zoom the viewport to the default zoom policy. |
| 741 | */ |
| 742 | fitToNone: function() { |
| 743 | this.mightZoom_(() => { |
| 744 | this.fittingType_ = FittingType.NONE; |
| 745 | if (!this.documentDimensions_) |
| 746 | return; |
| 747 | this.setZoomInternal_(Math.min( |
| 748 | this.defaultZoom_, |
| 749 | this.computeFittingZoom_(this.documentDimensions_, true, false))); |
| 750 | this.updateViewport_(); |
| 751 | }); |
| 752 | }, |
| 753 | |
| 754 | /** |
[email protected] | 3528d630 | 2014-02-19 08:13:07 | [diff] [blame] | 755 | * Zoom out to the next predefined zoom level. |
| 756 | */ |
| 757 | zoomOut: function() { |
dpapad | 9afc280 | 2017-08-09 22:01:43 | [diff] [blame] | 758 | this.mightZoom_(() => { |
Henrique Nakashima | 50b18e0 | 2017-11-21 23:29:57 | [diff] [blame] | 759 | this.fittingType_ = FittingType.NONE; |
[email protected] | 499e956 | 2014-06-26 05:45:27 | [diff] [blame] | 760 | var nextZoom = Viewport.ZOOM_FACTORS[0]; |
| 761 | for (var i = 0; i < Viewport.ZOOM_FACTORS.length; i++) { |
mcnee | 2999413a | 2016-12-06 20:29:25 | [diff] [blame] | 762 | if (Viewport.ZOOM_FACTORS[i] < this.internalZoom_) |
[email protected] | 499e956 | 2014-06-26 05:45:27 | [diff] [blame] | 763 | nextZoom = Viewport.ZOOM_FACTORS[i]; |
| 764 | } |
[email protected] | fbad5bb | 2014-07-18 07:20:36 | [diff] [blame] | 765 | this.setZoomInternal_(nextZoom); |
[email protected] | 499e956 | 2014-06-26 05:45:27 | [diff] [blame] | 766 | this.updateViewport_(); |
dpapad | 9afc280 | 2017-08-09 22:01:43 | [diff] [blame] | 767 | }); |
[email protected] | 3528d630 | 2014-02-19 08:13:07 | [diff] [blame] | 768 | }, |
| 769 | |
| 770 | /** |
| 771 | * Zoom in to the next predefined zoom level. |
| 772 | */ |
| 773 | zoomIn: function() { |
dpapad | 9afc280 | 2017-08-09 22:01:43 | [diff] [blame] | 774 | this.mightZoom_(() => { |
Henrique Nakashima | 50b18e0 | 2017-11-21 23:29:57 | [diff] [blame] | 775 | this.fittingType_ = FittingType.NONE; |
[email protected] | 499e956 | 2014-06-26 05:45:27 | [diff] [blame] | 776 | var nextZoom = Viewport.ZOOM_FACTORS[Viewport.ZOOM_FACTORS.length - 1]; |
| 777 | for (var i = Viewport.ZOOM_FACTORS.length - 1; i >= 0; i--) { |
mcnee | 2999413a | 2016-12-06 20:29:25 | [diff] [blame] | 778 | if (Viewport.ZOOM_FACTORS[i] > this.internalZoom_) |
[email protected] | 499e956 | 2014-06-26 05:45:27 | [diff] [blame] | 779 | nextZoom = Viewport.ZOOM_FACTORS[i]; |
| 780 | } |
[email protected] | fbad5bb | 2014-07-18 07:20:36 | [diff] [blame] | 781 | this.setZoomInternal_(nextZoom); |
[email protected] | 499e956 | 2014-06-26 05:45:27 | [diff] [blame] | 782 | this.updateViewport_(); |
dpapad | 9afc280 | 2017-08-09 22:01:43 | [diff] [blame] | 783 | }); |
[email protected] | 3528d630 | 2014-02-19 08:13:07 | [diff] [blame] | 784 | }, |
| 785 | |
| 786 | /** |
mcnee | 6e1abbf | 2016-11-09 18:05:31 | [diff] [blame] | 787 | * Pinch zoom event handler. |
Henrique Nakashima | d5de6d0d | 2018-04-13 18:02:42 | [diff] [blame] | 788 | * |
mcnee | 6e1abbf | 2016-11-09 18:05:31 | [diff] [blame] | 789 | * @param {!Object} e The pinch event. |
| 790 | */ |
| 791 | pinchZoom: function(e) { |
dpapad | 9afc280 | 2017-08-09 22:01:43 | [diff] [blame] | 792 | this.mightZoom_(() => { |
mcnee | 6e1abbf | 2016-11-09 18:05:31 | [diff] [blame] | 793 | this.pinchPhase_ = e.direction == 'out' ? |
dbeam | 70db0fb | 2017-06-19 17:09:27 | [diff] [blame] | 794 | Viewport.PinchPhase.PINCH_UPDATE_ZOOM_OUT : |
| 795 | Viewport.PinchPhase.PINCH_UPDATE_ZOOM_IN; |
mcnee | 6e1abbf | 2016-11-09 18:05:31 | [diff] [blame] | 796 | |
| 797 | var scaleDelta = e.startScaleRatio / this.prevScale_; |
| 798 | this.pinchPanVector_ = |
| 799 | vectorDelta(e.center, this.firstPinchCenterInFrame_); |
| 800 | |
dbeam | 70db0fb | 2017-06-19 17:09:27 | [diff] [blame] | 801 | var needsScrollbars = |
| 802 | this.documentNeedsScrollbars_(this.zoomManager_.applyBrowserZoom( |
Kevin McNee | 4e78edc | 2017-10-26 19:55:54 | [diff] [blame] | 803 | Viewport.clampZoom(this.internalZoom_ * scaleDelta))); |
mcnee | 6e1abbf | 2016-11-09 18:05:31 | [diff] [blame] | 804 | |
| 805 | this.pinchCenter_ = e.center; |
| 806 | |
| 807 | // If there's no horizontal scrolling, keep the content centered so the |
| 808 | // user can't zoom in on the non-content area. |
| 809 | // TODO(mcnee) Investigate other ways of scaling when we don't have |
| 810 | // horizontal scrolling. We want to keep the document centered, |
| 811 | // but this causes a potentially awkward transition when we start |
| 812 | // using the gesture center. |
| 813 | if (!needsScrollbars.horizontal) { |
| 814 | this.pinchCenter_ = { |
| 815 | x: this.window_.innerWidth / 2, |
| 816 | y: this.window_.innerHeight / 2 |
| 817 | }; |
| 818 | } else if (this.keepContentCentered_) { |
| 819 | this.oldCenterInContent = |
| 820 | this.frameToContent(frameToPluginCoordinate(e.center)); |
| 821 | this.keepContentCentered_ = false; |
| 822 | } |
| 823 | |
dbeam | 70db0fb | 2017-06-19 17:09:27 | [diff] [blame] | 824 | this.setPinchZoomInternal_(scaleDelta, frameToPluginCoordinate(e.center)); |
mcnee | 6e1abbf | 2016-11-09 18:05:31 | [diff] [blame] | 825 | this.updateViewport_(); |
| 826 | this.prevScale_ = e.startScaleRatio; |
dpapad | 9afc280 | 2017-08-09 22:01:43 | [diff] [blame] | 827 | }); |
mcnee | 6e1abbf | 2016-11-09 18:05:31 | [diff] [blame] | 828 | }, |
| 829 | |
| 830 | pinchZoomStart: function(e) { |
| 831 | this.pinchPhase_ = Viewport.PinchPhase.PINCH_START; |
| 832 | this.prevScale_ = 1; |
| 833 | this.oldCenterInContent = |
| 834 | this.frameToContent(frameToPluginCoordinate(e.center)); |
| 835 | |
mcnee | 2999413a | 2016-12-06 20:29:25 | [diff] [blame] | 836 | var needsScrollbars = this.documentNeedsScrollbars_(this.zoom); |
mcnee | 6e1abbf | 2016-11-09 18:05:31 | [diff] [blame] | 837 | this.keepContentCentered_ = !needsScrollbars.horizontal; |
| 838 | // We keep track of begining of the pinch. |
| 839 | // By doing so we will be able to compute the pan distance. |
| 840 | this.firstPinchCenterInFrame_ = e.center; |
| 841 | }, |
| 842 | |
| 843 | pinchZoomEnd: function(e) { |
dpapad | 9afc280 | 2017-08-09 22:01:43 | [diff] [blame] | 844 | this.mightZoom_(() => { |
mcnee | 6e1abbf | 2016-11-09 18:05:31 | [diff] [blame] | 845 | this.pinchPhase_ = Viewport.PinchPhase.PINCH_END; |
| 846 | var scaleDelta = e.startScaleRatio / this.prevScale_; |
| 847 | this.pinchCenter_ = e.center; |
| 848 | |
dbeam | 70db0fb | 2017-06-19 17:09:27 | [diff] [blame] | 849 | this.setPinchZoomInternal_(scaleDelta, frameToPluginCoordinate(e.center)); |
mcnee | 6e1abbf | 2016-11-09 18:05:31 | [diff] [blame] | 850 | this.updateViewport_(); |
dpapad | 9afc280 | 2017-08-09 22:01:43 | [diff] [blame] | 851 | }); |
mcnee | 6e1abbf | 2016-11-09 18:05:31 | [diff] [blame] | 852 | |
| 853 | this.pinchPhase_ = Viewport.PinchPhase.PINCH_NONE; |
| 854 | this.pinchPanVector_ = null; |
| 855 | this.pinchCenter_ = null; |
| 856 | this.firstPinchCenterInFrame_ = null; |
| 857 | }, |
| 858 | |
| 859 | /** |
[email protected] | 3528d630 | 2014-02-19 08:13:07 | [diff] [blame] | 860 | * Go to the given page index. |
Henrique Nakashima | d5de6d0d | 2018-04-13 18:02:42 | [diff] [blame] | 861 | * |
[email protected] | f90b9a4 | 2014-08-20 05:37:34 | [diff] [blame] | 862 | * @param {number} page the index of the page to go to. zero-based. |
[email protected] | 3528d630 | 2014-02-19 08:13:07 | [diff] [blame] | 863 | */ |
| 864 | goToPage: function(page) { |
Henrique Nakashima | 85fc58b3 | 2017-12-11 21:53:42 | [diff] [blame] | 865 | this.goToPageAndXY(page, 0, 0); |
Henrique Nakashima | 9d9e063 | 2017-10-06 21:38:18 | [diff] [blame] | 866 | }, |
| 867 | |
| 868 | /** |
| 869 | * Go to the given y position in the given page index. |
Henrique Nakashima | d5de6d0d | 2018-04-13 18:02:42 | [diff] [blame] | 870 | * |
Henrique Nakashima | 9d9e063 | 2017-10-06 21:38:18 | [diff] [blame] | 871 | * @param {number} page the index of the page to go to. zero-based. |
Henrique Nakashima | 85fc58b3 | 2017-12-11 21:53:42 | [diff] [blame] | 872 | * @param {number} x the x position in the page to go to. |
Henrique Nakashima | 9d9e063 | 2017-10-06 21:38:18 | [diff] [blame] | 873 | * @param {number} y the y position in the page to go to. |
| 874 | */ |
Henrique Nakashima | 85fc58b3 | 2017-12-11 21:53:42 | [diff] [blame] | 875 | goToPageAndXY: function(page, x, y) { |
dpapad | 9afc280 | 2017-08-09 22:01:43 | [diff] [blame] | 876 | this.mightZoom_(() => { |
alexandrec | 9754b9e4 | 2015-01-19 03:41:19 | [diff] [blame] | 877 | if (this.pageDimensions_.length === 0) |
[email protected] | 499e956 | 2014-06-26 05:45:27 | [diff] [blame] | 878 | return; |
| 879 | if (page < 0) |
| 880 | page = 0; |
| 881 | if (page >= this.pageDimensions_.length) |
| 882 | page = this.pageDimensions_.length - 1; |
| 883 | var dimensions = this.pageDimensions_[page]; |
raymes | bd82a94 | 2015-08-05 07:05:18 | [diff] [blame] | 884 | var toolbarOffset = 0; |
Henrique Nakashima | 50b18e0 | 2017-11-21 23:29:57 | [diff] [blame] | 885 | // Unless we're in fit to page or fit to height mode, scroll above the |
| 886 | // page by |this.topToolbarHeight_| so that the toolbar isn't covering it |
raymes | bd82a94 | 2015-08-05 07:05:18 | [diff] [blame] | 887 | // initially. |
Henrique Nakashima | 50b18e0 | 2017-11-21 23:29:57 | [diff] [blame] | 888 | if (!this.isPagedMode()) |
raymes | bd82a94 | 2015-08-05 07:05:18 | [diff] [blame] | 889 | toolbarOffset = this.topToolbarHeight_; |
| 890 | this.position = { |
Henrique Nakashima | 85fc58b3 | 2017-12-11 21:53:42 | [diff] [blame] | 891 | x: (dimensions.x + x) * this.zoom, |
Henrique Nakashima | 9d9e063 | 2017-10-06 21:38:18 | [diff] [blame] | 892 | y: (dimensions.y + y) * this.zoom - toolbarOffset |
raymes | bd82a94 | 2015-08-05 07:05:18 | [diff] [blame] | 893 | }; |
[email protected] | 499e956 | 2014-06-26 05:45:27 | [diff] [blame] | 894 | this.updateViewport_(); |
dpapad | 9afc280 | 2017-08-09 22:01:43 | [diff] [blame] | 895 | }); |
[email protected] | 3528d630 | 2014-02-19 08:13:07 | [diff] [blame] | 896 | }, |
| 897 | |
| 898 | /** |
| 899 | * Set the dimensions of the document. |
Henrique Nakashima | d5de6d0d | 2018-04-13 18:02:42 | [diff] [blame] | 900 | * |
[email protected] | 3528d630 | 2014-02-19 08:13:07 | [diff] [blame] | 901 | * @param {Object} documentDimensions the dimensions of the document |
| 902 | */ |
| 903 | setDocumentDimensions: function(documentDimensions) { |
dpapad | 9afc280 | 2017-08-09 22:01:43 | [diff] [blame] | 904 | this.mightZoom_(() => { |
[email protected] | 499e956 | 2014-06-26 05:45:27 | [diff] [blame] | 905 | var initialDimensions = !this.documentDimensions_; |
| 906 | this.documentDimensions_ = documentDimensions; |
| 907 | this.pageDimensions_ = this.documentDimensions_.pageDimensions; |
| 908 | if (initialDimensions) { |
dbeam | 70db0fb | 2017-06-19 17:09:27 | [diff] [blame] | 909 | this.setZoomInternal_(Math.min( |
| 910 | this.defaultZoom_, |
Henrique Nakashima | 50b18e0 | 2017-11-21 23:29:57 | [diff] [blame] | 911 | this.computeFittingZoom_(this.documentDimensions_, true, false))); |
dbeam | 70db0fb | 2017-06-19 17:09:27 | [diff] [blame] | 912 | this.position = {x: 0, y: -this.topToolbarHeight_}; |
[email protected] | 499e956 | 2014-06-26 05:45:27 | [diff] [blame] | 913 | } |
| 914 | this.contentSizeChanged_(); |
| 915 | this.resize_(); |
dpapad | 9afc280 | 2017-08-09 22:01:43 | [diff] [blame] | 916 | }); |
[email protected] | 312112c7 | 2014-04-14 01:45:43 | [diff] [blame] | 917 | }, |
| 918 | |
| 919 | /** |
| 920 | * Get the coordinates of the page contents (excluding the page shadow) |
| 921 | * relative to the screen. |
Henrique Nakashima | d5de6d0d | 2018-04-13 18:02:42 | [diff] [blame] | 922 | * |
[email protected] | 312112c7 | 2014-04-14 01:45:43 | [diff] [blame] | 923 | * @param {number} page the index of the page to get the rect for. |
| 924 | * @return {Object} a rect representing the page in screen coordinates. |
| 925 | */ |
| 926 | getPageScreenRect: function(page) { |
[email protected] | 499e956 | 2014-06-26 05:45:27 | [diff] [blame] | 927 | if (!this.documentDimensions_) { |
dbeam | 70db0fb | 2017-06-19 17:09:27 | [diff] [blame] | 928 | return {x: 0, y: 0, width: 0, height: 0}; |
[email protected] | 499e956 | 2014-06-26 05:45:27 | [diff] [blame] | 929 | } |
[email protected] | 312112c7 | 2014-04-14 01:45:43 | [diff] [blame] | 930 | if (page >= this.pageDimensions_.length) |
| 931 | page = this.pageDimensions_.length - 1; |
| 932 | |
| 933 | var pageDimensions = this.pageDimensions_[page]; |
| 934 | |
| 935 | // Compute the page dimensions minus the shadows. |
| 936 | var insetDimensions = { |
| 937 | x: pageDimensions.x + Viewport.PAGE_SHADOW.left, |
| 938 | y: pageDimensions.y + Viewport.PAGE_SHADOW.top, |
| 939 | width: pageDimensions.width - Viewport.PAGE_SHADOW.left - |
| 940 | Viewport.PAGE_SHADOW.right, |
| 941 | height: pageDimensions.height - Viewport.PAGE_SHADOW.top - |
| 942 | Viewport.PAGE_SHADOW.bottom |
| 943 | }; |
| 944 | |
[email protected] | 345e7c6 | 2014-05-02 09:52:58 | [diff] [blame] | 945 | // Compute the x-coordinate of the page within the document. |
| 946 | // TODO(raymes): This should really be set when the PDF plugin passes the |
| 947 | // page coordinates, but it isn't yet. |
| 948 | var x = (this.documentDimensions_.width - pageDimensions.width) / 2 + |
| 949 | Viewport.PAGE_SHADOW.left; |
| 950 | // Compute the space on the left of the document if the document fits |
| 951 | // completely in the screen. |
dbeam | 70db0fb | 2017-06-19 17:09:27 | [diff] [blame] | 952 | var spaceOnLeft = |
| 953 | (this.size.width - this.documentDimensions_.width * this.zoom) / 2; |
[email protected] | 345e7c6 | 2014-05-02 09:52:58 | [diff] [blame] | 954 | spaceOnLeft = Math.max(spaceOnLeft, 0); |
[email protected] | 312112c7 | 2014-04-14 01:45:43 | [diff] [blame] | 955 | |
| 956 | return { |
mcnee | 2999413a | 2016-12-06 20:29:25 | [diff] [blame] | 957 | x: x * this.zoom + spaceOnLeft - this.window_.pageXOffset, |
| 958 | y: insetDimensions.y * this.zoom - this.window_.pageYOffset, |
| 959 | width: insetDimensions.width * this.zoom, |
| 960 | height: insetDimensions.height * this.zoom |
[email protected] | 312112c7 | 2014-04-14 01:45:43 | [diff] [blame] | 961 | }; |
Henrique Nakashima | 50b18e0 | 2017-11-21 23:29:57 | [diff] [blame] | 962 | }, |
| 963 | |
| 964 | /** |
| 965 | * Check if the current fitting type is a paged mode. |
| 966 | * |
| 967 | * In a paged mode, page up and page down scroll to the top of the |
| 968 | * previous/next page and part of the page is under the toolbar. |
| 969 | * |
| 970 | * @return {boolean} Whether the current fitting type is a paged mode. |
| 971 | */ |
| 972 | isPagedMode: function(page) { |
| 973 | return ( |
| 974 | this.fittingType_ == FittingType.FIT_TO_PAGE || |
| 975 | this.fittingType_ == FittingType.FIT_TO_HEIGHT); |
Henrique Nakashima | 585c7af0 | 2018-03-27 04:55:21 | [diff] [blame] | 976 | }, |
| 977 | |
| 978 | /** |
| 979 | * Scroll the viewport to the specified position. |
| 980 | * |
| 981 | * @param {!PartialPoint} point The position to which to move the viewport. |
| 982 | */ |
| 983 | scrollTo: function(point) { |
| 984 | let changed = false; |
| 985 | const newPosition = this.position; |
| 986 | if (point.x !== undefined && point.x != newPosition.x) { |
| 987 | newPosition.x = point.x; |
| 988 | changed = true; |
| 989 | } |
| 990 | if (point.y !== undefined && point.y != newPosition.y) { |
| 991 | newPosition.y = point.y; |
| 992 | changed = true; |
| 993 | } |
| 994 | |
| 995 | if (changed) |
| 996 | this.position = newPosition; |
| 997 | }, |
| 998 | |
| 999 | /** |
| 1000 | * Scroll the viewport by the specified delta. |
| 1001 | * |
| 1002 | * @param {!Point} delta The delta by which to move the viewport. |
| 1003 | */ |
| 1004 | scrollBy: function(delta) { |
| 1005 | const newPosition = this.position; |
| 1006 | newPosition.x += delta.x; |
| 1007 | newPosition.y += delta.y; |
| 1008 | this.scrollTo(newPosition); |
[email protected] | 3528d630 | 2014-02-19 08:13:07 | [diff] [blame] | 1009 | } |
| 1010 | }; |