08e2427f9c
Having recently worked with this code, in PR 14096 (and indirectly in PR 14112), I happened to notice a pre-existing issue with spreadModes at higher zoom levels. The `PDFRenderingQueue` code was written back when the viewer only supported "normal" vertical scrolling, and some edge-cases related to spreadModes are thus not perfectly supported. Depending on the zoom level, it's possible that there are "holes" in the currently visible page layout, and those pages will not be pre-rendered as you'd expect. *Steps to reproduce:* 0. Open the viewer, e.g. https://mozilla.github.io/pdf.js/web/viewer.html 1. Enable vertical scrolling. 2. Enable the ODD spreadMode. 3. Scroll down, such that both pages 1 and 3 are visible. 4. Zoom-in until *only* page 1 and 3 are visible. 5. Open the devtools and, using the DOM Inspector, notice how page 2 is *not* being pre-rendered despite all surrounding pages being rendered.
210 lines
5.6 KiB
JavaScript
210 lines
5.6 KiB
JavaScript
/* Copyright 2012 Mozilla Foundation
|
|
*
|
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
* you may not use this file except in compliance with the License.
|
|
* You may obtain a copy of the License at
|
|
*
|
|
* http://www.apache.org/licenses/LICENSE-2.0
|
|
*
|
|
* Unless required by applicable law or agreed to in writing, software
|
|
* distributed under the License is distributed on an "AS IS" BASIS,
|
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
* See the License for the specific language governing permissions and
|
|
* limitations under the License.
|
|
*/
|
|
|
|
import { RenderingCancelledException } from "pdfjs-lib";
|
|
|
|
const CLEANUP_TIMEOUT = 30000;
|
|
|
|
const RenderingStates = {
|
|
INITIAL: 0,
|
|
RUNNING: 1,
|
|
PAUSED: 2,
|
|
FINISHED: 3,
|
|
};
|
|
|
|
/**
|
|
* Controls rendering of the views for pages and thumbnails.
|
|
*/
|
|
class PDFRenderingQueue {
|
|
constructor() {
|
|
this.pdfViewer = null;
|
|
this.pdfThumbnailViewer = null;
|
|
this.onIdle = null;
|
|
this.highestPriorityPage = null;
|
|
/** @type {number} */
|
|
this.idleTimeout = null;
|
|
this.printing = false;
|
|
this.isThumbnailViewEnabled = false;
|
|
}
|
|
|
|
/**
|
|
* @param {PDFViewer} pdfViewer
|
|
*/
|
|
setViewer(pdfViewer) {
|
|
this.pdfViewer = pdfViewer;
|
|
}
|
|
|
|
/**
|
|
* @param {PDFThumbnailViewer} pdfThumbnailViewer
|
|
*/
|
|
setThumbnailViewer(pdfThumbnailViewer) {
|
|
this.pdfThumbnailViewer = pdfThumbnailViewer;
|
|
}
|
|
|
|
/**
|
|
* @param {IRenderableView} view
|
|
* @returns {boolean}
|
|
*/
|
|
isHighestPriority(view) {
|
|
return this.highestPriorityPage === view.renderingId;
|
|
}
|
|
|
|
/**
|
|
* @returns {boolean}
|
|
*/
|
|
hasViewer() {
|
|
return !!this.pdfViewer;
|
|
}
|
|
|
|
/**
|
|
* @param {Object} currentlyVisiblePages
|
|
*/
|
|
renderHighestPriority(currentlyVisiblePages) {
|
|
if (this.idleTimeout) {
|
|
clearTimeout(this.idleTimeout);
|
|
this.idleTimeout = null;
|
|
}
|
|
|
|
// Pages have a higher priority than thumbnails, so check them first.
|
|
if (this.pdfViewer.forceRendering(currentlyVisiblePages)) {
|
|
return;
|
|
}
|
|
// No pages needed rendering, so check thumbnails.
|
|
if (this.pdfThumbnailViewer && this.isThumbnailViewEnabled) {
|
|
if (this.pdfThumbnailViewer.forceRendering()) {
|
|
return;
|
|
}
|
|
}
|
|
|
|
if (this.printing) {
|
|
// If printing is currently ongoing do not reschedule cleanup.
|
|
return;
|
|
}
|
|
|
|
if (this.onIdle) {
|
|
this.idleTimeout = setTimeout(this.onIdle.bind(this), CLEANUP_TIMEOUT);
|
|
}
|
|
}
|
|
|
|
/**
|
|
* @param {Object} visible
|
|
* @param {Array} views
|
|
* @param {boolean} scrolledDown
|
|
* @param {boolean} [preRenderExtra]
|
|
*/
|
|
getHighestPriority(visible, views, scrolledDown, preRenderExtra = false) {
|
|
/**
|
|
* The state has changed. Figure out which page has the highest priority to
|
|
* render next (if any).
|
|
*
|
|
* Priority:
|
|
* 1. visible pages
|
|
* 2. if last scrolled down, the page after the visible pages, or
|
|
* if last scrolled up, the page before the visible pages
|
|
*/
|
|
const visibleViews = visible.views;
|
|
|
|
const numVisible = visibleViews.length;
|
|
if (numVisible === 0) {
|
|
return null;
|
|
}
|
|
for (let i = 0; i < numVisible; ++i) {
|
|
const view = visibleViews[i].view;
|
|
if (!this.isViewFinished(view)) {
|
|
return view;
|
|
}
|
|
}
|
|
const firstId = visible.first.id,
|
|
lastId = visible.last.id;
|
|
|
|
// All the visible views have rendered; try to handle any "holes" in the
|
|
// page layout (can happen e.g. with spreadModes at higher zoom levels).
|
|
if (lastId - firstId > 1) {
|
|
for (let i = 0, ii = lastId - firstId; i <= ii; i++) {
|
|
const holeId = scrolledDown ? firstId + i : lastId - i,
|
|
holeView = views[holeId - 1];
|
|
if (!this.isViewFinished(holeView)) {
|
|
return holeView;
|
|
}
|
|
}
|
|
}
|
|
|
|
// All the visible views have rendered; try to render next/previous page.
|
|
// (IDs start at 1, so no need to add 1 when `scrolledDown === true`.)
|
|
let preRenderIndex = scrolledDown ? lastId : firstId - 2;
|
|
let preRenderView = views[preRenderIndex];
|
|
|
|
if (preRenderView && !this.isViewFinished(preRenderView)) {
|
|
return preRenderView;
|
|
}
|
|
if (preRenderExtra) {
|
|
preRenderIndex += scrolledDown ? 1 : -1;
|
|
preRenderView = views[preRenderIndex];
|
|
|
|
if (preRenderView && !this.isViewFinished(preRenderView)) {
|
|
return preRenderView;
|
|
}
|
|
}
|
|
// Everything that needs to be rendered has been.
|
|
return null;
|
|
}
|
|
|
|
/**
|
|
* @param {IRenderableView} view
|
|
* @returns {boolean}
|
|
*/
|
|
isViewFinished(view) {
|
|
return view.renderingState === RenderingStates.FINISHED;
|
|
}
|
|
|
|
/**
|
|
* Render a page or thumbnail view. This calls the appropriate function
|
|
* based on the views state. If the view is already rendered it will return
|
|
* `false`.
|
|
*
|
|
* @param {IRenderableView} view
|
|
*/
|
|
renderView(view) {
|
|
switch (view.renderingState) {
|
|
case RenderingStates.FINISHED:
|
|
return false;
|
|
case RenderingStates.PAUSED:
|
|
this.highestPriorityPage = view.renderingId;
|
|
view.resume();
|
|
break;
|
|
case RenderingStates.RUNNING:
|
|
this.highestPriorityPage = view.renderingId;
|
|
break;
|
|
case RenderingStates.INITIAL:
|
|
this.highestPriorityPage = view.renderingId;
|
|
view
|
|
.draw()
|
|
.finally(() => {
|
|
this.renderHighestPriority();
|
|
})
|
|
.catch(reason => {
|
|
if (reason instanceof RenderingCancelledException) {
|
|
return;
|
|
}
|
|
console.error(`renderView: "${reason}"`);
|
|
});
|
|
break;
|
|
}
|
|
return true;
|
|
}
|
|
}
|
|
|
|
export { PDFRenderingQueue, RenderingStates };
|