Merge pull request #13546 from Snuffleupagus/base_factory-tweaks
Re-factor the `DOMCanvasFactory` and `DOMSVGFactory` implementations slightly
This commit is contained in:
commit
c7c59feeaf
@ -23,7 +23,14 @@ class BaseCanvasFactory {
|
|||||||
}
|
}
|
||||||
|
|
||||||
create(width, height) {
|
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) {
|
reset(canvasAndContext, width, height) {
|
||||||
@ -48,6 +55,13 @@ class BaseCanvasFactory {
|
|||||||
canvasAndContext.canvas = null;
|
canvasAndContext.canvas = null;
|
||||||
canvasAndContext.context = null;
|
canvasAndContext.context = null;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @private
|
||||||
|
*/
|
||||||
|
_createCanvas(width, height) {
|
||||||
|
unreachable("Abstract method `_createCanvas` called.");
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
class BaseCMapReaderFactory {
|
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 {
|
export {
|
||||||
BaseCanvasFactory,
|
BaseCanvasFactory,
|
||||||
BaseCMapReaderFactory,
|
BaseCMapReaderFactory,
|
||||||
BaseStandardFontDataFactory,
|
BaseStandardFontDataFactory,
|
||||||
|
BaseSVGFactory,
|
||||||
};
|
};
|
||||||
|
@ -26,6 +26,7 @@ import {
|
|||||||
BaseCanvasFactory,
|
BaseCanvasFactory,
|
||||||
BaseCMapReaderFactory,
|
BaseCMapReaderFactory,
|
||||||
BaseStandardFontDataFactory,
|
BaseStandardFontDataFactory,
|
||||||
|
BaseSVGFactory,
|
||||||
} from "./base_factory.js";
|
} from "./base_factory.js";
|
||||||
|
|
||||||
const DEFAULT_LINK_REL = "noopener noreferrer nofollow";
|
const DEFAULT_LINK_REL = "noopener noreferrer nofollow";
|
||||||
@ -37,38 +38,26 @@ class DOMCanvasFactory extends BaseCanvasFactory {
|
|||||||
this._document = ownerDocument;
|
this._document = ownerDocument;
|
||||||
}
|
}
|
||||||
|
|
||||||
create(width, height) {
|
_createCanvas(width, height) {
|
||||||
if (width <= 0 || height <= 0) {
|
|
||||||
throw new Error("Invalid canvas size");
|
|
||||||
}
|
|
||||||
const canvas = this._document.createElement("canvas");
|
const canvas = this._document.createElement("canvas");
|
||||||
const context = canvas.getContext("2d");
|
|
||||||
canvas.width = width;
|
canvas.width = width;
|
||||||
canvas.height = height;
|
canvas.height = height;
|
||||||
return {
|
return canvas;
|
||||||
canvas,
|
|
||||||
context,
|
|
||||||
};
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
function fetchData(url, asTypedArray) {
|
async function fetchData(url, asTypedArray = false) {
|
||||||
if (
|
if (
|
||||||
(typeof PDFJSDev !== "undefined" && PDFJSDev.test("MOZCENTRAL")) ||
|
(typeof PDFJSDev !== "undefined" && PDFJSDev.test("MOZCENTRAL")) ||
|
||||||
(isFetchSupported() && isValidFetchUrl(url, document.baseURI))
|
(isFetchSupported() && isValidFetchUrl(url, document.baseURI))
|
||||||
) {
|
) {
|
||||||
return fetch(url).then(async response => {
|
const response = await fetch(url);
|
||||||
if (!response.ok) {
|
if (!response.ok) {
|
||||||
throw new Error(response.statusText);
|
throw new Error(response.statusText);
|
||||||
}
|
}
|
||||||
let data;
|
return asTypedArray
|
||||||
if (asTypedArray) {
|
? new Uint8Array(await response.arrayBuffer())
|
||||||
data = new Uint8Array(await response.arrayBuffer());
|
: stringToBytes(await response.text());
|
||||||
} else {
|
|
||||||
data = stringToBytes(await response.text());
|
|
||||||
}
|
|
||||||
return data;
|
|
||||||
});
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// The Fetch API is not supported.
|
// The Fetch API is not supported.
|
||||||
@ -116,25 +105,8 @@ class DOMStandardFontDataFactory extends BaseStandardFontDataFactory {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
class DOMSVGFactory {
|
class DOMSVGFactory extends BaseSVGFactory {
|
||||||
create(width, height) {
|
_createSVG(type) {
|
||||||
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");
|
|
||||||
}
|
|
||||||
return document.createElementNS(SVG_NS, type);
|
return document.createElementNS(SVG_NS, type);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -55,16 +55,9 @@ if ((typeof PDFJSDev === "undefined" || PDFJSDev.test("GENERIC")) && isNodeJS) {
|
|||||||
};
|
};
|
||||||
|
|
||||||
NodeCanvasFactory = class extends BaseCanvasFactory {
|
NodeCanvasFactory = class extends BaseCanvasFactory {
|
||||||
create(width, height) {
|
_createCanvas(width, height) {
|
||||||
if (width <= 0 || height <= 0) {
|
|
||||||
throw new Error("Invalid canvas size");
|
|
||||||
}
|
|
||||||
const Canvas = __non_webpack_require__("canvas");
|
const Canvas = __non_webpack_require__("canvas");
|
||||||
const canvas = Canvas.createCanvas(width, height);
|
return Canvas.createCanvas(width, height);
|
||||||
return {
|
|
||||||
canvas,
|
|
||||||
context: canvas.getContext("2d"),
|
|
||||||
};
|
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user