Skip to content

Commit

Permalink
Update the Path2D polyfill for Node.js environments
Browse files Browse the repository at this point in the history
The polyfill that we use was recently split into two packages, and it now consists of a "core" package and a browser-specific package that build upon the former.
Hence we need to update to use the "core" package, and slightly tweak the code that loads/initializes the polyfill; see also https://www.npmjs.com/package/path2d

This patch was tested successfully with the [pdf2png example](https://github.com/mozilla/pdf.js/tree/master/examples/node/pdf2png), after running `gulp dist-install` locally, using [this PDF document](https://bug810214.bmoattachments.org/attachment.cgi?id=9254990) which contains Type3-fonts that render using `Path2D`.
  • Loading branch information
Snuffleupagus committed Mar 24, 2024
1 parent e7203f5 commit dc0df0a
Show file tree
Hide file tree
Showing 4 changed files with 24 additions and 24 deletions.
2 changes: 1 addition & 1 deletion gulpfile.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -2155,7 +2155,7 @@ function packageJson() {
license: DIST_LICENSE,
optionalDependencies: {
canvas: "^2.11.2",
"path2d-polyfill": "^2.0.1",
path2d: "^0.1.2",
},
browser: {
canvas: false,
Expand Down
26 changes: 10 additions & 16 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@
"merge-stream": "^2.0.0",
"mkdirp": "^3.0.1",
"needle": "^3.3.1",
"path2d-polyfill": "^2.1.1",
"path2d": "^0.1.2",
"pngjs": "^7.0.0",
"postcss": "^8.4.35",
"postcss-dark-theme-class": "^1.2.1",
Expand Down
18 changes: 12 additions & 6 deletions src/display/node_utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ if (typeof PDFJSDev !== "undefined" && PDFJSDev.test("MOZCENTRAL")) {
);
}

let fs, canvas, path2d_polyfill;
let fs, canvas, path2d;
if (isNodeJS) {
// Native packages.
fs = await __non_webpack_import__("fs");
Expand All @@ -36,7 +36,7 @@ if (isNodeJS) {
canvas = await __non_webpack_import__("canvas");
} catch {}
try {
path2d_polyfill = await __non_webpack_import__("path2d-polyfill");
path2d = await __non_webpack_import__("path2d");
} catch {}
}

Expand All @@ -59,11 +59,17 @@ if (typeof PDFJSDev !== "undefined" && !PDFJSDev.test("SKIP_BABEL")) {
return;
}
const CanvasRenderingContext2D = canvas?.CanvasRenderingContext2D;
const polyfillPath2D = path2d_polyfill?.polyfillPath2D;
const applyPath2DToCanvasRenderingContext =
path2d?.applyPath2DToCanvasRenderingContext;
const Path2D = path2d?.Path2D;

if (CanvasRenderingContext2D && polyfillPath2D) {
globalThis.CanvasRenderingContext2D = CanvasRenderingContext2D;
polyfillPath2D(globalThis);
if (
CanvasRenderingContext2D &&
applyPath2DToCanvasRenderingContext &&
Path2D
) {
applyPath2DToCanvasRenderingContext(CanvasRenderingContext2D);
globalThis.Path2D = Path2D;
} else {
warn("Cannot polyfill `Path2D`, rendering may be broken.");
}
Expand Down

0 comments on commit dc0df0a

Please sign in to comment.