[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:
parent
6d3bb47655
commit
641e2f506e
@ -90,6 +90,8 @@ const DefaultPartialEvaluatorOptions = Object.freeze({
|
|||||||
canvasMaxAreaInBytes: -1,
|
canvasMaxAreaInBytes: -1,
|
||||||
fontExtraProperties: false,
|
fontExtraProperties: false,
|
||||||
useSystemFonts: true,
|
useSystemFonts: true,
|
||||||
|
useWasm: true,
|
||||||
|
useWorkerFetch: true,
|
||||||
cMapUrl: null,
|
cMapUrl: null,
|
||||||
standardFontDataUrl: null,
|
standardFontDataUrl: null,
|
||||||
wasmUrl: null,
|
wasmUrl: null,
|
||||||
@ -384,7 +386,7 @@ class PartialEvaluator {
|
|||||||
}
|
}
|
||||||
let data;
|
let data;
|
||||||
|
|
||||||
if (this.options.cMapUrl !== null) {
|
if (this.options.useWorkerFetch) {
|
||||||
// Only compressed CMaps are (currently) supported here.
|
// Only compressed CMaps are (currently) supported here.
|
||||||
data = {
|
data = {
|
||||||
cMapData: await fetchBinaryData(`${this.options.cMapUrl}${name}.bcmap`),
|
cMapData: await fetchBinaryData(`${this.options.cMapUrl}${name}.bcmap`),
|
||||||
@ -424,7 +426,7 @@ class PartialEvaluator {
|
|||||||
let data;
|
let data;
|
||||||
|
|
||||||
try {
|
try {
|
||||||
if (this.options.standardFontDataUrl !== null) {
|
if (this.options.useWorkerFetch) {
|
||||||
data = await fetchBinaryData(
|
data = await fetchBinaryData(
|
||||||
`${this.options.standardFontDataUrl}${filename}`
|
`${this.options.standardFontDataUrl}${filename}`
|
||||||
);
|
);
|
||||||
|
|||||||
@ -31,24 +31,26 @@ class JpxImage {
|
|||||||
|
|
||||||
static #modulePromise = null;
|
static #modulePromise = null;
|
||||||
|
|
||||||
static #hasJSFallback = false;
|
static #useWasm = true;
|
||||||
|
|
||||||
|
static #useWorkerFetch = true;
|
||||||
|
|
||||||
static #wasmUrl = null;
|
static #wasmUrl = null;
|
||||||
|
|
||||||
static setOptions({ handler, wasmUrl }) {
|
static setOptions({ handler, useWasm, useWorkerFetch, wasmUrl }) {
|
||||||
if (this.#buffer || this.#hasJSFallback || this.#modulePromise) {
|
if (this.#buffer || this.#modulePromise) {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
this.#wasmUrl = wasmUrl || null;
|
this.#useWasm = useWasm;
|
||||||
if (wasmUrl === null) {
|
this.#useWorkerFetch = useWorkerFetch;
|
||||||
|
this.#wasmUrl = wasmUrl;
|
||||||
|
|
||||||
|
if (!useWorkerFetch) {
|
||||||
this.#handler = handler;
|
this.#handler = handler;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
static async #getJsModule(fallbackCallback) {
|
static async #getJsModule(fallbackCallback) {
|
||||||
if (typeof PDFJSDev !== "undefined" && PDFJSDev.test("TESTING")) {
|
|
||||||
this.#wasmUrl ??= "/build/generic/web/wasm/";
|
|
||||||
}
|
|
||||||
const path =
|
const path =
|
||||||
typeof PDFJSDev === "undefined"
|
typeof PDFJSDev === "undefined"
|
||||||
? `../${this.#wasmUrl}openjpeg_nowasm_fallback.js`
|
? `../${this.#wasmUrl}openjpeg_nowasm_fallback.js`
|
||||||
@ -63,8 +65,6 @@ class JpxImage {
|
|||||||
} catch (e) {
|
} catch (e) {
|
||||||
warn(`JpxImage#getJsModule: ${e}`);
|
warn(`JpxImage#getJsModule: ${e}`);
|
||||||
}
|
}
|
||||||
|
|
||||||
this.#hasJSFallback = true;
|
|
||||||
fallbackCallback(instance);
|
fallbackCallback(instance);
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -72,7 +72,7 @@ class JpxImage {
|
|||||||
const filename = "openjpeg.wasm";
|
const filename = "openjpeg.wasm";
|
||||||
try {
|
try {
|
||||||
if (!this.#buffer) {
|
if (!this.#buffer) {
|
||||||
if (this.#wasmUrl !== null) {
|
if (this.#useWorkerFetch) {
|
||||||
this.#buffer = await fetchBinaryData(`${this.#wasmUrl}${filename}`);
|
this.#buffer = await fetchBinaryData(`${this.#wasmUrl}${filename}`);
|
||||||
} else {
|
} else {
|
||||||
this.#buffer = await this.#handler.sendWithPromise(
|
this.#buffer = await this.#handler.sendWithPromise(
|
||||||
@ -100,7 +100,7 @@ class JpxImage {
|
|||||||
if (!this.#modulePromise) {
|
if (!this.#modulePromise) {
|
||||||
const { promise, resolve } = Promise.withResolvers();
|
const { promise, resolve } = Promise.withResolvers();
|
||||||
const promises = [promise];
|
const promises = [promise];
|
||||||
if (this.#hasJSFallback) {
|
if (!this.#useWasm) {
|
||||||
this.#getJsModule(resolve);
|
this.#getJsModule(resolve);
|
||||||
} else {
|
} else {
|
||||||
promises.push(
|
promises.push(
|
||||||
|
|||||||
@ -144,6 +144,9 @@ const RENDERING_CANCELLED_TIMEOUT = 100; // ms
|
|||||||
* the `CMapReaderFactory`, `StandardFontDataFactory`, and `WasmFactory`
|
* the `CMapReaderFactory`, `StandardFontDataFactory`, and `WasmFactory`
|
||||||
* options are ignored.
|
* options are ignored.
|
||||||
* The default value is `true` in web environments and `false` in Node.js.
|
* 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.
|
* @property {boolean} [stopAtErrors] - Reject certain promises, e.g.
|
||||||
* `getOperatorList`, `getTextContent`, and `RenderTask`, when the associated
|
* `getOperatorList`, `getTextContent`, and `RenderTask`, when the associated
|
||||||
* PDF data cannot be successfully parsed, instead of attempting to recover
|
* PDF data cannot be successfully parsed, instead of attempting to recover
|
||||||
@ -320,6 +323,7 @@ function getDocument(src = {}) {
|
|||||||
? NodeFilterFactory
|
? NodeFilterFactory
|
||||||
: DOMFilterFactory);
|
: DOMFilterFactory);
|
||||||
const enableHWA = src.enableHWA === true;
|
const enableHWA = src.enableHWA === true;
|
||||||
|
const useWasm = src.useWasm !== false;
|
||||||
|
|
||||||
// Parameters whose default values depend on other parameters.
|
// Parameters whose default values depend on other parameters.
|
||||||
const length = rangeTransport ? rangeTransport.length : (src.length ?? NaN);
|
const length = rangeTransport ? rangeTransport.length : (src.length ?? NaN);
|
||||||
@ -410,9 +414,11 @@ function getDocument(src = {}) {
|
|||||||
canvasMaxAreaInBytes,
|
canvasMaxAreaInBytes,
|
||||||
fontExtraProperties,
|
fontExtraProperties,
|
||||||
useSystemFonts,
|
useSystemFonts,
|
||||||
cMapUrl: useWorkerFetch ? cMapUrl : null,
|
useWasm,
|
||||||
standardFontDataUrl: useWorkerFetch ? standardFontDataUrl : null,
|
useWorkerFetch,
|
||||||
wasmUrl: useWorkerFetch ? wasmUrl : null,
|
cMapUrl,
|
||||||
|
standardFontDataUrl,
|
||||||
|
wasmUrl,
|
||||||
},
|
},
|
||||||
};
|
};
|
||||||
const transportParams = {
|
const transportParams = {
|
||||||
|
|||||||
@ -638,10 +638,11 @@ class Driver {
|
|||||||
password: task.password,
|
password: task.password,
|
||||||
cMapUrl: CMAP_URL,
|
cMapUrl: CMAP_URL,
|
||||||
standardFontDataUrl: STANDARD_FONT_DATA_URL,
|
standardFontDataUrl: STANDARD_FONT_DATA_URL,
|
||||||
wasmUrl: task.noWasm ? null : WASM_URL,
|
wasmUrl: WASM_URL,
|
||||||
disableAutoFetch: !task.enableAutoFetch,
|
disableAutoFetch: !task.enableAutoFetch,
|
||||||
pdfBug: true,
|
pdfBug: true,
|
||||||
useSystemFonts: task.useSystemFonts,
|
useSystemFonts: task.useSystemFonts,
|
||||||
|
useWasm: task.useWasm,
|
||||||
useWorkerFetch: task.useWorkerFetch,
|
useWorkerFetch: task.useWorkerFetch,
|
||||||
enableXfa: task.enableXfa,
|
enableXfa: task.enableXfa,
|
||||||
isOffscreenCanvasSupported,
|
isOffscreenCanvasSupported,
|
||||||
|
|||||||
@ -6422,9 +6422,9 @@
|
|||||||
"id": "issue19326_nowasm",
|
"id": "issue19326_nowasm",
|
||||||
"file": "pdfs/issue19326.pdf",
|
"file": "pdfs/issue19326.pdf",
|
||||||
"md5": "b4d937017daf439a6318501428e0c6ba",
|
"md5": "b4d937017daf439a6318501428e0c6ba",
|
||||||
"noWasm": true,
|
|
||||||
"rounds": 1,
|
"rounds": 1,
|
||||||
"type": "eq"
|
"type": "eq",
|
||||||
|
"useWasm": false
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
"id": "issue19326_main_thread_fetch",
|
"id": "issue19326_main_thread_fetch",
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user