Skip to content

Commit 1756b48

Browse files
Merge pull request #20949 from Snuffleupagus/BinaryDataFactory-2
[api-minor] Replace the `CMapReaderFactory`, `StandardFontDataFactory`, and `WasmFactory` API options with a single factory/option
2 parents 0a6894d + 3a372fd commit 1756b48

20 files changed

Lines changed: 152 additions & 356 deletions

gulpfile.mjs

Lines changed: 6 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -195,9 +195,7 @@ function createWebpackAlias(defines) {
195195
"fluent-dom": "node_modules/@fluent/dom/esm/index.js",
196196
};
197197
const libraryAlias = {
198-
"display-cmap_reader_factory": "src/display/stubs.js",
199-
"display-standard_fontdata_factory": "src/display/stubs.js",
200-
"display-wasm_factory": "src/display/stubs.js",
198+
"display-binary_data_factory": "src/display/stubs.js",
201199
"display-fetch_stream": "src/display/stubs.js",
202200
"display-network": "src/display/stubs.js",
203201
"display-node_stream": "src/display/stubs.js",
@@ -227,11 +225,8 @@ function createWebpackAlias(defines) {
227225
};
228226

229227
if (defines.CHROME) {
230-
libraryAlias["display-cmap_reader_factory"] =
231-
"src/display/cmap_reader_factory.js";
232-
libraryAlias["display-standard_fontdata_factory"] =
233-
"src/display/standard_fontdata_factory.js";
234-
libraryAlias["display-wasm_factory"] = "src/display/wasm_factory.js";
228+
libraryAlias["display-binary_data_factory"] =
229+
"src/display/binary_data_factory.js";
235230
libraryAlias["display-fetch_stream"] = "src/display/fetch_stream.js";
236231
libraryAlias["display-network"] = "src/display/network.js";
237232

@@ -244,11 +239,8 @@ function createWebpackAlias(defines) {
244239
// Aliases defined here must also be replicated in the paths section of
245240
// the tsconfig.json file for the type generation to work.
246241
// In the tsconfig.json files, the .js extension must be omitted.
247-
libraryAlias["display-cmap_reader_factory"] =
248-
"src/display/cmap_reader_factory.js";
249-
libraryAlias["display-standard_fontdata_factory"] =
250-
"src/display/standard_fontdata_factory.js";
251-
libraryAlias["display-wasm_factory"] = "src/display/wasm_factory.js";
242+
libraryAlias["display-binary_data_factory"] =
243+
"src/display/binary_data_factory.js";
252244
libraryAlias["display-fetch_stream"] = "src/display/fetch_stream.js";
253245
libraryAlias["display-network"] = "src/display/network.js";
254246
libraryAlias["display-node_stream"] = "src/display/node_stream.js";
@@ -1547,9 +1539,7 @@ function buildLibHelper(bundleDefines, inputStream, outputDir) {
15471539
defines: bundleDefines,
15481540
map: {
15491541
"pdfjs-lib": "../pdf.js",
1550-
"display-cmap_reader_factory": "./cmap_reader_factory.js",
1551-
"display-standard_fontdata_factory": "./standard_fontdata_factory.js",
1552-
"display-wasm_factory": "./wasm_factory.js",
1542+
"display-binary_data_factory": "./binary_data_factory.js",
15531543
"display-fetch_stream": "./fetch_stream.js",
15541544
"display-network": "./network.js",
15551545
"display-node_stream": "./node_stream.js",

src/core/evaluator.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -416,7 +416,7 @@ class PartialEvaluator {
416416
// Get the data on the main-thread instead.
417417
data = {
418418
cMapData: await this.handler.sendWithPromise("FetchBinaryData", {
419-
type: "cMapReaderFactory",
419+
kind: "cMapUrl",
420420
filename: `${name}${this.options.cMapPacked ? ".bcmap" : ""}`,
421421
}),
422422
isCompressed: this.options.cMapPacked,
@@ -459,7 +459,7 @@ class PartialEvaluator {
459459
}
460460
// Get the data on the main-thread instead.
461461
data = await this.handler.sendWithPromise("FetchBinaryData", {
462-
type: "standardFontDataFactory",
462+
kind: "standardFontDataUrl",
463463
filename,
464464
});
465465
}

src/core/jbig2_ccittFax_wasm.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@ class JBig2CCITTFaxWasmImage {
5353
}
5454
this.#buffer = await this.#handler.sendWithPromise(
5555
"FetchBinaryData",
56-
{ type: "wasmFactory", filename }
56+
{ kind: "wasmUrl", filename }
5757
);
5858
}
5959
}

src/core/jpx.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -77,7 +77,7 @@ class JpxImage {
7777
}
7878
this.#buffer = await this.#handler.sendWithPromise(
7979
"FetchBinaryData",
80-
{ type: "wasmFactory", filename }
80+
{ kind: "wasmUrl", filename }
8181
);
8282
}
8383
}

