Move the main-thread CMap/StandardFontData factory initialization to getDocument

By default we're using worker-thread fetching (in browsers) of this data nowadays, however in Node.js environments or if the user provides custom factories we still fallback to main-thread fetching.
Hence it makes sense, as far as I'm concerned, to move this initialization into the `getDocument` function to ensure that the factories can actually be initialized *before* attempting to load the document.

Also, this further reduces the amount of `getDocument` parameters that we need to pass into into the `WorkerTransport` class.
This commit is contained in:
Jonas Jenwald 2023-01-30 17:33:00 +01:00
parent ce8ac6d96a
commit 0a0f3fc733

View File

@ -347,6 +347,20 @@ function getDocument(src) {
// Set the main-thread verbosity level. // Set the main-thread verbosity level.
setVerbosityLevel(verbosity); setVerbosityLevel(verbosity);
// Ensure that the various factories can be initialized, when necessary,
// since the user may provide *custom* ones.
const transportFactory = useWorkerFetch
? null
: {
cMapReaderFactory: new CMapReaderFactory({
baseUrl: cMapUrl,
isCompressed: cMapPacked,
}),
standardFontDataFactory: new StandardFontDataFactory({
baseUrl: standardFontDataUrl,
}),
};
if (!worker) { if (!worker) {
const workerParams = { const workerParams = {
verbosity, verbosity,
@ -387,11 +401,6 @@ function getDocument(src) {
}, },
}; };
const transportParams = { const transportParams = {
cMapUrl,
cMapPacked,
CMapReaderFactory,
standardFontDataUrl,
StandardFontDataFactory,
ignoreErrors, ignoreErrors,
isEvalSupported, isEvalSupported,
disableFontFace, disableFontFace,
@ -400,7 +409,6 @@ function getDocument(src) {
ownerDocument, ownerDocument,
disableAutoFetch, disableAutoFetch,
pdfBug, pdfBug,
useWorkerFetch,
styleElement, styleElement,
}; };
@ -458,7 +466,8 @@ function getDocument(src) {
messageHandler, messageHandler,
task, task,
networkStream, networkStream,
transportParams transportParams,
transportFactory
); );
task._transport = transport; task._transport = transport;
messageHandler.send("Ready", null); messageHandler.send("Ready", null);
@ -2326,7 +2335,7 @@ class WorkerTransport {
#pagePromises = new Map(); #pagePromises = new Map();
constructor(messageHandler, loadingTask, networkStream, params) { constructor(messageHandler, loadingTask, networkStream, params, factory) {
this.messageHandler = messageHandler; this.messageHandler = messageHandler;
this.loadingTask = loadingTask; this.loadingTask = loadingTask;
this.commonObjs = new PDFObjects(); this.commonObjs = new PDFObjects();
@ -2337,15 +2346,8 @@ class WorkerTransport {
}); });
this._params = params; this._params = params;
if (!params.useWorkerFetch) { this.cMapReaderFactory = factory?.cMapReaderFactory;
this.CMapReaderFactory = new params.CMapReaderFactory({ this.standardFontDataFactory = factory?.standardFontDataFactory;
baseUrl: params.cMapUrl,
isCompressed: params.cMapPacked,
});
this.StandardFontDataFactory = new params.StandardFontDataFactory({
baseUrl: params.standardFontDataUrl,
});
}
this.destroyed = false; this.destroyed = false;
this.destroyCapability = null; this.destroyCapability = null;
@ -2813,28 +2815,28 @@ class WorkerTransport {
if (this.destroyed) { if (this.destroyed) {
return Promise.reject(new Error("Worker was destroyed.")); return Promise.reject(new Error("Worker was destroyed."));
} }
if (!this.CMapReaderFactory) { if (!this.cMapReaderFactory) {
return Promise.reject( return Promise.reject(
new Error( new Error(
"CMapReaderFactory not initialized, see the `useWorkerFetch` parameter." "CMapReaderFactory not initialized, see the `useWorkerFetch` parameter."
) )
); );
} }
return this.CMapReaderFactory.fetch(data); return this.cMapReaderFactory.fetch(data);
}); });
messageHandler.on("FetchStandardFontData", data => { messageHandler.on("FetchStandardFontData", data => {
if (this.destroyed) { if (this.destroyed) {
return Promise.reject(new Error("Worker was destroyed.")); return Promise.reject(new Error("Worker was destroyed."));
} }
if (!this.StandardFontDataFactory) { if (!this.standardFontDataFactory) {
return Promise.reject( return Promise.reject(
new Error( new Error(
"StandardFontDataFactory not initialized, see the `useWorkerFetch` parameter." "StandardFontDataFactory not initialized, see the `useWorkerFetch` parameter."
) )
); );
} }
return this.StandardFontDataFactory.fetch(data); return this.standardFontDataFactory.fetch(data);
}); });
} }