Skip to content

Commit

Permalink
Merge pull request #12174 from Snuffleupagus/AppOptions-printResolution
Browse files Browse the repository at this point in the history
Do the `AppOptions.get("printResolution")` lookup once in `web/app.js `, when initializing `PDFPrintServiceFactory`-instances, rather than for every printed page
  • Loading branch information
timvandermeij authored Aug 5, 2020
2 parents 66d5345 + 97d796e commit a289eb8
Show file tree
Hide file tree
Showing 3 changed files with 74 additions and 15 deletions.
3 changes: 3 additions & 0 deletions web/app.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
49 changes: 40 additions & 9 deletions web/firefox_print_service.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);

Expand Down Expand Up @@ -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
);
}
},

Expand All @@ -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
);
},
};

Expand Down
37 changes: 31 additions & 6 deletions web/pdf_print_service.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);

Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -347,14 +365,21 @@ 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.");
}
activeService = new PDFPrintService(
pdfDocument,
pagesOverview,
printContainer,
printResolution,
l10n
);
return activeService;
Expand Down

0 comments on commit a289eb8

Please sign in to comment.