1
0
Fork 0
mirror of https://github.com/mozilla/pdf.js.git synced 2025-04-22 16:18:08 +02:00

[api-minor] Re-factor how the useWorkerFetch option is used internally

With the recently added OpenJPEG no-wasm fallback we need to send the `wasmUrl` option to the worker-thread *regardless* of the value of the `useWorkerFetch` option, since the fallback won't work if we don't have a URL to `import` it from.
For consistency the code is re-factored to always send the factory-urls to the worker-thread, and simply check the `useWorkerFetch` option there instead.

Also, as a follow-up to PR 19525, introduce a new `useWasm` option that can be used in e.g. browser-tests to forcibly disable WebAssembly usage.
This commit is contained in:
Jonas Jenwald 2025-02-21 23:20:44 +01:00
parent 6d3bb47655
commit 641e2f506e
5 changed files with 29 additions and 20 deletions

View file

@ -90,6 +90,8 @@ const DefaultPartialEvaluatorOptions = Object.freeze({
canvasMaxAreaInBytes: -1,
fontExtraProperties: false,
useSystemFonts: true,
useWasm: true,
useWorkerFetch: true,
cMapUrl: null,
standardFontDataUrl: null,
wasmUrl: null,
@ -384,7 +386,7 @@ class PartialEvaluator {
}
let data;
if (this.options.cMapUrl !== null) {
if (this.options.useWorkerFetch) {
// Only compressed CMaps are (currently) supported here.
data = {
cMapData: await fetchBinaryData(`${this.options.cMapUrl}${name}.bcmap`),
@ -424,7 +426,7 @@ class PartialEvaluator {
let data;
try {
if (this.options.standardFontDataUrl !== null) {
if (this.options.useWorkerFetch) {
data = await fetchBinaryData(
`${this.options.standardFontDataUrl}${filename}`
);

View file

@ -31,24 +31,26 @@ class JpxImage {
static #modulePromise = null;
static #hasJSFallback = false;
static #useWasm = true;
static #useWorkerFetch = true;
static #wasmUrl = null;
static setOptions({ handler, wasmUrl }) {
if (this.#buffer || this.#hasJSFallback || this.#modulePromise) {
static setOptions({ handler, useWasm, useWorkerFetch, wasmUrl }) {
if (this.#buffer || this.#modulePromise) {
return;
}
this.#wasmUrl = wasmUrl || null;
if (wasmUrl === null) {
this.#useWasm = useWasm;
this.#useWorkerFetch = useWorkerFetch;
this.#wasmUrl = wasmUrl;
if (!useWorkerFetch) {
this.#handler = handler;
}
}
static async #getJsModule(fallbackCallback) {
if (typeof PDFJSDev !== "undefined" && PDFJSDev.test("TESTING")) {
this.#wasmUrl ??= "/build/generic/web/wasm/";
}
const path =
typeof PDFJSDev === "undefined"
? `../${this.#wasmUrl}openjpeg_nowasm_fallback.js`
@ -63,8 +65,6 @@ class JpxImage {
} catch (e) {
warn(`JpxImage#getJsModule: ${e}`);
}
this.#hasJSFallback = true;
fallbackCallback(instance);
}
@ -72,7 +72,7 @@ class JpxImage {
const filename = "openjpeg.wasm";
try {
if (!this.#buffer) {
if (this.#wasmUrl !== null) {
if (this.#useWorkerFetch) {
this.#buffer = await fetchBinaryData(`${this.#wasmUrl}${filename}`);
} else {
this.#buffer = await this.#handler.sendWithPromise(
@ -100,7 +100,7 @@ class JpxImage {
if (!this.#modulePromise) {
const { promise, resolve } = Promise.withResolvers();
const promises = [promise];
if (this.#hasJSFallback) {
if (!this.#useWasm) {
this.#getJsModule(resolve);
} else {
promises.push(

View file

@ -144,6 +144,9 @@ const RENDERING_CANCELLED_TIMEOUT = 100; // ms
* the `CMapReaderFactory`, `StandardFontDataFactory`, and `WasmFactory`
* options are 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.
* The default value is `true`.
* @property {boolean} [stopAtErrors] - Reject certain promises, e.g.
* `getOperatorList`, `getTextContent`, and `RenderTask`, when the associated
* PDF data cannot be successfully parsed, instead of attempting to recover
@ -320,6 +323,7 @@ function getDocument(src = {}) {
? NodeFilterFactory
: DOMFilterFactory);
const enableHWA = src.enableHWA === true;
const useWasm = src.useWasm !== false;
// Parameters whose default values depend on other parameters.
const length = rangeTransport ? rangeTransport.length : (src.length ?? NaN);
@ -410,9 +414,11 @@ function getDocument(src = {}) {
canvasMaxAreaInBytes,
fontExtraProperties,
useSystemFonts,
cMapUrl: useWorkerFetch ? cMapUrl : null,
standardFontDataUrl: useWorkerFetch ? standardFontDataUrl : null,
wasmUrl: useWorkerFetch ? wasmUrl : null,
useWasm,
useWorkerFetch,
cMapUrl,
standardFontDataUrl,
wasmUrl,
},
};
const transportParams = {