Merge pull request #13294 from timvandermeij/src-no-var

Enable the `no-var` linting rule in `src/core/{cmap,image,worker}.js`
This commit is contained in:
Tim van der Meij 2021-04-25 17:44:13 +02:00 committed by GitHub
commit 72be684c10
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 187 additions and 192 deletions

View File

@ -12,7 +12,6 @@
* See the License for the specific language governing permissions and * See the License for the specific language governing permissions and
* limitations under the License. * limitations under the License.
*/ */
/* eslint-disable no-var */
import { import {
CMapCompressionType, CMapCompressionType,
@ -26,7 +25,7 @@ import { Lexer } from "./parser.js";
import { MissingDataException } from "./core_utils.js"; import { MissingDataException } from "./core_utils.js";
import { Stream } from "./stream.js"; import { Stream } from "./stream.js";
var BUILT_IN_CMAPS = [ const BUILT_IN_CMAPS = [
// << Start unicode maps. // << Start unicode maps.
"Adobe-GB1-UCS2", "Adobe-GB1-UCS2",
"Adobe-CNS1-UCS2", "Adobe-CNS1-UCS2",
@ -240,7 +239,7 @@ class CMap {
if (high - low > MAX_MAP_RANGE) { if (high - low > MAX_MAP_RANGE) {
throw new Error("mapBfRange - ignoring data above MAX_MAP_RANGE."); throw new Error("mapBfRange - ignoring data above MAX_MAP_RANGE.");
} }
var lastByte = dstLow.length - 1; const lastByte = dstLow.length - 1;
while (low <= high) { while (low <= high) {
this._map[low++] = dstLow; this._map[low++] = dstLow;
// Only the last byte has to be incremented. // Only the last byte has to be incremented.
@ -437,10 +436,10 @@ class IdentityCMap extends CMap {
} }
} }
var BinaryCMapReader = (function BinaryCMapReaderClosure() { const BinaryCMapReader = (function BinaryCMapReaderClosure() {
function hexToInt(a, size) { function hexToInt(a, size) {
var n = 0; let n = 0;
for (var i = 0; i <= size; i++) { for (let i = 0; i <= size; i++) {
n = (n << 8) | a[i]; n = (n << 8) | a[i];
} }
return n >>> 0; return n >>> 0;
@ -459,8 +458,8 @@ var BinaryCMapReader = (function BinaryCMapReaderClosure() {
} }
function addHex(a, b, size) { function addHex(a, b, size) {
var c = 0; let c = 0;
for (var i = size; i >= 0; i--) { for (let i = size; i >= 0; i--) {
c += a[i] + b[i]; c += a[i] + b[i];
a[i] = c & 255; a[i] = c & 255;
c >>= 8; c >>= 8;
@ -468,16 +467,16 @@ var BinaryCMapReader = (function BinaryCMapReaderClosure() {
} }
function incHex(a, size) { function incHex(a, size) {
var c = 1; let c = 1;
for (var i = size; i >= 0 && c > 0; i--) { for (let i = size; i >= 0 && c > 0; i--) {
c += a[i]; c += a[i];
a[i] = c & 255; a[i] = c & 255;
c >>= 8; c >>= 8;
} }
} }
var MAX_NUM_SIZE = 16; const MAX_NUM_SIZE = 16;
var MAX_ENCODED_NUM_SIZE = 19; // ceil(MAX_NUM_SIZE * 7 / 8) const MAX_ENCODED_NUM_SIZE = 19; // ceil(MAX_NUM_SIZE * 7 / 8)
class BinaryCMapStream { class BinaryCMapStream {
constructor(data) { constructor(data) {
@ -495,10 +494,10 @@ var BinaryCMapReader = (function BinaryCMapReaderClosure() {
} }
readNumber() { readNumber() {
var n = 0; let n = 0;
var last; let last;
do { do {
var b = this.readByte(); const b = this.readByte();
if (b < 0) { if (b < 0) {
throw new FormatError("unexpected EOF in bcmap"); throw new FormatError("unexpected EOF in bcmap");
} }
@ -509,7 +508,7 @@ var BinaryCMapReader = (function BinaryCMapReaderClosure() {
} }
readSigned() { readSigned() {
var n = this.readNumber(); const n = this.readNumber();
return n & 1 ? ~(n >>> 1) : n >>> 1; return n & 1 ? ~(n >>> 1) : n >>> 1;
} }
@ -519,18 +518,18 @@ var BinaryCMapReader = (function BinaryCMapReaderClosure() {
} }
readHexNumber(num, size) { readHexNumber(num, size) {
var last; let last;
var stack = this.tmpBuf, const stack = this.tmpBuf;
sp = 0; let sp = 0;
do { do {
var b = this.readByte(); const b = this.readByte();
if (b < 0) { if (b < 0) {
throw new FormatError("unexpected EOF in bcmap"); throw new FormatError("unexpected EOF in bcmap");
} }
last = !(b & 0x80); last = !(b & 0x80);
stack[sp++] = b & 0x7f; stack[sp++] = b & 0x7f;
} while (!last); } while (!last);
var i = size, let i = size,
buffer = 0, buffer = 0,
bufferSize = 0; bufferSize = 0;
while (i >= 0) { while (i >= 0) {
@ -547,18 +546,18 @@ var BinaryCMapReader = (function BinaryCMapReaderClosure() {
readHexSigned(num, size) { readHexSigned(num, size) {
this.readHexNumber(num, size); this.readHexNumber(num, size);
var sign = num[size] & 1 ? 255 : 0; const sign = num[size] & 1 ? 255 : 0;
var c = 0; let c = 0;
for (var i = 0; i <= size; i++) { for (let i = 0; i <= size; i++) {
c = ((c & 1) << 8) | num[i]; c = ((c & 1) << 8) | num[i];
num[i] = (c >> 1) ^ sign; num[i] = (c >> 1) ^ sign;
} }
} }
readString() { readString() {
var len = this.readNumber(); const len = this.readNumber();
var s = ""; let s = "";
for (var i = 0; i < len; i++) { for (let i = 0; i < len; i++) {
s += String.fromCharCode(this.readNumber()); s += String.fromCharCode(this.readNumber());
} }
return s; return s;
@ -568,21 +567,21 @@ var BinaryCMapReader = (function BinaryCMapReaderClosure() {
// eslint-disable-next-line no-shadow // eslint-disable-next-line no-shadow
class BinaryCMapReader { class BinaryCMapReader {
async process(data, cMap, extend) { async process(data, cMap, extend) {
var stream = new BinaryCMapStream(data); const stream = new BinaryCMapStream(data);
var header = stream.readByte(); const header = stream.readByte();
cMap.vertical = !!(header & 1); cMap.vertical = !!(header & 1);
var useCMap = null; let useCMap = null;
var start = new Uint8Array(MAX_NUM_SIZE); const start = new Uint8Array(MAX_NUM_SIZE);
var end = new Uint8Array(MAX_NUM_SIZE); const end = new Uint8Array(MAX_NUM_SIZE);
var char = new Uint8Array(MAX_NUM_SIZE); const char = new Uint8Array(MAX_NUM_SIZE);
var charCode = new Uint8Array(MAX_NUM_SIZE); const charCode = new Uint8Array(MAX_NUM_SIZE);
var tmp = new Uint8Array(MAX_NUM_SIZE); const tmp = new Uint8Array(MAX_NUM_SIZE);
var code; let code;
var b; let b;
while ((b = stream.readByte()) >= 0) { while ((b = stream.readByte()) >= 0) {
var type = b >> 5; const type = b >> 5;
if (type === 7) { if (type === 7) {
// metadata, e.g. comment or usecmap // metadata, e.g. comment or usecmap
switch (b & 0x1f) { switch (b & 0x1f) {
@ -595,16 +594,15 @@ var BinaryCMapReader = (function BinaryCMapReaderClosure() {
} }
continue; continue;
} }
var sequence = !!(b & 0x10); const sequence = !!(b & 0x10);
var dataSize = b & 15; const dataSize = b & 15;
if (dataSize + 1 > MAX_NUM_SIZE) { if (dataSize + 1 > MAX_NUM_SIZE) {
throw new Error("BinaryCMapReader.process: Invalid dataSize."); throw new Error("BinaryCMapReader.process: Invalid dataSize.");
} }
var ucs2DataSize = 1; const ucs2DataSize = 1;
var subitemsCount = stream.readNumber(); const subitemsCount = stream.readNumber();
var i;
switch (type) { switch (type) {
case 0: // codespacerange case 0: // codespacerange
stream.readHex(start, dataSize); stream.readHex(start, dataSize);
@ -615,7 +613,7 @@ var BinaryCMapReader = (function BinaryCMapReaderClosure() {
hexToInt(start, dataSize), hexToInt(start, dataSize),
hexToInt(end, dataSize) hexToInt(end, dataSize)
); );
for (i = 1; i < subitemsCount; i++) { for (let i = 1; i < subitemsCount; i++) {
incHex(end, dataSize); incHex(end, dataSize);
stream.readHexNumber(start, dataSize); stream.readHexNumber(start, dataSize);
addHex(start, end, dataSize); addHex(start, end, dataSize);
@ -634,7 +632,7 @@ var BinaryCMapReader = (function BinaryCMapReaderClosure() {
addHex(end, start, dataSize); addHex(end, start, dataSize);
stream.readNumber(); // code stream.readNumber(); // code
// undefined range, skipping // undefined range, skipping
for (i = 1; i < subitemsCount; i++) { for (let i = 1; i < subitemsCount; i++) {
incHex(end, dataSize); incHex(end, dataSize);
stream.readHexNumber(start, dataSize); stream.readHexNumber(start, dataSize);
addHex(start, end, dataSize); addHex(start, end, dataSize);
@ -648,7 +646,7 @@ var BinaryCMapReader = (function BinaryCMapReaderClosure() {
stream.readHex(char, dataSize); stream.readHex(char, dataSize);
code = stream.readNumber(); code = stream.readNumber();
cMap.mapOne(hexToInt(char, dataSize), code); cMap.mapOne(hexToInt(char, dataSize), code);
for (i = 1; i < subitemsCount; i++) { for (let i = 1; i < subitemsCount; i++) {
incHex(char, dataSize); incHex(char, dataSize);
if (!sequence) { if (!sequence) {
stream.readHexNumber(tmp, dataSize); stream.readHexNumber(tmp, dataSize);
@ -668,7 +666,7 @@ var BinaryCMapReader = (function BinaryCMapReaderClosure() {
hexToInt(end, dataSize), hexToInt(end, dataSize),
code code
); );
for (i = 1; i < subitemsCount; i++) { for (let i = 1; i < subitemsCount; i++) {
incHex(end, dataSize); incHex(end, dataSize);
if (!sequence) { if (!sequence) {
stream.readHexNumber(start, dataSize); stream.readHexNumber(start, dataSize);
@ -693,7 +691,7 @@ var BinaryCMapReader = (function BinaryCMapReaderClosure() {
hexToInt(char, ucs2DataSize), hexToInt(char, ucs2DataSize),
hexToStr(charCode, dataSize) hexToStr(charCode, dataSize)
); );
for (i = 1; i < subitemsCount; i++) { for (let i = 1; i < subitemsCount; i++) {
incHex(char, ucs2DataSize); incHex(char, ucs2DataSize);
if (!sequence) { if (!sequence) {
stream.readHexNumber(tmp, ucs2DataSize); stream.readHexNumber(tmp, ucs2DataSize);
@ -718,7 +716,7 @@ var BinaryCMapReader = (function BinaryCMapReaderClosure() {
hexToInt(end, ucs2DataSize), hexToInt(end, ucs2DataSize),
hexToStr(charCode, dataSize) hexToStr(charCode, dataSize)
); );
for (i = 1; i < subitemsCount; i++) { for (let i = 1; i < subitemsCount; i++) {
incHex(end, ucs2DataSize); incHex(end, ucs2DataSize);
if (!sequence) { if (!sequence) {
stream.readHexNumber(start, ucs2DataSize); stream.readHexNumber(start, ucs2DataSize);
@ -751,10 +749,10 @@ var BinaryCMapReader = (function BinaryCMapReaderClosure() {
return BinaryCMapReader; return BinaryCMapReader;
})(); })();
var CMapFactory = (function CMapFactoryClosure() { const CMapFactory = (function CMapFactoryClosure() {
function strToInt(str) { function strToInt(str) {
var a = 0; let a = 0;
for (var i = 0; i < str.length; i++) { for (let i = 0; i < str.length; i++) {
a = (a << 8) | str.charCodeAt(i); a = (a << 8) | str.charCodeAt(i);
} }
return a >>> 0; return a >>> 0;
@ -774,7 +772,7 @@ var CMapFactory = (function CMapFactoryClosure() {
function parseBfChar(cMap, lexer) { function parseBfChar(cMap, lexer) {
while (true) { while (true) {
var obj = lexer.getObj(); let obj = lexer.getObj();
if (isEOF(obj)) { if (isEOF(obj)) {
break; break;
} }
@ -782,18 +780,18 @@ var CMapFactory = (function CMapFactoryClosure() {
return; return;
} }
expectString(obj); expectString(obj);
var src = strToInt(obj); const src = strToInt(obj);
obj = lexer.getObj(); obj = lexer.getObj();
// TODO are /dstName used? // TODO are /dstName used?
expectString(obj); expectString(obj);
var dst = obj; const dst = obj;
cMap.mapOne(src, dst); cMap.mapOne(src, dst);
} }
} }
function parseBfRange(cMap, lexer) { function parseBfRange(cMap, lexer) {
while (true) { while (true) {
var obj = lexer.getObj(); let obj = lexer.getObj();
if (isEOF(obj)) { if (isEOF(obj)) {
break; break;
} }
@ -801,17 +799,17 @@ var CMapFactory = (function CMapFactoryClosure() {
return; return;
} }
expectString(obj); expectString(obj);
var low = strToInt(obj); const low = strToInt(obj);
obj = lexer.getObj(); obj = lexer.getObj();
expectString(obj); expectString(obj);
var high = strToInt(obj); const high = strToInt(obj);
obj = lexer.getObj(); obj = lexer.getObj();
if (Number.isInteger(obj) || isString(obj)) { if (Number.isInteger(obj) || isString(obj)) {
var dstLow = Number.isInteger(obj) ? String.fromCharCode(obj) : obj; const dstLow = Number.isInteger(obj) ? String.fromCharCode(obj) : obj;
cMap.mapBfRange(low, high, dstLow); cMap.mapBfRange(low, high, dstLow);
} else if (isCmd(obj, "[")) { } else if (isCmd(obj, "[")) {
obj = lexer.getObj(); obj = lexer.getObj();
var array = []; const array = [];
while (!isCmd(obj, "]") && !isEOF(obj)) { while (!isCmd(obj, "]") && !isEOF(obj)) {
array.push(obj); array.push(obj);
obj = lexer.getObj(); obj = lexer.getObj();
@ -826,7 +824,7 @@ var CMapFactory = (function CMapFactoryClosure() {
function parseCidChar(cMap, lexer) { function parseCidChar(cMap, lexer) {
while (true) { while (true) {
var obj = lexer.getObj(); let obj = lexer.getObj();
if (isEOF(obj)) { if (isEOF(obj)) {
break; break;
} }
@ -834,17 +832,17 @@ var CMapFactory = (function CMapFactoryClosure() {
return; return;
} }
expectString(obj); expectString(obj);
var src = strToInt(obj); const src = strToInt(obj);
obj = lexer.getObj(); obj = lexer.getObj();
expectInt(obj); expectInt(obj);
var dst = obj; const dst = obj;
cMap.mapOne(src, dst); cMap.mapOne(src, dst);
} }
} }
function parseCidRange(cMap, lexer) { function parseCidRange(cMap, lexer) {
while (true) { while (true) {
var obj = lexer.getObj(); let obj = lexer.getObj();
if (isEOF(obj)) { if (isEOF(obj)) {
break; break;
} }
@ -852,20 +850,20 @@ var CMapFactory = (function CMapFactoryClosure() {
return; return;
} }
expectString(obj); expectString(obj);
var low = strToInt(obj); const low = strToInt(obj);
obj = lexer.getObj(); obj = lexer.getObj();
expectString(obj); expectString(obj);
var high = strToInt(obj); const high = strToInt(obj);
obj = lexer.getObj(); obj = lexer.getObj();
expectInt(obj); expectInt(obj);
var dstLow = obj; const dstLow = obj;
cMap.mapCidRange(low, high, dstLow); cMap.mapCidRange(low, high, dstLow);
} }
} }
function parseCodespaceRange(cMap, lexer) { function parseCodespaceRange(cMap, lexer) {
while (true) { while (true) {
var obj = lexer.getObj(); let obj = lexer.getObj();
if (isEOF(obj)) { if (isEOF(obj)) {
break; break;
} }
@ -875,37 +873,36 @@ var CMapFactory = (function CMapFactoryClosure() {
if (!isString(obj)) { if (!isString(obj)) {
break; break;
} }
var low = strToInt(obj); const low = strToInt(obj);
obj = lexer.getObj(); obj = lexer.getObj();
if (!isString(obj)) { if (!isString(obj)) {
break; break;
} }
var high = strToInt(obj); const high = strToInt(obj);
cMap.addCodespaceRange(obj.length, low, high); cMap.addCodespaceRange(obj.length, low, high);
} }
throw new FormatError("Invalid codespace range."); throw new FormatError("Invalid codespace range.");
} }
function parseWMode(cMap, lexer) { function parseWMode(cMap, lexer) {
var obj = lexer.getObj(); const obj = lexer.getObj();
if (Number.isInteger(obj)) { if (Number.isInteger(obj)) {
cMap.vertical = !!obj; cMap.vertical = !!obj;
} }
} }
function parseCMapName(cMap, lexer) { function parseCMapName(cMap, lexer) {
var obj = lexer.getObj(); const obj = lexer.getObj();
if (isName(obj) && isString(obj.name)) { if (isName(obj) && isString(obj.name)) {
cMap.name = obj.name; cMap.name = obj.name;
} }
} }
async function parseCMap(cMap, lexer, fetchBuiltInCMap, useCMap) { async function parseCMap(cMap, lexer, fetchBuiltInCMap, useCMap) {
var previous; let previous, embeddedUseCMap;
var embeddedUseCMap;
objLoop: while (true) { objLoop: while (true) {
try { try {
var obj = lexer.getObj(); const obj = lexer.getObj();
if (isEOF(obj)) { if (isEOF(obj)) {
break; break;
} else if (isName(obj)) { } else if (isName(obj)) {
@ -966,8 +963,8 @@ var CMapFactory = (function CMapFactoryClosure() {
// If there aren't any code space ranges defined clone all the parent ones // If there aren't any code space ranges defined clone all the parent ones
// into this cMap. // into this cMap.
if (cMap.numCodespaceRanges === 0) { if (cMap.numCodespaceRanges === 0) {
var useCodespaceRanges = cMap.useCMap.codespaceRanges; const useCodespaceRanges = cMap.useCMap.codespaceRanges;
for (var i = 0; i < useCodespaceRanges.length; i++) { for (let i = 0; i < useCodespaceRanges.length; i++) {
cMap.codespaceRanges[i] = useCodespaceRanges[i].slice(); cMap.codespaceRanges[i] = useCodespaceRanges[i].slice();
} }
cMap.numCodespaceRanges = cMap.useCMap.numCodespaceRanges; cMap.numCodespaceRanges = cMap.useCMap.numCodespaceRanges;
@ -997,7 +994,7 @@ var CMapFactory = (function CMapFactoryClosure() {
} }
const { cMapData, compressionType } = await fetchBuiltInCMap(name); const { cMapData, compressionType } = await fetchBuiltInCMap(name);
var cMap = new CMap(true); const cMap = new CMap(true);
if (compressionType === CMapCompressionType.BINARY) { if (compressionType === CMapCompressionType.BINARY) {
return new BinaryCMapReader().process(cMapData, cMap, useCMap => { return new BinaryCMapReader().process(cMapData, cMap, useCMap => {
@ -1005,7 +1002,7 @@ var CMapFactory = (function CMapFactoryClosure() {
}); });
} }
if (compressionType === CMapCompressionType.NONE) { if (compressionType === CMapCompressionType.NONE) {
var lexer = new Lexer(new Stream(cMapData)); const lexer = new Lexer(new Stream(cMapData));
return parseCMap(cMap, lexer, fetchBuiltInCMap, null); return parseCMap(cMap, lexer, fetchBuiltInCMap, null);
} }
throw new Error( throw new Error(
@ -1015,9 +1012,9 @@ var CMapFactory = (function CMapFactoryClosure() {
return { return {
async create(params) { async create(params) {
var encoding = params.encoding; const encoding = params.encoding;
var fetchBuiltInCMap = params.fetchBuiltInCMap; const fetchBuiltInCMap = params.fetchBuiltInCMap;
var useCMap = params.useCMap; const useCMap = params.useCMap;
if (isName(encoding)) { if (isName(encoding)) {
return createBuiltInCMap(encoding.name, fetchBuiltInCMap); return createBuiltInCMap(encoding.name, fetchBuiltInCMap);

View File

@ -12,7 +12,6 @@
* See the License for the specific language governing permissions and * See the License for the specific language governing permissions and
* limitations under the License. * limitations under the License.
*/ */
/* eslint-disable no-var */
import { assert, FormatError, ImageKind, info, warn } from "../shared/util.js"; import { assert, FormatError, ImageKind, info, warn } from "../shared/util.js";
import { isName, isStream, Name } from "./primitives.js"; import { isName, isStream, Name } from "./primitives.js";
@ -47,7 +46,7 @@ function decodeAndClamp(value, addend, coefficient, max) {
* @returns {TypedArray} The resized image mask buffer. * @returns {TypedArray} The resized image mask buffer.
*/ */
function resizeImageMask(src, bpc, w1, h1, w2, h2) { function resizeImageMask(src, bpc, w1, h1, w2, h2) {
var length = w2 * h2; const length = w2 * h2;
let dest; let dest;
if (bpc <= 8) { if (bpc <= 8) {
dest = new Uint8Array(length); dest = new Uint8Array(length);
@ -56,15 +55,15 @@ function resizeImageMask(src, bpc, w1, h1, w2, h2) {
} else { } else {
dest = new Uint32Array(length); dest = new Uint32Array(length);
} }
var xRatio = w1 / w2; const xRatio = w1 / w2;
var yRatio = h1 / h2; const yRatio = h1 / h2;
var i, let i,
j, j,
py, py,
newIndex = 0, newIndex = 0,
oldIndex; oldIndex;
var xScaled = new Uint16Array(w2); const xScaled = new Uint16Array(w2);
var w1Scanline = w1; const w1Scanline = w1;
for (i = 0; i < w2; i++) { for (i = 0; i < w2; i++) {
xScaled[i] = Math.floor(i * xRatio); xScaled[i] = Math.floor(i * xRatio);
@ -92,13 +91,13 @@ class PDFImage {
localColorSpaceCache, localColorSpaceCache,
}) { }) {
this.image = image; this.image = image;
var dict = image.dict; const dict = image.dict;
const filter = dict.get("Filter"); const filter = dict.get("Filter");
if (isName(filter)) { if (isName(filter)) {
switch (filter.name) { switch (filter.name) {
case "JPXDecode": case "JPXDecode":
var jpxImage = new JpxImage(); const jpxImage = new JpxImage();
jpxImage.parseImageProperties(image.stream); jpxImage.parseImageProperties(image.stream);
image.stream.reset(); image.stream.reset();
@ -144,7 +143,7 @@ class PDFImage {
this.imageMask = dict.get("ImageMask", "IM") || false; this.imageMask = dict.get("ImageMask", "IM") || false;
this.matte = dict.get("Matte") || false; this.matte = dict.get("Matte") || false;
var bitsPerComponent = image.bitsPerComponent; let bitsPerComponent = image.bitsPerComponent;
if (!bitsPerComponent) { if (!bitsPerComponent) {
bitsPerComponent = dict.get("BitsPerComponent", "BPC"); bitsPerComponent = dict.get("BitsPerComponent", "BPC");
if (!bitsPerComponent) { if (!bitsPerComponent) {
@ -201,13 +200,13 @@ class PDFImage {
) { ) {
this.needsDecode = true; this.needsDecode = true;
// Do some preprocessing to avoid more math. // Do some preprocessing to avoid more math.
var max = (1 << bitsPerComponent) - 1; const max = (1 << bitsPerComponent) - 1;
this.decodeCoefficients = []; this.decodeCoefficients = [];
this.decodeAddends = []; this.decodeAddends = [];
const isIndexed = this.colorSpace && this.colorSpace.name === "Indexed"; const isIndexed = this.colorSpace && this.colorSpace.name === "Indexed";
for (var i = 0, j = 0; i < this.decode.length; i += 2, ++j) { for (let i = 0, j = 0; i < this.decode.length; i += 2, ++j) {
var dmin = this.decode[i]; const dmin = this.decode[i];
var dmax = this.decode[i + 1]; const dmax = this.decode[i + 1];
this.decodeCoefficients[j] = isIndexed this.decodeCoefficients[j] = isIndexed
? (dmax - dmin) / max ? (dmax - dmin) / max
: dmax - dmin; : dmax - dmin;
@ -226,7 +225,7 @@ class PDFImage {
}); });
} else if (mask) { } else if (mask) {
if (isStream(mask)) { if (isStream(mask)) {
var maskDict = mask.dict, const maskDict = mask.dict,
imageMask = maskDict.get("ImageMask", "IM"); imageMask = maskDict.get("ImageMask", "IM");
if (!imageMask) { if (!imageMask) {
warn("Ignoring /Mask in image without /ImageMask."); warn("Ignoring /Mask in image without /ImageMask.");
@ -310,10 +309,10 @@ class PDFImage {
// In particular, if inverseDecode is true, then the array we return must // In particular, if inverseDecode is true, then the array we return must
// have a length of |computedLength|. // have a length of |computedLength|.
var computedLength = ((width + 7) >> 3) * height; const computedLength = ((width + 7) >> 3) * height;
var actualLength = imgArray.byteLength; const actualLength = imgArray.byteLength;
var haveFullData = computedLength === actualLength; const haveFullData = computedLength === actualLength;
var data, i; let data, i;
if (imageIsFromDecodeStream && (!inverseDecode || haveFullData)) { if (imageIsFromDecodeStream && (!inverseDecode || haveFullData)) {
// imgArray came from a DecodeStream and its data is in an appropriate // imgArray came from a DecodeStream and its data is in an appropriate
@ -360,13 +359,13 @@ class PDFImage {
} }
decodeBuffer(buffer) { decodeBuffer(buffer) {
var bpc = this.bpc; const bpc = this.bpc;
var numComps = this.numComps; const numComps = this.numComps;
var decodeAddends = this.decodeAddends; const decodeAddends = this.decodeAddends;
var decodeCoefficients = this.decodeCoefficients; const decodeCoefficients = this.decodeCoefficients;
var max = (1 << bpc) - 1; const max = (1 << bpc) - 1;
var i, ii; let i, ii;
if (bpc === 1) { if (bpc === 1) {
// If the buffer needed decode that means it just needs to be inverted. // If the buffer needed decode that means it just needs to be inverted.
@ -375,9 +374,9 @@ class PDFImage {
} }
return; return;
} }
var index = 0; let index = 0;
for (i = 0, ii = this.width * this.height; i < ii; i++) { for (i = 0, ii = this.width * this.height; i < ii; i++) {
for (var j = 0; j < numComps; j++) { for (let j = 0; j < numComps; j++) {
buffer[index] = decodeAndClamp( buffer[index] = decodeAndClamp(
buffer[index], buffer[index],
decodeAddends[j], decodeAddends[j],
@ -390,19 +389,19 @@ class PDFImage {
} }
getComponents(buffer) { getComponents(buffer) {
var bpc = this.bpc; const bpc = this.bpc;
// This image doesn't require any extra work. // This image doesn't require any extra work.
if (bpc === 8) { if (bpc === 8) {
return buffer; return buffer;
} }
var width = this.width; const width = this.width;
var height = this.height; const height = this.height;
var numComps = this.numComps; const numComps = this.numComps;
var length = width * height * numComps; const length = width * height * numComps;
var bufferPos = 0; let bufferPos = 0;
let output; let output;
if (bpc <= 8) { if (bpc <= 8) {
output = new Uint8Array(length); output = new Uint8Array(length);
@ -411,17 +410,17 @@ class PDFImage {
} else { } else {
output = new Uint32Array(length); output = new Uint32Array(length);
} }
var rowComps = width * numComps; const rowComps = width * numComps;
var max = (1 << bpc) - 1; const max = (1 << bpc) - 1;
var i = 0, let i = 0,
ii, ii,
buf; buf;
if (bpc === 1) { if (bpc === 1) {
// Optimization for reading 1 bpc images. // Optimization for reading 1 bpc images.
var mask, loop1End, loop2End; let mask, loop1End, loop2End;
for (var j = 0; j < height; j++) { for (let j = 0; j < height; j++) {
loop1End = i + (rowComps & ~7); loop1End = i + (rowComps & ~7);
loop2End = i + rowComps; loop2End = i + rowComps;
@ -451,7 +450,7 @@ class PDFImage {
} }
} else { } else {
// The general case that handles all other bpc values. // The general case that handles all other bpc values.
var bits = 0; let bits = 0;
buf = 0; buf = 0;
for (i = 0, ii = length; i < ii; ++i) { for (i = 0, ii = length; i < ii; ++i) {
if (i % rowComps === 0) { if (i % rowComps === 0) {
@ -464,7 +463,7 @@ class PDFImage {
bits += 8; bits += 8;
} }
var remainingBits = bits - bpc; const remainingBits = bits - bpc;
let value = buf >> remainingBits; let value = buf >> remainingBits;
if (value < 0) { if (value < 0) {
value = 0; value = 0;
@ -489,9 +488,9 @@ class PDFImage {
'PDFImage.fillOpacity: Unsupported "rgbaBuf" type.' 'PDFImage.fillOpacity: Unsupported "rgbaBuf" type.'
); );
} }
var smask = this.smask; const smask = this.smask;
var mask = this.mask; const mask = this.mask;
var alphaBuf, sw, sh, i, ii, j; let alphaBuf, sw, sh, i, ii, j;
if (smask) { if (smask) {
sw = smask.width; sw = smask.width;
@ -521,13 +520,13 @@ class PDFImage {
// Color key mask: if any of the components are outside the range // Color key mask: if any of the components are outside the range
// then they should be painted. // then they should be painted.
alphaBuf = new Uint8ClampedArray(width * height); alphaBuf = new Uint8ClampedArray(width * height);
var numComps = this.numComps; const numComps = this.numComps;
for (i = 0, ii = width * height; i < ii; ++i) { for (i = 0, ii = width * height; i < ii; ++i) {
var opacity = 0; let opacity = 0;
var imageOffset = i * numComps; const imageOffset = i * numComps;
for (j = 0; j < numComps; ++j) { for (j = 0; j < numComps; ++j) {
var color = image[imageOffset + j]; const color = image[imageOffset + j];
var maskOffset = j * 2; const maskOffset = j * 2;
if (color < mask[maskOffset] || color > mask[maskOffset + 1]) { if (color < mask[maskOffset] || color > mask[maskOffset + 1]) {
opacity = 255; opacity = 255;
break; break;
@ -562,17 +561,17 @@ class PDFImage {
'PDFImage.undoPreblend: Unsupported "buffer" type.' 'PDFImage.undoPreblend: Unsupported "buffer" type.'
); );
} }
var matte = this.smask && this.smask.matte; const matte = this.smask && this.smask.matte;
if (!matte) { if (!matte) {
return; return;
} }
var matteRgb = this.colorSpace.getRgb(matte, 0); const matteRgb = this.colorSpace.getRgb(matte, 0);
var matteR = matteRgb[0]; const matteR = matteRgb[0];
var matteG = matteRgb[1]; const matteG = matteRgb[1];
var matteB = matteRgb[2]; const matteB = matteRgb[2];
var length = width * height * 4; const length = width * height * 4;
for (var i = 0; i < length; i += 4) { for (let i = 0; i < length; i += 4) {
var alpha = buffer[i + 3]; const alpha = buffer[i + 3];
if (alpha === 0) { if (alpha === 0) {
// according formula we have to get Infinity in all components // according formula we have to get Infinity in all components
// making it white (typical paper color) should be okay // making it white (typical paper color) should be okay
@ -581,7 +580,7 @@ class PDFImage {
buffer[i + 2] = 255; buffer[i + 2] = 255;
continue; continue;
} }
var k = 255 / alpha; const k = 255 / alpha;
buffer[i] = (buffer[i] - matteR) * k + matteR; buffer[i] = (buffer[i] - matteR) * k + matteR;
buffer[i + 1] = (buffer[i + 1] - matteG) * k + matteG; buffer[i + 1] = (buffer[i + 1] - matteG) * k + matteG;
buffer[i + 2] = (buffer[i + 2] - matteB) * k + matteB; buffer[i + 2] = (buffer[i + 2] - matteB) * k + matteB;
@ -589,9 +588,9 @@ class PDFImage {
} }
createImageData(forceRGBA = false) { createImageData(forceRGBA = false) {
var drawWidth = this.drawWidth; const drawWidth = this.drawWidth;
var drawHeight = this.drawHeight; const drawHeight = this.drawHeight;
var imgData = { const imgData = {
width: drawWidth, width: drawWidth,
height: drawHeight, height: drawHeight,
kind: 0, kind: 0,
@ -599,14 +598,14 @@ class PDFImage {
// Other fields are filled in below. // Other fields are filled in below.
}; };
var numComps = this.numComps; const numComps = this.numComps;
var originalWidth = this.width; const originalWidth = this.width;
var originalHeight = this.height; const originalHeight = this.height;
var bpc = this.bpc; const bpc = this.bpc;
// Rows start at byte boundary. // Rows start at byte boundary.
var rowBytes = (originalWidth * numComps * bpc + 7) >> 3; const rowBytes = (originalWidth * numComps * bpc + 7) >> 3;
var imgArray; let imgArray;
if (!forceRGBA) { if (!forceRGBA) {
// If it is a 1-bit-per-pixel grayscale (i.e. black-and-white) image // If it is a 1-bit-per-pixel grayscale (i.e. black-and-white) image
@ -616,7 +615,7 @@ class PDFImage {
// //
// Similarly, if it is a 24-bit-per pixel RGB image without any // Similarly, if it is a 24-bit-per pixel RGB image without any
// complications, we avoid expanding by 1.333x to RGBA form. // complications, we avoid expanding by 1.333x to RGBA form.
var kind; let kind;
if (this.colorSpace.name === "DeviceGray" && bpc === 1) { if (this.colorSpace.name === "DeviceGray" && bpc === 1) {
kind = ImageKind.GRAYSCALE_1BPP; kind = ImageKind.GRAYSCALE_1BPP;
} else if ( } else if (
@ -644,7 +643,7 @@ class PDFImage {
if (this.image instanceof DecodeStream) { if (this.image instanceof DecodeStream) {
imgData.data = imgArray; imgData.data = imgArray;
} else { } else {
var newArray = new Uint8ClampedArray(imgArray.length); const newArray = new Uint8ClampedArray(imgArray.length);
newArray.set(imgArray); newArray.set(imgArray);
imgData.data = newArray; imgData.data = newArray;
} }
@ -654,8 +653,8 @@ class PDFImage {
kind === ImageKind.GRAYSCALE_1BPP, kind === ImageKind.GRAYSCALE_1BPP,
"PDFImage.createImageData: The image must be grayscale." "PDFImage.createImageData: The image must be grayscale."
); );
var buffer = imgData.data; const buffer = imgData.data;
for (var i = 0, ii = buffer.length; i < ii; i++) { for (let i = 0, ii = buffer.length; i < ii; i++) {
buffer[i] ^= 0xff; buffer[i] ^= 0xff;
} }
} }
@ -685,14 +684,14 @@ class PDFImage {
imgArray = this.getImageBytes(originalHeight * rowBytes); imgArray = this.getImageBytes(originalHeight * rowBytes);
// imgArray can be incomplete (e.g. after CCITT fax encoding). // imgArray can be incomplete (e.g. after CCITT fax encoding).
var actualHeight = const actualHeight =
0 | (((imgArray.length / rowBytes) * drawHeight) / originalHeight); 0 | (((imgArray.length / rowBytes) * drawHeight) / originalHeight);
var comps = this.getComponents(imgArray); const comps = this.getComponents(imgArray);
// If opacity data is present, use RGBA_32BPP form. Otherwise, use the // If opacity data is present, use RGBA_32BPP form. Otherwise, use the
// more compact RGB_24BPP form if allowable. // more compact RGB_24BPP form if allowable.
var alpha01, maybeUndoPreblend; let alpha01, maybeUndoPreblend;
if (!forceRGBA && !this.smask && !this.mask) { if (!forceRGBA && !this.smask && !this.mask) {
imgData.kind = ImageKind.RGB_24BPP; imgData.kind = ImageKind.RGB_24BPP;
imgData.data = new Uint8ClampedArray(drawWidth * drawHeight * 3); imgData.data = new Uint8ClampedArray(drawWidth * drawHeight * 3);
@ -745,23 +744,23 @@ class PDFImage {
'PDFImage.fillGrayBuffer: Unsupported "buffer" type.' 'PDFImage.fillGrayBuffer: Unsupported "buffer" type.'
); );
} }
var numComps = this.numComps; const numComps = this.numComps;
if (numComps !== 1) { if (numComps !== 1) {
throw new FormatError( throw new FormatError(
`Reading gray scale from a color image: ${numComps}` `Reading gray scale from a color image: ${numComps}`
); );
} }
var width = this.width; const width = this.width;
var height = this.height; const height = this.height;
var bpc = this.bpc; const bpc = this.bpc;
// rows start at byte boundary // rows start at byte boundary
var rowBytes = (width * numComps * bpc + 7) >> 3; const rowBytes = (width * numComps * bpc + 7) >> 3;
var imgArray = this.getImageBytes(height * rowBytes); const imgArray = this.getImageBytes(height * rowBytes);
var comps = this.getComponents(imgArray); const comps = this.getComponents(imgArray);
var i, length; let i, length;
if (bpc === 1) { if (bpc === 1) {
// inline decoding (= inversion) for 1 bpc images // inline decoding (= inversion) for 1 bpc images
@ -785,7 +784,7 @@ class PDFImage {
} }
length = width * height; length = width * height;
// we aren't using a colorspace so we need to scale the value // we aren't using a colorspace so we need to scale the value
var scale = 255 / ((1 << bpc) - 1); const scale = 255 / ((1 << bpc) - 1);
for (i = 0; i < length; ++i) { for (i = 0; i < length; ++i) {
buffer[i] = scale * comps[i]; buffer[i] = scale * comps[i];
} }

View File

@ -12,7 +12,6 @@
* See the License for the specific language governing permissions and * See the License for the specific language governing permissions and
* limitations under the License. * limitations under the License.
*/ */
/* eslint-disable no-var */
import { import {
AbortException, AbortException,
@ -69,7 +68,7 @@ class WorkerTask {
class WorkerMessageHandler { class WorkerMessageHandler {
static setup(handler, port) { static setup(handler, port) {
var testMessageProcessed = false; let testMessageProcessed = false;
handler.on("test", function wphSetupTest(data) { handler.on("test", function wphSetupTest(data) {
if (testMessageProcessed) { if (testMessageProcessed) {
return; // we already processed 'test' message once return; // we already processed 'test' message once
@ -100,10 +99,10 @@ class WorkerMessageHandler {
static createDocumentHandler(docParams, port) { static createDocumentHandler(docParams, port) {
// This context is actually holds references on pdfManager and handler, // This context is actually holds references on pdfManager and handler,
// until the latter is destroyed. // until the latter is destroyed.
var pdfManager; let pdfManager;
var terminated = false; let terminated = false;
var cancelXHRs = null; let cancelXHRs = null;
var WorkerTasks = []; const WorkerTasks = [];
const verbosity = getVerbosityLevel(); const verbosity = getVerbosityLevel();
const apiVersion = docParams.apiVersion; const apiVersion = docParams.apiVersion;
@ -152,10 +151,10 @@ class WorkerMessageHandler {
} }
} }
var docId = docParams.docId; const docId = docParams.docId;
var docBaseUrl = docParams.docBaseUrl; const docBaseUrl = docParams.docBaseUrl;
var workerHandlerName = docParams.docId + "_worker"; const workerHandlerName = docParams.docId + "_worker";
var handler = new MessageHandler(workerHandlerName, docId, port); let handler = new MessageHandler(workerHandlerName, docId, port);
// Ensure that postMessage transfers are always correctly enabled/disabled, // Ensure that postMessage transfers are always correctly enabled/disabled,
// to prevent "DataCloneError" in browsers without transfers support. // to prevent "DataCloneError" in browsers without transfers support.
@ -173,7 +172,7 @@ class WorkerMessageHandler {
function finishWorkerTask(task) { function finishWorkerTask(task) {
task.finish(); task.finish();
var i = WorkerTasks.indexOf(task); const i = WorkerTasks.indexOf(task);
WorkerTasks.splice(i, 1); WorkerTasks.splice(i, 1);
} }
@ -208,10 +207,10 @@ class WorkerMessageHandler {
} }
function getPdfManager(data, evaluatorOptions, enableXfa) { function getPdfManager(data, evaluatorOptions, enableXfa) {
var pdfManagerCapability = createPromiseCapability(); const pdfManagerCapability = createPromiseCapability();
let newPdfManager; let newPdfManager;
var source = data.source; const source = data.source;
if (source.data) { if (source.data) {
try { try {
newPdfManager = new LocalPdfManager( newPdfManager = new LocalPdfManager(
@ -229,7 +228,7 @@ class WorkerMessageHandler {
return pdfManagerCapability.promise; return pdfManagerCapability.promise;
} }
var pdfStream, let pdfStream,
cachedChunks = []; cachedChunks = [];
try { try {
pdfStream = new PDFWorkerStream(handler); pdfStream = new PDFWorkerStream(handler);
@ -238,7 +237,7 @@ class WorkerMessageHandler {
return pdfManagerCapability.promise; return pdfManagerCapability.promise;
} }
var fullRequest = pdfStream.getFullReader(); const fullRequest = pdfStream.getFullReader();
fullRequest.headersReady fullRequest.headersReady
.then(function () { .then(function () {
if (!fullRequest.isRangeSupported) { if (!fullRequest.isRangeSupported) {
@ -246,7 +245,7 @@ class WorkerMessageHandler {
} }
// We don't need auto-fetch when streaming is enabled. // We don't need auto-fetch when streaming is enabled.
var disableAutoFetch = const disableAutoFetch =
source.disableAutoFetch || fullRequest.isStreamingSupported; source.disableAutoFetch || fullRequest.isStreamingSupported;
newPdfManager = new NetworkPdfManager( newPdfManager = new NetworkPdfManager(
docId, docId,
@ -278,9 +277,9 @@ class WorkerMessageHandler {
cancelXHRs = null; cancelXHRs = null;
}); });
var loaded = 0; let loaded = 0;
var flushChunks = function () { const flushChunks = function () {
var pdfFile = arraysToBytes(cachedChunks); const pdfFile = arraysToBytes(cachedChunks);
if (source.length && pdfFile.length !== source.length) { if (source.length && pdfFile.length !== source.length) {
warn("reported HTTP length is different from actual"); warn("reported HTTP length is different from actual");
} }
@ -300,8 +299,8 @@ class WorkerMessageHandler {
} }
cachedChunks = []; cachedChunks = [];
}; };
var readPromise = new Promise(function (resolve, reject) { const readPromise = new Promise(function (resolve, reject) {
var readChunk = function ({ value, done }) { const readChunk = function ({ value, done }) {
try { try {
ensureNotTerminated(); ensureNotTerminated();
if (done) { if (done) {
@ -355,7 +354,7 @@ class WorkerMessageHandler {
ensureNotTerminated(); ensureNotTerminated();
if (ex instanceof PasswordException) { if (ex instanceof PasswordException) {
var task = new WorkerTask(`PasswordException: response ${ex.code}`); const task = new WorkerTask(`PasswordException: response ${ex.code}`);
startWorkerTask(task); startWorkerTask(task);
handler handler
@ -406,7 +405,7 @@ class WorkerMessageHandler {
ensureNotTerminated(); ensureNotTerminated();
var evaluatorOptions = { const evaluatorOptions = {
maxImageSize: data.maxImageSize, maxImageSize: data.maxImageSize,
disableFontFace: data.disableFontFace, disableFontFace: data.disableFontFace,
ignoreErrors: data.ignoreErrors, ignoreErrors: data.ignoreErrors,
@ -656,9 +655,9 @@ class WorkerMessageHandler {
); );
handler.on("GetOperatorList", function wphSetupRenderPage(data, sink) { handler.on("GetOperatorList", function wphSetupRenderPage(data, sink) {
var pageIndex = data.pageIndex; const pageIndex = data.pageIndex;
pdfManager.getPage(pageIndex).then(function (page) { pdfManager.getPage(pageIndex).then(function (page) {
var task = new WorkerTask(`GetOperatorList: page ${pageIndex}`); const task = new WorkerTask(`GetOperatorList: page ${pageIndex}`);
startWorkerTask(task); startWorkerTask(task);
// NOTE: Keep this condition in sync with the `info` helper function. // NOTE: Keep this condition in sync with the `info` helper function.
@ -707,12 +706,12 @@ class WorkerMessageHandler {
}); });
handler.on("GetTextContent", function wphExtractText(data, sink) { handler.on("GetTextContent", function wphExtractText(data, sink) {
var pageIndex = data.pageIndex; const pageIndex = data.pageIndex;
sink.onPull = function (desiredSize) {}; sink.onPull = function (desiredSize) {};
sink.onCancel = function (reason) {}; sink.onCancel = function (reason) {};
pdfManager.getPage(pageIndex).then(function (page) { pdfManager.getPage(pageIndex).then(function (page) {
var task = new WorkerTask("GetTextContent: page " + pageIndex); const task = new WorkerTask("GetTextContent: page " + pageIndex);
startWorkerTask(task); startWorkerTask(task);
// NOTE: Keep this condition in sync with the `info` helper function. // NOTE: Keep this condition in sync with the `info` helper function.
@ -806,7 +805,7 @@ class WorkerMessageHandler {
} }
static initializeFromPort(port) { static initializeFromPort(port) {
var handler = new MessageHandler("worker", "main", port); const handler = new MessageHandler("worker", "main", port);
WorkerMessageHandler.setup(handler, port); WorkerMessageHandler.setup(handler, port);
handler.send("ready", null); handler.send("ready", null);
} }