Skip to content
Snippets Groups Projects
pdfjs.parser.js 367 KiB
Newer Older
  • Learn to ignore specific revisions
  • Alexey Lunin's avatar
    Alexey Lunin committed
    7001 7002 7003 7004 7005 7006 7007 7008 7009 7010 7011 7012 7013 7014 7015 7016 7017 7018 7019 7020 7021 7022 7023 7024 7025 7026 7027 7028 7029 7030 7031 7032 7033 7034 7035 7036 7037 7038 7039 7040 7041 7042 7043 7044 7045 7046 7047 7048 7049 7050 7051 7052 7053 7054 7055 7056 7057 7058 7059 7060 7061 7062 7063 7064 7065 7066 7067 7068 7069 7070 7071 7072 7073 7074 7075 7076 7077 7078 7079 7080 7081 7082 7083 7084 7085 7086 7087 7088 7089 7090 7091 7092 7093 7094 7095 7096 7097 7098 7099 7100 7101 7102 7103 7104 7105 7106 7107 7108 7109 7110 7111 7112 7113 7114 7115 7116 7117 7118 7119 7120 7121 7122 7123 7124 7125 7126 7127 7128 7129 7130 7131 7132 7133 7134 7135 7136 7137 7138 7139 7140 7141 7142 7143 7144 7145 7146 7147 7148 7149 7150 7151 7152 7153 7154 7155 7156 7157 7158 7159 7160 7161 7162 7163 7164 7165 7166 7167 7168 7169 7170 7171 7172 7173 7174 7175 7176 7177 7178 7179 7180 7181 7182 7183 7184 7185 7186 7187 7188 7189 7190 7191 7192 7193 7194 7195 7196 7197 7198 7199 7200 7201 7202 7203 7204 7205 7206 7207 7208 7209 7210 7211 7212 7213 7214 7215 7216 7217 7218 7219 7220 7221 7222 7223 7224 7225 7226 7227 7228 7229 7230 7231 7232 7233 7234 7235 7236 7237 7238 7239 7240 7241 7242 7243 7244 7245 7246 7247 7248 7249 7250 7251 7252 7253 7254 7255 7256 7257 7258 7259 7260 7261 7262 7263 7264 7265 7266 7267 7268 7269 7270 7271 7272 7273 7274 7275 7276 7277 7278 7279 7280 7281 7282 7283 7284 7285 7286 7287 7288 7289 7290 7291 7292 7293 7294 7295 7296 7297 7298 7299 7300 7301 7302 7303 7304 7305 7306 7307 7308 7309 7310 7311 7312 7313 7314 7315 7316 7317 7318 7319 7320 7321 7322 7323 7324 7325 7326 7327 7328 7329 7330 7331 7332 7333 7334 7335 7336 7337 7338 7339 7340 7341 7342 7343 7344 7345 7346 7347 7348 7349 7350 7351 7352 7353 7354 7355 7356 7357 7358 7359 7360 7361 7362 7363 7364 7365 7366 7367 7368 7369 7370 7371 7372 7373 7374 7375 7376 7377 7378 7379 7380 7381 7382 7383 7384 7385 7386 7387 7388 7389 7390 7391 7392 7393 7394 7395 7396 7397 7398 7399 7400 7401 7402 7403 7404 7405 7406 7407 7408 7409 7410 7411 7412 7413 7414 7415 7416 7417 7418 7419 7420 7421 7422 7423 7424 7425 7426 7427 7428 7429 7430 7431 7432 7433 7434 7435 7436 7437 7438 7439 7440 7441 7442 7443 7444 7445 7446 7447 7448 7449 7450 7451 7452 7453 7454 7455 7456 7457 7458 7459 7460 7461 7462 7463 7464 7465 7466 7467 7468 7469 7470 7471 7472 7473 7474 7475 7476 7477 7478 7479 7480 7481 7482 7483 7484 7485 7486 7487 7488 7489 7490 7491 7492 7493 7494 7495 7496 7497 7498 7499 7500 7501 7502 7503 7504 7505 7506 7507 7508 7509 7510 7511 7512 7513 7514 7515 7516 7517 7518 7519 7520 7521 7522 7523 7524 7525 7526 7527 7528 7529 7530 7531 7532 7533 7534 7535 7536 7537 7538 7539 7540 7541 7542 7543 7544 7545 7546 7547 7548 7549 7550 7551 7552 7553 7554 7555 7556 7557 7558 7559 7560 7561 7562 7563 7564 7565 7566 7567 7568 7569 7570 7571 7572 7573 7574 7575 7576 7577 7578 7579 7580 7581 7582 7583 7584 7585 7586 7587 7588 7589 7590 7591 7592 7593 7594 7595 7596 7597 7598 7599 7600 7601 7602 7603 7604 7605 7606 7607 7608 7609 7610 7611 7612 7613 7614 7615 7616 7617 7618 7619 7620 7621 7622 7623 7624 7625 7626 7627 7628 7629 7630 7631 7632 7633 7634 7635 7636 7637 7638 7639 7640 7641 7642 7643 7644 7645 7646 7647 7648 7649 7650 7651 7652 7653 7654 7655 7656 7657 7658 7659 7660 7661 7662 7663 7664 7665 7666 7667 7668 7669 7670 7671 7672 7673 7674 7675 7676 7677 7678 7679 7680 7681 7682 7683 7684 7685 7686 7687 7688 7689 7690 7691 7692 7693 7694 7695 7696 7697 7698 7699 7700 7701 7702 7703 7704 7705 7706 7707 7708 7709 7710 7711 7712 7713 7714 7715 7716 7717 7718 7719 7720 7721 7722 7723 7724 7725 7726 7727 7728 7729 7730 7731 7732 7733 7734 7735 7736 7737 7738 7739 7740 7741 7742 7743 7744 7745 7746 7747 7748 7749 7750 7751 7752 7753 7754 7755 7756 7757 7758 7759 7760 7761 7762 7763 7764 7765 7766 7767 7768 7769 7770 7771 7772 7773 7774 7775 7776 7777 7778 7779 7780 7781 7782 7783 7784 7785 7786 7787 7788 7789 7790 7791 7792 7793 7794 7795 7796 7797 7798 7799 7800 7801 7802 7803 7804 7805 7806 7807 7808 7809 7810 7811 7812 7813 7814 7815 7816 7817 7818 7819 7820 7821 7822 7823 7824 7825 7826 7827 7828 7829 7830 7831 7832 7833 7834 7835 7836 7837 7838 7839 7840 7841 7842 7843 7844 7845 7846 7847 7848 7849 7850 7851 7852 7853 7854 7855 7856 7857 7858 7859 7860 7861 7862 7863 7864 7865 7866 7867 7868 7869 7870 7871 7872 7873 7874 7875 7876 7877 7878 7879 7880 7881 7882 7883 7884 7885 7886 7887 7888 7889 7890 7891 7892 7893 7894 7895 7896 7897 7898 7899 7900 7901 7902 7903 7904 7905 7906 7907 7908 7909 7910 7911 7912 7913 7914 7915 7916 7917 7918 7919 7920 7921 7922 7923 7924 7925 7926 7927 7928 7929 7930 7931 7932 7933 7934 7935 7936 7937 7938 7939 7940 7941 7942 7943 7944 7945 7946 7947 7948 7949 7950 7951 7952 7953 7954 7955 7956 7957 7958 7959 7960 7961 7962 7963 7964 7965 7966 7967 7968 7969 7970 7971 7972 7973 7974 7975 7976 7977 7978 7979 7980 7981 7982 7983 7984 7985 7986 7987 7988 7989 7990 7991 7992 7993 7994 7995 7996 7997 7998 7999 8000
              }
            }
            return stream.pos - 4 - startPos;
          },
          /**
           * Find the EOI (end-of-image) marker 0xFFD9 of the stream.
           * @returns {number} The inline stream length.
           */
          findDCTDecodeInlineStreamEnd: function Parser_findDCTDecodeInlineStreamEnd(
            stream
          ) {
            var startPos = stream.pos,
              foundEOI = false,
              b,
              markerLength,
              length;
            while ((b = stream.getByte()) !== -1) {
              if (b !== 0xff) {
                // Not a valid marker.
                continue;
              }
              switch (stream.getByte()) {
                case 0x00: // Byte stuffing.
                  // 0xFF00 appears to be a very common byte sequence in JPEG images.
                  break;
    
                case 0xff: // Fill byte.
                  // Avoid skipping a valid marker, resetting the stream position.
                  stream.skip(-1);
                  break;
    
                case 0xd9: // EOI
                  foundEOI = true;
                  break;
    
                case 0xc0: // SOF0
                case 0xc1: // SOF1
                case 0xc2: // SOF2
                case 0xc3: // SOF3
    
                case 0xc5: // SOF5
                case 0xc6: // SOF6
                case 0xc7: // SOF7
    
                case 0xc9: // SOF9
                case 0xca: // SOF10
                case 0xcb: // SOF11
    
                case 0xcd: // SOF13
                case 0xce: // SOF14
                case 0xcf: // SOF15
    
                case 0xc4: // DHT
                case 0xcc: // DAC
    
                case 0xda: // SOS
                case 0xdb: // DQT
                case 0xdc: // DNL
                case 0xdd: // DRI
                case 0xde: // DHP
                case 0xdf: // EXP
    
                case 0xe0: // APP0
                case 0xe1: // APP1
                case 0xe2: // APP2
                case 0xe3: // APP3
                case 0xe4: // APP4
                case 0xe5: // APP5
                case 0xe6: // APP6
                case 0xe7: // APP7
                case 0xe8: // APP8
                case 0xe9: // APP9
                case 0xea: // APP10
                case 0xeb: // APP11
                case 0xec: // APP12
                case 0xed: // APP13
                case 0xee: // APP14
                case 0xef: // APP15
    
                case 0xfe: // COM
                  // The marker should be followed by the length of the segment.
                  markerLength = stream.getUint16();
                  if (markerLength > 2) {
                    // |markerLength| contains the byte length of the marker segment,
                    // including its own length (2 bytes) and excluding the marker.
                    stream.skip(markerLength - 2); // Jump to the next marker.
                  } else {
                    // The marker length is invalid, resetting the stream position.
                    stream.skip(-2);
                  }
                  break;
              }
              if (foundEOI) {
                break;
              }
            }
            length = stream.pos - startPos;
            if (b === -1) {
              warn(
                "Inline DCTDecode image stream: " +
                  "EOI marker not found, searching for /EI/ instead."
              );
              stream.skip(-length); // Reset the stream position.
              return this.findDefaultInlineStreamEnd(stream);
            }
            this.inlineStreamSkipEI(stream);
            return length;
          },
          /**
           * Find the EOD (end-of-data) marker '~>' (i.e. TILDE + GT) of the stream.
           * @returns {number} The inline stream length.
           */
          findASCII85DecodeInlineStreamEnd: function Parser_findASCII85DecodeInlineStreamEnd(
            stream
          ) {
            var TILDE = 0x7e,
              GT = 0x3e;
            var startPos = stream.pos,
              ch,
              length;
            while ((ch = stream.getByte()) !== -1) {
              if (ch === TILDE && stream.peekByte() === GT) {
                stream.skip();
                break;
              }
            }
            length = stream.pos - startPos;
            if (ch === -1) {
              warn(
                "Inline ASCII85Decode image stream: " +
                  "EOD marker not found, searching for /EI/ instead."
              );
              stream.skip(-length); // Reset the stream position.
              return this.findDefaultInlineStreamEnd(stream);
            }
            this.inlineStreamSkipEI(stream);
            return length;
          },
          /**
           * Find the EOD (end-of-data) marker '>' (i.e. GT) of the stream.
           * @returns {number} The inline stream length.
           */
          findASCIIHexDecodeInlineStreamEnd: function Parser_findASCIIHexDecodeInlineStreamEnd(
            stream
          ) {
            var GT = 0x3e;
            var startPos = stream.pos,
              ch,
              length;
            while ((ch = stream.getByte()) !== -1) {
              if (ch === GT) {
                break;
              }
            }
            length = stream.pos - startPos;
            if (ch === -1) {
              warn(
                "Inline ASCIIHexDecode image stream: " +
                  "EOD marker not found, searching for /EI/ instead."
              );
              stream.skip(-length); // Reset the stream position.
              return this.findDefaultInlineStreamEnd(stream);
            }
            this.inlineStreamSkipEI(stream);
            return length;
          },
          /**
           * Skip over the /EI/ for streams where we search for an EOD marker.
           */
          inlineStreamSkipEI: function Parser_inlineStreamSkipEI(stream) {
            var E = 0x45,
              I = 0x49;
            var state = 0,
              ch;
            while ((ch = stream.getByte()) !== -1) {
              if (state === 0) {
                state = ch === E ? 1 : 0;
              } else if (state === 1) {
                state = ch === I ? 2 : 0;
              } else if (state === 2) {
                break;
              }
            }
          },
          makeInlineImage: function Parser_makeInlineImage(cipherTransform) {
            var lexer = this.lexer;
            var stream = lexer.stream;
    
            // Parse dictionary.
            var dict = new Dict(this.xref);
            while (!isCmd(this.buf1, "ID") && !isEOF(this.buf1)) {
              if (!isName(this.buf1)) {
                error("Dictionary key must be a name object");
              }
              var key = this.buf1.name;
              this.shift();
              if (isEOF(this.buf1)) {
                break;
              }
              dict.set(key, this.getObj(cipherTransform));
            }
    
            // Extract the name of the first (i.e. the current) image filter.
            var filter = dict.get("Filter", "F"),
              filterName;
            if (isName(filter)) {
              filterName = filter.name;
            } else if (isArray(filter) && isName(filter[0])) {
              filterName = filter[0].name;
            }
    
            // Parse image stream.
            var startPos = stream.pos,
              length,
              i,
              ii;
            if (filterName === "DCTDecode" || filterName === "DCT") {
              length = this.findDCTDecodeInlineStreamEnd(stream);
            } else if (filterName === "ASCII85Decide" || filterName === "A85") {
              length = this.findASCII85DecodeInlineStreamEnd(stream);
            } else if (filterName === "ASCIIHexDecode" || filterName === "AHx") {
              length = this.findASCIIHexDecodeInlineStreamEnd(stream);
            } else {
              length = this.findDefaultInlineStreamEnd(stream);
            }
            var imageStream = stream.makeSubStream(startPos, length, dict);
    
            // Cache all images below the MAX_LENGTH_TO_CACHE threshold by their
            // adler32 checksum.
            var adler32;
            if (length < MAX_LENGTH_TO_CACHE) {
              var imageBytes = imageStream.getBytes();
              imageStream.reset();
    
              var a = 1;
              var b = 0;
              for (i = 0, ii = imageBytes.length; i < ii; ++i) {
                // No modulo required in the loop if imageBytes.length < 5552.
                a += imageBytes[i] & 0xff;
                b += a;
              }
              adler32 = (b % 65521 << 16) | a % 65521;
    
              if (this.imageCache.adler32 === adler32) {
                this.buf2 = Cmd.get("EI");
                this.shift();
    
                this.imageCache[adler32].reset();
                return this.imageCache[adler32];
              }
            }
    
            if (cipherTransform) {
              imageStream = cipherTransform.createStream(imageStream, length);
            }
    
            imageStream = this.filter(imageStream, dict, length);
            imageStream.dict = dict;
            if (adler32 !== undefined) {
              imageStream.cacheKey = "inline_" + length + "_" + adler32;
              this.imageCache[adler32] = imageStream;
            }
    
            this.buf2 = Cmd.get("EI");
            this.shift();
    
            return imageStream;
          },
          makeStream: function Parser_makeStream(dict, cipherTransform) {
            var lexer = this.lexer;
            var stream = lexer.stream;
    
            // get stream start position
            lexer.skipToNextLine();
            var pos = stream.pos - 1;
    
            // get length
            var length = dict.get("Length");
            if (!isInt(length)) {
              info("Bad " + length + " attribute in stream");
              length = 0;
            }
    
            // skip over the stream data
            stream.pos = pos + length;
            lexer.nextChar();
    
            // Shift '>>' and check whether the new object marks the end of the stream
            if (this.tryShift() && isCmd(this.buf2, "endstream")) {
              this.shift(); // 'stream'
            } else {
              // bad stream length, scanning for endstream
              stream.pos = pos;
              var SCAN_BLOCK_SIZE = 2048;
              var ENDSTREAM_SIGNATURE_LENGTH = 9;
              var ENDSTREAM_SIGNATURE = [
                0x65,
                0x6e,
                0x64,
                0x73,
                0x74,
                0x72,
                0x65,
                0x61,
                0x6d
              ];
              var skipped = 0,
                found = false,
                i,
                j;
              while (stream.pos < stream.end) {
                var scanBytes = stream.peekBytes(SCAN_BLOCK_SIZE);
                var scanLength = scanBytes.length - ENDSTREAM_SIGNATURE_LENGTH;
                if (scanLength <= 0) {
                  break;
                }
                found = false;
                for (i = 0, j = 0; i < scanLength; i++) {
                  var b = scanBytes[i];
                  if (b !== ENDSTREAM_SIGNATURE[j]) {
                    i -= j;
                    j = 0;
                  } else {
                    j++;
                    if (j >= ENDSTREAM_SIGNATURE_LENGTH) {
                      i++;
                      found = true;
                      break;
                    }
                  }
                }
                if (found) {
                  skipped += i - ENDSTREAM_SIGNATURE_LENGTH;
                  stream.pos += i - ENDSTREAM_SIGNATURE_LENGTH;
                  break;
                }
                skipped += scanLength;
                stream.pos += scanLength;
              }
              if (!found) {
                error("Missing endstream");
              }
              length = skipped;
    
              lexer.nextChar();
              this.shift();
              this.shift();
            }
            this.shift(); // 'endstream'
    
            stream = stream.makeSubStream(pos, length, dict);
            if (cipherTransform) {
              stream = cipherTransform.createStream(stream, length);
            }
            stream = this.filter(stream, dict, length);
            stream.dict = dict;
            return stream;
          },
          filter: function Parser_filter(stream, dict, length) {
            var filter = dict.get("Filter", "F");
            var params = dict.get("DecodeParms", "DP");
            if (isName(filter)) {
              return this.makeFilter(stream, filter.name, length, params);
            }
    
            var maybeLength = length;
            if (isArray(filter)) {
              var filterArray = filter;
              var paramsArray = params;
              for (var i = 0, ii = filterArray.length; i < ii; ++i) {
                filter = filterArray[i];
                if (!isName(filter)) {
                  error("Bad filter name: " + filter);
                }
    
                params = null;
                if (isArray(paramsArray) && i in paramsArray) {
                  params = paramsArray[i];
                }
                stream = this.makeFilter(stream, filter.name, maybeLength, params);
                // after the first stream the length variable is invalid
                maybeLength = null;
              }
            }
            return stream;
          },
          makeFilter: function Parser_makeFilter(
            stream,
            name,
            maybeLength,
            params
          ) {
            if (stream.dict.get("Length") === 0 && !maybeLength) {
              warn('Empty "' + name + '" stream.');
              return new NullStream(stream);
            }
            try {
              if (params && this.xref) {
                params = this.xref.fetchIfRef(params);
              }
              var xrefStreamStats = this.xref.stats.streamTypes;
              if (name === "FlateDecode" || name === "Fl") {
                xrefStreamStats[StreamType.FLATE] = true;
                if (params) {
                  return new PredictorStream(
                    new FlateStream(stream, maybeLength),
                    maybeLength,
                    params
                  );
                }
                return new FlateStream(stream, maybeLength);
              }
              if (name === "LZWDecode" || name === "LZW") {
                xrefStreamStats[StreamType.LZW] = true;
                var earlyChange = 1;
                if (params) {
                  if (params.has("EarlyChange")) {
                    earlyChange = params.get("EarlyChange");
                  }
                  return new PredictorStream(
                    new LZWStream(stream, maybeLength, earlyChange),
                    maybeLength,
                    params
                  );
                }
                return new LZWStream(stream, maybeLength, earlyChange);
              }
              if (name === "DCTDecode" || name === "DCT") {
                xrefStreamStats[StreamType.DCT] = true;
                return new JpegStream(stream, maybeLength, stream.dict, this.xref);
              }
              if (name === "JPXDecode" || name === "JPX") {
                xrefStreamStats[StreamType.JPX] = true;
                return new JpxStream(stream, maybeLength, stream.dict);
              }
              if (name === "ASCII85Decode" || name === "A85") {
                xrefStreamStats[StreamType.A85] = true;
                return new Ascii85Stream(stream, maybeLength);
              }
              if (name === "ASCIIHexDecode" || name === "AHx") {
                xrefStreamStats[StreamType.AHX] = true;
                return new AsciiHexStream(stream, maybeLength);
              }
              if (name === "CCITTFaxDecode" || name === "CCF") {
                xrefStreamStats[StreamType.CCF] = true;
                return new CCITTFaxStream(stream, maybeLength, params);
              }
              if (name === "RunLengthDecode" || name === "RL") {
                xrefStreamStats[StreamType.RL] = true;
                return new RunLengthStream(stream, maybeLength);
              }
              if (name === "JBIG2Decode") {
                xrefStreamStats[StreamType.JBIG] = true;
                return new Jbig2Stream(stream, maybeLength, stream.dict);
              }
              warn('filter "' + name + '" not supported yet');
              return stream;
            } catch (ex) {
              if (ex instanceof MissingDataException) {
                throw ex;
              }
              warn('Invalid stream: "' + ex + '"');
              return new NullStream(stream);
            }
          }
        };
    
        return Parser;
      })();
    
      var Lexer = (function LexerClosure() {
        function Lexer(stream, knownCommands) {
          this.stream = stream;
          this.nextChar();
    
          // While lexing, we build up many strings one char at a time. Using += for
          // this can result in lots of garbage strings. It's better to build an
          // array of single-char strings and then join() them together at the end.
          // And reusing a single array (i.e. |this.strBuf|) over and over for this
          // purpose uses less memory than using a new array for each string.
          this.strBuf = [];
    
          // The PDFs might have "glued" commands with other commands, operands or
          // literals, e.g. "q1". The knownCommands is a dictionary of the valid
          // commands and their prefixes. The prefixes are built the following way:
          // if there a command that is a prefix of the other valid command or
          // literal (e.g. 'f' and 'false') the following prefixes must be included,
          // 'fa', 'fal', 'fals'. The prefixes are not needed, if the command has no
          // other commands or literals as a prefix. The knowCommands is optional.
          this.knownCommands = knownCommands;
        }
    
        Lexer.isSpace = function Lexer_isSpace(ch) {
          // Space is one of the following characters: SPACE, TAB, CR or LF.
          return ch === 0x20 || ch === 0x09 || ch === 0x0d || ch === 0x0a;
        };
    
        // A '1' in this array means the character is white space. A '1' or
        // '2' means the character ends a name or command.
        var specialChars = [
          1,
          0,
          0,
          0,
          0,
          0,
          0,
          0,
          0,
          1,
          1,
          0,
          1,
          1,
          0,
          0, // 0x
          0,
          0,
          0,
          0,
          0,
          0,
          0,
          0,
          0,
          0,
          0,
          0,
          0,
          0,
          0,
          0, // 1x
          1,
          0,
          0,
          0,
          0,
          2,
          0,
          0,
          2,
          2,
          0,
          0,
          0,
          0,
          0,
          2, // 2x
          0,
          0,
          0,
          0,
          0,
          0,
          0,
          0,
          0,
          0,
          0,
          0,
          2,
          0,
          2,
          0, // 3x
          0,
          0,
          0,
          0,
          0,
          0,
          0,
          0,
          0,
          0,
          0,
          0,
          0,
          0,
          0,
          0, // 4x
          0,
          0,
          0,
          0,
          0,
          0,
          0,
          0,
          0,
          0,
          0,
          2,
          0,
          2,
          0,
          0, // 5x
          0,
          0,
          0,
          0,
          0,
          0,
          0,
          0,
          0,
          0,
          0,
          0,
          0,
          0,
          0,
          0, // 6x
          0,
          0,
          0,
          0,
          0,
          0,
          0,
          0,
          0,
          0,
          0,
          2,
          0,
          2,
          0,
          0, // 7x
          0,
          0,
          0,
          0,
          0,
          0,
          0,
          0,
          0,
          0,
          0,
          0,
          0,
          0,
          0,
          0, // 8x
          0,
          0,
          0,
          0,
          0,
          0,
          0,
          0,
          0,
          0,
          0,
          0,
          0,
          0,
          0,
          0, // 9x
          0,
          0,
          0,
          0,
          0,
          0,
          0,
          0,
          0,
          0,
          0,
          0,
          0,
          0,
          0,
          0, // ax
          0,
          0,
          0,
          0,
          0,
          0,
          0,
          0,
          0,
          0,
          0,
          0,
          0,
          0,
          0,
          0, // bx
          0,
          0,
          0,
          0,
          0,
          0,
          0,
          0,
          0,
          0,
          0,
          0,
          0,
          0,
          0,
          0, // cx
          0,
          0,
          0,
          0,
          0,
          0,
          0,
          0,
          0,
          0,
          0,
          0,
          0,
          0,
          0,
          0, // dx
          0,
          0,
          0,
          0,
          0,
          0,
          0,
          0,
          0,
          0,
          0,
          0,
          0,
          0,
          0,
          0, // ex
          0,
          0,
          0,
          0,
          0,
          0,
          0,
          0,
          0,
          0,
          0,
          0,
          0,
          0,
          0,
          0 // fx
        ];
    
        function toHexDigit(ch) {
          if (ch >= 0x30 && ch <= 0x39) {
            // '0'-'9'
            return ch & 0x0f;
          }
          if ((ch >= 0x41 && ch <= 0x46) || (ch >= 0x61 && ch <= 0x66)) {
            // 'A'-'F', 'a'-'f'
            return (ch & 0x0f) + 9;
          }
          return -1;
        }
    
        Lexer.prototype = {
          nextChar: function Lexer_nextChar() {
            return (this.currentChar = this.stream.getByte());
          },
          peekChar: function Lexer_peekChar() {
            return this.stream.peekByte();
          },
          getNumber: function Lexer_getNumber() {
            var ch = this.currentChar;
            var eNotation = false;
            var divideBy = 0; // different from 0 if it's a floating point value
            var sign = 1;
    
            if (ch === 0x2d) {
              // '-'
              sign = -1;
              ch = this.nextChar();
    
              if (ch === 0x2d) {
                // '-'
                // Ignore double negative (this is consistent with Adobe Reader).
                ch = this.nextChar();
              }
            } else if (ch === 0x2b) {
              // '+'
              ch = this.nextChar();
            }
            if (ch === 0x2e) {
              // '.'
              divideBy = 10;
              ch = this.nextChar();
            }
            if (ch < 0x30 || ch > 0x39) {
              // '0' - '9'
              error("Invalid number: " + String.fromCharCode(ch));
              return 0;
            }
    
            var baseValue = ch - 0x30; // '0'
            var powerValue = 0;
            var powerValueSign = 1;
    
            while ((ch = this.nextChar()) >= 0) {
              if (0x30 <= ch && ch <= 0x39) {
                // '0' - '9'
                var currentDigit = ch - 0x30; // '0'
                if (eNotation) {
                  // We are after an 'e' or 'E'
                  powerValue = powerValue * 10 + currentDigit;
                } else {
                  if (divideBy !== 0) {
                    // We are after a point
                    divideBy *= 10;
                  }
                  baseValue = baseValue * 10 + currentDigit;
                }
              } else if (ch === 0x2e) {
                // '.'
                if (divideBy === 0) {
                  divideBy = 1;
                } else {
                  // A number can have only one '.'
                  break;
                }
              } else if (ch === 0x2d) {
                // '-'
                // ignore minus signs in the middle of numbers to match
                // Adobe's behavior
                warn("Badly formated number");
              } else if (ch === 0x45 || ch === 0x65) {
                // 'E', 'e'
                // 'E' can be either a scientific notation or the beginning of a new
                // operator
                ch = this.peekChar();
                if (ch === 0x2b || ch === 0x2d) {
                  // '+', '-'
                  powerValueSign = ch === 0x2d ? -1 : 1;
                  this.nextChar(); // Consume the sign character
                } else if (ch < 0x30 || ch > 0x39) {
                  // '0' - '9'
                  // The 'E' must be the beginning of a new operator
                  break;
                }
                eNotation = true;
              } else {
                // the last character doesn't belong to us
                break;
              }
            }
    
            if (divideBy !== 0) {
              baseValue /= divideBy;
            }
            if (eNotation) {
              baseValue *= Math.pow(10, powerValueSign * powerValue);
            }
            return sign * baseValue;
          },
          getString: function Lexer_getString() {
            var numParen = 1;
            var done = false;
            var strBuf = this.strBuf;
            strBuf.length = 0;
    
            var ch = this.nextChar();
            while (true) {
              var charBuffered = false;
              switch (ch | 0) {
                case -1:
                  warn("Unterminated string");
                  done = true;
                  break;
                case 0x28: // '('
                  ++numParen;
                  strBuf.push("(");
                  break;
                case 0x29: // ')'
                  if (--numParen === 0) {
                    this.nextChar(); // consume strings ')'
                    done = true;
                  } else {
                    strBuf.push(")");
                  }
                  break;
                case 0x5c: // '\\'
                  ch = this.nextChar();
                  switch (ch) {
                    case -1:
                      warn("Unterminated string");
                      done = true;
                      break;
                    case 0x6e: // 'n'
                      strBuf.push("\n");
                      break;
                    case 0x72: // 'r'
                      strBuf.push("\r");
                      break;
                    case 0x74: // 't'
                      strBuf.push("\t");
                      break;
                    case 0x62: // 'b'
                      strBuf.push("\b");
                      break;
                    case 0x66: // 'f'
                      strBuf.push("\f");
                      break;
                    case 0x5c: // '\'
                    case 0x28: // '('
                    case 0x29: // ')'
                      strBuf.push(String.fromCharCode(ch));
                      break;
                    case 0x30:
                    case 0x31:
                    case 0x32:
                    case 0x33: // '0'-'3'
                    case 0x34:
                    case 0x35:
                    case 0x36:
                    case 0x37: // '4'-'7'
                      var x = ch & 0x0f;
                      ch = this.nextChar();
                      charBuffered = true;
                      if (ch >= 0x30 && ch <= 0x37) {
                        // '0'-'7'
                        x = (x << 3) + (ch & 0x0f);
                        ch = this.nextChar();
                        if (ch >= 0x30 && ch <= 0x37) {
                          // '0'-'7'
                          charBuffered = false;
                          x = (x << 3) + (ch & 0x0f);
                        }
                      }
                      strBuf.push(String.fromCharCode(x));
                      break;
                    case 0x0d: // CR
                      if (this.peekChar() === 0x0a) {
                        // LF
                        this.nextChar();
                      }
                      break;
                    case 0x0a: // LF
                      break;
                    default:
                      strBuf.push(String.fromCharCode(ch));
                      break;
                  }
                  break;
                default:
                  strBuf.push(String.fromCharCode(ch));
                  break;
              }
              if (done) {
                break;
              }
              if (!charBuffered) {
                ch = this.nextChar();
              }
            }
            return strBuf.join("");
          },
          getName: function Lexer_getName() {
            var ch, previousCh;
            var strBuf = this.strBuf;
            strBuf.length = 0;
            while ((ch = this.nextChar()) >= 0 && !specialChars[ch]) {
              if (ch === 0x23) {
                // '#'
                ch = this.nextChar();
                if (specialChars[ch]) {
                  warn(
                    "Lexer_getName: " +
                      "NUMBER SIGN (#) should be followed by a hexadecimal number."
                  );
                  strBuf.push("#");
                  break;
                }
                var x = toHexDigit(ch);
                if (x !== -1) {
                  previousCh = ch;
                  ch = this.nextChar();
                  var x2 = toHexDigit(ch);
                  if (x2 === -1) {
                    warn(
                      "Lexer_getName: Illegal digit (" +
                        String.fromCharCode(ch) +
                        ") in hexadecimal number."
                    );
                    strBuf.push("#", String.fromCharCode(previousCh));
                    if (specialChars[ch]) {
                      break;