mirror of
https://github.com/mozilla/pdf.js.git
synced 2025-04-23 08:38:06 +02:00
[api-minor] Replace the canvas
package with @napi-rs/canvas
The `@napi-rs/canvas` package has fewer dependencies, which should *hopefully* make installing and using it easier for `pdfjs-dist` end-users. (Over the years we've seen, repeatedly, that `canvas` can be difficult to install successfully.) Furthermore, this package includes more functionality (such as `Path2D`) which reduces the overall number of dependencies in the PDF.js project. One point to note is that `@napi-rs/canvas` is a fair bit newer than `canvas`, and has a lot fewer users, however looking at the commit history it does seem to be actively maintained. Note that I've successfully tested the [Node.js examples](https://github.com/mozilla/pdf.js/tree/master/examples/node), in particular the `pdf2png` one, with this patch applied and things appear to work fine. Please see: - https://www.npmjs.com/package/@napi-rs/canvas - https://github.com/Brooooooklyn/canvas
This commit is contained in:
parent
bff6738966
commit
86f943ca03
7 changed files with 188 additions and 104 deletions
|
@ -31,50 +31,29 @@ if (
|
|||
!PDFJSDev.test("SKIP_BABEL") &&
|
||||
isNodeJS
|
||||
) {
|
||||
let canvas, path2d;
|
||||
let canvas;
|
||||
try {
|
||||
const require = process
|
||||
.getBuiltinModule("module")
|
||||
.createRequire(import.meta.url);
|
||||
|
||||
try {
|
||||
canvas = require("canvas");
|
||||
canvas = require("@napi-rs/canvas");
|
||||
} catch (ex) {
|
||||
warn(`Cannot load "canvas" package: "${ex}".`);
|
||||
}
|
||||
try {
|
||||
path2d = require("path2d");
|
||||
} catch (ex) {
|
||||
warn(`Cannot load "path2d" package: "${ex}".`);
|
||||
warn(`Cannot load "@napi-rs/canvas" package: "${ex}".`);
|
||||
}
|
||||
} catch {}
|
||||
|
||||
if (!globalThis.DOMMatrix) {
|
||||
const DOMMatrix = canvas?.DOMMatrix;
|
||||
|
||||
if (DOMMatrix) {
|
||||
globalThis.DOMMatrix = DOMMatrix;
|
||||
if (canvas?.DOMMatrix) {
|
||||
globalThis.DOMMatrix = canvas.DOMMatrix;
|
||||
} else {
|
||||
warn("Cannot polyfill `DOMMatrix`, rendering may be broken.");
|
||||
}
|
||||
}
|
||||
if (!globalThis.Path2D) {
|
||||
const CanvasRenderingContext2D = canvas?.CanvasRenderingContext2D;
|
||||
const applyPath2DToCanvasRenderingContext =
|
||||
path2d?.applyPath2DToCanvasRenderingContext;
|
||||
const Path2D = path2d?.Path2D;
|
||||
|
||||
if (
|
||||
CanvasRenderingContext2D &&
|
||||
applyPath2DToCanvasRenderingContext &&
|
||||
Path2D
|
||||
) {
|
||||
try {
|
||||
applyPath2DToCanvasRenderingContext(CanvasRenderingContext2D);
|
||||
} catch (ex) {
|
||||
warn(`applyPath2DToCanvasRenderingContext: "${ex}".`);
|
||||
}
|
||||
globalThis.Path2D = Path2D;
|
||||
if (canvas?.Path2D) {
|
||||
globalThis.Path2D = canvas.Path2D;
|
||||
} else {
|
||||
warn("Cannot polyfill `Path2D`, rendering may be broken.");
|
||||
}
|
||||
|
@ -97,7 +76,7 @@ class NodeCanvasFactory extends BaseCanvasFactory {
|
|||
const require = process
|
||||
.getBuiltinModule("module")
|
||||
.createRequire(import.meta.url);
|
||||
const canvas = require("canvas");
|
||||
const canvas = require("@napi-rs/canvas");
|
||||
return canvas.createCanvas(width, height);
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue