mirror of
https://github.com/mozilla/pdf.js.git
synced 2025-04-26 10:08:06 +02:00
Cleaning up fonts when viewer is idle for some time
This commit is contained in:
parent
945e370d4f
commit
e712c4136a
9 changed files with 93 additions and 9 deletions
|
@ -25,12 +25,13 @@
|
|||
|
||||
var Page = (function PageClosure() {
|
||||
|
||||
function Page(pdfManager, xref, pageIndex, pageDict, ref) {
|
||||
function Page(pdfManager, xref, pageIndex, pageDict, ref, fontCache) {
|
||||
this.pdfManager = pdfManager;
|
||||
this.pageIndex = pageIndex;
|
||||
this.pageDict = pageDict;
|
||||
this.xref = xref;
|
||||
this.ref = ref;
|
||||
this.fontCache = fontCache;
|
||||
this.idCounters = {
|
||||
obj: 0
|
||||
};
|
||||
|
@ -160,7 +161,7 @@ var Page = (function PageClosure() {
|
|||
var partialEvaluator = new PartialEvaluator(
|
||||
pdfManager, this.xref, handler,
|
||||
this.pageIndex, 'p' + this.pageIndex + '_',
|
||||
this.idCounters);
|
||||
this.idCounters, this.fontCache);
|
||||
|
||||
var dataPromises = Promise.all(
|
||||
[contentStreamPromise, resourcesPromise], reject);
|
||||
|
@ -226,7 +227,7 @@ var Page = (function PageClosure() {
|
|||
var partialEvaluator = new PartialEvaluator(
|
||||
pdfManager, self.xref, handler,
|
||||
self.pageIndex, 'p' + self.pageIndex + '_',
|
||||
self.idCounters);
|
||||
self.idCounters, self.fontCache);
|
||||
|
||||
var bidiTexts = partialEvaluator.getTextContent(contentStream,
|
||||
self.resources);
|
||||
|
@ -496,6 +497,10 @@ var PDFDocument = (function PDFDocumentClosure() {
|
|||
|
||||
getPage: function PDFDocument_getPage(pageIndex) {
|
||||
return this.catalog.getPage(pageIndex);
|
||||
},
|
||||
|
||||
cleanup: function PDFDocument_cleanup() {
|
||||
return this.catalog.cleanup();
|
||||
}
|
||||
};
|
||||
|
||||
|
|
|
@ -26,7 +26,7 @@
|
|||
|
||||
var PartialEvaluator = (function PartialEvaluatorClosure() {
|
||||
function PartialEvaluator(pdfManager, xref, handler, pageIndex,
|
||||
uniquePrefix, idCounters) {
|
||||
uniquePrefix, idCounters, fontCache) {
|
||||
this.state = new EvalState();
|
||||
this.stateStack = [];
|
||||
|
||||
|
@ -36,7 +36,7 @@ var PartialEvaluator = (function PartialEvaluatorClosure() {
|
|||
this.pageIndex = pageIndex;
|
||||
this.uniquePrefix = uniquePrefix;
|
||||
this.idCounters = idCounters;
|
||||
this.fontCache = new RefSetCache();
|
||||
this.fontCache = fontCache;
|
||||
}
|
||||
|
||||
// Specifies properties for each command
|
||||
|
|
|
@ -189,7 +189,7 @@ var RefSet = (function RefSetClosure() {
|
|||
|
||||
var RefSetCache = (function RefSetCacheClosure() {
|
||||
function RefSetCache() {
|
||||
this.dict = {};
|
||||
this.dict = Object.create(null);
|
||||
}
|
||||
|
||||
RefSetCache.prototype = {
|
||||
|
@ -203,6 +203,16 @@ var RefSetCache = (function RefSetCacheClosure() {
|
|||
|
||||
put: function RefSetCache_put(ref, obj) {
|
||||
this.dict['R' + ref.num + '.' + ref.gen] = obj;
|
||||
},
|
||||
|
||||
forEach: function RefSetCache_forEach(fn, thisArg) {
|
||||
for (var i in this.dict) {
|
||||
fn.call(thisArg, this.dict[i]);
|
||||
}
|
||||
},
|
||||
|
||||
clear: function RefSetCache_clear() {
|
||||
this.dict = Object.create(null);
|
||||
}
|
||||
};
|
||||
|
||||
|
@ -214,6 +224,7 @@ var Catalog = (function CatalogClosure() {
|
|||
this.pdfManager = pdfManager;
|
||||
this.xref = xref;
|
||||
this.catDict = xref.getCatalogObj();
|
||||
this.fontCache = new RefSetCache();
|
||||
assertWellFormed(isDict(this.catDict),
|
||||
'catalog object is not a dictionary');
|
||||
|
||||
|
@ -400,13 +411,22 @@ var Catalog = (function CatalogClosure() {
|
|||
return shadow(this, 'javaScript', javaScript);
|
||||
},
|
||||
|
||||
cleanup: function Catalog_cleanup() {
|
||||
this.fontCache.forEach(function (font) {
|
||||
delete font.sent;
|
||||
delete font.translated;
|
||||
});
|
||||
this.fontCache.clear();
|
||||
},
|
||||
|
||||
getPage: function Catalog_getPage(pageIndex) {
|
||||
if (!(pageIndex in this.pagePromises)) {
|
||||
this.pagePromises[pageIndex] = this.getPageDict(pageIndex).then(
|
||||
function (a) {
|
||||
var dict = a[0];
|
||||
var ref = a[1];
|
||||
return new Page(this.pdfManager, this.xref, pageIndex, dict, ref);
|
||||
return new Page(this.pdfManager, this.xref, pageIndex, dict, ref,
|
||||
this.fontCache);
|
||||
}.bind(this)
|
||||
);
|
||||
}
|
||||
|
|
|
@ -46,6 +46,10 @@ var BasePdfManager = (function BasePdfManagerClosure() {
|
|||
return this.pdfModel.getPage(pageIndex);
|
||||
},
|
||||
|
||||
cleanup: function BasePdfManager_cleanup() {
|
||||
return this.pdfModel.cleanup();
|
||||
},
|
||||
|
||||
ensure: function BasePdfManager_ensure(obj, prop, args) {
|
||||
return new NotImplementedException();
|
||||
},
|
||||
|
|
|
@ -373,6 +373,11 @@ var WorkerMessageHandler = PDFJS.WorkerMessageHandler = {
|
|||
});
|
||||
});
|
||||
|
||||
handler.on('Cleanup', function wphCleanup(data, promise) {
|
||||
pdfManager.cleanup();
|
||||
promise.resolve(true);
|
||||
});
|
||||
|
||||
handler.on('Terminate', function wphTerminate(data, promise) {
|
||||
pdfManager.terminate();
|
||||
promise.resolve();
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue