Merge pull request #12174 from Snuffleupagus/AppOptions-printResolution

Do the `AppOptions.get("printResolution")` lookup once in `web/app.js `, when initializing `PDFPrintServiceFactory`-instances, rather than for every printed page
This commit is contained in:
Tim van der Meij 2020-08-05 22:47:37 +02:00 committed by GitHub
commit a289eb8325
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 74 additions and 15 deletions

View File

@ -1609,10 +1609,13 @@ const PDFViewerApplication = {
const pagesOverview = this.pdfViewer.getPagesOverview(); const pagesOverview = this.pdfViewer.getPagesOverview();
const printContainer = this.appConfig.printContainer; const printContainer = this.appConfig.printContainer;
const printResolution = AppOptions.get("printResolution");
const printService = PDFPrintServiceFactory.instance.createPrintService( const printService = PDFPrintServiceFactory.instance.createPrintService(
this.pdfDocument, this.pdfDocument,
pagesOverview, pagesOverview,
printContainer, printContainer,
printResolution,
this.l10n this.l10n
); );
this.printService = printService; this.printService = printService;

View File

@ -13,18 +13,22 @@
* limitations under the License. * limitations under the License.
*/ */
import { AppOptions } from "./app_options.js";
import { CSS_UNITS } from "./ui_utils.js"; import { CSS_UNITS } from "./ui_utils.js";
import { PDFPrintServiceFactory } from "./app.js"; import { PDFPrintServiceFactory } from "./app.js";
import { shadow } from "pdfjs-lib"; import { shadow } from "pdfjs-lib";
// Creates a placeholder with div and canvas with right size for the page. // 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"); const canvas = document.createElement("canvas");
// The size of the canvas in pixels for printing. // The size of the canvas in pixels for printing.
const PRINT_RESOLUTION = AppOptions.get("printResolution") || 150; const PRINT_UNITS = printResolution / 72.0;
const PRINT_UNITS = PRINT_RESOLUTION / 72.0;
canvas.width = Math.floor(size.width * PRINT_UNITS); canvas.width = Math.floor(size.width * PRINT_UNITS);
canvas.height = Math.floor(size.height * 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.pdfDocument = pdfDocument;
this.pagesOverview = pagesOverview; this.pagesOverview = pagesOverview;
this.printContainer = printContainer; this.printContainer = printContainer;
this._printResolution = printResolution || 150;
} }
FirefoxPrintService.prototype = { FirefoxPrintService.prototype = {
layout() { layout() {
const { pdfDocument, pagesOverview, printContainer } = this; const {
pdfDocument,
pagesOverview,
printContainer,
_printResolution,
} = this;
const body = document.querySelector("body"); const body = document.querySelector("body");
body.setAttribute("data-pdfjsprinting", true); body.setAttribute("data-pdfjsprinting", true);
for (let i = 0, ii = pagesOverview.length; i < ii; ++i) { 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); return shadow(this, "supportsPrinting", value);
}, },
createPrintService(pdfDocument, pagesOverview, printContainer) { createPrintService(
return new FirefoxPrintService(pdfDocument, pagesOverview, printContainer); pdfDocument,
pagesOverview,
printContainer,
printResolution
) {
return new FirefoxPrintService(
pdfDocument,
pagesOverview,
printContainer,
printResolution
);
}, },
}; };

View File

@ -22,12 +22,17 @@ let overlayManager = null;
// Renders the page to the canvas of the given print service, and returns // Renders the page to the canvas of the given print service, and returns
// the suggested dimensions of the output page. // the suggested dimensions of the output page.
function renderPage(activeServiceOnEntry, pdfDocument, pageNumber, size) { function renderPage(
activeServiceOnEntry,
pdfDocument,
pageNumber,
size,
printResolution
) {
const scratchCanvas = activeService.scratchCanvas; const scratchCanvas = activeService.scratchCanvas;
// The size of the canvas in pixels for printing. // The size of the canvas in pixels for printing.
const PRINT_RESOLUTION = AppOptions.get("printResolution") || 150; const PRINT_UNITS = printResolution / 72.0;
const PRINT_UNITS = PRINT_RESOLUTION / 72.0;
scratchCanvas.width = Math.floor(size.width * PRINT_UNITS); scratchCanvas.width = Math.floor(size.width * PRINT_UNITS);
scratchCanvas.height = Math.floor(size.height * 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.pdfDocument = pdfDocument;
this.pagesOverview = pagesOverview; this.pagesOverview = pagesOverview;
this.printContainer = printContainer; this.printContainer = printContainer;
this._printResolution = printResolution || 150;
this.l10n = l10n || NullL10n; this.l10n = l10n || NullL10n;
this.disableCreateObjectURL = AppOptions.get("disableCreateObjectURL"); this.disableCreateObjectURL = AppOptions.get("disableCreateObjectURL");
this.currentPage = -1; this.currentPage = -1;
@ -154,7 +166,13 @@ PDFPrintService.prototype = {
} }
const index = this.currentPage; const index = this.currentPage;
renderProgress(index, pageCount, this.l10n); 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(this.useRenderedPage.bind(this))
.then(function () { .then(function () {
renderNextPage(resolve, reject); renderNextPage(resolve, reject);
@ -347,7 +365,13 @@ function ensureOverlay() {
PDFPrintServiceFactory.instance = { PDFPrintServiceFactory.instance = {
supportsPrinting: true, supportsPrinting: true,
createPrintService(pdfDocument, pagesOverview, printContainer, l10n) { createPrintService(
pdfDocument,
pagesOverview,
printContainer,
printResolution,
l10n
) {
if (activeService) { if (activeService) {
throw new Error("The print service is created and active."); throw new Error("The print service is created and active.");
} }
@ -355,6 +379,7 @@ PDFPrintServiceFactory.instance = {
pdfDocument, pdfDocument,
pagesOverview, pagesOverview,
printContainer, printContainer,
printResolution,
l10n l10n
); );
return activeService; return activeService;