Browse Source

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

Jonas Jenwald 8 years ago
parent
commit
111419a64a
  1. 11
      src/core/document.js
  2. 13
      src/core/evaluator.js
  3. 8
      src/core/obj.js
  4. 6
      test/unit/document_spec.js

11
src/core/document.js

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

13
src/core/evaluator.js

@ -170,18 +170,29 @@ var PartialEvaluator = (function PartialEvaluatorClosure() {
}; };
function PartialEvaluator(pdfManager, xref, handler, pageIndex, function PartialEvaluator(pdfManager, xref, handler, pageIndex,
idFactory, fontCache, options) { idFactory, fontCache, builtInCMapCache, options) {
this.pdfManager = pdfManager; this.pdfManager = pdfManager;
this.xref = xref; this.xref = xref;
this.handler = handler; this.handler = handler;
this.pageIndex = pageIndex; this.pageIndex = pageIndex;
this.idFactory = idFactory; this.idFactory = idFactory;
this.fontCache = fontCache; this.fontCache = fontCache;
this.builtInCMapCache = builtInCMapCache;
this.options = options || DefaultPartialEvaluatorOptions; this.options = options || DefaultPartialEvaluatorOptions;
this.fetchBuiltInCMap = function (name) { this.fetchBuiltInCMap = function (name) {
var cachedCMap = builtInCMapCache[name];
if (cachedCMap) {
return Promise.resolve(cachedCMap);
}
return handler.sendWithPromise('FetchBuiltInCMap', { return handler.sendWithPromise('FetchBuiltInCMap', {
name: name, 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;
}); });
}; };
} }

8
src/core/obj.js

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

6
test/unit/document_spec.js

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

Loading…
Cancel
Save