pdfjs-dist
Version:
Generic build of Mozilla's PDF.js library.
1,384 lines (1,350 loc) • 61.3 kB
JavaScript
/**
* @licstart The following is the entire license notice for the
* Javascript code in this page
*
* Copyright 2018 Mozilla Foundation
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
* @licend The above is the entire license notice for the
* Javascript code in this page
*/
'use strict';
Object.defineProperty(exports, "__esModule", {
value: true
});
exports.FileSpec = exports.XRef = exports.ObjectLoader = exports.Catalog = undefined;
var _regenerator = require('babel-runtime/regenerator');
var _regenerator2 = _interopRequireDefault(_regenerator);
var _slicedToArray = function () { function sliceIterator(arr, i) { var _arr = []; var _n = true; var _d = false; var _e = undefined; try { for (var _i = arr[Symbol.iterator](), _s; !(_n = (_s = _i.next()).done); _n = true) { _arr.push(_s.value); if (i && _arr.length === i) break; } } catch (err) { _d = true; _e = err; } finally { try { if (!_n && _i["return"]) _i["return"](); } finally { if (_d) throw _e; } } return _arr; } return function (arr, i) { if (Array.isArray(arr)) { return arr; } else if (Symbol.iterator in Object(arr)) { return sliceIterator(arr, i); } else { throw new TypeError("Invalid attempt to destructure non-iterable instance"); } }; }();
var _typeof = typeof Symbol === "function" && typeof Symbol.iterator === "symbol" ? function (obj) { return typeof obj; } : function (obj) { return obj && typeof Symbol === "function" && obj.constructor === Symbol && obj !== Symbol.prototype ? "symbol" : typeof obj; };
var _createClass = function () { function defineProperties(target, props) { for (var i = 0; i < props.length; i++) { var descriptor = props[i]; descriptor.enumerable = descriptor.enumerable || false; descriptor.configurable = true; if ("value" in descriptor) descriptor.writable = true; Object.defineProperty(target, descriptor.key, descriptor); } } return function (Constructor, protoProps, staticProps) { if (protoProps) defineProperties(Constructor.prototype, protoProps); if (staticProps) defineProperties(Constructor, staticProps); return Constructor; }; }();
var _util = require('../shared/util');
var _primitives = require('./primitives');
var _parser = require('./parser');
var _chunked_stream = require('./chunked_stream');
var _crypto = require('./crypto');
var _colorspace = require('./colorspace');
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
function _possibleConstructorReturn(self, call) { if (!self) { throw new ReferenceError("this hasn't been initialised - super() hasn't been called"); } return call && (typeof call === "object" || typeof call === "function") ? call : self; }
function _inherits(subClass, superClass) { if (typeof superClass !== "function" && superClass !== null) { throw new TypeError("Super expression must either be null or a function, not " + typeof superClass); } subClass.prototype = Object.create(superClass && superClass.prototype, { constructor: { value: subClass, enumerable: false, writable: true, configurable: true } }); if (superClass) Object.setPrototypeOf ? Object.setPrototypeOf(subClass, superClass) : subClass.__proto__ = superClass; }
function _asyncToGenerator(fn) { return function () { var gen = fn.apply(this, arguments); return new Promise(function (resolve, reject) { function step(key, arg) { try { var info = gen[key](arg); var value = info.value; } catch (error) { reject(error); return; } if (info.done) { resolve(value); } else { return Promise.resolve(value).then(function (value) { step("next", value); }, function (err) { step("throw", err); }); } } return step("next"); }); }; }
function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } }
function fetchDestination(dest) {
return (0, _primitives.isDict)(dest) ? dest.get('D') : dest;
}
var Catalog = function () {
function Catalog(pdfManager, xref) {
_classCallCheck(this, Catalog);
this.pdfManager = pdfManager;
this.xref = xref;
this.catDict = xref.getCatalogObj();
if (!(0, _primitives.isDict)(this.catDict)) {
throw new _util.FormatError('Catalog object is not a dictionary.');
}
this.fontCache = new _primitives.RefSetCache();
this.builtInCMapCache = new Map();
this.pageKidsCountCache = new _primitives.RefSetCache();
}
_createClass(Catalog, [{
key: '_readDocumentOutline',
value: function _readDocumentOutline() {
var obj = this.catDict.get('Outlines');
if (!(0, _primitives.isDict)(obj)) {
return null;
}
obj = obj.getRaw('First');
if (!(0, _primitives.isRef)(obj)) {
return null;
}
var root = { items: [] };
var queue = [{
obj: obj,
parent: root
}];
var processed = new _primitives.RefSet();
processed.put(obj);
var xref = this.xref,
blackColor = new Uint8ClampedArray(3);
while (queue.length > 0) {
var i = queue.shift();
var outlineDict = xref.fetchIfRef(i.obj);
if (outlineDict === null) {
continue;
}
if (!outlineDict.has('Title')) {
throw new _util.FormatError('Invalid outline item encountered.');
}
var data = {
url: null,
dest: null
};
Catalog.parseDestDictionary({
destDict: outlineDict,
resultObj: data,
docBaseUrl: this.pdfManager.docBaseUrl
});
var title = outlineDict.get('Title');
var flags = outlineDict.get('F') || 0;
var color = outlineDict.getArray('C');
var rgbColor = blackColor;
if (Array.isArray(color) && color.length === 3 && (color[0] !== 0 || color[1] !== 0 || color[2] !== 0)) {
rgbColor = _colorspace.ColorSpace.singletons.rgb.getRgb(color, 0);
}
var outlineItem = {
dest: data.dest,
url: data.url,
unsafeUrl: data.unsafeUrl,
newWindow: data.newWindow,
title: (0, _util.stringToPDFString)(title),
color: rgbColor,
count: outlineDict.get('Count'),
bold: !!(flags & 2),
italic: !!(flags & 1),
items: []
};
i.parent.items.push(outlineItem);
obj = outlineDict.getRaw('First');
if ((0, _primitives.isRef)(obj) && !processed.has(obj)) {
queue.push({
obj: obj,
parent: outlineItem
});
processed.put(obj);
}
obj = outlineDict.getRaw('Next');
if ((0, _primitives.isRef)(obj) && !processed.has(obj)) {
queue.push({
obj: obj,
parent: i.parent
});
processed.put(obj);
}
}
return root.items.length > 0 ? root.items : null;
}
}, {
key: '_readPermissions',
value: function _readPermissions() {
var encrypt = this.xref.trailer.get('Encrypt');
if (!(0, _primitives.isDict)(encrypt)) {
return null;
}
var flags = encrypt.get('P');
if (!(0, _util.isNum)(flags)) {
return null;
}
flags += Math.pow(2, 32);
var permissions = [];
for (var key in _util.PermissionFlag) {
var value = _util.PermissionFlag[key];
if (flags & value) {
permissions.push(value);
}
}
return permissions;
}
}, {
key: 'getDestination',
value: function getDestination(destinationId) {
var obj = this._readDests();
if (obj instanceof NameTree || obj instanceof _primitives.Dict) {
return fetchDestination(obj.get(destinationId) || null);
}
return null;
}
}, {
key: '_readDests',
value: function _readDests() {
var obj = this.catDict.get('Names');
if (obj && obj.has('Dests')) {
return new NameTree(obj.getRaw('Dests'), this.xref);
} else if (this.catDict.has('Dests')) {
return this.catDict.get('Dests');
}
}
}, {
key: '_readPageLabels',
value: function _readPageLabels() {
var obj = this.catDict.getRaw('PageLabels');
if (!obj) {
return null;
}
var pageLabels = new Array(this.numPages);
var style = null,
prefix = '';
var numberTree = new NumberTree(obj, this.xref);
var nums = numberTree.getAll();
var currentLabel = '',
currentIndex = 1;
for (var i = 0, ii = this.numPages; i < ii; i++) {
if (i in nums) {
var labelDict = nums[i];
if (!(0, _primitives.isDict)(labelDict)) {
throw new _util.FormatError('PageLabel is not a dictionary.');
}
if (labelDict.has('Type') && !(0, _primitives.isName)(labelDict.get('Type'), 'PageLabel')) {
throw new _util.FormatError('Invalid type in PageLabel dictionary.');
}
if (labelDict.has('S')) {
var s = labelDict.get('S');
if (!(0, _primitives.isName)(s)) {
throw new _util.FormatError('Invalid style in PageLabel dictionary.');
}
style = s.name;
} else {
style = null;
}
if (labelDict.has('P')) {
var p = labelDict.get('P');
if (!(0, _util.isString)(p)) {
throw new _util.FormatError('Invalid prefix in PageLabel dictionary.');
}
prefix = (0, _util.stringToPDFString)(p);
} else {
prefix = '';
}
if (labelDict.has('St')) {
var st = labelDict.get('St');
if (!(Number.isInteger(st) && st >= 1)) {
throw new _util.FormatError('Invalid start in PageLabel dictionary.');
}
currentIndex = st;
} else {
currentIndex = 1;
}
}
switch (style) {
case 'D':
currentLabel = currentIndex;
break;
case 'R':
case 'r':
currentLabel = (0, _util.toRomanNumerals)(currentIndex, style === 'r');
break;
case 'A':
case 'a':
var LIMIT = 26;
var A_UPPER_CASE = 0x41,
A_LOWER_CASE = 0x61;
var baseCharCode = style === 'a' ? A_LOWER_CASE : A_UPPER_CASE;
var letterIndex = currentIndex - 1;
var character = String.fromCharCode(baseCharCode + letterIndex % LIMIT);
var charBuf = [];
for (var j = 0, jj = letterIndex / LIMIT | 0; j <= jj; j++) {
charBuf.push(character);
}
currentLabel = charBuf.join('');
break;
default:
if (style) {
throw new _util.FormatError('Invalid style "' + style + '" in PageLabel dictionary.');
}
currentLabel = '';
}
pageLabels[i] = prefix + currentLabel;
currentIndex++;
}
return pageLabels;
}
}, {
key: 'cleanup',
value: function cleanup() {
var _this = this;
this.pageKidsCountCache.clear();
var promises = [];
this.fontCache.forEach(function (promise) {
promises.push(promise);
});
return Promise.all(promises).then(function (translatedFonts) {
for (var i = 0, ii = translatedFonts.length; i < ii; i++) {
var font = translatedFonts[i].dict;
delete font.translated;
}
_this.fontCache.clear();
_this.builtInCMapCache.clear();
});
}
}, {
key: 'getPageDict',
value: function getPageDict(pageIndex) {
var capability = (0, _util.createPromiseCapability)();
var nodesToVisit = [this.catDict.getRaw('Pages')];
var xref = this.xref,
pageKidsCountCache = this.pageKidsCountCache;
var count = void 0,
currentPageIndex = 0;
function next() {
var _loop = function _loop() {
var currentNode = nodesToVisit.pop();
if ((0, _primitives.isRef)(currentNode)) {
count = pageKidsCountCache.get(currentNode);
if (count > 0 && currentPageIndex + count < pageIndex) {
currentPageIndex += count;
return 'continue';
}
xref.fetchAsync(currentNode).then(function (obj) {
if ((0, _primitives.isDict)(obj, 'Page') || (0, _primitives.isDict)(obj) && !obj.has('Kids')) {
if (pageIndex === currentPageIndex) {
if (currentNode && !pageKidsCountCache.has(currentNode)) {
pageKidsCountCache.put(currentNode, 1);
}
capability.resolve([obj, currentNode]);
} else {
currentPageIndex++;
next();
}
return;
}
nodesToVisit.push(obj);
next();
}, capability.reject);
return {
v: void 0
};
}
if (!(0, _primitives.isDict)(currentNode)) {
capability.reject(new _util.FormatError('Page dictionary kid reference points to wrong type of object.'));
return {
v: void 0
};
}
count = currentNode.get('Count');
if (Number.isInteger(count) && count >= 0) {
var objId = currentNode.objId;
if (objId && !pageKidsCountCache.has(objId)) {
pageKidsCountCache.put(objId, count);
}
if (currentPageIndex + count <= pageIndex) {
currentPageIndex += count;
return 'continue';
}
}
var kids = currentNode.get('Kids');
if (!Array.isArray(kids)) {
if ((0, _primitives.isName)(currentNode.get('Type'), 'Page') || !currentNode.has('Type') && currentNode.has('Contents')) {
if (currentPageIndex === pageIndex) {
capability.resolve([currentNode, null]);
return {
v: void 0
};
}
currentPageIndex++;
return 'continue';
}
capability.reject(new _util.FormatError('Page dictionary kids object is not an array.'));
return {
v: void 0
};
}
for (var last = kids.length - 1; last >= 0; last--) {
nodesToVisit.push(kids[last]);
}
};
while (nodesToVisit.length) {
var _ret = _loop();
switch (_ret) {
case 'continue':
continue;
default:
if ((typeof _ret === 'undefined' ? 'undefined' : _typeof(_ret)) === "object") return _ret.v;
}
}
capability.reject(new Error('Page index ' + pageIndex + ' not found.'));
}
next();
return capability.promise;
}
}, {
key: 'getPageIndex',
value: function getPageIndex(pageRef) {
var xref = this.xref;
function pagesBeforeRef(kidRef) {
var total = 0,
parentRef = void 0;
return xref.fetchAsync(kidRef).then(function (node) {
if ((0, _primitives.isRefsEqual)(kidRef, pageRef) && !(0, _primitives.isDict)(node, 'Page') && !((0, _primitives.isDict)(node) && !node.has('Type') && node.has('Contents'))) {
throw new _util.FormatError('The reference does not point to a /Page dictionary.');
}
if (!node) {
return null;
}
if (!(0, _primitives.isDict)(node)) {
throw new _util.FormatError('Node must be a dictionary.');
}
parentRef = node.getRaw('Parent');
return node.getAsync('Parent');
}).then(function (parent) {
if (!parent) {
return null;
}
if (!(0, _primitives.isDict)(parent)) {
throw new _util.FormatError('Parent must be a dictionary.');
}
return parent.getAsync('Kids');
}).then(function (kids) {
if (!kids) {
return null;
}
var kidPromises = [];
var found = false;
for (var i = 0, ii = kids.length; i < ii; i++) {
var kid = kids[i];
if (!(0, _primitives.isRef)(kid)) {
throw new _util.FormatError('Kid must be a reference.');
}
if ((0, _primitives.isRefsEqual)(kid, kidRef)) {
found = true;
break;
}
kidPromises.push(xref.fetchAsync(kid).then(function (kid) {
if (!(0, _primitives.isDict)(kid)) {
throw new _util.FormatError('Kid node must be a dictionary.');
}
if (kid.has('Count')) {
total += kid.get('Count');
} else {
total++;
}
}));
}
if (!found) {
throw new _util.FormatError('Kid reference not found in parent\'s kids.');
}
return Promise.all(kidPromises).then(function () {
return [total, parentRef];
});
});
}
var total = 0;
function next(ref) {
return pagesBeforeRef(ref).then(function (args) {
if (!args) {
return total;
}
var _args = _slicedToArray(args, 2),
count = _args[0],
parentRef = _args[1];
total += count;
return next(parentRef);
});
}
return next(pageRef);
}
}, {
key: 'metadata',
get: function get() {
var streamRef = this.catDict.getRaw('Metadata');
if (!(0, _primitives.isRef)(streamRef)) {
return (0, _util.shadow)(this, 'metadata', null);
}
var suppressEncryption = !(this.xref.encrypt && this.xref.encrypt.encryptMetadata);
var stream = this.xref.fetch(streamRef, suppressEncryption);
var metadata = void 0;
if (stream && (0, _primitives.isDict)(stream.dict)) {
var type = stream.dict.get('Type');
var subtype = stream.dict.get('Subtype');
if ((0, _primitives.isName)(type, 'Metadata') && (0, _primitives.isName)(subtype, 'XML')) {
try {
metadata = (0, _util.stringToUTF8String)((0, _util.bytesToString)(stream.getBytes()));
} catch (e) {
if (e instanceof _util.MissingDataException) {
throw e;
}
(0, _util.info)('Skipping invalid metadata.');
}
}
}
return (0, _util.shadow)(this, 'metadata', metadata);
}
}, {
key: 'toplevelPagesDict',
get: function get() {
var pagesObj = this.catDict.get('Pages');
if (!(0, _primitives.isDict)(pagesObj)) {
throw new _util.FormatError('Invalid top-level pages dictionary.');
}
return (0, _util.shadow)(this, 'toplevelPagesDict', pagesObj);
}
}, {
key: 'documentOutline',
get: function get() {
var obj = null;
try {
obj = this._readDocumentOutline();
} catch (ex) {
if (ex instanceof _util.MissingDataException) {
throw ex;
}
(0, _util.warn)('Unable to read document outline.');
}
return (0, _util.shadow)(this, 'documentOutline', obj);
}
}, {
key: 'permissions',
get: function get() {
var permissions = null;
try {
permissions = this._readPermissions();
} catch (ex) {
if (ex instanceof _util.MissingDataException) {
throw ex;
}
(0, _util.warn)('Unable to read permissions.');
}
return (0, _util.shadow)(this, 'permissions', permissions);
}
}, {
key: 'numPages',
get: function get() {
var obj = this.toplevelPagesDict.get('Count');
if (!Number.isInteger(obj)) {
throw new _util.FormatError('Page count in top-level pages dictionary is not an integer.');
}
return (0, _util.shadow)(this, 'numPages', obj);
}
}, {
key: 'destinations',
get: function get() {
var obj = this._readDests(),
dests = Object.create(null);
if (obj instanceof NameTree) {
var names = obj.getAll();
for (var name in names) {
dests[name] = fetchDestination(names[name]);
}
} else if (obj instanceof _primitives.Dict) {
obj.forEach(function (key, value) {
if (value) {
dests[key] = fetchDestination(value);
}
});
}
return (0, _util.shadow)(this, 'destinations', dests);
}
}, {
key: 'pageLabels',
get: function get() {
var obj = null;
try {
obj = this._readPageLabels();
} catch (ex) {
if (ex instanceof _util.MissingDataException) {
throw ex;
}
(0, _util.warn)('Unable to read page labels.');
}
return (0, _util.shadow)(this, 'pageLabels', obj);
}
}, {
key: 'pageMode',
get: function get() {
var obj = this.catDict.get('PageMode');
var pageMode = 'UseNone';
if ((0, _primitives.isName)(obj)) {
switch (obj.name) {
case 'UseNone':
case 'UseOutlines':
case 'UseThumbs':
case 'FullScreen':
case 'UseOC':
case 'UseAttachments':
pageMode = obj.name;
}
}
return (0, _util.shadow)(this, 'pageMode', pageMode);
}
}, {
key: 'attachments',
get: function get() {
var obj = this.catDict.get('Names');
var attachments = null;
if (obj && obj.has('EmbeddedFiles')) {
var nameTree = new NameTree(obj.getRaw('EmbeddedFiles'), this.xref);
var names = nameTree.getAll();
for (var name in names) {
var fs = new FileSpec(names[name], this.xref);
if (!attachments) {
attachments = Object.create(null);
}
attachments[(0, _util.stringToPDFString)(name)] = fs.serializable;
}
}
return (0, _util.shadow)(this, 'attachments', attachments);
}
}, {
key: 'javaScript',
get: function get() {
var obj = this.catDict.get('Names');
var javaScript = null;
function appendIfJavaScriptDict(jsDict) {
var type = jsDict.get('S');
if (!(0, _primitives.isName)(type, 'JavaScript')) {
return;
}
var js = jsDict.get('JS');
if ((0, _primitives.isStream)(js)) {
js = (0, _util.bytesToString)(js.getBytes());
} else if (!(0, _util.isString)(js)) {
return;
}
if (!javaScript) {
javaScript = [];
}
javaScript.push((0, _util.stringToPDFString)(js));
}
if (obj && obj.has('JavaScript')) {
var nameTree = new NameTree(obj.getRaw('JavaScript'), this.xref);
var names = nameTree.getAll();
for (var name in names) {
var jsDict = names[name];
if ((0, _primitives.isDict)(jsDict)) {
appendIfJavaScriptDict(jsDict);
}
}
}
var openActionDict = this.catDict.get('OpenAction');
if ((0, _primitives.isDict)(openActionDict, 'Action')) {
var actionType = openActionDict.get('S');
if ((0, _primitives.isName)(actionType, 'Named')) {
var action = openActionDict.get('N');
if ((0, _primitives.isName)(action, 'Print')) {
if (!javaScript) {
javaScript = [];
}
javaScript.push('print({});');
}
} else {
appendIfJavaScriptDict(openActionDict);
}
}
return (0, _util.shadow)(this, 'javaScript', javaScript);
}
}], [{
key: 'parseDestDictionary',
value: function parseDestDictionary(params) {
function addDefaultProtocolToUrl(url) {
if (url.indexOf('www.') === 0) {
return 'http://' + url;
}
return url;
}
function tryConvertUrlEncoding(url) {
try {
return (0, _util.stringToUTF8String)(url);
} catch (e) {
return url;
}
}
var destDict = params.destDict;
if (!(0, _primitives.isDict)(destDict)) {
(0, _util.warn)('parseDestDictionary: `destDict` must be a dictionary.');
return;
}
var resultObj = params.resultObj;
if ((typeof resultObj === 'undefined' ? 'undefined' : _typeof(resultObj)) !== 'object') {
(0, _util.warn)('parseDestDictionary: `resultObj` must be an object.');
return;
}
var docBaseUrl = params.docBaseUrl || null;
var action = destDict.get('A'),
url = void 0,
dest = void 0;
if (!(0, _primitives.isDict)(action) && destDict.has('Dest')) {
action = destDict.get('Dest');
}
if ((0, _primitives.isDict)(action)) {
var actionType = action.get('S');
if (!(0, _primitives.isName)(actionType)) {
(0, _util.warn)('parseDestDictionary: Invalid type in Action dictionary.');
return;
}
var actionName = actionType.name;
switch (actionName) {
case 'URI':
url = action.get('URI');
if ((0, _primitives.isName)(url)) {
url = '/' + url.name;
} else if ((0, _util.isString)(url)) {
url = addDefaultProtocolToUrl(url);
}
break;
case 'GoTo':
dest = action.get('D');
break;
case 'Launch':
case 'GoToR':
var urlDict = action.get('F');
if ((0, _primitives.isDict)(urlDict)) {
url = urlDict.get('F') || null;
} else if ((0, _util.isString)(urlDict)) {
url = urlDict;
}
var remoteDest = action.get('D');
if (remoteDest) {
if ((0, _primitives.isName)(remoteDest)) {
remoteDest = remoteDest.name;
}
if ((0, _util.isString)(url)) {
var baseUrl = url.split('#')[0];
if ((0, _util.isString)(remoteDest)) {
url = baseUrl + '#' + remoteDest;
} else if (Array.isArray(remoteDest)) {
url = baseUrl + '#' + JSON.stringify(remoteDest);
}
}
}
var newWindow = action.get('NewWindow');
if ((0, _util.isBool)(newWindow)) {
resultObj.newWindow = newWindow;
}
break;
case 'Named':
var namedAction = action.get('N');
if ((0, _primitives.isName)(namedAction)) {
resultObj.action = namedAction.name;
}
break;
case 'JavaScript':
var jsAction = action.get('JS');
var js = void 0;
if ((0, _primitives.isStream)(jsAction)) {
js = (0, _util.bytesToString)(jsAction.getBytes());
} else if ((0, _util.isString)(jsAction)) {
js = jsAction;
}
if (js) {
var URL_OPEN_METHODS = ['app.launchURL', 'window.open'];
var regex = new RegExp('^\\s*(' + URL_OPEN_METHODS.join('|').split('.').join('\\.') + ')\\((?:\'|\")([^\'\"]*)(?:\'|\")(?:,\\s*(\\w+)\\)|\\))', 'i');
var jsUrl = regex.exec((0, _util.stringToPDFString)(js));
if (jsUrl && jsUrl[2]) {
url = jsUrl[2];
if (jsUrl[3] === 'true' && jsUrl[1] === 'app.launchURL') {
resultObj.newWindow = true;
}
break;
}
}
default:
(0, _util.warn)('parseDestDictionary: unsupported action type "' + actionName + '".');
break;
}
} else if (destDict.has('Dest')) {
dest = destDict.get('Dest');
}
if ((0, _util.isString)(url)) {
url = tryConvertUrlEncoding(url);
var absoluteUrl = (0, _util.createValidAbsoluteUrl)(url, docBaseUrl);
if (absoluteUrl) {
resultObj.url = absoluteUrl.href;
}
resultObj.unsafeUrl = url;
}
if (dest) {
if ((0, _primitives.isName)(dest)) {
dest = dest.name;
}
if ((0, _util.isString)(dest) || Array.isArray(dest)) {
resultObj.dest = dest;
}
}
}
}]);
return Catalog;
}();
var XRef = function XRefClosure() {
function XRef(stream, pdfManager) {
this.stream = stream;
this.pdfManager = pdfManager;
this.entries = [];
this.xrefstms = Object.create(null);
this.cache = [];
this.stats = {
streamTypes: [],
fontTypes: []
};
}
XRef.prototype = {
setStartXRef: function XRef_setStartXRef(startXRef) {
this.startXRefQueue = [startXRef];
},
parse: function XRef_parse(recoveryMode) {
var trailerDict;
if (!recoveryMode) {
trailerDict = this.readXRef();
} else {
(0, _util.warn)('Indexing all PDF objects');
trailerDict = this.indexObjects();
}
trailerDict.assignXref(this);
this.trailer = trailerDict;
var encrypt = void 0;
try {
encrypt = trailerDict.get('Encrypt');
} catch (ex) {
if (ex instanceof _util.MissingDataException) {
throw ex;
}
(0, _util.warn)('XRef.parse - Invalid "Encrypt" reference: "' + ex + '".');
}
if ((0, _primitives.isDict)(encrypt)) {
var ids = trailerDict.get('ID');
var fileId = ids && ids.length ? ids[0] : '';
encrypt.suppressEncryption = true;
this.encrypt = new _crypto.CipherTransformFactory(encrypt, fileId, this.pdfManager.password);
}
var root = void 0;
try {
root = trailerDict.get('Root');
} catch (ex) {
if (ex instanceof _util.MissingDataException) {
throw ex;
}
(0, _util.warn)('XRef.parse - Invalid "Root" reference: "' + ex + '".');
}
if ((0, _primitives.isDict)(root) && root.has('Pages')) {
this.root = root;
} else {
if (!recoveryMode) {
throw new _util.XRefParseException();
}
throw new _util.FormatError('Invalid root reference');
}
},
processXRefTable: function XRef_processXRefTable(parser) {
if (!('tableState' in this)) {
this.tableState = {
entryNum: 0,
streamPos: parser.lexer.stream.pos,
parserBuf1: parser.buf1,
parserBuf2: parser.buf2
};
}
var obj = this.readXRefTable(parser);
if (!(0, _primitives.isCmd)(obj, 'trailer')) {
throw new _util.FormatError('Invalid XRef table: could not find trailer dictionary');
}
var dict = parser.getObj();
if (!(0, _primitives.isDict)(dict) && dict.dict) {
dict = dict.dict;
}
if (!(0, _primitives.isDict)(dict)) {
throw new _util.FormatError('Invalid XRef table: could not parse trailer dictionary');
}
delete this.tableState;
return dict;
},
readXRefTable: function XRef_readXRefTable(parser) {
var stream = parser.lexer.stream;
var tableState = this.tableState;
stream.pos = tableState.streamPos;
parser.buf1 = tableState.parserBuf1;
parser.buf2 = tableState.parserBuf2;
var obj;
while (true) {
if (!('firstEntryNum' in tableState) || !('entryCount' in tableState)) {
if ((0, _primitives.isCmd)(obj = parser.getObj(), 'trailer')) {
break;
}
tableState.firstEntryNum = obj;
tableState.entryCount = parser.getObj();
}
var first = tableState.firstEntryNum;
var count = tableState.entryCount;
if (!Number.isInteger(first) || !Number.isInteger(count)) {
throw new _util.FormatError('Invalid XRef table: wrong types in subsection header');
}
for (var i = tableState.entryNum; i < count; i++) {
tableState.streamPos = stream.pos;
tableState.entryNum = i;
tableState.parserBuf1 = parser.buf1;
tableState.parserBuf2 = parser.buf2;
var entry = {};
entry.offset = parser.getObj();
entry.gen = parser.getObj();
var type = parser.getObj();
if ((0, _primitives.isCmd)(type, 'f')) {
entry.free = true;
} else if ((0, _primitives.isCmd)(type, 'n')) {
entry.uncompressed = true;
}
if (!Number.isInteger(entry.offset) || !Number.isInteger(entry.gen) || !(entry.free || entry.uncompressed)) {
throw new _util.FormatError('Invalid entry in XRef subsection: ' + first + ', ' + count);
}
if (i === 0 && entry.free && first === 1) {
first = 0;
}
if (!this.entries[i + first]) {
this.entries[i + first] = entry;
}
}
tableState.entryNum = 0;
tableState.streamPos = stream.pos;
tableState.parserBuf1 = parser.buf1;
tableState.parserBuf2 = parser.buf2;
delete tableState.firstEntryNum;
delete tableState.entryCount;
}
if (this.entries[0] && !this.entries[0].free) {
throw new _util.FormatError('Invalid XRef table: unexpected first object');
}
return obj;
},
processXRefStream: function XRef_processXRefStream(stream) {
if (!('streamState' in this)) {
var streamParameters = stream.dict;
var byteWidths = streamParameters.get('W');
var range = streamParameters.get('Index');
if (!range) {
range = [0, streamParameters.get('Size')];
}
this.streamState = {
entryRanges: range,
byteWidths: byteWidths,
entryNum: 0,
streamPos: stream.pos
};
}
this.readXRefStream(stream);
delete this.streamState;
return stream.dict;
},
readXRefStream: function XRef_readXRefStream(stream) {
var i, j;
var streamState = this.streamState;
stream.pos = streamState.streamPos;
var byteWidths = streamState.byteWidths;
var typeFieldWidth = byteWidths[0];
var offsetFieldWidth = byteWidths[1];
var generationFieldWidth = byteWidths[2];
var entryRanges = streamState.entryRanges;
while (entryRanges.length > 0) {
var first = entryRanges[0];
var n = entryRanges[1];
if (!Number.isInteger(first) || !Number.isInteger(n)) {
throw new _util.FormatError('Invalid XRef range fields: ' + first + ', ' + n);
}
if (!Number.isInteger(typeFieldWidth) || !Number.isInteger(offsetFieldWidth) || !Number.isInteger(generationFieldWidth)) {
throw new _util.FormatError('Invalid XRef entry fields length: ' + first + ', ' + n);
}
for (i = streamState.entryNum; i < n; ++i) {
streamState.entryNum = i;
streamState.streamPos = stream.pos;
var type = 0,
offset = 0,
generation = 0;
for (j = 0; j < typeFieldWidth; ++j) {
type = type << 8 | stream.getByte();
}
if (typeFieldWidth === 0) {
type = 1;
}
for (j = 0; j < offsetFieldWidth; ++j) {
offset = offset << 8 | stream.getByte();
}
for (j = 0; j < generationFieldWidth; ++j) {
generation = generation << 8 | stream.getByte();
}
var entry = {};
entry.offset = offset;
entry.gen = generation;
switch (type) {
case 0:
entry.free = true;
break;
case 1:
entry.uncompressed = true;
break;
case 2:
break;
default:
throw new _util.FormatError('Invalid XRef entry type: ' + type);
}
if (!this.entries[first + i]) {
this.entries[first + i] = entry;
}
}
streamState.entryNum = 0;
streamState.streamPos = stream.pos;
entryRanges.splice(0, 2);
}
},
indexObjects: function XRef_indexObjects() {
var TAB = 0x9,
LF = 0xA,
CR = 0xD,
SPACE = 0x20;
var PERCENT = 0x25,
LT = 0x3C;
function readToken(data, offset) {
var token = '',
ch = data[offset];
while (ch !== LF && ch !== CR && ch !== LT) {
if (++offset >= data.length) {
break;
}
token += String.fromCharCode(ch);
ch = data[offset];
}
return token;
}
function skipUntil(data, offset, what) {
var length = what.length,
dataLength = data.length;
var skipped = 0;
while (offset < dataLength) {
var i = 0;
while (i < length && data[offset + i] === what[i]) {
++i;
}
if (i >= length) {
break;
}
offset++;
skipped++;
}
return skipped;
}
var objRegExp = /^(\d+)\s+(\d+)\s+obj\b/;
var endobjRegExp = /\bendobj[\b\s]$/;
var nestedObjRegExp = /\s+(\d+\s+\d+\s+obj[\b\s])$/;
var CHECK_CONTENT_LENGTH = 25;
var trailerBytes = new Uint8Array([116, 114, 97, 105, 108, 101, 114]);
var startxrefBytes = new Uint8Array([115, 116, 97, 114, 116, 120, 114, 101, 102]);
var objBytes = new Uint8Array([111, 98, 106]);
var xrefBytes = new Uint8Array([47, 88, 82, 101, 102]);
this.entries.length = 0;
var stream = this.stream;
stream.pos = 0;
var buffer = stream.getBytes();
var position = stream.start,
length = buffer.length;
var trailers = [],
xrefStms = [];
while (position < length) {
var ch = buffer[position];
if (ch === TAB || ch === LF || ch === CR || ch === SPACE) {
++position;
continue;
}
if (ch === PERCENT) {
do {
++position;
if (position >= length) {
break;
}
ch = buffer[position];
} while (ch !== LF && ch !== CR);
continue;
}
var token = readToken(buffer, position);
var m;
if (token.indexOf('xref') === 0 && (token.length === 4 || /\s/.test(token[4]))) {
position += skipUntil(buffer, position, trailerBytes);
trailers.push(position);
position += skipUntil(buffer, position, startxrefBytes);
} else if (m = objRegExp.exec(token)) {
if (typeof this.entries[m[1]] === 'undefined') {
this.entries[m[1]] = {
offset: position - stream.start,
gen: m[2] | 0,
uncompressed: true
};
}
var contentLength = void 0,
startPos = position + token.length;
while (startPos < buffer.length) {
var endPos = startPos + skipUntil(buffer, startPos, objBytes) + 4;
contentLength = endPos - position;
var checkPos = Math.max(endPos - CHECK_CONTENT_LENGTH, startPos);
var tokenStr = (0, _util.bytesToString)(buffer.subarray(checkPos, endPos));
if (endobjRegExp.test(tokenStr)) {
break;
} else {
var objToken = nestedObjRegExp.exec(tokenStr);
if (objToken && objToken[1]) {
(0, _util.warn)('indexObjects: Found new "obj" inside of another "obj", ' + 'caused by missing "endobj" -- trying to recover.');
contentLength -= objToken[1].length;
break;
}
}
startPos = endPos;
}
var content = buffer.subarray(position, position + contentLength);
var xrefTagOffset = skipUntil(content, 0, xrefBytes);
if (xrefTagOffset < contentLength && content[xrefTagOffset + 5] < 64) {
xrefStms.push(position - stream.start);
this.xrefstms[position - stream.start] = 1;
}
position += contentLength;
} else if (token.indexOf('trailer') === 0 && (token.length === 7 || /\s/.test(token[7]))) {
trailers.push(position);
position += skipUntil(buffer, position, startxrefBytes);
} else {
position += token.length + 1;
}
}
var i, ii;
for (i = 0, ii = xrefStms.length; i < ii; ++i) {
this.startXRefQueue.push(xrefStms[i]);
this.readXRef(true);
}
var trailerDict = void 0;
for (i = 0, ii = trailers.length; i < ii; ++i) {
stream.pos = trailers[i];
var parser = new _parser.Parser(new _parser.Lexer(stream), true, this, true);
var obj = parser.getObj();
if (!(0, _primitives.isCmd)(obj, 'trailer')) {
continue;
}
var dict = parser.getObj();
if (!(0, _primitives.isDict)(dict)) {
continue;
}
var rootDict = void 0;
try {
rootDict = dict.get('Root');
} catch (ex) {
if (ex instanceof _util.MissingDataException) {
throw ex;
}
continue;
}
if (!(0, _primitives.isDict)(rootDict) || !rootDict.has('Pages')) {
continue;
}
if (dict.has('ID')) {
return dict;
}
trailerDict = dict;
}
if (trailerDict) {
return trailerDict;
}
throw new _util.InvalidPDFException('Invalid PDF structure');
},
readXRef: function XRef_readXRef(recoveryMode) {
var stream = this.stream;
var startXRefParsedCache = Object.create(null);
try {
while (this.startXRefQueue.length) {
var startXRef = this.startXRefQueue[0];
if (startXRefParsedCache[startXRef]) {
(0, _util.warn)('readXRef - skipping XRef table since it was already parsed.');
this.startXRefQueue.shift();
continue;
}
startXRefParsedCache[startXRef] = true;
stream.pos = startXRef + stream.start;
var parser = new _parser.Parser(new _parser.Lexer(stream), true, this);
var obj = parser.getObj();
var dict;
if ((0, _primitives.isCmd)(obj, 'xref')) {
dict = this.processXRefTable(parser);
if (!this.topDict) {
this.topDict = dict;
}
obj = dict.get('XRefStm');
if (Number.isInteger(obj)) {
var pos = obj;
if (!(pos in this.xrefstms)) {
this.xrefstms[pos] = 1;
this.startXRefQueue.push(pos);
}
}
} else if (Number.isInteger(obj)) {
if (!Number.isInteger(parser.getObj()) || !(0, _primitives.isCmd)(parser.getObj(), 'obj') || !(0, _primitives.isStream)(obj = parser.getObj())) {
throw new _util.FormatError('Invalid XRef stream');
}
dict = this.processXRefStream(obj);
if (!this.topDict) {
this.topDict = dict;
}
if (!dict) {
throw new _util.FormatError('Failed to read XRef stream');
}
} else {
throw new _util.FormatError('Invalid XRef stream header');
}
obj = dict.get('Prev');
if (Number.isInteger(obj)) {
this.startXRefQueue.push(obj);
} else if ((0, _primitives.isRef)(obj)) {
this.startXRefQueue.push(obj.num);
}
this.startXRefQueue.shift();
}
return this.topDict;
} catch (e) {
if (e instanceof _util.MissingDataException) {
throw e;
}
(0, _util.info)('(while reading XRef): ' + e);
}
if (recoveryMode) {
return;
}
throw new _util.XRefParseException();
},
getEntry: function XRef_getEntry(i) {
var xrefEntry = this.entries[i];
if (xrefEntry && !xrefEntry.free && xrefEntry.offset) {
return xrefEntry;
}
return null;
},
fetchIfRef: function XRef_fetchIfRef(obj, suppressEncryption) {
if (!(0, _primitives.isRef)(obj)) {
return obj;
}
return this.fetch(obj, suppressEncryption);
},
fetch: function XRef_fetch(ref, suppressEncryption) {
if (!(0, _primitives.isRef)(ref)) {
throw new Error('ref object is not a reference');
}
var num = ref.num;
if (num in this.cache) {
var cacheEntry = this.cache[num];
if (cacheEntry instanceof _primitives.Dict && !cacheEntry.objId) {
cacheEntry.objId = ref.toString();
}
return cacheEntry;
}
var xrefEntry = this.getEntry(num);
if (xrefEntry === null) {
return this.cache[num] = null;
}
if (xrefEntry.uncompressed) {
xrefEntry = this.fetchUncompressed(ref, xrefEntry, suppressEncryption);
} else {
xrefEntry = this.fetchCompressed(xrefEntry, suppressEncryption);
}
if ((0, _primitives.isDict)(xrefEntry)) {
xrefEntry.objId = ref.toString();
} else if ((0, _primitives.isStream)(xrefEntry)) {
xrefEntry.dict.objId = ref.toString();
}
return xrefEntry;
},
fetchUncompressed: function XRef_fetchUncompressed(ref, xrefEntry, suppressEncryption) {
var gen = ref.gen;
var num = ref.num;
if (xrefEntry.gen !== gen) {
throw new _util.FormatError('inconsistent generation in XRef');
}
var stream = this.stream.makeSubStream(xrefEntry.offset + this.stream.start);
var parser = new _parser.Parser(new _parser.Lexer(stream), true, this);
var obj1 = parser.getObj();
var obj2 = parser.getObj();
var obj3 = parser.getObj();
if (!Number.isInteger(obj1)) {
obj1 = parseInt(obj1, 10);
}
if (!Number.isInteger(obj2)) {
obj2 = parseInt(obj2, 10);
}
if (obj1 !== num || obj2 !== gen || !(0, _primitives.isCmd)(obj3)) {
throw new _util.FormatError('bad XRef entry');
}
if (obj3.cmd !== 'obj') {
if (obj3.cmd.indexOf('obj') === 0) {
num = parseInt(obj3.cmd.substring(3), 10);
if (!Number.isNaN(num)) {
return num;
}
}
throw new _util.FormatError('bad XRef entry');
}
if (this.encrypt && !suppressEncryption) {
xrefEntry = parser.getObj(this.encrypt.createCipherTransform(num, gen));
} else {
xrefEntry = parser.getObj();
}
if (!(0, _primitives.isStream)(xrefEntry)) {
this.cache[num] = xrefEntry;
}
return xrefEntry;
},
fetchCompressed: function XRef_fetchCompressed(xrefEntry, suppressEncryption) {
var tableOffset = xrefEntry.offset;
var stream = this.fetch(new _primitives.Ref(tableOffset, 0));
if (!(0, _primitives.isStream)(stream)) {
throw new _util.FormatError('bad ObjStm stream');
}
var first = stream.dict.get('First');
var n = stream.dict.get('N');
if (!Number.isInteger(first) || !Number.isInteger(n)) {
throw new _util.FormatError('invalid first and n parameters for ObjStm stream');
}
var parser = new _parser.Parser(new _parser.Lexer(stream), false, this);
parser.allowStreams = true;
var i,
entries = [],
num,
nums = [];
for (i = 0; i < n; ++i) {
num = parser.getObj();
if (!Number.isInteger(num)) {
throw new _util.FormatError('invalid object number in the ObjStm stream: ' + num);
}
nums.push(num);
var offset = parser.getObj();
if (!Number.isInteger(offset)) {
throw new _util.FormatError('invalid object offset in the ObjStm stream: ' + offset);
}
}
for (i = 0; i < n; ++i) {
entries.push(parser.getObj());
if ((0, _primitives.isCmd)(parser.buf1, 'endobj')) {
parser.shift();
}
num = nums[i];
var entry = this.entries[num];
if (entry && entry.offset === tableOffset && entry.gen === i) {
this.cache[num] = entries[i];
}
}
xrefEntry = entries[xrefEntry.gen];
if (xrefEntry === undefined) {
throw new _util.FormatError('bad XRef entry for compressed object');
}
return xrefEntry;
},
fetchIfRefAsync: function () {
var _ref = _asyncToGenerator( /*#__PURE__*/_regenerator2.default.mark(function _callee(obj, suppressEncryption) {
return _regenerator2.default.wrap(function _callee$(_context) {
while (1) {
switch (_context.prev = _context.next) {
case 0:
if ((0, _primitives.isRef)(obj)) {
_context.next = 2;
break;
}
return _context.abrupt('return', obj);
case 2:
return _context.abrupt('return', this.fetchAsync(obj, suppressEncryption));
case 3:
case 'end':
return _context.stop();
}
}
}, _callee, this);
}));
function fetchIfRefAsync(_x, _x2) {
return _ref.apply(this, arguments);
}
return fetchIfRefAsync;
}(),
fetchAsync: function () {
var _ref2 = _asyncToGenerator( /*#__PURE__*/_regenerator2.d