Convert code in src/core/type1_parser.js to use "standard" classes

All of this code predates the existence of native JS classes, however we can now clean this up a little bit.
This commit is contained in:
Jonas Jenwald 2021-03-12 12:08:26 +01:00
parent 8fc8dc020e
commit ab91f42a5e

View File

@ -80,20 +80,16 @@ const Type1CharString = (function Type1CharStringClosure() {
}; };
// eslint-disable-next-line no-shadow // eslint-disable-next-line no-shadow
function Type1CharString() { class Type1CharString {
this.width = 0; constructor() {
this.lsb = 0; this.width = 0;
this.flexing = false; this.lsb = 0;
this.output = []; this.flexing = false;
this.stack = []; this.output = [];
} this.stack = [];
}
Type1CharString.prototype = { convert(encoded, subrs, seacAnalysisEnabled) {
convert: function Type1CharString_convert(
encoded,
subrs,
seacAnalysisEnabled
) {
const count = encoded.length; const count = encoded.length;
let error = false; let error = false;
let wx, sbx, subrNumber; let wx, sbx, subrNumber;
@ -331,7 +327,7 @@ const Type1CharString = (function Type1CharStringClosure() {
this.stack.push(value); this.stack.push(value);
} }
return error; return error;
}, }
executeCommand(howManyArgs, command, keepStack) { executeCommand(howManyArgs, command, keepStack) {
const stackLength = this.stack.length; const stackLength = this.stack.length;
@ -362,8 +358,8 @@ const Type1CharString = (function Type1CharStringClosure() {
this.stack.length = 0; this.stack.length = 0;
} }
return false; return false;
}, }
}; }
return Type1CharString; return Type1CharString;
})(); })();
@ -455,33 +451,33 @@ const Type1Parser = (function Type1ParserClosure() {
} }
// eslint-disable-next-line no-shadow // eslint-disable-next-line no-shadow
function Type1Parser(stream, encrypted, seacAnalysisEnabled) { class Type1Parser {
if (encrypted) { constructor(stream, encrypted, seacAnalysisEnabled) {
const data = stream.getBytes(); if (encrypted) {
const isBinary = !( const data = stream.getBytes();
(isHexDigit(data[0]) || isWhiteSpace(data[0])) && const isBinary = !(
isHexDigit(data[1]) && (isHexDigit(data[0]) || isWhiteSpace(data[0])) &&
isHexDigit(data[2]) && isHexDigit(data[1]) &&
isHexDigit(data[3]) && isHexDigit(data[2]) &&
isHexDigit(data[4]) && isHexDigit(data[3]) &&
isHexDigit(data[5]) && isHexDigit(data[4]) &&
isHexDigit(data[6]) && isHexDigit(data[5]) &&
isHexDigit(data[7]) isHexDigit(data[6]) &&
); isHexDigit(data[7])
stream = new Stream( );
isBinary stream = new Stream(
? decrypt(data, EEXEC_ENCRYPT_KEY, 4) isBinary
: decryptAscii(data, EEXEC_ENCRYPT_KEY, 4) ? decrypt(data, EEXEC_ENCRYPT_KEY, 4)
); : decryptAscii(data, EEXEC_ENCRYPT_KEY, 4)
);
}
this.seacAnalysisEnabled = !!seacAnalysisEnabled;
this.stream = stream;
this.nextChar();
} }
this.seacAnalysisEnabled = !!seacAnalysisEnabled;
this.stream = stream; readNumberArray() {
this.nextChar();
}
Type1Parser.prototype = {
readNumberArray: function Type1Parser_readNumberArray() {
this.getToken(); // read '[' or '{' (arrays can start with either) this.getToken(); // read '[' or '{' (arrays can start with either)
const array = []; const array = [];
while (true) { while (true) {
@ -492,32 +488,31 @@ const Type1Parser = (function Type1ParserClosure() {
array.push(parseFloat(token || 0)); array.push(parseFloat(token || 0));
} }
return array; return array;
}, }
readNumber: function Type1Parser_readNumber() { readNumber() {
const token = this.getToken(); const token = this.getToken();
return parseFloat(token || 0); return parseFloat(token || 0);
}, }
readInt: function Type1Parser_readInt() { 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.
const token = this.getToken(); const token = this.getToken();
return parseInt(token || 0, 10) | 0; return parseInt(token || 0, 10) | 0;
}, }
readBoolean: function Type1Parser_readBoolean() { readBoolean() {
const 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;
}, }
nextChar: function Type1_nextChar() { nextChar() {
return (this.currentChar = this.stream.getByte()); return (this.currentChar = this.stream.getByte());
}, }
getToken: function Type1Parser_getToken() { getToken() {
// Eat whitespace and comments. // Eat whitespace and comments.
let comment = false; let comment = false;
let ch = this.currentChar; let ch = this.currentChar;
@ -547,22 +542,22 @@ const Type1Parser = (function Type1ParserClosure() {
ch = this.nextChar(); ch = this.nextChar();
} while (ch >= 0 && !isWhiteSpace(ch) && !isSpecial(ch)); } while (ch >= 0 && !isWhiteSpace(ch) && !isSpecial(ch));
return token; return token;
}, }
readCharStrings: function Type1Parser_readCharStrings(bytes, lenIV) { readCharStrings(bytes, lenIV) {
if (lenIV === -1) { if (lenIV === -1) {
// This isn't in the spec, but Adobe's tx program handles -1 // This isn't in the spec, but Adobe's tx program handles -1
// as plain text. // as plain text.
return bytes; return bytes;
} }
return decrypt(bytes, CHAR_STRS_ENCRYPT_KEY, lenIV); return decrypt(bytes, CHAR_STRS_ENCRYPT_KEY, lenIV);
}, }
/* /*
* Returns an object containing a Subrs array and a CharStrings * Returns an object containing a Subrs array and a CharStrings
* array extracted from and eexec encrypted block of data * array extracted from and eexec encrypted block of data
*/ */
extractFontProgram: function Type1Parser_extractFontProgram(properties) { extractFontProgram(properties) {
const stream = this.stream; const stream = this.stream;
const subrs = [], const subrs = [],
@ -717,9 +712,9 @@ const Type1Parser = (function Type1ParserClosure() {
} }
return program; return program;
}, }
extractFontHeader: function Type1Parser_extractFontHeader(properties) { extractFontHeader(properties) {
let token; let token;
while ((token = this.getToken()) !== null) { while ((token = this.getToken()) !== null) {
if (token !== "/") { if (token !== "/") {
@ -772,8 +767,8 @@ const Type1Parser = (function Type1ParserClosure() {
break; break;
} }
} }
}, }
}; }
return Type1Parser; return Type1Parser;
})(); })();