Merge pull request #19021 from Snuffleupagus/PartialEvaluator-#fetchData

Add a `PartialEvaluator` helper for fetching CMap and Standard Font data
This commit is contained in:
Tim van der Meij 2024-11-12 19:56:39 +01:00 committed by GitHub
commit 6676492920
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194

View File

@ -381,6 +381,16 @@ class PartialEvaluator {
return false; return false;
} }
async #fetchData(url) {
const response = await fetch(url);
if (!response.ok) {
throw new Error(
`Failed to fetch file "${url}" with "${response.statusText}".`
);
}
return new Uint8Array(await response.arrayBuffer());
}
async fetchBuiltInCMap(name) { async fetchBuiltInCMap(name) {
const cachedData = this.builtInCMapCache.get(name); const cachedData = this.builtInCMapCache.get(name);
if (cachedData) { if (cachedData) {
@ -390,17 +400,10 @@ class PartialEvaluator {
if (this.options.cMapUrl !== null) { if (this.options.cMapUrl !== null) {
// Only compressed CMaps are (currently) supported here. // Only compressed CMaps are (currently) supported here.
const url = `${this.options.cMapUrl}${name}.bcmap`; const cMapData = await this.#fetchData(
const response = await fetch(url); `${this.options.cMapUrl}${name}.bcmap`
if (!response.ok) { );
throw new Error( data = { cMapData, isCompressed: true };
`fetchBuiltInCMap: failed to fetch file "${url}" with "${response.statusText}".`
);
}
data = {
cMapData: new Uint8Array(await response.arrayBuffer()),
isCompressed: true,
};
} else { } else {
// Get the data on the main-thread instead. // Get the data on the main-thread instead.
data = await this.handler.sendWithPromise("FetchBuiltInCMap", { name }); data = await this.handler.sendWithPromise("FetchBuiltInCMap", { name });
@ -431,30 +434,19 @@ class PartialEvaluator {
filename = standardFontNameToFileName[name]; filename = standardFontNameToFileName[name];
let data; let data;
if (this.options.standardFontDataUrl !== null) { try {
const url = `${this.options.standardFontDataUrl}${filename}`; if (this.options.standardFontDataUrl !== null) {
const response = await fetch(url); data = await this.#fetchData(
if (!response.ok) { `${this.options.standardFontDataUrl}${filename}`
warn(
`fetchStandardFontData: failed to fetch file "${url}" with "${response.statusText}".`
); );
} else { } else {
data = new Uint8Array(await response.arrayBuffer()); // Get the data on the main-thread instead.
}
} else {
// Get the data on the main-thread instead.
try {
data = await this.handler.sendWithPromise("FetchStandardFontData", { data = await this.handler.sendWithPromise("FetchStandardFontData", {
filename, filename,
}); });
} catch (e) {
warn(
`fetchStandardFontData: failed to fetch file "${filename}" with "${e}".`
);
} }
} } catch (ex) {
warn(ex);
if (!data) {
return null; return null;
} }
// Cache the "raw" standard font data, to avoid fetching it repeatedly // Cache the "raw" standard font data, to avoid fetching it repeatedly