Merge pull request #13385 from Snuffleupagus/operator_list-class

Convert `src/core/operator_list.js` to use standard classes
This commit is contained in:
Tim van der Meij 2021-05-16 14:37:41 +02:00 committed by GitHub
commit 35c82af446
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -13,9 +13,8 @@
* limitations under the License. * limitations under the License.
*/ */
import { assert, ImageKind, OPS, warn } from "../shared/util.js"; import { assert, ImageKind, OPS, shadow, warn } from "../shared/util.js";
const QueueOptimizer = (function QueueOptimizerClosure() {
function addState(parentState, pattern, checkFn, iterateFn, processFn) { function addState(parentState, pattern, checkFn, iterateFn, processFn) {
let state = parentState; let state = parentState;
for (let i = 0, ii = pattern.length - 1; i < ii; i++) { for (let i = 0, ii = pattern.length - 1; i < ii; i++) {
@ -29,12 +28,7 @@ const QueueOptimizer = (function QueueOptimizerClosure() {
}; };
} }
function handlePaintSolidColorImageMask( function handlePaintSolidColorImageMask(iFirstSave, count, fnArray, argsArray) {
iFirstSave,
count,
fnArray,
argsArray
) {
// Handles special case of mainly LaTeX documents which use image masks to // Handles special case of mainly LaTeX documents which use image masks to
// draw lines with the current fill style. // draw lines with the current fill style.
// 'count' groups of (save, transform, paintImageMaskXObject, restore)+ // 'count' groups of (save, transform, paintImageMaskXObject, restore)+
@ -309,8 +303,7 @@ const QueueOptimizer = (function QueueOptimizerClosure() {
const argsArray = context.argsArray; const argsArray = context.argsArray;
const iFirstTransform = context.iCurr - 2; const iFirstTransform = context.iCurr - 2;
return ( return (
argsArray[iFirstTransform][1] === 0 && argsArray[iFirstTransform][1] === 0 && argsArray[iFirstTransform][2] === 0
argsArray[iFirstTransform][2] === 0
); );
}, },
function iterateImageGroup(context, i) { function iterateImageGroup(context, i) {
@ -491,9 +484,27 @@ const QueueOptimizer = (function QueueOptimizerClosure() {
} }
); );
// eslint-disable-next-line no-shadow class NullOptimizer {
function QueueOptimizer(queue) { constructor(queue) {
this.queue = queue; this.queue = queue;
}
_optimize() {}
push(fn, args) {
this.queue.fnArray.push(fn);
this.queue.argsArray.push(args);
this._optimize();
}
flush() {}
reset() {}
}
class QueueOptimizer extends NullOptimizer {
constructor(queue) {
super(queue);
this.state = null; this.state = null;
this.context = { this.context = {
iCurr: 0, iCurr: 0,
@ -504,7 +515,6 @@ const QueueOptimizer = (function QueueOptimizerClosure() {
this.lastProcessed = 0; this.lastProcessed = 0;
} }
QueueOptimizer.prototype = {
_optimize() { _optimize() {
// Process new fnArray item(s) chunk. // Process new fnArray item(s) chunk.
const fnArray = this.queue.fnArray; const fnArray = this.queue.fnArray;
@ -557,13 +567,7 @@ const QueueOptimizer = (function QueueOptimizerClosure() {
this.state = state; this.state = state;
this.match = match; this.match = match;
this.lastProcessed = i; this.lastProcessed = i;
}, }
push(fn, args) {
this.queue.fnArray.push(fn);
this.queue.argsArray.push(args);
this._optimize();
},
flush() { flush() {
while (this.match) { while (this.match) {
@ -574,43 +578,26 @@ const QueueOptimizer = (function QueueOptimizerClosure() {
// Repeat optimization until all chunks are exhausted. // Repeat optimization until all chunks are exhausted.
this._optimize(); this._optimize();
} }
}, }
reset() { reset() {
this.state = null; this.state = null;
this.match = null; this.match = null;
this.lastProcessed = 0; this.lastProcessed = 0;
}, }
};
return QueueOptimizer;
})();
const NullOptimizer = (function NullOptimizerClosure() {
// eslint-disable-next-line no-shadow
function NullOptimizer(queue) {
this.queue = queue;
} }
NullOptimizer.prototype = { class OperatorList {
push(fn, args) { static get CHUNK_SIZE() {
this.queue.fnArray.push(fn); return shadow(this, "CHUNK_SIZE", 1000);
this.queue.argsArray.push(args); }
},
flush() {}, // Close to chunk size.
static get CHUNK_SIZE_ABOUT() {
return shadow(this, "CHUNK_SIZE_ABOUT", OperatorList.CHUNK_SIZE - 5);
}
reset() {}, constructor(intent, streamSink) {
};
return NullOptimizer;
})();
const OperatorList = (function OperatorListClosure() {
const CHUNK_SIZE = 1000;
const CHUNK_SIZE_ABOUT = CHUNK_SIZE - 5; // close to chunk size
// eslint-disable-next-line no-shadow
function OperatorList(intent, streamSink) {
this._streamSink = streamSink; this._streamSink = streamSink;
this.fnArray = []; this.fnArray = [];
this.argsArray = []; this.argsArray = [];
@ -625,14 +612,13 @@ const OperatorList = (function OperatorListClosure() {
this._resolved = streamSink ? null : Promise.resolve(); this._resolved = streamSink ? null : Promise.resolve();
} }
OperatorList.prototype = {
get length() { get length() {
return this.argsArray.length; return this.argsArray.length;
}, }
get ready() { get ready() {
return this._resolved || this._streamSink.ready; return this._resolved || this._streamSink.ready;
}, }
/** /**
* @type {number} The total length of the entire operator list, since * @type {number} The total length of the entire operator list, since
@ -640,23 +626,23 @@ const OperatorList = (function OperatorListClosure() {
*/ */
get totalLength() { get totalLength() {
return this._totalLength + this.length; return this._totalLength + this.length;
}, }
addOp(fn, args) { addOp(fn, args) {
this.optimizer.push(fn, args); this.optimizer.push(fn, args);
this.weight++; this.weight++;
if (this._streamSink) { if (this._streamSink) {
if (this.weight >= CHUNK_SIZE) { if (this.weight >= OperatorList.CHUNK_SIZE) {
this.flush(); this.flush();
} else if ( } else if (
this.weight >= CHUNK_SIZE_ABOUT && this.weight >= OperatorList.CHUNK_SIZE_ABOUT &&
(fn === OPS.restore || fn === OPS.endText) (fn === OPS.restore || fn === OPS.endText)
) { ) {
// heuristic to flush on boundary of restore or endText // Heuristic to flush on boundary of restore or endText.
this.flush(); this.flush();
} }
} }
}, }
addDependency(dependency) { addDependency(dependency) {
if (this.dependencies.has(dependency)) { if (this.dependencies.has(dependency)) {
@ -664,13 +650,13 @@ const OperatorList = (function OperatorListClosure() {
} }
this.dependencies.add(dependency); this.dependencies.add(dependency);
this.addOp(OPS.dependency, [dependency]); this.addOp(OPS.dependency, [dependency]);
}, }
addDependencies(dependencies) { addDependencies(dependencies) {
for (const dependency of dependencies) { for (const dependency of dependencies) {
this.addDependency(dependency); this.addDependency(dependency);
} }
}, }
addOpList(opList) { addOpList(opList) {
if (!(opList instanceof OperatorList)) { if (!(opList instanceof OperatorList)) {
@ -683,7 +669,7 @@ const OperatorList = (function OperatorListClosure() {
for (let i = 0, ii = opList.length; i < ii; i++) { for (let i = 0, ii = opList.length; i < ii; i++) {
this.addOp(opList.fnArray[i], opList.argsArray[i]); this.addOp(opList.fnArray[i], opList.argsArray[i]);
} }
}, }
getIR() { getIR() {
return { return {
@ -691,7 +677,7 @@ const OperatorList = (function OperatorListClosure() {
argsArray: this.argsArray, argsArray: this.argsArray,
length: this.length, length: this.length,
}; };
}, }
get _transfers() { get _transfers() {
const transfers = []; const transfers = [];
@ -701,7 +687,7 @@ const OperatorList = (function OperatorListClosure() {
case OPS.paintInlineImageXObject: case OPS.paintInlineImageXObject:
case OPS.paintInlineImageXObjectGroup: case OPS.paintInlineImageXObjectGroup:
case OPS.paintImageMaskXObject: case OPS.paintImageMaskXObject:
const arg = argsArray[i][0]; // first param in imgData const arg = argsArray[i][0]; // First parameter in imgData.
if ( if (
typeof PDFJSDev === "undefined" || typeof PDFJSDev === "undefined" ||
@ -719,7 +705,7 @@ const OperatorList = (function OperatorListClosure() {
} }
} }
return transfers; return transfers;
}, }
flush(lastChunk = false) { flush(lastChunk = false) {
this.optimizer.flush(); this.optimizer.flush();
@ -742,10 +728,7 @@ const OperatorList = (function OperatorListClosure() {
this.argsArray.length = 0; this.argsArray.length = 0;
this.weight = 0; this.weight = 0;
this.optimizer.reset(); this.optimizer.reset();
}, }
}; }
return OperatorList;
})();
export { OperatorList }; export { OperatorList };