From 97d796e3728f94e5b124724ea0eaffc701f03359 Mon Sep 17 00:00:00 2001 From: Jonas Jenwald Date: Wed, 5 Aug 2020 13:24:32 +0200 Subject: [PATCH] Do the `AppOptions.get("printResolution")` lookup once in `web/app.js`, when initializing `PDFPrintServiceFactory`-instances, rather than for every printed page There's really no point in repeating these lookups over and over, since the value should be constant for the duration of one print invocation anyway. --- web/app.js | 3 +++ web/firefox_print_service.js | 49 +++++++++++++++++++++++++++++------- web/pdf_print_service.js | 37 ++++++++++++++++++++++----- 3 files changed, 74 insertions(+), 15 deletions(-) diff --git a/web/app.js b/web/app.js index e84a2af22b78a..1dc3859db8c4e 100644 --- a/web/app.js +++ b/web/app.js @@ -1609,10 +1609,13 @@ const PDFViewerApplication = { const pagesOverview = this.pdfViewer.getPagesOverview(); const printContainer = this.appConfig.printContainer; + const printResolution = AppOptions.get("printResolution"); + const printService = PDFPrintServiceFactory.instance.createPrintService( this.pdfDocument, pagesOverview, printContainer, + printResolution, this.l10n ); this.printService = printService; diff --git a/web/firefox_print_service.js b/web/firefox_print_service.js index d293140ad1802..11cd0f1312b85 100644 --- a/web/firefox_print_service.js +++ b/web/firefox_print_service.js @@ -13,18 +13,22 @@ * limitations under the License. */ -import { AppOptions } from "./app_options.js"; import { CSS_UNITS } from "./ui_utils.js"; import { PDFPrintServiceFactory } from "./app.js"; import { shadow } from "pdfjs-lib"; // Creates a placeholder with div and canvas with right size for the page. -function composePage(pdfDocument, pageNumber, size, printContainer) { +function composePage( + pdfDocument, + pageNumber, + size, + printContainer, + printResolution +) { const canvas = document.createElement("canvas"); // The size of the canvas in pixels for printing. - const PRINT_RESOLUTION = AppOptions.get("printResolution") || 150; - const PRINT_UNITS = PRINT_RESOLUTION / 72.0; + const PRINT_UNITS = printResolution / 72.0; canvas.width = Math.floor(size.width * PRINT_UNITS); canvas.height = Math.floor(size.height * PRINT_UNITS); @@ -76,21 +80,38 @@ function composePage(pdfDocument, pageNumber, size, printContainer) { }; } -function FirefoxPrintService(pdfDocument, pagesOverview, printContainer) { +function FirefoxPrintService( + pdfDocument, + pagesOverview, + printContainer, + printResolution +) { this.pdfDocument = pdfDocument; this.pagesOverview = pagesOverview; this.printContainer = printContainer; + this._printResolution = printResolution || 150; } FirefoxPrintService.prototype = { layout() { - const { pdfDocument, pagesOverview, printContainer } = this; + const { + pdfDocument, + pagesOverview, + printContainer, + _printResolution, + } = this; const body = document.querySelector("body"); body.setAttribute("data-pdfjsprinting", true); for (let i = 0, ii = pagesOverview.length; i < ii; ++i) { - composePage(pdfDocument, i + 1, pagesOverview[i], printContainer); + composePage( + pdfDocument, + /* pageNumber = */ i + 1, + pagesOverview[i], + printContainer, + _printResolution + ); } }, @@ -110,8 +131,18 @@ PDFPrintServiceFactory.instance = { return shadow(this, "supportsPrinting", value); }, - createPrintService(pdfDocument, pagesOverview, printContainer) { - return new FirefoxPrintService(pdfDocument, pagesOverview, printContainer); + createPrintService( + pdfDocument, + pagesOverview, + printContainer, + printResolution + ) { + return new FirefoxPrintService( + pdfDocument, + pagesOverview, + printContainer, + printResolution + ); }, }; diff --git a/web/pdf_print_service.js b/web/pdf_print_service.js index 6d0b4a368e4db..ebde93a0ee9c1 100644 --- a/web/pdf_print_service.js +++ b/web/pdf_print_service.js @@ -22,12 +22,17 @@ let overlayManager = null; // Renders the page to the canvas of the given print service, and returns // the suggested dimensions of the output page. -function renderPage(activeServiceOnEntry, pdfDocument, pageNumber, size) { +function renderPage( + activeServiceOnEntry, + pdfDocument, + pageNumber, + size, + printResolution +) { const scratchCanvas = activeService.scratchCanvas; // The size of the canvas in pixels for printing. - const PRINT_RESOLUTION = AppOptions.get("printResolution") || 150; - const PRINT_UNITS = PRINT_RESOLUTION / 72.0; + const PRINT_UNITS = printResolution / 72.0; scratchCanvas.width = Math.floor(size.width * PRINT_UNITS); scratchCanvas.height = Math.floor(size.height * PRINT_UNITS); @@ -61,10 +66,17 @@ function renderPage(activeServiceOnEntry, pdfDocument, pageNumber, size) { }); } -function PDFPrintService(pdfDocument, pagesOverview, printContainer, l10n) { +function PDFPrintService( + pdfDocument, + pagesOverview, + printContainer, + printResolution, + l10n +) { this.pdfDocument = pdfDocument; this.pagesOverview = pagesOverview; this.printContainer = printContainer; + this._printResolution = printResolution || 150; this.l10n = l10n || NullL10n; this.disableCreateObjectURL = AppOptions.get("disableCreateObjectURL"); this.currentPage = -1; @@ -154,7 +166,13 @@ PDFPrintService.prototype = { } const index = this.currentPage; renderProgress(index, pageCount, this.l10n); - renderPage(this, this.pdfDocument, index + 1, this.pagesOverview[index]) + renderPage( + this, + this.pdfDocument, + /* pageNumber = */ index + 1, + this.pagesOverview[index], + this._printResolution + ) .then(this.useRenderedPage.bind(this)) .then(function () { renderNextPage(resolve, reject); @@ -347,7 +365,13 @@ function ensureOverlay() { PDFPrintServiceFactory.instance = { supportsPrinting: true, - createPrintService(pdfDocument, pagesOverview, printContainer, l10n) { + createPrintService( + pdfDocument, + pagesOverview, + printContainer, + printResolution, + l10n + ) { if (activeService) { throw new Error("The print service is created and active."); } @@ -355,6 +379,7 @@ PDFPrintServiceFactory.instance = { pdfDocument, pagesOverview, printContainer, + printResolution, l10n ); return activeService;