From 3b637e71d4e7bdf92e163c79f56b6f67182a8240 Mon Sep 17 00:00:00 2001 From: Tim van der Meij Date: Sat, 5 Jan 2019 18:51:37 +0100 Subject: [PATCH 1/2] Convert `src/core/arithmetic_decoder.js` to ES6 syntax --- src/core/arithmetic_decoder.js | 263 ++++++++++++++++----------------- 1 file changed, 131 insertions(+), 132 deletions(-) diff --git a/src/core/arithmetic_decoder.js b/src/core/arithmetic_decoder.js index 446a2c4f839e9..6d589c8efdac7 100644 --- a/src/core/arithmetic_decoder.js +++ b/src/core/arithmetic_decoder.js @@ -12,8 +12,61 @@ * See the License for the specific language governing permissions and * limitations under the License. */ +/* eslint no-var: error */ -/* This class implements the QM Coder decoding as defined in +// Table C-2 +const QeTable = [ + { qe: 0x5601, nmps: 1, nlps: 1, switchFlag: 1, }, + { qe: 0x3401, nmps: 2, nlps: 6, switchFlag: 0, }, + { qe: 0x1801, nmps: 3, nlps: 9, switchFlag: 0, }, + { qe: 0x0AC1, nmps: 4, nlps: 12, switchFlag: 0, }, + { qe: 0x0521, nmps: 5, nlps: 29, switchFlag: 0, }, + { qe: 0x0221, nmps: 38, nlps: 33, switchFlag: 0, }, + { qe: 0x5601, nmps: 7, nlps: 6, switchFlag: 1, }, + { qe: 0x5401, nmps: 8, nlps: 14, switchFlag: 0, }, + { qe: 0x4801, nmps: 9, nlps: 14, switchFlag: 0, }, + { qe: 0x3801, nmps: 10, nlps: 14, switchFlag: 0, }, + { qe: 0x3001, nmps: 11, nlps: 17, switchFlag: 0, }, + { qe: 0x2401, nmps: 12, nlps: 18, switchFlag: 0, }, + { qe: 0x1C01, nmps: 13, nlps: 20, switchFlag: 0, }, + { qe: 0x1601, nmps: 29, nlps: 21, switchFlag: 0, }, + { qe: 0x5601, nmps: 15, nlps: 14, switchFlag: 1, }, + { qe: 0x5401, nmps: 16, nlps: 14, switchFlag: 0, }, + { qe: 0x5101, nmps: 17, nlps: 15, switchFlag: 0, }, + { qe: 0x4801, nmps: 18, nlps: 16, switchFlag: 0, }, + { qe: 0x3801, nmps: 19, nlps: 17, switchFlag: 0, }, + { qe: 0x3401, nmps: 20, nlps: 18, switchFlag: 0, }, + { qe: 0x3001, nmps: 21, nlps: 19, switchFlag: 0, }, + { qe: 0x2801, nmps: 22, nlps: 19, switchFlag: 0, }, + { qe: 0x2401, nmps: 23, nlps: 20, switchFlag: 0, }, + { qe: 0x2201, nmps: 24, nlps: 21, switchFlag: 0, }, + { qe: 0x1C01, nmps: 25, nlps: 22, switchFlag: 0, }, + { qe: 0x1801, nmps: 26, nlps: 23, switchFlag: 0, }, + { qe: 0x1601, nmps: 27, nlps: 24, switchFlag: 0, }, + { qe: 0x1401, nmps: 28, nlps: 25, switchFlag: 0, }, + { qe: 0x1201, nmps: 29, nlps: 26, switchFlag: 0, }, + { qe: 0x1101, nmps: 30, nlps: 27, switchFlag: 0, }, + { qe: 0x0AC1, nmps: 31, nlps: 28, switchFlag: 0, }, + { qe: 0x09C1, nmps: 32, nlps: 29, switchFlag: 0, }, + { qe: 0x08A1, nmps: 33, nlps: 30, switchFlag: 0, }, + { qe: 0x0521, nmps: 34, nlps: 31, switchFlag: 0, }, + { qe: 0x0441, nmps: 35, nlps: 32, switchFlag: 0, }, + { qe: 0x02A1, nmps: 36, nlps: 33, switchFlag: 0, }, + { qe: 0x0221, nmps: 37, nlps: 34, switchFlag: 0, }, + { qe: 0x0141, nmps: 38, nlps: 35, switchFlag: 0, }, + { qe: 0x0111, nmps: 39, nlps: 36, switchFlag: 0, }, + { qe: 0x0085, nmps: 40, nlps: 37, switchFlag: 0, }, + { qe: 0x0049, nmps: 41, nlps: 38, switchFlag: 0, }, + { qe: 0x0025, nmps: 42, nlps: 39, switchFlag: 0, }, + { qe: 0x0015, nmps: 43, nlps: 40, switchFlag: 0, }, + { qe: 0x0009, nmps: 44, nlps: 41, switchFlag: 0, }, + { qe: 0x0005, nmps: 45, nlps: 42, switchFlag: 0, }, + { qe: 0x0001, nmps: 45, nlps: 43, switchFlag: 0, }, + { qe: 0x5601, nmps: 46, nlps: 46, switchFlag: 0, }, +]; + +/** + * This class implements the QM Coder decoding as defined in * JPEG 2000 Part I Final Committee Draft Version 1.0 * Annex C.3 Arithmetic decoding procedure * available at http://www.jpeg.org/public/fcd15444-1.pdf @@ -21,60 +74,9 @@ * The arithmetic decoder is used in conjunction with context models to decode * JPEG2000 and JBIG2 streams. */ -var ArithmeticDecoder = (function ArithmeticDecoderClosure() { - // Table C-2 - var QeTable = [ - { qe: 0x5601, nmps: 1, nlps: 1, switchFlag: 1, }, - { qe: 0x3401, nmps: 2, nlps: 6, switchFlag: 0, }, - { qe: 0x1801, nmps: 3, nlps: 9, switchFlag: 0, }, - { qe: 0x0AC1, nmps: 4, nlps: 12, switchFlag: 0, }, - { qe: 0x0521, nmps: 5, nlps: 29, switchFlag: 0, }, - { qe: 0x0221, nmps: 38, nlps: 33, switchFlag: 0, }, - { qe: 0x5601, nmps: 7, nlps: 6, switchFlag: 1, }, - { qe: 0x5401, nmps: 8, nlps: 14, switchFlag: 0, }, - { qe: 0x4801, nmps: 9, nlps: 14, switchFlag: 0, }, - { qe: 0x3801, nmps: 10, nlps: 14, switchFlag: 0, }, - { qe: 0x3001, nmps: 11, nlps: 17, switchFlag: 0, }, - { qe: 0x2401, nmps: 12, nlps: 18, switchFlag: 0, }, - { qe: 0x1C01, nmps: 13, nlps: 20, switchFlag: 0, }, - { qe: 0x1601, nmps: 29, nlps: 21, switchFlag: 0, }, - { qe: 0x5601, nmps: 15, nlps: 14, switchFlag: 1, }, - { qe: 0x5401, nmps: 16, nlps: 14, switchFlag: 0, }, - { qe: 0x5101, nmps: 17, nlps: 15, switchFlag: 0, }, - { qe: 0x4801, nmps: 18, nlps: 16, switchFlag: 0, }, - { qe: 0x3801, nmps: 19, nlps: 17, switchFlag: 0, }, - { qe: 0x3401, nmps: 20, nlps: 18, switchFlag: 0, }, - { qe: 0x3001, nmps: 21, nlps: 19, switchFlag: 0, }, - { qe: 0x2801, nmps: 22, nlps: 19, switchFlag: 0, }, - { qe: 0x2401, nmps: 23, nlps: 20, switchFlag: 0, }, - { qe: 0x2201, nmps: 24, nlps: 21, switchFlag: 0, }, - { qe: 0x1C01, nmps: 25, nlps: 22, switchFlag: 0, }, - { qe: 0x1801, nmps: 26, nlps: 23, switchFlag: 0, }, - { qe: 0x1601, nmps: 27, nlps: 24, switchFlag: 0, }, - { qe: 0x1401, nmps: 28, nlps: 25, switchFlag: 0, }, - { qe: 0x1201, nmps: 29, nlps: 26, switchFlag: 0, }, - { qe: 0x1101, nmps: 30, nlps: 27, switchFlag: 0, }, - { qe: 0x0AC1, nmps: 31, nlps: 28, switchFlag: 0, }, - { qe: 0x09C1, nmps: 32, nlps: 29, switchFlag: 0, }, - { qe: 0x08A1, nmps: 33, nlps: 30, switchFlag: 0, }, - { qe: 0x0521, nmps: 34, nlps: 31, switchFlag: 0, }, - { qe: 0x0441, nmps: 35, nlps: 32, switchFlag: 0, }, - { qe: 0x02A1, nmps: 36, nlps: 33, switchFlag: 0, }, - { qe: 0x0221, nmps: 37, nlps: 34, switchFlag: 0, }, - { qe: 0x0141, nmps: 38, nlps: 35, switchFlag: 0, }, - { qe: 0x0111, nmps: 39, nlps: 36, switchFlag: 0, }, - { qe: 0x0085, nmps: 40, nlps: 37, switchFlag: 0, }, - { qe: 0x0049, nmps: 41, nlps: 38, switchFlag: 0, }, - { qe: 0x0025, nmps: 42, nlps: 39, switchFlag: 0, }, - { qe: 0x0015, nmps: 43, nlps: 40, switchFlag: 0, }, - { qe: 0x0009, nmps: 44, nlps: 41, switchFlag: 0, }, - { qe: 0x0005, nmps: 45, nlps: 42, switchFlag: 0, }, - { qe: 0x0001, nmps: 45, nlps: 43, switchFlag: 0, }, - { qe: 0x5601, nmps: 46, nlps: 46, switchFlag: 0, } - ]; - +class ArithmeticDecoder { // C.3.5 Initialisation of the decoder (INITDEC) - function ArithmeticDecoder(data, start, end) { + constructor(data, start, end) { this.data = data; this.bp = start; this.dataEnd = end; @@ -90,95 +92,92 @@ var ArithmeticDecoder = (function ArithmeticDecoderClosure() { this.a = 0x8000; } - ArithmeticDecoder.prototype = { - // C.3.4 Compressed data input (BYTEIN) - byteIn: function ArithmeticDecoder_byteIn() { - var data = this.data; - var bp = this.bp; - if (data[bp] === 0xFF) { - var b1 = data[bp + 1]; - if (b1 > 0x8F) { - this.clow += 0xFF00; - this.ct = 8; - } else { - bp++; - this.clow += (data[bp] << 9); - this.ct = 7; - this.bp = bp; - } + // C.3.4 Compressed data input (BYTEIN) + byteIn() { + const data = this.data; + let bp = this.bp; + + if (data[bp] === 0xFF) { + if (data[bp + 1] > 0x8F) { + this.clow += 0xFF00; + this.ct = 8; } else { bp++; - this.clow += bp < this.dataEnd ? (data[bp] << 8) : 0xFF00; - this.ct = 8; + this.clow += (data[bp] << 9); + this.ct = 7; this.bp = bp; } - if (this.clow > 0xFFFF) { - this.chigh += (this.clow >> 16); - this.clow &= 0xFFFF; - } - }, - // C.3.2 Decoding a decision (DECODE) - readBit: function ArithmeticDecoder_readBit(contexts, pos) { - // contexts are packed into 1 byte: - // highest 7 bits carry cx.index, lowest bit carries cx.mps - var cx_index = contexts[pos] >> 1, cx_mps = contexts[pos] & 1; - var qeTableIcx = QeTable[cx_index]; - var qeIcx = qeTableIcx.qe; - var d; - var a = this.a - qeIcx; + } else { + bp++; + this.clow += bp < this.dataEnd ? (data[bp] << 8) : 0xFF00; + this.ct = 8; + this.bp = bp; + } + if (this.clow > 0xFFFF) { + this.chigh += (this.clow >> 16); + this.clow &= 0xFFFF; + } + } - if (this.chigh < qeIcx) { - // exchangeLps - if (a < qeIcx) { - a = qeIcx; - d = cx_mps; - cx_index = qeTableIcx.nmps; - } else { - a = qeIcx; - d = 1 ^ cx_mps; - if (qeTableIcx.switchFlag === 1) { - cx_mps = d; - } - cx_index = qeTableIcx.nlps; - } + // C.3.2 Decoding a decision (DECODE) + readBit(contexts, pos) { + // Contexts are packed into 1 byte: + // highest 7 bits carry cx.index, lowest bit carries cx.mps + let cx_index = contexts[pos] >> 1, cx_mps = contexts[pos] & 1; + const qeTableIcx = QeTable[cx_index]; + const qeIcx = qeTableIcx.qe; + let d; + let a = this.a - qeIcx; + + if (this.chigh < qeIcx) { + // exchangeLps + if (a < qeIcx) { + a = qeIcx; + d = cx_mps; + cx_index = qeTableIcx.nmps; } else { - this.chigh -= qeIcx; - if ((a & 0x8000) !== 0) { - this.a = a; - return cx_mps; - } - // exchangeMps - if (a < qeIcx) { - d = 1 ^ cx_mps; - if (qeTableIcx.switchFlag === 1) { - cx_mps = d; - } - cx_index = qeTableIcx.nlps; - } else { - d = cx_mps; - cx_index = qeTableIcx.nmps; + a = qeIcx; + d = 1 ^ cx_mps; + if (qeTableIcx.switchFlag === 1) { + cx_mps = d; } + cx_index = qeTableIcx.nlps; } - // C.3.3 renormD; - do { - if (this.ct === 0) { - this.byteIn(); + } else { + this.chigh -= qeIcx; + if ((a & 0x8000) !== 0) { + this.a = a; + return cx_mps; + } + // exchangeMps + if (a < qeIcx) { + d = 1 ^ cx_mps; + if (qeTableIcx.switchFlag === 1) { + cx_mps = d; } + cx_index = qeTableIcx.nlps; + } else { + d = cx_mps; + cx_index = qeTableIcx.nmps; + } + } + // C.3.3 renormD; + do { + if (this.ct === 0) { + this.byteIn(); + } - a <<= 1; - this.chigh = ((this.chigh << 1) & 0xFFFF) | ((this.clow >> 15) & 1); - this.clow = (this.clow << 1) & 0xFFFF; - this.ct--; - } while ((a & 0x8000) === 0); - this.a = a; - - contexts[pos] = cx_index << 1 | cx_mps; - return d; - }, - }; + a <<= 1; + this.chigh = ((this.chigh << 1) & 0xFFFF) | ((this.clow >> 15) & 1); + this.clow = (this.clow << 1) & 0xFFFF; + this.ct--; + } while ((a & 0x8000) === 0); + this.a = a; - return ArithmeticDecoder; -})(); + contexts[pos] = cx_index << 1 | cx_mps; + return d; + } +} export { ArithmeticDecoder, From f162fed6b9b017e82df28d4b66492d0c59e02fa8 Mon Sep 17 00:00:00 2001 From: Tim van der Meij Date: Sat, 5 Jan 2019 19:03:53 +0100 Subject: [PATCH 2/2] Convert `src/core/charsets.js` and `src/core/standard_fonts.js` to ES6 syntax Moreover, include the "no var" ESLint comment to `src/core/annotation.js` and `src/core/ps_parser.js` since they are already converted. --- src/core/annotation.js | 1 + src/core/charsets.js | 7 ++++--- src/core/ps_parser.js | 1 + src/core/standard_fonts.js | 15 ++++++++------- 4 files changed, 14 insertions(+), 10 deletions(-) diff --git a/src/core/annotation.js b/src/core/annotation.js index db95bd6be94a7..c9dfa7fafce88 100644 --- a/src/core/annotation.js +++ b/src/core/annotation.js @@ -12,6 +12,7 @@ * See the License for the specific language governing permissions and * limitations under the License. */ +/* eslint no-var: error */ import { AnnotationBorderStyleType, AnnotationFieldFlag, AnnotationFlag, diff --git a/src/core/charsets.js b/src/core/charsets.js index fcf47da3a442e..2f6652efa463d 100644 --- a/src/core/charsets.js +++ b/src/core/charsets.js @@ -12,8 +12,9 @@ * See the License for the specific language governing permissions and * limitations under the License. */ +/* eslint no-var: error */ -var ISOAdobeCharset = [ +const ISOAdobeCharset = [ '.notdef', 'space', 'exclam', 'quotedbl', 'numbersign', 'dollar', 'percent', 'ampersand', 'quoteright', 'parenleft', 'parenright', 'asterisk', 'plus', 'comma', 'hyphen', 'period', 'slash', 'zero', @@ -50,7 +51,7 @@ var ISOAdobeCharset = [ 'ugrave', 'yacute', 'ydieresis', 'zcaron' ]; -var ExpertCharset = [ +const ExpertCharset = [ '.notdef', 'space', 'exclamsmall', 'Hungarumlautsmall', 'dollaroldstyle', 'dollarsuperior', 'ampersandsmall', 'Acutesmall', 'parenleftsuperior', 'parenrightsuperior', 'twodotenleader', 'onedotenleader', 'comma', @@ -90,7 +91,7 @@ var ExpertCharset = [ 'Ydieresissmall' ]; -var ExpertSubsetCharset = [ +const ExpertSubsetCharset = [ '.notdef', 'space', 'dollaroldstyle', 'dollarsuperior', 'parenleftsuperior', 'parenrightsuperior', 'twodotenleader', 'onedotenleader', 'comma', 'hyphen', 'period', 'fraction', diff --git a/src/core/ps_parser.js b/src/core/ps_parser.js index e28a6e98a6721..8428760a1968a 100644 --- a/src/core/ps_parser.js +++ b/src/core/ps_parser.js @@ -12,6 +12,7 @@ * See the License for the specific language governing permissions and * limitations under the License. */ +/* eslint no-var: error */ import { FormatError, isSpace, shadow } from '../shared/util'; import { EOF } from './primitives'; diff --git a/src/core/standard_fonts.js b/src/core/standard_fonts.js index 3cb2e0160c592..9ead2b1c7c442 100644 --- a/src/core/standard_fonts.js +++ b/src/core/standard_fonts.js @@ -12,6 +12,7 @@ * See the License for the specific language governing permissions and * limitations under the License. */ +/* eslint no-var: error */ import { getLookupTableFactory } from '../shared/util'; @@ -19,7 +20,7 @@ import { getLookupTableFactory } from '../shared/util'; * Hold a map of decoded fonts and of the standard fourteen Type1 * fonts and their acronyms. */ -var getStdFontMap = getLookupTableFactory(function (t) { +const getStdFontMap = getLookupTableFactory(function (t) { t['ArialNarrow'] = 'Helvetica'; t['ArialNarrow-Bold'] = 'Helvetica-Bold'; t['ArialNarrow-BoldItalic'] = 'Helvetica-BoldOblique'; @@ -82,7 +83,7 @@ var getStdFontMap = getLookupTableFactory(function (t) { * Holds the map of the non-standard fonts that might be included as * a standard fonts without glyph data. */ -var getNonStdFontMap = getLookupTableFactory(function (t) { +const getNonStdFontMap = getLookupTableFactory(function (t) { t['Calibri'] = 'Helvetica'; t['Calibri-Bold'] = 'Helvetica-Bold'; t['Calibri-BoldItalic'] = 'Helvetica-BoldOblique'; @@ -120,7 +121,7 @@ var getNonStdFontMap = getLookupTableFactory(function (t) { t['Wingdings'] = 'ZapfDingbats'; }); -var getSerifFonts = getLookupTableFactory(function (t) { +const getSerifFonts = getLookupTableFactory(function (t) { t['Adobe Jenson'] = true; t['Adobe Text'] = true; t['Albertus'] = true; @@ -256,7 +257,7 @@ var getSerifFonts = getLookupTableFactory(function (t) { t['XITS'] = true; }); -var getSymbolsFonts = getLookupTableFactory(function (t) { +const getSymbolsFonts = getLookupTableFactory(function (t) { t['Dingbats'] = true; t['Symbol'] = true; t['ZapfDingbats'] = true; @@ -265,7 +266,7 @@ var getSymbolsFonts = getLookupTableFactory(function (t) { // Glyph map for well-known standard fonts. Sometimes Ghostscript uses CID // fonts, but does not embed the CID to GID mapping. The mapping is incomplete // for all glyphs, but common for some set of the standard fonts. -var getGlyphMapForStandardFonts = getLookupTableFactory(function (t) { +const getGlyphMapForStandardFonts = getLookupTableFactory(function (t) { t[2] = 10; t[3] = 32; t[4] = 33; t[5] = 34; t[6] = 35; t[7] = 36; t[8] = 37; t[9] = 38; t[10] = 39; t[11] = 40; t[12] = 41; t[13] = 42; t[14] = 43; t[15] = 44; t[16] = 45; t[17] = 46; t[18] = 47; t[19] = 48; t[20] = 49; @@ -355,7 +356,7 @@ var getGlyphMapForStandardFonts = getLookupTableFactory(function (t) { // The glyph map for ArialBlack differs slightly from the glyph map used for // other well-known standard fonts. Hence we use this (incomplete) CID to GID // mapping to adjust the glyph map for non-embedded ArialBlack fonts. -var getSupplementalGlyphMapForArialBlack = +const getSupplementalGlyphMapForArialBlack = getLookupTableFactory(function (t) { t[227] = 322; t[264] = 261; t[291] = 346; }); @@ -363,7 +364,7 @@ var getSupplementalGlyphMapForArialBlack = // The glyph map for Calibri (a Windows font) differs from the glyph map used // in the standard fonts. Hence we use this (incomplete) CID to GID mapping to // adjust the glyph map for non-embedded Calibri fonts. -let getSupplementalGlyphMapForCalibri = getLookupTableFactory(function(t) { +const getSupplementalGlyphMapForCalibri = getLookupTableFactory(function(t) { t[1] = 32; t[4] = 65; t[17] = 66; t[18] = 67; t[24] = 68; t[28] = 69; t[38] = 70; t[39] = 71; t[44] = 72; t[47] = 73; t[58] = 74; t[60] = 75; t[62] = 76; t[68] = 77; t[69] = 78; t[75] = 79; t[87] = 80; t[89] = 81;