Merge pull request #13304 from Snuffleupagus/src-core-classes

Convert more code in `src/core/` to use standard classes
This commit is contained in:
Tim van der Meij 2021-04-27 19:37:09 +02:00 committed by GitHub
commit fae183b7cc
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
5 changed files with 978 additions and 1015 deletions

View File

@ -19,15 +19,14 @@
* license. * license.
*/ */
import { info } from "../shared/util.js";
/** /**
* @typedef {Object} CCITTFaxDecoderSource * @typedef {Object} CCITTFaxDecoderSource
* @property {function} next - Method that return one byte of data for decoding, * @property {function} next - Method that return one byte of data for decoding,
* or -1 when EOF is reached. * or -1 when EOF is reached.
*/ */
import { info } from "../shared/util.js";
const CCITTFaxDecoder = (function CCITTFaxDecoder() {
const ccittEOL = -2; const ccittEOL = -2;
const ccittEOF = -1; const ccittEOF = -1;
const twoDimPass = 0; const twoDimPass = 0;
@ -465,8 +464,8 @@ const CCITTFaxDecoder = (function CCITTFaxDecoder() {
* @param {CCITTFaxDecoderSource} source - The data which should be decoded. * @param {CCITTFaxDecoderSource} source - The data which should be decoded.
* @param {Object} [options] - Decoding options. * @param {Object} [options] - Decoding options.
*/ */
// eslint-disable-next-line no-shadow class CCITTFaxDecoder {
function CCITTFaxDecoder(source, options = {}) { constructor(source, options = {}) {
if (!source || typeof source.next !== "function") { if (!source || typeof source.next !== "function") {
throw new Error('CCITTFaxDecoder - invalid "source" parameter.'); throw new Error('CCITTFaxDecoder - invalid "source" parameter.');
} }
@ -511,7 +510,6 @@ const CCITTFaxDecoder = (function CCITTFaxDecoder() {
} }
} }
CCITTFaxDecoder.prototype = {
readNextChar() { readNextChar() {
if (this.eof) { if (this.eof) {
return -1; return -1;
@ -569,10 +567,7 @@ const CCITTFaxDecoder = (function CCITTFaxDecoder() {
code2 += code3 = this._getBlackCode(); code2 += code3 = this._getBlackCode();
} while (code3 >= 64); } while (code3 >= 64);
} }
this._addPixels( this._addPixels(codingLine[this.codingPos] + code1, blackPixels);
codingLine[this.codingPos] + code1,
blackPixels
);
if (codingLine[this.codingPos] < columns) { if (codingLine[this.codingPos] < columns) {
this._addPixels( this._addPixels(
codingLine[this.codingPos] + code2, codingLine[this.codingPos] + code2,
@ -845,7 +840,7 @@ const CCITTFaxDecoder = (function CCITTFaxDecoder() {
c ^= 0xff; c ^= 0xff;
} }
return c; return c;
}, }
/** /**
* @private * @private
@ -867,7 +862,7 @@ const CCITTFaxDecoder = (function CCITTFaxDecoder() {
codingLine[codingPos] = a1; codingLine[codingPos] = a1;
} }
this.codingPos = codingPos; this.codingPos = codingPos;
}, }
/** /**
* @private * @private
@ -900,7 +895,7 @@ const CCITTFaxDecoder = (function CCITTFaxDecoder() {
} }
this.codingPos = codingPos; this.codingPos = codingPos;
}, }
/** /**
* This function returns the code found from the table. * This function returns the code found from the table.
@ -930,7 +925,7 @@ const CCITTFaxDecoder = (function CCITTFaxDecoder() {
} }
} }
return [false, 0, false]; return [false, 0, false];
}, }
/** /**
* @private * @private
@ -953,7 +948,7 @@ const CCITTFaxDecoder = (function CCITTFaxDecoder() {
} }
info("Bad two dim code"); info("Bad two dim code");
return ccittEOF; return ccittEOF;
}, }
/** /**
* @private * @private
@ -991,7 +986,7 @@ const CCITTFaxDecoder = (function CCITTFaxDecoder() {
info("bad white code"); info("bad white code");
this._eatBits(1); this._eatBits(1);
return 1; return 1;
}, }
/** /**
* @private * @private
@ -1034,7 +1029,7 @@ const CCITTFaxDecoder = (function CCITTFaxDecoder() {
info("bad black code"); info("bad black code");
this._eatBits(1); this._eatBits(1);
return 1; return 1;
}, }
/** /**
* @private * @private
@ -1052,7 +1047,7 @@ const CCITTFaxDecoder = (function CCITTFaxDecoder() {
this.inputBits += 8; this.inputBits += 8;
} }
return (this.inputBuf >> (this.inputBits - n)) & (0xffff >> (16 - n)); return (this.inputBuf >> (this.inputBits - n)) & (0xffff >> (16 - n));
}, }
/** /**
* @private * @private
@ -1061,10 +1056,7 @@ const CCITTFaxDecoder = (function CCITTFaxDecoder() {
if ((this.inputBits -= n) < 0) { if ((this.inputBits -= n) < 0) {
this.inputBits = 0; this.inputBits = 0;
} }
}, }
}; }
return CCITTFaxDecoder;
})();
export { CCITTFaxDecoder }; export { CCITTFaxDecoder };

View File

@ -17,9 +17,10 @@ import { Dict, isDict } from "./primitives.js";
import { CCITTFaxDecoder } from "./ccitt.js"; import { CCITTFaxDecoder } from "./ccitt.js";
import { DecodeStream } from "./stream.js"; import { DecodeStream } from "./stream.js";
const CCITTFaxStream = (function CCITTFaxStreamClosure() { class CCITTFaxStream extends DecodeStream {
// eslint-disable-next-line no-shadow constructor(str, maybeLength, params) {
function CCITTFaxStream(str, maybeLength, params) { super(maybeLength);
this.str = str; this.str = str;
this.dict = str.dict; this.dict = str.dict;
@ -41,13 +42,9 @@ const CCITTFaxStream = (function CCITTFaxStreamClosure() {
EndOfBlock: params.get("EndOfBlock"), EndOfBlock: params.get("EndOfBlock"),
BlackIs1: params.get("BlackIs1"), BlackIs1: params.get("BlackIs1"),
}); });
DecodeStream.call(this, maybeLength);
} }
CCITTFaxStream.prototype = Object.create(DecodeStream.prototype); readBlock() {
CCITTFaxStream.prototype.readBlock = function () {
while (!this.eof) { while (!this.eof) {
const c = this.ccittFaxDecoder.readNextChar(); const c = this.ccittFaxDecoder.readNextChar();
if (c === -1) { if (c === -1) {
@ -57,9 +54,7 @@ const CCITTFaxStream = (function CCITTFaxStreamClosure() {
this.ensureBuffer(this.bufferLength + 1); this.ensureBuffer(this.bufferLength + 1);
this.buffer[this.bufferLength++] = c; this.buffer[this.bufferLength++] = c;
} }
}; }
}
return CCITTFaxStream;
})();
export { CCITTFaxStream }; export { CCITTFaxStream };

View File

@ -22,33 +22,27 @@ import { shadow } from "../shared/util.js";
* For JBIG2's we use a library to decode these images and * For JBIG2's we use a library to decode these images and
* the stream behaves like all the other DecodeStreams. * the stream behaves like all the other DecodeStreams.
*/ */
const Jbig2Stream = (function Jbig2StreamClosure() { class Jbig2Stream extends DecodeStream {
// eslint-disable-next-line no-shadow constructor(stream, maybeLength, dict, params) {
function Jbig2Stream(stream, maybeLength, dict, params) { super(maybeLength);
this.stream = stream; this.stream = stream;
this.maybeLength = maybeLength; this.maybeLength = maybeLength;
this.dict = dict; this.dict = dict;
this.params = params; this.params = params;
DecodeStream.call(this, maybeLength);
} }
Jbig2Stream.prototype = Object.create(DecodeStream.prototype); get bytes() {
Object.defineProperty(Jbig2Stream.prototype, "bytes", {
get() {
// If `this.maybeLength` is null, we'll get the entire stream. // If `this.maybeLength` is null, we'll get the entire stream.
return shadow(this, "bytes", this.stream.getBytes(this.maybeLength)); return shadow(this, "bytes", this.stream.getBytes(this.maybeLength));
}, }
configurable: true,
});
Jbig2Stream.prototype.ensureBuffer = function (requested) { ensureBuffer(requested) {
// No-op, since `this.readBlock` will always parse the entire image and // No-op, since `this.readBlock` will always parse the entire image and
// directly insert all of its data into `this.buffer`. // directly insert all of its data into `this.buffer`.
}; }
Jbig2Stream.prototype.readBlock = function () { readBlock() {
if (this.eof) { if (this.eof) {
return; return;
} }
@ -73,9 +67,7 @@ const Jbig2Stream = (function Jbig2StreamClosure() {
this.buffer = data; this.buffer = data;
this.bufferLength = dataLength; this.bufferLength = dataLength;
this.eof = true; this.eof = true;
}; }
}
return Jbig2Stream;
})();
export { Jbig2Stream }; export { Jbig2Stream };

View File

@ -22,9 +22,8 @@ import { shadow } from "../shared/util.js";
* For JPEG's we use a library to decode these images and the stream behaves * For JPEG's we use a library to decode these images and the stream behaves
* like all the other DecodeStreams. * like all the other DecodeStreams.
*/ */
const JpegStream = (function JpegStreamClosure() { class JpegStream extends DecodeStream {
// eslint-disable-next-line no-shadow constructor(stream, maybeLength, dict, params) {
function JpegStream(stream, maybeLength, dict, params) {
// Some images may contain 'junk' before the SOI (start-of-image) marker. // Some images may contain 'junk' before the SOI (start-of-image) marker.
// Note: this seems to mainly affect inline images. // Note: this seems to mainly affect inline images.
let ch; let ch;
@ -35,30 +34,25 @@ const JpegStream = (function JpegStreamClosure() {
break; break;
} }
} }
super(maybeLength);
this.stream = stream; this.stream = stream;
this.maybeLength = maybeLength; this.maybeLength = maybeLength;
this.dict = dict; this.dict = dict;
this.params = params; this.params = params;
DecodeStream.call(this, maybeLength);
} }
JpegStream.prototype = Object.create(DecodeStream.prototype); get bytes() {
Object.defineProperty(JpegStream.prototype, "bytes", {
get: function JpegStream_bytes() {
// If `this.maybeLength` is null, we'll get the entire stream. // If `this.maybeLength` is null, we'll get the entire stream.
return shadow(this, "bytes", this.stream.getBytes(this.maybeLength)); return shadow(this, "bytes", this.stream.getBytes(this.maybeLength));
}, }
configurable: true,
});
JpegStream.prototype.ensureBuffer = function (requested) { ensureBuffer(requested) {
// No-op, since `this.readBlock` will always parse the entire image and // No-op, since `this.readBlock` will always parse the entire image and
// directly insert all of its data into `this.buffer`. // directly insert all of its data into `this.buffer`.
}; }
JpegStream.prototype.readBlock = function () { readBlock() {
if (this.eof) { if (this.eof) {
return; return;
} }
@ -105,9 +99,7 @@ const JpegStream = (function JpegStreamClosure() {
this.buffer = data; this.buffer = data;
this.bufferLength = data.length; this.bufferLength = data.length;
this.eof = true; this.eof = true;
}; }
}
return JpegStream;
})();
export { JpegStream }; export { JpegStream };

View File

@ -21,33 +21,27 @@ import { shadow } from "../shared/util.js";
* For JPEG 2000's we use a library to decode these images and * For JPEG 2000's we use a library to decode these images and
* the stream behaves like all the other DecodeStreams. * the stream behaves like all the other DecodeStreams.
*/ */
const JpxStream = (function JpxStreamClosure() { class JpxStream extends DecodeStream {
// eslint-disable-next-line no-shadow constructor(stream, maybeLength, dict, params) {
function JpxStream(stream, maybeLength, dict, params) { super(maybeLength);
this.stream = stream; this.stream = stream;
this.maybeLength = maybeLength; this.maybeLength = maybeLength;
this.dict = dict; this.dict = dict;
this.params = params; this.params = params;
DecodeStream.call(this, maybeLength);
} }
JpxStream.prototype = Object.create(DecodeStream.prototype); get bytes() {
Object.defineProperty(JpxStream.prototype, "bytes", {
get: function JpxStream_bytes() {
// If `this.maybeLength` is null, we'll get the entire stream. // If `this.maybeLength` is null, we'll get the entire stream.
return shadow(this, "bytes", this.stream.getBytes(this.maybeLength)); return shadow(this, "bytes", this.stream.getBytes(this.maybeLength));
}, }
configurable: true,
});
JpxStream.prototype.ensureBuffer = function (requested) { ensureBuffer(requested) {
// No-op, since `this.readBlock` will always parse the entire image and // No-op, since `this.readBlock` will always parse the entire image and
// directly insert all of its data into `this.buffer`. // directly insert all of its data into `this.buffer`.
}; }
JpxStream.prototype.readBlock = function () { readBlock() {
if (this.eof) { if (this.eof) {
return; return;
} }
@ -87,9 +81,7 @@ const JpxStream = (function JpxStreamClosure() {
} }
this.bufferLength = this.buffer.length; this.bufferLength = this.buffer.length;
this.eof = true; this.eof = true;
}; }
}
return JpxStream;
})();
export { JpxStream }; export { JpxStream };