-
Notifications
You must be signed in to change notification settings - Fork 10.1k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
[api-minor] Use the NodeCanvasFactory
/NodeCMapReaderFactory
classes as defaults in Node.js environments (issue 11900)
#12039
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -47,6 +47,7 @@ import { | |
StatTimer, | ||
} from "./display_utils.js"; | ||
import { FontFaceObject, FontLoader } from "./font_loader.js"; | ||
import { NodeCanvasFactory, NodeCMapReaderFactory } from "./node_utils.js"; | ||
import { apiCompatibilityParams } from "./api_compatibility.js"; | ||
import { CanvasGraphics } from "./canvas.js"; | ||
import { GlobalWorkerOptions } from "./worker_options.js"; | ||
|
@@ -59,6 +60,15 @@ import { WebGLContext } from "./webgl.js"; | |
const DEFAULT_RANGE_CHUNK_SIZE = 65536; // 2^16 = 65536 | ||
const RENDERING_CANCELLED_TIMEOUT = 100; // ms | ||
|
||
const DefaultCanvasFactory = | ||
(typeof PDFJSDev === "undefined" || PDFJSDev.test("GENERIC")) && isNodeJS | ||
? NodeCanvasFactory | ||
: DOMCanvasFactory; | ||
const DefaultCMapReaderFactory = | ||
(typeof PDFJSDev === "undefined" || PDFJSDev.test("GENERIC")) && isNodeJS | ||
? NodeCMapReaderFactory | ||
: DOMCMapReaderFactory; | ||
|
||
/** | ||
* @typedef {function} IPDFStreamFactory | ||
* @param {DocumentInitParameters} params - The document initialization | ||
|
@@ -242,7 +252,8 @@ function getDocument(src) { | |
} | ||
|
||
params.rangeChunkSize = params.rangeChunkSize || DEFAULT_RANGE_CHUNK_SIZE; | ||
params.CMapReaderFactory = params.CMapReaderFactory || DOMCMapReaderFactory; | ||
params.CMapReaderFactory = | ||
params.CMapReaderFactory || DefaultCMapReaderFactory; | ||
params.ignoreErrors = params.stopAtErrors !== true; | ||
params.fontExtraProperties = params.fontExtraProperties === true; | ||
params.pdfBug = params.pdfBug === true; | ||
|
@@ -863,9 +874,9 @@ class PDFDocumentProxy { | |
* just before viewport transform. | ||
* @property {Object} [imageLayer] - An object that has beginLayout, | ||
* endLayout and appendImage functions. | ||
* @property {Object} [canvasFactory] - The factory that will be used | ||
* @property {Object} [canvasFactory] - The factory instance that will be used | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I'm really not happy about the discrepancy between |
||
* when creating canvases. The default value is | ||
* {DOMCanvasFactory}. | ||
* {new DOMCanvasFactory()}. | ||
* @property {Object} [background] - Background to use for the canvas. | ||
* Can use any valid canvas.fillStyle: A DOMString parsed as | ||
* CSS <color> value, a CanvasGradient object (a linear or | ||
|
@@ -1015,7 +1026,7 @@ class PDFPageProxy { | |
intentState.streamReaderCancelTimeout = null; | ||
} | ||
|
||
const canvasFactoryInstance = canvasFactory || new DOMCanvasFactory(); | ||
const canvasFactoryInstance = canvasFactory || new DefaultCanvasFactory(); | ||
const webGLContext = new WebGLContext({ | ||
enable: enableWebGL, | ||
}); | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,65 @@ | ||
/* Copyright 2020 Mozilla Foundation | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
/* globals __non_webpack_require__ */ | ||
/* eslint no-var: error */ | ||
|
||
import { BaseCanvasFactory, BaseCMapReaderFactory } from "./display_utils.js"; | ||
import { isNodeJS } from "../shared/is_node.js"; | ||
import { unreachable } from "../shared/util.js"; | ||
|
||
let NodeCanvasFactory = class { | ||
constructor() { | ||
unreachable("Not implemented: NodeCanvasFactory"); | ||
} | ||
}; | ||
|
||
let NodeCMapReaderFactory = class { | ||
constructor() { | ||
unreachable("Not implemented: NodeCMapReaderFactory"); | ||
} | ||
}; | ||
|
||
if ((typeof PDFJSDev === "undefined" || PDFJSDev.test("GENERIC")) && isNodeJS) { | ||
NodeCanvasFactory = class extends BaseCanvasFactory { | ||
create(width, height) { | ||
if (width <= 0 || height <= 0) { | ||
throw new Error("Invalid canvas size"); | ||
} | ||
const Canvas = __non_webpack_require__("canvas"); | ||
const canvas = Canvas.createCanvas(width, height); | ||
return { | ||
canvas, | ||
context: canvas.getContext("2d"), | ||
}; | ||
} | ||
}; | ||
|
||
NodeCMapReaderFactory = class extends BaseCMapReaderFactory { | ||
_fetchData(url, compressionType) { | ||
return new Promise((resolve, reject) => { | ||
const fs = __non_webpack_require__("fs"); | ||
fs.readFile(url, (error, data) => { | ||
if (error || !data) { | ||
reject(new Error(error)); | ||
return; | ||
} | ||
resolve({ cMapData: new Uint8Array(data), compressionType }); | ||
}); | ||
}); | ||
} | ||
}; | ||
} | ||
|
||
export { NodeCanvasFactory, NodeCMapReaderFactory }; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Maybe we should also export the various canvas/CMap-factories in the API as well, such that we could remove things like e.g.
pdf.js/examples/node/pdf2png/pdf2png.js
Lines 20 to 49 in 9993397
However, I'm not sure if that's (generally) desirable and it also felt somewhat orthogonal to the rest of the patch...