Cache built-in binary CMap files in the worker (issue 4794)

This commit is contained in:
Jonas Jenwald 2017-02-14 14:28:31 +01:00
parent 769c1450b7
commit 111419a64a
4 changed files with 29 additions and 9 deletions

View File

@ -71,13 +71,15 @@ var Page = (function PageClosure() {
var DEFAULT_USER_UNIT = 1.0;
var LETTER_SIZE_MEDIABOX = [0, 0, 612, 792];
function Page(pdfManager, xref, pageIndex, pageDict, ref, fontCache) {
function Page(pdfManager, xref, pageIndex, pageDict, ref, fontCache,
builtInCMapCache) {
this.pdfManager = pdfManager;
this.pageIndex = pageIndex;
this.pageDict = pageDict;
this.xref = xref;
this.ref = ref;
this.fontCache = fontCache;
this.builtInCMapCache = builtInCMapCache;
this.evaluatorOptions = pdfManager.evaluatorOptions;
this.resourcesPromise = null;
@ -248,6 +250,7 @@ var Page = (function PageClosure() {
handler, this.pageIndex,
this.idFactory,
this.fontCache,
this.builtInCMapCache,
this.evaluatorOptions);
var dataPromises = Promise.all([contentStreamPromise, resourcesPromise]);
@ -315,6 +318,7 @@ var Page = (function PageClosure() {
handler, self.pageIndex,
self.idFactory,
self.fontCache,
self.builtInCMapCache,
self.evaluatorOptions);
return partialEvaluator.getTextContent(contentStream,
@ -551,9 +555,10 @@ var PDFDocument = (function PDFDocumentClosure() {
this.xref.parse(recoveryMode);
var self = this;
var pageFactory = {
createPage: function (pageIndex, dict, ref, fontCache) {
createPage: function (pageIndex, dict, ref, fontCache,
builtInCMapCache) {
return new Page(self.pdfManager, self.xref, pageIndex, dict, ref,
fontCache);
fontCache, builtInCMapCache);
}
};
this.catalog = new Catalog(this.pdfManager, this.xref, pageFactory);

View File

@ -170,18 +170,29 @@ var PartialEvaluator = (function PartialEvaluatorClosure() {
};
function PartialEvaluator(pdfManager, xref, handler, pageIndex,
idFactory, fontCache, options) {
idFactory, fontCache, builtInCMapCache, options) {
this.pdfManager = pdfManager;
this.xref = xref;
this.handler = handler;
this.pageIndex = pageIndex;
this.idFactory = idFactory;
this.fontCache = fontCache;
this.builtInCMapCache = builtInCMapCache;
this.options = options || DefaultPartialEvaluatorOptions;
this.fetchBuiltInCMap = function (name) {
var cachedCMap = builtInCMapCache[name];
if (cachedCMap) {
return Promise.resolve(cachedCMap);
}
return handler.sendWithPromise('FetchBuiltInCMap', {
name: name,
}).then(function (data) {
if (data.compressionType !== CMapCompressionType.NONE) {
// Given the size of uncompressed CMaps, only cache compressed ones.
builtInCMapCache[name] = data;
}
return data;
});
};
}

View File

@ -71,8 +71,8 @@ var Catalog = (function CatalogClosure() {
this.xref = xref;
this.catDict = xref.getCatalogObj();
this.fontCache = new RefSetCache();
assert(isDict(this.catDict),
'catalog object is not a dictionary');
this.builtInCMapCache = Object.create(null);
assert(isDict(this.catDict), 'catalog object is not a dictionary');
// TODO refactor to move getPage() to the PDFDocument.
this.pageFactory = pageFactory;
@ -428,6 +428,7 @@ var Catalog = (function CatalogClosure() {
delete font.translated;
}
this.fontCache.clear();
this.builtInCMapCache = Object.create(null);
}.bind(this));
},
@ -438,7 +439,8 @@ var Catalog = (function CatalogClosure() {
var dict = a[0];
var ref = a[1];
return this.pageFactory.createPage(pageIndex, dict, ref,
this.fontCache);
this.fontCache,
this.builtInCMapCache);
}.bind(this)
);
}

View File

@ -34,11 +34,13 @@ describe('document', function () {
var page1 = new Page(/* pdfManager = */ { }, /* xref = */ null,
/* pageIndex = */ 0,
/* pageDict = */ null, /* ref = */ null,
/* fontCache = */ null);
/* fontCache = */ null,
/* builtInCMapCache = */ null);
var page2 = new Page(/* pdfManager = */ { }, /* xref = */ null,
/* pageIndex = */ 1,
/* pageDict = */ null, /* ref = */ null,
/* fontCache = */ null);
/* fontCache = */ null,
/* builtInCMapCache = */ null);
var idFactory1 = page1.idFactory, idFactory2 = page2.idFactory;