mirror of
https://github.com/mozilla/pdf.js.git
synced 2025-04-23 16:48:08 +02:00
Change the BaseCMapReaderFactory
fetch-helper to return a Uint8Array
This moves more functionality into the base-class, rather than having to duplicate that in the extending classes. For consistency, also updates the `BaseStandardFontDataFactory` and introduces more `async`/`await` in various relevant code.
This commit is contained in:
parent
d37e4b08e4
commit
df696063b9
3 changed files with 35 additions and 32 deletions
|
@ -127,22 +127,27 @@ class BaseCMapReaderFactory {
|
|||
throw new Error("CMap name must be specified.");
|
||||
}
|
||||
const url = this.baseUrl + name + (this.isCompressed ? ".bcmap" : "");
|
||||
const compressionType = this.isCompressed
|
||||
? CMapCompressionType.BINARY
|
||||
: CMapCompressionType.NONE;
|
||||
|
||||
return this._fetchData(url, compressionType).catch(reason => {
|
||||
throw new Error(
|
||||
`Unable to load ${this.isCompressed ? "binary " : ""}CMap at: ${url}`
|
||||
);
|
||||
});
|
||||
return this._fetch(url)
|
||||
.then(cMapData => ({
|
||||
cMapData,
|
||||
compressionType: this.isCompressed
|
||||
? CMapCompressionType.BINARY
|
||||
: CMapCompressionType.NONE,
|
||||
}))
|
||||
.catch(reason => {
|
||||
throw new Error(
|
||||
`Unable to load ${this.isCompressed ? "binary " : ""}CMap at: ${url}`
|
||||
);
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
* @ignore
|
||||
* @returns {Promise<Uint8Array>}
|
||||
*/
|
||||
_fetchData(url, compressionType) {
|
||||
unreachable("Abstract method `_fetchData` called.");
|
||||
async _fetch(url) {
|
||||
unreachable("Abstract method `_fetch` called.");
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -168,16 +173,17 @@ class BaseStandardFontDataFactory {
|
|||
}
|
||||
const url = `${this.baseUrl}${filename}`;
|
||||
|
||||
return this._fetchData(url).catch(reason => {
|
||||
return this._fetch(url).catch(reason => {
|
||||
throw new Error(`Unable to load font data at: ${url}`);
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
* @ignore
|
||||
* @returns {Promise<Uint8Array>}
|
||||
*/
|
||||
_fetchData(url) {
|
||||
unreachable("Abstract method `_fetchData` called.");
|
||||
async _fetch(url) {
|
||||
unreachable("Abstract method `_fetch` called.");
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
@ -564,17 +564,14 @@ class DOMCMapReaderFactory extends BaseCMapReaderFactory {
|
|||
/**
|
||||
* @ignore
|
||||
*/
|
||||
_fetchData(url, compressionType) {
|
||||
return fetchData(
|
||||
async _fetch(url) {
|
||||
const data = await fetchData(
|
||||
url,
|
||||
/* type = */ this.isCompressed ? "arraybuffer" : "text"
|
||||
).then(data => ({
|
||||
cMapData:
|
||||
data instanceof ArrayBuffer
|
||||
? new Uint8Array(data)
|
||||
: stringToBytes(data),
|
||||
compressionType,
|
||||
}));
|
||||
);
|
||||
return data instanceof ArrayBuffer
|
||||
? new Uint8Array(data)
|
||||
: stringToBytes(data);
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -582,10 +579,9 @@ class DOMStandardFontDataFactory extends BaseStandardFontDataFactory {
|
|||
/**
|
||||
* @ignore
|
||||
*/
|
||||
_fetchData(url) {
|
||||
return fetchData(url, /* type = */ "arraybuffer").then(
|
||||
data => new Uint8Array(data)
|
||||
);
|
||||
async _fetch(url) {
|
||||
const data = await fetchData(url, /* type = */ "arraybuffer");
|
||||
return new Uint8Array(data);
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
@ -113,10 +113,11 @@ class NodePackages {
|
|||
}
|
||||
}
|
||||
|
||||
const fetchData = function (url) {
|
||||
async function fetchData(url) {
|
||||
const fs = NodePackages.get("fs");
|
||||
return fs.promises.readFile(url).then(data => new Uint8Array(data));
|
||||
};
|
||||
const data = await fs.promises.readFile(url);
|
||||
return new Uint8Array(data);
|
||||
}
|
||||
|
||||
class NodeFilterFactory extends BaseFilterFactory {}
|
||||
|
||||
|
@ -134,8 +135,8 @@ class NodeCMapReaderFactory extends BaseCMapReaderFactory {
|
|||
/**
|
||||
* @ignore
|
||||
*/
|
||||
_fetchData(url, compressionType) {
|
||||
return fetchData(url).then(data => ({ cMapData: data, compressionType }));
|
||||
async _fetch(url) {
|
||||
return fetchData(url);
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -143,7 +144,7 @@ class NodeStandardFontDataFactory extends BaseStandardFontDataFactory {
|
|||
/**
|
||||
* @ignore
|
||||
*/
|
||||
_fetchData(url) {
|
||||
async _fetch(url) {
|
||||
return fetchData(url);
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue