Skip to content

Commit 1911980

Browse files
Snuffleupaguspull[bot]
authored andcommitted
Add a PartialEvaluator helper for fetching CMap and Standard Font data
This avoids a little bit of code duplication, which cannot hurt.
1 parent 36896dd commit 1911980

File tree

1 file changed

+21
-29
lines changed

1 file changed

+21
-29
lines changed

src/core/evaluator.js

Lines changed: 21 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -381,6 +381,16 @@ class PartialEvaluator {
381381
return false;
382382
}
383383

384+
async #fetchData(url) {
385+
const response = await fetch(url);
386+
if (!response.ok) {
387+
throw new Error(
388+
`Failed to fetch file "${url}" with "${response.statusText}".`
389+
);
390+
}
391+
return new Uint8Array(await response.arrayBuffer());
392+
}
393+
384394
async fetchBuiltInCMap(name) {
385395
const cachedData = this.builtInCMapCache.get(name);
386396
if (cachedData) {
@@ -390,17 +400,10 @@ class PartialEvaluator {
390400

391401
if (this.options.cMapUrl !== null) {
392402
// Only compressed CMaps are (currently) supported here.
393-
const url = `${this.options.cMapUrl}${name}.bcmap`;
394-
const response = await fetch(url);
395-
if (!response.ok) {
396-
throw new Error(
397-
`fetchBuiltInCMap: failed to fetch file "${url}" with "${response.statusText}".`
398-
);
399-
}
400-
data = {
401-
cMapData: new Uint8Array(await response.arrayBuffer()),
402-
isCompressed: true,
403-
};
403+
const cMapData = await this.#fetchData(
404+
`${this.options.cMapUrl}${name}.bcmap`
405+
);
406+
data = { cMapData, isCompressed: true };
404407
} else {
405408
// Get the data on the main-thread instead.
406409
data = await this.handler.sendWithPromise("FetchBuiltInCMap", { name });
@@ -431,30 +434,19 @@ class PartialEvaluator {
431434
filename = standardFontNameToFileName[name];
432435
let data;
433436

434-
if (this.options.standardFontDataUrl !== null) {
435-
const url = `${this.options.standardFontDataUrl}${filename}`;
436-
const response = await fetch(url);
437-
if (!response.ok) {
438-
warn(
439-
`fetchStandardFontData: failed to fetch file "${url}" with "${response.statusText}".`
437+
try {
438+
if (this.options.standardFontDataUrl !== null) {
439+
data = await this.#fetchData(
440+
`${this.options.standardFontDataUrl}${filename}`
440441
);
441442
} else {
442-
data = new Uint8Array(await response.arrayBuffer());
443-
}
444-
} else {
445-
// Get the data on the main-thread instead.
446-
try {
443+
// Get the data on the main-thread instead.
447444
data = await this.handler.sendWithPromise("FetchStandardFontData", {
448445
filename,
449446
});
450-
} catch (e) {
451-
warn(
452-
`fetchStandardFontData: failed to fetch file "${filename}" with "${e}".`
453-
);
454447
}
455-
}
456-
457-
if (!data) {
448+
} catch (ex) {
449+
warn(ex);
458450
return null;
459451
}
460452
// Cache the "raw" standard font data, to avoid fetching it repeatedly

0 commit comments

Comments
 (0)