obj.js 47 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466
  1. /* Copyright 2017 Mozilla Foundation
  2. *
  3. * Licensed under the Apache License, Version 2.0 (the "License");
  4. * you may not use this file except in compliance with the License.
  5. * You may obtain a copy of the License at
  6. *
  7. * http://www.apache.org/licenses/LICENSE-2.0
  8. *
  9. * Unless required by applicable law or agreed to in writing, software
  10. * distributed under the License is distributed on an "AS IS" BASIS,
  11. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. * See the License for the specific language governing permissions and
  13. * limitations under the License.
  14. */
  15. 'use strict';
  16. var sharedUtil = require('../shared/util.js');
  17. var corePrimitives = require('./primitives.js');
  18. var coreCrypto = require('./crypto.js');
  19. var coreParser = require('./parser.js');
  20. var coreChunkedStream = require('./chunked_stream.js');
  21. var coreColorSpace = require('./colorspace.js');
  22. var InvalidPDFException = sharedUtil.InvalidPDFException;
  23. var MissingDataException = sharedUtil.MissingDataException;
  24. var XRefParseException = sharedUtil.XRefParseException;
  25. var assert = sharedUtil.assert;
  26. var bytesToString = sharedUtil.bytesToString;
  27. var createPromiseCapability = sharedUtil.createPromiseCapability;
  28. var error = sharedUtil.error;
  29. var info = sharedUtil.info;
  30. var isArray = sharedUtil.isArray;
  31. var isBool = sharedUtil.isBool;
  32. var isInt = sharedUtil.isInt;
  33. var isString = sharedUtil.isString;
  34. var shadow = sharedUtil.shadow;
  35. var stringToPDFString = sharedUtil.stringToPDFString;
  36. var stringToUTF8String = sharedUtil.stringToUTF8String;
  37. var warn = sharedUtil.warn;
  38. var createValidAbsoluteUrl = sharedUtil.createValidAbsoluteUrl;
  39. var Util = sharedUtil.Util;
  40. var Dict = corePrimitives.Dict;
  41. var Ref = corePrimitives.Ref;
  42. var RefSet = corePrimitives.RefSet;
  43. var RefSetCache = corePrimitives.RefSetCache;
  44. var isName = corePrimitives.isName;
  45. var isCmd = corePrimitives.isCmd;
  46. var isDict = corePrimitives.isDict;
  47. var isRef = corePrimitives.isRef;
  48. var isRefsEqual = corePrimitives.isRefsEqual;
  49. var isStream = corePrimitives.isStream;
  50. var CipherTransformFactory = coreCrypto.CipherTransformFactory;
  51. var Lexer = coreParser.Lexer;
  52. var Parser = coreParser.Parser;
  53. var ChunkedStream = coreChunkedStream.ChunkedStream;
  54. var ColorSpace = coreColorSpace.ColorSpace;
  55. var Catalog = function CatalogClosure() {
  56. function Catalog(pdfManager, xref, pageFactory) {
  57. this.pdfManager = pdfManager;
  58. this.xref = xref;
  59. this.catDict = xref.getCatalogObj();
  60. assert(isDict(this.catDict), 'catalog object is not a dictionary');
  61. this.fontCache = new RefSetCache();
  62. this.builtInCMapCache = Object.create(null);
  63. this.pageKidsCountCache = new RefSetCache();
  64. this.pageFactory = pageFactory;
  65. this.pagePromises = [];
  66. }
  67. Catalog.prototype = {
  68. get metadata() {
  69. var streamRef = this.catDict.getRaw('Metadata');
  70. if (!isRef(streamRef)) {
  71. return shadow(this, 'metadata', null);
  72. }
  73. var encryptMetadata = !this.xref.encrypt ? false : this.xref.encrypt.encryptMetadata;
  74. var stream = this.xref.fetch(streamRef, !encryptMetadata);
  75. var metadata;
  76. if (stream && isDict(stream.dict)) {
  77. var type = stream.dict.get('Type');
  78. var subtype = stream.dict.get('Subtype');
  79. if (isName(type, 'Metadata') && isName(subtype, 'XML')) {
  80. try {
  81. metadata = stringToUTF8String(bytesToString(stream.getBytes()));
  82. } catch (e) {
  83. if (e instanceof MissingDataException) {
  84. throw e;
  85. }
  86. info('Skipping invalid metadata.');
  87. }
  88. }
  89. }
  90. return shadow(this, 'metadata', metadata);
  91. },
  92. get toplevelPagesDict() {
  93. var pagesObj = this.catDict.get('Pages');
  94. assert(isDict(pagesObj), 'invalid top-level pages dictionary');
  95. return shadow(this, 'toplevelPagesDict', pagesObj);
  96. },
  97. get documentOutline() {
  98. var obj = null;
  99. try {
  100. obj = this.readDocumentOutline();
  101. } catch (ex) {
  102. if (ex instanceof MissingDataException) {
  103. throw ex;
  104. }
  105. warn('Unable to read document outline');
  106. }
  107. return shadow(this, 'documentOutline', obj);
  108. },
  109. readDocumentOutline: function Catalog_readDocumentOutline() {
  110. var obj = this.catDict.get('Outlines');
  111. if (!isDict(obj)) {
  112. return null;
  113. }
  114. obj = obj.getRaw('First');
  115. if (!isRef(obj)) {
  116. return null;
  117. }
  118. var root = { items: [] };
  119. var queue = [{
  120. obj: obj,
  121. parent: root
  122. }];
  123. var processed = new RefSet();
  124. processed.put(obj);
  125. var xref = this.xref,
  126. blackColor = new Uint8Array(3);
  127. while (queue.length > 0) {
  128. var i = queue.shift();
  129. var outlineDict = xref.fetchIfRef(i.obj);
  130. if (outlineDict === null) {
  131. continue;
  132. }
  133. assert(outlineDict.has('Title'), 'Invalid outline item');
  134. var data = {
  135. url: null,
  136. dest: null
  137. };
  138. Catalog.parseDestDictionary({
  139. destDict: outlineDict,
  140. resultObj: data,
  141. docBaseUrl: this.pdfManager.docBaseUrl
  142. });
  143. var title = outlineDict.get('Title');
  144. var flags = outlineDict.get('F') || 0;
  145. var color = outlineDict.getArray('C'),
  146. rgbColor = blackColor;
  147. if (isArray(color) && color.length === 3 && (color[0] !== 0 || color[1] !== 0 || color[2] !== 0)) {
  148. rgbColor = ColorSpace.singletons.rgb.getRgb(color, 0);
  149. }
  150. var outlineItem = {
  151. dest: data.dest,
  152. url: data.url,
  153. unsafeUrl: data.unsafeUrl,
  154. newWindow: data.newWindow,
  155. title: stringToPDFString(title),
  156. color: rgbColor,
  157. count: outlineDict.get('Count'),
  158. bold: !!(flags & 2),
  159. italic: !!(flags & 1),
  160. items: []
  161. };
  162. i.parent.items.push(outlineItem);
  163. obj = outlineDict.getRaw('First');
  164. if (isRef(obj) && !processed.has(obj)) {
  165. queue.push({
  166. obj: obj,
  167. parent: outlineItem
  168. });
  169. processed.put(obj);
  170. }
  171. obj = outlineDict.getRaw('Next');
  172. if (isRef(obj) && !processed.has(obj)) {
  173. queue.push({
  174. obj: obj,
  175. parent: i.parent
  176. });
  177. processed.put(obj);
  178. }
  179. }
  180. return root.items.length > 0 ? root.items : null;
  181. },
  182. get numPages() {
  183. var obj = this.toplevelPagesDict.get('Count');
  184. assert(isInt(obj), 'page count in top level pages object is not an integer');
  185. return shadow(this, 'num', obj);
  186. },
  187. get destinations() {
  188. function fetchDestination(dest) {
  189. return isDict(dest) ? dest.get('D') : dest;
  190. }
  191. var xref = this.xref;
  192. var dests = {},
  193. nameTreeRef,
  194. nameDictionaryRef;
  195. var obj = this.catDict.get('Names');
  196. if (obj && obj.has('Dests')) {
  197. nameTreeRef = obj.getRaw('Dests');
  198. } else if (this.catDict.has('Dests')) {
  199. nameDictionaryRef = this.catDict.get('Dests');
  200. }
  201. if (nameDictionaryRef) {
  202. obj = nameDictionaryRef;
  203. obj.forEach(function catalogForEach(key, value) {
  204. if (!value) {
  205. return;
  206. }
  207. dests[key] = fetchDestination(value);
  208. });
  209. }
  210. if (nameTreeRef) {
  211. var nameTree = new NameTree(nameTreeRef, xref);
  212. var names = nameTree.getAll();
  213. for (var name in names) {
  214. dests[name] = fetchDestination(names[name]);
  215. }
  216. }
  217. return shadow(this, 'destinations', dests);
  218. },
  219. getDestination: function Catalog_getDestination(destinationId) {
  220. function fetchDestination(dest) {
  221. return isDict(dest) ? dest.get('D') : dest;
  222. }
  223. var xref = this.xref;
  224. var dest = null,
  225. nameTreeRef,
  226. nameDictionaryRef;
  227. var obj = this.catDict.get('Names');
  228. if (obj && obj.has('Dests')) {
  229. nameTreeRef = obj.getRaw('Dests');
  230. } else if (this.catDict.has('Dests')) {
  231. nameDictionaryRef = this.catDict.get('Dests');
  232. }
  233. if (nameDictionaryRef) {
  234. var value = nameDictionaryRef.get(destinationId);
  235. if (value) {
  236. dest = fetchDestination(value);
  237. }
  238. }
  239. if (nameTreeRef) {
  240. var nameTree = new NameTree(nameTreeRef, xref);
  241. dest = fetchDestination(nameTree.get(destinationId));
  242. }
  243. return dest;
  244. },
  245. get pageLabels() {
  246. var obj = null;
  247. try {
  248. obj = this.readPageLabels();
  249. } catch (ex) {
  250. if (ex instanceof MissingDataException) {
  251. throw ex;
  252. }
  253. warn('Unable to read page labels.');
  254. }
  255. return shadow(this, 'pageLabels', obj);
  256. },
  257. readPageLabels: function Catalog_readPageLabels() {
  258. var obj = this.catDict.getRaw('PageLabels');
  259. if (!obj) {
  260. return null;
  261. }
  262. var pageLabels = new Array(this.numPages);
  263. var style = null;
  264. var prefix = '';
  265. var numberTree = new NumberTree(obj, this.xref);
  266. var nums = numberTree.getAll();
  267. var currentLabel = '',
  268. currentIndex = 1;
  269. for (var i = 0, ii = this.numPages; i < ii; i++) {
  270. if (i in nums) {
  271. var labelDict = nums[i];
  272. assert(isDict(labelDict), 'The PageLabel is not a dictionary.');
  273. var type = labelDict.get('Type');
  274. assert(!type || isName(type, 'PageLabel'), 'Invalid type in PageLabel dictionary.');
  275. var s = labelDict.get('S');
  276. assert(!s || isName(s), 'Invalid style in PageLabel dictionary.');
  277. style = s ? s.name : null;
  278. var p = labelDict.get('P');
  279. assert(!p || isString(p), 'Invalid prefix in PageLabel dictionary.');
  280. prefix = p ? stringToPDFString(p) : '';
  281. var st = labelDict.get('St');
  282. assert(!st || isInt(st) && st >= 1, 'Invalid start in PageLabel dictionary.');
  283. currentIndex = st || 1;
  284. }
  285. switch (style) {
  286. case 'D':
  287. currentLabel = currentIndex;
  288. break;
  289. case 'R':
  290. case 'r':
  291. currentLabel = Util.toRoman(currentIndex, style === 'r');
  292. break;
  293. case 'A':
  294. case 'a':
  295. var LIMIT = 26;
  296. var A_UPPER_CASE = 0x41,
  297. A_LOWER_CASE = 0x61;
  298. var baseCharCode = style === 'a' ? A_LOWER_CASE : A_UPPER_CASE;
  299. var letterIndex = currentIndex - 1;
  300. var character = String.fromCharCode(baseCharCode + letterIndex % LIMIT);
  301. var charBuf = [];
  302. for (var j = 0, jj = letterIndex / LIMIT | 0; j <= jj; j++) {
  303. charBuf.push(character);
  304. }
  305. currentLabel = charBuf.join('');
  306. break;
  307. default:
  308. assert(!style, 'Invalid style "' + style + '" in PageLabel dictionary.');
  309. }
  310. pageLabels[i] = prefix + currentLabel;
  311. currentLabel = '';
  312. currentIndex++;
  313. }
  314. return pageLabels;
  315. },
  316. get attachments() {
  317. var xref = this.xref;
  318. var attachments = null,
  319. nameTreeRef;
  320. var obj = this.catDict.get('Names');
  321. if (obj) {
  322. nameTreeRef = obj.getRaw('EmbeddedFiles');
  323. }
  324. if (nameTreeRef) {
  325. var nameTree = new NameTree(nameTreeRef, xref);
  326. var names = nameTree.getAll();
  327. for (var name in names) {
  328. var fs = new FileSpec(names[name], xref);
  329. if (!attachments) {
  330. attachments = Object.create(null);
  331. }
  332. attachments[stringToPDFString(name)] = fs.serializable;
  333. }
  334. }
  335. return shadow(this, 'attachments', attachments);
  336. },
  337. get javaScript() {
  338. var xref = this.xref;
  339. var obj = this.catDict.get('Names');
  340. var javaScript = [];
  341. function appendIfJavaScriptDict(jsDict) {
  342. var type = jsDict.get('S');
  343. if (!isName(type, 'JavaScript')) {
  344. return;
  345. }
  346. var js = jsDict.get('JS');
  347. if (isStream(js)) {
  348. js = bytesToString(js.getBytes());
  349. } else if (!isString(js)) {
  350. return;
  351. }
  352. javaScript.push(stringToPDFString(js));
  353. }
  354. if (obj && obj.has('JavaScript')) {
  355. var nameTree = new NameTree(obj.getRaw('JavaScript'), xref);
  356. var names = nameTree.getAll();
  357. for (var name in names) {
  358. var jsDict = names[name];
  359. if (isDict(jsDict)) {
  360. appendIfJavaScriptDict(jsDict);
  361. }
  362. }
  363. }
  364. var openactionDict = this.catDict.get('OpenAction');
  365. if (isDict(openactionDict, 'Action')) {
  366. var actionType = openactionDict.get('S');
  367. if (isName(actionType, 'Named')) {
  368. var action = openactionDict.get('N');
  369. if (isName(action, 'Print')) {
  370. javaScript.push('print({});');
  371. }
  372. } else {
  373. appendIfJavaScriptDict(openactionDict);
  374. }
  375. }
  376. return shadow(this, 'javaScript', javaScript);
  377. },
  378. cleanup: function Catalog_cleanup() {
  379. this.pageKidsCountCache.clear();
  380. var promises = [];
  381. this.fontCache.forEach(function (promise) {
  382. promises.push(promise);
  383. });
  384. return Promise.all(promises).then(function (translatedFonts) {
  385. for (var i = 0, ii = translatedFonts.length; i < ii; i++) {
  386. var font = translatedFonts[i].dict;
  387. delete font.translated;
  388. }
  389. this.fontCache.clear();
  390. this.builtInCMapCache = Object.create(null);
  391. }.bind(this));
  392. },
  393. getPage: function Catalog_getPage(pageIndex) {
  394. if (!(pageIndex in this.pagePromises)) {
  395. this.pagePromises[pageIndex] = this.getPageDict(pageIndex).then(function (a) {
  396. var dict = a[0];
  397. var ref = a[1];
  398. return this.pageFactory.createPage(pageIndex, dict, ref, this.fontCache, this.builtInCMapCache);
  399. }.bind(this));
  400. }
  401. return this.pagePromises[pageIndex];
  402. },
  403. getPageDict: function Catalog_getPageDict(pageIndex) {
  404. var capability = createPromiseCapability();
  405. var nodesToVisit = [this.catDict.getRaw('Pages')];
  406. var count,
  407. currentPageIndex = 0;
  408. var xref = this.xref,
  409. pageKidsCountCache = this.pageKidsCountCache;
  410. function next() {
  411. while (nodesToVisit.length) {
  412. var currentNode = nodesToVisit.pop();
  413. if (isRef(currentNode)) {
  414. count = pageKidsCountCache.get(currentNode);
  415. if (count > 0 && currentPageIndex + count < pageIndex) {
  416. currentPageIndex += count;
  417. continue;
  418. }
  419. xref.fetchAsync(currentNode).then(function (obj) {
  420. if (isDict(obj, 'Page') || isDict(obj) && !obj.has('Kids')) {
  421. if (pageIndex === currentPageIndex) {
  422. if (currentNode && !pageKidsCountCache.has(currentNode)) {
  423. pageKidsCountCache.put(currentNode, 1);
  424. }
  425. capability.resolve([obj, currentNode]);
  426. } else {
  427. currentPageIndex++;
  428. next();
  429. }
  430. return;
  431. }
  432. nodesToVisit.push(obj);
  433. next();
  434. }, capability.reject);
  435. return;
  436. }
  437. assert(isDict(currentNode), 'page dictionary kid reference points to wrong type of object');
  438. count = currentNode.get('Count');
  439. var objId = currentNode.objId;
  440. if (objId && !pageKidsCountCache.has(objId)) {
  441. pageKidsCountCache.put(objId, count);
  442. }
  443. if (currentPageIndex + count <= pageIndex) {
  444. currentPageIndex += count;
  445. continue;
  446. }
  447. var kids = currentNode.get('Kids');
  448. assert(isArray(kids), 'page dictionary kids object is not an array');
  449. for (var last = kids.length - 1; last >= 0; last--) {
  450. nodesToVisit.push(kids[last]);
  451. }
  452. }
  453. capability.reject('Page index ' + pageIndex + ' not found.');
  454. }
  455. next();
  456. return capability.promise;
  457. },
  458. getPageIndex: function Catalog_getPageIndex(pageRef) {
  459. var xref = this.xref;
  460. function pagesBeforeRef(kidRef) {
  461. var total = 0;
  462. var parentRef;
  463. return xref.fetchAsync(kidRef).then(function (node) {
  464. if (isRefsEqual(kidRef, pageRef) && !isDict(node, 'Page') && !(isDict(node) && !node.has('Type') && node.has('Contents'))) {
  465. throw new Error('The reference does not point to a /Page Dict.');
  466. }
  467. if (!node) {
  468. return null;
  469. }
  470. assert(isDict(node), 'node must be a Dict.');
  471. parentRef = node.getRaw('Parent');
  472. return node.getAsync('Parent');
  473. }).then(function (parent) {
  474. if (!parent) {
  475. return null;
  476. }
  477. assert(isDict(parent), 'parent must be a Dict.');
  478. return parent.getAsync('Kids');
  479. }).then(function (kids) {
  480. if (!kids) {
  481. return null;
  482. }
  483. var kidPromises = [];
  484. var found = false;
  485. for (var i = 0; i < kids.length; i++) {
  486. var kid = kids[i];
  487. assert(isRef(kid), 'kid must be a Ref.');
  488. if (kid.num === kidRef.num) {
  489. found = true;
  490. break;
  491. }
  492. kidPromises.push(xref.fetchAsync(kid).then(function (kid) {
  493. if (kid.has('Count')) {
  494. var count = kid.get('Count');
  495. total += count;
  496. } else {
  497. total++;
  498. }
  499. }));
  500. }
  501. if (!found) {
  502. error('kid ref not found in parents kids');
  503. }
  504. return Promise.all(kidPromises).then(function () {
  505. return [total, parentRef];
  506. });
  507. });
  508. }
  509. var total = 0;
  510. function next(ref) {
  511. return pagesBeforeRef(ref).then(function (args) {
  512. if (!args) {
  513. return total;
  514. }
  515. var count = args[0];
  516. var parentRef = args[1];
  517. total += count;
  518. return next(parentRef);
  519. });
  520. }
  521. return next(pageRef);
  522. }
  523. };
  524. Catalog.parseDestDictionary = function Catalog_parseDestDictionary(params) {
  525. function addDefaultProtocolToUrl(url) {
  526. if (url.indexOf('www.') === 0) {
  527. return 'http://' + url;
  528. }
  529. return url;
  530. }
  531. function tryConvertUrlEncoding(url) {
  532. try {
  533. return stringToUTF8String(url);
  534. } catch (e) {
  535. return url;
  536. }
  537. }
  538. var destDict = params.destDict;
  539. if (!isDict(destDict)) {
  540. warn('Catalog_parseDestDictionary: "destDict" must be a dictionary.');
  541. return;
  542. }
  543. var resultObj = params.resultObj;
  544. if (typeof resultObj !== 'object') {
  545. warn('Catalog_parseDestDictionary: "resultObj" must be an object.');
  546. return;
  547. }
  548. var docBaseUrl = params.docBaseUrl || null;
  549. var action = destDict.get('A'),
  550. url,
  551. dest;
  552. if (isDict(action)) {
  553. var linkType = action.get('S').name;
  554. switch (linkType) {
  555. case 'URI':
  556. url = action.get('URI');
  557. if (isName(url)) {
  558. url = '/' + url.name;
  559. } else if (isString(url)) {
  560. url = addDefaultProtocolToUrl(url);
  561. }
  562. break;
  563. case 'GoTo':
  564. dest = action.get('D');
  565. break;
  566. case 'Launch':
  567. case 'GoToR':
  568. var urlDict = action.get('F');
  569. if (isDict(urlDict)) {
  570. url = urlDict.get('F') || null;
  571. } else if (isString(urlDict)) {
  572. url = urlDict;
  573. }
  574. var remoteDest = action.get('D');
  575. if (remoteDest) {
  576. if (isName(remoteDest)) {
  577. remoteDest = remoteDest.name;
  578. }
  579. if (isString(url)) {
  580. var baseUrl = url.split('#')[0];
  581. if (isString(remoteDest)) {
  582. url = baseUrl + '#' + (/^\d+$/.test(remoteDest) ? 'nameddest=' : '') + remoteDest;
  583. } else if (isArray(remoteDest)) {
  584. url = baseUrl + '#' + JSON.stringify(remoteDest);
  585. }
  586. }
  587. }
  588. var newWindow = action.get('NewWindow');
  589. if (isBool(newWindow)) {
  590. resultObj.newWindow = newWindow;
  591. }
  592. break;
  593. case 'Named':
  594. var namedAction = action.get('N');
  595. if (isName(namedAction)) {
  596. resultObj.action = namedAction.name;
  597. }
  598. break;
  599. case 'JavaScript':
  600. var jsAction = action.get('JS'),
  601. js;
  602. if (isStream(jsAction)) {
  603. js = bytesToString(jsAction.getBytes());
  604. } else if (isString(jsAction)) {
  605. js = jsAction;
  606. }
  607. if (js) {
  608. var URL_OPEN_METHODS = ['app.launchURL', 'window.open'];
  609. var regex = new RegExp('^\\s*(' + URL_OPEN_METHODS.join('|').split('.').join('\\.') + ')\\((?:\'|\")([^\'\"]*)(?:\'|\")(?:,\\s*(\\w+)\\)|\\))', 'i');
  610. var jsUrl = regex.exec(stringToPDFString(js));
  611. if (jsUrl && jsUrl[2]) {
  612. url = jsUrl[2];
  613. if (jsUrl[3] === 'true' && jsUrl[1] === 'app.launchURL') {
  614. resultObj.newWindow = true;
  615. }
  616. break;
  617. }
  618. }
  619. default:
  620. warn('Catalog_parseDestDictionary: Unrecognized link type "' + linkType + '".');
  621. break;
  622. }
  623. } else if (destDict.has('Dest')) {
  624. dest = destDict.get('Dest');
  625. }
  626. if (isString(url)) {
  627. url = tryConvertUrlEncoding(url);
  628. var absoluteUrl = createValidAbsoluteUrl(url, docBaseUrl);
  629. if (absoluteUrl) {
  630. resultObj.url = absoluteUrl.href;
  631. }
  632. resultObj.unsafeUrl = url;
  633. }
  634. if (dest) {
  635. if (isName(dest)) {
  636. dest = dest.name;
  637. }
  638. if (isString(dest) || isArray(dest)) {
  639. resultObj.dest = dest;
  640. }
  641. }
  642. };
  643. return Catalog;
  644. }();
  645. var XRef = function XRefClosure() {
  646. function XRef(stream, pdfManager) {
  647. this.stream = stream;
  648. this.pdfManager = pdfManager;
  649. this.entries = [];
  650. this.xrefstms = Object.create(null);
  651. this.cache = [];
  652. this.stats = {
  653. streamTypes: [],
  654. fontTypes: []
  655. };
  656. }
  657. XRef.prototype = {
  658. setStartXRef: function XRef_setStartXRef(startXRef) {
  659. this.startXRefQueue = [startXRef];
  660. },
  661. parse: function XRef_parse(recoveryMode) {
  662. var trailerDict;
  663. if (!recoveryMode) {
  664. trailerDict = this.readXRef();
  665. } else {
  666. warn('Indexing all PDF objects');
  667. trailerDict = this.indexObjects();
  668. }
  669. trailerDict.assignXref(this);
  670. this.trailer = trailerDict;
  671. var encrypt = trailerDict.get('Encrypt');
  672. if (isDict(encrypt)) {
  673. var ids = trailerDict.get('ID');
  674. var fileId = ids && ids.length ? ids[0] : '';
  675. encrypt.suppressEncryption = true;
  676. this.encrypt = new CipherTransformFactory(encrypt, fileId, this.pdfManager.password);
  677. }
  678. if (!(this.root = trailerDict.get('Root'))) {
  679. error('Invalid root reference');
  680. }
  681. },
  682. processXRefTable: function XRef_processXRefTable(parser) {
  683. if (!('tableState' in this)) {
  684. this.tableState = {
  685. entryNum: 0,
  686. streamPos: parser.lexer.stream.pos,
  687. parserBuf1: parser.buf1,
  688. parserBuf2: parser.buf2
  689. };
  690. }
  691. var obj = this.readXRefTable(parser);
  692. if (!isCmd(obj, 'trailer')) {
  693. error('Invalid XRef table: could not find trailer dictionary');
  694. }
  695. var dict = parser.getObj();
  696. if (!isDict(dict) && dict.dict) {
  697. dict = dict.dict;
  698. }
  699. if (!isDict(dict)) {
  700. error('Invalid XRef table: could not parse trailer dictionary');
  701. }
  702. delete this.tableState;
  703. return dict;
  704. },
  705. readXRefTable: function XRef_readXRefTable(parser) {
  706. var stream = parser.lexer.stream;
  707. var tableState = this.tableState;
  708. stream.pos = tableState.streamPos;
  709. parser.buf1 = tableState.parserBuf1;
  710. parser.buf2 = tableState.parserBuf2;
  711. var obj;
  712. while (true) {
  713. if (!('firstEntryNum' in tableState) || !('entryCount' in tableState)) {
  714. if (isCmd(obj = parser.getObj(), 'trailer')) {
  715. break;
  716. }
  717. tableState.firstEntryNum = obj;
  718. tableState.entryCount = parser.getObj();
  719. }
  720. var first = tableState.firstEntryNum;
  721. var count = tableState.entryCount;
  722. if (!isInt(first) || !isInt(count)) {
  723. error('Invalid XRef table: wrong types in subsection header');
  724. }
  725. for (var i = tableState.entryNum; i < count; i++) {
  726. tableState.streamPos = stream.pos;
  727. tableState.entryNum = i;
  728. tableState.parserBuf1 = parser.buf1;
  729. tableState.parserBuf2 = parser.buf2;
  730. var entry = {};
  731. entry.offset = parser.getObj();
  732. entry.gen = parser.getObj();
  733. var type = parser.getObj();
  734. if (isCmd(type, 'f')) {
  735. entry.free = true;
  736. } else if (isCmd(type, 'n')) {
  737. entry.uncompressed = true;
  738. }
  739. if (!isInt(entry.offset) || !isInt(entry.gen) || !(entry.free || entry.uncompressed)) {
  740. error('Invalid entry in XRef subsection: ' + first + ', ' + count);
  741. }
  742. if (i === 0 && entry.free && first === 1) {
  743. first = 0;
  744. }
  745. if (!this.entries[i + first]) {
  746. this.entries[i + first] = entry;
  747. }
  748. }
  749. tableState.entryNum = 0;
  750. tableState.streamPos = stream.pos;
  751. tableState.parserBuf1 = parser.buf1;
  752. tableState.parserBuf2 = parser.buf2;
  753. delete tableState.firstEntryNum;
  754. delete tableState.entryCount;
  755. }
  756. if (this.entries[0] && !this.entries[0].free) {
  757. error('Invalid XRef table: unexpected first object');
  758. }
  759. return obj;
  760. },
  761. processXRefStream: function XRef_processXRefStream(stream) {
  762. if (!('streamState' in this)) {
  763. var streamParameters = stream.dict;
  764. var byteWidths = streamParameters.get('W');
  765. var range = streamParameters.get('Index');
  766. if (!range) {
  767. range = [0, streamParameters.get('Size')];
  768. }
  769. this.streamState = {
  770. entryRanges: range,
  771. byteWidths: byteWidths,
  772. entryNum: 0,
  773. streamPos: stream.pos
  774. };
  775. }
  776. this.readXRefStream(stream);
  777. delete this.streamState;
  778. return stream.dict;
  779. },
  780. readXRefStream: function XRef_readXRefStream(stream) {
  781. var i, j;
  782. var streamState = this.streamState;
  783. stream.pos = streamState.streamPos;
  784. var byteWidths = streamState.byteWidths;
  785. var typeFieldWidth = byteWidths[0];
  786. var offsetFieldWidth = byteWidths[1];
  787. var generationFieldWidth = byteWidths[2];
  788. var entryRanges = streamState.entryRanges;
  789. while (entryRanges.length > 0) {
  790. var first = entryRanges[0];
  791. var n = entryRanges[1];
  792. if (!isInt(first) || !isInt(n)) {
  793. error('Invalid XRef range fields: ' + first + ', ' + n);
  794. }
  795. if (!isInt(typeFieldWidth) || !isInt(offsetFieldWidth) || !isInt(generationFieldWidth)) {
  796. error('Invalid XRef entry fields length: ' + first + ', ' + n);
  797. }
  798. for (i = streamState.entryNum; i < n; ++i) {
  799. streamState.entryNum = i;
  800. streamState.streamPos = stream.pos;
  801. var type = 0,
  802. offset = 0,
  803. generation = 0;
  804. for (j = 0; j < typeFieldWidth; ++j) {
  805. type = type << 8 | stream.getByte();
  806. }
  807. if (typeFieldWidth === 0) {
  808. type = 1;
  809. }
  810. for (j = 0; j < offsetFieldWidth; ++j) {
  811. offset = offset << 8 | stream.getByte();
  812. }
  813. for (j = 0; j < generationFieldWidth; ++j) {
  814. generation = generation << 8 | stream.getByte();
  815. }
  816. var entry = {};
  817. entry.offset = offset;
  818. entry.gen = generation;
  819. switch (type) {
  820. case 0:
  821. entry.free = true;
  822. break;
  823. case 1:
  824. entry.uncompressed = true;
  825. break;
  826. case 2:
  827. break;
  828. default:
  829. error('Invalid XRef entry type: ' + type);
  830. }
  831. if (!this.entries[first + i]) {
  832. this.entries[first + i] = entry;
  833. }
  834. }
  835. streamState.entryNum = 0;
  836. streamState.streamPos = stream.pos;
  837. entryRanges.splice(0, 2);
  838. }
  839. },
  840. indexObjects: function XRef_indexObjects() {
  841. var TAB = 0x9,
  842. LF = 0xA,
  843. CR = 0xD,
  844. SPACE = 0x20;
  845. var PERCENT = 0x25,
  846. LT = 0x3C;
  847. function readToken(data, offset) {
  848. var token = '',
  849. ch = data[offset];
  850. while (ch !== LF && ch !== CR && ch !== LT) {
  851. if (++offset >= data.length) {
  852. break;
  853. }
  854. token += String.fromCharCode(ch);
  855. ch = data[offset];
  856. }
  857. return token;
  858. }
  859. function skipUntil(data, offset, what) {
  860. var length = what.length,
  861. dataLength = data.length;
  862. var skipped = 0;
  863. while (offset < dataLength) {
  864. var i = 0;
  865. while (i < length && data[offset + i] === what[i]) {
  866. ++i;
  867. }
  868. if (i >= length) {
  869. break;
  870. }
  871. offset++;
  872. skipped++;
  873. }
  874. return skipped;
  875. }
  876. var objRegExp = /^(\d+)\s+(\d+)\s+obj\b/;
  877. var trailerBytes = new Uint8Array([116, 114, 97, 105, 108, 101, 114]);
  878. var startxrefBytes = new Uint8Array([115, 116, 97, 114, 116, 120, 114, 101, 102]);
  879. var endobjBytes = new Uint8Array([101, 110, 100, 111, 98, 106]);
  880. var xrefBytes = new Uint8Array([47, 88, 82, 101, 102]);
  881. this.entries.length = 0;
  882. var stream = this.stream;
  883. stream.pos = 0;
  884. var buffer = stream.getBytes();
  885. var position = stream.start,
  886. length = buffer.length;
  887. var trailers = [],
  888. xrefStms = [];
  889. while (position < length) {
  890. var ch = buffer[position];
  891. if (ch === TAB || ch === LF || ch === CR || ch === SPACE) {
  892. ++position;
  893. continue;
  894. }
  895. if (ch === PERCENT) {
  896. do {
  897. ++position;
  898. if (position >= length) {
  899. break;
  900. }
  901. ch = buffer[position];
  902. } while (ch !== LF && ch !== CR);
  903. continue;
  904. }
  905. var token = readToken(buffer, position);
  906. var m;
  907. if (token.indexOf('xref') === 0 && (token.length === 4 || /\s/.test(token[4]))) {
  908. position += skipUntil(buffer, position, trailerBytes);
  909. trailers.push(position);
  910. position += skipUntil(buffer, position, startxrefBytes);
  911. } else if (m = objRegExp.exec(token)) {
  912. if (typeof this.entries[m[1]] === 'undefined') {
  913. this.entries[m[1]] = {
  914. offset: position - stream.start,
  915. gen: m[2] | 0,
  916. uncompressed: true
  917. };
  918. }
  919. var contentLength = skipUntil(buffer, position, endobjBytes) + 7;
  920. var content = buffer.subarray(position, position + contentLength);
  921. var xrefTagOffset = skipUntil(content, 0, xrefBytes);
  922. if (xrefTagOffset < contentLength && content[xrefTagOffset + 5] < 64) {
  923. xrefStms.push(position - stream.start);
  924. this.xrefstms[position - stream.start] = 1;
  925. }
  926. position += contentLength;
  927. } else if (token.indexOf('trailer') === 0 && (token.length === 7 || /\s/.test(token[7]))) {
  928. trailers.push(position);
  929. position += skipUntil(buffer, position, startxrefBytes);
  930. } else {
  931. position += token.length + 1;
  932. }
  933. }
  934. var i, ii;
  935. for (i = 0, ii = xrefStms.length; i < ii; ++i) {
  936. this.startXRefQueue.push(xrefStms[i]);
  937. this.readXRef(true);
  938. }
  939. var dict;
  940. for (i = 0, ii = trailers.length; i < ii; ++i) {
  941. stream.pos = trailers[i];
  942. var parser = new Parser(new Lexer(stream), true, this, true);
  943. var obj = parser.getObj();
  944. if (!isCmd(obj, 'trailer')) {
  945. continue;
  946. }
  947. dict = parser.getObj();
  948. if (!isDict(dict)) {
  949. continue;
  950. }
  951. if (dict.has('ID')) {
  952. return dict;
  953. }
  954. }
  955. if (dict) {
  956. return dict;
  957. }
  958. throw new InvalidPDFException('Invalid PDF structure');
  959. },
  960. readXRef: function XRef_readXRef(recoveryMode) {
  961. var stream = this.stream;
  962. try {
  963. while (this.startXRefQueue.length) {
  964. var startXRef = this.startXRefQueue[0];
  965. stream.pos = startXRef + stream.start;
  966. var parser = new Parser(new Lexer(stream), true, this);
  967. var obj = parser.getObj();
  968. var dict;
  969. if (isCmd(obj, 'xref')) {
  970. dict = this.processXRefTable(parser);
  971. if (!this.topDict) {
  972. this.topDict = dict;
  973. }
  974. obj = dict.get('XRefStm');
  975. if (isInt(obj)) {
  976. var pos = obj;
  977. if (!(pos in this.xrefstms)) {
  978. this.xrefstms[pos] = 1;
  979. this.startXRefQueue.push(pos);
  980. }
  981. }
  982. } else if (isInt(obj)) {
  983. if (!isInt(parser.getObj()) || !isCmd(parser.getObj(), 'obj') || !isStream(obj = parser.getObj())) {
  984. error('Invalid XRef stream');
  985. }
  986. dict = this.processXRefStream(obj);
  987. if (!this.topDict) {
  988. this.topDict = dict;
  989. }
  990. if (!dict) {
  991. error('Failed to read XRef stream');
  992. }
  993. } else {
  994. error('Invalid XRef stream header');
  995. }
  996. obj = dict.get('Prev');
  997. if (isInt(obj)) {
  998. this.startXRefQueue.push(obj);
  999. } else if (isRef(obj)) {
  1000. this.startXRefQueue.push(obj.num);
  1001. }
  1002. this.startXRefQueue.shift();
  1003. }
  1004. return this.topDict;
  1005. } catch (e) {
  1006. if (e instanceof MissingDataException) {
  1007. throw e;
  1008. }
  1009. info('(while reading XRef): ' + e);
  1010. }
  1011. if (recoveryMode) {
  1012. return;
  1013. }
  1014. throw new XRefParseException();
  1015. },
  1016. getEntry: function XRef_getEntry(i) {
  1017. var xrefEntry = this.entries[i];
  1018. if (xrefEntry && !xrefEntry.free && xrefEntry.offset) {
  1019. return xrefEntry;
  1020. }
  1021. return null;
  1022. },
  1023. fetchIfRef: function XRef_fetchIfRef(obj, suppressEncryption) {
  1024. if (!isRef(obj)) {
  1025. return obj;
  1026. }
  1027. return this.fetch(obj, suppressEncryption);
  1028. },
  1029. fetch: function XRef_fetch(ref, suppressEncryption) {
  1030. assert(isRef(ref), 'ref object is not a reference');
  1031. var num = ref.num;
  1032. if (num in this.cache) {
  1033. var cacheEntry = this.cache[num];
  1034. if (cacheEntry instanceof Dict && !cacheEntry.objId) {
  1035. cacheEntry.objId = ref.toString();
  1036. }
  1037. return cacheEntry;
  1038. }
  1039. var xrefEntry = this.getEntry(num);
  1040. if (xrefEntry === null) {
  1041. return this.cache[num] = null;
  1042. }
  1043. if (xrefEntry.uncompressed) {
  1044. xrefEntry = this.fetchUncompressed(ref, xrefEntry, suppressEncryption);
  1045. } else {
  1046. xrefEntry = this.fetchCompressed(xrefEntry, suppressEncryption);
  1047. }
  1048. if (isDict(xrefEntry)) {
  1049. xrefEntry.objId = ref.toString();
  1050. } else if (isStream(xrefEntry)) {
  1051. xrefEntry.dict.objId = ref.toString();
  1052. }
  1053. return xrefEntry;
  1054. },
  1055. fetchUncompressed: function XRef_fetchUncompressed(ref, xrefEntry, suppressEncryption) {
  1056. var gen = ref.gen;
  1057. var num = ref.num;
  1058. if (xrefEntry.gen !== gen) {
  1059. error('inconsistent generation in XRef');
  1060. }
  1061. var stream = this.stream.makeSubStream(xrefEntry.offset + this.stream.start);
  1062. var parser = new Parser(new Lexer(stream), true, this);
  1063. var obj1 = parser.getObj();
  1064. var obj2 = parser.getObj();
  1065. var obj3 = parser.getObj();
  1066. if (!isInt(obj1) || parseInt(obj1, 10) !== num || !isInt(obj2) || parseInt(obj2, 10) !== gen || !isCmd(obj3)) {
  1067. error('bad XRef entry');
  1068. }
  1069. if (!isCmd(obj3, 'obj')) {
  1070. if (obj3.cmd.indexOf('obj') === 0) {
  1071. num = parseInt(obj3.cmd.substring(3), 10);
  1072. if (!isNaN(num)) {
  1073. return num;
  1074. }
  1075. }
  1076. error('bad XRef entry');
  1077. }
  1078. if (this.encrypt && !suppressEncryption) {
  1079. xrefEntry = parser.getObj(this.encrypt.createCipherTransform(num, gen));
  1080. } else {
  1081. xrefEntry = parser.getObj();
  1082. }
  1083. if (!isStream(xrefEntry)) {
  1084. this.cache[num] = xrefEntry;
  1085. }
  1086. return xrefEntry;
  1087. },
  1088. fetchCompressed: function XRef_fetchCompressed(xrefEntry, suppressEncryption) {
  1089. var tableOffset = xrefEntry.offset;
  1090. var stream = this.fetch(new Ref(tableOffset, 0));
  1091. if (!isStream(stream)) {
  1092. error('bad ObjStm stream');
  1093. }
  1094. var first = stream.dict.get('First');
  1095. var n = stream.dict.get('N');
  1096. if (!isInt(first) || !isInt(n)) {
  1097. error('invalid first and n parameters for ObjStm stream');
  1098. }
  1099. var parser = new Parser(new Lexer(stream), false, this);
  1100. parser.allowStreams = true;
  1101. var i,
  1102. entries = [],
  1103. num,
  1104. nums = [];
  1105. for (i = 0; i < n; ++i) {
  1106. num = parser.getObj();
  1107. if (!isInt(num)) {
  1108. error('invalid object number in the ObjStm stream: ' + num);
  1109. }
  1110. nums.push(num);
  1111. var offset = parser.getObj();
  1112. if (!isInt(offset)) {
  1113. error('invalid object offset in the ObjStm stream: ' + offset);
  1114. }
  1115. }
  1116. for (i = 0; i < n; ++i) {
  1117. entries.push(parser.getObj());
  1118. if (isCmd(parser.buf1, 'endobj')) {
  1119. parser.shift();
  1120. }
  1121. num = nums[i];
  1122. var entry = this.entries[num];
  1123. if (entry && entry.offset === tableOffset && entry.gen === i) {
  1124. this.cache[num] = entries[i];
  1125. }
  1126. }
  1127. xrefEntry = entries[xrefEntry.gen];
  1128. if (xrefEntry === undefined) {
  1129. error('bad XRef entry for compressed object');
  1130. }
  1131. return xrefEntry;
  1132. },
  1133. fetchIfRefAsync: function XRef_fetchIfRefAsync(obj, suppressEncryption) {
  1134. if (!isRef(obj)) {
  1135. return Promise.resolve(obj);
  1136. }
  1137. return this.fetchAsync(obj, suppressEncryption);
  1138. },
  1139. fetchAsync: function XRef_fetchAsync(ref, suppressEncryption) {
  1140. var streamManager = this.stream.manager;
  1141. var xref = this;
  1142. return new Promise(function tryFetch(resolve, reject) {
  1143. try {
  1144. resolve(xref.fetch(ref, suppressEncryption));
  1145. } catch (e) {
  1146. if (e instanceof MissingDataException) {
  1147. streamManager.requestRange(e.begin, e.end).then(function () {
  1148. tryFetch(resolve, reject);
  1149. }, reject);
  1150. return;
  1151. }
  1152. reject(e);
  1153. }
  1154. });
  1155. },
  1156. getCatalogObj: function XRef_getCatalogObj() {
  1157. return this.root;
  1158. }
  1159. };
  1160. return XRef;
  1161. }();
  1162. var NameOrNumberTree = function NameOrNumberTreeClosure() {
  1163. function NameOrNumberTree(root, xref) {
  1164. throw new Error('Cannot initialize NameOrNumberTree.');
  1165. }
  1166. NameOrNumberTree.prototype = {
  1167. getAll: function NameOrNumberTree_getAll() {
  1168. var dict = Object.create(null);
  1169. if (!this.root) {
  1170. return dict;
  1171. }
  1172. var xref = this.xref;
  1173. var processed = new RefSet();
  1174. processed.put(this.root);
  1175. var queue = [this.root];
  1176. while (queue.length > 0) {
  1177. var i, n;
  1178. var obj = xref.fetchIfRef(queue.shift());
  1179. if (!isDict(obj)) {
  1180. continue;
  1181. }
  1182. if (obj.has('Kids')) {
  1183. var kids = obj.get('Kids');
  1184. for (i = 0, n = kids.length; i < n; i++) {
  1185. var kid = kids[i];
  1186. assert(!processed.has(kid), 'Duplicate entry in "' + this._type + '" tree.');
  1187. queue.push(kid);
  1188. processed.put(kid);
  1189. }
  1190. continue;
  1191. }
  1192. var entries = obj.get(this._type);
  1193. if (isArray(entries)) {
  1194. for (i = 0, n = entries.length; i < n; i += 2) {
  1195. dict[xref.fetchIfRef(entries[i])] = xref.fetchIfRef(entries[i + 1]);
  1196. }
  1197. }
  1198. }
  1199. return dict;
  1200. },
  1201. get: function NameOrNumberTree_get(key) {
  1202. if (!this.root) {
  1203. return null;
  1204. }
  1205. var xref = this.xref;
  1206. var kidsOrEntries = xref.fetchIfRef(this.root);
  1207. var loopCount = 0;
  1208. var MAX_LEVELS = 10;
  1209. var l, r, m;
  1210. while (kidsOrEntries.has('Kids')) {
  1211. if (++loopCount > MAX_LEVELS) {
  1212. warn('Search depth limit reached for "' + this._type + '" tree.');
  1213. return null;
  1214. }
  1215. var kids = kidsOrEntries.get('Kids');
  1216. if (!isArray(kids)) {
  1217. return null;
  1218. }
  1219. l = 0;
  1220. r = kids.length - 1;
  1221. while (l <= r) {
  1222. m = l + r >> 1;
  1223. var kid = xref.fetchIfRef(kids[m]);
  1224. var limits = kid.get('Limits');
  1225. if (key < xref.fetchIfRef(limits[0])) {
  1226. r = m - 1;
  1227. } else if (key > xref.fetchIfRef(limits[1])) {
  1228. l = m + 1;
  1229. } else {
  1230. kidsOrEntries = xref.fetchIfRef(kids[m]);
  1231. break;
  1232. }
  1233. }
  1234. if (l > r) {
  1235. return null;
  1236. }
  1237. }
  1238. var entries = kidsOrEntries.get(this._type);
  1239. if (isArray(entries)) {
  1240. l = 0;
  1241. r = entries.length - 2;
  1242. while (l <= r) {
  1243. m = l + r & ~1;
  1244. var currentKey = xref.fetchIfRef(entries[m]);
  1245. if (key < currentKey) {
  1246. r = m - 2;
  1247. } else if (key > currentKey) {
  1248. l = m + 2;
  1249. } else {
  1250. return xref.fetchIfRef(entries[m + 1]);
  1251. }
  1252. }
  1253. }
  1254. return null;
  1255. }
  1256. };
  1257. return NameOrNumberTree;
  1258. }();
  1259. var NameTree = function NameTreeClosure() {
  1260. function NameTree(root, xref) {
  1261. this.root = root;
  1262. this.xref = xref;
  1263. this._type = 'Names';
  1264. }
  1265. Util.inherit(NameTree, NameOrNumberTree, {});
  1266. return NameTree;
  1267. }();
  1268. var NumberTree = function NumberTreeClosure() {
  1269. function NumberTree(root, xref) {
  1270. this.root = root;
  1271. this.xref = xref;
  1272. this._type = 'Nums';
  1273. }
  1274. Util.inherit(NumberTree, NameOrNumberTree, {});
  1275. return NumberTree;
  1276. }();
  1277. var FileSpec = function FileSpecClosure() {
  1278. function FileSpec(root, xref) {
  1279. if (!root || !isDict(root)) {
  1280. return;
  1281. }
  1282. this.xref = xref;
  1283. this.root = root;
  1284. if (root.has('FS')) {
  1285. this.fs = root.get('FS');
  1286. }
  1287. this.description = root.has('Desc') ? stringToPDFString(root.get('Desc')) : '';
  1288. if (root.has('RF')) {
  1289. warn('Related file specifications are not supported');
  1290. }
  1291. this.contentAvailable = true;
  1292. if (!root.has('EF')) {
  1293. this.contentAvailable = false;
  1294. warn('Non-embedded file specifications are not supported');
  1295. }
  1296. }
  1297. function pickPlatformItem(dict) {
  1298. if (dict.has('UF')) {
  1299. return dict.get('UF');
  1300. } else if (dict.has('F')) {
  1301. return dict.get('F');
  1302. } else if (dict.has('Unix')) {
  1303. return dict.get('Unix');
  1304. } else if (dict.has('Mac')) {
  1305. return dict.get('Mac');
  1306. } else if (dict.has('DOS')) {
  1307. return dict.get('DOS');
  1308. }
  1309. return null;
  1310. }
  1311. FileSpec.prototype = {
  1312. get filename() {
  1313. if (!this._filename && this.root) {
  1314. var filename = pickPlatformItem(this.root) || 'unnamed';
  1315. this._filename = stringToPDFString(filename).replace(/\\\\/g, '\\').replace(/\\\//g, '/').replace(/\\/g, '/');
  1316. }
  1317. return this._filename;
  1318. },
  1319. get content() {
  1320. if (!this.contentAvailable) {
  1321. return null;
  1322. }
  1323. if (!this.contentRef && this.root) {
  1324. this.contentRef = pickPlatformItem(this.root.get('EF'));
  1325. }
  1326. var content = null;
  1327. if (this.contentRef) {
  1328. var xref = this.xref;
  1329. var fileObj = xref.fetchIfRef(this.contentRef);
  1330. if (fileObj && isStream(fileObj)) {
  1331. content = fileObj.getBytes();
  1332. } else {
  1333. warn('Embedded file specification points to non-existing/invalid ' + 'content');
  1334. }
  1335. } else {
  1336. warn('Embedded file specification does not have a content');
  1337. }
  1338. return content;
  1339. },
  1340. get serializable() {
  1341. return {
  1342. filename: this.filename,
  1343. content: this.content
  1344. };
  1345. }
  1346. };
  1347. return FileSpec;
  1348. }();
  1349. var ObjectLoader = function () {
  1350. function mayHaveChildren(value) {
  1351. return isRef(value) || isDict(value) || isArray(value) || isStream(value);
  1352. }
  1353. function addChildren(node, nodesToVisit) {
  1354. var value;
  1355. if (isDict(node) || isStream(node)) {
  1356. var map;
  1357. if (isDict(node)) {
  1358. map = node.map;
  1359. } else {
  1360. map = node.dict.map;
  1361. }
  1362. for (var key in map) {
  1363. value = map[key];
  1364. if (mayHaveChildren(value)) {
  1365. nodesToVisit.push(value);
  1366. }
  1367. }
  1368. } else if (isArray(node)) {
  1369. for (var i = 0, ii = node.length; i < ii; i++) {
  1370. value = node[i];
  1371. if (mayHaveChildren(value)) {
  1372. nodesToVisit.push(value);
  1373. }
  1374. }
  1375. }
  1376. }
  1377. function ObjectLoader(obj, keys, xref) {
  1378. this.obj = obj;
  1379. this.keys = keys;
  1380. this.xref = xref;
  1381. this.refSet = null;
  1382. this.capability = null;
  1383. }
  1384. ObjectLoader.prototype = {
  1385. load: function ObjectLoader_load() {
  1386. var keys = this.keys;
  1387. this.capability = createPromiseCapability();
  1388. if (!(this.xref.stream instanceof ChunkedStream) || this.xref.stream.getMissingChunks().length === 0) {
  1389. this.capability.resolve();
  1390. return this.capability.promise;
  1391. }
  1392. this.refSet = new RefSet();
  1393. var nodesToVisit = [];
  1394. for (var i = 0; i < keys.length; i++) {
  1395. nodesToVisit.push(this.obj[keys[i]]);
  1396. }
  1397. this._walk(nodesToVisit);
  1398. return this.capability.promise;
  1399. },
  1400. _walk: function ObjectLoader_walk(nodesToVisit) {
  1401. var nodesToRevisit = [];
  1402. var pendingRequests = [];
  1403. while (nodesToVisit.length) {
  1404. var currentNode = nodesToVisit.pop();
  1405. if (isRef(currentNode)) {
  1406. if (this.refSet.has(currentNode)) {
  1407. continue;
  1408. }
  1409. try {
  1410. var ref = currentNode;
  1411. this.refSet.put(ref);
  1412. currentNode = this.xref.fetch(currentNode);
  1413. } catch (e) {
  1414. if (!(e instanceof MissingDataException)) {
  1415. throw e;
  1416. }
  1417. nodesToRevisit.push(currentNode);
  1418. pendingRequests.push({
  1419. begin: e.begin,
  1420. end: e.end
  1421. });
  1422. }
  1423. }
  1424. if (currentNode && currentNode.getBaseStreams) {
  1425. var baseStreams = currentNode.getBaseStreams();
  1426. var foundMissingData = false;
  1427. for (var i = 0; i < baseStreams.length; i++) {
  1428. var stream = baseStreams[i];
  1429. if (stream.getMissingChunks && stream.getMissingChunks().length) {
  1430. foundMissingData = true;
  1431. pendingRequests.push({
  1432. begin: stream.start,
  1433. end: stream.end
  1434. });
  1435. }
  1436. }
  1437. if (foundMissingData) {
  1438. nodesToRevisit.push(currentNode);
  1439. }
  1440. }
  1441. addChildren(currentNode, nodesToVisit);
  1442. }
  1443. if (pendingRequests.length) {
  1444. this.xref.stream.manager.requestRanges(pendingRequests).then(function pendingRequestCallback() {
  1445. nodesToVisit = nodesToRevisit;
  1446. for (var i = 0; i < nodesToRevisit.length; i++) {
  1447. var node = nodesToRevisit[i];
  1448. if (isRef(node)) {
  1449. this.refSet.remove(node);
  1450. }
  1451. }
  1452. this._walk(nodesToVisit);
  1453. }.bind(this), this.capability.reject);
  1454. return;
  1455. }
  1456. this.refSet = null;
  1457. this.capability.resolve();
  1458. }
  1459. };
  1460. return ObjectLoader;
  1461. }();
  1462. exports.Catalog = Catalog;
  1463. exports.ObjectLoader = ObjectLoader;
  1464. exports.XRef = XRef;
  1465. exports.FileSpec = FileSpec;