Merge pull request #16288 from Snuffleupagus/print-code-cleanup

Slightly modernize print-related code
This commit is contained in:
Tim van der Meij 2023-04-15 13:08:37 +02:00 committed by GitHub
commit 4d8a60b435
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 64 additions and 73 deletions

View File

@ -114,25 +114,25 @@ function composePage(
}; };
} }
function FirefoxPrintService( class FirefoxPrintService {
pdfDocument, constructor(
pagesOverview, pdfDocument,
printContainer, pagesOverview,
printResolution, printContainer,
optionalContentConfigPromise = null, printResolution,
printAnnotationStoragePromise = null optionalContentConfigPromise = null,
) { printAnnotationStoragePromise = null
this.pdfDocument = pdfDocument; ) {
this.pagesOverview = pagesOverview; this.pdfDocument = pdfDocument;
this.printContainer = printContainer; this.pagesOverview = pagesOverview;
this._printResolution = printResolution || 150; this.printContainer = printContainer;
this._optionalContentConfigPromise = this._printResolution = printResolution || 150;
optionalContentConfigPromise || pdfDocument.getOptionalContentConfig(); this._optionalContentConfigPromise =
this._printAnnotationStoragePromise = optionalContentConfigPromise || pdfDocument.getOptionalContentConfig();
printAnnotationStoragePromise || Promise.resolve(); this._printAnnotationStoragePromise =
} printAnnotationStoragePromise || Promise.resolve();
}
FirefoxPrintService.prototype = {
layout() { layout() {
const { const {
pdfDocument, pdfDocument,
@ -146,16 +146,13 @@ FirefoxPrintService.prototype = {
const body = document.querySelector("body"); const body = document.querySelector("body");
body.setAttribute("data-pdfjsprinting", true); body.setAttribute("data-pdfjsprinting", true);
const hasEqualPageSizes = this.pagesOverview.every(function (size) { const { width, height } = this.pagesOverview[0];
return ( const hasEqualPageSizes = this.pagesOverview.every(
size.width === this.pagesOverview[0].width && size => size.width === width && size.height === height
size.height === this.pagesOverview[0].height );
);
}, this);
if (!hasEqualPageSizes) { if (!hasEqualPageSizes) {
console.warn( console.warn(
"Not all pages have the same size. The printed " + "Not all pages have the same size. The printed result may be incorrect!"
"result may be incorrect!"
); );
} }
@ -163,8 +160,7 @@ FirefoxPrintService.prototype = {
// set. Note that we assume that all pages have the same size, because // set. Note that we assume that all pages have the same size, because
// variable-size pages are scaled down to the initial page size in Firefox. // variable-size pages are scaled down to the initial page size in Firefox.
this.pageStyleSheet = document.createElement("style"); this.pageStyleSheet = document.createElement("style");
const pageSize = this.pagesOverview[0]; this.pageStyleSheet.textContent = `@page { size: ${width}pt ${height}pt;}`;
this.pageStyleSheet.textContent = `@page { size: ${pageSize.width}pt ${pageSize.height}pt;}`;
body.append(this.pageStyleSheet); body.append(this.pageStyleSheet);
if (pdfDocument.isPureXfa) { if (pdfDocument.isPureXfa) {
@ -183,7 +179,7 @@ FirefoxPrintService.prototype = {
_printAnnotationStoragePromise _printAnnotationStoragePromise
); );
} }
}, }
destroy() { destroy() {
this.printContainer.textContent = ""; this.printContainer.textContent = "";
@ -195,8 +191,8 @@ FirefoxPrintService.prototype = {
this.pageStyleSheet.remove(); this.pageStyleSheet.remove();
this.pageStyleSheet = null; this.pageStyleSheet = null;
} }
}, }
}; }
PDFPrintServiceFactory.instance = { PDFPrintServiceFactory.instance = {
get supportsPrinting() { get supportsPrinting() {

View File

@ -62,46 +62,43 @@ function renderPage(
}); });
} }
function PDFPrintService( class PDFPrintService {
pdfDocument, constructor(
pagesOverview, pdfDocument,
printContainer, pagesOverview,
printResolution, printContainer,
optionalContentConfigPromise = null, printResolution,
printAnnotationStoragePromise = null, optionalContentConfigPromise = null,
l10n printAnnotationStoragePromise = null,
) { l10n
this.pdfDocument = pdfDocument; ) {
this.pagesOverview = pagesOverview; this.pdfDocument = pdfDocument;
this.printContainer = printContainer; this.pagesOverview = pagesOverview;
this._printResolution = printResolution || 150; this.printContainer = printContainer;
this._optionalContentConfigPromise = this._printResolution = printResolution || 150;
optionalContentConfigPromise || pdfDocument.getOptionalContentConfig(); this._optionalContentConfigPromise =
this._printAnnotationStoragePromise = optionalContentConfigPromise || pdfDocument.getOptionalContentConfig();
printAnnotationStoragePromise || Promise.resolve(); this._printAnnotationStoragePromise =
this.l10n = l10n; printAnnotationStoragePromise || Promise.resolve();
this.currentPage = -1; this.l10n = l10n;
// The temporary canvas where renderPage paints one page at a time. this.currentPage = -1;
this.scratchCanvas = document.createElement("canvas"); // The temporary canvas where renderPage paints one page at a time.
} this.scratchCanvas = document.createElement("canvas");
}
PDFPrintService.prototype = {
layout() { layout() {
this.throwIfInactive(); this.throwIfInactive();
const body = document.querySelector("body"); const body = document.querySelector("body");
body.setAttribute("data-pdfjsprinting", true); body.setAttribute("data-pdfjsprinting", true);
const hasEqualPageSizes = this.pagesOverview.every(function (size) { const { width, height } = this.pagesOverview[0];
return ( const hasEqualPageSizes = this.pagesOverview.every(
size.width === this.pagesOverview[0].width && size => size.width === width && size.height === height
size.height === this.pagesOverview[0].height );
);
}, this);
if (!hasEqualPageSizes) { if (!hasEqualPageSizes) {
console.warn( console.warn(
"Not all pages have the same size. The printed " + "Not all pages have the same size. The printed result may be incorrect!"
"result may be incorrect!"
); );
} }
@ -115,11 +112,9 @@ PDFPrintService.prototype = {
// will be ignored and the user has to select the correct paper size in // will be ignored and the user has to select the correct paper size in
// the UI if wanted. // the UI if wanted.
this.pageStyleSheet = document.createElement("style"); this.pageStyleSheet = document.createElement("style");
const pageSize = this.pagesOverview[0]; this.pageStyleSheet.textContent = `@page { size: ${width}pt ${height}pt;}`;
this.pageStyleSheet.textContent =
"@page { size: " + pageSize.width + "pt " + pageSize.height + "pt;}";
body.append(this.pageStyleSheet); body.append(this.pageStyleSheet);
}, }
destroy() { destroy() {
if (activeService !== this) { if (activeService !== this) {
@ -144,7 +139,7 @@ PDFPrintService.prototype = {
overlayManager.close(dialog); overlayManager.close(dialog);
} }
}); });
}, }
renderPages() { renderPages() {
if (this.pdfDocument.isPureXfa) { if (this.pdfDocument.isPureXfa) {
@ -177,7 +172,7 @@ PDFPrintService.prototype = {
}, reject); }, reject);
}; };
return new Promise(renderNextPage); return new Promise(renderNextPage);
}, }
useRenderedPage() { useRenderedPage() {
this.throwIfInactive(); this.throwIfInactive();
@ -200,7 +195,7 @@ PDFPrintService.prototype = {
img.onload = resolve; img.onload = resolve;
img.onerror = reject; img.onerror = reject;
}); });
}, }
performPrint() { performPrint() {
this.throwIfInactive(); this.throwIfInactive();
@ -218,18 +213,18 @@ PDFPrintService.prototype = {
setTimeout(resolve, 20); // Tidy-up. setTimeout(resolve, 20); // Tidy-up.
}, 0); }, 0);
}); });
}, }
get active() { get active() {
return this === activeService; return this === activeService;
}, }
throwIfInactive() { throwIfInactive() {
if (!this.active) { if (!this.active) {
throw new Error("This print request was cancelled or completed."); throw new Error("This print request was cancelled or completed.");
} }
}, }
}; }
const print = window.print; const print = window.print;
window.print = function () { window.print = function () {