Terminate getOperationList and getTextContent every 20 ms

This commit is contained in:
Yury Delendik 2014-05-09 20:41:03 -05:00
parent d8eb8b1de1
commit 88aa396aca

View File

@ -38,6 +38,28 @@ var PartialEvaluator = (function PartialEvaluatorClosure() {
this.fontCache = fontCache; this.fontCache = fontCache;
} }
// Trying to minimize Date.now() usage and check every 100 time
var TIME_SLOT_DURATION_MS = 20;
var CHECK_TIME_EVERY = 100;
function TimeSlotManager() {
this.reset();
}
TimeSlotManager.prototype = {
check: function TimeSlotManager_check() {
if (++this.checked < CHECK_TIME_EVERY) {
return false;
}
this.checked = 0;
return this.endTime <= Date.now();
},
reset: function TimeSlotManager_reset() {
this.endTime = Date.now() + TIME_SLOT_DURATION_MS;
this.checked = 0;
}
};
var deferred = Promise.resolve();
var TILING_PATTERN = 1, SHADING_PATTERN = 2; var TILING_PATTERN = 1, SHADING_PATTERN = 2;
PartialEvaluator.prototype = { PartialEvaluator.prototype = {
@ -569,10 +591,13 @@ var PartialEvaluator = (function PartialEvaluatorClosure() {
var stateManager = new StateManager(initialState || new EvalState()); var stateManager = new StateManager(initialState || new EvalState());
var preprocessor = new EvaluatorPreprocessor(stream, xref, stateManager); var preprocessor = new EvaluatorPreprocessor(stream, xref, stateManager);
var shading; var shading;
var timeSlotManager = new TimeSlotManager();
return new Promise(function next(resolve, reject) { return new Promise(function next(resolve, reject) {
var operation, i, ii; timeSlotManager.reset();
while ((operation = preprocessor.read())) { var stop, operation, i, ii;
while (!(stop = timeSlotManager.check()) &&
(operation = preprocessor.read())) {
var args = operation.args; var args = operation.args;
var fn = operation.fn; var fn = operation.fn;
@ -735,6 +760,12 @@ var PartialEvaluator = (function PartialEvaluatorClosure() {
} }
operatorList.addOp(fn, args); operatorList.addOp(fn, args);
} }
if (stop) {
deferred.then(function () {
next(resolve, reject);
});
return;
}
// Some PDFs don't close all restores inside object/form. // Some PDFs don't close all restores inside object/form.
// Closing those for them. // Closing those for them.
for (i = 0, ii = preprocessor.savedStatesDepth; i < ii; i++) { for (i = 0, ii = preprocessor.savedStatesDepth; i < ii; i++) {
@ -900,8 +931,13 @@ var PartialEvaluator = (function PartialEvaluatorClosure() {
return textChunk; return textChunk;
} }
var timeSlotManager = new TimeSlotManager();
return new Promise(function next(resolve, reject) { return new Promise(function next(resolve, reject) {
while ((operation = preprocessor.read())) { timeSlotManager.reset();
var stop;
while (!(stop = timeSlotManager.check()) &&
(operation = preprocessor.read())) {
textState = stateManager.state; textState = stateManager.state;
var fn = operation.fn; var fn = operation.fn;
var args = operation.args; var args = operation.args;
@ -1073,7 +1109,12 @@ var PartialEvaluator = (function PartialEvaluatorClosure() {
break; break;
} // switch } // switch
} // while } // while
if (stop) {
deferred.then(function () {
next(resolve, reject);
});
return;
}
resolve(textContent); resolve(textContent);
}); });
}, },