Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add a PartialEvaluator helper for fetching CMap and Standard Font data #19021

Merged
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
50 changes: 21 additions & 29 deletions src/core/evaluator.js
Original file line number Diff line number Diff line change
Expand Up @@ -381,6 +381,16 @@ class PartialEvaluator {
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) {
const cachedData = this.builtInCMapCache.get(name);
if (cachedData) {
Expand All @@ -390,17 +400,10 @@ class PartialEvaluator {

if (this.options.cMapUrl !== null) {
// Only compressed CMaps are (currently) supported here.
const url = `${this.options.cMapUrl}${name}.bcmap`;
const response = await fetch(url);
if (!response.ok) {
throw new Error(
`fetchBuiltInCMap: failed to fetch file "${url}" with "${response.statusText}".`
);
}
data = {
cMapData: new Uint8Array(await response.arrayBuffer()),
isCompressed: true,
};
const cMapData = await this.#fetchData(
`${this.options.cMapUrl}${name}.bcmap`
);
data = { cMapData, isCompressed: true };
} else {
// Get the data on the main-thread instead.
data = await this.handler.sendWithPromise("FetchBuiltInCMap", { name });
Expand Down Expand Up @@ -431,30 +434,19 @@ class PartialEvaluator {
filename = standardFontNameToFileName[name];
let data;

if (this.options.standardFontDataUrl !== null) {
const url = `${this.options.standardFontDataUrl}${filename}`;
const response = await fetch(url);
if (!response.ok) {
warn(
`fetchStandardFontData: failed to fetch file "${url}" with "${response.statusText}".`
try {
if (this.options.standardFontDataUrl !== null) {
data = await this.#fetchData(
`${this.options.standardFontDataUrl}${filename}`
);
} else {
data = new Uint8Array(await response.arrayBuffer());
}
} else {
// Get the data on the main-thread instead.
try {
// Get the data on the main-thread instead.
data = await this.handler.sendWithPromise("FetchStandardFontData", {
filename,
});
} catch (e) {
warn(
`fetchStandardFontData: failed to fetch file "${filename}" with "${e}".`
);
}
}

if (!data) {
} catch (ex) {
warn(ex);
return null;
}
// Cache the "raw" standard font data, to avoid fetching it repeatedly
Expand Down