libpdfviewer/app/src/main/assets/viewer.js

223 lines
6.8 KiB
JavaScript
Raw Normal View History

"use strict";
pdfjsLib.GlobalWorkerOptions.workerSrc = "/pdf.worker.js";
const padding = document.getElementById("padding");
let pdfDoc = null;
let pageRendering = false;
let renderPending = false;
let renderPendingZoom = 0;
const canvas = document.getElementById('content');
2017-11-14 18:29:10 +01:00
let orientationDegrees = 0;
2019-11-18 11:49:58 +01:00
let zoomRatio = 1;
let textLayerDiv = document.getElementById("text");
let task = null;
let newPageNumber = 0;
2019-11-18 11:49:58 +01:00
let newZoomRatio = 1;
let useRender;
const cache = [];
const maxCached = 6;
function maybeRenderNextPage() {
if (renderPending) {
pageRendering = false;
renderPending = false;
renderPage(channel.getPage(), renderPendingZoom, false);
return true;
}
return false;
}
function handleRenderingError(error) {
console.log("rendering error: " + error);
pageRendering = false;
maybeRenderNextPage();
}
function doPrerender(pageNumber, prerenderTrigger) {
if (useRender) {
if (pageNumber + 1 <= pdfDoc.numPages) {
renderPage(pageNumber + 1, false, true, pageNumber);
} else if (pageNumber - 1 > 0) {
renderPage(pageNumber - 1, false, true, pageNumber);
}
2019-07-01 07:56:54 +02:00
} else if (pageNumber === prerenderTrigger + 1) {
if (prerenderTrigger - 1 > 0) {
renderPage(prerenderTrigger - 1, false, true, prerenderTrigger);
}
}
}
function display(newCanvas, zoom) {
canvas.height = newCanvas.height;
canvas.width = newCanvas.width;
canvas.style.height = newCanvas.style.height;
canvas.style.width = newCanvas.style.width;
padding.style.width = canvas.style.width;
canvas.getContext("2d", { alpha: false }).drawImage(newCanvas, 0, 0);
if (!zoom) {
scrollTo(0, 0);
}
}
function renderPage(pageNumber, zoom, prerender, prerenderTrigger=0) {
pageRendering = true;
useRender = !prerender;
newPageNumber = pageNumber;
2019-11-18 11:49:58 +01:00
newZoomRatio = channel.getZoomRatio();
2017-11-14 18:29:10 +01:00
orientationDegrees = channel.getDocumentOrientationDegrees();
2019-11-18 11:49:58 +01:00
console.log("page: " + pageNumber + ", zoom: " + newZoomRatio +
2017-11-14 18:29:10 +01:00
", orientationDegrees: " + orientationDegrees + ", prerender: " + prerender);
for (let i = 0; i < cache.length; i++) {
const cached = cache[i];
2019-11-18 11:49:58 +01:00
if (cached.pageNumber === pageNumber && cached.zoomRatio === newZoomRatio &&
2019-07-01 07:56:54 +02:00
cache.orientationDegrees === orientationDegrees) {
if (useRender) {
cache.splice(i, 1);
cache.push(cached);
display(cached.canvas, zoom);
textLayerDiv.replaceWith(cached.textLayerDiv);
textLayerDiv = cached.textLayerDiv;
}
pageRendering = false;
doPrerender(pageNumber, prerenderTrigger);
return;
}
}
pdfDoc.getPage(pageNumber).then(function(page) {
if (maybeRenderNextPage()) {
return;
}
2019-11-18 11:49:58 +01:00
const viewport = page.getViewport({scale: newZoomRatio, rotation: orientationDegrees})
if (useRender) {
2019-11-18 11:49:58 +01:00
if (newZoomRatio !== zoomRatio) {
canvas.style.height = viewport.height + "px";
canvas.style.width = viewport.width + "px";
}
2019-11-18 11:49:58 +01:00
zoomRatio = newZoomRatio;
}
if (zoom == 2) {
pageRendering = false;
return;
}
const newCanvas = document.createElement("canvas");
const ratio = window.devicePixelRatio;
newCanvas.height = viewport.height * ratio;
newCanvas.width = viewport.width * ratio;
newCanvas.style.height = viewport.height + "px";
newCanvas.style.width = viewport.width + "px";
const newContext = newCanvas.getContext("2d", { alpha: false });
newContext.scale(ratio, ratio);
task = page.render({
canvasContext: newContext,
viewport: viewport
});
2019-08-16 07:19:50 +02:00
task.promise.then(function() {
task = null;
let rendered = false;
function render() {
if (!useRender || rendered) {
return;
}
display(newCanvas, zoom);
rendered = true;
}
render();
const textLayerFrag = document.createDocumentFragment();
2019-08-16 07:19:50 +02:00
task = pdfjsLib.renderTextLayer({
textContentStream: page.streamTextContent(),
container: textLayerFrag,
viewport: viewport
});
task.promise.then(function() {
task = null;
render();
const newTextLayerDiv = textLayerDiv.cloneNode();
newTextLayerDiv.style.height = newCanvas.style.height;
newTextLayerDiv.style.width = newCanvas.style.width;
if (useRender) {
textLayerDiv.replaceWith(newTextLayerDiv);
textLayerDiv = newTextLayerDiv;
}
newTextLayerDiv.appendChild(textLayerFrag);
if (cache.length === maxCached) {
cache.shift()
}
cache.push({
pageNumber: pageNumber,
2019-11-18 11:49:58 +01:00
zoomRatio: newZoomRatio,
2017-11-14 18:29:10 +01:00
orientationDegrees: orientationDegrees,
canvas: newCanvas,
textLayerDiv: newTextLayerDiv
});
pageRendering = false;
doPrerender(pageNumber, prerenderTrigger);
}).catch(handleRenderingError);
}).catch(handleRenderingError);
});
}
function onRenderPage(zoom) {
if (pageRendering) {
if (newPageNumber === channel.getPage() && newZoomRatio === channel.getZoomRatio() &&
2017-11-14 18:29:10 +01:00
orientationDegrees === channel.getDocumentOrientationDegrees()) {
useRender = true;
return;
}
renderPending = true;
renderPendingZoom = zoom;
if (task !== null) {
task.cancel();
task = null;
}
} else {
renderPage(channel.getPage(), zoom, false);
}
}
2019-08-17 01:42:51 +02:00
function isTextSelected() {
return window.getSelection().toString() !== "";
}
function updateInset() {
const windowInsetTop = channel.getWindowInsetTop() / window.devicePixelRatio + "px";
padding.style.paddingTop = windowInsetTop;
textLayerDiv.style.top = windowInsetTop;
}
updateInset();
2019-08-16 07:19:50 +02:00
pdfjsLib.getDocument("https://localhost/placeholder.pdf").promise.then(function(newDoc) {
pdfDoc = newDoc;
channel.setNumPages(pdfDoc.numPages);
pdfDoc.getMetadata().then(function(data) {
channel.setDocumentProperties(JSON.stringify(data.info));
}).catch(function(error) {
console.log("getMetadata error: " + error);
});
renderPage(channel.getPage(), false, false);
}).catch(function(error) {
console.log("getDocument error: " + error);
});