From 111419a64ab53786085f8a85ef256f5635bb19db Mon Sep 17 00:00:00 2001 From: Jonas Jenwald Date: Tue, 14 Feb 2017 14:28:31 +0100 Subject: [PATCH] Cache built-in binary CMap files in the worker (issue 4794) --- src/core/document.js | 11 ++++++++--- src/core/evaluator.js | 13 ++++++++++++- src/core/obj.js | 8 +++++--- test/unit/document_spec.js | 6 ++++-- 4 files changed, 29 insertions(+), 9 deletions(-) diff --git a/src/core/document.js b/src/core/document.js index 9b2e4eade..8d482a317 100644 --- a/src/core/document.js +++ b/src/core/document.js @@ -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); diff --git a/src/core/evaluator.js b/src/core/evaluator.js index bdcddea42..348af8a46 100644 --- a/src/core/evaluator.js +++ b/src/core/evaluator.js @@ -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; }); }; } diff --git a/src/core/obj.js b/src/core/obj.js index c45e543a5..035ff0383 100644 --- a/src/core/obj.js +++ b/src/core/obj.js @@ -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) ); } diff --git a/test/unit/document_spec.js b/test/unit/document_spec.js index cdec2ae6b..11f3fbad8 100644 --- a/test/unit/document_spec.js +++ b/test/unit/document_spec.js @@ -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;