From 3a372fde94fcfe5188af0e182a94bad14876393e Mon Sep 17 00:00:00 2001 From: Jonas Jenwald Date: Sat, 21 Mar 2026 22:53:39 +0100 Subject: [PATCH] [api-minor] Replace the `CMapReaderFactory`, `StandardFontDataFactory`, and `WasmFactory` API options with a single factory/option Currently we have no less than three different, but very similar, factories for reading built-in CMap files, standard font files, and wasm files on the main-thread.[1] These factories were added at different points in time, since I cannot imagine that we'd add essentially three copies of the same code otherwise. Nowadays these factories are often not even used[2], since worker-thread fetching is used whenever possible to improve performance. In particular, they will *only* be used when either: - The PDF.js library runs in Node.js environments. - The user manually sets `useWorkerFetch = false` when calling `getDocument`. - The user provides custom `CMapReaderFactory`, `StandardFontDataFactory`, and/or `WasmFactory` instances when calling `getDocument`. By replacing these factories with *a single* new `BinaryDataFactory` factory/option the number of `getDocument` options are thus reduced, which cannot hurt. This also reduces the total bundle-size of the Firefox PDF Viewer a little bit, and it slightly reduces the number of import maps that need to be maintained. *Please note:* For users that provide custom `CMapReaderFactory`, `StandardFontDataFactory`, and `WasmFactory` instances when calling `getDocument` this will be a breaking change, however it's unlikely that (many) such users exist. (The *internal* format data-format of `CMapReaderFactory` was changed in PR 18951, and there hasn't been a single question/complaint about it in well over a year.) --- [1] Any new functionality could easily lead to more such factories being added in the future, which wouldn't be great. [2] Note that the Firefox PDF Viewer no longer use these factories, since it "forcibly" sets `useWorkerFetch = true` during building. --- gulpfile.mjs | 22 ++----- src/core/evaluator.js | 4 +- src/core/jbig2_ccittFax_wasm.js | 2 +- src/core/jpx.js | 2 +- src/display/api.js | 76 ++++++----------------- src/display/binary_data_factory.js | 79 ++++++++++++++++++++++++ src/display/cmap_reader_factory.js | 63 ------------------- src/display/node_utils.js | 30 ++------- src/display/standard_fontdata_factory.js | 61 ------------------ src/display/stubs.js | 16 ++--- src/display/wasm_factory.js | 59 ------------------ test/components/simple-viewer.html | 4 +- test/unit/annotation_spec.js | 25 ++++---- test/unit/cmap_spec.js | 18 +++--- test/unit/test_utils.js | 25 +++----- test/unit/unit_test.html | 4 +- tsconfig.json | 6 +- web/internal/debugger.html | 4 +- web/viewer-geckoview.html | 4 +- web/viewer.html | 4 +- 20 files changed, 152 insertions(+), 356 deletions(-) create mode 100644 src/display/binary_data_factory.js delete mode 100644 src/display/cmap_reader_factory.js delete mode 100644 src/display/standard_fontdata_factory.js delete mode 100644 src/display/wasm_factory.js diff --git a/gulpfile.mjs b/gulpfile.mjs index c4ade0ec3..8e7ee7fc1 100644 --- a/gulpfile.mjs +++ b/gulpfile.mjs @@ -195,9 +195,7 @@ function createWebpackAlias(defines) { "fluent-dom": "node_modules/@fluent/dom/esm/index.js", }; const libraryAlias = { - "display-cmap_reader_factory": "src/display/stubs.js", - "display-standard_fontdata_factory": "src/display/stubs.js", - "display-wasm_factory": "src/display/stubs.js", + "display-binary_data_factory": "src/display/stubs.js", "display-fetch_stream": "src/display/stubs.js", "display-network": "src/display/stubs.js", "display-node_stream": "src/display/stubs.js", @@ -227,11 +225,8 @@ function createWebpackAlias(defines) { }; if (defines.CHROME) { - libraryAlias["display-cmap_reader_factory"] = - "src/display/cmap_reader_factory.js"; - libraryAlias["display-standard_fontdata_factory"] = - "src/display/standard_fontdata_factory.js"; - libraryAlias["display-wasm_factory"] = "src/display/wasm_factory.js"; + libraryAlias["display-binary_data_factory"] = + "src/display/binary_data_factory.js"; libraryAlias["display-fetch_stream"] = "src/display/fetch_stream.js"; libraryAlias["display-network"] = "src/display/network.js"; @@ -244,11 +239,8 @@ function createWebpackAlias(defines) { // Aliases defined here must also be replicated in the paths section of // the tsconfig.json file for the type generation to work. // In the tsconfig.json files, the .js extension must be omitted. - libraryAlias["display-cmap_reader_factory"] = - "src/display/cmap_reader_factory.js"; - libraryAlias["display-standard_fontdata_factory"] = - "src/display/standard_fontdata_factory.js"; - libraryAlias["display-wasm_factory"] = "src/display/wasm_factory.js"; + libraryAlias["display-binary_data_factory"] = + "src/display/binary_data_factory.js"; libraryAlias["display-fetch_stream"] = "src/display/fetch_stream.js"; libraryAlias["display-network"] = "src/display/network.js"; libraryAlias["display-node_stream"] = "src/display/node_stream.js"; @@ -1547,9 +1539,7 @@ function buildLibHelper(bundleDefines, inputStream, outputDir) { defines: bundleDefines, map: { "pdfjs-lib": "../pdf.js", - "display-cmap_reader_factory": "./cmap_reader_factory.js", - "display-standard_fontdata_factory": "./standard_fontdata_factory.js", - "display-wasm_factory": "./wasm_factory.js", + "display-binary_data_factory": "./binary_data_factory.js", "display-fetch_stream": "./fetch_stream.js", "display-network": "./network.js", "display-node_stream": "./node_stream.js", diff --git a/src/core/evaluator.js b/src/core/evaluator.js index 5fc89afc6..656f2a4e8 100644 --- a/src/core/evaluator.js +++ b/src/core/evaluator.js @@ -416,7 +416,7 @@ class PartialEvaluator { // Get the data on the main-thread instead. data = { cMapData: await this.handler.sendWithPromise("FetchBinaryData", { - type: "cMapReaderFactory", + kind: "cMapUrl", filename: `${name}${this.options.cMapPacked ? ".bcmap" : ""}`, }), isCompressed: this.options.cMapPacked, @@ -459,7 +459,7 @@ class PartialEvaluator { } // Get the data on the main-thread instead. data = await this.handler.sendWithPromise("FetchBinaryData", { - type: "standardFontDataFactory", + kind: "standardFontDataUrl", filename, }); } diff --git a/src/core/jbig2_ccittFax_wasm.js b/src/core/jbig2_ccittFax_wasm.js index 11e5bc5f2..51d652c5b 100644 --- a/src/core/jbig2_ccittFax_wasm.js +++ b/src/core/jbig2_ccittFax_wasm.js @@ -53,7 +53,7 @@ class JBig2CCITTFaxWasmImage { } this.#buffer = await this.#handler.sendWithPromise( "FetchBinaryData", - { type: "wasmFactory", filename } + { kind: "wasmUrl", filename } ); } } diff --git a/src/core/jpx.js b/src/core/jpx.js index 5b2b62ce5..6421643ca 100644 --- a/src/core/jpx.js +++ b/src/core/jpx.js @@ -77,7 +77,7 @@ class JpxImage { } this.#buffer = await this.#handler.sendWithPromise( "FetchBinaryData", - { type: "wasmFactory", filename } + { kind: "wasmUrl", filename } ); } } diff --git a/src/display/api.js b/src/display/api.js index 4f21e7384..13f690708 100644 --- a/src/display/api.js +++ b/src/display/api.js @@ -66,18 +66,14 @@ import { } from "./api_utils.js"; import { MessageHandler, wrapReason } from "../shared/message_handler.js"; import { + NodeBinaryDataFactory, NodeCanvasFactory, - NodeCMapReaderFactory, NodeFilterFactory, - NodeStandardFontDataFactory, - NodeWasmFactory, } from "display-node_utils"; import { CanvasGraphics } from "./canvas.js"; +import { DOMBinaryDataFactory } from "display-binary_data_factory"; import { DOMCanvasFactory } from "./canvas_factory.js"; -import { DOMCMapReaderFactory } from "display-cmap_reader_factory"; import { DOMFilterFactory } from "./filter_factory.js"; -import { DOMStandardFontDataFactory } from "display-standard_fontdata_factory"; -import { DOMWasmFactory } from "display-wasm_factory"; import { GlobalWorkerOptions } from "./worker_options.js"; import { initWebGPUMesh } from "./webgpu_mesh.js"; import { Metadata } from "./metadata.js"; @@ -140,9 +136,6 @@ const RENDERING_CANCELLED_TIMEOUT = 100; // ms * located. Include the trailing slash. * @property {boolean} [cMapPacked] - Specifies if the Adobe CMaps are binary * packed or not. The default value is `true`. - * @property {Object} [CMapReaderFactory] - The factory that will be used when - * reading built-in CMap files. - * The default value is {DOMCMapReaderFactory}. * @property {string} [iccUrl] - The URL where the predefined ICC profiles are * located. Include the trailing slash. * @property {boolean} [useSystemFonts] - When `true`, fonts that aren't @@ -152,18 +145,11 @@ const RENDERING_CANCELLED_TIMEOUT = 100; // ms * regardless of the environment (to prevent completely broken fonts). * @property {string} [standardFontDataUrl] - The URL where the standard font * files are located. Include the trailing slash. - * @property {Object} [StandardFontDataFactory] - The factory that will be used - * when reading the standard font files. - * The default value is {DOMStandardFontDataFactory}. * @property {string} [wasmUrl] - The URL where the wasm files are located. * Include the trailing slash. - * @property {Object} [WasmFactory] - The factory that will be used - * when reading the wasm files. - * The default value is {DOMWasmFactory}. * @property {boolean} [useWorkerFetch] - Enable using the Fetch API in the - * worker-thread when reading CMap and standard font files. When `true`, - * the `CMapReaderFactory`, `StandardFontDataFactory`, and `WasmFactory` - * options are ignored. + * worker-thread when reading built-in CMap files, standard font files, + * and wasm files. If `true`, the `BinaryDataFactory` option is ignored. * The default value is `true` in web environments and `false` in Node.js. * @property {boolean} [useWasm] - Attempt to use WebAssembly in order to * improve e.g. image decoding performance. @@ -235,6 +221,10 @@ const RENDERING_CANCELLED_TIMEOUT = 100; // ms * @property {Object} [FilterFactory] - The factory that will be used to * create SVG filters when rendering some images on the main canvas. * The default value is {DOMFilterFactory}. + * @property {Object} [BinaryDataFactory] - The factory that will be used when + * falling back to reading built-in CMap files, standard font files, + * and wasm files in the main-thread. + * The default value is {DOMBinaryDataFactory}. * @property {boolean} [enableHWA] - Enables hardware acceleration for * rendering. The default value is `false`. * @property {Object} [pagesMapper] - The pages mapper that will be used to map @@ -287,24 +277,9 @@ function getDocument(src = {}) { : null; const cMapUrl = getFactoryUrlProp(src.cMapUrl); const cMapPacked = src.cMapPacked !== false; - const CMapReaderFactory = - src.CMapReaderFactory || - (typeof PDFJSDev !== "undefined" && PDFJSDev.test("GENERIC") && isNodeJS - ? NodeCMapReaderFactory - : DOMCMapReaderFactory); const iccUrl = getFactoryUrlProp(src.iccUrl); const standardFontDataUrl = getFactoryUrlProp(src.standardFontDataUrl); - const StandardFontDataFactory = - src.StandardFontDataFactory || - (typeof PDFJSDev !== "undefined" && PDFJSDev.test("GENERIC") && isNodeJS - ? NodeStandardFontDataFactory - : DOMStandardFontDataFactory); const wasmUrl = getFactoryUrlProp(src.wasmUrl); - const WasmFactory = - src.WasmFactory || - (typeof PDFJSDev !== "undefined" && PDFJSDev.test("GENERIC") && isNodeJS - ? NodeWasmFactory - : DOMWasmFactory); const ignoreErrors = src.stopAtErrors !== true; const maxImageSize = Number.isInteger(src.maxImageSize) && src.maxImageSize > -1 @@ -347,6 +322,11 @@ function getDocument(src = {}) { (typeof PDFJSDev !== "undefined" && PDFJSDev.test("GENERIC") && isNodeJS ? NodeFilterFactory : DOMFilterFactory); + const BinaryDataFactory = + src.BinaryDataFactory || + (typeof PDFJSDev !== "undefined" && PDFJSDev.test("GENERIC") && isNodeJS + ? NodeBinaryDataFactory + : DOMBinaryDataFactory); const enableHWA = src.enableHWA === true; const enableWebGPU = src.enableWebGPU === true; const useWasm = src.useWasm !== false; @@ -362,9 +342,7 @@ function getDocument(src = {}) { ? src.useWorkerFetch : (typeof PDFJSDev !== "undefined" && PDFJSDev.test("MOZCENTRAL")) || !!( - CMapReaderFactory === DOMCMapReaderFactory && - StandardFontDataFactory === DOMStandardFontDataFactory && - WasmFactory === DOMWasmFactory && + BinaryDataFactory === DOMBinaryDataFactory && cMapUrl && cMapPacked && standardFontDataUrl && @@ -388,21 +366,11 @@ function getDocument(src = {}) { const transportFactory = { canvasFactory: new CanvasFactory({ ownerDocument, enableHWA }), filterFactory: new FilterFactory({ docId, ownerDocument }), - cMapReaderFactory: + binaryDataFactory: (typeof PDFJSDev !== "undefined" && PDFJSDev.test("MOZCENTRAL")) || useWorkerFetch ? null - : new CMapReaderFactory({ baseUrl: cMapUrl }), - standardFontDataFactory: - (typeof PDFJSDev !== "undefined" && PDFJSDev.test("MOZCENTRAL")) || - useWorkerFetch - ? null - : new StandardFontDataFactory({ baseUrl: standardFontDataUrl }), - wasmFactory: - (typeof PDFJSDev !== "undefined" && PDFJSDev.test("MOZCENTRAL")) || - useWorkerFetch - ? null - : new WasmFactory({ baseUrl: wasmUrl }), + : new BinaryDataFactory({ cMapUrl, standardFontDataUrl, wasmUrl }), }; if (!worker) { @@ -2474,9 +2442,7 @@ class WorkerTransport { this.canvasFactory = factory.canvasFactory; this.filterFactory = factory.filterFactory; if (typeof PDFJSDev === "undefined" || !PDFJSDev.test("MOZCENTRAL")) { - this.cMapReaderFactory = factory.cMapReaderFactory; - this.standardFontDataFactory = factory.standardFontDataFactory; - this.wasmFactory = factory.wasmFactory; + this.binaryDataFactory = factory.binaryDataFactory; } this.pagesMapper = pagesMapper; @@ -2943,14 +2909,12 @@ class WorkerTransport { if (this.destroyed) { throw new Error("Worker was destroyed."); } - const factory = this[data.type]; - - if (!factory) { + if (!this.binaryDataFactory) { throw new Error( - `${data.type} not initialized, see the \`useWorkerFetch\` parameter.` + "`BinaryDataFactory` not initialized, see the `useWorkerFetch` parameter." ); } - return factory.fetch(data); + return this.binaryDataFactory.fetch(data); }); } } diff --git a/src/display/binary_data_factory.js b/src/display/binary_data_factory.js new file mode 100644 index 000000000..343ac72f0 --- /dev/null +++ b/src/display/binary_data_factory.js @@ -0,0 +1,79 @@ +/* Copyright 2015 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. + */ + +import { stringToBytes, unreachable } from "../shared/util.js"; +import { fetchData } from "./display_utils.js"; + +class BaseBinaryDataFactory { + #errorStr = Object.freeze({ + cMapUrl: "CMap", + standardFontDataUrl: "font", + wasmUrl: "wasm", + }); + + constructor({ cMapUrl = null, standardFontDataUrl = null, wasmUrl = null }) { + if ( + (typeof PDFJSDev === "undefined" || PDFJSDev.test("TESTING")) && + this.constructor === BaseBinaryDataFactory + ) { + unreachable("Cannot initialize BaseBinaryDataFactory."); + } + this.cMapUrl = cMapUrl; + this.standardFontDataUrl = standardFontDataUrl; + this.wasmUrl = wasmUrl; + } + + async fetch({ kind, filename }) { + switch (kind) { + case "cMapUrl": + case "standardFontDataUrl": + case "wasmUrl": + break; + default: + unreachable(`Not implemented: ${kind}`); + } + const baseUrl = this[kind]; + if (!baseUrl) { + throw new Error(`Ensure that the \`${kind}\` API parameter is provided.`); + } + const url = `${baseUrl}${filename}`; + + return this._fetch(url, kind).catch(reason => { + throw new Error(`Unable to load ${this.#errorStr[kind]} data at: ${url}`); + }); + } + + /** + * @ignore + * @returns {Promise} + */ + async _fetch(url, kind) { + unreachable("Abstract method `_fetch` called."); + } +} + +class DOMBinaryDataFactory extends BaseBinaryDataFactory { + /** + * @ignore + */ + async _fetch(url, kind) { + const type = + kind === "cMapUrl" && !url.endsWith(".bcmap") ? "text" : "bytes"; + const data = await fetchData(url, type); + return data instanceof Uint8Array ? data : stringToBytes(data); + } +} + +export { BaseBinaryDataFactory, DOMBinaryDataFactory }; diff --git a/src/display/cmap_reader_factory.js b/src/display/cmap_reader_factory.js deleted file mode 100644 index 03fababc9..000000000 --- a/src/display/cmap_reader_factory.js +++ /dev/null @@ -1,63 +0,0 @@ -/* Copyright 2015 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. - */ - -import { stringToBytes, unreachable } from "../shared/util.js"; -import { fetchData } from "./display_utils.js"; - -class BaseCMapReaderFactory { - constructor({ baseUrl = null }) { - if ( - (typeof PDFJSDev === "undefined" || PDFJSDev.test("TESTING")) && - this.constructor === BaseCMapReaderFactory - ) { - unreachable("Cannot initialize BaseCMapReaderFactory."); - } - this.baseUrl = baseUrl; - } - - async fetch({ filename }) { - if (!this.baseUrl) { - throw new Error("Ensure that the `cMapUrl` API parameter is provided."); - } - const url = `${this.baseUrl}${filename}`; - - return this._fetch(url).catch(reason => { - throw new Error(`Unable to load CMap data at: ${url}`); - }); - } - - /** - * @ignore - * @returns {Promise} - */ - async _fetch(url) { - unreachable("Abstract method `_fetch` called."); - } -} - -class DOMCMapReaderFactory extends BaseCMapReaderFactory { - /** - * @ignore - */ - async _fetch(url) { - const data = await fetchData( - url, - /* type = */ url.endsWith(".bcmap") ? "bytes" : "text" - ); - return data instanceof Uint8Array ? data : stringToBytes(data); - } -} - -export { BaseCMapReaderFactory, DOMCMapReaderFactory }; diff --git a/src/display/node_utils.js b/src/display/node_utils.js index 724cca78b..67e0de0fb 100644 --- a/src/display/node_utils.js +++ b/src/display/node_utils.js @@ -15,11 +15,9 @@ /* globals process */ import { isNodeJS, warn } from "../shared/util.js"; +import { BaseBinaryDataFactory } from "./binary_data_factory.js"; import { BaseCanvasFactory } from "./canvas_factory.js"; -import { BaseCMapReaderFactory } from "./cmap_reader_factory.js"; import { BaseFilterFactory } from "./filter_factory.js"; -import { BaseStandardFontDataFactory } from "./standard_fontdata_factory.js"; -import { BaseWasmFactory } from "./wasm_factory.js"; if (typeof PDFJSDev !== "undefined" && PDFJSDev.test("MOZCENTRAL")) { throw new Error( @@ -98,38 +96,18 @@ class NodeCanvasFactory extends BaseCanvasFactory { } } -class NodeCMapReaderFactory extends BaseCMapReaderFactory { +class NodeBinaryDataFactory extends BaseBinaryDataFactory { /** * @ignore */ - async _fetch(url) { - return fetchData(url); - } -} - -class NodeStandardFontDataFactory extends BaseStandardFontDataFactory { - /** - * @ignore - */ - async _fetch(url) { - return fetchData(url); - } -} - -class NodeWasmFactory extends BaseWasmFactory { - /** - * @ignore - */ - async _fetch(url) { + async _fetch(url, kind) { return fetchData(url); } } export { fetchData, + NodeBinaryDataFactory, NodeCanvasFactory, - NodeCMapReaderFactory, NodeFilterFactory, - NodeStandardFontDataFactory, - NodeWasmFactory, }; diff --git a/src/display/standard_fontdata_factory.js b/src/display/standard_fontdata_factory.js deleted file mode 100644 index 2de484aa8..000000000 --- a/src/display/standard_fontdata_factory.js +++ /dev/null @@ -1,61 +0,0 @@ -/* Copyright 2015 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. - */ - -import { fetchData } from "./display_utils.js"; -import { unreachable } from "../shared/util.js"; - -class BaseStandardFontDataFactory { - constructor({ baseUrl = null }) { - if ( - (typeof PDFJSDev === "undefined" || PDFJSDev.test("TESTING")) && - this.constructor === BaseStandardFontDataFactory - ) { - unreachable("Cannot initialize BaseStandardFontDataFactory."); - } - this.baseUrl = baseUrl; - } - - async fetch({ filename }) { - if (!this.baseUrl) { - throw new Error( - "Ensure that the `standardFontDataUrl` API parameter is provided." - ); - } - const url = `${this.baseUrl}${filename}`; - - return this._fetch(url).catch(reason => { - throw new Error(`Unable to load font data at: ${url}`); - }); - } - - /** - * @ignore - * @returns {Promise} - */ - async _fetch(url) { - unreachable("Abstract method `_fetch` called."); - } -} - -class DOMStandardFontDataFactory extends BaseStandardFontDataFactory { - /** - * @ignore - */ - async _fetch(url) { - return fetchData(url, /* type = */ "bytes"); - } -} - -export { BaseStandardFontDataFactory, DOMStandardFontDataFactory }; diff --git a/src/display/stubs.js b/src/display/stubs.js index beec23c40..80a7c1eca 100644 --- a/src/display/stubs.js +++ b/src/display/stubs.js @@ -13,27 +13,19 @@ * limitations under the License. */ -const DOMCMapReaderFactory = null; -const DOMWasmFactory = null; -const DOMStandardFontDataFactory = null; +const DOMBinaryDataFactory = null; +const NodeBinaryDataFactory = null; const NodeCanvasFactory = null; -const NodeCMapReaderFactory = null; const NodeFilterFactory = null; -const NodeWasmFactory = null; -const NodeStandardFontDataFactory = null; const PDFFetchStream = null; const PDFNetworkStream = null; const PDFNodeStream = null; export { - DOMCMapReaderFactory, - DOMStandardFontDataFactory, - DOMWasmFactory, + DOMBinaryDataFactory, + NodeBinaryDataFactory, NodeCanvasFactory, - NodeCMapReaderFactory, NodeFilterFactory, - NodeStandardFontDataFactory, - NodeWasmFactory, PDFFetchStream, PDFNetworkStream, PDFNodeStream, diff --git a/src/display/wasm_factory.js b/src/display/wasm_factory.js deleted file mode 100644 index c7549505d..000000000 --- a/src/display/wasm_factory.js +++ /dev/null @@ -1,59 +0,0 @@ -/* Copyright 2015 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. - */ - -import { fetchData } from "./display_utils.js"; -import { unreachable } from "../shared/util.js"; - -class BaseWasmFactory { - constructor({ baseUrl = null }) { - if ( - (typeof PDFJSDev === "undefined" || PDFJSDev.test("TESTING")) && - this.constructor === BaseWasmFactory - ) { - unreachable("Cannot initialize BaseWasmFactory."); - } - this.baseUrl = baseUrl; - } - - async fetch({ filename }) { - if (!this.baseUrl) { - throw new Error("Ensure that the `wasmUrl` API parameter is provided."); - } - const url = `${this.baseUrl}${filename}`; - - return this._fetch(url).catch(reason => { - throw new Error(`Unable to load wasm data at: ${url}`); - }); - } - - /** - * @ignore - * @returns {Promise} - */ - async _fetch(url) { - unreachable("Abstract method `_fetch` called."); - } -} - -class DOMWasmFactory extends BaseWasmFactory { - /** - * @ignore - */ - async _fetch(url) { - return fetchData(url, /* type = */ "bytes"); - } -} - -export { BaseWasmFactory, DOMWasmFactory }; diff --git a/test/components/simple-viewer.html b/test/components/simple-viewer.html index 49703137d..6cfa948cd 100644 --- a/test/components/simple-viewer.html +++ b/test/components/simple-viewer.html @@ -43,9 +43,7 @@ limitations under the License. "pdfjs/": "../../src/", "pdfjs-lib": "../../src/pdf.js", - "display-cmap_reader_factory": "../../src/display/cmap_reader_factory.js", - "display-standard_fontdata_factory": "../../src/display/standard_fontdata_factory.js", - "display-wasm_factory": "../../src/display/wasm_factory.js", + "display-binary_data_factory": "../../src/display/binary_data_factory.js", "display-fetch_stream": "../../src/display/fetch_stream.js", "display-network": "../../src/display/network.js", "display-node_stream": "../../src/display/stubs.js", diff --git a/test/unit/annotation_spec.js b/test/unit/annotation_spec.js index db39ad905..820aacced 100644 --- a/test/unit/annotation_spec.js +++ b/test/unit/annotation_spec.js @@ -35,8 +35,7 @@ import { import { CMAP_URL, createIdFactory, - DefaultCMapReaderFactory, - DefaultStandardFontDataFactory, + DefaultBinaryDataFactory, fetchBuiltInCMapHelper, STANDARD_FONT_DATA_URL, XRefMock, @@ -83,10 +82,14 @@ describe("annotation", function () { } } - const fontDataReader = new DefaultStandardFontDataFactory({ - baseUrl: STANDARD_FONT_DATA_URL, + const binaryDataFactory = new DefaultBinaryDataFactory({ + cMapUrl: CMAP_URL, + standardFontDataUrl: STANDARD_FONT_DATA_URL, }); + const fetchBuiltInCMap = name => + fetchBuiltInCMapHelper(binaryDataFactory, /* cMapPacked = */ true, name); + class HandlerMock { constructor() { this.inputs = []; @@ -96,11 +99,11 @@ describe("annotation", function () { this.inputs.push({ name, data }); } - sendWithPromise(name, data) { - if (name !== "FetchStandardFontData") { - return Promise.reject(new Error(`Unsupported mock ${name}.`)); + async sendWithPromise(name, data) { + if (name === "FetchBinaryData") { + return binaryDataFactory.fetch(data); } - return fontDataReader.fetch(data); + throw new Error(`Unsupported mock ${name}.`); } } @@ -114,12 +117,6 @@ describe("annotation", function () { annotationGlobalsMock = await AnnotationFactory.createGlobals(pdfManagerMock); - const CMapReaderFactory = new DefaultCMapReaderFactory({ - baseUrl: CMAP_URL, - }); - const fetchBuiltInCMap = name => - fetchBuiltInCMapHelper(CMapReaderFactory, /* cMapPacked = */ true, name); - const builtInCMapCache = new Map(); for (const name of ["UniJIS-UTF16-H", "Adobe-Japan1-UCS2"]) { builtInCMapCache.set(name, await fetchBuiltInCMap(name)); diff --git a/test/unit/cmap_spec.js b/test/unit/cmap_spec.js index ae1557e5b..2c3762024 100644 --- a/test/unit/cmap_spec.js +++ b/test/unit/cmap_spec.js @@ -16,7 +16,7 @@ import { CMap, CMapFactory, IdentityCMap } from "../../src/core/cmap.js"; import { CMAP_URL, - DefaultCMapReaderFactory, + DefaultBinaryDataFactory, fetchBuiltInCMapHelper, } from "./test_utils.js"; import { Name } from "../../src/core/primitives.js"; @@ -26,12 +26,12 @@ describe("cmap", function () { let fetchBuiltInCMap; beforeAll(function () { - const CMapReaderFactory = new DefaultCMapReaderFactory({ - baseUrl: CMAP_URL, + const binaryDataFactory = new DefaultBinaryDataFactory({ + cMapUrl: CMAP_URL, }); fetchBuiltInCMap = name => - fetchBuiltInCMapHelper(CMapReaderFactory, /* cMapPacked = */ true, name); + fetchBuiltInCMapHelper(binaryDataFactory, /* cMapPacked = */ true, name); }); afterAll(function () { @@ -205,9 +205,9 @@ describe("cmap", function () { it("attempts to load a built-in CMap without the necessary API parameters", async function () { function tmpFetchBuiltInCMap(name) { - const CMapReaderFactory = new DefaultCMapReaderFactory({}); + const binaryDataFactory = new DefaultBinaryDataFactory({}); return fetchBuiltInCMapHelper( - CMapReaderFactory, + binaryDataFactory, /* cMapPacked = */ true, name ); @@ -232,11 +232,11 @@ describe("cmap", function () { it("attempts to load a built-in CMap with inconsistent API parameters", async function () { function tmpFetchBuiltInCMap(name) { - const CMapReaderFactory = new DefaultCMapReaderFactory({ - baseUrl: CMAP_URL, + const binaryDataFactory = new DefaultBinaryDataFactory({ + cMapUrl: CMAP_URL, }); return fetchBuiltInCMapHelper( - CMapReaderFactory, + binaryDataFactory, /* cMapPacked = */ false, name ); diff --git a/test/unit/test_utils.js b/test/unit/test_utils.js index 946b5c516..154845ba7 100644 --- a/test/unit/test_utils.js +++ b/test/unit/test_utils.js @@ -16,13 +16,11 @@ import { assert, isNodeJS } from "../../src/shared/util.js"; import { fetchData as fetchDataNode, - NodeCMapReaderFactory, - NodeStandardFontDataFactory, + NodeBinaryDataFactory, } from "../../src/display/node_utils.js"; import { NullStream, StringStream } from "../../src/core/stream.js"; import { Page, PDFDocument } from "../../src/core/document.js"; -import { DOMCMapReaderFactory } from "../../src/display/cmap_reader_factory.js"; -import { DOMStandardFontDataFactory } from "../../src/display/standard_fontdata_factory.js"; +import { DOMBinaryDataFactory } from "../../src/display/binary_data_factory.js"; import { fetchData as fetchDataDOM } from "../../src/display/display_utils.js"; import { Ref } from "../../src/core/primitives.js"; @@ -45,19 +43,15 @@ class DefaultFileReaderFactory { } } -const DefaultCMapReaderFactory = +const DefaultBinaryDataFactory = typeof PDFJSDev !== "undefined" && PDFJSDev.test("GENERIC") && isNodeJS - ? NodeCMapReaderFactory - : DOMCMapReaderFactory; + ? NodeBinaryDataFactory + : DOMBinaryDataFactory; -const DefaultStandardFontDataFactory = - typeof PDFJSDev !== "undefined" && PDFJSDev.test("GENERIC") && isNodeJS - ? NodeStandardFontDataFactory - : DOMStandardFontDataFactory; - -async function fetchBuiltInCMapHelper(cMapReaderFactory, cMapPacked, name) { +async function fetchBuiltInCMapHelper(binaryDataFactory, cMapPacked, name) { return { - cMapData: await cMapReaderFactory.fetch({ + cMapData: await binaryDataFactory.fetch({ + kind: "cMapUrl", filename: `${name}${cMapPacked ? ".bcmap" : ""}`, }), isCompressed: cMapPacked, @@ -258,9 +252,8 @@ export { buildGetDocumentParams, CMAP_URL, createIdFactory, - DefaultCMapReaderFactory, + DefaultBinaryDataFactory, DefaultFileReaderFactory, - DefaultStandardFontDataFactory, fetchBuiltInCMapHelper, getCrossOriginHostname, STANDARD_FONT_DATA_URL, diff --git a/test/unit/unit_test.html b/test/unit/unit_test.html index 947d56bf3..05afda18c 100644 --- a/test/unit/unit_test.html +++ b/test/unit/unit_test.html @@ -20,9 +20,7 @@ "fluent-dom": "../../node_modules/@fluent/dom/esm/index.js", "cached-iterable": "../../node_modules/cached-iterable/src/index.mjs", - "display-cmap_reader_factory": "../../src/display/cmap_reader_factory.js", - "display-standard_fontdata_factory": "../../src/display/standard_fontdata_factory.js", - "display-wasm_factory": "../../src/display/wasm_factory.js", + "display-binary_data_factory": "../../src/display/binary_data_factory.js", "display-fetch_stream": "../../src/display/fetch_stream.js", "display-network": "../../src/display/network.js", "display-node_stream": "../../src/display/stubs.js", diff --git a/tsconfig.json b/tsconfig.json index e5368ec25..357a5982b 100644 --- a/tsconfig.json +++ b/tsconfig.json @@ -10,11 +10,7 @@ "moduleResolution": "node", "paths": { "pdfjs-lib": ["./src/pdf"], - "display-cmap_reader_factory": ["./src/display/cmap_reader_factory"], - "display-standard_fontdata_factory": [ - "./src/display/standard_fontdata_factory" - ], - "display-wasm_factory": ["./src/display/wasm_factory"], + "display-binary_data_factory": ["./src/display/binary_data_factory"], "display-fetch_stream": ["./src/display/fetch_stream"], "display-network": ["./src/display/network"], "display-node_stream": ["./src/display/node_stream"], diff --git a/web/internal/debugger.html b/web/internal/debugger.html index 6500b32e2..07985e109 100644 --- a/web/internal/debugger.html +++ b/web/internal/debugger.html @@ -126,9 +126,7 @@ limitations under the License. "pdfjs/": "../../src/", "pdfjs-lib": "../../src/pdf.js", - "display-cmap_reader_factory": "../../src/display/cmap_reader_factory.js", - "display-standard_fontdata_factory": "../../src/display/standard_fontdata_factory.js", - "display-wasm_factory": "../../src/display/wasm_factory.js", + "display-binary_data_factory": "../../src/display/binary_data_factory.js", "display-fetch_stream": "../../src/display/fetch_stream.js", "display-network": "../../src/display/network.js", "display-node_stream": "../../src/display/stubs.js", diff --git a/web/viewer-geckoview.html b/web/viewer-geckoview.html index 315a3df0f..06911da2c 100644 --- a/web/viewer-geckoview.html +++ b/web/viewer-geckoview.html @@ -59,9 +59,7 @@ See https://github.com/adobe-type-tools/cmap-resources "fluent-dom": "../node_modules/@fluent/dom/esm/index.js", "cached-iterable": "../node_modules/cached-iterable/src/index.mjs", - "display-cmap_reader_factory": "../src/display/cmap_reader_factory.js", - "display-standard_fontdata_factory": "../src/display/standard_fontdata_factory.js", - "display-wasm_factory": "../src/display/wasm_factory.js", + "display-binary_data_factory": "../src/display/binary_data_factory.js", "display-fetch_stream": "../src/display/fetch_stream.js", "display-network": "../src/display/network.js", "display-node_stream": "../src/display/stubs.js", diff --git a/web/viewer.html b/web/viewer.html index 5e4ef6e1b..bf7cc3782 100644 --- a/web/viewer.html +++ b/web/viewer.html @@ -62,9 +62,7 @@ See https://github.com/adobe-type-tools/cmap-resources "fluent-dom": "../node_modules/@fluent/dom/esm/index.js", "cached-iterable": "../node_modules/cached-iterable/src/index.mjs", - "display-cmap_reader_factory": "../src/display/cmap_reader_factory.js", - "display-standard_fontdata_factory": "../src/display/standard_fontdata_factory.js", - "display-wasm_factory": "../src/display/wasm_factory.js", + "display-binary_data_factory": "../src/display/binary_data_factory.js", "display-fetch_stream": "../src/display/fetch_stream.js", "display-network": "../src/display/network.js", "display-node_stream": "../src/display/stubs.js",