Merge pull request #15315 from Snuffleupagus/type1-parser-rm-closure

Remove the remaining closures in the `src/core/type1_parser.js` file
This commit is contained in:
Jonas Jenwald 2022-08-15 18:56:31 +02:00 committed by GitHub
commit 518115fddc
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -22,7 +22,25 @@ import { warn } from "../shared/util.js";
// in tracemonkey and various other pdfs with type1 fonts. // in tracemonkey and various other pdfs with type1 fonts.
const HINTING_ENABLED = false; const HINTING_ENABLED = false;
/* const COMMAND_MAP = {
hstem: [1],
vstem: [3],
vmoveto: [4],
rlineto: [5],
hlineto: [6],
vlineto: [7],
rrcurveto: [8],
callsubr: [10],
flex: [12, 35],
drop: [12, 18],
endchar: [14],
rmoveto: [21],
hmoveto: [22],
vhcurveto: [30],
hvcurveto: [31],
};
/**
* CharStrings are encoded following the the CharString Encoding sequence * CharStrings are encoded following the the CharString Encoding sequence
* describe in Chapter 6 of the "Adobe Type1 Font Format" specification. * describe in Chapter 6 of the "Adobe Type1 Font Format" specification.
* The value in a byte indicates a command, a number, or subsequent bytes * The value in a byte indicates a command, a number, or subsequent bytes
@ -60,27 +78,7 @@ const 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.
*/ */
const Type1CharString = (function Type1CharStringClosure() { class Type1CharString {
const COMMAND_MAP = {
hstem: [1],
vstem: [3],
vmoveto: [4],
rlineto: [5],
hlineto: [6],
vlineto: [7],
rrcurveto: [8],
callsubr: [10],
flex: [12, 35],
drop: [12, 18],
endchar: [14],
rmoveto: [21],
hmoveto: [22],
vhcurveto: [30],
hvcurveto: [31],
};
// eslint-disable-next-line no-shadow
class Type1CharString {
constructor() { constructor() {
this.width = 0; this.width = 0;
this.lsb = 0; this.lsb = 0;
@ -155,11 +153,7 @@ const Type1CharString = (function Type1CharStringClosure() {
error = true; error = true;
break; break;
} }
error = this.convert( error = this.convert(subrs[subrNumber], subrs, seacAnalysisEnabled);
subrs[subrNumber],
subrs,
seacAnalysisEnabled
);
break; break;
case 11: // return case 11: // return
return error; return error;
@ -359,37 +353,25 @@ const Type1CharString = (function Type1CharStringClosure() {
} }
return false; return false;
} }
} }
return Type1CharString; /**
})();
/*
* Type1Parser encapsulate the needed code for parsing a Type1 font
* program. Some of its logic depends on the Type2 charstrings
* structure.
* Note: this doesn't really parse the font since that would require evaluation
* of PostScript, but it is possible in most cases to extract what we need
* without a full parse.
*/
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 takes a key as a parameter which can be
* for decrypting the eexec block of for decoding charStrings. * for decrypting the eexec block or for decoding charStrings.
*/ */
const EEXEC_ENCRYPT_KEY = 55665; const EEXEC_ENCRYPT_KEY = 55665;
const CHAR_STRS_ENCRYPT_KEY = 4330; const CHAR_STRS_ENCRYPT_KEY = 4330;
function isHexDigit(code) { function isHexDigit(code) {
return ( return (
(code >= 48 && code <= 57) || // '0'-'9' (code >= 48 && code <= 57) || // '0'-'9'
(code >= 65 && code <= 70) || // 'A'-'F' (code >= 65 && code <= 70) || // 'A'-'F'
(code >= 97 && code <= 102) // 'a'-'f' (code >= 97 && code <= 102) // 'a'-'f'
); );
} }
function decrypt(data, key, discardNumber) { function decrypt(data, key, discardNumber) {
if (discardNumber >= data.length) { if (discardNumber >= data.length) {
return new Uint8Array(0); return new Uint8Array(0);
} }
@ -409,9 +391,9 @@ const Type1Parser = (function Type1ParserClosure() {
r = ((value + r) * c1 + c2) & ((1 << 16) - 1); r = ((value + r) * c1 + c2) & ((1 << 16) - 1);
} }
return decrypted; return decrypted;
} }
function decryptAscii(data, key, discardNumber) { function decryptAscii(data, key, discardNumber) {
const c1 = 52845, const c1 = 52845,
c2 = 22719; c2 = 22719;
let r = key | 0; let r = key | 0;
@ -436,9 +418,9 @@ const Type1Parser = (function Type1ParserClosure() {
} }
} }
return decrypted.slice(discardNumber, j); return decrypted.slice(discardNumber, j);
} }
function isSpecial(c) { function isSpecial(c) {
return ( return (
c === /* '/' = */ 0x2f || c === /* '/' = */ 0x2f ||
c === /* '[' = */ 0x5b || c === /* '[' = */ 0x5b ||
@ -448,10 +430,16 @@ const Type1Parser = (function Type1ParserClosure() {
c === /* '(' = */ 0x28 || c === /* '(' = */ 0x28 ||
c === /* ')' = */ 0x29 c === /* ')' = */ 0x29
); );
} }
// eslint-disable-next-line no-shadow /**
class Type1Parser { * Type1Parser encapsulate the needed code for parsing a Type1 font program.
* Some of its logic depends on the Type2 charstrings structure.
* NOTE: This doesn't really parse the font since that would require evaluation
* of PostScript, but it is possible in most cases to extract what we need
* without a full parse.
*/
class Type1Parser {
constructor(stream, encrypted, seacAnalysisEnabled) { constructor(stream, encrypted, seacAnalysisEnabled) {
if (encrypted) { if (encrypted) {
const data = stream.getBytes(); const data = stream.getBytes();
@ -775,9 +763,6 @@ const Type1Parser = (function Type1ParserClosure() {
} }
} }
} }
} }
return Type1Parser;
})();
export { Type1Parser }; export { Type1Parser };