Fetch binary CMap data in the worker-thread, when useWorkerFetch
is set
This patch uses the new option added in PR 12726 to *also* allow fetching binary CMap data directly in the worker-thread in browsers. Given that these changes remove the need to transfer data between threads for the default (browser) use-case, we can also revert the changes in PR 11118 since that simplifies the overall implementation.
This commit is contained in:
parent
248113bbf0
commit
d995f90183
@ -96,8 +96,9 @@ const DefaultPartialEvaluatorOptions = Object.freeze({
|
|||||||
ignoreErrors: false,
|
ignoreErrors: false,
|
||||||
isEvalSupported: true,
|
isEvalSupported: true,
|
||||||
fontExtraProperties: false,
|
fontExtraProperties: false,
|
||||||
standardFontDataUrl: null,
|
|
||||||
useSystemFonts: true,
|
useSystemFonts: true,
|
||||||
|
cMapUrl: null,
|
||||||
|
standardFontDataUrl: null,
|
||||||
});
|
});
|
||||||
|
|
||||||
const PatternType = {
|
const PatternType = {
|
||||||
@ -360,23 +361,25 @@ class PartialEvaluator {
|
|||||||
if (cachedData) {
|
if (cachedData) {
|
||||||
return cachedData;
|
return cachedData;
|
||||||
}
|
}
|
||||||
const readableStream = this.handler.sendWithStream("FetchBuiltInCMap", {
|
let data;
|
||||||
name,
|
|
||||||
});
|
|
||||||
const reader = readableStream.getReader();
|
|
||||||
|
|
||||||
const data = await new Promise(function (resolve, reject) {
|
if (this.options.cMapUrl !== null) {
|
||||||
function pump() {
|
// Only compressed CMaps are (currently) supported here.
|
||||||
reader.read().then(function ({ value, done }) {
|
const url = `${this.options.cMapUrl}${name}.bcmap`;
|
||||||
if (done) {
|
const response = await fetch(url);
|
||||||
return;
|
if (!response.ok) {
|
||||||
}
|
throw new Error(
|
||||||
resolve(value);
|
`fetchBuiltInCMap: failed to fetch file "${url}" with "${response.statusText}".`
|
||||||
pump();
|
);
|
||||||
}, reject);
|
|
||||||
}
|
}
|
||||||
pump();
|
data = {
|
||||||
});
|
cMapData: new Uint8Array(await response.arrayBuffer()),
|
||||||
|
compressionType: CMapCompressionType.BINARY,
|
||||||
|
};
|
||||||
|
} else {
|
||||||
|
// Get the data on the main-thread instead.
|
||||||
|
data = await this.handler.sendWithPromise("FetchBuiltInCMap", { name });
|
||||||
|
}
|
||||||
|
|
||||||
if (data.compressionType !== CMapCompressionType.NONE) {
|
if (data.compressionType !== CMapCompressionType.NONE) {
|
||||||
// Given the size of uncompressed CMaps, only cache compressed ones.
|
// Given the size of uncompressed CMaps, only cache compressed ones.
|
||||||
|
@ -412,6 +412,7 @@ class WorkerMessageHandler {
|
|||||||
isEvalSupported: data.isEvalSupported,
|
isEvalSupported: data.isEvalSupported,
|
||||||
fontExtraProperties: data.fontExtraProperties,
|
fontExtraProperties: data.fontExtraProperties,
|
||||||
useSystemFonts: data.useSystemFonts,
|
useSystemFonts: data.useSystemFonts,
|
||||||
|
cMapUrl: data.cMapUrl,
|
||||||
standardFontDataUrl: data.standardFontDataUrl,
|
standardFontDataUrl: data.standardFontDataUrl,
|
||||||
};
|
};
|
||||||
|
|
||||||
|
@ -152,19 +152,19 @@ function setPDFNetworkStreamFactory(pdfNetworkStreamFactory) {
|
|||||||
* reading built-in CMap files. Providing a custom factory is useful for
|
* reading built-in CMap files. Providing a custom factory is useful for
|
||||||
* environments without Fetch API or `XMLHttpRequest` support, such as
|
* environments without Fetch API or `XMLHttpRequest` support, such as
|
||||||
* Node.js. The default value is {DOMCMapReaderFactory}.
|
* Node.js. The default value is {DOMCMapReaderFactory}.
|
||||||
* @property {boolean} [useSystemFonts] - When true, fonts that aren't embedded
|
* @property {boolean} [useSystemFonts] - When `true`, fonts that aren't
|
||||||
* in the PDF will fallback to a system font. Defaults to true for web
|
* embedded in the PDF document will fallback to a system font.
|
||||||
* environments and false for node.
|
* The default value is `true` in web environments and `false` in Node.js.
|
||||||
* @property {string} [standardFontDataUrl] - The URL where the standard font
|
* @property {string} [standardFontDataUrl] - The URL where the standard font
|
||||||
* files are located. Include the trailing slash.
|
* files are located. Include the trailing slash.
|
||||||
* @property {boolean} [useWorkerFetch] - Enable using fetch in the worker for
|
|
||||||
* resources. This currently only used for fetching the font data from the
|
|
||||||
* worker thread. When `true`, StandardFontDataFactory will be ignored. The
|
|
||||||
* default value is `true` in web environment and `false` for Node.
|
|
||||||
* @property {Object} [StandardFontDataFactory] - The factory that will be used
|
* @property {Object} [StandardFontDataFactory] - The factory that will be used
|
||||||
* when reading the standard font files. Providing a custom factory is useful
|
* when reading the standard font files. Providing a custom factory is useful
|
||||||
* for environments without Fetch API or `XMLHttpRequest` support, such as
|
* for environments without Fetch API or `XMLHttpRequest` support, such as
|
||||||
* Node.js. The default value is {DOMStandardFontDataFactory}.
|
* Node.js. The default value is {DOMStandardFontDataFactory}.
|
||||||
|
* @property {boolean} [useWorkerFetch] - Enable using the Fetch API in the
|
||||||
|
* worker-thread when reading CMap and standard font files. When `true`,
|
||||||
|
* the `CMapReaderFactory` and `StandardFontDataFactory` options are ignored.
|
||||||
|
* The default value is `true` in web wenvironments and `false` in Node.js.
|
||||||
* @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
|
||||||
@ -333,6 +333,7 @@ function getDocument(src) {
|
|||||||
}
|
}
|
||||||
if (typeof params.useWorkerFetch !== "boolean") {
|
if (typeof params.useWorkerFetch !== "boolean") {
|
||||||
params.useWorkerFetch =
|
params.useWorkerFetch =
|
||||||
|
params.CMapReaderFactory === DOMCMapReaderFactory &&
|
||||||
params.StandardFontDataFactory === DOMStandardFontDataFactory;
|
params.StandardFontDataFactory === DOMStandardFontDataFactory;
|
||||||
}
|
}
|
||||||
if (typeof params.isEvalSupported !== "boolean") {
|
if (typeof params.isEvalSupported !== "boolean") {
|
||||||
@ -487,6 +488,7 @@ function _fetchDocument(worker, source, pdfDataRangeTransport, docId) {
|
|||||||
fontExtraProperties: source.fontExtraProperties,
|
fontExtraProperties: source.fontExtraProperties,
|
||||||
enableXfa: source.enableXfa,
|
enableXfa: source.enableXfa,
|
||||||
useSystemFonts: source.useSystemFonts,
|
useSystemFonts: source.useSystemFonts,
|
||||||
|
cMapUrl: source.useWorkerFetch ? source.cMapUrl : null,
|
||||||
standardFontDataUrl: source.useWorkerFetch
|
standardFontDataUrl: source.useWorkerFetch
|
||||||
? source.standardFontDataUrl
|
? source.standardFontDataUrl
|
||||||
: null,
|
: null,
|
||||||
@ -2680,36 +2682,19 @@ class WorkerTransport {
|
|||||||
this._onUnsupportedFeature.bind(this)
|
this._onUnsupportedFeature.bind(this)
|
||||||
);
|
);
|
||||||
|
|
||||||
|
messageHandler.on("FetchBuiltInCMap", data => {
|
||||||
|
if (this.destroyed) {
|
||||||
|
return Promise.reject(new Error("Worker was destroyed"));
|
||||||
|
}
|
||||||
|
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"));
|
||||||
}
|
}
|
||||||
return this.StandardFontDataFactory.fetch(data);
|
return this.StandardFontDataFactory.fetch(data);
|
||||||
});
|
});
|
||||||
|
|
||||||
messageHandler.on("FetchBuiltInCMap", (data, sink) => {
|
|
||||||
if (this.destroyed) {
|
|
||||||
sink.error(new Error("Worker was destroyed"));
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
let fetched = false;
|
|
||||||
|
|
||||||
sink.onPull = () => {
|
|
||||||
if (fetched) {
|
|
||||||
sink.close();
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
fetched = true;
|
|
||||||
|
|
||||||
this.CMapReaderFactory.fetch(data)
|
|
||||||
.then(function (builtInCMap) {
|
|
||||||
sink.enqueue(builtInCMap, 1, [builtInCMap.cMapData.buffer]);
|
|
||||||
})
|
|
||||||
.catch(function (reason) {
|
|
||||||
sink.error(reason);
|
|
||||||
});
|
|
||||||
};
|
|
||||||
});
|
|
||||||
}
|
}
|
||||||
|
|
||||||
_onUnsupportedFeature({ featureId }) {
|
_onUnsupportedFeature({ featureId }) {
|
||||||
|
@ -19,7 +19,7 @@
|
|||||||
|
|
||||||
const WAITING_TIME = 100; // ms
|
const WAITING_TIME = 100; // ms
|
||||||
const PDF_TO_CSS_UNITS = 96.0 / 72.0;
|
const PDF_TO_CSS_UNITS = 96.0 / 72.0;
|
||||||
const CMAP_URL = "../external/bcmaps/";
|
const CMAP_URL = "/build/generic/web/cmaps/";
|
||||||
const CMAP_PACKED = true;
|
const CMAP_PACKED = true;
|
||||||
const STANDARD_FONT_DATA_URL = "/build/generic/web/standard_fonts/";
|
const STANDARD_FONT_DATA_URL = "/build/generic/web/standard_fonts/";
|
||||||
const IMAGE_RESOURCES_PATH = "/web/images/";
|
const IMAGE_RESOURCES_PATH = "/web/images/";
|
||||||
|
@ -4046,6 +4046,15 @@
|
|||||||
"lastPage": 1,
|
"lastPage": 1,
|
||||||
"type": "eq"
|
"type": "eq"
|
||||||
},
|
},
|
||||||
|
{ "id": "mao-main_thread_fetch",
|
||||||
|
"file": "pdfs/mao.pdf",
|
||||||
|
"md5": "797093d67c4d4d4231ac6e1fb66bf6c3",
|
||||||
|
"rounds": 1,
|
||||||
|
"link": true,
|
||||||
|
"lastPage": 1,
|
||||||
|
"type": "eq",
|
||||||
|
"useWorkerFetch": false
|
||||||
|
},
|
||||||
{ "id": "mao-text",
|
{ "id": "mao-text",
|
||||||
"file": "pdfs/mao.pdf",
|
"file": "pdfs/mao.pdf",
|
||||||
"md5": "797093d67c4d4d4231ac6e1fb66bf6c3",
|
"md5": "797093d67c4d4d4231ac6e1fb66bf6c3",
|
||||||
|
Loading…
x
Reference in New Issue
Block a user