mirror of
https://github.com/mozilla/pdf.js.git
synced 2025-04-21 15:48:06 +02:00
Load built-in CMap files using the Fetch API when possible
This commit is contained in:
parent
df7756d2a4
commit
cbc07f985b
3 changed files with 75 additions and 40 deletions
|
@ -99,32 +99,31 @@ class NodeCMapReaderFactory {
|
|||
this.isCompressed = isCompressed;
|
||||
}
|
||||
|
||||
fetch({ name, }) {
|
||||
async fetch({ name, }) {
|
||||
if (!this.baseUrl) {
|
||||
return Promise.reject(new Error(
|
||||
throw new Error(
|
||||
'The CMap "baseUrl" parameter must be specified, ensure that ' +
|
||||
'the "cMapUrl" and "cMapPacked" API parameters are provided.'));
|
||||
'the "cMapUrl" and "cMapPacked" API parameters are provided.');
|
||||
}
|
||||
if (!name) {
|
||||
return Promise.reject(new Error('CMap name must be specified.'));
|
||||
throw new Error('CMap name must be specified.');
|
||||
}
|
||||
return new Promise((resolve, reject) => {
|
||||
let url = this.baseUrl + name + (this.isCompressed ? '.bcmap' : '');
|
||||
const url = this.baseUrl + name + (this.isCompressed ? '.bcmap' : '');
|
||||
const compressionType = (this.isCompressed ? CMapCompressionType.BINARY :
|
||||
CMapCompressionType.NONE);
|
||||
|
||||
let fs = require('fs');
|
||||
return new Promise((resolve, reject) => {
|
||||
const fs = require('fs');
|
||||
fs.readFile(url, (error, data) => {
|
||||
if (error || !data) {
|
||||
reject(new Error('Unable to load ' +
|
||||
(this.isCompressed ? 'binary ' : '') +
|
||||
'CMap at: ' + url));
|
||||
reject(new Error(error));
|
||||
return;
|
||||
}
|
||||
resolve({
|
||||
cMapData: new Uint8Array(data),
|
||||
compressionType: this.isCompressed ?
|
||||
CMapCompressionType.BINARY : CMapCompressionType.NONE,
|
||||
});
|
||||
resolve({ cMapData: new Uint8Array(data), compressionType, });
|
||||
});
|
||||
}).catch((reason) => {
|
||||
throw new Error(`Unable to load ${this.isCompressed ? 'binary ' : ''}` +
|
||||
`CMap at: ${url}`);
|
||||
});
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue