pdf.js/src/core/stream.js

148 lines
4.0 KiB
JavaScript
Raw Normal View History

2012-09-01 07:48:21 +09:00
/* Copyright 2012 Mozilla Foundation
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
2011-10-26 10:18:22 +09:00
import { stringToBytes } from "../shared/util.js";
const Stream = (function StreamClosure() {
// eslint-disable-next-line no-shadow
2011-12-09 07:18:43 +09:00
function Stream(arrayBuffer, start, length, dict) {
Enable auto-formatting of the entire code-base using Prettier (issue 11444) Note that Prettier, purposely, has only limited [configuration options](https://prettier.io/docs/en/options.html). The configuration file is based on [the one in `mozilla central`](https://searchfox.org/mozilla-central/source/.prettierrc) with just a few additions (to avoid future breakage if the defaults ever changes). Prettier is being used for a couple of reasons: - To be consistent with `mozilla-central`, where Prettier is already in use across the tree. - To ensure a *consistent* coding style everywhere, which is automatically enforced during linting (since Prettier is used as an ESLint plugin). This thus ends "all" formatting disussions once and for all, removing the need for review comments on most stylistic matters. Many ESLint options are now redundant, and I've tried my best to remove all the now unnecessary options (but I may have missed some). Note also that since Prettier considers the `printWidth` option as a guide, rather than a hard rule, this patch resorts to a small hack in the ESLint config to ensure that *comments* won't become too long. *Please note:* This patch is generated automatically, by appending the `--fix` argument to the ESLint call used in the `gulp lint` task. It will thus require some additional clean-up, which will be done in a *separate* commit. (On a more personal note, I'll readily admit that some of the changes Prettier makes are *extremely* ugly. However, in the name of consistency we'll probably have to live with that.)
2019-12-25 23:59:37 +09:00
this.bytes =
arrayBuffer instanceof Uint8Array
? arrayBuffer
: new Uint8Array(arrayBuffer);
2011-10-25 08:55:23 +09:00
this.start = start || 0;
this.pos = this.start;
Enable auto-formatting of the entire code-base using Prettier (issue 11444) Note that Prettier, purposely, has only limited [configuration options](https://prettier.io/docs/en/options.html). The configuration file is based on [the one in `mozilla central`](https://searchfox.org/mozilla-central/source/.prettierrc) with just a few additions (to avoid future breakage if the defaults ever changes). Prettier is being used for a couple of reasons: - To be consistent with `mozilla-central`, where Prettier is already in use across the tree. - To ensure a *consistent* coding style everywhere, which is automatically enforced during linting (since Prettier is used as an ESLint plugin). This thus ends "all" formatting disussions once and for all, removing the need for review comments on most stylistic matters. Many ESLint options are now redundant, and I've tried my best to remove all the now unnecessary options (but I may have missed some). Note also that since Prettier considers the `printWidth` option as a guide, rather than a hard rule, this patch resorts to a small hack in the ESLint config to ensure that *comments* won't become too long. *Please note:* This patch is generated automatically, by appending the `--fix` argument to the ESLint call used in the `gulp lint` task. It will thus require some additional clean-up, which will be done in a *separate* commit. (On a more personal note, I'll readily admit that some of the changes Prettier makes are *extremely* ugly. However, in the name of consistency we'll probably have to live with that.)
2019-12-25 23:59:37 +09:00
this.end = start + length || this.bytes.length;
this.dict = dict;
2011-10-25 08:55:23 +09:00
}
// required methods for a stream. if a particular stream does not
// implement these, an error should be thrown
2011-12-09 07:18:43 +09:00
Stream.prototype = {
2011-10-25 08:55:23 +09:00
get length() {
return this.end - this.start;
},
get isEmpty() {
return this.length === 0;
},
getByte: function Stream_getByte() {
2014-03-23 05:19:08 +09:00
if (this.pos >= this.end) {
2013-07-01 05:45:15 +09:00
return -1;
2014-03-23 05:19:08 +09:00
}
2011-10-25 08:55:23 +09:00
return this.bytes[this.pos++];
},
getUint16: function Stream_getUint16() {
const b0 = this.getByte();
const b1 = this.getByte();
if (b0 === -1 || b1 === -1) {
return -1;
}
return (b0 << 8) + b1;
},
getInt32: function Stream_getInt32() {
const b0 = this.getByte();
const b1 = this.getByte();
const b2 = this.getByte();
const b3 = this.getByte();
return (b0 << 24) + (b1 << 16) + (b2 << 8) + b3;
},
// Returns subarray of original buffer, should only be read.
getBytes(length, forceClamped = false) {
const bytes = this.bytes;
const pos = this.pos;
const strEnd = this.end;
2011-10-25 08:55:23 +09:00
2014-03-23 05:19:08 +09:00
if (!length) {
const subarray = bytes.subarray(pos, strEnd);
// `this.bytes` is always a `Uint8Array` here.
Enable auto-formatting of the entire code-base using Prettier (issue 11444) Note that Prettier, purposely, has only limited [configuration options](https://prettier.io/docs/en/options.html). The configuration file is based on [the one in `mozilla central`](https://searchfox.org/mozilla-central/source/.prettierrc) with just a few additions (to avoid future breakage if the defaults ever changes). Prettier is being used for a couple of reasons: - To be consistent with `mozilla-central`, where Prettier is already in use across the tree. - To ensure a *consistent* coding style everywhere, which is automatically enforced during linting (since Prettier is used as an ESLint plugin). This thus ends "all" formatting disussions once and for all, removing the need for review comments on most stylistic matters. Many ESLint options are now redundant, and I've tried my best to remove all the now unnecessary options (but I may have missed some). Note also that since Prettier considers the `printWidth` option as a guide, rather than a hard rule, this patch resorts to a small hack in the ESLint config to ensure that *comments* won't become too long. *Please note:* This patch is generated automatically, by appending the `--fix` argument to the ESLint call used in the `gulp lint` task. It will thus require some additional clean-up, which will be done in a *separate* commit. (On a more personal note, I'll readily admit that some of the changes Prettier makes are *extremely* ugly. However, in the name of consistency we'll probably have to live with that.)
2019-12-25 23:59:37 +09:00
return forceClamped ? new Uint8ClampedArray(subarray) : subarray;
2014-03-23 05:19:08 +09:00
}
let end = pos + length;
2014-03-23 05:19:08 +09:00
if (end > strEnd) {
2011-10-25 08:55:23 +09:00
end = strEnd;
2014-03-23 05:19:08 +09:00
}
2011-10-25 08:55:23 +09:00
this.pos = end;
const subarray = bytes.subarray(pos, end);
// `this.bytes` is always a `Uint8Array` here.
Enable auto-formatting of the entire code-base using Prettier (issue 11444) Note that Prettier, purposely, has only limited [configuration options](https://prettier.io/docs/en/options.html). The configuration file is based on [the one in `mozilla central`](https://searchfox.org/mozilla-central/source/.prettierrc) with just a few additions (to avoid future breakage if the defaults ever changes). Prettier is being used for a couple of reasons: - To be consistent with `mozilla-central`, where Prettier is already in use across the tree. - To ensure a *consistent* coding style everywhere, which is automatically enforced during linting (since Prettier is used as an ESLint plugin). This thus ends "all" formatting disussions once and for all, removing the need for review comments on most stylistic matters. Many ESLint options are now redundant, and I've tried my best to remove all the now unnecessary options (but I may have missed some). Note also that since Prettier considers the `printWidth` option as a guide, rather than a hard rule, this patch resorts to a small hack in the ESLint config to ensure that *comments* won't become too long. *Please note:* This patch is generated automatically, by appending the `--fix` argument to the ESLint call used in the `gulp lint` task. It will thus require some additional clean-up, which will be done in a *separate* commit. (On a more personal note, I'll readily admit that some of the changes Prettier makes are *extremely* ugly. However, in the name of consistency we'll probably have to live with that.)
2019-12-25 23:59:37 +09:00
return forceClamped ? new Uint8ClampedArray(subarray) : subarray;
2011-10-25 08:55:23 +09:00
},
peekByte: function Stream_peekByte() {
const peekedByte = this.getByte();
if (peekedByte !== -1) {
this.pos--;
}
return peekedByte;
},
peekBytes(length, forceClamped = false) {
const bytes = this.getBytes(length, forceClamped);
this.pos -= bytes.length;
return bytes;
},
getByteRange(begin, end) {
if (begin < 0) {
begin = 0;
}
if (end > this.end) {
end = this.end;
}
return this.bytes.subarray(begin, end);
},
skip: function Stream_skip(n) {
2014-03-23 05:19:08 +09:00
if (!n) {
2011-10-25 08:55:23 +09:00
n = 1;
2014-03-23 05:19:08 +09:00
}
2011-10-25 08:55:23 +09:00
this.pos += n;
},
reset: function Stream_reset() {
2011-10-25 08:55:23 +09:00
this.pos = this.start;
},
moveStart: function Stream_moveStart() {
2011-10-25 08:55:23 +09:00
this.start = this.pos;
},
makeSubStream: function Stream_makeSubStream(start, length, dict) {
2011-10-25 08:55:23 +09:00
return new Stream(this.bytes.buffer, start, length, dict);
},
};
2011-12-09 07:18:43 +09:00
return Stream;
2011-10-25 08:55:23 +09:00
})();
const StringStream = (function StringStreamClosure() {
// eslint-disable-next-line no-shadow
2011-12-09 07:18:43 +09:00
function StringStream(str) {
const bytes = stringToBytes(str);
2011-10-25 08:55:23 +09:00
Stream.call(this, bytes);
}
2011-12-09 07:18:43 +09:00
StringStream.prototype = Stream.prototype;
2011-10-25 08:55:23 +09:00
2011-12-09 07:18:43 +09:00
return StringStream;
2011-10-25 08:55:23 +09:00
})();
const NullStream = (function NullStreamClosure() {
// eslint-disable-next-line no-shadow
2012-10-23 00:53:15 +09:00
function NullStream() {
Stream.call(this, new Uint8Array(0));
}
NullStream.prototype = Stream.prototype;
return NullStream;
})();
export { NullStream, Stream, StringStream };