blob: 06428a9d844797743f454e9df6798dde1d89c70e [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
[email protected]3528d6302014-02-19 08:13:0727 */
28function Viewport(window,
29 sizer,
[email protected]345e7c62014-05-02 09:52:5830 viewportChangedCallback,
[email protected]499e9562014-06-26 05:45:2731 beforeZoomCallback,
32 afterZoomCallback,
alexandrec915a1a42015-02-06 06:41:1633 scrollbarWidth) {
[email protected]3528d6302014-02-19 08:13:0734 this.window_ = window;
35 this.sizer_ = sizer;
[email protected]3528d6302014-02-19 08:13:0736 this.viewportChangedCallback_ = viewportChangedCallback;
[email protected]499e9562014-06-26 05:45:2737 this.beforeZoomCallback_ = beforeZoomCallback;
38 this.afterZoomCallback_ = afterZoomCallback;
39 this.allowedToChangeZoom_ = false;
[email protected]3528d6302014-02-19 08:13:0740 this.zoom_ = 1;
[email protected]312112c72014-04-14 01:45:4341 this.documentDimensions_ = null;
[email protected]3528d6302014-02-19 08:13:0742 this.pageDimensions_ = [];
[email protected]345e7c62014-05-02 09:52:5843 this.scrollbarWidth_ = scrollbarWidth;
[email protected]312112c72014-04-14 01:45:4344 this.fittingType_ = Viewport.FittingType.NONE;
[email protected]3528d6302014-02-19 08:13:0745
46 window.addEventListener('scroll', this.updateViewport_.bind(this));
[email protected]312112c72014-04-14 01:45:4347 window.addEventListener('resize', this.resize_.bind(this));
[email protected]3528d6302014-02-19 08:13:0748}
49
[email protected]312112c72014-04-14 01:45:4350/**
51 * Enumeration of page fitting types.
52 * @enum {string}
53 */
54Viewport.FittingType = {
55 NONE: 'none',
56 FIT_TO_PAGE: 'fit-to-page',
57 FIT_TO_WIDTH: 'fit-to-width'
58};
59
60/**
[email protected]8dcaa262014-05-30 13:33:3761 * The increment to scroll a page by in pixels when up/down/left/right arrow
62 * keys are pressed. Usually we just let the browser handle scrolling on the
63 * window when these keys are pressed but in certain cases we need to simulate
64 * these events.
65 */
66Viewport.SCROLL_INCREMENT = 40;
67
68/**
[email protected]312112c72014-04-14 01:45:4369 * Predefined zoom factors to be used when zooming in/out. These are in
[email protected]c90b3292014-08-20 00:57:4170 * ascending order. This should match the list in
wjmaclean91ddc472015-01-15 19:58:2771 * components/ui/zoom/page_zoom_constants.h
[email protected]312112c72014-04-14 01:45:4372 */
73Viewport.ZOOM_FACTORS = [0.25, 0.333, 0.5, 0.666, 0.75, 0.9, 1,
74 1.1, 1.25, 1.5, 1.75, 2, 2.5, 3, 4, 5];
75
76/**
[email protected]4271e16b2014-08-22 12:18:5977 * The minimum and maximum range to be used to clip zoom factor.
78 */
79Viewport.ZOOM_FACTOR_RANGE = {
80 min: Viewport.ZOOM_FACTORS[0],
81 max: Viewport.ZOOM_FACTORS[Viewport.ZOOM_FACTORS.length - 1]
82};
83
84/**
[email protected]312112c72014-04-14 01:45:4385 * The width of the page shadow around pages in pixels.
86 */
87Viewport.PAGE_SHADOW = {top: 3, bottom: 7, left: 5, right: 5};
88
[email protected]3528d6302014-02-19 08:13:0789Viewport.prototype = {
90 /**
91 * @private
92 * Returns true if the document needs scrollbars at the given zoom level.
93 * @param {number} zoom compute whether scrollbars are needed at this zoom
[email protected]312112c72014-04-14 01:45:4394 * @return {Object} with 'horizontal' and 'vertical' keys which map to bool
95 * values indicating if the horizontal and vertical scrollbars are needed
[email protected]3528d6302014-02-19 08:13:0796 * respectively.
97 */
98 documentNeedsScrollbars_: function(zoom) {
[email protected]499e9562014-06-26 05:45:2799 if (!this.documentDimensions_) {
100 return {
101 horizontal: false,
102 vertical: false
103 };
104 }
[email protected]312112c72014-04-14 01:45:43105 var documentWidth = this.documentDimensions_.width * zoom;
106 var documentHeight = this.documentDimensions_.height * zoom;
sammc63334fed2014-11-10 03:07:35107
108 // If scrollbars are required for one direction, expand the document in the
109 // other direction to take the width of the scrollbars into account when
110 // deciding whether the other direction needs scrollbars.
111 if (documentWidth > this.window_.innerWidth)
112 documentHeight += this.scrollbarWidth_;
113 else if (documentHeight > this.window_.innerHeight)
114 documentWidth += this.scrollbarWidth_;
[email protected]3528d6302014-02-19 08:13:07115 return {
[email protected]312112c72014-04-14 01:45:43116 horizontal: documentWidth > this.window_.innerWidth,
117 vertical: documentHeight > this.window_.innerHeight
[email protected]3528d6302014-02-19 08:13:07118 };
119 },
120
121 /**
122 * Returns true if the document needs scrollbars at the current zoom level.
123 * @return {Object} with 'x' and 'y' keys which map to bool values
124 * indicating if the horizontal and vertical scrollbars are needed
125 * respectively.
126 */
127 documentHasScrollbars: function() {
128 return this.documentNeedsScrollbars_(this.zoom_);
129 },
130
131 /**
132 * @private
133 * Helper function called when the zoomed document size changes.
134 */
135 contentSizeChanged_: function() {
[email protected]345e7c62014-05-02 09:52:58136 if (this.documentDimensions_) {
137 this.sizer_.style.width =
138 this.documentDimensions_.width * this.zoom_ + 'px';
139 this.sizer_.style.height =
alexandrec915a1a42015-02-06 06:41:16140 this.documentDimensions_.height * this.zoom_ + 'px';
[email protected]345e7c62014-05-02 09:52:58141 }
[email protected]3528d6302014-02-19 08:13:07142 },
143
144 /**
[email protected]312112c72014-04-14 01:45:43145 * @private
146 * Called when the viewport should be updated.
[email protected]3528d6302014-02-19 08:13:07147 */
[email protected]312112c72014-04-14 01:45:43148 updateViewport_: function() {
149 this.viewportChangedCallback_();
150 },
151
152 /**
153 * @private
154 * Called when the viewport size changes.
155 */
156 resize_: function() {
157 if (this.fittingType_ == Viewport.FittingType.FIT_TO_PAGE)
raymes161514f2015-02-17 05:09:51158 this.fitToPageInternal_(false);
[email protected]312112c72014-04-14 01:45:43159 else if (this.fittingType_ == Viewport.FittingType.FIT_TO_WIDTH)
160 this.fitToWidth();
161 else
162 this.updateViewport_();
163 },
164
165 /**
166 * @type {Object} the scroll position of the viewport.
167 */
168 get position() {
169 return {
170 x: this.window_.pageXOffset,
171 y: this.window_.pageYOffset
172 };
173 },
174
175 /**
176 * Scroll the viewport to the specified position.
177 * @type {Object} position the position to scroll to.
178 */
179 set position(position) {
180 this.window_.scrollTo(position.x, position.y);
181 },
182
183 /**
184 * @type {Object} the size of the viewport excluding scrollbars.
185 */
186 get size() {
187 var needsScrollbars = this.documentNeedsScrollbars_(this.zoom_);
188 var scrollbarWidth = needsScrollbars.vertical ? this.scrollbarWidth_ : 0;
189 var scrollbarHeight = needsScrollbars.horizontal ? this.scrollbarWidth_ : 0;
190 return {
191 width: this.window_.innerWidth - scrollbarWidth,
192 height: this.window_.innerHeight - scrollbarHeight
193 };
194 },
195
196 /**
197 * @type {number} the zoom level of the viewport.
198 */
199 get zoom() {
200 return this.zoom_;
201 },
202
203 /**
204 * @private
[email protected]499e9562014-06-26 05:45:27205 * Used to wrap a function that might perform zooming on the viewport. This is
206 * required so that we can notify the plugin that zooming is in progress
207 * so that while zooming is taking place it can stop reacting to scroll events
208 * from the viewport. This is to avoid flickering.
209 */
210 mightZoom_: function(f) {
211 this.beforeZoomCallback_();
212 this.allowedToChangeZoom_ = true;
213 f();
214 this.allowedToChangeZoom_ = false;
215 this.afterZoomCallback_();
216 },
217
218 /**
219 * @private
[email protected]312112c72014-04-14 01:45:43220 * Sets the zoom of the viewport.
221 * @param {number} newZoom the zoom level to zoom to.
222 */
[email protected]fbad5bb2014-07-18 07:20:36223 setZoomInternal_: function(newZoom) {
224 if (!this.allowedToChangeZoom_) {
225 throw 'Called Viewport.setZoomInternal_ without calling ' +
226 'Viewport.mightZoom_.';
227 }
sammc5c33b7e2014-11-07 04:03:09228 // Record the scroll position (relative to the top-left of the window).
[email protected]3528d6302014-02-19 08:13:07229 var currentScrollPos = [
sammc5c33b7e2014-11-07 04:03:09230 this.window_.pageXOffset / this.zoom_,
231 this.window_.pageYOffset / this.zoom_
[email protected]3528d6302014-02-19 08:13:07232 ];
sammc5c33b7e2014-11-07 04:03:09233 this.zoom_ = newZoom;
[email protected]3528d6302014-02-19 08:13:07234 this.contentSizeChanged_();
235 // Scroll to the scaled scroll position.
sammc5c33b7e2014-11-07 04:03:09236 this.window_.scrollTo(currentScrollPos[0] * newZoom,
237 currentScrollPos[1] * newZoom);
[email protected]3528d6302014-02-19 08:13:07238 },
239
240 /**
[email protected]fbad5bb2014-07-18 07:20:36241 * Sets the zoom to the given zoom level.
242 * @param {number} newZoom the zoom level to zoom to.
[email protected]499e9562014-06-26 05:45:27243 */
[email protected]fbad5bb2014-07-18 07:20:36244 setZoom: function(newZoom) {
[email protected]4271e16b2014-08-22 12:18:59245 newZoom = Math.max(Viewport.ZOOM_FACTOR_RANGE.min,
246 Math.min(newZoom, Viewport.ZOOM_FACTOR_RANGE.max));
[email protected]499e9562014-06-26 05:45:27247 this.mightZoom_(function() {
[email protected]fbad5bb2014-07-18 07:20:36248 this.setZoomInternal_(newZoom);
249 this.updateViewport_();
[email protected]499e9562014-06-26 05:45:27250 }.bind(this));
251 },
252
253 /**
[email protected]312112c72014-04-14 01:45:43254 * @type {number} the width of scrollbars in the viewport in pixels.
[email protected]3528d6302014-02-19 08:13:07255 */
[email protected]312112c72014-04-14 01:45:43256 get scrollbarWidth() {
257 return this.scrollbarWidth_;
[email protected]3528d6302014-02-19 08:13:07258 },
259
260 /**
[email protected]312112c72014-04-14 01:45:43261 * @type {Viewport.FittingType} the fitting type the viewport is currently in.
[email protected]3528d6302014-02-19 08:13:07262 */
[email protected]312112c72014-04-14 01:45:43263 get fittingType() {
264 return this.fittingType_;
[email protected]3528d6302014-02-19 08:13:07265 },
266
267 /**
[email protected]56b7e3c2014-02-20 04:31:24268 * @private
269 * @param {integer} y the y-coordinate to get the page at.
270 * @return {integer} the index of a page overlapping the given y-coordinate.
271 */
272 getPageAtY_: function(y) {
273 var min = 0;
274 var max = this.pageDimensions_.length - 1;
275 while (max >= min) {
276 var page = Math.floor(min + ((max - min) / 2));
[email protected]13df2a42014-02-27 03:50:41277 // There might be a gap between the pages, in which case use the bottom
278 // of the previous page as the top for finding the page.
279 var top = 0;
280 if (page > 0) {
281 top = this.pageDimensions_[page - 1].y +
282 this.pageDimensions_[page - 1].height;
283 }
284 var bottom = this.pageDimensions_[page].y +
285 this.pageDimensions_[page].height;
286
[email protected]56b7e3c2014-02-20 04:31:24287 if (top <= y && bottom > y)
288 return page;
289 else if (top > y)
290 max = page - 1;
291 else
292 min = page + 1;
293 }
294 return 0;
295 },
296
297 /**
sammcd06823a962014-11-10 04:51:15298 * Returns the page with the greatest proportion of its height in the current
299 * viewport.
[email protected]3528d6302014-02-19 08:13:07300 * @return {int} the index of the most visible page.
301 */
302 getMostVisiblePage: function() {
[email protected]312112c72014-04-14 01:45:43303 var firstVisiblePage = this.getPageAtY_(this.position.y / this.zoom_);
sammcd06823a962014-11-10 04:51:15304 if (firstVisiblePage == this.pageDimensions_.length - 1)
305 return firstVisiblePage;
306
[email protected]345e7c62014-05-02 09:52:58307 var viewportRect = {
308 x: this.position.x / this.zoom_,
309 y: this.position.y / this.zoom_,
310 width: this.size.width / this.zoom_,
311 height: this.size.height / this.zoom_
312 };
sammcd06823a962014-11-10 04:51:15313 var firstVisiblePageVisibility = getIntersectionHeight(
314 this.pageDimensions_[firstVisiblePage], viewportRect) /
315 this.pageDimensions_[firstVisiblePage].height;
316 var nextPageVisibility = getIntersectionHeight(
317 this.pageDimensions_[firstVisiblePage + 1], viewportRect) /
318 this.pageDimensions_[firstVisiblePage + 1].height;
319 if (nextPageVisibility > firstVisiblePageVisibility)
320 return firstVisiblePage + 1;
321 return firstVisiblePage;
[email protected]3528d6302014-02-19 08:13:07322 },
323
324 /**
325 * @private
326 * Compute the zoom level for fit-to-page or fit-to-width. |pageDimensions| is
327 * the dimensions for a given page and if |widthOnly| is true, it indicates
328 * that fit-to-page zoom should be computed rather than fit-to-page.
329 * @param {Object} pageDimensions the dimensions of a given page
330 * @param {boolean} widthOnly a bool indicating whether fit-to-page or
331 * fit-to-width should be computed.
332 * @return {number} the zoom to use
333 */
334 computeFittingZoom_: function(pageDimensions, widthOnly) {
335 // First compute the zoom without scrollbars.
336 var zoomWidth = this.window_.innerWidth / pageDimensions.width;
337 var zoom;
alexandrec9754b9e42015-01-19 03:41:19338 var zoomHeight;
[email protected]3528d6302014-02-19 08:13:07339 if (widthOnly) {
340 zoom = zoomWidth;
341 } else {
alexandrec9754b9e42015-01-19 03:41:19342 zoomHeight = this.window_.innerHeight / pageDimensions.height;
[email protected]3528d6302014-02-19 08:13:07343 zoom = Math.min(zoomWidth, zoomHeight);
344 }
345 // Check if there needs to be any scrollbars.
346 var needsScrollbars = this.documentNeedsScrollbars_(zoom);
347
348 // If the document fits, just return the zoom.
[email protected]312112c72014-04-14 01:45:43349 if (!needsScrollbars.horizontal && !needsScrollbars.vertical)
[email protected]3528d6302014-02-19 08:13:07350 return zoom;
351
352 var zoomedDimensions = {
353 width: this.documentDimensions_.width * zoom,
354 height: this.documentDimensions_.height * zoom
355 };
356
357 // Check if adding a scrollbar will result in needing the other scrollbar.
358 var scrollbarWidth = this.scrollbarWidth_;
[email protected]312112c72014-04-14 01:45:43359 if (needsScrollbars.horizontal &&
[email protected]3528d6302014-02-19 08:13:07360 zoomedDimensions.height > this.window_.innerHeight - scrollbarWidth) {
[email protected]312112c72014-04-14 01:45:43361 needsScrollbars.vertical = true;
[email protected]3528d6302014-02-19 08:13:07362 }
[email protected]312112c72014-04-14 01:45:43363 if (needsScrollbars.vertical &&
[email protected]3528d6302014-02-19 08:13:07364 zoomedDimensions.width > this.window_.innerWidth - scrollbarWidth) {
[email protected]312112c72014-04-14 01:45:43365 needsScrollbars.horizontal = true;
[email protected]3528d6302014-02-19 08:13:07366 }
367
368 // Compute available window space.
369 var windowWithScrollbars = {
370 width: this.window_.innerWidth,
371 height: this.window_.innerHeight
372 };
[email protected]312112c72014-04-14 01:45:43373 if (needsScrollbars.horizontal)
[email protected]3528d6302014-02-19 08:13:07374 windowWithScrollbars.height -= scrollbarWidth;
[email protected]312112c72014-04-14 01:45:43375 if (needsScrollbars.vertical)
[email protected]3528d6302014-02-19 08:13:07376 windowWithScrollbars.width -= scrollbarWidth;
377
378 // Recompute the zoom.
379 zoomWidth = windowWithScrollbars.width / pageDimensions.width;
380 if (widthOnly) {
381 zoom = zoomWidth;
382 } else {
alexandrec9754b9e42015-01-19 03:41:19383 zoomHeight = windowWithScrollbars.height / pageDimensions.height;
[email protected]3528d6302014-02-19 08:13:07384 zoom = Math.min(zoomWidth, zoomHeight);
385 }
386 return zoom;
387 },
388
389 /**
390 * Zoom the viewport so that the page-width consumes the entire viewport.
391 */
392 fitToWidth: function() {
[email protected]499e9562014-06-26 05:45:27393 this.mightZoom_(function() {
394 this.fittingType_ = Viewport.FittingType.FIT_TO_WIDTH;
395 if (!this.documentDimensions_)
396 return;
[email protected]499e9562014-06-26 05:45:27397 // When computing fit-to-width, the maximum width of a page in the
398 // document is used, which is equal to the size of the document width.
[email protected]fbad5bb2014-07-18 07:20:36399 this.setZoomInternal_(this.computeFittingZoom_(this.documentDimensions_,
400 true));
[email protected]499e9562014-06-26 05:45:27401 var page = this.getMostVisiblePage();
[email protected]499e9562014-06-26 05:45:27402 this.updateViewport_();
403 }.bind(this));
[email protected]3528d6302014-02-19 08:13:07404 },
405
406 /**
raymes161514f2015-02-17 05:09:51407 * @private
408 * Zoom the viewport so that a page consumes the entire viewport.
409 * @param {boolean} scrollToTopOfPage Set to true if the viewport should be
410 * scrolled to the top of the current page. Set to false if the viewport
411 * should remain at the current scroll position.
[email protected]3528d6302014-02-19 08:13:07412 */
raymes161514f2015-02-17 05:09:51413 fitToPageInternal_: function(scrollToTopOfPage) {
[email protected]499e9562014-06-26 05:45:27414 this.mightZoom_(function() {
415 this.fittingType_ = Viewport.FittingType.FIT_TO_PAGE;
416 if (!this.documentDimensions_)
417 return;
418 var page = this.getMostVisiblePage();
sammc07f53aa2014-11-06 06:57:46419 // Fit to the current page's height and the widest page's width.
420 var dimensions = {
421 width: this.documentDimensions_.width,
422 height: this.pageDimensions_[page].height,
423 };
424 this.setZoomInternal_(this.computeFittingZoom_(dimensions, false));
raymes161514f2015-02-17 05:09:51425 if (scrollToTopOfPage)
426 this.window_.scrollTo(0, this.pageDimensions_[page].y * this.zoom_);
[email protected]499e9562014-06-26 05:45:27427 this.updateViewport_();
428 }.bind(this));
[email protected]3528d6302014-02-19 08:13:07429 },
430
431 /**
raymes161514f2015-02-17 05:09:51432 * Zoom the viewport so that a page consumes the entire viewport. Also scrolls
433 * the viewport to the top of the current page.
434 */
435 fitToPage: function() {
436 this.fitToPageInternal_(true);
437 },
438
439 /**
[email protected]3528d6302014-02-19 08:13:07440 * Zoom out to the next predefined zoom level.
441 */
442 zoomOut: function() {
[email protected]499e9562014-06-26 05:45:27443 this.mightZoom_(function() {
444 this.fittingType_ = Viewport.FittingType.NONE;
445 var nextZoom = Viewport.ZOOM_FACTORS[0];
446 for (var i = 0; i < Viewport.ZOOM_FACTORS.length; i++) {
447 if (Viewport.ZOOM_FACTORS[i] < this.zoom_)
448 nextZoom = Viewport.ZOOM_FACTORS[i];
449 }
[email protected]fbad5bb2014-07-18 07:20:36450 this.setZoomInternal_(nextZoom);
[email protected]499e9562014-06-26 05:45:27451 this.updateViewport_();
452 }.bind(this));
[email protected]3528d6302014-02-19 08:13:07453 },
454
455 /**
456 * Zoom in to the next predefined zoom level.
457 */
458 zoomIn: function() {
[email protected]499e9562014-06-26 05:45:27459 this.mightZoom_(function() {
460 this.fittingType_ = Viewport.FittingType.NONE;
461 var nextZoom = Viewport.ZOOM_FACTORS[Viewport.ZOOM_FACTORS.length - 1];
462 for (var i = Viewport.ZOOM_FACTORS.length - 1; i >= 0; i--) {
463 if (Viewport.ZOOM_FACTORS[i] > this.zoom_)
464 nextZoom = Viewport.ZOOM_FACTORS[i];
465 }
[email protected]fbad5bb2014-07-18 07:20:36466 this.setZoomInternal_(nextZoom);
[email protected]499e9562014-06-26 05:45:27467 this.updateViewport_();
468 }.bind(this));
[email protected]3528d6302014-02-19 08:13:07469 },
470
471 /**
472 * Go to the given page index.
[email protected]f90b9a42014-08-20 05:37:34473 * @param {number} page the index of the page to go to. zero-based.
[email protected]3528d6302014-02-19 08:13:07474 */
475 goToPage: function(page) {
[email protected]499e9562014-06-26 05:45:27476 this.mightZoom_(function() {
alexandrec9754b9e42015-01-19 03:41:19477 if (this.pageDimensions_.length === 0)
[email protected]499e9562014-06-26 05:45:27478 return;
479 if (page < 0)
480 page = 0;
481 if (page >= this.pageDimensions_.length)
482 page = this.pageDimensions_.length - 1;
483 var dimensions = this.pageDimensions_[page];
484 this.window_.scrollTo(dimensions.x * this.zoom_,
485 dimensions.y * this.zoom_);
486 this.updateViewport_();
487 }.bind(this));
[email protected]3528d6302014-02-19 08:13:07488 },
489
490 /**
491 * Set the dimensions of the document.
492 * @param {Object} documentDimensions the dimensions of the document
493 */
494 setDocumentDimensions: function(documentDimensions) {
[email protected]499e9562014-06-26 05:45:27495 this.mightZoom_(function() {
496 var initialDimensions = !this.documentDimensions_;
497 this.documentDimensions_ = documentDimensions;
498 this.pageDimensions_ = this.documentDimensions_.pageDimensions;
499 if (initialDimensions) {
[email protected]fbad5bb2014-07-18 07:20:36500 this.setZoomInternal_(this.computeFittingZoom_(this.documentDimensions_,
501 true));
[email protected]499e9562014-06-26 05:45:27502 if (this.zoom_ > 1)
[email protected]fbad5bb2014-07-18 07:20:36503 this.setZoomInternal_(1);
[email protected]499e9562014-06-26 05:45:27504 this.window_.scrollTo(0, 0);
505 }
506 this.contentSizeChanged_();
507 this.resize_();
508 }.bind(this));
[email protected]312112c72014-04-14 01:45:43509 },
510
511 /**
512 * Get the coordinates of the page contents (excluding the page shadow)
513 * relative to the screen.
514 * @param {number} page the index of the page to get the rect for.
515 * @return {Object} a rect representing the page in screen coordinates.
516 */
517 getPageScreenRect: function(page) {
[email protected]499e9562014-06-26 05:45:27518 if (!this.documentDimensions_) {
519 return {
520 x: 0,
521 y: 0,
522 width: 0,
523 height: 0
524 };
525 }
[email protected]312112c72014-04-14 01:45:43526 if (page >= this.pageDimensions_.length)
527 page = this.pageDimensions_.length - 1;
528
529 var pageDimensions = this.pageDimensions_[page];
530
531 // Compute the page dimensions minus the shadows.
532 var insetDimensions = {
533 x: pageDimensions.x + Viewport.PAGE_SHADOW.left,
534 y: pageDimensions.y + Viewport.PAGE_SHADOW.top,
535 width: pageDimensions.width - Viewport.PAGE_SHADOW.left -
536 Viewport.PAGE_SHADOW.right,
537 height: pageDimensions.height - Viewport.PAGE_SHADOW.top -
538 Viewport.PAGE_SHADOW.bottom
539 };
540
[email protected]345e7c62014-05-02 09:52:58541 // Compute the x-coordinate of the page within the document.
542 // TODO(raymes): This should really be set when the PDF plugin passes the
543 // page coordinates, but it isn't yet.
544 var x = (this.documentDimensions_.width - pageDimensions.width) / 2 +
545 Viewport.PAGE_SHADOW.left;
546 // Compute the space on the left of the document if the document fits
547 // completely in the screen.
548 var spaceOnLeft = (this.size.width -
549 this.documentDimensions_.width * this.zoom_) / 2;
550 spaceOnLeft = Math.max(spaceOnLeft, 0);
[email protected]312112c72014-04-14 01:45:43551
552 return {
[email protected]345e7c62014-05-02 09:52:58553 x: x * this.zoom_ + spaceOnLeft - this.window_.pageXOffset,
554 y: insetDimensions.y * this.zoom_ - this.window_.pageYOffset,
[email protected]312112c72014-04-14 01:45:43555 width: insetDimensions.width * this.zoom_,
556 height: insetDimensions.height * this.zoom_
557 };
[email protected]3528d6302014-02-19 08:13:07558 }
559};