Skip to content

Commit d47a878

Browse files
committed
Do the AppOptions.get("printResolution") lookup once in web/app.js, when initializing PDFPrintServiceFactory-instances, rather then 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.
1 parent 63e33a5 commit d47a878

File tree

3 files changed

+74
-15
lines changed

3 files changed

+74
-15
lines changed

web/app.js

+3
Original file line numberDiff line numberDiff line change
@@ -1609,10 +1609,13 @@ const PDFViewerApplication = {
16091609

16101610
const pagesOverview = this.pdfViewer.getPagesOverview();
16111611
const printContainer = this.appConfig.printContainer;
1612+
const printResolution = AppOptions.get("printResolution");
1613+
16121614
const printService = PDFPrintServiceFactory.instance.createPrintService(
16131615
this.pdfDocument,
16141616
pagesOverview,
16151617
printContainer,
1618+
printResolution,
16161619
this.l10n
16171620
);
16181621
this.printService = printService;

web/firefox_print_service.js

+40-9
Original file line numberDiff line numberDiff line change
@@ -13,18 +13,22 @@
1313
* limitations under the License.
1414
*/
1515

16-
import { AppOptions } from "./app_options.js";
1716
import { CSS_UNITS } from "./ui_utils.js";
1817
import { PDFPrintServiceFactory } from "./app.js";
1918
import { shadow } from "pdfjs-lib";
2019

2120
// Creates a placeholder with div and canvas with right size for the page.
22-
function composePage(pdfDocument, pageNumber, size, printContainer) {
21+
function composePage(
22+
pdfDocument,
23+
pageNumber,
24+
size,
25+
printContainer,
26+
printResolution
27+
) {
2328
const canvas = document.createElement("canvas");
2429

2530
// The size of the canvas in pixels for printing.
26-
const PRINT_RESOLUTION = AppOptions.get("printResolution") || 150;
27-
const PRINT_UNITS = PRINT_RESOLUTION / 72.0;
31+
const PRINT_UNITS = printResolution / 72.0;
2832
canvas.width = Math.floor(size.width * PRINT_UNITS);
2933
canvas.height = Math.floor(size.height * PRINT_UNITS);
3034

@@ -76,21 +80,38 @@ function composePage(pdfDocument, pageNumber, size, printContainer) {
7680
};
7781
}
7882

79-
function FirefoxPrintService(pdfDocument, pagesOverview, printContainer) {
83+
function FirefoxPrintService(
84+
pdfDocument,
85+
pagesOverview,
86+
printContainer,
87+
printResolution
88+
) {
8089
this.pdfDocument = pdfDocument;
8190
this.pagesOverview = pagesOverview;
8291
this.printContainer = printContainer;
92+
this._printResolution = printResolution || 150;
8393
}
8494

8595
FirefoxPrintService.prototype = {
8696
layout() {
87-
const { pdfDocument, pagesOverview, printContainer } = this;
97+
const {
98+
pdfDocument,
99+
pagesOverview,
100+
printContainer,
101+
_printResolution,
102+
} = this;
88103

89104
const body = document.querySelector("body");
90105
body.setAttribute("data-pdfjsprinting", true);
91106

92107
for (let i = 0, ii = pagesOverview.length; i < ii; ++i) {
93-
composePage(pdfDocument, i + 1, pagesOverview[i], printContainer);
108+
composePage(
109+
pdfDocument,
110+
/* pageNumber = */ i + 1,
111+
pagesOverview[i],
112+
printContainer,
113+
_printResolution
114+
);
94115
}
95116
},
96117

@@ -110,8 +131,18 @@ PDFPrintServiceFactory.instance = {
110131
return shadow(this, "supportsPrinting", value);
111132
},
112133

113-
createPrintService(pdfDocument, pagesOverview, printContainer) {
114-
return new FirefoxPrintService(pdfDocument, pagesOverview, printContainer);
134+
createPrintService(
135+
pdfDocument,
136+
pagesOverview,
137+
printContainer,
138+
printResolution
139+
) {
140+
return new FirefoxPrintService(
141+
pdfDocument,
142+
pagesOverview,
143+
printContainer,
144+
printResolution
145+
);
115146
},
116147
};
117148

web/pdf_print_service.js

+31-6
Original file line numberDiff line numberDiff line change
@@ -22,12 +22,17 @@ let overlayManager = null;
2222

2323
// Renders the page to the canvas of the given print service, and returns
2424
// the suggested dimensions of the output page.
25-
function renderPage(activeServiceOnEntry, pdfDocument, pageNumber, size) {
25+
function renderPage(
26+
activeServiceOnEntry,
27+
pdfDocument,
28+
pageNumber,
29+
size,
30+
printResolution
31+
) {
2632
const scratchCanvas = activeService.scratchCanvas;
2733

2834
// The size of the canvas in pixels for printing.
29-
const PRINT_RESOLUTION = AppOptions.get("printResolution") || 150;
30-
const PRINT_UNITS = PRINT_RESOLUTION / 72.0;
35+
const PRINT_UNITS = printResolution / 72.0;
3136
scratchCanvas.width = Math.floor(size.width * PRINT_UNITS);
3237
scratchCanvas.height = Math.floor(size.height * PRINT_UNITS);
3338

@@ -61,10 +66,17 @@ function renderPage(activeServiceOnEntry, pdfDocument, pageNumber, size) {
6166
});
6267
}
6368

64-
function PDFPrintService(pdfDocument, pagesOverview, printContainer, l10n) {
69+
function PDFPrintService(
70+
pdfDocument,
71+
pagesOverview,
72+
printContainer,
73+
printResolution,
74+
l10n
75+
) {
6576
this.pdfDocument = pdfDocument;
6677
this.pagesOverview = pagesOverview;
6778
this.printContainer = printContainer;
79+
this._printResolution = printResolution || 150;
6880
this.l10n = l10n || NullL10n;
6981
this.disableCreateObjectURL = AppOptions.get("disableCreateObjectURL");
7082
this.currentPage = -1;
@@ -154,7 +166,13 @@ PDFPrintService.prototype = {
154166
}
155167
const index = this.currentPage;
156168
renderProgress(index, pageCount, this.l10n);
157-
renderPage(this, this.pdfDocument, index + 1, this.pagesOverview[index])
169+
renderPage(
170+
this,
171+
this.pdfDocument,
172+
/* pageNumber = */ index + 1,
173+
this.pagesOverview[index],
174+
this._printResolution
175+
)
158176
.then(this.useRenderedPage.bind(this))
159177
.then(function () {
160178
renderNextPage(resolve, reject);
@@ -347,14 +365,21 @@ function ensureOverlay() {
347365
PDFPrintServiceFactory.instance = {
348366
supportsPrinting: true,
349367

350-
createPrintService(pdfDocument, pagesOverview, printContainer, l10n) {
368+
createPrintService(
369+
pdfDocument,
370+
pagesOverview,
371+
printContainer,
372+
printResolution,
373+
l10n
374+
) {
351375
if (activeService) {
352376
throw new Error("The print service is created and active.");
353377
}
354378
activeService = new PDFPrintService(
355379
pdfDocument,
356380
pagesOverview,
357381
printContainer,
382+
printResolution,
358383
l10n
359384
);
360385
return activeService;

0 commit comments

Comments
 (0)