Skip to content

Commit ac4a5c3

Browse files
Merge pull request #12021 from Snuffleupagus/move-fetchBuiltInCMap
Move the `fetchBuiltInCMap` method to the `PartialEvaluator.prototype`
2 parents c1cb9ee + f070871 commit ac4a5c3

1 file changed

Lines changed: 35 additions & 32 deletions

File tree

src/core/evaluator.js

Lines changed: 35 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -116,34 +116,7 @@ var PartialEvaluator = (function PartialEvaluatorClosure() {
116116
this.pdfFunctionFactory = pdfFunctionFactory;
117117
this.parsingType3Font = false;
118118

119-
this.fetchBuiltInCMap = async name => {
120-
if (this.builtInCMapCache.has(name)) {
121-
return this.builtInCMapCache.get(name);
122-
}
123-
const readableStream = this.handler.sendWithStream("FetchBuiltInCMap", {
124-
name,
125-
});
126-
const reader = readableStream.getReader();
127-
128-
const data = await new Promise(function (resolve, reject) {
129-
function pump() {
130-
reader.read().then(function ({ value, done }) {
131-
if (done) {
132-
return;
133-
}
134-
resolve(value);
135-
pump();
136-
}, reject);
137-
}
138-
pump();
139-
});
140-
141-
if (data.compressionType !== CMapCompressionType.NONE) {
142-
// Given the size of uncompressed CMaps, only cache compressed ones.
143-
this.builtInCMapCache.set(name, data);
144-
}
145-
return data;
146-
};
119+
this._fetchBuiltInCMapBound = this.fetchBuiltInCMap.bind(this);
147120
}
148121

149122
// Trying to minimize Date.now() usage and check every 100 time
@@ -375,6 +348,36 @@ var PartialEvaluator = (function PartialEvaluatorClosure() {
375348
return false;
376349
},
377350

351+
async fetchBuiltInCMap(name) {
352+
const cachedData = this.builtInCMapCache.get(name);
353+
if (cachedData) {
354+
return cachedData;
355+
}
356+
const readableStream = this.handler.sendWithStream("FetchBuiltInCMap", {
357+
name,
358+
});
359+
const reader = readableStream.getReader();
360+
361+
const data = await new Promise(function (resolve, reject) {
362+
function pump() {
363+
reader.read().then(function ({ value, done }) {
364+
if (done) {
365+
return;
366+
}
367+
resolve(value);
368+
pump();
369+
}, reject);
370+
}
371+
pump();
372+
});
373+
374+
if (data.compressionType !== CMapCompressionType.NONE) {
375+
// Given the size of uncompressed CMaps, only cache compressed ones.
376+
this.builtInCMapCache.set(name, data);
377+
}
378+
return data;
379+
},
380+
378381
async buildFormXObject(
379382
resources,
380383
xobj,
@@ -2691,7 +2694,7 @@ var PartialEvaluator = (function PartialEvaluatorClosure() {
26912694
// from the ASN Web site; see the Bibliography).
26922695
return CMapFactory.create({
26932696
encoding: ucs2CMapName,
2694-
fetchBuiltInCMap: this.fetchBuiltInCMap,
2697+
fetchBuiltInCMap: this._fetchBuiltInCMapBound,
26952698
useCMap: null,
26962699
}).then(function (ucs2CMap) {
26972700
const cMap = properties.cMap;
@@ -2724,7 +2727,7 @@ var PartialEvaluator = (function PartialEvaluatorClosure() {
27242727
if (isName(cmapObj)) {
27252728
return CMapFactory.create({
27262729
encoding: cmapObj,
2727-
fetchBuiltInCMap: this.fetchBuiltInCMap,
2730+
fetchBuiltInCMap: this._fetchBuiltInCMapBound,
27282731
useCMap: null,
27292732
}).then(function (cmap) {
27302733
if (cmap instanceof IdentityCMap) {
@@ -2735,7 +2738,7 @@ var PartialEvaluator = (function PartialEvaluatorClosure() {
27352738
} else if (isStream(cmapObj)) {
27362739
return CMapFactory.create({
27372740
encoding: cmapObj,
2738-
fetchBuiltInCMap: this.fetchBuiltInCMap,
2741+
fetchBuiltInCMap: this._fetchBuiltInCMapBound,
27392742
useCMap: null,
27402743
}).then(
27412744
function (cmap) {
@@ -3230,7 +3233,7 @@ var PartialEvaluator = (function PartialEvaluatorClosure() {
32303233
}
32313234
cMapPromise = CMapFactory.create({
32323235
encoding: cidEncoding,
3233-
fetchBuiltInCMap: this.fetchBuiltInCMap,
3236+
fetchBuiltInCMap: this._fetchBuiltInCMapBound,
32343237
useCMap: null,
32353238
}).then(function (cMap) {
32363239
properties.cMap = cMap;

0 commit comments

Comments
 (0)