Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 6 additions & 16 deletions gulpfile.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down Expand Up @@ -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";

Expand All @@ -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";
Expand Down Expand Up @@ -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",
Expand Down
4 changes: 2 additions & 2 deletions src/core/evaluator.js
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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,
});
}
Expand Down
2 changes: 1 addition & 1 deletion src/core/jbig2_ccittFax_wasm.js
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ class JBig2CCITTFaxWasmImage {
}
this.#buffer = await this.#handler.sendWithPromise(
"FetchBinaryData",
{ type: "wasmFactory", filename }
{ kind: "wasmUrl", filename }
);
}
}
Expand Down
2 changes: 1 addition & 1 deletion src/core/jpx.js
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ class JpxImage {
}
this.#buffer = await this.#handler.sendWithPromise(
"FetchBinaryData",
{ type: "wasmFactory", filename }
{ kind: "wasmUrl", filename }
);
}
}
Expand Down
76 changes: 20 additions & 56 deletions src/display/api.js
Original file line number Diff line number Diff line change
Expand Up @@ -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";
Expand Down Expand Up @@ -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
Expand All @@ -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.
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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;
Expand All @@ -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 &&
Expand All @@ -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) {
Expand Down Expand Up @@ -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;

Expand Down Expand Up @@ -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);
});
}
}
Expand Down
79 changes: 79 additions & 0 deletions src/display/binary_data_factory.js
Original file line number Diff line number Diff line change
@@ -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 };
63 changes: 0 additions & 63 deletions src/display/cmap_reader_factory.js

This file was deleted.

Loading
Loading