1
0
Fork 0
mirror of https://github.com/mozilla/pdf.js.git synced 2025-04-22 16:18:08 +02:00

Polyfill Path2D in Node.js environments

Until just recently the only existing `Path2D` polyfill didn't have support for Node.js and/or the `node-canvas` package. Given that this was just fixed, in the latest version, we can now finally remove our inline-checks at the relevant call-sites; please also see https://github.com/nilzona/path2d-polyfill#usage-with-node-canvas
This commit is contained in:
Jonas Jenwald 2023-01-22 17:01:06 +01:00
parent cb5a28ceca
commit cf0369d622
6 changed files with 237 additions and 2462 deletions

View file

@ -38,7 +38,6 @@ import {
TilingPattern,
} from "./pattern_helper.js";
import { applyMaskImageData } from "../shared/image_utils.js";
import { isNodeJS } from "../shared/is_node.js";
// <canvas> contexts store most of the state we need natively.
// However, PDF needs a bit more state, which we store here.
@ -55,13 +54,7 @@ const EXECUTION_TIME = 15; // ms
const EXECUTION_STEPS = 10;
// To disable Type3 compilation, set the value to `-1`.
const MAX_SIZE_TO_COMPILE =
typeof PDFJSDev !== "undefined" &&
PDFJSDev.test("GENERIC") &&
isNodeJS &&
typeof Path2D === "undefined"
? -1
: 1000;
const MAX_SIZE_TO_COMPILE = 1000;
const FULL_CHUNK_HEIGHT = 16;

View file

@ -21,7 +21,6 @@ import {
Util,
} from "../shared/util.js";
import { getCurrentTransform } from "./display_utils.js";
import { isNodeJS } from "../shared/is_node.js";
const PathType = {
FILL: "Fill",
@ -30,7 +29,7 @@ const PathType = {
};
function applyBoundingBox(ctx, bbox) {
if (!bbox || isNodeJS) {
if (!bbox) {
return;
}
const width = bbox[2] - bbox[0];

View file

@ -46,6 +46,18 @@ import { isNodeJS } from "./is_node.js";
globalThis.DOMMatrix = __non_webpack_require__("canvas").DOMMatrix;
})();
// Support: Node.js
(function checkPath2D() {
if (globalThis.Path2D || !isNodeJS) {
return;
}
const { CanvasRenderingContext2D } = __non_webpack_require__("canvas");
const { polyfillPath2D } = __non_webpack_require__("path2d-polyfill");
globalThis.CanvasRenderingContext2D = CanvasRenderingContext2D;
polyfillPath2D(globalThis);
})();
// Support: Node.js
(function checkReadableStream() {
if (globalThis.ReadableStream || !isNodeJS) {