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

Move the fetchBuiltInCMap method to the PartialEvaluator.prototype #12021

Merged
merged 1 commit into from
Jun 24, 2020
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
67 changes: 35 additions & 32 deletions src/core/evaluator.js
Original file line number Diff line number Diff line change
Expand Up @@ -116,34 +116,7 @@ var PartialEvaluator = (function PartialEvaluatorClosure() {
this.pdfFunctionFactory = pdfFunctionFactory;
this.parsingType3Font = false;

this.fetchBuiltInCMap = async name => {
if (this.builtInCMapCache.has(name)) {
return this.builtInCMapCache.get(name);
}
const readableStream = this.handler.sendWithStream("FetchBuiltInCMap", {
name,
});
const reader = readableStream.getReader();

const data = await new Promise(function (resolve, reject) {
function pump() {
reader.read().then(function ({ value, done }) {
if (done) {
return;
}
resolve(value);
pump();
}, reject);
}
pump();
});

if (data.compressionType !== CMapCompressionType.NONE) {
// Given the size of uncompressed CMaps, only cache compressed ones.
this.builtInCMapCache.set(name, data);
}
return data;
};
this._fetchBuiltInCMapBound = this.fetchBuiltInCMap.bind(this);
}

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

async fetchBuiltInCMap(name) {
const cachedData = this.builtInCMapCache.get(name);
if (cachedData) {
return cachedData;
}
const readableStream = this.handler.sendWithStream("FetchBuiltInCMap", {
name,
});
const reader = readableStream.getReader();

const data = await new Promise(function (resolve, reject) {
function pump() {
reader.read().then(function ({ value, done }) {
if (done) {
return;
}
resolve(value);
pump();
}, reject);
}
pump();
});

if (data.compressionType !== CMapCompressionType.NONE) {
// Given the size of uncompressed CMaps, only cache compressed ones.
this.builtInCMapCache.set(name, data);
}
return data;
},

async buildFormXObject(
resources,
xobj,
Expand Down Expand Up @@ -2691,7 +2694,7 @@ var PartialEvaluator = (function PartialEvaluatorClosure() {
// from the ASN Web site; see the Bibliography).
return CMapFactory.create({
encoding: ucs2CMapName,
fetchBuiltInCMap: this.fetchBuiltInCMap,
fetchBuiltInCMap: this._fetchBuiltInCMapBound,
useCMap: null,
}).then(function (ucs2CMap) {
const cMap = properties.cMap;
Expand Down Expand Up @@ -2724,7 +2727,7 @@ var PartialEvaluator = (function PartialEvaluatorClosure() {
if (isName(cmapObj)) {
return CMapFactory.create({
encoding: cmapObj,
fetchBuiltInCMap: this.fetchBuiltInCMap,
fetchBuiltInCMap: this._fetchBuiltInCMapBound,
useCMap: null,
}).then(function (cmap) {
if (cmap instanceof IdentityCMap) {
Expand All @@ -2735,7 +2738,7 @@ var PartialEvaluator = (function PartialEvaluatorClosure() {
} else if (isStream(cmapObj)) {
return CMapFactory.create({
encoding: cmapObj,
fetchBuiltInCMap: this.fetchBuiltInCMap,
fetchBuiltInCMap: this._fetchBuiltInCMapBound,
useCMap: null,
}).then(
function (cmap) {
Expand Down Expand Up @@ -3230,7 +3233,7 @@ var PartialEvaluator = (function PartialEvaluatorClosure() {
}
cMapPromise = CMapFactory.create({
encoding: cidEncoding,
fetchBuiltInCMap: this.fetchBuiltInCMap,
fetchBuiltInCMap: this._fetchBuiltInCMapBound,
useCMap: null,
}).then(function (cMap) {
properties.cMap = cMap;
Expand Down