From d10b8509167b26b59b6c968ac3be937b8095ece2 Mon Sep 17 00:00:00 2001 From: Jonas Jenwald Date: Fri, 11 Jun 2021 16:35:42 +0200 Subject: [PATCH 1/3] 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. --- src/display/base_factory.js | 16 +++++++++++++++- src/display/display_utils.js | 11 ++--------- src/display/node_utils.js | 11 ++--------- 3 files changed, 19 insertions(+), 19 deletions(-) diff --git a/src/display/base_factory.js b/src/display/base_factory.js index 0325779a1..69d0954a9 100644 --- a/src/display/base_factory.js +++ b/src/display/base_factory.js @@ -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 { diff --git a/src/display/display_utils.js b/src/display/display_utils.js index eba1a6fbd..7021c6fd4 100644 --- a/src/display/display_utils.js +++ b/src/display/display_utils.js @@ -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; } } diff --git a/src/display/node_utils.js b/src/display/node_utils.js index cf8a387c5..5ea0d42aa 100644 --- a/src/display/node_utils.js +++ b/src/display/node_utils.js @@ -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); } }; From b05a22d01b62aadb0ab496cbf77846f3b1783999 Mon Sep 17 00:00:00 2001 From: Jonas Jenwald Date: Fri, 11 Jun 2021 16:50:57 +0200 Subject: [PATCH 2/3] Re-factor the `DOMSVGFactory` to extend an abstract base class This is first of all consistent with all of the other (similar) factories, and secondly it will also simplify a future addition of a corresponding `NodeSVGFactory` (if that's ever deemed necessary). --- src/display/base_factory.js | 37 ++++++++++++++++++++++++++++++++++++ src/display/display_utils.js | 22 +++------------------ 2 files changed, 40 insertions(+), 19 deletions(-) diff --git a/src/display/base_factory.js b/src/display/base_factory.js index 69d0954a9..23c381e23 100644 --- a/src/display/base_factory.js +++ b/src/display/base_factory.js @@ -136,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, }; diff --git a/src/display/display_utils.js b/src/display/display_utils.js index 7021c6fd4..98b7424b2 100644 --- a/src/display/display_utils.js +++ b/src/display/display_utils.js @@ -26,6 +26,7 @@ import { BaseCanvasFactory, BaseCMapReaderFactory, BaseStandardFontDataFactory, + BaseSVGFactory, } from "./base_factory.js"; const DEFAULT_LINK_REL = "noopener noreferrer nofollow"; @@ -109,25 +110,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); } } From 7b17dc8bfdd1d0e07caa3e8abc9d1b4800d867ba Mon Sep 17 00:00:00 2001 From: Jonas Jenwald Date: Fri, 11 Jun 2021 17:12:55 +0200 Subject: [PATCH 3/3] Re-factor the `fetchData` helper function, in `src/display/display_utils.js` to be asynchronous --- src/display/display_utils.js | 21 ++++++++------------- 1 file changed, 8 insertions(+), 13 deletions(-) diff --git a/src/display/display_utils.js b/src/display/display_utils.js index 98b7424b2..95e9180ae 100644 --- a/src/display/display_utils.js +++ b/src/display/display_utils.js @@ -46,23 +46,18 @@ class DOMCanvasFactory extends BaseCanvasFactory { } } -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.