mirror of
https://github.com/mozilla/pdf.js.git
synced 2026-06-08 03:01:03 +02:00
Merge pull request #20949 from Snuffleupagus/BinaryDataFactory-2
[api-minor] Replace the `CMapReaderFactory`, `StandardFontDataFactory`, and `WasmFactory` API options with a single factory/option
This commit is contained in:
commit
1756b48417
22
gulpfile.mjs
22
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",
|
||||
|
||||
@ -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,
|
||||
});
|
||||
}
|
||||
|
||||
@ -53,7 +53,7 @@ class JBig2CCITTFaxWasmImage {
|
||||
}
|
||||
this.#buffer = await this.#handler.sendWithPromise(
|
||||
"FetchBinaryData",
|
||||
{ type: "wasmFactory", filename }
|
||||
{ kind: "wasmUrl", filename }
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
@ -77,7 +77,7 @@ class JpxImage {
|
||||
}
|
||||
this.#buffer = await this.#handler.sendWithPromise(
|
||||
"FetchBinaryData",
|
||||
{ type: "wasmFactory", filename }
|
||||
{ kind: "wasmUrl", filename }
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
@ -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);
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
79
src/display/binary_data_factory.js
Normal file
79
src/display/binary_data_factory.js
Normal file
@ -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<Uint8Array>}
|
||||
*/
|
||||
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 };
|
||||
@ -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<Uint8Array>}
|
||||
*/
|
||||
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 };
|
||||
@ -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,
|
||||
};
|
||||
|
||||
@ -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<Uint8Array>}
|
||||
*/
|
||||
async _fetch(url) {
|
||||
unreachable("Abstract method `_fetch` called.");
|
||||
}
|
||||
}
|
||||
|
||||
class DOMStandardFontDataFactory extends BaseStandardFontDataFactory {
|
||||
/**
|
||||
* @ignore
|
||||
*/
|
||||
async _fetch(url) {
|
||||
return fetchData(url, /* type = */ "bytes");
|
||||
}
|
||||
}
|
||||
|
||||
export { BaseStandardFontDataFactory, DOMStandardFontDataFactory };
|
||||
@ -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,
|
||||
|
||||
@ -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<Uint8Array>}
|
||||
*/
|
||||
async _fetch(url) {
|
||||
unreachable("Abstract method `_fetch` called.");
|
||||
}
|
||||
}
|
||||
|
||||
class DOMWasmFactory extends BaseWasmFactory {
|
||||
/**
|
||||
* @ignore
|
||||
*/
|
||||
async _fetch(url) {
|
||||
return fetchData(url, /* type = */ "bytes");
|
||||
}
|
||||
}
|
||||
|
||||
export { BaseWasmFactory, DOMWasmFactory };
|
||||
@ -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",
|
||||
|
||||
@ -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));
|
||||
|
||||
@ -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
|
||||
);
|
||||
|
||||
@ -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,
|
||||
|
||||
@ -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",
|
||||
|
||||
@ -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"],
|
||||
|
||||
@ -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",
|
||||
|
||||
@ -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",
|
||||
|
||||
@ -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",
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user