blob: 18fb6c2ad403f4c8a49956f43a37bc776eaece6e [file] [log] [blame]
[email protected]3528d6302014-02-19 08:13:071// 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/**
sammcd06823a962014-11-10 04:51:156 * Returns the height of the intersection of two rectangles.
[email protected]3528d6302014-02-19 08:13:077 * @param {Object} rect1 the first rect
8 * @param {Object} rect2 the second rect
sammcd06823a962014-11-10 04:51:159 * @return {number} the height of the intersection of the rects
[email protected]3528d6302014-02-19 08:13:0710 */
sammcd06823a962014-11-10 04:51:1511function getIntersectionHeight(rect1, rect2) {
12 return Math.max(0,
[email protected]3528d6302014-02-19 08:13:0713 Math.min(rect1.y + rect1.height, rect2.y + rect2.height) -
14 Math.max(rect1.y, rect2.y));
[email protected]3528d6302014-02-19 08:13:0715}
16
17/**
[email protected]3528d6302014-02-19 08:13:0718 * Create a new viewport.
alexandrec9754b9e42015-01-19 03:41:1919 * @constructor
[email protected]3528d6302014-02-19 08:13:0720 * @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]3528d6302014-02-19 08:13:0723 * @param {Function} viewportChangedCallback is run when the viewport changes
[email protected]499e9562014-06-26 05:45:2724 * @param {Function} beforeZoomCallback is run before a change in zoom
25 * @param {Function} afterZoomCallback is run after a change in zoom
[email protected]345e7c62014-05-02 09:52:5826 * @param {number} scrollbarWidth the width of scrollbars on the page
sammc43d4982f2015-04-22 07:46:5127 * @param {number} defaultZoom The default zoom level.
raymesbd82a942015-08-05 07:05:1828 * @param {number} topToolbarHeight The number of pixels that should initially
29 * be left blank above the document for the toolbar.
[email protected]3528d6302014-02-19 08:13:0730 */
31function Viewport(window,
32 sizer,
[email protected]345e7c62014-05-02 09:52:5833 viewportChangedCallback,
[email protected]499e9562014-06-26 05:45:2734 beforeZoomCallback,
35 afterZoomCallback,
sammc43d4982f2015-04-22 07:46:5136 scrollbarWidth,
raymesbd82a942015-08-05 07:05:1837 defaultZoom,
38 topToolbarHeight) {
[email protected]3528d6302014-02-19 08:13:0739 this.window_ = window;
40 this.sizer_ = sizer;
[email protected]3528d6302014-02-19 08:13:0741 this.viewportChangedCallback_ = viewportChangedCallback;
[email protected]499e9562014-06-26 05:45:2742 this.beforeZoomCallback_ = beforeZoomCallback;
43 this.afterZoomCallback_ = afterZoomCallback;
44 this.allowedToChangeZoom_ = false;
[email protected]3528d6302014-02-19 08:13:0745 this.zoom_ = 1;
[email protected]312112c72014-04-14 01:45:4346 this.documentDimensions_ = null;
[email protected]3528d6302014-02-19 08:13:0747 this.pageDimensions_ = [];
[email protected]345e7c62014-05-02 09:52:5848 this.scrollbarWidth_ = scrollbarWidth;
[email protected]312112c72014-04-14 01:45:4349 this.fittingType_ = Viewport.FittingType.NONE;
sammc43d4982f2015-04-22 07:46:5150 this.defaultZoom_ = defaultZoom;
raymesbd82a942015-08-05 07:05:1851 this.topToolbarHeight_ = topToolbarHeight;
[email protected]3528d6302014-02-19 08:13:0752
53 window.addEventListener('scroll', this.updateViewport_.bind(this));
[email protected]312112c72014-04-14 01:45:4354 window.addEventListener('resize', this.resize_.bind(this));
[email protected]3528d6302014-02-19 08:13:0755}
56
[email protected]312112c72014-04-14 01:45:4357/**
58 * Enumeration of page fitting types.
59 * @enum {string}
60 */
61Viewport.FittingType = {
62 NONE: 'none',
63 FIT_TO_PAGE: 'fit-to-page',
64 FIT_TO_WIDTH: 'fit-to-width'
65};
66
67/**
[email protected]8dcaa262014-05-30 13:33:3768 * 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 */
73Viewport.SCROLL_INCREMENT = 40;
74
75/**
[email protected]312112c72014-04-14 01:45:4376 * Predefined zoom factors to be used when zooming in/out. These are in
bsep24e7ec42016-08-25 02:11:2077 * ascending order. This should match the lists in
78 * components/ui/zoom/page_zoom_constants.h and
79 * chrome/browser/resources/settings/appearance_page/appearance_page.js
[email protected]312112c72014-04-14 01:45:4380 */
bsep9924b842016-08-22 18:57:1481Viewport.ZOOM_FACTORS = [0.25, 1 / 3, 0.5, 2 / 3, 0.75, 0.8, 0.9,
82 1, 1.1, 1.25, 1.5, 1.75, 2, 2.5, 3, 4, 5];
[email protected]312112c72014-04-14 01:45:4383
84/**
[email protected]4271e16b2014-08-22 12:18:5985 * The minimum and maximum range to be used to clip zoom factor.
86 */
87Viewport.ZOOM_FACTOR_RANGE = {
88 min: Viewport.ZOOM_FACTORS[0],
89 max: Viewport.ZOOM_FACTORS[Viewport.ZOOM_FACTORS.length - 1]
90};
91
92/**
[email protected]312112c72014-04-14 01:45:4393 * The width of the page shadow around pages in pixels.
94 */
95Viewport.PAGE_SHADOW = {top: 3, bottom: 7, left: 5, right: 5};
96
[email protected]3528d6302014-02-19 08:13:0797Viewport.prototype = {
98 /**
raymese6e90c62015-08-10 06:21:4099 * Returns the zoomed and rounded document dimensions for the given zoom.
100 * Rounding is necessary when interacting with the renderer which tends to
101 * operate in integral values (for example for determining if scrollbars
102 * should be shown).
103 * @param {number} zoom The zoom to use to compute the scaled dimensions.
104 * @return {Object} A dictionary with scaled 'width'/'height' of the document.
105 * @private
106 */
107 getZoomedDocumentDimensions_: function(zoom) {
108 if (!this.documentDimensions_)
109 return null;
110 return {
111 width: Math.round(this.documentDimensions_.width * zoom),
112 height: Math.round(this.documentDimensions_.height * zoom)
113 };
114 },
115
116 /**
[email protected]3528d6302014-02-19 08:13:07117 * @private
118 * Returns true if the document needs scrollbars at the given zoom level.
119 * @param {number} zoom compute whether scrollbars are needed at this zoom
[email protected]312112c72014-04-14 01:45:43120 * @return {Object} with 'horizontal' and 'vertical' keys which map to bool
121 * values indicating if the horizontal and vertical scrollbars are needed
[email protected]3528d6302014-02-19 08:13:07122 * respectively.
123 */
124 documentNeedsScrollbars_: function(zoom) {
raymese6e90c62015-08-10 06:21:40125 var zoomedDimensions = this.getZoomedDocumentDimensions_(zoom);
126 if (!zoomedDimensions) {
[email protected]499e9562014-06-26 05:45:27127 return {
128 horizontal: false,
129 vertical: false
130 };
131 }
sammc63334fed2014-11-10 03:07:35132
133 // If scrollbars are required for one direction, expand the document in the
134 // other direction to take the width of the scrollbars into account when
135 // deciding whether the other direction needs scrollbars.
raymese6e90c62015-08-10 06:21:40136 if (zoomedDimensions.width > this.window_.innerWidth)
137 zoomedDimensions.height += this.scrollbarWidth_;
138 else if (zoomedDimensions.height > this.window_.innerHeight)
139 zoomedDimensions.width += this.scrollbarWidth_;
[email protected]3528d6302014-02-19 08:13:07140 return {
raymese6e90c62015-08-10 06:21:40141 horizontal: zoomedDimensions.width > this.window_.innerWidth,
raymes051fb2c2015-09-21 04:56:41142 vertical: zoomedDimensions.height + this.topToolbarHeight_ >
143 this.window_.innerHeight
[email protected]3528d6302014-02-19 08:13:07144 };
145 },
146
147 /**
148 * Returns true if the document needs scrollbars at the current zoom level.
149 * @return {Object} with 'x' and 'y' keys which map to bool values
150 * indicating if the horizontal and vertical scrollbars are needed
151 * respectively.
152 */
153 documentHasScrollbars: function() {
154 return this.documentNeedsScrollbars_(this.zoom_);
155 },
156
157 /**
158 * @private
159 * Helper function called when the zoomed document size changes.
160 */
161 contentSizeChanged_: function() {
raymese6e90c62015-08-10 06:21:40162 var zoomedDimensions = this.getZoomedDocumentDimensions_(this.zoom_);
163 if (zoomedDimensions) {
164 this.sizer_.style.width = zoomedDimensions.width + 'px';
165 this.sizer_.style.height = zoomedDimensions.height +
raymesbd82a942015-08-05 07:05:18166 this.topToolbarHeight_ + 'px';
[email protected]345e7c62014-05-02 09:52:58167 }
[email protected]3528d6302014-02-19 08:13:07168 },
169
170 /**
[email protected]312112c72014-04-14 01:45:43171 * @private
172 * Called when the viewport should be updated.
[email protected]3528d6302014-02-19 08:13:07173 */
[email protected]312112c72014-04-14 01:45:43174 updateViewport_: function() {
175 this.viewportChangedCallback_();
176 },
177
178 /**
179 * @private
180 * Called when the viewport size changes.
181 */
182 resize_: function() {
183 if (this.fittingType_ == Viewport.FittingType.FIT_TO_PAGE)
raymes161514f2015-02-17 05:09:51184 this.fitToPageInternal_(false);
[email protected]312112c72014-04-14 01:45:43185 else if (this.fittingType_ == Viewport.FittingType.FIT_TO_WIDTH)
186 this.fitToWidth();
187 else
188 this.updateViewport_();
189 },
190
191 /**
192 * @type {Object} the scroll position of the viewport.
193 */
194 get position() {
195 return {
196 x: this.window_.pageXOffset,
raymesbd82a942015-08-05 07:05:18197 y: this.window_.pageYOffset - this.topToolbarHeight_
[email protected]312112c72014-04-14 01:45:43198 };
199 },
200
201 /**
202 * Scroll the viewport to the specified position.
203 * @type {Object} position the position to scroll to.
204 */
205 set position(position) {
raymesbd82a942015-08-05 07:05:18206 this.window_.scrollTo(position.x, position.y + this.topToolbarHeight_);
[email protected]312112c72014-04-14 01:45:43207 },
208
209 /**
210 * @type {Object} the size of the viewport excluding scrollbars.
211 */
212 get size() {
213 var needsScrollbars = this.documentNeedsScrollbars_(this.zoom_);
214 var scrollbarWidth = needsScrollbars.vertical ? this.scrollbarWidth_ : 0;
215 var scrollbarHeight = needsScrollbars.horizontal ? this.scrollbarWidth_ : 0;
216 return {
217 width: this.window_.innerWidth - scrollbarWidth,
218 height: this.window_.innerHeight - scrollbarHeight
219 };
220 },
221
222 /**
223 * @type {number} the zoom level of the viewport.
224 */
225 get zoom() {
226 return this.zoom_;
227 },
228
229 /**
230 * @private
[email protected]499e9562014-06-26 05:45:27231 * Used to wrap a function that might perform zooming on the viewport. This is
232 * required so that we can notify the plugin that zooming is in progress
233 * so that while zooming is taking place it can stop reacting to scroll events
234 * from the viewport. This is to avoid flickering.
235 */
236 mightZoom_: function(f) {
237 this.beforeZoomCallback_();
238 this.allowedToChangeZoom_ = true;
239 f();
240 this.allowedToChangeZoom_ = false;
241 this.afterZoomCallback_();
242 },
243
244 /**
245 * @private
[email protected]312112c72014-04-14 01:45:43246 * Sets the zoom of the viewport.
247 * @param {number} newZoom the zoom level to zoom to.
248 */
[email protected]fbad5bb2014-07-18 07:20:36249 setZoomInternal_: function(newZoom) {
250 if (!this.allowedToChangeZoom_) {
251 throw 'Called Viewport.setZoomInternal_ without calling ' +
252 'Viewport.mightZoom_.';
253 }
sammc5c33b7e2014-11-07 04:03:09254 // Record the scroll position (relative to the top-left of the window).
raymesbd82a942015-08-05 07:05:18255 var currentScrollPos = {
256 x: this.position.x / this.zoom_,
257 y: this.position.y / this.zoom_
258 };
sammc5c33b7e2014-11-07 04:03:09259 this.zoom_ = newZoom;
[email protected]3528d6302014-02-19 08:13:07260 this.contentSizeChanged_();
261 // Scroll to the scaled scroll position.
raymesbd82a942015-08-05 07:05:18262 this.position = {
263 x: currentScrollPos.x * newZoom,
264 y: currentScrollPos.y * newZoom
265 };
[email protected]3528d6302014-02-19 08:13:07266 },
267
268 /**
[email protected]fbad5bb2014-07-18 07:20:36269 * Sets the zoom to the given zoom level.
270 * @param {number} newZoom the zoom level to zoom to.
[email protected]499e9562014-06-26 05:45:27271 */
[email protected]fbad5bb2014-07-18 07:20:36272 setZoom: function(newZoom) {
sammc5050705e2015-03-27 00:01:27273 this.fittingType_ = Viewport.FittingType.NONE;
[email protected]4271e16b2014-08-22 12:18:59274 newZoom = Math.max(Viewport.ZOOM_FACTOR_RANGE.min,
275 Math.min(newZoom, Viewport.ZOOM_FACTOR_RANGE.max));
[email protected]499e9562014-06-26 05:45:27276 this.mightZoom_(function() {
[email protected]fbad5bb2014-07-18 07:20:36277 this.setZoomInternal_(newZoom);
278 this.updateViewport_();
[email protected]499e9562014-06-26 05:45:27279 }.bind(this));
280 },
281
282 /**
[email protected]312112c72014-04-14 01:45:43283 * @type {number} the width of scrollbars in the viewport in pixels.
[email protected]3528d6302014-02-19 08:13:07284 */
[email protected]312112c72014-04-14 01:45:43285 get scrollbarWidth() {
286 return this.scrollbarWidth_;
[email protected]3528d6302014-02-19 08:13:07287 },
288
289 /**
[email protected]312112c72014-04-14 01:45:43290 * @type {Viewport.FittingType} the fitting type the viewport is currently in.
[email protected]3528d6302014-02-19 08:13:07291 */
[email protected]312112c72014-04-14 01:45:43292 get fittingType() {
293 return this.fittingType_;
[email protected]3528d6302014-02-19 08:13:07294 },
295
296 /**
[email protected]56b7e3c2014-02-20 04:31:24297 * @private
298 * @param {integer} y the y-coordinate to get the page at.
299 * @return {integer} the index of a page overlapping the given y-coordinate.
300 */
301 getPageAtY_: function(y) {
302 var min = 0;
303 var max = this.pageDimensions_.length - 1;
304 while (max >= min) {
305 var page = Math.floor(min + ((max - min) / 2));
[email protected]13df2a42014-02-27 03:50:41306 // There might be a gap between the pages, in which case use the bottom
307 // of the previous page as the top for finding the page.
308 var top = 0;
309 if (page > 0) {
310 top = this.pageDimensions_[page - 1].y +
311 this.pageDimensions_[page - 1].height;
312 }
313 var bottom = this.pageDimensions_[page].y +
314 this.pageDimensions_[page].height;
315
[email protected]56b7e3c2014-02-20 04:31:24316 if (top <= y && bottom > y)
317 return page;
318 else if (top > y)
319 max = page - 1;
320 else
321 min = page + 1;
322 }
323 return 0;
324 },
325
326 /**
sammcd06823a962014-11-10 04:51:15327 * Returns the page with the greatest proportion of its height in the current
328 * viewport.
[email protected]3528d6302014-02-19 08:13:07329 * @return {int} the index of the most visible page.
330 */
331 getMostVisiblePage: function() {
[email protected]312112c72014-04-14 01:45:43332 var firstVisiblePage = this.getPageAtY_(this.position.y / this.zoom_);
sammcd06823a962014-11-10 04:51:15333 if (firstVisiblePage == this.pageDimensions_.length - 1)
334 return firstVisiblePage;
335
[email protected]345e7c62014-05-02 09:52:58336 var viewportRect = {
337 x: this.position.x / this.zoom_,
338 y: this.position.y / this.zoom_,
339 width: this.size.width / this.zoom_,
340 height: this.size.height / this.zoom_
341 };
sammcd06823a962014-11-10 04:51:15342 var firstVisiblePageVisibility = getIntersectionHeight(
343 this.pageDimensions_[firstVisiblePage], viewportRect) /
344 this.pageDimensions_[firstVisiblePage].height;
345 var nextPageVisibility = getIntersectionHeight(
346 this.pageDimensions_[firstVisiblePage + 1], viewportRect) /
347 this.pageDimensions_[firstVisiblePage + 1].height;
348 if (nextPageVisibility > firstVisiblePageVisibility)
349 return firstVisiblePage + 1;
350 return firstVisiblePage;
[email protected]3528d6302014-02-19 08:13:07351 },
352
353 /**
354 * @private
355 * Compute the zoom level for fit-to-page or fit-to-width. |pageDimensions| is
356 * the dimensions for a given page and if |widthOnly| is true, it indicates
357 * that fit-to-page zoom should be computed rather than fit-to-page.
358 * @param {Object} pageDimensions the dimensions of a given page
359 * @param {boolean} widthOnly a bool indicating whether fit-to-page or
360 * fit-to-width should be computed.
361 * @return {number} the zoom to use
362 */
363 computeFittingZoom_: function(pageDimensions, widthOnly) {
364 // First compute the zoom without scrollbars.
365 var zoomWidth = this.window_.innerWidth / pageDimensions.width;
366 var zoom;
alexandrec9754b9e42015-01-19 03:41:19367 var zoomHeight;
[email protected]3528d6302014-02-19 08:13:07368 if (widthOnly) {
369 zoom = zoomWidth;
370 } else {
alexandrec9754b9e42015-01-19 03:41:19371 zoomHeight = this.window_.innerHeight / pageDimensions.height;
[email protected]3528d6302014-02-19 08:13:07372 zoom = Math.min(zoomWidth, zoomHeight);
373 }
374 // Check if there needs to be any scrollbars.
375 var needsScrollbars = this.documentNeedsScrollbars_(zoom);
376
377 // If the document fits, just return the zoom.
[email protected]312112c72014-04-14 01:45:43378 if (!needsScrollbars.horizontal && !needsScrollbars.vertical)
[email protected]3528d6302014-02-19 08:13:07379 return zoom;
380
raymese6e90c62015-08-10 06:21:40381 var zoomedDimensions = this.getZoomedDocumentDimensions_(zoom);
[email protected]3528d6302014-02-19 08:13:07382
383 // Check if adding a scrollbar will result in needing the other scrollbar.
384 var scrollbarWidth = this.scrollbarWidth_;
[email protected]312112c72014-04-14 01:45:43385 if (needsScrollbars.horizontal &&
[email protected]3528d6302014-02-19 08:13:07386 zoomedDimensions.height > this.window_.innerHeight - scrollbarWidth) {
[email protected]312112c72014-04-14 01:45:43387 needsScrollbars.vertical = true;
[email protected]3528d6302014-02-19 08:13:07388 }
[email protected]312112c72014-04-14 01:45:43389 if (needsScrollbars.vertical &&
[email protected]3528d6302014-02-19 08:13:07390 zoomedDimensions.width > this.window_.innerWidth - scrollbarWidth) {
[email protected]312112c72014-04-14 01:45:43391 needsScrollbars.horizontal = true;
[email protected]3528d6302014-02-19 08:13:07392 }
393
394 // Compute available window space.
395 var windowWithScrollbars = {
396 width: this.window_.innerWidth,
397 height: this.window_.innerHeight
398 };
[email protected]312112c72014-04-14 01:45:43399 if (needsScrollbars.horizontal)
[email protected]3528d6302014-02-19 08:13:07400 windowWithScrollbars.height -= scrollbarWidth;
[email protected]312112c72014-04-14 01:45:43401 if (needsScrollbars.vertical)
[email protected]3528d6302014-02-19 08:13:07402 windowWithScrollbars.width -= scrollbarWidth;
403
404 // Recompute the zoom.
405 zoomWidth = windowWithScrollbars.width / pageDimensions.width;
406 if (widthOnly) {
407 zoom = zoomWidth;
408 } else {
alexandrec9754b9e42015-01-19 03:41:19409 zoomHeight = windowWithScrollbars.height / pageDimensions.height;
[email protected]3528d6302014-02-19 08:13:07410 zoom = Math.min(zoomWidth, zoomHeight);
411 }
412 return zoom;
413 },
414
415 /**
416 * Zoom the viewport so that the page-width consumes the entire viewport.
417 */
418 fitToWidth: function() {
[email protected]499e9562014-06-26 05:45:27419 this.mightZoom_(function() {
420 this.fittingType_ = Viewport.FittingType.FIT_TO_WIDTH;
421 if (!this.documentDimensions_)
422 return;
[email protected]499e9562014-06-26 05:45:27423 // When computing fit-to-width, the maximum width of a page in the
424 // document is used, which is equal to the size of the document width.
[email protected]fbad5bb2014-07-18 07:20:36425 this.setZoomInternal_(this.computeFittingZoom_(this.documentDimensions_,
426 true));
[email protected]499e9562014-06-26 05:45:27427 var page = this.getMostVisiblePage();
[email protected]499e9562014-06-26 05:45:27428 this.updateViewport_();
429 }.bind(this));
[email protected]3528d6302014-02-19 08:13:07430 },
431
432 /**
raymes161514f2015-02-17 05:09:51433 * @private
434 * Zoom the viewport so that a page consumes the entire viewport.
435 * @param {boolean} scrollToTopOfPage Set to true if the viewport should be
436 * scrolled to the top of the current page. Set to false if the viewport
437 * should remain at the current scroll position.
[email protected]3528d6302014-02-19 08:13:07438 */
raymes161514f2015-02-17 05:09:51439 fitToPageInternal_: function(scrollToTopOfPage) {
[email protected]499e9562014-06-26 05:45:27440 this.mightZoom_(function() {
441 this.fittingType_ = Viewport.FittingType.FIT_TO_PAGE;
442 if (!this.documentDimensions_)
443 return;
444 var page = this.getMostVisiblePage();
sammc07f53aa2014-11-06 06:57:46445 // Fit to the current page's height and the widest page's width.
446 var dimensions = {
447 width: this.documentDimensions_.width,
448 height: this.pageDimensions_[page].height,
449 };
450 this.setZoomInternal_(this.computeFittingZoom_(dimensions, false));
raymesbd82a942015-08-05 07:05:18451 if (scrollToTopOfPage) {
452 this.position = {
453 x: 0,
454 y: this.pageDimensions_[page].y * this.zoom_
455 };
456 }
[email protected]499e9562014-06-26 05:45:27457 this.updateViewport_();
458 }.bind(this));
[email protected]3528d6302014-02-19 08:13:07459 },
460
461 /**
raymes161514f2015-02-17 05:09:51462 * Zoom the viewport so that a page consumes the entire viewport. Also scrolls
463 * the viewport to the top of the current page.
464 */
465 fitToPage: function() {
466 this.fitToPageInternal_(true);
467 },
468
469 /**
[email protected]3528d6302014-02-19 08:13:07470 * Zoom out to the next predefined zoom level.
471 */
472 zoomOut: function() {
[email protected]499e9562014-06-26 05:45:27473 this.mightZoom_(function() {
474 this.fittingType_ = Viewport.FittingType.NONE;
475 var nextZoom = Viewport.ZOOM_FACTORS[0];
476 for (var i = 0; i < Viewport.ZOOM_FACTORS.length; i++) {
477 if (Viewport.ZOOM_FACTORS[i] < this.zoom_)
478 nextZoom = Viewport.ZOOM_FACTORS[i];
479 }
[email protected]fbad5bb2014-07-18 07:20:36480 this.setZoomInternal_(nextZoom);
[email protected]499e9562014-06-26 05:45:27481 this.updateViewport_();
482 }.bind(this));
[email protected]3528d6302014-02-19 08:13:07483 },
484
485 /**
486 * Zoom in to the next predefined zoom level.
487 */
488 zoomIn: function() {
[email protected]499e9562014-06-26 05:45:27489 this.mightZoom_(function() {
490 this.fittingType_ = Viewport.FittingType.NONE;
491 var nextZoom = Viewport.ZOOM_FACTORS[Viewport.ZOOM_FACTORS.length - 1];
492 for (var i = Viewport.ZOOM_FACTORS.length - 1; i >= 0; i--) {
493 if (Viewport.ZOOM_FACTORS[i] > this.zoom_)
494 nextZoom = Viewport.ZOOM_FACTORS[i];
495 }
[email protected]fbad5bb2014-07-18 07:20:36496 this.setZoomInternal_(nextZoom);
[email protected]499e9562014-06-26 05:45:27497 this.updateViewport_();
498 }.bind(this));
[email protected]3528d6302014-02-19 08:13:07499 },
500
501 /**
502 * Go to the given page index.
[email protected]f90b9a42014-08-20 05:37:34503 * @param {number} page the index of the page to go to. zero-based.
[email protected]3528d6302014-02-19 08:13:07504 */
505 goToPage: function(page) {
[email protected]499e9562014-06-26 05:45:27506 this.mightZoom_(function() {
alexandrec9754b9e42015-01-19 03:41:19507 if (this.pageDimensions_.length === 0)
[email protected]499e9562014-06-26 05:45:27508 return;
509 if (page < 0)
510 page = 0;
511 if (page >= this.pageDimensions_.length)
512 page = this.pageDimensions_.length - 1;
513 var dimensions = this.pageDimensions_[page];
raymesbd82a942015-08-05 07:05:18514 var toolbarOffset = 0;
515 // Unless we're in fit to page mode, scroll above the page by
516 // |this.topToolbarHeight_| so that the toolbar isn't covering it
517 // initially.
518 if (this.fittingType_ != Viewport.FittingType.FIT_TO_PAGE)
519 toolbarOffset = this.topToolbarHeight_;
520 this.position = {
521 x: dimensions.x * this.zoom_,
522 y: dimensions.y * this.zoom_ - toolbarOffset
523 };
[email protected]499e9562014-06-26 05:45:27524 this.updateViewport_();
525 }.bind(this));
[email protected]3528d6302014-02-19 08:13:07526 },
527
528 /**
529 * Set the dimensions of the document.
530 * @param {Object} documentDimensions the dimensions of the document
531 */
532 setDocumentDimensions: function(documentDimensions) {
[email protected]499e9562014-06-26 05:45:27533 this.mightZoom_(function() {
534 var initialDimensions = !this.documentDimensions_;
535 this.documentDimensions_ = documentDimensions;
536 this.pageDimensions_ = this.documentDimensions_.pageDimensions;
537 if (initialDimensions) {
sammc43d4982f2015-04-22 07:46:51538 this.setZoomInternal_(
539 Math.min(this.defaultZoom_,
540 this.computeFittingZoom_(this.documentDimensions_, true)));
raymesbd82a942015-08-05 07:05:18541 this.position = {
542 x: 0,
543 y: -this.topToolbarHeight_
544 };
[email protected]499e9562014-06-26 05:45:27545 }
546 this.contentSizeChanged_();
547 this.resize_();
548 }.bind(this));
[email protected]312112c72014-04-14 01:45:43549 },
550
551 /**
552 * Get the coordinates of the page contents (excluding the page shadow)
553 * relative to the screen.
554 * @param {number} page the index of the page to get the rect for.
555 * @return {Object} a rect representing the page in screen coordinates.
556 */
557 getPageScreenRect: function(page) {
[email protected]499e9562014-06-26 05:45:27558 if (!this.documentDimensions_) {
559 return {
560 x: 0,
561 y: 0,
562 width: 0,
563 height: 0
564 };
565 }
[email protected]312112c72014-04-14 01:45:43566 if (page >= this.pageDimensions_.length)
567 page = this.pageDimensions_.length - 1;
568
569 var pageDimensions = this.pageDimensions_[page];
570
571 // Compute the page dimensions minus the shadows.
572 var insetDimensions = {
573 x: pageDimensions.x + Viewport.PAGE_SHADOW.left,
574 y: pageDimensions.y + Viewport.PAGE_SHADOW.top,
575 width: pageDimensions.width - Viewport.PAGE_SHADOW.left -
576 Viewport.PAGE_SHADOW.right,
577 height: pageDimensions.height - Viewport.PAGE_SHADOW.top -
578 Viewport.PAGE_SHADOW.bottom
579 };
580
[email protected]345e7c62014-05-02 09:52:58581 // Compute the x-coordinate of the page within the document.
582 // TODO(raymes): This should really be set when the PDF plugin passes the
583 // page coordinates, but it isn't yet.
584 var x = (this.documentDimensions_.width - pageDimensions.width) / 2 +
585 Viewport.PAGE_SHADOW.left;
586 // Compute the space on the left of the document if the document fits
587 // completely in the screen.
588 var spaceOnLeft = (this.size.width -
589 this.documentDimensions_.width * this.zoom_) / 2;
590 spaceOnLeft = Math.max(spaceOnLeft, 0);
[email protected]312112c72014-04-14 01:45:43591
592 return {
[email protected]345e7c62014-05-02 09:52:58593 x: x * this.zoom_ + spaceOnLeft - this.window_.pageXOffset,
594 y: insetDimensions.y * this.zoom_ - this.window_.pageYOffset,
[email protected]312112c72014-04-14 01:45:43595 width: insetDimensions.width * this.zoom_,
596 height: insetDimensions.height * this.zoom_
597 };
[email protected]3528d6302014-02-19 08:13:07598 }
599};