src/display/api.js

Lines changed: 20 additions & 56 deletions
Original file line numberDiff line numberDiff line change
@@ -66,18 +66,14 @@ import {
6666
} from "./api_utils.js";
6767
import { MessageHandler, wrapReason } from "../shared/message_handler.js";
6868
import {
69+
NodeBinaryDataFactory,
6970
NodeCanvasFactory,
70-
NodeCMapReaderFactory,
7171
NodeFilterFactory,
72-
NodeStandardFontDataFactory,
73-
NodeWasmFactory,
7472
} from "display-node_utils";
7573
import { CanvasGraphics } from "./canvas.js";
74+
import { DOMBinaryDataFactory } from "display-binary_data_factory";
7675
import { DOMCanvasFactory } from "./canvas_factory.js";
77-
import { DOMCMapReaderFactory } from "display-cmap_reader_factory";
7876
import { DOMFilterFactory } from "./filter_factory.js";
79-
import { DOMStandardFontDataFactory } from "display-standard_fontdata_factory";
80-
import { DOMWasmFactory } from "display-wasm_factory";
8177
import { GlobalWorkerOptions } from "./worker_options.js";
8278
import { initWebGPUMesh } from "./webgpu_mesh.js";
8379
import { Metadata } from "./metadata.js";
@@ -140,9 +136,6 @@ const RENDERING_CANCELLED_TIMEOUT = 100; // ms
140136
* located. Include the trailing slash.
141137
* @property {boolean} [cMapPacked] - Specifies if the Adobe CMaps are binary
142138
* packed or not. The default value is `true`.
143-
* @property {Object} [CMapReaderFactory] - The factory that will be used when
144-
* reading built-in CMap files.
145-
* The default value is {DOMCMapReaderFactory}.
146139
* @property {string} [iccUrl] - The URL where the predefined ICC profiles are
147140
* located. Include the trailing slash.
148141
* @property {boolean} [useSystemFonts] - When `true`, fonts that aren't
@@ -152,18 +145,11 @@ const RENDERING_CANCELLED_TIMEOUT = 100; // ms
152145
* regardless of the environment (to prevent completely broken fonts).
153146
* @property {string} [standardFontDataUrl] - The URL where the standard font
154147
* files are located. Include the trailing slash.
155-
* @property {Object} [StandardFontDataFactory] - The factory that will be used
156-
* when reading the standard font files.
157-
* The default value is {DOMStandardFontDataFactory}.
158148
* @property {string} [wasmUrl] - The URL where the wasm files are located.
159149
* Include the trailing slash.
160-
* @property {Object} [WasmFactory] - The factory that will be used
161-
* when reading the wasm files.
162-
* The default value is {DOMWasmFactory}.
163150
* @property {boolean} [useWorkerFetch] - Enable using the Fetch API in the
164-
* worker-thread when reading CMap and standard font files. When `true`,
165-
* the `CMapReaderFactory`, `StandardFontDataFactory`, and `WasmFactory`
166-
* options are ignored.
151+
* worker-thread when reading built-in CMap files, standard font files,
152+
* and wasm files. If `true`, the `BinaryDataFactory` option is ignored.
167153
* The default value is `true` in web environments and `false` in Node.js.
168154
* @property {boolean} [useWasm] - Attempt to use WebAssembly in order to
169155
* improve e.g. image decoding performance.
@@ -235,6 +221,10 @@ const RENDERING_CANCELLED_TIMEOUT = 100; // ms
235221
* @property {Object} [FilterFactory] - The factory that will be used to
236222
* create SVG filters when rendering some images on the main canvas.
237223
* The default value is {DOMFilterFactory}.
224+
* @property {Object} [BinaryDataFactory] - The factory that will be used when
225+
* falling back to reading built-in CMap files, standard font files,
226+
* and wasm files in the main-thread.
227+
* The default value is {DOMBinaryDataFactory}.
238228
* @property {boolean} [enableHWA] - Enables hardware acceleration for
239229
* rendering. The default value is `false`.
240230
* @property {Object} [pagesMapper] - The pages mapper that will be used to map
@@ -287,24 +277,9 @@ function getDocument(src = {}) {
287277
: null;
288278
const cMapUrl = getFactoryUrlProp(src.cMapUrl);
289279
const cMapPacked = src.cMapPacked !== false;
290-
const CMapReaderFactory =
291-
src.CMapReaderFactory ||
292-
(typeof PDFJSDev !== "undefined" && PDFJSDev.test("GENERIC") && isNodeJS
293-
? NodeCMapReaderFactory
294-
: DOMCMapReaderFactory);
295280
const iccUrl = getFactoryUrlProp(src.iccUrl);
296281
const standardFontDataUrl = getFactoryUrlProp(src.standardFontDataUrl);
297-
const StandardFontDataFactory =
298-
src.StandardFontDataFactory ||
299-
(typeof PDFJSDev !== "undefined" && PDFJSDev.test("GENERIC") && isNodeJS
300-
? NodeStandardFontDataFactory
301-
: DOMStandardFontDataFactory);
302282
const wasmUrl = getFactoryUrlProp(src.wasmUrl);
303-
const WasmFactory =
304-
src.WasmFactory ||
305-
(typeof PDFJSDev !== "undefined" && PDFJSDev.test("GENERIC") && isNodeJS
306-
? NodeWasmFactory
307-
: DOMWasmFactory);
308283
const ignoreErrors = src.stopAtErrors !== true;
309284
const maxImageSize =
310285
Number.isInteger(src.maxImageSize) && src.maxImageSize > -1
@@ -347,6 +322,11 @@ function getDocument(src = {}) {
347322
(typeof PDFJSDev !== "undefined" && PDFJSDev.test("GENERIC") && isNodeJS
348323
? NodeFilterFactory
349324
: DOMFilterFactory);
325+
const BinaryDataFactory =
326+
src.BinaryDataFactory ||
327+
(typeof PDFJSDev !== "undefined" && PDFJSDev.test("GENERIC") && isNodeJS
328+
? NodeBinaryDataFactory
329+
: DOMBinaryDataFactory);
350330
const enableHWA = src.enableHWA === true;
351331
const enableWebGPU = src.enableWebGPU === true;
352332
const useWasm = src.useWasm !== false;
@@ -362,9 +342,7 @@ function getDocument(src = {}) {
362342
? src.useWorkerFetch
363343
: (typeof PDFJSDev !== "undefined" && PDFJSDev.test("MOZCENTRAL")) ||
364344
!!(
365-
CMapReaderFactory === DOMCMapReaderFactory &&
366-
StandardFontDataFactory === DOMStandardFontDataFactory &&
367-
WasmFactory === DOMWasmFactory &&
345+
BinaryDataFactory === DOMBinaryDataFactory &&
368346
cMapUrl &&
369347
cMapPacked &&
370348
standardFontDataUrl &&
@@ -388,21 +366,11 @@ function getDocument(src = {}) {
388366
const transportFactory = {
389367
canvasFactory: new CanvasFactory({ ownerDocument, enableHWA }),
390368
filterFactory: new FilterFactory({ docId, ownerDocument }),
391-
cMapReaderFactory:
369+
binaryDataFactory:
392370
(typeof PDFJSDev !== "undefined" && PDFJSDev.test("MOZCENTRAL")) ||
393371
useWorkerFetch
394372
? null
395-
: new CMapReaderFactory({ baseUrl: cMapUrl }),
396-
standardFontDataFactory:
397-
(typeof PDFJSDev !== "undefined" && PDFJSDev.test("MOZCENTRAL")) ||
398-
useWorkerFetch
399-
? null
400-
: new StandardFontDataFactory({ baseUrl: standardFontDataUrl }),
401-
wasmFactory:
402-
(typeof PDFJSDev !== "undefined" && PDFJSDev.test("MOZCENTRAL")) ||
403-
useWorkerFetch
404-
? null
405-
: new WasmFactory({ baseUrl: wasmUrl }),
373+
: new BinaryDataFactory({ cMapUrl, standardFontDataUrl, wasmUrl }),
406374
};
407375

408376
if (!worker) {
@@ -2474,9 +2442,7 @@ class WorkerTransport {
24742442
this.canvasFactory = factory.canvasFactory;
24752443
this.filterFactory = factory.filterFactory;
24762444
if (typeof PDFJSDev === "undefined" || !PDFJSDev.test("MOZCENTRAL")) {
2477-
this.cMapReaderFactory = factory.cMapReaderFactory;
2478-
this.standardFontDataFactory = factory.standardFontDataFactory;
2479-
this.wasmFactory = factory.wasmFactory;
2445+
this.binaryDataFactory = factory.binaryDataFactory;
24802446
}
24812447
this.pagesMapper = pagesMapper;
24822448

@@ -2943,14 +2909,12 @@ class WorkerTransport {
29432909
if (this.destroyed) {
29442910
throw new Error("Worker was destroyed.");
29452911
}
2946-
const factory = this[data.type];
2947-
2948-
if (!factory) {
2912+
if (!this.binaryDataFactory) {
29492913
throw new Error(
2950-
`${data.type} not initialized, see the \`useWorkerFetch\` parameter.`
2914+
"`BinaryDataFactory` not initialized, see the `useWorkerFetch` parameter."
29512915
);
29522916
}
2953-
return factory.fetch(data);
2917+
return this.binaryDataFactory.fetch(data);
29542918
});
29552919
}
29562920
}

src/display/binary_data_factory.js

Lines changed: 79 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,79 @@
1+
/* Copyright 2015 Mozilla Foundation
2+
*
3+
* Licensed under the Apache License, Version 2.0 (the "License");
4+
* you may not use this file except in compliance with the License.
5+
* You may obtain a copy of the License at
6+
*
7+
* http://www.apache.org/licenses/LICENSE-2.0
8+
*
9+
* Unless required by applicable law or agreed to in writing, software
10+
* distributed under the License is distributed on an "AS IS" BASIS,
11+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
* See the License for the specific language governing permissions and
13+
* limitations under the License.
14+
*/
15+
16+
import { stringToBytes, unreachable } from "../shared/util.js";
17+
import { fetchData } from "./display_utils.js";
18+
19+
class BaseBinaryDataFactory {
20+
#errorStr = Object.freeze({
21+
cMapUrl: "CMap",
22+
standardFontDataUrl: "font",
23+
wasmUrl: "wasm",
24+
});
25+
26+
constructor({ cMapUrl = null, standardFontDataUrl = null, wasmUrl = null }) {
27+
if (
28+
(typeof PDFJSDev === "undefined" || PDFJSDev.test("TESTING")) &&
29+
this.constructor === BaseBinaryDataFactory
30+
) {
31+
unreachable("Cannot initialize BaseBinaryDataFactory.");
32+
}
33+
this.cMapUrl = cMapUrl;
34+
this.standardFontDataUrl = standardFontDataUrl;
35+
this.wasmUrl = wasmUrl;
36+
}
37+
38+
async fetch({ kind, filename }) {
39+
switch (kind) {
40+
case "cMapUrl":
41+
case "standardFontDataUrl":
42+
case "wasmUrl":
43+
break;
44+
default:
45+
unreachable(`Not implemented: ${kind}`);
46+
}
47+
const baseUrl = this[kind];
48+
if (!baseUrl) {
49+
throw new Error(`Ensure that the \`${kind}\` API parameter is provided.`);
50+
}
51+
const url = `${baseUrl}${filename}`;
52+
53+
return this._fetch(url, kind).catch(reason => {
54+
throw new Error(`Unable to load ${this.#errorStr[kind]} data at: ${url}`);
55+
});
56+
}
57+
58+
/**
59+
* @ignore
60+
* @returns {Promise<Uint8Array>}
61+
*/
62+
async _fetch(url, kind) {
63+
unreachable("Abstract method `_fetch` called.");
64+
}
65+
}
66+
67+
class DOMBinaryDataFactory extends BaseBinaryDataFactory {
68+
/**
69+
* @ignore
70+
*/
71+
async _fetch(url, kind) {
72+
const type =
73+
kind === "cMapUrl" && !url.endsWith(".bcmap") ? "text" : "bytes";
74+
const data = await fetchData(url, type);
75+
return data instanceof Uint8Array ? data : stringToBytes(data);
76+
}
77+
}
78+
79+
export { BaseBinaryDataFactory, DOMBinaryDataFactory };

src/display/cmap_reader_factory.js

Lines changed: 0 additions & 63 deletions
This file was deleted.

0 commit comments

Comments
 (0)