UNPKG

@syncfusion/ej2-pdf

Version:

Feature-rich JavaScript PDF library with built-in support for loading and manipulating PDF document.

153 lines (152 loc) 5.68 kB
import { _copyRange } from './../utils'; /** * Provides a circular output buffer for decompressed data with efficient write and copy helpers. * * @private */ var _DecompressedOutput = /** @class */ (function () { function _DecompressedOutput() { this._end = 0; this._usedBytes = 0; this._dOutput = Array(_DecompressedOutput._dOutSize).fill(0); this._end = 0; this._usedBytes = 0; } Object.defineProperty(_DecompressedOutput.prototype, "_unusedBytes", { /** * Returns the remaining free capacity in the output ring buffer. * * @private * @returns {number} unusedBytes. */ get: function () { return _DecompressedOutput._dOutSize - this._usedBytes; }, enumerable: true, configurable: true }); /** * Writes a single byte into the ring buffer and advances the write position. * * @private * @param {number} b Byte to write. * @returns {void} nothing. */ _DecompressedOutput.prototype._write = function (b) { this._dOutput[this._end++] = b; this._end &= _DecompressedOutput._dOutMask; ++this._usedBytes; }; /** * Copies a back reference of the given length and distance from previously written output. * * @private * @param {number} length Number of bytes to copy. * @param {number} distance Distance back from current position. * @returns {void} nothing. */ _DecompressedOutput.prototype._writeLD = function (length, distance) { this._usedBytes += length; var copyStart = (this._end - distance) & _DecompressedOutput._dOutMask; var border = _DecompressedOutput._dOutSize - length; if (copyStart <= border && this._end < border) { if (length <= distance) { _copyRange(this._dOutput, this._end, this._dOutput, copyStart, copyStart + length); this._end += length; } else { while (length-- > 0) { this._dOutput[this._end++] = this._dOutput[copyStart++]; } } } else { while (length-- > 0) { this._dOutput[this._end++] = this._dOutput[copyStart++]; this._end &= _DecompressedOutput._dOutMask; copyStart &= _DecompressedOutput._dOutMask; } } }; /** * Copies up to the requested byte count from the input buffer into the ring buffer with wrap handling. * * @private * @param {_InBuffer} input Input bit buffer. * @param {number} length Maximum number of bytes to copy. * @returns {number} copied Number of bytes copied. */ _DecompressedOutput.prototype._copyFrom = function (input, length) { length = Math.min(Math.min(length, _DecompressedOutput._dOutSize - this._usedBytes), input._bytes); var copied; var tailLen = _DecompressedOutput._dOutSize - this._end; if (length > tailLen) { copied = input._copyTo(this._dOutput, this._end, tailLen); if (copied === tailLen) { copied += input._copyTo(this._dOutput, 0, length - tailLen); } } else { copied = input._copyTo(this._dOutput, this._end, length); } this._end = (this._end + copied) & _DecompressedOutput._dOutMask; this._usedBytes += copied; return copied; }; /** * Copies up to the requested number of bytes from the ring buffer into the provided output array * and decreases the used byte count. * * @private * @param {number[]} output Target output array. * @param {number} offset Offset in the target array where copying begins. * @param {number} length Requested number of bytes to copy. * @returns {{count: number, data: number[]}} result Object containing copied count and output array. */ _DecompressedOutput.prototype._copyTo = function (output, offset, length) { var end; if (length > this._usedBytes) { end = this._end; length = this._usedBytes; } else { end = (this._end - this._usedBytes + length) & _DecompressedOutput._dOutMask; } var copied = length; var tailLen = length - end; var sourceStart = _DecompressedOutput._dOutSize - tailLen; if (tailLen > 0) { for (var i = 0; i < tailLen && i + sourceStart < this._dOutput.length && i + offset < output.length; i++) { output[offset + i] = this._dOutput[sourceStart + i]; } var sourceStartIndex_1 = _DecompressedOutput._dOutSize - tailLen; _copyRange(output, offset, this._dOutput, sourceStartIndex_1, sourceStartIndex_1 + tailLen); offset += tailLen; length = end; } sourceStart = end - length; var sourceStartIndex = end - length; /** * Copies a contiguous range of bytes from the source buffer to the destination buffer. * * @private */ _copyRange(output, offset, this._dOutput, sourceStartIndex, end); this._usedBytes -= copied; return { 'count': copied, 'data': output }; }; /** * Size of the circular output buffer. * * @private */ _DecompressedOutput._dOutSize = 32768; /** * Bit mask used to wrap output buffer indices. * * @private */ _DecompressedOutput._dOutMask = 32767; return _DecompressedOutput; }()); export { _DecompressedOutput };