Move most functionality in the create
methods into the BaseCanvasFactory
This *slightly* reduces the amount of code duplication in the `DOMCanvasFactory.create` and `NodeCanvasFactory.create` methods.
This commit is contained in:
parent
7b4fa0a038
commit
d10b850916
@ -23,7 +23,14 @@ class BaseCanvasFactory {
|
||||
}
|
||||
|
||||
create(width, height) {
|
||||
unreachable("Abstract method `create` called.");
|
||||
if (width <= 0 || height <= 0) {
|
||||
throw new Error("Invalid canvas size");
|
||||
}
|
||||
const canvas = this._createCanvas(width, height);
|
||||
return {
|
||||
canvas,
|
||||
context: canvas.getContext("2d"),
|
||||
};
|
||||
}
|
||||
|
||||
reset(canvasAndContext, width, height) {
|
||||
@ -48,6 +55,13 @@ class BaseCanvasFactory {
|
||||
canvasAndContext.canvas = null;
|
||||
canvasAndContext.context = null;
|
||||
}
|
||||
|
||||
/**
|
||||
* @private
|
||||
*/
|
||||
_createCanvas(width, height) {
|
||||
unreachable("Abstract method `_createCanvas` called.");
|
||||
}
|
||||
}
|
||||
|
||||
class BaseCMapReaderFactory {
|
||||
|
@ -37,18 +37,11 @@ class DOMCanvasFactory extends BaseCanvasFactory {
|
||||
this._document = ownerDocument;
|
||||
}
|
||||
|
||||
create(width, height) {
|
||||
if (width <= 0 || height <= 0) {
|
||||
throw new Error("Invalid canvas size");
|
||||
}
|
||||
_createCanvas(width, height) {
|
||||
const canvas = this._document.createElement("canvas");
|
||||
const context = canvas.getContext("2d");
|
||||
canvas.width = width;
|
||||
canvas.height = height;
|
||||
return {
|
||||
canvas,
|
||||
context,
|
||||
};
|
||||
return canvas;
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -55,16 +55,9 @@ 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");
|
||||
}
|
||||
_createCanvas(width, height) {
|
||||
const Canvas = __non_webpack_require__("canvas");
|
||||
const canvas = Canvas.createCanvas(width, height);
|
||||
return {
|
||||
canvas,
|
||||
context: canvas.getContext("2d"),
|
||||
};
|
||||
return Canvas.createCanvas(width, height);
|
||||
}
|
||||
};
|
||||
|
||||
|
Loading…
x
Reference in New Issue
Block a user