Merge pull request #13546 from Snuffleupagus/base_factory-tweaks

Re-factor the `DOMCanvasFactory` and `DOMSVGFactory` implementations slightly
This commit is contained in:
Tim van der Meij 2021-06-11 21:19:11 +02:00 committed by GitHub
commit c7c59feeaf
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 67 additions and 51 deletions

View File

@ -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 {
@ -122,8 +136,45 @@ class BaseStandardFontDataFactory {
}
}
class BaseSVGFactory {
constructor() {
if (this.constructor === BaseSVGFactory) {
unreachable("Cannot initialize BaseSVGFactory.");
}
}
create(width, height) {
if (width <= 0 || height <= 0) {
throw new Error("Invalid SVG dimensions");
}
const svg = this._createSVG("svg:svg");
svg.setAttribute("version", "1.1");
svg.setAttribute("width", `${width}px`);
svg.setAttribute("height", `${height}px`);
svg.setAttribute("preserveAspectRatio", "none");
svg.setAttribute("viewBox", `0 0 ${width} ${height}`);
return svg;
}
createElement(type) {
if (typeof type !== "string") {
throw new Error("Invalid SVG element type");
}
return this._createSVG(type);
}
/**
* @private
*/
_createSVG(type) {
unreachable("Abstract method `_createSVG` called.");
}
}
export {
BaseCanvasFactory,
BaseCMapReaderFactory,
BaseStandardFontDataFactory,
BaseSVGFactory,
};

View File

@ -26,6 +26,7 @@ import {
BaseCanvasFactory,
BaseCMapReaderFactory,
BaseStandardFontDataFactory,
BaseSVGFactory,
} from "./base_factory.js";
const DEFAULT_LINK_REL = "noopener noreferrer nofollow";
@ -37,38 +38,26 @@ 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;
}
}
function fetchData(url, asTypedArray) {
async function fetchData(url, asTypedArray = false) {
if (
(typeof PDFJSDev !== "undefined" && PDFJSDev.test("MOZCENTRAL")) ||
(isFetchSupported() && isValidFetchUrl(url, document.baseURI))
) {
return fetch(url).then(async response => {
if (!response.ok) {
throw new Error(response.statusText);
}
let data;
if (asTypedArray) {
data = new Uint8Array(await response.arrayBuffer());
} else {
data = stringToBytes(await response.text());
}
return data;
});
const response = await fetch(url);
if (!response.ok) {
throw new Error(response.statusText);
}
return asTypedArray
? new Uint8Array(await response.arrayBuffer())
: stringToBytes(await response.text());
}
// The Fetch API is not supported.
@ -116,25 +105,8 @@ class DOMStandardFontDataFactory extends BaseStandardFontDataFactory {
}
}
class DOMSVGFactory {
create(width, height) {
if (width <= 0 || height <= 0) {
throw new Error("Invalid SVG dimensions");
}
const svg = document.createElementNS(SVG_NS, "svg:svg");
svg.setAttribute("version", "1.1");
svg.setAttribute("width", width + "px");
svg.setAttribute("height", height + "px");
svg.setAttribute("preserveAspectRatio", "none");
svg.setAttribute("viewBox", "0 0 " + width + " " + height);
return svg;
}
createElement(type) {
if (typeof type !== "string") {
throw new Error("Invalid SVG element type");
}
class DOMSVGFactory extends BaseSVGFactory {
_createSVG(type) {
return document.createElementNS(SVG_NS, type);
}
}

View File

@ -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);
}
};