Enable the ESLint no-var rule in the src/core/type1_parser.js file

Note that the majority of these changes were done automatically, by using `gulp lint --fix`, and the manual changes were limited to the following diff:

```diff
diff --git a/src/core/type1_parser.js b/src/core/type1_parser.js
index 192781de1..05c5fe2e5 100644
--- a/src/core/type1_parser.js
+++ b/src/core/type1_parser.js
@@ -251,7 +251,7 @@ const Type1CharString = (function Type1CharStringClosure() {
               // vhea tables reconstruction -- ignoring it.
               this.stack.pop(); // wy
               wx = this.stack.pop();
-              var sby = this.stack.pop();
+              const sby = this.stack.pop();
               sbx = this.stack.pop();
               this.lsb = sbx;
               this.width = wx;
@@ -263,8 +263,8 @@ const Type1CharString = (function Type1CharStringClosure() {
                 error = true;
                 break;
               }
-              var num2 = this.stack.pop();
-              var num1 = this.stack.pop();
+              const num2 = this.stack.pop();
+              const num1 = this.stack.pop();
               this.stack.push(num1 / num2);
               break;
             case (12 << 8) + 16: // callothersubr
@@ -273,7 +273,7 @@ const Type1CharString = (function Type1CharStringClosure() {
                 break;
               }
               subrNumber = this.stack.pop();
-              var numArgs = this.stack.pop();
+              const numArgs = this.stack.pop();
               if (subrNumber === 0 && numArgs === 3) {
                 const flexArgs = this.stack.splice(this.stack.length - 17, 17);
                 this.stack.push(
@@ -397,9 +397,9 @@ const Type1Parser = (function Type1ParserClosure() {
     if (discardNumber >= data.length) {
       return new Uint8Array(0);
     }
+    const c1 = 52845,
+      c2 = 22719;
     let r = key | 0,
-      c1 = 52845,
-      c2 = 22719,
       i,
       j;
     for (i = 0; i < discardNumber; i++) {
@@ -416,9 +416,9 @@ const Type1Parser = (function Type1ParserClosure() {
   }

   function decryptAscii(data, key, discardNumber) {
-    let r = key | 0,
-      c1 = 52845,
+    const c1 = 52845,
       c2 = 22719;
+    let r = key | 0;
     const count = data.length,
       maybeLength = count >>> 1;
     const decrypted = new Uint8Array(maybeLength);
@@ -429,7 +429,7 @@ const Type1Parser = (function Type1ParserClosure() {
         continue;
       }
       i++;
-      var digit2;
+      let digit2;
       while (i < count && !isHexDigit((digit2 = data[i]))) {
         i++;
       }
@@ -599,7 +599,7 @@ const Type1Parser = (function Type1ParserClosure() {
               if (token !== "/") {
                 continue;
               }
-              var glyph = this.getToken();
+              const glyph = this.getToken();
               length = this.readInt();
               this.getToken(); // read in 'RD' or '-|'
               data = length > 0 ? stream.getBytes(length) : new Uint8Array(0);
@@ -638,7 +638,7 @@ const Type1Parser = (function Type1ParserClosure() {
           case "OtherBlues":
           case "FamilyBlues":
           case "FamilyOtherBlues":
-            var blueArray = this.readNumberArray();
+            const blueArray = this.readNumberArray();
             // *Blue* values may contain invalid data: disables reading of
             // those values when hinting is disabled.
             if (
@@ -672,7 +672,7 @@ const Type1Parser = (function Type1ParserClosure() {
       }

       for (let i = 0; i < charstrings.length; i++) {
-        glyph = charstrings[i].glyph;
+        const glyph = charstrings[i].glyph;
         encoded = charstrings[i].encoded;
         const charString = new Type1CharString();
         const error = charString.convert(
@@ -728,12 +728,12 @@ const Type1Parser = (function Type1ParserClosure() {
         token = this.getToken();
         switch (token) {
           case "FontMatrix":
-            var matrix = this.readNumberArray();
+            const matrix = this.readNumberArray();
             properties.fontMatrix = matrix;
             break;
           case "Encoding":
-            var encodingArg = this.getToken();
-            var encoding;
+            const encodingArg = this.getToken();
+            let encoding;
             if (!/^\d+$/.test(encodingArg)) {
               // encoding name is specified
               encoding = getEncoding(encodingArg);
@@ -764,7 +764,7 @@ const Type1Parser = (function Type1ParserClosure() {
             properties.builtInEncoding = encoding;
             break;
           case "FontBBox":
-            var fontBBox = this.readNumberArray();
+            const fontBBox = this.readNumberArray();
             // adjusting ascent/descent
             properties.ascent = Math.max(fontBBox[3], fontBBox[1]);
             properties.descent = Math.min(fontBBox[1], fontBBox[3]);
```
This commit is contained in:
Jonas Jenwald 2021-03-12 12:02:01 +01:00
parent 82062f7e0d
commit 8fc8dc020e

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 { getEncoding } from "./encodings.js"; import { getEncoding } from "./encodings.js";
import { isWhiteSpace } from "./core_utils.js"; import { isWhiteSpace } from "./core_utils.js";
@ -21,7 +20,7 @@ import { warn } from "../shared/util.js";
// Hinting is currently disabled due to unknown problems on windows // Hinting is currently disabled due to unknown problems on windows
// in tracemonkey and various other pdfs with type1 fonts. // in tracemonkey and various other pdfs with type1 fonts.
var HINTING_ENABLED = false; const HINTING_ENABLED = false;
/* /*
* CharStrings are encoded following the the CharString Encoding sequence * CharStrings are encoded following the the CharString Encoding sequence
@ -61,8 +60,8 @@ var HINTING_ENABLED = false;
* to be encoded and this encoding technique helps to minimize the length of * to be encoded and this encoding technique helps to minimize the length of
* the charStrings. * the charStrings.
*/ */
var Type1CharString = (function Type1CharStringClosure() { const Type1CharString = (function Type1CharStringClosure() {
var COMMAND_MAP = { const COMMAND_MAP = {
hstem: [1], hstem: [1],
vstem: [3], vstem: [3],
vmoveto: [4], vmoveto: [4],
@ -95,11 +94,11 @@ var Type1CharString = (function Type1CharStringClosure() {
subrs, subrs,
seacAnalysisEnabled seacAnalysisEnabled
) { ) {
var count = encoded.length; const count = encoded.length;
var error = false; let error = false;
var wx, sbx, subrNumber; let wx, sbx, subrNumber;
for (var i = 0; i < count; i++) { for (let i = 0; i < count; i++) {
var value = encoded[i]; let value = encoded[i];
if (value < 32) { if (value < 32) {
if (value === 12) { if (value === 12) {
value = (value << 8) + encoded[++i]; value = (value << 8) + encoded[++i];
@ -127,7 +126,7 @@ var Type1CharString = (function Type1CharStringClosure() {
} }
// Add the dx for flex and but also swap the values so they are // Add the dx for flex and but also swap the values so they are
// the right order. // the right order.
var dy = this.stack.pop(); const dy = this.stack.pop();
this.stack.push(0, dy); this.stack.push(0, dy);
break; break;
} }
@ -252,7 +251,7 @@ var Type1CharString = (function Type1CharStringClosure() {
// vhea tables reconstruction -- ignoring it. // vhea tables reconstruction -- ignoring it.
this.stack.pop(); // wy this.stack.pop(); // wy
wx = this.stack.pop(); wx = this.stack.pop();
var sby = this.stack.pop(); const sby = this.stack.pop();
sbx = this.stack.pop(); sbx = this.stack.pop();
this.lsb = sbx; this.lsb = sbx;
this.width = wx; this.width = wx;
@ -264,8 +263,8 @@ var Type1CharString = (function Type1CharStringClosure() {
error = true; error = true;
break; break;
} }
var num2 = this.stack.pop(); const num2 = this.stack.pop();
var num1 = this.stack.pop(); const num1 = this.stack.pop();
this.stack.push(num1 / num2); this.stack.push(num1 / num2);
break; break;
case (12 << 8) + 16: // callothersubr case (12 << 8) + 16: // callothersubr
@ -274,9 +273,9 @@ var Type1CharString = (function Type1CharStringClosure() {
break; break;
} }
subrNumber = this.stack.pop(); subrNumber = this.stack.pop();
var numArgs = this.stack.pop(); const numArgs = this.stack.pop();
if (subrNumber === 0 && numArgs === 3) { if (subrNumber === 0 && numArgs === 3) {
var flexArgs = this.stack.splice(this.stack.length - 17, 17); const flexArgs = this.stack.splice(this.stack.length - 17, 17);
this.stack.push( this.stack.push(
flexArgs[2] + flexArgs[0], // bcp1x + rpx flexArgs[2] + flexArgs[0], // bcp1x + rpx
flexArgs[3] + flexArgs[1], // bcp1y + rpy flexArgs[3] + flexArgs[1], // bcp1y + rpy
@ -335,13 +334,13 @@ var Type1CharString = (function Type1CharStringClosure() {
}, },
executeCommand(howManyArgs, command, keepStack) { executeCommand(howManyArgs, command, keepStack) {
var stackLength = this.stack.length; const stackLength = this.stack.length;
if (howManyArgs > stackLength) { if (howManyArgs > stackLength) {
return true; return true;
} }
var start = stackLength - howManyArgs; const start = stackLength - howManyArgs;
for (var i = start; i < stackLength; i++) { for (let i = start; i < stackLength; i++) {
var value = this.stack[i]; let value = this.stack[i];
if (Number.isInteger(value)) { if (Number.isInteger(value)) {
this.output.push(28, (value >> 8) & 0xff, value & 0xff); this.output.push(28, (value >> 8) & 0xff, value & 0xff);
} else { } else {
@ -377,14 +376,14 @@ var Type1CharString = (function Type1CharStringClosure() {
* of PostScript, but it is possible in most cases to extract what we need * of PostScript, but it is possible in most cases to extract what we need
* without a full parse. * without a full parse.
*/ */
var Type1Parser = (function Type1ParserClosure() { const Type1Parser = (function Type1ParserClosure() {
/* /*
* Decrypt a Sequence of Ciphertext Bytes to Produce the Original Sequence * Decrypt a Sequence of Ciphertext Bytes to Produce the Original Sequence
* of Plaintext Bytes. The function took a key as a parameter which can be * of Plaintext Bytes. The function took a key as a parameter which can be
* for decrypting the eexec block of for decoding charStrings. * for decrypting the eexec block of for decoding charStrings.
*/ */
var EEXEC_ENCRYPT_KEY = 55665; const EEXEC_ENCRYPT_KEY = 55665;
var CHAR_STRS_ENCRYPT_KEY = 4330; const CHAR_STRS_ENCRYPT_KEY = 4330;
function isHexDigit(code) { function isHexDigit(code) {
return ( return (
@ -398,18 +397,18 @@ var Type1Parser = (function Type1ParserClosure() {
if (discardNumber >= data.length) { if (discardNumber >= data.length) {
return new Uint8Array(0); return new Uint8Array(0);
} }
var r = key | 0, const c1 = 52845,
c1 = 52845, c2 = 22719;
c2 = 22719, let r = key | 0,
i, i,
j; j;
for (i = 0; i < discardNumber; i++) { for (i = 0; i < discardNumber; i++) {
r = ((data[i] + r) * c1 + c2) & ((1 << 16) - 1); r = ((data[i] + r) * c1 + c2) & ((1 << 16) - 1);
} }
var count = data.length - discardNumber; const count = data.length - discardNumber;
var decrypted = new Uint8Array(count); const decrypted = new Uint8Array(count);
for (i = discardNumber, j = 0; j < count; i++, j++) { for (i = discardNumber, j = 0; j < count; i++, j++) {
var value = data[i]; const value = data[i];
decrypted[j] = value ^ (r >> 8); decrypted[j] = value ^ (r >> 8);
r = ((value + r) * c1 + c2) & ((1 << 16) - 1); r = ((value + r) * c1 + c2) & ((1 << 16) - 1);
} }
@ -417,25 +416,25 @@ var Type1Parser = (function Type1ParserClosure() {
} }
function decryptAscii(data, key, discardNumber) { function decryptAscii(data, key, discardNumber) {
var r = key | 0, const c1 = 52845,
c1 = 52845,
c2 = 22719; c2 = 22719;
var count = data.length, let r = key | 0;
const count = data.length,
maybeLength = count >>> 1; maybeLength = count >>> 1;
var decrypted = new Uint8Array(maybeLength); const decrypted = new Uint8Array(maybeLength);
var i, j; let i, j;
for (i = 0, j = 0; i < count; i++) { for (i = 0, j = 0; i < count; i++) {
var digit1 = data[i]; const digit1 = data[i];
if (!isHexDigit(digit1)) { if (!isHexDigit(digit1)) {
continue; continue;
} }
i++; i++;
var digit2; let digit2;
while (i < count && !isHexDigit((digit2 = data[i]))) { while (i < count && !isHexDigit((digit2 = data[i]))) {
i++; i++;
} }
if (i < count) { if (i < count) {
var value = parseInt(String.fromCharCode(digit1, digit2), 16); const value = parseInt(String.fromCharCode(digit1, digit2), 16);
decrypted[j++] = value ^ (r >> 8); decrypted[j++] = value ^ (r >> 8);
r = ((value + r) * c1 + c2) & ((1 << 16) - 1); r = ((value + r) * c1 + c2) & ((1 << 16) - 1);
} }
@ -458,8 +457,8 @@ var Type1Parser = (function Type1ParserClosure() {
// eslint-disable-next-line no-shadow // eslint-disable-next-line no-shadow
function Type1Parser(stream, encrypted, seacAnalysisEnabled) { function Type1Parser(stream, encrypted, seacAnalysisEnabled) {
if (encrypted) { if (encrypted) {
var data = stream.getBytes(); const data = stream.getBytes();
var isBinary = !( const isBinary = !(
(isHexDigit(data[0]) || isWhiteSpace(data[0])) && (isHexDigit(data[0]) || isWhiteSpace(data[0])) &&
isHexDigit(data[1]) && isHexDigit(data[1]) &&
isHexDigit(data[2]) && isHexDigit(data[2]) &&
@ -484,9 +483,9 @@ var Type1Parser = (function Type1ParserClosure() {
Type1Parser.prototype = { Type1Parser.prototype = {
readNumberArray: function Type1Parser_readNumberArray() { readNumberArray: function Type1Parser_readNumberArray() {
this.getToken(); // read '[' or '{' (arrays can start with either) this.getToken(); // read '[' or '{' (arrays can start with either)
var array = []; const array = [];
while (true) { while (true) {
var token = this.getToken(); const token = this.getToken();
if (token === null || token === "]" || token === "}") { if (token === null || token === "]" || token === "}") {
break; break;
} }
@ -496,19 +495,19 @@ var Type1Parser = (function Type1ParserClosure() {
}, },
readNumber: function Type1Parser_readNumber() { readNumber: function Type1Parser_readNumber() {
var token = this.getToken(); const token = this.getToken();
return parseFloat(token || 0); return parseFloat(token || 0);
}, },
readInt: function Type1Parser_readInt() { readInt: function Type1Parser_readInt() {
// Use '| 0' to prevent setting a double into length such as the double // Use '| 0' to prevent setting a double into length such as the double
// does not flow into the loop variable. // does not flow into the loop variable.
var token = this.getToken(); const token = this.getToken();
return parseInt(token || 0, 10) | 0; return parseInt(token || 0, 10) | 0;
}, },
readBoolean: function Type1Parser_readBoolean() { readBoolean: function Type1Parser_readBoolean() {
var token = this.getToken(); const token = this.getToken();
// Use 1 and 0 since that's what type2 charstrings use. // Use 1 and 0 since that's what type2 charstrings use.
return token === "true" ? 1 : 0; return token === "true" ? 1 : 0;
@ -520,8 +519,8 @@ var Type1Parser = (function Type1ParserClosure() {
getToken: function Type1Parser_getToken() { getToken: function Type1Parser_getToken() {
// Eat whitespace and comments. // Eat whitespace and comments.
var comment = false; let comment = false;
var ch = this.currentChar; let ch = this.currentChar;
while (true) { while (true) {
if (ch === -1) { if (ch === -1) {
return null; return null;
@ -542,7 +541,7 @@ var Type1Parser = (function Type1ParserClosure() {
this.nextChar(); this.nextChar();
return String.fromCharCode(ch); return String.fromCharCode(ch);
} }
var token = ""; let token = "";
do { do {
token += String.fromCharCode(ch); token += String.fromCharCode(ch);
ch = this.nextChar(); ch = this.nextChar();
@ -564,20 +563,20 @@ var Type1Parser = (function Type1ParserClosure() {
* array extracted from and eexec encrypted block of data * array extracted from and eexec encrypted block of data
*/ */
extractFontProgram: function Type1Parser_extractFontProgram(properties) { extractFontProgram: function Type1Parser_extractFontProgram(properties) {
var stream = this.stream; const stream = this.stream;
var subrs = [], const subrs = [],
charstrings = []; charstrings = [];
var privateData = Object.create(null); const privateData = Object.create(null);
privateData.lenIV = 4; privateData.lenIV = 4;
var program = { const program = {
subrs: [], subrs: [],
charstrings: [], charstrings: [],
properties: { properties: {
privateData, privateData,
}, },
}; };
var token, length, data, lenIV, encoded; let token, length, data, lenIV, encoded;
while ((token = this.getToken()) !== null) { while ((token = this.getToken()) !== null) {
if (token !== "/") { if (token !== "/") {
continue; continue;
@ -600,7 +599,7 @@ var Type1Parser = (function Type1ParserClosure() {
if (token !== "/") { if (token !== "/") {
continue; continue;
} }
var glyph = this.getToken(); const glyph = this.getToken();
length = this.readInt(); length = this.readInt();
this.getToken(); // read in 'RD' or '-|' this.getToken(); // read in 'RD' or '-|'
data = length > 0 ? stream.getBytes(length) : new Uint8Array(0); data = length > 0 ? stream.getBytes(length) : new Uint8Array(0);
@ -639,7 +638,7 @@ var Type1Parser = (function Type1ParserClosure() {
case "OtherBlues": case "OtherBlues":
case "FamilyBlues": case "FamilyBlues":
case "FamilyOtherBlues": case "FamilyOtherBlues":
var blueArray = this.readNumberArray(); const blueArray = this.readNumberArray();
// *Blue* values may contain invalid data: disables reading of // *Blue* values may contain invalid data: disables reading of
// those values when hinting is disabled. // those values when hinting is disabled.
if ( if (
@ -672,16 +671,16 @@ var Type1Parser = (function Type1ParserClosure() {
} }
} }
for (var i = 0; i < charstrings.length; i++) { for (let i = 0; i < charstrings.length; i++) {
glyph = charstrings[i].glyph; const glyph = charstrings[i].glyph;
encoded = charstrings[i].encoded; encoded = charstrings[i].encoded;
var charString = new Type1CharString(); const charString = new Type1CharString();
var error = charString.convert( const error = charString.convert(
encoded, encoded,
subrs, subrs,
this.seacAnalysisEnabled this.seacAnalysisEnabled
); );
var output = charString.output; let output = charString.output;
if (error) { if (error) {
// It seems when FreeType encounters an error while evaluating a glyph // It seems when FreeType encounters an error while evaluating a glyph
// that it completely ignores the glyph so we'll mimic that behaviour // that it completely ignores the glyph so we'll mimic that behaviour
@ -721,7 +720,7 @@ var Type1Parser = (function Type1ParserClosure() {
}, },
extractFontHeader: function Type1Parser_extractFontHeader(properties) { extractFontHeader: function Type1Parser_extractFontHeader(properties) {
var token; let token;
while ((token = this.getToken()) !== null) { while ((token = this.getToken()) !== null) {
if (token !== "/") { if (token !== "/") {
continue; continue;
@ -729,21 +728,21 @@ var Type1Parser = (function Type1ParserClosure() {
token = this.getToken(); token = this.getToken();
switch (token) { switch (token) {
case "FontMatrix": case "FontMatrix":
var matrix = this.readNumberArray(); const matrix = this.readNumberArray();
properties.fontMatrix = matrix; properties.fontMatrix = matrix;
break; break;
case "Encoding": case "Encoding":
var encodingArg = this.getToken(); const encodingArg = this.getToken();
var encoding; let encoding;
if (!/^\d+$/.test(encodingArg)) { if (!/^\d+$/.test(encodingArg)) {
// encoding name is specified // encoding name is specified
encoding = getEncoding(encodingArg); encoding = getEncoding(encodingArg);
} else { } else {
encoding = []; encoding = [];
var size = parseInt(encodingArg, 10) | 0; const size = parseInt(encodingArg, 10) | 0;
this.getToken(); // read in 'array' this.getToken(); // read in 'array'
for (var j = 0; j < size; j++) { for (let j = 0; j < size; j++) {
token = this.getToken(); token = this.getToken();
// skipping till first dup or def (e.g. ignoring for statement) // skipping till first dup or def (e.g. ignoring for statement)
while (token !== "dup" && token !== "def") { while (token !== "dup" && token !== "def") {
@ -755,9 +754,9 @@ var Type1Parser = (function Type1ParserClosure() {
if (token === "def") { if (token === "def") {
break; // read all array data break; // read all array data
} }
var index = this.readInt(); const index = this.readInt();
this.getToken(); // read in '/' this.getToken(); // read in '/'
var glyph = this.getToken(); const glyph = this.getToken();
encoding[index] = glyph; encoding[index] = glyph;
this.getToken(); // read the in 'put' this.getToken(); // read the in 'put'
} }
@ -765,7 +764,7 @@ var Type1Parser = (function Type1ParserClosure() {
properties.builtInEncoding = encoding; properties.builtInEncoding = encoding;
break; break;
case "FontBBox": case "FontBBox":
var fontBBox = this.readNumberArray(); const fontBBox = this.readNumberArray();
// adjusting ascent/descent // adjusting ascent/descent
properties.ascent = Math.max(fontBBox[3], fontBBox[1]); properties.ascent = Math.max(fontBBox[3], fontBBox[1]);
properties.descent = Math.min(fontBBox[1], fontBBox[3]); properties.descent = Math.min(fontBBox[1], fontBBox[3]);