Convert PDFFunction to a standard class with static methods

For e.g. `gulp mozcentral`, the *built* `pdf.worker.js` file decreases from `1 837 608` to `1 834 907` bytes with this patch-series.
The improvement comes first of all from less overall indentation in `PDFFunction`, and secondly from the removal of (now) unnecessary indirection in the code.
This commit is contained in:
Jonas Jenwald 2021-07-17 16:26:28 +02:00
parent d35fe3e796
commit 481af097b4

View File

@ -131,9 +131,8 @@ function toNumberArray(arr) {
return arr; return arr;
} }
const PDFFunction = (function PDFFunctionClosure() { class PDFFunction {
return { static getSampleArray(size, outputSize, bps, stream) {
getSampleArray(size, outputSize, bps, stream) {
let i, ii; let i, ii;
let length = 1; let length = 1;
for (i = 0, ii = size.length; i < ii; i++) { for (i = 0, ii = size.length; i < ii; i++) {
@ -160,9 +159,9 @@ const PDFFunction = (function PDFFunctionClosure() {
codeBuf &= (1 << codeSize) - 1; codeBuf &= (1 << codeSize) - 1;
} }
return array; return array;
}, }
parse({ xref, isEvalSupported, fn }) { static parse({ xref, isEvalSupported, fn }) {
const dict = fn.dict || fn; const dict = fn.dict || fn;
const typeNum = dict.get("FunctionType"); const typeNum = dict.get("FunctionType");
@ -179,9 +178,9 @@ const PDFFunction = (function PDFFunctionClosure() {
return this.constructPostScript({ xref, isEvalSupported, fn, dict }); return this.constructPostScript({ xref, isEvalSupported, fn, dict });
} }
throw new FormatError("Unknown type of function"); throw new FormatError("Unknown type of function");
}, }
parseArray({ xref, isEvalSupported, fnObj }) { static parseArray({ xref, isEvalSupported, fnObj }) {
if (!Array.isArray(fnObj)) { if (!Array.isArray(fnObj)) {
// not an array -- parsing as regular function // not an array -- parsing as regular function
return this.parse({ xref, isEvalSupported, fn: fnObj }); return this.parse({ xref, isEvalSupported, fn: fnObj });
@ -198,9 +197,9 @@ const PDFFunction = (function PDFFunctionClosure() {
fnArray[i](src, srcOffset, dest, destOffset + i); fnArray[i](src, srcOffset, dest, destOffset + i);
} }
}; };
}, }
constructSampled({ xref, isEvalSupported, fn, dict }) { static constructSampled({ xref, isEvalSupported, fn, dict }) {
function toMultiArray(arr) { function toMultiArray(arr) {
const inputLength = arr.length; const inputLength = arr.length;
const out = []; const out = [];
@ -328,15 +327,12 @@ const PDFFunction = (function PDFFunctionClosure() {
rj = interpolate(rj, 0, 1, decode[j][0], decode[j][1]); rj = interpolate(rj, 0, 1, decode[j][0], decode[j][1]);
// y_j = min(max(r_j, range_2j), range_2j+1) // y_j = min(max(r_j, range_2j), range_2j+1)
dest[destOffset + j] = Math.min( dest[destOffset + j] = Math.min(Math.max(rj, range[j][0]), range[j][1]);
Math.max(rj, range[j][0]),
range[j][1]
);
} }
}; };
}, }
constructInterpolated({ xref, isEvalSupported, dict }) { static constructInterpolated({ xref, isEvalSupported, dict }) {
const c0 = toNumberArray(dict.getArray("C0")) || [0]; const c0 = toNumberArray(dict.getArray("C0")) || [0];
const c1 = toNumberArray(dict.getArray("C1")) || [1]; const c1 = toNumberArray(dict.getArray("C1")) || [1];
const n = dict.get("N"); const n = dict.get("N");
@ -347,21 +343,16 @@ const PDFFunction = (function PDFFunctionClosure() {
} }
const length = diff.length; const length = diff.length;
return function constructInterpolatedFn( return function constructInterpolatedFn(src, srcOffset, dest, destOffset) {
src,
srcOffset,
dest,
destOffset
) {
const x = n === 1 ? src[srcOffset] : src[srcOffset] ** n; const x = n === 1 ? src[srcOffset] : src[srcOffset] ** n;
for (let j = 0; j < length; ++j) { for (let j = 0; j < length; ++j) {
dest[destOffset + j] = c0[j] + x * diff[j]; dest[destOffset + j] = c0[j] + x * diff[j];
} }
}; };
}, }
constructStiched({ xref, isEvalSupported, dict }) { static constructStiched({ xref, isEvalSupported, dict }) {
const domain = toNumberArray(dict.getArray("Domain")); const domain = toNumberArray(dict.getArray("Domain"));
if (!domain) { if (!domain) {
@ -429,9 +420,9 @@ const PDFFunction = (function PDFFunctionClosure() {
// call the appropriate function // call the appropriate function
fns[i](tmpBuf, 0, dest, destOffset); fns[i](tmpBuf, 0, dest, destOffset);
}; };
}, }
constructPostScript({ xref, isEvalSupported, fn, dict }) { static constructPostScript({ xref, isEvalSupported, fn, dict }) {
const domain = toNumberArray(dict.getArray("Domain")); const domain = toNumberArray(dict.getArray("Domain"));
const range = toNumberArray(dict.getArray("Range")); const range = toNumberArray(dict.getArray("Range"));
@ -454,13 +445,7 @@ const PDFFunction = (function PDFFunctionClosure() {
// subtraction, Math.max, and also contains 'var' and 'return' // subtraction, Math.max, and also contains 'var' and 'return'
// statements. See the generation in the PostScriptCompiler below. // statements. See the generation in the PostScriptCompiler below.
// eslint-disable-next-line no-new-func // eslint-disable-next-line no-new-func
return new Function( return new Function("src", "srcOffset", "dest", "destOffset", compiled);
"src",
"srcOffset",
"dest",
"destOffset",
compiled
);
} }
} }
info("Unable to compile PS function"); info("Unable to compile PS function");
@ -515,9 +500,8 @@ const PDFFunction = (function PDFFunctionClosure() {
} }
dest.set(output, destOffset); dest.set(output, destOffset);
}; };
}, }
}; }
})();
function isPDFFunction(v) { function isPDFFunction(v) {
let fnDict; let fnDict;