blob: 35a5f6590e49a84619c6c44d805e1b3f9c9b6f9f [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
[email protected]c90b3292014-08-20 00:57:4177 * ascending order. This should match the list in
wjmaclean91ddc472015-01-15 19:58:2778 * components/ui/zoom/page_zoom_constants.h
[email protected]312112c72014-04-14 01:45:4379 */
80Viewport.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]4271e16b2014-08-22 12:18:5984 * The minimum and maximum range to be used to clip zoom factor.
85 */
86Viewport.ZOOM_FACTOR_RANGE = {
87 min: Viewport.ZOOM_FACTORS[0],
88 max: Viewport.ZOOM_FACTORS[Viewport.ZOOM_FACTORS.length - 1]
89};
90
91/**
[email protected]312112c72014-04-14 01:45:4392 * The width of the page shadow around pages in pixels.
93 */
94Viewport.PAGE_SHADOW = {top: 3, bottom: 7, left: 5, right: 5};
95
[email protected]3528d6302014-02-19 08:13:0796Viewport.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]312112c72014-04-14 01:45:43101 * @return {Object} with 'horizontal' and 'vertical' keys which map to bool
102 * values indicating if the horizontal and vertical scrollbars are needed
[email protected]3528d6302014-02-19 08:13:07103 * respectively.
104 */
105 documentNeedsScrollbars_: function(zoom) {
[email protected]499e9562014-06-26 05:45:27106 if (!this.documentDimensions_) {
107 return {
108 horizontal: false,
109 vertical: false
110 };
111 }
[email protected]312112c72014-04-14 01:45:43112 var documentWidth = this.documentDimensions_.width * zoom;
113 var documentHeight = this.documentDimensions_.height * zoom;
sammc63334fed2014-11-10 03:07:35114
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]3528d6302014-02-19 08:13:07122 return {
[email protected]312112c72014-04-14 01:45:43123 horizontal: documentWidth > this.window_.innerWidth,
124 vertical: documentHeight > this.window_.innerHeight
[email protected]3528d6302014-02-19 08:13:07125 };
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]345e7c62014-05-02 09:52:58143 if (this.documentDimensions_) {
144 this.sizer_.style.width =
145 this.documentDimensions_.width * this.zoom_ + 'px';
raymesbd82a942015-08-05 07:05:18146 this.sizer_.style.height = this.documentDimensions_.height * this.zoom_ +
147 this.topToolbarHeight_ + 'px';
[email protected]345e7c62014-05-02 09:52:58148 }
[email protected]3528d6302014-02-19 08:13:07149 },
150
151 /**
[email protected]312112c72014-04-14 01:45:43152 * @private
153 * Called when the viewport should be updated.
[email protected]3528d6302014-02-19 08:13:07154 */
[email protected]312112c72014-04-14 01:45:43155 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)
raymes161514f2015-02-17 05:09:51165 this.fitToPageInternal_(false);
[email protected]312112c72014-04-14 01:45:43166 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,
raymesbd82a942015-08-05 07:05:18178 y: this.window_.pageYOffset - this.topToolbarHeight_
[email protected]312112c72014-04-14 01:45:43179 };
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) {
raymesbd82a942015-08-05 07:05:18187 this.window_.scrollTo(position.x, position.y + this.topToolbarHeight_);
[email protected]312112c72014-04-14 01:45:43188 },
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]499e9562014-06-26 05:45:27212 * 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]312112c72014-04-14 01:45:43227 * Sets the zoom of the viewport.
228 * @param {number} newZoom the zoom level to zoom to.
229 */
[email protected]fbad5bb2014-07-18 07:20:36230 setZoomInternal_: function(newZoom) {
231 if (!this.allowedToChangeZoom_) {
232 throw 'Called Viewport.setZoomInternal_ without calling ' +
233 'Viewport.mightZoom_.';
234 }
sammc5c33b7e2014-11-07 04:03:09235 // Record the scroll position (relative to the top-left of the window).
raymesbd82a942015-08-05 07:05:18236 var currentScrollPos = {
237 x: this.position.x / this.zoom_,
238 y: this.position.y / this.zoom_
239 };
sammc5c33b7e2014-11-07 04:03:09240 this.zoom_ = newZoom;
[email protected]3528d6302014-02-19 08:13:07241 this.contentSizeChanged_();
242 // Scroll to the scaled scroll position.
raymesbd82a942015-08-05 07:05:18243 this.position = {
244 x: currentScrollPos.x * newZoom,
245 y: currentScrollPos.y * newZoom
246 };
[email protected]3528d6302014-02-19 08:13:07247 },
248
249 /**
[email protected]fbad5bb2014-07-18 07:20:36250 * Sets the zoom to the given zoom level.
251 * @param {number} newZoom the zoom level to zoom to.
[email protected]499e9562014-06-26 05:45:27252 */
[email protected]fbad5bb2014-07-18 07:20:36253 setZoom: function(newZoom) {
sammc5050705e2015-03-27 00:01:27254 this.fittingType_ = Viewport.FittingType.NONE;
[email protected]4271e16b2014-08-22 12:18:59255 newZoom = Math.max(Viewport.ZOOM_FACTOR_RANGE.min,
256 Math.min(newZoom, Viewport.ZOOM_FACTOR_RANGE.max));
[email protected]499e9562014-06-26 05:45:27257 this.mightZoom_(function() {
[email protected]fbad5bb2014-07-18 07:20:36258 this.setZoomInternal_(newZoom);
259 this.updateViewport_();
[email protected]499e9562014-06-26 05:45:27260 }.bind(this));
261 },
262
263 /**
[email protected]312112c72014-04-14 01:45:43264 * @type {number} the width of scrollbars in the viewport in pixels.
[email protected]3528d6302014-02-19 08:13:07265 */
[email protected]312112c72014-04-14 01:45:43266 get scrollbarWidth() {
267 return this.scrollbarWidth_;
[email protected]3528d6302014-02-19 08:13:07268 },
269
270 /**
[email protected]312112c72014-04-14 01:45:43271 * @type {Viewport.FittingType} the fitting type the viewport is currently in.
[email protected]3528d6302014-02-19 08:13:07272 */
[email protected]312112c72014-04-14 01:45:43273 get fittingType() {
274 return this.fittingType_;
[email protected]3528d6302014-02-19 08:13:07275 },
276
277 /**
[email protected]56b7e3c2014-02-20 04:31:24278 * @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]13df2a42014-02-27 03:50:41287 // 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]56b7e3c2014-02-20 04:31:24297 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 /**
sammcd06823a962014-11-10 04:51:15308 * Returns the page with the greatest proportion of its height in the current
309 * viewport.
[email protected]3528d6302014-02-19 08:13:07310 * @return {int} the index of the most visible page.
311 */
312 getMostVisiblePage: function() {
[email protected]312112c72014-04-14 01:45:43313 var firstVisiblePage = this.getPageAtY_(this.position.y / this.zoom_);
sammcd06823a962014-11-10 04:51:15314 if (firstVisiblePage == this.pageDimensions_.length - 1)
315 return firstVisiblePage;
316
[email protected]345e7c62014-05-02 09:52:58317 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 };
sammcd06823a962014-11-10 04:51:15323 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]3528d6302014-02-19 08:13:07332 },
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;
alexandrec9754b9e42015-01-19 03:41:19348 var zoomHeight;
[email protected]3528d6302014-02-19 08:13:07349 if (widthOnly) {
350 zoom = zoomWidth;
351 } else {
alexandrec9754b9e42015-01-19 03:41:19352 zoomHeight = this.window_.innerHeight / pageDimensions.height;
[email protected]3528d6302014-02-19 08:13:07353 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]312112c72014-04-14 01:45:43359 if (!needsScrollbars.horizontal && !needsScrollbars.vertical)
[email protected]3528d6302014-02-19 08:13:07360 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]312112c72014-04-14 01:45:43369 if (needsScrollbars.horizontal &&
[email protected]3528d6302014-02-19 08:13:07370 zoomedDimensions.height > this.window_.innerHeight - scrollbarWidth) {
[email protected]312112c72014-04-14 01:45:43371 needsScrollbars.vertical = true;
[email protected]3528d6302014-02-19 08:13:07372 }
[email protected]312112c72014-04-14 01:45:43373 if (needsScrollbars.vertical &&
[email protected]3528d6302014-02-19 08:13:07374 zoomedDimensions.width > this.window_.innerWidth - scrollbarWidth) {
[email protected]312112c72014-04-14 01:45:43375 needsScrollbars.horizontal = true;
[email protected]3528d6302014-02-19 08:13:07376 }
377
378 // Compute available window space.
379 var windowWithScrollbars = {
380 width: this.window_.innerWidth,
381 height: this.window_.innerHeight
382 };
[email protected]312112c72014-04-14 01:45:43383 if (needsScrollbars.horizontal)
[email protected]3528d6302014-02-19 08:13:07384 windowWithScrollbars.height -= scrollbarWidth;
[email protected]312112c72014-04-14 01:45:43385 if (needsScrollbars.vertical)
[email protected]3528d6302014-02-19 08:13:07386 windowWithScrollbars.width -= scrollbarWidth;
387
388 // Recompute the zoom.
389 zoomWidth = windowWithScrollbars.width / pageDimensions.width;
390 if (widthOnly) {
391 zoom = zoomWidth;
392 } else {
alexandrec9754b9e42015-01-19 03:41:19393 zoomHeight = windowWithScrollbars.height / pageDimensions.height;
[email protected]3528d6302014-02-19 08:13:07394 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]499e9562014-06-26 05:45:27403 this.mightZoom_(function() {
404 this.fittingType_ = Viewport.FittingType.FIT_TO_WIDTH;
405 if (!this.documentDimensions_)
406 return;
[email protected]499e9562014-06-26 05:45:27407 // 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]fbad5bb2014-07-18 07:20:36409 this.setZoomInternal_(this.computeFittingZoom_(this.documentDimensions_,
410 true));
[email protected]499e9562014-06-26 05:45:27411 var page = this.getMostVisiblePage();
[email protected]499e9562014-06-26 05:45:27412 this.updateViewport_();
413 }.bind(this));
[email protected]3528d6302014-02-19 08:13:07414 },
415
416 /**
raymes161514f2015-02-17 05:09:51417 * @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]3528d6302014-02-19 08:13:07422 */
raymes161514f2015-02-17 05:09:51423 fitToPageInternal_: function(scrollToTopOfPage) {
[email protected]499e9562014-06-26 05:45:27424 this.mightZoom_(function() {
425 this.fittingType_ = Viewport.FittingType.FIT_TO_PAGE;
426 if (!this.documentDimensions_)
427 return;
428 var page = this.getMostVisiblePage();
sammc07f53aa2014-11-06 06:57:46429 // 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));
raymesbd82a942015-08-05 07:05:18435 if (scrollToTopOfPage) {
436 this.position = {
437 x: 0,
438 y: this.pageDimensions_[page].y * this.zoom_
439 };
440 }
[email protected]499e9562014-06-26 05:45:27441 this.updateViewport_();
442 }.bind(this));
[email protected]3528d6302014-02-19 08:13:07443 },
444
445 /**
raymes161514f2015-02-17 05:09:51446 * 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]3528d6302014-02-19 08:13:07454 * Zoom out to the next predefined zoom level.
455 */
456 zoomOut: function() {
[email protected]499e9562014-06-26 05:45:27457 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]fbad5bb2014-07-18 07:20:36464 this.setZoomInternal_(nextZoom);
[email protected]499e9562014-06-26 05:45:27465 this.updateViewport_();
466 }.bind(this));
[email protected]3528d6302014-02-19 08:13:07467 },
468
469 /**
470 * Zoom in to the next predefined zoom level.
471 */
472 zoomIn: function() {
[email protected]499e9562014-06-26 05:45:27473 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]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 * Go to the given page index.
[email protected]f90b9a42014-08-20 05:37:34487 * @param {number} page the index of the page to go to. zero-based.
[email protected]3528d6302014-02-19 08:13:07488 */
489 goToPage: function(page) {
[email protected]499e9562014-06-26 05:45:27490 this.mightZoom_(function() {
alexandrec9754b9e42015-01-19 03:41:19491 if (this.pageDimensions_.length === 0)
[email protected]499e9562014-06-26 05:45:27492 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];
raymesbd82a942015-08-05 07:05:18498 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]499e9562014-06-26 05:45:27508 this.updateViewport_();
509 }.bind(this));
[email protected]3528d6302014-02-19 08:13:07510 },
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]499e9562014-06-26 05:45:27517 this.mightZoom_(function() {
518 var initialDimensions = !this.documentDimensions_;
519 this.documentDimensions_ = documentDimensions;
520 this.pageDimensions_ = this.documentDimensions_.pageDimensions;
521 if (initialDimensions) {
sammc43d4982f2015-04-22 07:46:51522 this.setZoomInternal_(
523 Math.min(this.defaultZoom_,
524 this.computeFittingZoom_(this.documentDimensions_, true)));
raymesbd82a942015-08-05 07:05:18525 this.position = {
526 x: 0,
527 y: -this.topToolbarHeight_
528 };
[email protected]499e9562014-06-26 05:45:27529 }
530 this.contentSizeChanged_();
531 this.resize_();
532 }.bind(this));
[email protected]312112c72014-04-14 01:45:43533 },
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]499e9562014-06-26 05:45:27542 if (!this.documentDimensions_) {
543 return {
544 x: 0,
545 y: 0,
546 width: 0,
547 height: 0
548 };
549 }
[email protected]312112c72014-04-14 01:45:43550 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]345e7c62014-05-02 09:52:58565 // 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]312112c72014-04-14 01:45:43575
576 return {
[email protected]345e7c62014-05-02 09:52:58577 x: x * this.zoom_ + spaceOnLeft - this.window_.pageXOffset,
578 y: insetDimensions.y * this.zoom_ - this.window_.pageYOffset,
[email protected]312112c72014-04-14 01:45:43579 width: insetDimensions.width * this.zoom_,
580 height: insetDimensions.height * this.zoom_
581 };
[email protected]3528d6302014-02-19 08:13:07582 }
583};