pdfjs-dist 2.2.228 → 2.6.347

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.

Potentially problematic release.


This version of pdfjs-dist might be problematic. Click here for more details.

Files changed (229) hide show
  1. package/README.md +4 -0
  2. package/bower.json +1 -1
  3. package/build/pdf.js +8816 -19300
  4. package/build/pdf.js.map +1 -1
  5. package/build/pdf.min.js +22 -1
  6. package/build/pdf.worker.entry.js +5 -3
  7. package/build/pdf.worker.js +27185 -35746
  8. package/build/pdf.worker.js.map +1 -1
  9. package/build/pdf.worker.min.js +22 -1
  10. package/es5/build/pdf.js +26665 -0
  11. package/es5/build/pdf.js.map +1 -0
  12. package/es5/build/pdf.min.js +22 -0
  13. package/{lib/shared/global_scope.js → es5/build/pdf.worker.entry.js} +5 -10
  14. package/es5/build/pdf.worker.js +61231 -0
  15. package/es5/build/pdf.worker.js.map +1 -0
  16. package/es5/build/pdf.worker.min.js +22 -0
  17. package/es5/image_decoders/pdf.image_decoders.js +12529 -0
  18. package/es5/image_decoders/pdf.image_decoders.js.map +1 -0
  19. package/es5/image_decoders/pdf.image_decoders.min.js +22 -0
  20. package/es5/web/images/annotation-check.svg +11 -0
  21. package/es5/web/images/annotation-comment.svg +16 -0
  22. package/es5/web/images/annotation-help.svg +26 -0
  23. package/es5/web/images/annotation-insert.svg +10 -0
  24. package/es5/web/images/annotation-key.svg +11 -0
  25. package/es5/web/images/annotation-newparagraph.svg +11 -0
  26. package/es5/web/images/annotation-noicon.svg +7 -0
  27. package/es5/web/images/annotation-note.svg +42 -0
  28. package/es5/web/images/annotation-paragraph.svg +16 -0
  29. package/es5/web/images/loading-icon.gif +0 -0
  30. package/es5/web/images/shadow.png +0 -0
  31. package/es5/web/pdf_viewer.css +406 -0
  32. package/es5/web/pdf_viewer.js +7886 -0
  33. package/es5/web/pdf_viewer.js.map +1 -0
  34. package/image_decoders/pdf.image_decoders.js +1498 -4832
  35. package/image_decoders/pdf.image_decoders.js.map +1 -1
  36. package/image_decoders/pdf.image_decoders.min.js +22 -1
  37. package/lib/README.md +7 -0
  38. package/lib/core/annotation.js +1144 -927
  39. package/lib/core/arithmetic_decoder.js +81 -97
  40. package/lib/core/bidi.js +54 -46
  41. package/lib/core/ccitt.js +88 -81
  42. package/lib/core/ccitt_stream.js +15 -14
  43. package/lib/core/cff_parser.js +747 -716
  44. package/lib/core/charsets.js +4 -4
  45. package/lib/core/chunked_stream.js +462 -553
  46. package/lib/core/cmap.js +231 -277
  47. package/lib/core/colorspace.js +715 -857
  48. package/lib/core/core_utils.js +57 -52
  49. package/lib/core/crypto.js +416 -437
  50. package/lib/core/document.js +629 -670
  51. package/lib/core/encodings.js +15 -15
  52. package/lib/core/evaluator.js +3229 -2800
  53. package/lib/core/font_renderer.js +135 -178
  54. package/lib/core/fonts.js +602 -534
  55. package/lib/core/function.js +355 -279
  56. package/lib/core/glyphlist.js +4527 -4526
  57. package/lib/core/image.js +442 -439
  58. package/lib/core/image_utils.js +221 -61
  59. package/lib/core/jbig2.js +325 -316
  60. package/lib/core/jbig2_stream.js +18 -17
  61. package/lib/core/jpeg_stream.js +21 -26
  62. package/lib/core/jpg.js +289 -233
  63. package/lib/core/jpx.js +161 -143
  64. package/lib/core/jpx_stream.js +28 -28
  65. package/lib/core/metrics.js +2928 -2928
  66. package/lib/core/murmurhash3.js +87 -102
  67. package/lib/core/obj.js +1343 -1300
  68. package/lib/core/operator_list.js +90 -67
  69. package/lib/core/parser.js +990 -987
  70. package/lib/core/pattern.js +105 -77
  71. package/lib/core/pdf_manager.js +149 -316
  72. package/lib/core/primitives.js +156 -104
  73. package/lib/core/ps_parser.js +175 -214
  74. package/lib/core/standard_fonts.js +237 -236
  75. package/lib/core/stream.js +94 -74
  76. package/lib/core/type1_parser.js +89 -69
  77. package/lib/core/unicode.js +1654 -1654
  78. package/lib/core/worker.js +275 -290
  79. package/lib/core/worker_stream.js +101 -210
  80. package/lib/core/writer.js +242 -0
  81. package/lib/display/annotation_layer.js +772 -1041
  82. package/lib/display/annotation_storage.js +89 -0
  83. package/lib/display/api.js +1575 -1868
  84. package/lib/display/api_compatibility.js +12 -17
  85. package/lib/display/canvas.js +370 -222
  86. package/lib/display/content_disposition.js +40 -59
  87. package/lib/display/display_utils.js +341 -536
  88. package/lib/display/fetch_stream.js +188 -304
  89. package/lib/display/font_loader.js +282 -414
  90. package/lib/display/metadata.js +81 -99
  91. package/lib/display/network.js +362 -511
  92. package/lib/display/network_utils.js +25 -18
  93. package/lib/display/node_stream.js +283 -465
  94. package/lib/display/node_utils.js +87 -0
  95. package/lib/display/optional_content_config.js +184 -0
  96. package/lib/display/pattern_helper.js +93 -44
  97. package/lib/display/svg.js +1163 -1401
  98. package/lib/display/text_layer.js +154 -137
  99. package/lib/display/transport_stream.js +237 -375
  100. package/lib/display/webgl.js +70 -83
  101. package/lib/display/worker_options.js +3 -3
  102. package/lib/display/xml_parser.js +303 -392
  103. package/lib/examples/node/domstubs.js +37 -37
  104. package/lib/pdf.js +220 -58
  105. package/lib/pdf.worker.js +14 -6
  106. package/lib/shared/compatibility.js +3 -246
  107. package/lib/shared/is_node.js +7 -6
  108. package/lib/shared/message_handler.js +327 -332
  109. package/lib/shared/util.js +271 -306
  110. package/lib/test/unit/annotation_spec.js +1902 -763
  111. package/lib/test/unit/annotation_storage_spec.js +83 -0
  112. package/lib/test/unit/api_spec.js +790 -592
  113. package/lib/test/unit/bidi_spec.js +7 -7
  114. package/lib/test/unit/cff_parser_spec.js +73 -72
  115. package/lib/test/unit/clitests_helper.js +7 -9
  116. package/lib/test/unit/cmap_spec.js +86 -88
  117. package/lib/test/unit/colorspace_spec.js +346 -154
  118. package/lib/test/unit/core_utils_spec.js +125 -105
  119. package/lib/test/unit/crypto_spec.js +232 -185
  120. package/lib/test/unit/custom_spec.js +150 -24
  121. package/lib/test/unit/display_svg_spec.js +34 -39
  122. package/lib/test/unit/display_utils_spec.js +139 -149
  123. package/lib/test/unit/document_spec.js +128 -17
  124. package/lib/test/unit/encodings_spec.js +12 -34
  125. package/lib/test/unit/evaluator_spec.js +112 -91
  126. package/lib/test/unit/fetch_stream_spec.js +31 -29
  127. package/lib/test/unit/function_spec.js +206 -204
  128. package/lib/test/unit/jasmine-boot.js +48 -32
  129. package/lib/test/unit/message_handler_spec.js +173 -159
  130. package/lib/test/unit/metadata_spec.js +71 -71
  131. package/lib/test/unit/murmurhash3_spec.js +12 -12
  132. package/lib/test/unit/network_spec.js +13 -11
  133. package/lib/test/unit/network_utils_spec.js +158 -144
  134. package/lib/test/unit/node_stream_spec.js +76 -90
  135. package/lib/test/unit/parser_spec.js +108 -114
  136. package/lib/test/unit/pdf_find_controller_spec.js +55 -86
  137. package/lib/test/unit/pdf_find_utils_spec.js +32 -32
  138. package/lib/test/unit/pdf_history_spec.js +32 -32
  139. package/lib/test/unit/primitives_spec.js +337 -156
  140. package/lib/test/unit/stream_spec.js +16 -14
  141. package/lib/test/unit/test_utils.js +91 -304
  142. package/lib/test/unit/testreporter.js +33 -22
  143. package/lib/test/unit/type1_parser_spec.js +42 -42
  144. package/lib/test/unit/ui_utils_spec.js +299 -461
  145. package/lib/test/unit/unicode_spec.js +42 -42
  146. package/lib/test/unit/util_spec.js +129 -149
  147. package/lib/test/unit/writer_spec.js +84 -0
  148. package/lib/web/annotation_layer_builder.js +78 -111
  149. package/lib/web/app.js +1542 -1267
  150. package/lib/web/app_options.js +76 -91
  151. package/lib/web/base_tree_viewer.js +100 -0
  152. package/lib/web/base_viewer.js +869 -834
  153. package/lib/web/chromecom.js +165 -252
  154. package/lib/web/debugger.js +149 -205
  155. package/lib/web/download_manager.js +34 -58
  156. package/lib/web/firefox_print_service.js +62 -34
  157. package/lib/web/firefoxcom.js +206 -374
  158. package/lib/web/genericcom.js +26 -108
  159. package/lib/web/genericl10n.js +24 -153
  160. package/lib/web/grab_to_pan.js +30 -30
  161. package/lib/web/interfaces.js +80 -254
  162. package/lib/web/overlay_manager.js +70 -246
  163. package/lib/web/password_prompt.js +38 -64
  164. package/lib/web/pdf_attachment_viewer.js +150 -126
  165. package/lib/web/pdf_cursor_tools.js +75 -102
  166. package/lib/web/pdf_document_properties.js +229 -383
  167. package/lib/web/pdf_find_bar.js +136 -170
  168. package/lib/web/pdf_find_controller.js +492 -548
  169. package/lib/web/pdf_find_utils.js +13 -13
  170. package/lib/web/pdf_history.js +397 -406
  171. package/lib/web/pdf_layer_viewer.js +208 -0
  172. package/lib/web/pdf_link_service.js +304 -348
  173. package/lib/web/pdf_outline_viewer.js +119 -212
  174. package/lib/web/pdf_page_view.js +474 -513
  175. package/lib/web/pdf_presentation_mode.js +308 -357
  176. package/lib/web/pdf_print_service.js +96 -110
  177. package/lib/web/pdf_rendering_queue.js +87 -108
  178. package/lib/web/pdf_sidebar.js +286 -302
  179. package/lib/web/pdf_sidebar_resizer.js +92 -119
  180. package/lib/web/pdf_single_page_viewer.js +77 -126
  181. package/lib/web/pdf_thumbnail_view.js +289 -297
  182. package/lib/web/pdf_thumbnail_viewer.js +202 -206
  183. package/lib/web/pdf_viewer.component.js +20 -21
  184. package/lib/web/pdf_viewer.js +55 -115
  185. package/lib/web/preferences.js +69 -273
  186. package/lib/web/secondary_toolbar.js +164 -196
  187. package/lib/web/text_layer_builder.js +284 -319
  188. package/lib/web/toolbar.js +217 -210
  189. package/lib/web/ui_utils.js +274 -421
  190. package/lib/web/view_history.js +52 -226
  191. package/lib/web/viewer_compatibility.js +21 -6
  192. package/package.json +4 -9
  193. package/types/display/annotation_layer.d.ts +61 -0
  194. package/types/display/annotation_storage.d.ts +36 -0
  195. package/types/display/api.d.ts +1107 -0
  196. package/types/display/api_compatibility.d.ts +1 -0
  197. package/types/display/canvas.d.ts +4 -0
  198. package/types/display/content_disposition.d.ts +7 -0
  199. package/types/display/display_utils.d.ts +243 -0
  200. package/types/display/fetch_stream.d.ts +67 -0
  201. package/types/display/font_loader.d.ts +19 -0
  202. package/types/display/metadata.d.ts +9 -0
  203. package/types/display/network.d.ts +80 -0
  204. package/types/display/network_utils.d.ts +14 -0
  205. package/types/display/node_stream.d.ts +72 -0
  206. package/types/display/node_utils.d.ts +6 -0
  207. package/types/display/optional_content_config.d.ts +12 -0
  208. package/types/display/pattern_helper.d.ts +5 -0
  209. package/types/display/svg.d.ts +2 -0
  210. package/types/display/text_layer.d.ts +83 -0
  211. package/types/display/transport_stream.d.ts +57 -0
  212. package/types/display/webgl.d.ts +20 -0
  213. package/types/display/worker_options.d.ts +27 -0
  214. package/types/display/xml_parser.d.ts +35 -0
  215. package/types/pdf.d.ts +33 -0
  216. package/types/shared/compatibility.d.ts +1 -0
  217. package/types/shared/is_node.d.ts +1 -0
  218. package/types/shared/message_handler.d.ts +62 -0
  219. package/types/shared/util.d.ts +421 -0
  220. package/web/images/shadow.png +0 -0
  221. package/web/pdf_viewer.css +29 -27
  222. package/web/pdf_viewer.js +3524 -4746
  223. package/web/pdf_viewer.js.map +1 -1
  224. package/webpack.js +6 -5
  225. package/external/streams/streams-lib.js +0 -3962
  226. package/external/url/url-lib.js +0 -627
  227. package/lib/shared/streams_polyfill.js +0 -43
  228. package/lib/shared/url_polyfill.js +0 -56
  229. package/web/images/texture.png +0 -0
package/lib/core/obj.js CHANGED
@@ -2,7 +2,7 @@
2
2
  * @licstart The following is the entire license notice for the
3
3
  * Javascript code in this page
4
4
  *
5
- * Copyright 2019 Mozilla Foundation
5
+ * Copyright 2020 Mozilla Foundation
6
6
  *
7
7
  * Licensed under the Apache License, Version 2.0 (the "License");
8
8
  * you may not use this file except in compliance with the License.
@@ -26,1200 +26,1322 @@ Object.defineProperty(exports, "__esModule", {
26
26
  });
27
27
  exports.FileSpec = exports.XRef = exports.ObjectLoader = exports.Catalog = void 0;
28
28
 
29
- var _regenerator = _interopRequireDefault(require("@babel/runtime/regenerator"));
29
+ var _util = require("../shared/util.js");
30
30
 
31
- var _util = require("../shared/util");
31
+ var _primitives = require("./primitives.js");
32
32
 
33
- var _primitives = require("./primitives");
33
+ var _parser = require("./parser.js");
34
34
 
35
- var _parser = require("./parser");
35
+ var _core_utils = require("./core_utils.js");
36
36
 
37
- var _core_utils = require("./core_utils");
37
+ var _crypto = require("./crypto.js");
38
38
 
39
- var _chunked_stream = require("./chunked_stream");
39
+ var _colorspace = require("./colorspace.js");
40
40
 
41
- var _crypto = require("./crypto");
41
+ var _image_utils = require("./image_utils.js");
42
42
 
43
- var _colorspace = require("./colorspace");
43
+ function fetchDestination(dest) {
44
+ return (0, _primitives.isDict)(dest) ? dest.get("D") : dest;
45
+ }
44
46
 
45
- function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { "default": obj }; }
47
+ class Catalog {
48
+ constructor(pdfManager, xref) {
49
+ this.pdfManager = pdfManager;
50
+ this.xref = xref;
51
+ this._catDict = xref.getCatalogObj();
46
52
 
47
- function _possibleConstructorReturn(self, call) { if (call && (_typeof(call) === "object" || typeof call === "function")) { return call; } return _assertThisInitialized(self); }
53
+ if (!(0, _primitives.isDict)(this._catDict)) {
54
+ throw new _util.FormatError("Catalog object is not a dictionary.");
55
+ }
48
56
 
49
- function _assertThisInitialized(self) { if (self === void 0) { throw new ReferenceError("this hasn't been initialised - super() hasn't been called"); } return self; }
57
+ this.fontCache = new _primitives.RefSetCache();
58
+ this.builtInCMapCache = new Map();
59
+ this.globalImageCache = new _image_utils.GlobalImageCache();
60
+ this.pageKidsCountCache = new _primitives.RefSetCache();
61
+ }
50
62
 
51
- function _getPrototypeOf(o) { _getPrototypeOf = Object.setPrototypeOf ? Object.getPrototypeOf : function _getPrototypeOf(o) { return o.__proto__ || Object.getPrototypeOf(o); }; return _getPrototypeOf(o); }
63
+ get version() {
64
+ const version = this._catDict.get("Version");
52
65
 
53
- function _inherits(subClass, superClass) { if (typeof superClass !== "function" && superClass !== null) { throw new TypeError("Super expression must either be null or a function"); } subClass.prototype = Object.create(superClass && superClass.prototype, { constructor: { value: subClass, writable: true, configurable: true } }); if (superClass) _setPrototypeOf(subClass, superClass); }
66
+ if (!(0, _primitives.isName)(version)) {
67
+ return (0, _util.shadow)(this, "version", null);
68
+ }
54
69
 
55
- function _setPrototypeOf(o, p) { _setPrototypeOf = Object.setPrototypeOf || function _setPrototypeOf(o, p) { o.__proto__ = p; return o; }; return _setPrototypeOf(o, p); }
70
+ return (0, _util.shadow)(this, "version", version.name);
71
+ }
56
72
 
57
- function asyncGeneratorStep(gen, resolve, reject, _next, _throw, key, arg) { try { var info = gen[key](arg); var value = info.value; } catch (error) { reject(error); return; } if (info.done) { resolve(value); } else { Promise.resolve(value).then(_next, _throw); } }
73
+ get collection() {
74
+ let collection = null;
58
75
 
59
- function _asyncToGenerator(fn) { return function () { var self = this, args = arguments; return new Promise(function (resolve, reject) { var gen = fn.apply(self, args); function _next(value) { asyncGeneratorStep(gen, resolve, reject, _next, _throw, "next", value); } function _throw(err) { asyncGeneratorStep(gen, resolve, reject, _next, _throw, "throw", err); } _next(undefined); }); }; }
76
+ try {
77
+ const obj = this._catDict.get("Collection");
60
78
 
61
- function _slicedToArray(arr, i) { return _arrayWithHoles(arr) || _iterableToArrayLimit(arr, i) || _nonIterableRest(); }
79
+ if ((0, _primitives.isDict)(obj) && obj.size > 0) {
80
+ collection = obj;
81
+ }
82
+ } catch (ex) {
83
+ if (ex instanceof _core_utils.MissingDataException) {
84
+ throw ex;
85
+ }
62
86
 
63
- function _nonIterableRest() { throw new TypeError("Invalid attempt to destructure non-iterable instance"); }
87
+ (0, _util.info)("Cannot fetch Collection entry; assuming no collection is present.");
88
+ }
64
89
 
65
- function _iterableToArrayLimit(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"] != null) _i["return"](); } finally { if (_d) throw _e; } } return _arr; }
90
+ return (0, _util.shadow)(this, "collection", collection);
91
+ }
66
92
 
67
- function _arrayWithHoles(arr) { if (Array.isArray(arr)) return arr; }
93
+ get acroForm() {
94
+ let acroForm = null;
68
95
 
69
- function _typeof(obj) { if (typeof Symbol === "function" && typeof Symbol.iterator === "symbol") { _typeof = function _typeof(obj) { return typeof obj; }; } else { _typeof = function _typeof(obj) { return obj && typeof Symbol === "function" && obj.constructor === Symbol && obj !== Symbol.prototype ? "symbol" : typeof obj; }; } return _typeof(obj); }
96
+ try {
97
+ const obj = this._catDict.get("AcroForm");
70
98
 
71
- function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } }
99
+ if ((0, _primitives.isDict)(obj) && obj.size > 0) {
100
+ acroForm = obj;
101
+ }
102
+ } catch (ex) {
103
+ if (ex instanceof _core_utils.MissingDataException) {
104
+ throw ex;
105
+ }
72
106
 
73
- 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); } }
107
+ (0, _util.info)("Cannot fetch AcroForm entry; assuming no forms are present.");
108
+ }
74
109
 
75
- function _createClass(Constructor, protoProps, staticProps) { if (protoProps) _defineProperties(Constructor.prototype, protoProps); if (staticProps) _defineProperties(Constructor, staticProps); return Constructor; }
110
+ return (0, _util.shadow)(this, "acroForm", acroForm);
111
+ }
76
112
 
77
- function fetchDestination(dest) {
78
- return (0, _primitives.isDict)(dest) ? dest.get('D') : dest;
79
- }
113
+ get metadata() {
114
+ const streamRef = this._catDict.getRaw("Metadata");
80
115
 
81
- var Catalog =
82
- /*#__PURE__*/
83
- function () {
84
- function Catalog(pdfManager, xref) {
85
- _classCallCheck(this, Catalog);
116
+ if (!(0, _primitives.isRef)(streamRef)) {
117
+ return (0, _util.shadow)(this, "metadata", null);
118
+ }
86
119
 
87
- this.pdfManager = pdfManager;
88
- this.xref = xref;
89
- this.catDict = xref.getCatalogObj();
120
+ const suppressEncryption = !(this.xref.encrypt && this.xref.encrypt.encryptMetadata);
121
+ const stream = this.xref.fetch(streamRef, suppressEncryption);
122
+ let metadata;
123
+
124
+ if (stream && (0, _primitives.isDict)(stream.dict)) {
125
+ const type = stream.dict.get("Type");
126
+ const subtype = stream.dict.get("Subtype");
127
+
128
+ if ((0, _primitives.isName)(type, "Metadata") && (0, _primitives.isName)(subtype, "XML")) {
129
+ try {
130
+ metadata = (0, _util.stringToUTF8String)((0, _util.bytesToString)(stream.getBytes()));
131
+ } catch (e) {
132
+ if (e instanceof _core_utils.MissingDataException) {
133
+ throw e;
134
+ }
90
135
 
91
- if (!(0, _primitives.isDict)(this.catDict)) {
92
- throw new _util.FormatError('Catalog object is not a dictionary.');
136
+ (0, _util.info)("Skipping invalid metadata.");
137
+ }
138
+ }
93
139
  }
94
140
 
95
- this.fontCache = new _primitives.RefSetCache();
96
- this.builtInCMapCache = new Map();
97
- this.pageKidsCountCache = new _primitives.RefSetCache();
141
+ return (0, _util.shadow)(this, "metadata", metadata);
98
142
  }
99
143
 
100
- _createClass(Catalog, [{
101
- key: "_readDocumentOutline",
102
- value: function _readDocumentOutline() {
103
- var obj = this.catDict.get('Outlines');
144
+ get toplevelPagesDict() {
145
+ const pagesObj = this._catDict.get("Pages");
104
146
 
105
- if (!(0, _primitives.isDict)(obj)) {
106
- return null;
147
+ if (!(0, _primitives.isDict)(pagesObj)) {
148
+ throw new _util.FormatError("Invalid top-level pages dictionary.");
149
+ }
150
+
151
+ return (0, _util.shadow)(this, "toplevelPagesDict", pagesObj);
152
+ }
153
+
154
+ get documentOutline() {
155
+ let obj = null;
156
+
157
+ try {
158
+ obj = this._readDocumentOutline();
159
+ } catch (ex) {
160
+ if (ex instanceof _core_utils.MissingDataException) {
161
+ throw ex;
107
162
  }
108
163
 
109
- obj = obj.getRaw('First');
164
+ (0, _util.warn)("Unable to read document outline.");
165
+ }
110
166
 
111
- if (!(0, _primitives.isRef)(obj)) {
112
- return null;
167
+ return (0, _util.shadow)(this, "documentOutline", obj);
168
+ }
169
+
170
+ _readDocumentOutline() {
171
+ let obj = this._catDict.get("Outlines");
172
+
173
+ if (!(0, _primitives.isDict)(obj)) {
174
+ return null;
175
+ }
176
+
177
+ obj = obj.getRaw("First");
178
+
179
+ if (!(0, _primitives.isRef)(obj)) {
180
+ return null;
181
+ }
182
+
183
+ const root = {
184
+ items: []
185
+ };
186
+ const queue = [{
187
+ obj,
188
+ parent: root
189
+ }];
190
+ const processed = new _primitives.RefSet();
191
+ processed.put(obj);
192
+ const xref = this.xref,
193
+ blackColor = new Uint8ClampedArray(3);
194
+
195
+ while (queue.length > 0) {
196
+ const i = queue.shift();
197
+ const outlineDict = xref.fetchIfRef(i.obj);
198
+
199
+ if (outlineDict === null) {
200
+ continue;
113
201
  }
114
202
 
115
- var root = {
203
+ if (!outlineDict.has("Title")) {
204
+ throw new _util.FormatError("Invalid outline item encountered.");
205
+ }
206
+
207
+ const data = {
208
+ url: null,
209
+ dest: null
210
+ };
211
+ Catalog.parseDestDictionary({
212
+ destDict: outlineDict,
213
+ resultObj: data,
214
+ docBaseUrl: this.pdfManager.docBaseUrl
215
+ });
216
+ const title = outlineDict.get("Title");
217
+ const flags = outlineDict.get("F") || 0;
218
+ const color = outlineDict.getArray("C");
219
+ const count = outlineDict.get("Count");
220
+ let rgbColor = blackColor;
221
+
222
+ if (Array.isArray(color) && color.length === 3 && (color[0] !== 0 || color[1] !== 0 || color[2] !== 0)) {
223
+ rgbColor = _colorspace.ColorSpace.singletons.rgb.getRgb(color, 0);
224
+ }
225
+
226
+ const outlineItem = {
227
+ dest: data.dest,
228
+ url: data.url,
229
+ unsafeUrl: data.unsafeUrl,
230
+ newWindow: data.newWindow,
231
+ title: (0, _util.stringToPDFString)(title),
232
+ color: rgbColor,
233
+ count: Number.isInteger(count) ? count : undefined,
234
+ bold: !!(flags & 2),
235
+ italic: !!(flags & 1),
116
236
  items: []
117
237
  };
118
- var queue = [{
119
- obj: obj,
120
- parent: root
121
- }];
122
- var processed = new _primitives.RefSet();
123
- processed.put(obj);
124
- var xref = this.xref,
125
- blackColor = new Uint8ClampedArray(3);
126
-
127
- while (queue.length > 0) {
128
- var i = queue.shift();
129
- var outlineDict = xref.fetchIfRef(i.obj);
238
+ i.parent.items.push(outlineItem);
239
+ obj = outlineDict.getRaw("First");
130
240
 
131
- if (outlineDict === null) {
132
- continue;
133
- }
241
+ if ((0, _primitives.isRef)(obj) && !processed.has(obj)) {
242
+ queue.push({
243
+ obj,
244
+ parent: outlineItem
245
+ });
246
+ processed.put(obj);
247
+ }
134
248
 
135
- if (!outlineDict.has('Title')) {
136
- throw new _util.FormatError('Invalid outline item encountered.');
137
- }
249
+ obj = outlineDict.getRaw("Next");
138
250
 
139
- var data = {
140
- url: null,
141
- dest: null
142
- };
143
- Catalog.parseDestDictionary({
144
- destDict: outlineDict,
145
- resultObj: data,
146
- docBaseUrl: this.pdfManager.docBaseUrl
251
+ if ((0, _primitives.isRef)(obj) && !processed.has(obj)) {
252
+ queue.push({
253
+ obj,
254
+ parent: i.parent
147
255
  });
148
- var title = outlineDict.get('Title');
149
- var flags = outlineDict.get('F') || 0;
150
- var color = outlineDict.getArray('C');
151
- var count = outlineDict.get('Count');
152
- var rgbColor = blackColor;
153
-
154
- if (Array.isArray(color) && color.length === 3 && (color[0] !== 0 || color[1] !== 0 || color[2] !== 0)) {
155
- rgbColor = _colorspace.ColorSpace.singletons.rgb.getRgb(color, 0);
156
- }
157
-
158
- var outlineItem = {
159
- dest: data.dest,
160
- url: data.url,
161
- unsafeUrl: data.unsafeUrl,
162
- newWindow: data.newWindow,
163
- title: (0, _util.stringToPDFString)(title),
164
- color: rgbColor,
165
- count: Number.isInteger(count) ? count : undefined,
166
- bold: !!(flags & 2),
167
- italic: !!(flags & 1),
168
- items: []
169
- };
170
- i.parent.items.push(outlineItem);
171
- obj = outlineDict.getRaw('First');
256
+ processed.put(obj);
257
+ }
258
+ }
172
259
 
173
- if ((0, _primitives.isRef)(obj) && !processed.has(obj)) {
174
- queue.push({
175
- obj: obj,
176
- parent: outlineItem
177
- });
178
- processed.put(obj);
179
- }
260
+ return root.items.length > 0 ? root.items : null;
261
+ }
180
262
 
181
- obj = outlineDict.getRaw('Next');
263
+ get permissions() {
264
+ let permissions = null;
182
265
 
183
- if ((0, _primitives.isRef)(obj) && !processed.has(obj)) {
184
- queue.push({
185
- obj: obj,
186
- parent: i.parent
187
- });
188
- processed.put(obj);
189
- }
266
+ try {
267
+ permissions = this._readPermissions();
268
+ } catch (ex) {
269
+ if (ex instanceof _core_utils.MissingDataException) {
270
+ throw ex;
190
271
  }
191
272
 
192
- return root.items.length > 0 ? root.items : null;
273
+ (0, _util.warn)("Unable to read permissions.");
193
274
  }
194
- }, {
195
- key: "_readPermissions",
196
- value: function _readPermissions() {
197
- var encrypt = this.xref.trailer.get('Encrypt');
198
275
 
199
- if (!(0, _primitives.isDict)(encrypt)) {
200
- return null;
201
- }
276
+ return (0, _util.shadow)(this, "permissions", permissions);
277
+ }
202
278
 
203
- var flags = encrypt.get('P');
279
+ _readPermissions() {
280
+ const encrypt = this.xref.trailer.get("Encrypt");
204
281
 
205
- if (!(0, _util.isNum)(flags)) {
206
- return null;
282
+ if (!(0, _primitives.isDict)(encrypt)) {
283
+ return null;
284
+ }
285
+
286
+ let flags = encrypt.get("P");
287
+
288
+ if (!(0, _util.isNum)(flags)) {
289
+ return null;
290
+ }
291
+
292
+ flags += 2 ** 32;
293
+ const permissions = [];
294
+
295
+ for (const key in _util.PermissionFlag) {
296
+ const value = _util.PermissionFlag[key];
297
+
298
+ if (flags & value) {
299
+ permissions.push(value);
207
300
  }
301
+ }
208
302
 
209
- flags += Math.pow(2, 32);
210
- var permissions = [];
303
+ return permissions;
304
+ }
211
305
 
212
- for (var key in _util.PermissionFlag) {
213
- var value = _util.PermissionFlag[key];
306
+ get optionalContentConfig() {
307
+ let config = null;
214
308
 
215
- if (flags & value) {
216
- permissions.push(value);
217
- }
309
+ try {
310
+ const properties = this._catDict.get("OCProperties");
311
+
312
+ if (!properties) {
313
+ return (0, _util.shadow)(this, "optionalContentConfig", null);
218
314
  }
219
315
 
220
- return permissions;
221
- }
222
- }, {
223
- key: "getDestination",
224
- value: function getDestination(destinationId) {
225
- var obj = this._readDests();
316
+ const defaultConfig = properties.get("D");
226
317
 
227
- if (obj instanceof NameTree || obj instanceof _primitives.Dict) {
228
- return fetchDestination(obj.get(destinationId) || null);
318
+ if (!defaultConfig) {
319
+ return (0, _util.shadow)(this, "optionalContentConfig", null);
229
320
  }
230
321
 
231
- return null;
232
- }
233
- }, {
234
- key: "_readDests",
235
- value: function _readDests() {
236
- var obj = this.catDict.get('Names');
322
+ const groupsData = properties.get("OCGs");
237
323
 
238
- if (obj && obj.has('Dests')) {
239
- return new NameTree(obj.getRaw('Dests'), this.xref);
240
- } else if (this.catDict.has('Dests')) {
241
- return this.catDict.get('Dests');
324
+ if (!Array.isArray(groupsData)) {
325
+ return (0, _util.shadow)(this, "optionalContentConfig", null);
242
326
  }
243
327
 
244
- return undefined;
245
- }
246
- }, {
247
- key: "_readPageLabels",
248
- value: function _readPageLabels() {
249
- var obj = this.catDict.getRaw('PageLabels');
328
+ const groups = [];
329
+ const groupRefs = [];
250
330
 
251
- if (!obj) {
252
- return null;
331
+ for (const groupRef of groupsData) {
332
+ if (!(0, _primitives.isRef)(groupRef)) {
333
+ continue;
334
+ }
335
+
336
+ groupRefs.push(groupRef);
337
+ const group = this.xref.fetchIfRef(groupRef);
338
+ groups.push({
339
+ id: groupRef.toString(),
340
+ name: (0, _util.isString)(group.get("Name")) ? (0, _util.stringToPDFString)(group.get("Name")) : null,
341
+ intent: (0, _util.isString)(group.get("Intent")) ? (0, _util.stringToPDFString)(group.get("Intent")) : null
342
+ });
253
343
  }
254
344
 
255
- var pageLabels = new Array(this.numPages);
256
- var style = null,
257
- prefix = '';
258
- var numberTree = new NumberTree(obj, this.xref);
259
- var nums = numberTree.getAll();
260
- var currentLabel = '',
261
- currentIndex = 1;
345
+ config = this._readOptionalContentConfig(defaultConfig, groupRefs);
346
+ config.groups = groups;
347
+ } catch (ex) {
348
+ if (ex instanceof _core_utils.MissingDataException) {
349
+ throw ex;
350
+ }
262
351
 
263
- for (var i = 0, ii = this.numPages; i < ii; i++) {
264
- if (i in nums) {
265
- var labelDict = nums[i];
352
+ (0, _util.warn)(`Unable to read optional content config: ${ex}`);
353
+ }
354
+
355
+ return (0, _util.shadow)(this, "optionalContentConfig", config);
356
+ }
266
357
 
267
- if (!(0, _primitives.isDict)(labelDict)) {
268
- throw new _util.FormatError('PageLabel is not a dictionary.');
358
+ _readOptionalContentConfig(config, contentGroupRefs) {
359
+ function parseOnOff(refs) {
360
+ const onParsed = [];
361
+
362
+ if (Array.isArray(refs)) {
363
+ for (const value of refs) {
364
+ if (!(0, _primitives.isRef)(value)) {
365
+ continue;
269
366
  }
270
367
 
271
- if (labelDict.has('Type') && !(0, _primitives.isName)(labelDict.get('Type'), 'PageLabel')) {
272
- throw new _util.FormatError('Invalid type in PageLabel dictionary.');
368
+ if (contentGroupRefs.includes(value)) {
369
+ onParsed.push(value.toString());
273
370
  }
371
+ }
372
+ }
274
373
 
275
- if (labelDict.has('S')) {
276
- var s = labelDict.get('S');
374
+ return onParsed;
375
+ }
277
376
 
278
- if (!(0, _primitives.isName)(s)) {
279
- throw new _util.FormatError('Invalid style in PageLabel dictionary.');
280
- }
377
+ function parseOrder(refs, nestedLevels = 0) {
378
+ if (!Array.isArray(refs)) {
379
+ return null;
380
+ }
281
381
 
282
- style = s.name;
283
- } else {
284
- style = null;
285
- }
382
+ const order = [];
286
383
 
287
- if (labelDict.has('P')) {
288
- var p = labelDict.get('P');
384
+ for (const value of refs) {
385
+ if ((0, _primitives.isRef)(value) && contentGroupRefs.includes(value)) {
386
+ parsedOrderRefs.put(value);
387
+ order.push(value.toString());
388
+ continue;
389
+ }
289
390
 
290
- if (!(0, _util.isString)(p)) {
291
- throw new _util.FormatError('Invalid prefix in PageLabel dictionary.');
292
- }
391
+ const nestedOrder = parseNestedOrder(value, nestedLevels);
293
392
 
294
- prefix = (0, _util.stringToPDFString)(p);
295
- } else {
296
- prefix = '';
297
- }
393
+ if (nestedOrder) {
394
+ order.push(nestedOrder);
395
+ }
396
+ }
298
397
 
299
- if (labelDict.has('St')) {
300
- var st = labelDict.get('St');
398
+ if (nestedLevels > 0) {
399
+ return order;
400
+ }
301
401
 
302
- if (!(Number.isInteger(st) && st >= 1)) {
303
- throw new _util.FormatError('Invalid start in PageLabel dictionary.');
304
- }
402
+ const hiddenGroups = [];
305
403
 
306
- currentIndex = st;
307
- } else {
308
- currentIndex = 1;
309
- }
404
+ for (const groupRef of contentGroupRefs) {
405
+ if (parsedOrderRefs.has(groupRef)) {
406
+ continue;
310
407
  }
311
408
 
312
- switch (style) {
313
- case 'D':
314
- currentLabel = currentIndex;
315
- break;
409
+ hiddenGroups.push(groupRef.toString());
410
+ }
316
411
 
317
- case 'R':
318
- case 'r':
319
- currentLabel = (0, _core_utils.toRomanNumerals)(currentIndex, style === 'r');
320
- break;
412
+ if (hiddenGroups.length) {
413
+ order.push({
414
+ name: null,
415
+ order: hiddenGroups
416
+ });
417
+ }
321
418
 
322
- case 'A':
323
- case 'a':
324
- var LIMIT = 26;
325
- var A_UPPER_CASE = 0x41,
326
- A_LOWER_CASE = 0x61;
327
- var baseCharCode = style === 'a' ? A_LOWER_CASE : A_UPPER_CASE;
328
- var letterIndex = currentIndex - 1;
329
- var character = String.fromCharCode(baseCharCode + letterIndex % LIMIT);
330
- var charBuf = [];
419
+ return order;
420
+ }
331
421
 
332
- for (var j = 0, jj = letterIndex / LIMIT | 0; j <= jj; j++) {
333
- charBuf.push(character);
334
- }
422
+ function parseNestedOrder(ref, nestedLevels) {
423
+ if (++nestedLevels > MAX_NESTED_LEVELS) {
424
+ (0, _util.warn)("parseNestedOrder - reached MAX_NESTED_LEVELS.");
425
+ return null;
426
+ }
335
427
 
336
- currentLabel = charBuf.join('');
337
- break;
428
+ const value = xref.fetchIfRef(ref);
338
429
 
339
- default:
340
- if (style) {
341
- throw new _util.FormatError("Invalid style \"".concat(style, "\" in PageLabel dictionary."));
342
- }
430
+ if (!Array.isArray(value)) {
431
+ return null;
432
+ }
343
433
 
344
- currentLabel = '';
345
- }
434
+ const nestedName = xref.fetchIfRef(value[0]);
346
435
 
347
- pageLabels[i] = prefix + currentLabel;
348
- currentIndex++;
436
+ if (typeof nestedName !== "string") {
437
+ return null;
349
438
  }
350
439
 
351
- return pageLabels;
440
+ const nestedOrder = parseOrder(value.slice(1), nestedLevels);
441
+
442
+ if (!nestedOrder || !nestedOrder.length) {
443
+ return null;
444
+ }
445
+
446
+ return {
447
+ name: (0, _util.stringToPDFString)(nestedName),
448
+ order: nestedOrder
449
+ };
352
450
  }
353
- }, {
354
- key: "fontFallback",
355
- value: function fontFallback(id, handler) {
356
- var promises = [];
357
- this.fontCache.forEach(function (promise) {
358
- promises.push(promise);
359
- });
360
- return Promise.all(promises).then(function (translatedFonts) {
361
- var _iteratorNormalCompletion = true;
362
- var _didIteratorError = false;
363
- var _iteratorError = undefined;
364
451
 
365
- try {
366
- for (var _iterator = translatedFonts[Symbol.iterator](), _step; !(_iteratorNormalCompletion = (_step = _iterator.next()).done); _iteratorNormalCompletion = true) {
367
- var translatedFont = _step.value;
452
+ const xref = this.xref,
453
+ parsedOrderRefs = new _primitives.RefSet(),
454
+ MAX_NESTED_LEVELS = 10;
455
+ return {
456
+ name: (0, _util.isString)(config.get("Name")) ? (0, _util.stringToPDFString)(config.get("Name")) : null,
457
+ creator: (0, _util.isString)(config.get("Creator")) ? (0, _util.stringToPDFString)(config.get("Creator")) : null,
458
+ baseState: (0, _primitives.isName)(config.get("BaseState")) ? config.get("BaseState").name : null,
459
+ on: parseOnOff(config.get("ON")),
460
+ off: parseOnOff(config.get("OFF")),
461
+ order: parseOrder(config.get("Order")),
462
+ groups: null
463
+ };
464
+ }
368
465
 
369
- if (translatedFont.loadedName === id) {
370
- translatedFont.fallback(handler);
371
- return;
372
- }
373
- }
374
- } catch (err) {
375
- _didIteratorError = true;
376
- _iteratorError = err;
377
- } finally {
378
- try {
379
- if (!_iteratorNormalCompletion && _iterator["return"] != null) {
380
- _iterator["return"]();
381
- }
382
- } finally {
383
- if (_didIteratorError) {
384
- throw _iteratorError;
385
- }
386
- }
466
+ get numPages() {
467
+ const obj = this.toplevelPagesDict.get("Count");
468
+
469
+ if (!Number.isInteger(obj)) {
470
+ throw new _util.FormatError("Page count in top-level pages dictionary is not an integer.");
471
+ }
472
+
473
+ return (0, _util.shadow)(this, "numPages", obj);
474
+ }
475
+
476
+ get destinations() {
477
+ const obj = this._readDests(),
478
+ dests = Object.create(null);
479
+
480
+ if (obj instanceof NameTree) {
481
+ const names = obj.getAll();
482
+
483
+ for (const name in names) {
484
+ dests[name] = fetchDestination(names[name]);
485
+ }
486
+ } else if (obj instanceof _primitives.Dict) {
487
+ obj.forEach(function (key, value) {
488
+ if (value) {
489
+ dests[key] = fetchDestination(value);
387
490
  }
388
491
  });
389
492
  }
390
- }, {
391
- key: "cleanup",
392
- value: function cleanup() {
393
- var _this = this;
394
-
395
- (0, _primitives.clearPrimitiveCaches)();
396
- this.pageKidsCountCache.clear();
397
- var promises = [];
398
- this.fontCache.forEach(function (promise) {
399
- promises.push(promise);
400
- });
401
- return Promise.all(promises).then(function (translatedFonts) {
402
- for (var i = 0, ii = translatedFonts.length; i < ii; i++) {
403
- var font = translatedFonts[i].dict;
404
- delete font.translated;
405
- }
406
493
 
407
- _this.fontCache.clear();
494
+ return (0, _util.shadow)(this, "destinations", dests);
495
+ }
408
496
 
409
- _this.builtInCMapCache.clear();
410
- });
497
+ getDestination(destinationId) {
498
+ const obj = this._readDests();
499
+
500
+ if (obj instanceof NameTree || obj instanceof _primitives.Dict) {
501
+ return fetchDestination(obj.get(destinationId) || null);
411
502
  }
412
- }, {
413
- key: "getPageDict",
414
- value: function getPageDict(pageIndex) {
415
- var capability = (0, _util.createPromiseCapability)();
416
- var nodesToVisit = [this.catDict.getRaw('Pages')];
417
- var xref = this.xref,
418
- pageKidsCountCache = this.pageKidsCountCache;
419
- var count,
420
- currentPageIndex = 0;
421
503
 
422
- function next() {
423
- var _loop = function _loop() {
424
- var currentNode = nodesToVisit.pop();
504
+ return null;
505
+ }
425
506
 
426
- if ((0, _primitives.isRef)(currentNode)) {
427
- count = pageKidsCountCache.get(currentNode);
507
+ _readDests() {
508
+ const obj = this._catDict.get("Names");
428
509
 
429
- if (count > 0 && currentPageIndex + count < pageIndex) {
430
- currentPageIndex += count;
431
- return "continue";
432
- }
510
+ if (obj && obj.has("Dests")) {
511
+ return new NameTree(obj.getRaw("Dests"), this.xref);
512
+ } else if (this._catDict.has("Dests")) {
513
+ return this._catDict.get("Dests");
514
+ }
433
515
 
434
- xref.fetchAsync(currentNode).then(function (obj) {
435
- if ((0, _primitives.isDict)(obj, 'Page') || (0, _primitives.isDict)(obj) && !obj.has('Kids')) {
436
- if (pageIndex === currentPageIndex) {
437
- if (currentNode && !pageKidsCountCache.has(currentNode)) {
438
- pageKidsCountCache.put(currentNode, 1);
439
- }
440
-
441
- capability.resolve([obj, currentNode]);
442
- } else {
443
- currentPageIndex++;
444
- next();
445
- }
516
+ return undefined;
517
+ }
446
518
 
447
- return;
448
- }
519
+ get pageLabels() {
520
+ let obj = null;
449
521
 
450
- nodesToVisit.push(obj);
451
- next();
452
- }, capability.reject);
453
- return {
454
- v: void 0
455
- };
456
- }
522
+ try {
523
+ obj = this._readPageLabels();
524
+ } catch (ex) {
525
+ if (ex instanceof _core_utils.MissingDataException) {
526
+ throw ex;
527
+ }
457
528
 
458
- if (!(0, _primitives.isDict)(currentNode)) {
459
- capability.reject(new _util.FormatError('Page dictionary kid reference points to wrong type of object.'));
460
- return {
461
- v: void 0
462
- };
463
- }
529
+ (0, _util.warn)("Unable to read page labels.");
530
+ }
464
531
 
465
- count = currentNode.get('Count');
532
+ return (0, _util.shadow)(this, "pageLabels", obj);
533
+ }
466
534
 
467
- if (Number.isInteger(count) && count >= 0) {
468
- var objId = currentNode.objId;
535
+ _readPageLabels() {
536
+ const obj = this._catDict.getRaw("PageLabels");
469
537
 
470
- if (objId && !pageKidsCountCache.has(objId)) {
471
- pageKidsCountCache.put(objId, count);
472
- }
538
+ if (!obj) {
539
+ return null;
540
+ }
473
541
 
474
- if (currentPageIndex + count <= pageIndex) {
475
- currentPageIndex += count;
476
- return "continue";
477
- }
478
- }
542
+ const pageLabels = new Array(this.numPages);
543
+ let style = null,
544
+ prefix = "";
545
+ const numberTree = new NumberTree(obj, this.xref);
546
+ const nums = numberTree.getAll();
547
+ let currentLabel = "",
548
+ currentIndex = 1;
479
549
 
480
- var kids = currentNode.get('Kids');
550
+ for (let i = 0, ii = this.numPages; i < ii; i++) {
551
+ if (i in nums) {
552
+ const labelDict = nums[i];
481
553
 
482
- if (!Array.isArray(kids)) {
483
- if ((0, _primitives.isName)(currentNode.get('Type'), 'Page') || !currentNode.has('Type') && currentNode.has('Contents')) {
484
- if (currentPageIndex === pageIndex) {
485
- capability.resolve([currentNode, null]);
486
- return {
487
- v: void 0
488
- };
489
- }
554
+ if (!(0, _primitives.isDict)(labelDict)) {
555
+ throw new _util.FormatError("PageLabel is not a dictionary.");
556
+ }
490
557
 
491
- currentPageIndex++;
492
- return "continue";
493
- }
558
+ if (labelDict.has("Type") && !(0, _primitives.isName)(labelDict.get("Type"), "PageLabel")) {
559
+ throw new _util.FormatError("Invalid type in PageLabel dictionary.");
560
+ }
494
561
 
495
- capability.reject(new _util.FormatError('Page dictionary kids object is not an array.'));
496
- return {
497
- v: void 0
498
- };
499
- }
562
+ if (labelDict.has("S")) {
563
+ const s = labelDict.get("S");
500
564
 
501
- for (var last = kids.length - 1; last >= 0; last--) {
502
- nodesToVisit.push(kids[last]);
565
+ if (!(0, _primitives.isName)(s)) {
566
+ throw new _util.FormatError("Invalid style in PageLabel dictionary.");
503
567
  }
504
- };
505
568
 
506
- while (nodesToVisit.length) {
507
- var _ret = _loop();
569
+ style = s.name;
570
+ } else {
571
+ style = null;
572
+ }
508
573
 
509
- switch (_ret) {
510
- case "continue":
511
- continue;
574
+ if (labelDict.has("P")) {
575
+ const p = labelDict.get("P");
512
576
 
513
- default:
514
- if (_typeof(_ret) === "object") return _ret.v;
577
+ if (!(0, _util.isString)(p)) {
578
+ throw new _util.FormatError("Invalid prefix in PageLabel dictionary.");
515
579
  }
580
+
581
+ prefix = (0, _util.stringToPDFString)(p);
582
+ } else {
583
+ prefix = "";
516
584
  }
517
585
 
518
- capability.reject(new Error("Page index ".concat(pageIndex, " not found.")));
519
- }
586
+ if (labelDict.has("St")) {
587
+ const st = labelDict.get("St");
520
588
 
521
- next();
522
- return capability.promise;
523
- }
524
- }, {
525
- key: "getPageIndex",
526
- value: function getPageIndex(pageRef) {
527
- var xref = this.xref;
528
-
529
- function pagesBeforeRef(kidRef) {
530
- var total = 0,
531
- parentRef;
532
- return xref.fetchAsync(kidRef).then(function (node) {
533
- if ((0, _primitives.isRefsEqual)(kidRef, pageRef) && !(0, _primitives.isDict)(node, 'Page') && !((0, _primitives.isDict)(node) && !node.has('Type') && node.has('Contents'))) {
534
- throw new _util.FormatError('The reference does not point to a /Page dictionary.');
589
+ if (!(Number.isInteger(st) && st >= 1)) {
590
+ throw new _util.FormatError("Invalid start in PageLabel dictionary.");
535
591
  }
536
592
 
537
- if (!node) {
538
- return null;
539
- }
593
+ currentIndex = st;
594
+ } else {
595
+ currentIndex = 1;
596
+ }
597
+ }
540
598
 
541
- if (!(0, _primitives.isDict)(node)) {
542
- throw new _util.FormatError('Node must be a dictionary.');
543
- }
599
+ switch (style) {
600
+ case "D":
601
+ currentLabel = currentIndex;
602
+ break;
544
603
 
545
- parentRef = node.getRaw('Parent');
546
- return node.getAsync('Parent');
547
- }).then(function (parent) {
548
- if (!parent) {
549
- return null;
550
- }
604
+ case "R":
605
+ case "r":
606
+ currentLabel = (0, _core_utils.toRomanNumerals)(currentIndex, style === "r");
607
+ break;
551
608
 
552
- if (!(0, _primitives.isDict)(parent)) {
553
- throw new _util.FormatError('Parent must be a dictionary.');
609
+ case "A":
610
+ case "a":
611
+ const LIMIT = 26;
612
+ const A_UPPER_CASE = 0x41,
613
+ A_LOWER_CASE = 0x61;
614
+ const baseCharCode = style === "a" ? A_LOWER_CASE : A_UPPER_CASE;
615
+ const letterIndex = currentIndex - 1;
616
+ const character = String.fromCharCode(baseCharCode + letterIndex % LIMIT);
617
+ const charBuf = [];
618
+
619
+ for (let j = 0, jj = letterIndex / LIMIT | 0; j <= jj; j++) {
620
+ charBuf.push(character);
554
621
  }
555
622
 
556
- return parent.getAsync('Kids');
557
- }).then(function (kids) {
558
- if (!kids) {
559
- return null;
623
+ currentLabel = charBuf.join("");
624
+ break;
625
+
626
+ default:
627
+ if (style) {
628
+ throw new _util.FormatError(`Invalid style "${style}" in PageLabel dictionary.`);
560
629
  }
561
630
 
562
- var kidPromises = [];
563
- var found = false;
631
+ currentLabel = "";
632
+ }
633
+
634
+ pageLabels[i] = prefix + currentLabel;
635
+ currentIndex++;
636
+ }
637
+
638
+ return pageLabels;
639
+ }
564
640
 
565
- for (var i = 0, ii = kids.length; i < ii; i++) {
566
- var kid = kids[i];
641
+ get pageLayout() {
642
+ const obj = this._catDict.get("PageLayout");
567
643
 
568
- if (!(0, _primitives.isRef)(kid)) {
569
- throw new _util.FormatError('Kid must be a reference.');
644
+ let pageLayout = "";
645
+
646
+ if ((0, _primitives.isName)(obj)) {
647
+ switch (obj.name) {
648
+ case "SinglePage":
649
+ case "OneColumn":
650
+ case "TwoColumnLeft":
651
+ case "TwoColumnRight":
652
+ case "TwoPageLeft":
653
+ case "TwoPageRight":
654
+ pageLayout = obj.name;
655
+ }
656
+ }
657
+
658
+ return (0, _util.shadow)(this, "pageLayout", pageLayout);
659
+ }
660
+
661
+ get pageMode() {
662
+ const obj = this._catDict.get("PageMode");
663
+
664
+ let pageMode = "UseNone";
665
+
666
+ if ((0, _primitives.isName)(obj)) {
667
+ switch (obj.name) {
668
+ case "UseNone":
669
+ case "UseOutlines":
670
+ case "UseThumbs":
671
+ case "FullScreen":
672
+ case "UseOC":
673
+ case "UseAttachments":
674
+ pageMode = obj.name;
675
+ }
676
+ }
677
+
678
+ return (0, _util.shadow)(this, "pageMode", pageMode);
679
+ }
680
+
681
+ get viewerPreferences() {
682
+ const ViewerPreferencesValidators = {
683
+ HideToolbar: _util.isBool,
684
+ HideMenubar: _util.isBool,
685
+ HideWindowUI: _util.isBool,
686
+ FitWindow: _util.isBool,
687
+ CenterWindow: _util.isBool,
688
+ DisplayDocTitle: _util.isBool,
689
+ NonFullScreenPageMode: _primitives.isName,
690
+ Direction: _primitives.isName,
691
+ ViewArea: _primitives.isName,
692
+ ViewClip: _primitives.isName,
693
+ PrintArea: _primitives.isName,
694
+ PrintClip: _primitives.isName,
695
+ PrintScaling: _primitives.isName,
696
+ Duplex: _primitives.isName,
697
+ PickTrayByPDFSize: _util.isBool,
698
+ PrintPageRange: Array.isArray,
699
+ NumCopies: Number.isInteger
700
+ };
701
+
702
+ const obj = this._catDict.get("ViewerPreferences");
703
+
704
+ let prefs = null;
705
+
706
+ if ((0, _primitives.isDict)(obj)) {
707
+ for (const key in ViewerPreferencesValidators) {
708
+ if (!obj.has(key)) {
709
+ continue;
710
+ }
711
+
712
+ const value = obj.get(key);
713
+
714
+ if (!ViewerPreferencesValidators[key](value)) {
715
+ (0, _util.info)(`Bad value in ViewerPreferences for "${key}".`);
716
+ continue;
717
+ }
718
+
719
+ let prefValue;
720
+
721
+ switch (key) {
722
+ case "NonFullScreenPageMode":
723
+ switch (value.name) {
724
+ case "UseNone":
725
+ case "UseOutlines":
726
+ case "UseThumbs":
727
+ case "UseOC":
728
+ prefValue = value.name;
729
+ break;
730
+
731
+ default:
732
+ prefValue = "UseNone";
570
733
  }
571
734
 
572
- if ((0, _primitives.isRefsEqual)(kid, kidRef)) {
573
- found = true;
574
- break;
735
+ break;
736
+
737
+ case "Direction":
738
+ switch (value.name) {
739
+ case "L2R":
740
+ case "R2L":
741
+ prefValue = value.name;
742
+ break;
743
+
744
+ default:
745
+ prefValue = "L2R";
575
746
  }
576
747
 
577
- kidPromises.push(xref.fetchAsync(kid).then(function (kid) {
578
- if (!(0, _primitives.isDict)(kid)) {
579
- throw new _util.FormatError('Kid node must be a dictionary.');
580
- }
748
+ break;
581
749
 
582
- if (kid.has('Count')) {
583
- total += kid.get('Count');
584
- } else {
585
- total++;
586
- }
587
- }));
588
- }
750
+ case "ViewArea":
751
+ case "ViewClip":
752
+ case "PrintArea":
753
+ case "PrintClip":
754
+ switch (value.name) {
755
+ case "MediaBox":
756
+ case "CropBox":
757
+ case "BleedBox":
758
+ case "TrimBox":
759
+ case "ArtBox":
760
+ prefValue = value.name;
761
+ break;
589
762
 
590
- if (!found) {
591
- throw new _util.FormatError('Kid reference not found in parent\'s kids.');
592
- }
763
+ default:
764
+ prefValue = "CropBox";
765
+ }
593
766
 
594
- return Promise.all(kidPromises).then(function () {
595
- return [total, parentRef];
596
- });
597
- });
598
- }
767
+ break;
599
768
 
600
- var total = 0;
769
+ case "PrintScaling":
770
+ switch (value.name) {
771
+ case "None":
772
+ case "AppDefault":
773
+ prefValue = value.name;
774
+ break;
601
775
 
602
- function next(ref) {
603
- return pagesBeforeRef(ref).then(function (args) {
604
- if (!args) {
605
- return total;
606
- }
776
+ default:
777
+ prefValue = "AppDefault";
778
+ }
607
779
 
608
- var _args = _slicedToArray(args, 2),
609
- count = _args[0],
610
- parentRef = _args[1];
780
+ break;
611
781
 
612
- total += count;
613
- return next(parentRef);
614
- });
615
- }
782
+ case "Duplex":
783
+ switch (value.name) {
784
+ case "Simplex":
785
+ case "DuplexFlipShortEdge":
786
+ case "DuplexFlipLongEdge":
787
+ prefValue = value.name;
788
+ break;
616
789
 
617
- return next(pageRef);
618
- }
619
- }, {
620
- key: "metadata",
621
- get: function get() {
622
- var streamRef = this.catDict.getRaw('Metadata');
790
+ default:
791
+ prefValue = "None";
792
+ }
623
793
 
624
- if (!(0, _primitives.isRef)(streamRef)) {
625
- return (0, _util.shadow)(this, 'metadata', null);
626
- }
794
+ break;
627
795
 
628
- var suppressEncryption = !(this.xref.encrypt && this.xref.encrypt.encryptMetadata);
629
- var stream = this.xref.fetch(streamRef, suppressEncryption);
630
- var metadata;
796
+ case "PrintPageRange":
797
+ const length = value.length;
631
798
 
632
- if (stream && (0, _primitives.isDict)(stream.dict)) {
633
- var type = stream.dict.get('Type');
634
- var subtype = stream.dict.get('Subtype');
799
+ if (length % 2 !== 0) {
800
+ break;
801
+ }
635
802
 
636
- if ((0, _primitives.isName)(type, 'Metadata') && (0, _primitives.isName)(subtype, 'XML')) {
637
- try {
638
- metadata = (0, _util.stringToUTF8String)((0, _util.bytesToString)(stream.getBytes()));
639
- } catch (e) {
640
- if (e instanceof _core_utils.MissingDataException) {
641
- throw e;
803
+ const isValid = value.every((page, i, arr) => {
804
+ return Number.isInteger(page) && page > 0 && (i === 0 || page >= arr[i - 1]) && page <= this.numPages;
805
+ });
806
+
807
+ if (isValid) {
808
+ prefValue = value;
642
809
  }
643
810
 
644
- (0, _util.info)('Skipping invalid metadata.');
811
+ break;
812
+
813
+ case "NumCopies":
814
+ if (value > 0) {
815
+ prefValue = value;
816
+ }
817
+
818
+ break;
819
+
820
+ default:
821
+ if (typeof value !== "boolean") {
822
+ throw new _util.FormatError(`viewerPreferences - expected a boolean value for: ${key}`);
823
+ }
824
+
825
+ prefValue = value;
826
+ }
827
+
828
+ if (prefValue !== undefined) {
829
+ if (!prefs) {
830
+ prefs = Object.create(null);
645
831
  }
832
+
833
+ prefs[key] = prefValue;
834
+ } else {
835
+ (0, _util.info)(`Bad value in ViewerPreferences for "${key}".`);
646
836
  }
647
837
  }
648
-
649
- return (0, _util.shadow)(this, 'metadata', metadata);
650
838
  }
651
- }, {
652
- key: "toplevelPagesDict",
653
- get: function get() {
654
- var pagesObj = this.catDict.get('Pages');
655
839
 
656
- if (!(0, _primitives.isDict)(pagesObj)) {
657
- throw new _util.FormatError('Invalid top-level pages dictionary.');
658
- }
840
+ return (0, _util.shadow)(this, "viewerPreferences", prefs);
841
+ }
659
842
 
660
- return (0, _util.shadow)(this, 'toplevelPagesDict', pagesObj);
661
- }
662
- }, {
663
- key: "documentOutline",
664
- get: function get() {
665
- var obj = null;
843
+ get openAction() {
844
+ const obj = this._catDict.get("OpenAction");
666
845
 
667
- try {
668
- obj = this._readDocumentOutline();
669
- } catch (ex) {
670
- if (ex instanceof _core_utils.MissingDataException) {
671
- throw ex;
672
- }
846
+ let openAction = null;
673
847
 
674
- (0, _util.warn)('Unable to read document outline.');
675
- }
848
+ if ((0, _primitives.isDict)(obj)) {
849
+ const destDict = new _primitives.Dict(this.xref);
850
+ destDict.set("A", obj);
851
+ const resultObj = {
852
+ url: null,
853
+ dest: null,
854
+ action: null
855
+ };
856
+ Catalog.parseDestDictionary({
857
+ destDict,
858
+ resultObj
859
+ });
676
860
 
677
- return (0, _util.shadow)(this, 'documentOutline', obj);
678
- }
679
- }, {
680
- key: "permissions",
681
- get: function get() {
682
- var permissions = null;
861
+ if (Array.isArray(resultObj.dest)) {
862
+ if (!openAction) {
863
+ openAction = Object.create(null);
864
+ }
683
865
 
684
- try {
685
- permissions = this._readPermissions();
686
- } catch (ex) {
687
- if (ex instanceof _core_utils.MissingDataException) {
688
- throw ex;
866
+ openAction.dest = resultObj.dest;
867
+ } else if (resultObj.action) {
868
+ if (!openAction) {
869
+ openAction = Object.create(null);
689
870
  }
690
871
 
691
- (0, _util.warn)('Unable to read permissions.');
872
+ openAction.action = resultObj.action;
873
+ }
874
+ } else if (Array.isArray(obj)) {
875
+ if (!openAction) {
876
+ openAction = Object.create(null);
692
877
  }
693
878
 
694
- return (0, _util.shadow)(this, 'permissions', permissions);
879
+ openAction.dest = obj;
695
880
  }
696
- }, {
697
- key: "numPages",
698
- get: function get() {
699
- var obj = this.toplevelPagesDict.get('Count');
700
881
 
701
- if (!Number.isInteger(obj)) {
702
- throw new _util.FormatError('Page count in top-level pages dictionary is not an integer.');
703
- }
882
+ return (0, _util.shadow)(this, "openAction", openAction);
883
+ }
704
884
 
705
- return (0, _util.shadow)(this, 'numPages', obj);
706
- }
707
- }, {
708
- key: "destinations",
709
- get: function get() {
710
- var obj = this._readDests(),
711
- dests = Object.create(null);
885
+ get attachments() {
886
+ const obj = this._catDict.get("Names");
712
887
 
713
- if (obj instanceof NameTree) {
714
- var names = obj.getAll();
888
+ let attachments = null;
715
889
 
716
- for (var name in names) {
717
- dests[name] = fetchDestination(names[name]);
718
- }
719
- } else if (obj instanceof _primitives.Dict) {
720
- obj.forEach(function (key, value) {
721
- if (value) {
722
- dests[key] = fetchDestination(value);
723
- }
724
- });
725
- }
890
+ if (obj && obj.has("EmbeddedFiles")) {
891
+ const nameTree = new NameTree(obj.getRaw("EmbeddedFiles"), this.xref);
892
+ const names = nameTree.getAll();
726
893
 
727
- return (0, _util.shadow)(this, 'destinations', dests);
728
- }
729
- }, {
730
- key: "pageLabels",
731
- get: function get() {
732
- var obj = null;
894
+ for (const name in names) {
895
+ const fs = new FileSpec(names[name], this.xref);
733
896
 
734
- try {
735
- obj = this._readPageLabels();
736
- } catch (ex) {
737
- if (ex instanceof _core_utils.MissingDataException) {
738
- throw ex;
897
+ if (!attachments) {
898
+ attachments = Object.create(null);
739
899
  }
740
900
 
741
- (0, _util.warn)('Unable to read page labels.');
901
+ attachments[(0, _util.stringToPDFString)(name)] = fs.serializable;
742
902
  }
743
-
744
- return (0, _util.shadow)(this, 'pageLabels', obj);
745
903
  }
746
- }, {
747
- key: "pageLayout",
748
- get: function get() {
749
- var obj = this.catDict.get('PageLayout');
750
- var pageLayout = '';
751
904
 
752
- if ((0, _primitives.isName)(obj)) {
753
- switch (obj.name) {
754
- case 'SinglePage':
755
- case 'OneColumn':
756
- case 'TwoColumnLeft':
757
- case 'TwoColumnRight':
758
- case 'TwoPageLeft':
759
- case 'TwoPageRight':
760
- pageLayout = obj.name;
761
- }
762
- }
905
+ return (0, _util.shadow)(this, "attachments", attachments);
906
+ }
763
907
 
764
- return (0, _util.shadow)(this, 'pageLayout', pageLayout);
765
- }
766
- }, {
767
- key: "pageMode",
768
- get: function get() {
769
- var obj = this.catDict.get('PageMode');
770
- var pageMode = 'UseNone';
908
+ get javaScript() {
909
+ const obj = this._catDict.get("Names");
771
910
 
772
- if ((0, _primitives.isName)(obj)) {
773
- switch (obj.name) {
774
- case 'UseNone':
775
- case 'UseOutlines':
776
- case 'UseThumbs':
777
- case 'FullScreen':
778
- case 'UseOC':
779
- case 'UseAttachments':
780
- pageMode = obj.name;
781
- }
782
- }
911
+ let javaScript = null;
783
912
 
784
- return (0, _util.shadow)(this, 'pageMode', pageMode);
785
- }
786
- }, {
787
- key: "viewerPreferences",
788
- get: function get() {
789
- var _this2 = this;
790
-
791
- var ViewerPreferencesValidators = {
792
- HideToolbar: _util.isBool,
793
- HideMenubar: _util.isBool,
794
- HideWindowUI: _util.isBool,
795
- FitWindow: _util.isBool,
796
- CenterWindow: _util.isBool,
797
- DisplayDocTitle: _util.isBool,
798
- NonFullScreenPageMode: _primitives.isName,
799
- Direction: _primitives.isName,
800
- ViewArea: _primitives.isName,
801
- ViewClip: _primitives.isName,
802
- PrintArea: _primitives.isName,
803
- PrintClip: _primitives.isName,
804
- PrintScaling: _primitives.isName,
805
- Duplex: _primitives.isName,
806
- PickTrayByPDFSize: _util.isBool,
807
- PrintPageRange: Array.isArray,
808
- NumCopies: Number.isInteger
809
- };
810
- var obj = this.catDict.get('ViewerPreferences');
811
- var prefs = Object.create(null);
913
+ function appendIfJavaScriptDict(jsDict) {
914
+ const type = jsDict.get("S");
812
915
 
813
- if ((0, _primitives.isDict)(obj)) {
814
- for (var key in ViewerPreferencesValidators) {
815
- if (!obj.has(key)) {
816
- continue;
817
- }
916
+ if (!(0, _primitives.isName)(type, "JavaScript")) {
917
+ return;
918
+ }
818
919
 
819
- var value = obj.get(key);
920
+ let js = jsDict.get("JS");
820
921
 
821
- if (!ViewerPreferencesValidators[key](value)) {
822
- (0, _util.info)("Bad value in ViewerPreferences for \"".concat(key, "\"."));
823
- continue;
824
- }
922
+ if ((0, _primitives.isStream)(js)) {
923
+ js = (0, _util.bytesToString)(js.getBytes());
924
+ } else if (!(0, _util.isString)(js)) {
925
+ return;
926
+ }
825
927
 
826
- var prefValue = void 0;
928
+ if (!javaScript) {
929
+ javaScript = [];
930
+ }
827
931
 
828
- switch (key) {
829
- case 'NonFullScreenPageMode':
830
- switch (value.name) {
831
- case 'UseNone':
832
- case 'UseOutlines':
833
- case 'UseThumbs':
834
- case 'UseOC':
835
- prefValue = value.name;
836
- break;
932
+ javaScript.push((0, _util.stringToPDFString)(js));
933
+ }
837
934
 
838
- default:
839
- prefValue = 'UseNone';
840
- }
935
+ if (obj && obj.has("JavaScript")) {
936
+ const nameTree = new NameTree(obj.getRaw("JavaScript"), this.xref);
937
+ const names = nameTree.getAll();
841
938
 
842
- break;
939
+ for (const name in names) {
940
+ const jsDict = names[name];
843
941
 
844
- case 'Direction':
845
- switch (value.name) {
846
- case 'L2R':
847
- case 'R2L':
848
- prefValue = value.name;
849
- break;
942
+ if ((0, _primitives.isDict)(jsDict)) {
943
+ appendIfJavaScriptDict(jsDict);
944
+ }
945
+ }
946
+ }
850
947
 
851
- default:
852
- prefValue = 'L2R';
853
- }
948
+ const openAction = this._catDict.get("OpenAction");
854
949
 
855
- break;
950
+ if ((0, _primitives.isDict)(openAction) && (0, _primitives.isName)(openAction.get("S"), "JavaScript")) {
951
+ appendIfJavaScriptDict(openAction);
952
+ }
856
953
 
857
- case 'ViewArea':
858
- case 'ViewClip':
859
- case 'PrintArea':
860
- case 'PrintClip':
861
- switch (value.name) {
862
- case 'MediaBox':
863
- case 'CropBox':
864
- case 'BleedBox':
865
- case 'TrimBox':
866
- case 'ArtBox':
867
- prefValue = value.name;
868
- break;
869
-
870
- default:
871
- prefValue = 'CropBox';
872
- }
954
+ return (0, _util.shadow)(this, "javaScript", javaScript);
955
+ }
873
956
 
874
- break;
957
+ fontFallback(id, handler) {
958
+ const promises = [];
959
+ this.fontCache.forEach(function (promise) {
960
+ promises.push(promise);
961
+ });
962
+ return Promise.all(promises).then(translatedFonts => {
963
+ for (const translatedFont of translatedFonts) {
964
+ if (translatedFont.loadedName === id) {
965
+ translatedFont.fallback(handler);
966
+ return;
967
+ }
968
+ }
969
+ });
970
+ }
875
971
 
876
- case 'PrintScaling':
877
- switch (value.name) {
878
- case 'None':
879
- case 'AppDefault':
880
- prefValue = value.name;
881
- break;
972
+ cleanup(manuallyTriggered = false) {
973
+ (0, _primitives.clearPrimitiveCaches)();
974
+ this.globalImageCache.clear(manuallyTriggered);
975
+ this.pageKidsCountCache.clear();
976
+ const promises = [];
977
+ this.fontCache.forEach(function (promise) {
978
+ promises.push(promise);
979
+ });
980
+ return Promise.all(promises).then(translatedFonts => {
981
+ for (const {
982
+ dict
983
+ } of translatedFonts) {
984
+ delete dict.translated;
985
+ }
986
+
987
+ this.fontCache.clear();
988
+ this.builtInCMapCache.clear();
989
+ });
990
+ }
882
991
 
883
- default:
884
- prefValue = 'AppDefault';
885
- }
992
+ getPageDict(pageIndex) {
993
+ const capability = (0, _util.createPromiseCapability)();
994
+ const nodesToVisit = [this._catDict.getRaw("Pages")];
995
+ const visitedNodes = new _primitives.RefSet();
996
+ const xref = this.xref,
997
+ pageKidsCountCache = this.pageKidsCountCache;
998
+ let count,
999
+ currentPageIndex = 0;
886
1000
 
887
- break;
1001
+ function next() {
1002
+ while (nodesToVisit.length) {
1003
+ const currentNode = nodesToVisit.pop();
888
1004
 
889
- case 'Duplex':
890
- switch (value.name) {
891
- case 'Simplex':
892
- case 'DuplexFlipShortEdge':
893
- case 'DuplexFlipLongEdge':
894
- prefValue = value.name;
895
- break;
1005
+ if ((0, _primitives.isRef)(currentNode)) {
1006
+ count = pageKidsCountCache.get(currentNode);
896
1007
 
897
- default:
898
- prefValue = 'None';
899
- }
1008
+ if (count > 0 && currentPageIndex + count < pageIndex) {
1009
+ currentPageIndex += count;
1010
+ continue;
1011
+ }
900
1012
 
901
- break;
1013
+ if (visitedNodes.has(currentNode)) {
1014
+ capability.reject(new _util.FormatError("Pages tree contains circular reference."));
1015
+ return;
1016
+ }
902
1017
 
903
- case 'PrintPageRange':
904
- var length = value.length;
1018
+ visitedNodes.put(currentNode);
1019
+ xref.fetchAsync(currentNode).then(function (obj) {
1020
+ if ((0, _primitives.isDict)(obj, "Page") || (0, _primitives.isDict)(obj) && !obj.has("Kids")) {
1021
+ if (pageIndex === currentPageIndex) {
1022
+ if (currentNode && !pageKidsCountCache.has(currentNode)) {
1023
+ pageKidsCountCache.put(currentNode, 1);
1024
+ }
905
1025
 
906
- if (length % 2 !== 0) {
907
- break;
1026
+ capability.resolve([obj, currentNode]);
1027
+ } else {
1028
+ currentPageIndex++;
1029
+ next();
908
1030
  }
909
1031
 
910
- var isValid = value.every(function (page, i, arr) {
911
- return Number.isInteger(page) && page > 0 && (i === 0 || page >= arr[i - 1]) && page <= _this2.numPages;
912
- });
1032
+ return;
1033
+ }
913
1034
 
914
- if (isValid) {
915
- prefValue = value;
916
- }
1035
+ nodesToVisit.push(obj);
1036
+ next();
1037
+ }, capability.reject);
1038
+ return;
1039
+ }
917
1040
 
918
- break;
1041
+ if (!(0, _primitives.isDict)(currentNode)) {
1042
+ capability.reject(new _util.FormatError("Page dictionary kid reference points to wrong type of object."));
1043
+ return;
1044
+ }
919
1045
 
920
- case 'NumCopies':
921
- if (value > 0) {
922
- prefValue = value;
923
- }
1046
+ count = currentNode.get("Count");
924
1047
 
925
- break;
1048
+ if (Number.isInteger(count) && count >= 0) {
1049
+ const objId = currentNode.objId;
926
1050
 
927
- default:
928
- (0, _util.assert)(typeof value === 'boolean');
929
- prefValue = value;
1051
+ if (objId && !pageKidsCountCache.has(objId)) {
1052
+ pageKidsCountCache.put(objId, count);
930
1053
  }
931
1054
 
932
- if (prefValue !== undefined) {
933
- prefs[key] = prefValue;
934
- } else {
935
- (0, _util.info)("Bad value in ViewerPreferences for \"".concat(key, "\"."));
1055
+ if (currentPageIndex + count <= pageIndex) {
1056
+ currentPageIndex += count;
1057
+ continue;
936
1058
  }
937
1059
  }
938
- }
939
-
940
- return (0, _util.shadow)(this, 'viewerPreferences', prefs);
941
- }
942
- }, {
943
- key: "openActionDestination",
944
- get: function get() {
945
- var obj = this.catDict.get('OpenAction');
946
- var openActionDest = null;
947
-
948
- if ((0, _primitives.isDict)(obj)) {
949
- var destDict = new _primitives.Dict(this.xref);
950
- destDict.set('A', obj);
951
- var resultObj = {
952
- url: null,
953
- dest: null
954
- };
955
- Catalog.parseDestDictionary({
956
- destDict: destDict,
957
- resultObj: resultObj
958
- });
959
-
960
- if (Array.isArray(resultObj.dest)) {
961
- openActionDest = resultObj.dest;
962
- }
963
- } else if (Array.isArray(obj)) {
964
- openActionDest = obj;
965
- }
966
-
967
- return (0, _util.shadow)(this, 'openActionDestination', openActionDest);
968
- }
969
- }, {
970
- key: "attachments",
971
- get: function get() {
972
- var obj = this.catDict.get('Names');
973
- var attachments = null;
974
1060
 
975
- if (obj && obj.has('EmbeddedFiles')) {
976
- var nameTree = new NameTree(obj.getRaw('EmbeddedFiles'), this.xref);
977
- var names = nameTree.getAll();
1061
+ const kids = currentNode.get("Kids");
978
1062
 
979
- for (var name in names) {
980
- var fs = new FileSpec(names[name], this.xref);
1063
+ if (!Array.isArray(kids)) {
1064
+ if ((0, _primitives.isName)(currentNode.get("Type"), "Page") || !currentNode.has("Type") && currentNode.has("Contents")) {
1065
+ if (currentPageIndex === pageIndex) {
1066
+ capability.resolve([currentNode, null]);
1067
+ return;
1068
+ }
981
1069
 
982
- if (!attachments) {
983
- attachments = Object.create(null);
1070
+ currentPageIndex++;
1071
+ continue;
984
1072
  }
985
1073
 
986
- attachments[(0, _util.stringToPDFString)(name)] = fs.serializable;
1074
+ capability.reject(new _util.FormatError("Page dictionary kids object is not an array."));
1075
+ return;
1076
+ }
1077
+
1078
+ for (let last = kids.length - 1; last >= 0; last--) {
1079
+ nodesToVisit.push(kids[last]);
987
1080
  }
988
1081
  }
989
1082
 
990
- return (0, _util.shadow)(this, 'attachments', attachments);
1083
+ capability.reject(new Error(`Page index ${pageIndex} not found.`));
991
1084
  }
992
- }, {
993
- key: "javaScript",
994
- get: function get() {
995
- var obj = this.catDict.get('Names');
996
- var javaScript = null;
997
1085
 
998
- function appendIfJavaScriptDict(jsDict) {
999
- var type = jsDict.get('S');
1086
+ next();
1087
+ return capability.promise;
1088
+ }
1000
1089
 
1001
- if (!(0, _primitives.isName)(type, 'JavaScript')) {
1002
- return;
1090
+ getPageIndex(pageRef) {
1091
+ const xref = this.xref;
1092
+
1093
+ function pagesBeforeRef(kidRef) {
1094
+ let total = 0,
1095
+ parentRef;
1096
+ return xref.fetchAsync(kidRef).then(function (node) {
1097
+ if ((0, _primitives.isRefsEqual)(kidRef, pageRef) && !(0, _primitives.isDict)(node, "Page") && !((0, _primitives.isDict)(node) && !node.has("Type") && node.has("Contents"))) {
1098
+ throw new _util.FormatError("The reference does not point to a /Page dictionary.");
1003
1099
  }
1004
1100
 
1005
- var js = jsDict.get('JS');
1101
+ if (!node) {
1102
+ return null;
1103
+ }
1006
1104
 
1007
- if ((0, _primitives.isStream)(js)) {
1008
- js = (0, _util.bytesToString)(js.getBytes());
1009
- } else if (!(0, _util.isString)(js)) {
1010
- return;
1105
+ if (!(0, _primitives.isDict)(node)) {
1106
+ throw new _util.FormatError("Node must be a dictionary.");
1011
1107
  }
1012
1108
 
1013
- if (!javaScript) {
1014
- javaScript = [];
1109
+ parentRef = node.getRaw("Parent");
1110
+ return node.getAsync("Parent");
1111
+ }).then(function (parent) {
1112
+ if (!parent) {
1113
+ return null;
1015
1114
  }
1016
1115
 
1017
- javaScript.push((0, _util.stringToPDFString)(js));
1018
- }
1116
+ if (!(0, _primitives.isDict)(parent)) {
1117
+ throw new _util.FormatError("Parent must be a dictionary.");
1118
+ }
1019
1119
 
1020
- if (obj && obj.has('JavaScript')) {
1021
- var nameTree = new NameTree(obj.getRaw('JavaScript'), this.xref);
1022
- var names = nameTree.getAll();
1120
+ return parent.getAsync("Kids");
1121
+ }).then(function (kids) {
1122
+ if (!kids) {
1123
+ return null;
1124
+ }
1023
1125
 
1024
- for (var name in names) {
1025
- var jsDict = names[name];
1126
+ const kidPromises = [];
1127
+ let found = false;
1026
1128
 
1027
- if ((0, _primitives.isDict)(jsDict)) {
1028
- appendIfJavaScriptDict(jsDict);
1029
- }
1030
- }
1031
- }
1129
+ for (let i = 0, ii = kids.length; i < ii; i++) {
1130
+ const kid = kids[i];
1032
1131
 
1033
- var openActionDict = this.catDict.get('OpenAction');
1132
+ if (!(0, _primitives.isRef)(kid)) {
1133
+ throw new _util.FormatError("Kid must be a reference.");
1134
+ }
1034
1135
 
1035
- if ((0, _primitives.isDict)(openActionDict, 'Action')) {
1036
- var actionType = openActionDict.get('S');
1136
+ if ((0, _primitives.isRefsEqual)(kid, kidRef)) {
1137
+ found = true;
1138
+ break;
1139
+ }
1037
1140
 
1038
- if ((0, _primitives.isName)(actionType, 'Named')) {
1039
- var action = openActionDict.get('N');
1141
+ kidPromises.push(xref.fetchAsync(kid).then(function (obj) {
1142
+ if (!(0, _primitives.isDict)(obj)) {
1143
+ throw new _util.FormatError("Kid node must be a dictionary.");
1144
+ }
1040
1145
 
1041
- if ((0, _primitives.isName)(action, 'Print')) {
1042
- if (!javaScript) {
1043
- javaScript = [];
1146
+ if (obj.has("Count")) {
1147
+ total += obj.get("Count");
1148
+ } else {
1149
+ total++;
1044
1150
  }
1151
+ }));
1152
+ }
1045
1153
 
1046
- javaScript.push('print({});');
1047
- }
1048
- } else {
1049
- appendIfJavaScriptDict(openActionDict);
1154
+ if (!found) {
1155
+ throw new _util.FormatError("Kid reference not found in parent's kids.");
1050
1156
  }
1051
- }
1052
1157
 
1053
- return (0, _util.shadow)(this, 'javaScript', javaScript);
1158
+ return Promise.all(kidPromises).then(function () {
1159
+ return [total, parentRef];
1160
+ });
1161
+ });
1054
1162
  }
1055
- }], [{
1056
- key: "parseDestDictionary",
1057
- value: function parseDestDictionary(params) {
1058
- function addDefaultProtocolToUrl(url) {
1059
- return url.startsWith('www.') ? "http://".concat(url) : url;
1060
- }
1061
1163
 
1062
- function tryConvertUrlEncoding(url) {
1063
- try {
1064
- return (0, _util.stringToUTF8String)(url);
1065
- } catch (e) {
1066
- return url;
1164
+ let total = 0;
1165
+
1166
+ function next(ref) {
1167
+ return pagesBeforeRef(ref).then(function (args) {
1168
+ if (!args) {
1169
+ return total;
1067
1170
  }
1068
- }
1069
1171
 
1070
- var destDict = params.destDict;
1172
+ const [count, parentRef] = args;
1173
+ total += count;
1174
+ return next(parentRef);
1175
+ });
1176
+ }
1071
1177
 
1072
- if (!(0, _primitives.isDict)(destDict)) {
1073
- (0, _util.warn)('parseDestDictionary: `destDict` must be a dictionary.');
1074
- return;
1075
- }
1178
+ return next(pageRef);
1179
+ }
1076
1180
 
1077
- var resultObj = params.resultObj;
1181
+ static parseDestDictionary(params) {
1182
+ function addDefaultProtocolToUrl(url) {
1183
+ return url.startsWith("www.") ? `http://${url}` : url;
1184
+ }
1078
1185
 
1079
- if (_typeof(resultObj) !== 'object') {
1080
- (0, _util.warn)('parseDestDictionary: `resultObj` must be an object.');
1081
- return;
1186
+ function tryConvertUrlEncoding(url) {
1187
+ try {
1188
+ return (0, _util.stringToUTF8String)(url);
1189
+ } catch (e) {
1190
+ return url;
1082
1191
  }
1192
+ }
1083
1193
 
1084
- var docBaseUrl = params.docBaseUrl || null;
1085
- var action = destDict.get('A'),
1086
- url,
1087
- dest;
1194
+ const destDict = params.destDict;
1088
1195
 
1089
- if (!(0, _primitives.isDict)(action) && destDict.has('Dest')) {
1090
- action = destDict.get('Dest');
1091
- }
1196
+ if (!(0, _primitives.isDict)(destDict)) {
1197
+ (0, _util.warn)("parseDestDictionary: `destDict` must be a dictionary.");
1198
+ return;
1199
+ }
1092
1200
 
1093
- if ((0, _primitives.isDict)(action)) {
1094
- var actionType = action.get('S');
1201
+ const resultObj = params.resultObj;
1095
1202
 
1096
- if (!(0, _primitives.isName)(actionType)) {
1097
- (0, _util.warn)('parseDestDictionary: Invalid type in Action dictionary.');
1098
- return;
1099
- }
1203
+ if (typeof resultObj !== "object") {
1204
+ (0, _util.warn)("parseDestDictionary: `resultObj` must be an object.");
1205
+ return;
1206
+ }
1100
1207
 
1101
- var actionName = actionType.name;
1208
+ const docBaseUrl = params.docBaseUrl || null;
1209
+ let action = destDict.get("A"),
1210
+ url,
1211
+ dest;
1102
1212
 
1103
- switch (actionName) {
1104
- case 'URI':
1105
- url = action.get('URI');
1213
+ if (!(0, _primitives.isDict)(action) && destDict.has("Dest")) {
1214
+ action = destDict.get("Dest");
1215
+ }
1106
1216
 
1107
- if ((0, _primitives.isName)(url)) {
1108
- url = '/' + url.name;
1109
- } else if ((0, _util.isString)(url)) {
1110
- url = addDefaultProtocolToUrl(url);
1111
- }
1217
+ if ((0, _primitives.isDict)(action)) {
1218
+ const actionType = action.get("S");
1112
1219
 
1113
- break;
1220
+ if (!(0, _primitives.isName)(actionType)) {
1221
+ (0, _util.warn)("parseDestDictionary: Invalid type in Action dictionary.");
1222
+ return;
1223
+ }
1114
1224
 
1115
- case 'GoTo':
1116
- dest = action.get('D');
1117
- break;
1225
+ const actionName = actionType.name;
1118
1226
 
1119
- case 'Launch':
1120
- case 'GoToR':
1121
- var urlDict = action.get('F');
1227
+ switch (actionName) {
1228
+ case "URI":
1229
+ url = action.get("URI");
1122
1230
 
1123
- if ((0, _primitives.isDict)(urlDict)) {
1124
- url = urlDict.get('F') || null;
1125
- } else if ((0, _util.isString)(urlDict)) {
1126
- url = urlDict;
1127
- }
1231
+ if ((0, _primitives.isName)(url)) {
1232
+ url = "/" + url.name;
1233
+ } else if ((0, _util.isString)(url)) {
1234
+ url = addDefaultProtocolToUrl(url);
1235
+ }
1128
1236
 
1129
- var remoteDest = action.get('D');
1237
+ break;
1130
1238
 
1131
- if (remoteDest) {
1132
- if ((0, _primitives.isName)(remoteDest)) {
1133
- remoteDest = remoteDest.name;
1134
- }
1239
+ case "GoTo":
1240
+ dest = action.get("D");
1241
+ break;
1135
1242
 
1136
- if ((0, _util.isString)(url)) {
1137
- var baseUrl = url.split('#')[0];
1243
+ case "Launch":
1244
+ case "GoToR":
1245
+ const urlDict = action.get("F");
1138
1246
 
1139
- if ((0, _util.isString)(remoteDest)) {
1140
- url = baseUrl + '#' + remoteDest;
1141
- } else if (Array.isArray(remoteDest)) {
1142
- url = baseUrl + '#' + JSON.stringify(remoteDest);
1143
- }
1144
- }
1247
+ if ((0, _primitives.isDict)(urlDict)) {
1248
+ url = urlDict.get("F") || null;
1249
+ } else if ((0, _util.isString)(urlDict)) {
1250
+ url = urlDict;
1251
+ }
1252
+
1253
+ let remoteDest = action.get("D");
1254
+
1255
+ if (remoteDest) {
1256
+ if ((0, _primitives.isName)(remoteDest)) {
1257
+ remoteDest = remoteDest.name;
1145
1258
  }
1146
1259
 
1147
- var newWindow = action.get('NewWindow');
1260
+ if ((0, _util.isString)(url)) {
1261
+ const baseUrl = url.split("#")[0];
1148
1262
 
1149
- if ((0, _util.isBool)(newWindow)) {
1150
- resultObj.newWindow = newWindow;
1263
+ if ((0, _util.isString)(remoteDest)) {
1264
+ url = baseUrl + "#" + remoteDest;
1265
+ } else if (Array.isArray(remoteDest)) {
1266
+ url = baseUrl + "#" + JSON.stringify(remoteDest);
1267
+ }
1151
1268
  }
1269
+ }
1152
1270
 
1153
- break;
1271
+ const newWindow = action.get("NewWindow");
1154
1272
 
1155
- case 'Named':
1156
- var namedAction = action.get('N');
1273
+ if ((0, _util.isBool)(newWindow)) {
1274
+ resultObj.newWindow = newWindow;
1275
+ }
1157
1276
 
1158
- if ((0, _primitives.isName)(namedAction)) {
1159
- resultObj.action = namedAction.name;
1160
- }
1277
+ break;
1161
1278
 
1162
- break;
1279
+ case "Named":
1280
+ const namedAction = action.get("N");
1281
+
1282
+ if ((0, _primitives.isName)(namedAction)) {
1283
+ resultObj.action = namedAction.name;
1284
+ }
1163
1285
 
1164
- case 'JavaScript':
1165
- var jsAction = action.get('JS');
1166
- var js;
1286
+ break;
1167
1287
 
1168
- if ((0, _primitives.isStream)(jsAction)) {
1169
- js = (0, _util.bytesToString)(jsAction.getBytes());
1170
- } else if ((0, _util.isString)(jsAction)) {
1171
- js = jsAction;
1172
- }
1288
+ case "JavaScript":
1289
+ const jsAction = action.get("JS");
1290
+ let js;
1173
1291
 
1174
- if (js) {
1175
- var URL_OPEN_METHODS = ['app.launchURL', 'window.open'];
1176
- var regex = new RegExp('^\\s*(' + URL_OPEN_METHODS.join('|').split('.').join('\\.') + ')\\((?:\'|\")([^\'\"]*)(?:\'|\")(?:,\\s*(\\w+)\\)|\\))', 'i');
1177
- var jsUrl = regex.exec((0, _util.stringToPDFString)(js));
1292
+ if ((0, _primitives.isStream)(jsAction)) {
1293
+ js = (0, _util.bytesToString)(jsAction.getBytes());
1294
+ } else if ((0, _util.isString)(jsAction)) {
1295
+ js = jsAction;
1296
+ }
1178
1297
 
1179
- if (jsUrl && jsUrl[2]) {
1180
- url = jsUrl[2];
1298
+ if (js) {
1299
+ const URL_OPEN_METHODS = ["app.launchURL", "window.open"];
1300
+ const regex = new RegExp("^\\s*(" + URL_OPEN_METHODS.join("|").split(".").join("\\.") + ")\\((?:'|\")([^'\"]*)(?:'|\")(?:,\\s*(\\w+)\\)|\\))", "i");
1301
+ const jsUrl = regex.exec((0, _util.stringToPDFString)(js));
1181
1302
 
1182
- if (jsUrl[3] === 'true' && jsUrl[1] === 'app.launchURL') {
1183
- resultObj.newWindow = true;
1184
- }
1303
+ if (jsUrl && jsUrl[2]) {
1304
+ url = jsUrl[2];
1185
1305
 
1186
- break;
1306
+ if (jsUrl[3] === "true" && jsUrl[1] === "app.launchURL") {
1307
+ resultObj.newWindow = true;
1187
1308
  }
1309
+
1310
+ break;
1188
1311
  }
1312
+ }
1189
1313
 
1190
- default:
1191
- (0, _util.warn)("parseDestDictionary: unsupported action type \"".concat(actionName, "\"."));
1192
- break;
1193
- }
1194
- } else if (destDict.has('Dest')) {
1195
- dest = destDict.get('Dest');
1314
+ default:
1315
+ (0, _util.warn)(`parseDestDictionary: unsupported action type "${actionName}".`);
1316
+ break;
1196
1317
  }
1318
+ } else if (destDict.has("Dest")) {
1319
+ dest = destDict.get("Dest");
1320
+ }
1197
1321
 
1198
- if ((0, _util.isString)(url)) {
1199
- url = tryConvertUrlEncoding(url);
1200
- var absoluteUrl = (0, _util.createValidAbsoluteUrl)(url, docBaseUrl);
1201
-
1202
- if (absoluteUrl) {
1203
- resultObj.url = absoluteUrl.href;
1204
- }
1322
+ if ((0, _util.isString)(url)) {
1323
+ url = tryConvertUrlEncoding(url);
1324
+ const absoluteUrl = (0, _util.createValidAbsoluteUrl)(url, docBaseUrl);
1205
1325
 
1206
- resultObj.unsafeUrl = url;
1326
+ if (absoluteUrl) {
1327
+ resultObj.url = absoluteUrl.href;
1207
1328
  }
1208
1329
 
1209
- if (dest) {
1210
- if ((0, _primitives.isName)(dest)) {
1211
- dest = dest.name;
1212
- }
1330
+ resultObj.unsafeUrl = url;
1331
+ }
1213
1332
 
1214
- if ((0, _util.isString)(dest) || Array.isArray(dest)) {
1215
- resultObj.dest = dest;
1216
- }
1333
+ if (dest) {
1334
+ if ((0, _primitives.isName)(dest)) {
1335
+ dest = dest.name;
1336
+ }
1337
+
1338
+ if ((0, _util.isString)(dest) || Array.isArray(dest)) {
1339
+ resultObj.dest = dest;
1217
1340
  }
1218
1341
  }
1219
- }]);
1342
+ }
1220
1343
 
1221
- return Catalog;
1222
- }();
1344
+ }
1223
1345
 
1224
1346
  exports.Catalog = Catalog;
1225
1347
 
@@ -1229,14 +1351,25 @@ var XRef = function XRefClosure() {
1229
1351
  this.pdfManager = pdfManager;
1230
1352
  this.entries = [];
1231
1353
  this.xrefstms = Object.create(null);
1232
- this.cache = [];
1354
+ this._cacheMap = new Map();
1233
1355
  this.stats = {
1234
- streamTypes: [],
1235
- fontTypes: []
1356
+ streamTypes: Object.create(null),
1357
+ fontTypes: Object.create(null)
1236
1358
  };
1359
+ this._newRefNum = null;
1237
1360
  }
1238
1361
 
1239
1362
  XRef.prototype = {
1363
+ getNewRef: function XRef_getNewRef() {
1364
+ if (this._newRefNum === null) {
1365
+ this._newRefNum = this.entries.length;
1366
+ }
1367
+
1368
+ return _primitives.Ref.get(this._newRefNum++, 0);
1369
+ },
1370
+ resetNewRef: function XRef_resetNewRef() {
1371
+ this._newRefNum = null;
1372
+ },
1240
1373
  setStartXRef: function XRef_setStartXRef(startXRef) {
1241
1374
  this.startXRefQueue = [startXRef];
1242
1375
  },
@@ -1246,55 +1379,55 @@ var XRef = function XRefClosure() {
1246
1379
  if (!recoveryMode) {
1247
1380
  trailerDict = this.readXRef();
1248
1381
  } else {
1249
- (0, _util.warn)('Indexing all PDF objects');
1382
+ (0, _util.warn)("Indexing all PDF objects");
1250
1383
  trailerDict = this.indexObjects();
1251
1384
  }
1252
1385
 
1253
1386
  trailerDict.assignXref(this);
1254
1387
  this.trailer = trailerDict;
1255
- var encrypt;
1388
+ let encrypt;
1256
1389
 
1257
1390
  try {
1258
- encrypt = trailerDict.get('Encrypt');
1391
+ encrypt = trailerDict.get("Encrypt");
1259
1392
  } catch (ex) {
1260
1393
  if (ex instanceof _core_utils.MissingDataException) {
1261
1394
  throw ex;
1262
1395
  }
1263
1396
 
1264
- (0, _util.warn)("XRef.parse - Invalid \"Encrypt\" reference: \"".concat(ex, "\"."));
1397
+ (0, _util.warn)(`XRef.parse - Invalid "Encrypt" reference: "${ex}".`);
1265
1398
  }
1266
1399
 
1267
1400
  if ((0, _primitives.isDict)(encrypt)) {
1268
- var ids = trailerDict.get('ID');
1269
- var fileId = ids && ids.length ? ids[0] : '';
1401
+ var ids = trailerDict.get("ID");
1402
+ var fileId = ids && ids.length ? ids[0] : "";
1270
1403
  encrypt.suppressEncryption = true;
1271
1404
  this.encrypt = new _crypto.CipherTransformFactory(encrypt, fileId, this.pdfManager.password);
1272
1405
  }
1273
1406
 
1274
- var root;
1407
+ let root;
1275
1408
 
1276
1409
  try {
1277
- root = trailerDict.get('Root');
1410
+ root = trailerDict.get("Root");
1278
1411
  } catch (ex) {
1279
1412
  if (ex instanceof _core_utils.MissingDataException) {
1280
1413
  throw ex;
1281
1414
  }
1282
1415
 
1283
- (0, _util.warn)("XRef.parse - Invalid \"Root\" reference: \"".concat(ex, "\"."));
1416
+ (0, _util.warn)(`XRef.parse - Invalid "Root" reference: "${ex}".`);
1284
1417
  }
1285
1418
 
1286
- if ((0, _primitives.isDict)(root) && root.has('Pages')) {
1419
+ if ((0, _primitives.isDict)(root) && root.has("Pages")) {
1287
1420
  this.root = root;
1288
1421
  } else {
1289
1422
  if (!recoveryMode) {
1290
1423
  throw new _core_utils.XRefParseException();
1291
1424
  }
1292
1425
 
1293
- throw new _util.FormatError('Invalid root reference');
1426
+ throw new _util.FormatError("Invalid root reference");
1294
1427
  }
1295
1428
  },
1296
1429
  processXRefTable: function XRef_processXRefTable(parser) {
1297
- if (!('tableState' in this)) {
1430
+ if (!("tableState" in this)) {
1298
1431
  this.tableState = {
1299
1432
  entryNum: 0,
1300
1433
  streamPos: parser.lexer.stream.pos,
@@ -1305,8 +1438,8 @@ var XRef = function XRefClosure() {
1305
1438
 
1306
1439
  var obj = this.readXRefTable(parser);
1307
1440
 
1308
- if (!(0, _primitives.isCmd)(obj, 'trailer')) {
1309
- throw new _util.FormatError('Invalid XRef table: could not find trailer dictionary');
1441
+ if (!(0, _primitives.isCmd)(obj, "trailer")) {
1442
+ throw new _util.FormatError("Invalid XRef table: could not find trailer dictionary");
1310
1443
  }
1311
1444
 
1312
1445
  var dict = parser.getObj();
@@ -1316,7 +1449,7 @@ var XRef = function XRefClosure() {
1316
1449
  }
1317
1450
 
1318
1451
  if (!(0, _primitives.isDict)(dict)) {
1319
- throw new _util.FormatError('Invalid XRef table: could not parse trailer dictionary');
1452
+ throw new _util.FormatError("Invalid XRef table: could not parse trailer dictionary");
1320
1453
  }
1321
1454
 
1322
1455
  delete this.tableState;
@@ -1331,8 +1464,8 @@ var XRef = function XRefClosure() {
1331
1464
  var obj;
1332
1465
 
1333
1466
  while (true) {
1334
- if (!('firstEntryNum' in tableState) || !('entryCount' in tableState)) {
1335
- if ((0, _primitives.isCmd)(obj = parser.getObj(), 'trailer')) {
1467
+ if (!("firstEntryNum" in tableState) || !("entryCount" in tableState)) {
1468
+ if ((0, _primitives.isCmd)(obj = parser.getObj(), "trailer")) {
1336
1469
  break;
1337
1470
  }
1338
1471
 
@@ -1344,7 +1477,7 @@ var XRef = function XRefClosure() {
1344
1477
  var count = tableState.entryCount;
1345
1478
 
1346
1479
  if (!Number.isInteger(first) || !Number.isInteger(count)) {
1347
- throw new _util.FormatError('Invalid XRef table: wrong types in subsection header');
1480
+ throw new _util.FormatError("Invalid XRef table: wrong types in subsection header");
1348
1481
  }
1349
1482
 
1350
1483
  for (var i = tableState.entryNum; i < count; i++) {
@@ -1359,18 +1492,18 @@ var XRef = function XRefClosure() {
1359
1492
 
1360
1493
  if (type instanceof _primitives.Cmd) {
1361
1494
  switch (type.cmd) {
1362
- case 'f':
1495
+ case "f":
1363
1496
  entry.free = true;
1364
1497
  break;
1365
1498
 
1366
- case 'n':
1499
+ case "n":
1367
1500
  entry.uncompressed = true;
1368
1501
  break;
1369
1502
  }
1370
1503
  }
1371
1504
 
1372
1505
  if (!Number.isInteger(entry.offset) || !Number.isInteger(entry.gen) || !(entry.free || entry.uncompressed)) {
1373
- throw new _util.FormatError("Invalid entry in XRef subsection: ".concat(first, ", ").concat(count));
1506
+ throw new _util.FormatError(`Invalid entry in XRef subsection: ${first}, ${count}`);
1374
1507
  }
1375
1508
 
1376
1509
  if (i === 0 && entry.free && first === 1) {
@@ -1391,24 +1524,24 @@ var XRef = function XRefClosure() {
1391
1524
  }
1392
1525
 
1393
1526
  if (this.entries[0] && !this.entries[0].free) {
1394
- throw new _util.FormatError('Invalid XRef table: unexpected first object');
1527
+ throw new _util.FormatError("Invalid XRef table: unexpected first object");
1395
1528
  }
1396
1529
 
1397
1530
  return obj;
1398
1531
  },
1399
1532
  processXRefStream: function XRef_processXRefStream(stream) {
1400
- if (!('streamState' in this)) {
1533
+ if (!("streamState" in this)) {
1401
1534
  var streamParameters = stream.dict;
1402
- var byteWidths = streamParameters.get('W');
1403
- var range = streamParameters.get('Index');
1535
+ var byteWidths = streamParameters.get("W");
1536
+ var range = streamParameters.get("Index");
1404
1537
 
1405
1538
  if (!range) {
1406
- range = [0, streamParameters.get('Size')];
1539
+ range = [0, streamParameters.get("Size")];
1407
1540
  }
1408
1541
 
1409
1542
  this.streamState = {
1410
1543
  entryRanges: range,
1411
- byteWidths: byteWidths,
1544
+ byteWidths,
1412
1545
  entryNum: 0,
1413
1546
  streamPos: stream.pos
1414
1547
  };
@@ -1433,11 +1566,11 @@ var XRef = function XRefClosure() {
1433
1566
  var n = entryRanges[1];
1434
1567
 
1435
1568
  if (!Number.isInteger(first) || !Number.isInteger(n)) {
1436
- throw new _util.FormatError("Invalid XRef range fields: ".concat(first, ", ").concat(n));
1569
+ throw new _util.FormatError(`Invalid XRef range fields: ${first}, ${n}`);
1437
1570
  }
1438
1571
 
1439
1572
  if (!Number.isInteger(typeFieldWidth) || !Number.isInteger(offsetFieldWidth) || !Number.isInteger(generationFieldWidth)) {
1440
- throw new _util.FormatError("Invalid XRef entry fields length: ".concat(first, ", ").concat(n));
1573
+ throw new _util.FormatError(`Invalid XRef entry fields length: ${first}, ${n}`);
1441
1574
  }
1442
1575
 
1443
1576
  for (i = streamState.entryNum; i < n; ++i) {
@@ -1480,7 +1613,7 @@ var XRef = function XRefClosure() {
1480
1613
  break;
1481
1614
 
1482
1615
  default:
1483
- throw new _util.FormatError("Invalid XRef entry type: ".concat(type));
1616
+ throw new _util.FormatError(`Invalid XRef entry type: ${type}`);
1484
1617
  }
1485
1618
 
1486
1619
  if (!this.entries[first + i]) {
@@ -1495,14 +1628,14 @@ var XRef = function XRefClosure() {
1495
1628
  },
1496
1629
  indexObjects: function XRef_indexObjects() {
1497
1630
  var TAB = 0x9,
1498
- LF = 0xA,
1499
- CR = 0xD,
1631
+ LF = 0xa,
1632
+ CR = 0xd,
1500
1633
  SPACE = 0x20;
1501
1634
  var PERCENT = 0x25,
1502
- LT = 0x3C;
1635
+ LT = 0x3c;
1503
1636
 
1504
1637
  function readToken(data, offset) {
1505
- var token = '',
1638
+ var token = "",
1506
1639
  ch = data[offset];
1507
1640
 
1508
1641
  while (ch !== LF && ch !== CR && ch !== LT) {
@@ -1541,12 +1674,12 @@ var XRef = function XRefClosure() {
1541
1674
  }
1542
1675
 
1543
1676
  var objRegExp = /^(\d+)\s+(\d+)\s+obj\b/;
1544
- var endobjRegExp = /\bendobj[\b\s]$/;
1545
- var nestedObjRegExp = /\s+(\d+\s+\d+\s+obj[\b\s<])$/;
1546
- var CHECK_CONTENT_LENGTH = 25;
1677
+ const endobjRegExp = /\bendobj[\b\s]$/;
1678
+ const nestedObjRegExp = /\s+(\d+\s+\d+\s+obj[\b\s<])$/;
1679
+ const CHECK_CONTENT_LENGTH = 25;
1547
1680
  var trailerBytes = new Uint8Array([116, 114, 97, 105, 108, 101, 114]);
1548
1681
  var startxrefBytes = new Uint8Array([115, 116, 97, 114, 116, 120, 114, 101, 102]);
1549
- var objBytes = new Uint8Array([111, 98, 106]);
1682
+ const objBytes = new Uint8Array([111, 98, 106]);
1550
1683
  var xrefBytes = new Uint8Array([47, 88, 82, 101, 102]);
1551
1684
  this.entries.length = 0;
1552
1685
  var stream = this.stream;
@@ -1582,35 +1715,35 @@ var XRef = function XRefClosure() {
1582
1715
  var token = readToken(buffer, position);
1583
1716
  var m;
1584
1717
 
1585
- if (token.startsWith('xref') && (token.length === 4 || /\s/.test(token[4]))) {
1718
+ if (token.startsWith("xref") && (token.length === 4 || /\s/.test(token[4]))) {
1586
1719
  position += skipUntil(buffer, position, trailerBytes);
1587
1720
  trailers.push(position);
1588
1721
  position += skipUntil(buffer, position, startxrefBytes);
1589
1722
  } else if (m = objRegExp.exec(token)) {
1590
- var num = m[1] | 0,
1591
- gen = m[2] | 0;
1723
+ const num = m[1] | 0,
1724
+ gen = m[2] | 0;
1592
1725
 
1593
- if (typeof this.entries[num] === 'undefined') {
1726
+ if (!this.entries[num] || this.entries[num].gen === gen) {
1594
1727
  this.entries[num] = {
1595
1728
  offset: position - stream.start,
1596
- gen: gen,
1729
+ gen,
1597
1730
  uncompressed: true
1598
1731
  };
1599
1732
  }
1600
1733
 
1601
- var contentLength = void 0,
1734
+ let contentLength,
1602
1735
  startPos = position + token.length;
1603
1736
 
1604
1737
  while (startPos < buffer.length) {
1605
- var endPos = startPos + skipUntil(buffer, startPos, objBytes) + 4;
1738
+ const endPos = startPos + skipUntil(buffer, startPos, objBytes) + 4;
1606
1739
  contentLength = endPos - position;
1607
- var checkPos = Math.max(endPos - CHECK_CONTENT_LENGTH, startPos);
1608
- var tokenStr = (0, _util.bytesToString)(buffer.subarray(checkPos, endPos));
1740
+ const checkPos = Math.max(endPos - CHECK_CONTENT_LENGTH, startPos);
1741
+ const tokenStr = (0, _util.bytesToString)(buffer.subarray(checkPos, endPos));
1609
1742
 
1610
1743
  if (endobjRegExp.test(tokenStr)) {
1611
1744
  break;
1612
1745
  } else {
1613
- var objToken = nestedObjRegExp.exec(tokenStr);
1746
+ const objToken = nestedObjRegExp.exec(tokenStr);
1614
1747
 
1615
1748
  if (objToken && objToken[1]) {
1616
1749
  (0, _util.warn)('indexObjects: Found new "obj" inside of another "obj", ' + 'caused by missing "endobj" -- trying to recover.');
@@ -1622,7 +1755,7 @@ var XRef = function XRefClosure() {
1622
1755
  startPos = endPos;
1623
1756
  }
1624
1757
 
1625
- var content = buffer.subarray(position, position + contentLength);
1758
+ const content = buffer.subarray(position, position + contentLength);
1626
1759
  var xrefTagOffset = skipUntil(content, 0, xrefBytes);
1627
1760
 
1628
1761
  if (xrefTagOffset < contentLength && content[xrefTagOffset + 5] < 64) {
@@ -1631,7 +1764,7 @@ var XRef = function XRefClosure() {
1631
1764
  }
1632
1765
 
1633
1766
  position += contentLength;
1634
- } else if (token.startsWith('trailer') && (token.length === 7 || /\s/.test(token[7]))) {
1767
+ } else if (token.startsWith("trailer") && (token.length === 7 || /\s/.test(token[7]))) {
1635
1768
  trailers.push(position);
1636
1769
  position += skipUntil(buffer, position, startxrefBytes);
1637
1770
  } else {
@@ -1646,11 +1779,11 @@ var XRef = function XRefClosure() {
1646
1779
  this.readXRef(true);
1647
1780
  }
1648
1781
 
1649
- var trailerDict;
1782
+ let trailerDict;
1650
1783
 
1651
1784
  for (i = 0, ii = trailers.length; i < ii; ++i) {
1652
1785
  stream.pos = trailers[i];
1653
- var parser = new _parser.Parser({
1786
+ const parser = new _parser.Parser({
1654
1787
  lexer: new _parser.Lexer(stream),
1655
1788
  xref: this,
1656
1789
  allowStreams: true,
@@ -1658,20 +1791,20 @@ var XRef = function XRefClosure() {
1658
1791
  });
1659
1792
  var obj = parser.getObj();
1660
1793
 
1661
- if (!(0, _primitives.isCmd)(obj, 'trailer')) {
1794
+ if (!(0, _primitives.isCmd)(obj, "trailer")) {
1662
1795
  continue;
1663
1796
  }
1664
1797
 
1665
- var dict = parser.getObj();
1798
+ const dict = parser.getObj();
1666
1799
 
1667
1800
  if (!(0, _primitives.isDict)(dict)) {
1668
1801
  continue;
1669
1802
  }
1670
1803
 
1671
- var rootDict = void 0;
1804
+ let rootDict;
1672
1805
 
1673
1806
  try {
1674
- rootDict = dict.get('Root');
1807
+ rootDict = dict.get("Root");
1675
1808
  } catch (ex) {
1676
1809
  if (ex instanceof _core_utils.MissingDataException) {
1677
1810
  throw ex;
@@ -1680,11 +1813,11 @@ var XRef = function XRefClosure() {
1680
1813
  continue;
1681
1814
  }
1682
1815
 
1683
- if (!(0, _primitives.isDict)(rootDict) || !rootDict.has('Pages')) {
1816
+ if (!(0, _primitives.isDict)(rootDict) || !rootDict.has("Pages")) {
1684
1817
  continue;
1685
1818
  }
1686
1819
 
1687
- if (dict.has('ID')) {
1820
+ if (dict.has("ID")) {
1688
1821
  return dict;
1689
1822
  }
1690
1823
 
@@ -1695,25 +1828,25 @@ var XRef = function XRefClosure() {
1695
1828
  return trailerDict;
1696
1829
  }
1697
1830
 
1698
- throw new _util.InvalidPDFException('Invalid PDF structure');
1831
+ throw new _util.InvalidPDFException("Invalid PDF structure.");
1699
1832
  },
1700
1833
  readXRef: function XRef_readXRef(recoveryMode) {
1701
1834
  var stream = this.stream;
1702
- var startXRefParsedCache = Object.create(null);
1835
+ const startXRefParsedCache = Object.create(null);
1703
1836
 
1704
1837
  try {
1705
1838
  while (this.startXRefQueue.length) {
1706
1839
  var startXRef = this.startXRefQueue[0];
1707
1840
 
1708
1841
  if (startXRefParsedCache[startXRef]) {
1709
- (0, _util.warn)('readXRef - skipping XRef table since it was already parsed.');
1842
+ (0, _util.warn)("readXRef - skipping XRef table since it was already parsed.");
1710
1843
  this.startXRefQueue.shift();
1711
1844
  continue;
1712
1845
  }
1713
1846
 
1714
1847
  startXRefParsedCache[startXRef] = true;
1715
1848
  stream.pos = startXRef + stream.start;
1716
- var parser = new _parser.Parser({
1849
+ const parser = new _parser.Parser({
1717
1850
  lexer: new _parser.Lexer(stream),
1718
1851
  xref: this,
1719
1852
  allowStreams: true
@@ -1721,14 +1854,14 @@ var XRef = function XRefClosure() {
1721
1854
  var obj = parser.getObj();
1722
1855
  var dict;
1723
1856
 
1724
- if ((0, _primitives.isCmd)(obj, 'xref')) {
1857
+ if ((0, _primitives.isCmd)(obj, "xref")) {
1725
1858
  dict = this.processXRefTable(parser);
1726
1859
 
1727
1860
  if (!this.topDict) {
1728
1861
  this.topDict = dict;
1729
1862
  }
1730
1863
 
1731
- obj = dict.get('XRefStm');
1864
+ obj = dict.get("XRefStm");
1732
1865
 
1733
1866
  if (Number.isInteger(obj)) {
1734
1867
  var pos = obj;
@@ -1739,8 +1872,8 @@ var XRef = function XRefClosure() {
1739
1872
  }
1740
1873
  }
1741
1874
  } else if (Number.isInteger(obj)) {
1742
- if (!Number.isInteger(parser.getObj()) || !(0, _primitives.isCmd)(parser.getObj(), 'obj') || !(0, _primitives.isStream)(obj = parser.getObj())) {
1743
- throw new _util.FormatError('Invalid XRef stream');
1875
+ if (!Number.isInteger(parser.getObj()) || !(0, _primitives.isCmd)(parser.getObj(), "obj") || !(0, _primitives.isStream)(obj = parser.getObj())) {
1876
+ throw new _util.FormatError("Invalid XRef stream");
1744
1877
  }
1745
1878
 
1746
1879
  dict = this.processXRefStream(obj);
@@ -1750,13 +1883,13 @@ var XRef = function XRefClosure() {
1750
1883
  }
1751
1884
 
1752
1885
  if (!dict) {
1753
- throw new _util.FormatError('Failed to read XRef stream');
1886
+ throw new _util.FormatError("Failed to read XRef stream");
1754
1887
  }
1755
1888
  } else {
1756
- throw new _util.FormatError('Invalid XRef stream header');
1889
+ throw new _util.FormatError("Invalid XRef stream header");
1757
1890
  }
1758
1891
 
1759
- obj = dict.get('Prev');
1892
+ obj = dict.get("Prev");
1760
1893
 
1761
1894
  if (Number.isInteger(obj)) {
1762
1895
  this.startXRefQueue.push(obj);
@@ -1773,7 +1906,7 @@ var XRef = function XRefClosure() {
1773
1906
  throw e;
1774
1907
  }
1775
1908
 
1776
- (0, _util.info)('(while reading XRef): ' + e);
1909
+ (0, _util.info)("(while reading XRef): " + e);
1777
1910
  }
1778
1911
 
1779
1912
  if (recoveryMode) {
@@ -1792,22 +1925,22 @@ var XRef = function XRefClosure() {
1792
1925
  return null;
1793
1926
  },
1794
1927
  fetchIfRef: function XRef_fetchIfRef(obj, suppressEncryption) {
1795
- if (!(0, _primitives.isRef)(obj)) {
1796
- return obj;
1928
+ if (obj instanceof _primitives.Ref) {
1929
+ return this.fetch(obj, suppressEncryption);
1797
1930
  }
1798
1931
 
1799
- return this.fetch(obj, suppressEncryption);
1932
+ return obj;
1800
1933
  },
1801
1934
  fetch: function XRef_fetch(ref, suppressEncryption) {
1802
- if (!(0, _primitives.isRef)(ref)) {
1803
- throw new Error('ref object is not a reference');
1935
+ if (!(ref instanceof _primitives.Ref)) {
1936
+ throw new Error("ref object is not a reference");
1804
1937
  }
1805
1938
 
1806
- var num = ref.num;
1939
+ const num = ref.num;
1807
1940
 
1808
- if (num in this.cache) {
1809
- var cacheEntry = this.cache[num];
1941
+ const cacheEntry = this._cacheMap.get(num);
1810
1942
 
1943
+ if (cacheEntry !== undefined) {
1811
1944
  if (cacheEntry instanceof _primitives.Dict && !cacheEntry.objId) {
1812
1945
  cacheEntry.objId = ref.toString();
1813
1946
  }
@@ -1815,10 +1948,12 @@ var XRef = function XRefClosure() {
1815
1948
  return cacheEntry;
1816
1949
  }
1817
1950
 
1818
- var xrefEntry = this.getEntry(num);
1951
+ let xrefEntry = this.getEntry(num);
1819
1952
 
1820
1953
  if (xrefEntry === null) {
1821
- return this.cache[num] = null;
1954
+ this._cacheMap.set(num, xrefEntry);
1955
+
1956
+ return xrefEntry;
1822
1957
  }
1823
1958
 
1824
1959
  if (xrefEntry.uncompressed) {
@@ -1835,17 +1970,17 @@ var XRef = function XRefClosure() {
1835
1970
 
1836
1971
  return xrefEntry;
1837
1972
  },
1838
- fetchUncompressed: function fetchUncompressed(ref, xrefEntry) {
1839
- var suppressEncryption = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : false;
1973
+
1974
+ fetchUncompressed(ref, xrefEntry, suppressEncryption = false) {
1840
1975
  var gen = ref.gen;
1841
1976
  var num = ref.num;
1842
1977
 
1843
1978
  if (xrefEntry.gen !== gen) {
1844
- throw new _core_utils.XRefEntryException("Inconsistent generation in XRef: ".concat(ref));
1979
+ throw new _core_utils.XRefEntryException(`Inconsistent generation in XRef: ${ref}`);
1845
1980
  }
1846
1981
 
1847
1982
  var stream = this.stream.makeSubStream(xrefEntry.offset + this.stream.start);
1848
- var parser = new _parser.Parser({
1983
+ const parser = new _parser.Parser({
1849
1984
  lexer: new _parser.Lexer(stream),
1850
1985
  xref: this,
1851
1986
  allowStreams: true
@@ -1854,20 +1989,12 @@ var XRef = function XRefClosure() {
1854
1989
  var obj2 = parser.getObj();
1855
1990
  var obj3 = parser.getObj();
1856
1991
 
1857
- if (!Number.isInteger(obj1)) {
1858
- obj1 = parseInt(obj1, 10);
1859
- }
1860
-
1861
- if (!Number.isInteger(obj2)) {
1862
- obj2 = parseInt(obj2, 10);
1863
- }
1864
-
1865
1992
  if (obj1 !== num || obj2 !== gen || !(obj3 instanceof _primitives.Cmd)) {
1866
- throw new _core_utils.XRefEntryException("Bad (uncompressed) XRef entry: ".concat(ref));
1993
+ throw new _core_utils.XRefEntryException(`Bad (uncompressed) XRef entry: ${ref}`);
1867
1994
  }
1868
1995
 
1869
- if (obj3.cmd !== 'obj') {
1870
- if (obj3.cmd.startsWith('obj')) {
1996
+ if (obj3.cmd !== "obj") {
1997
+ if (obj3.cmd.startsWith("obj")) {
1871
1998
  num = parseInt(obj3.cmd.substring(3), 10);
1872
1999
 
1873
2000
  if (!Number.isNaN(num)) {
@@ -1875,7 +2002,7 @@ var XRef = function XRefClosure() {
1875
2002
  }
1876
2003
  }
1877
2004
 
1878
- throw new _core_utils.XRefEntryException("Bad (uncompressed) XRef entry: ".concat(ref));
2005
+ throw new _core_utils.XRefEntryException(`Bad (uncompressed) XRef entry: ${ref}`);
1879
2006
  }
1880
2007
 
1881
2008
  if (this.encrypt && !suppressEncryption) {
@@ -1885,150 +2012,102 @@ var XRef = function XRefClosure() {
1885
2012
  }
1886
2013
 
1887
2014
  if (!(0, _primitives.isStream)(xrefEntry)) {
1888
- this.cache[num] = xrefEntry;
2015
+ this._cacheMap.set(num, xrefEntry);
1889
2016
  }
1890
2017
 
1891
2018
  return xrefEntry;
1892
2019
  },
1893
- fetchCompressed: function fetchCompressed(ref, xrefEntry) {
1894
- var suppressEncryption = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : false;
1895
- var tableOffset = xrefEntry.offset;
1896
- var stream = this.fetch(_primitives.Ref.get(tableOffset, 0));
2020
+
2021
+ fetchCompressed(ref, xrefEntry, suppressEncryption = false) {
2022
+ const tableOffset = xrefEntry.offset;
2023
+ const stream = this.fetch(_primitives.Ref.get(tableOffset, 0));
1897
2024
 
1898
2025
  if (!(0, _primitives.isStream)(stream)) {
1899
- throw new _util.FormatError('bad ObjStm stream');
2026
+ throw new _util.FormatError("bad ObjStm stream");
1900
2027
  }
1901
2028
 
1902
- var first = stream.dict.get('First');
1903
- var n = stream.dict.get('N');
2029
+ const first = stream.dict.get("First");
2030
+ const n = stream.dict.get("N");
1904
2031
 
1905
2032
  if (!Number.isInteger(first) || !Number.isInteger(n)) {
1906
- throw new _util.FormatError('invalid first and n parameters for ObjStm stream');
2033
+ throw new _util.FormatError("invalid first and n parameters for ObjStm stream");
1907
2034
  }
1908
2035
 
1909
- var parser = new _parser.Parser({
2036
+ const parser = new _parser.Parser({
1910
2037
  lexer: new _parser.Lexer(stream),
1911
2038
  xref: this,
1912
2039
  allowStreams: true
1913
2040
  });
1914
- var i,
1915
- entries = [],
1916
- num,
1917
- nums = [];
2041
+ const nums = new Array(n);
1918
2042
 
1919
- for (i = 0; i < n; ++i) {
1920
- num = parser.getObj();
2043
+ for (let i = 0; i < n; ++i) {
2044
+ const num = parser.getObj();
1921
2045
 
1922
2046
  if (!Number.isInteger(num)) {
1923
- throw new _util.FormatError("invalid object number in the ObjStm stream: ".concat(num));
2047
+ throw new _util.FormatError(`invalid object number in the ObjStm stream: ${num}`);
1924
2048
  }
1925
2049
 
1926
- nums.push(num);
1927
- var offset = parser.getObj();
2050
+ const offset = parser.getObj();
1928
2051
 
1929
2052
  if (!Number.isInteger(offset)) {
1930
- throw new _util.FormatError("invalid object offset in the ObjStm stream: ".concat(offset));
2053
+ throw new _util.FormatError(`invalid object offset in the ObjStm stream: ${offset}`);
1931
2054
  }
2055
+
2056
+ nums[i] = num;
1932
2057
  }
1933
2058
 
1934
- for (i = 0; i < n; ++i) {
1935
- entries.push(parser.getObj());
2059
+ const entries = new Array(n);
1936
2060
 
1937
- if ((0, _primitives.isCmd)(parser.buf1, 'endobj')) {
2061
+ for (let i = 0; i < n; ++i) {
2062
+ const obj = parser.getObj();
2063
+ entries[i] = obj;
2064
+
2065
+ if (parser.buf1 instanceof _primitives.Cmd && parser.buf1.cmd === "endobj") {
1938
2066
  parser.shift();
1939
2067
  }
1940
2068
 
1941
- num = nums[i];
1942
- var entry = this.entries[num];
2069
+ if ((0, _primitives.isStream)(obj)) {
2070
+ continue;
2071
+ }
2072
+
2073
+ const num = nums[i],
2074
+ entry = this.entries[num];
1943
2075
 
1944
2076
  if (entry && entry.offset === tableOffset && entry.gen === i) {
1945
- this.cache[num] = entries[i];
2077
+ this._cacheMap.set(num, obj);
1946
2078
  }
1947
2079
  }
1948
2080
 
1949
2081
  xrefEntry = entries[xrefEntry.gen];
1950
2082
 
1951
2083
  if (xrefEntry === undefined) {
1952
- throw new _core_utils.XRefEntryException("Bad (compressed) XRef entry: ".concat(ref));
2084
+ throw new _core_utils.XRefEntryException(`Bad (compressed) XRef entry: ${ref}`);
1953
2085
  }
1954
2086
 
1955
2087
  return xrefEntry;
1956
2088
  },
1957
- fetchIfRefAsync: function () {
1958
- var _fetchIfRefAsync = _asyncToGenerator(
1959
- /*#__PURE__*/
1960
- _regenerator["default"].mark(function _callee(obj, suppressEncryption) {
1961
- return _regenerator["default"].wrap(function _callee$(_context) {
1962
- while (1) {
1963
- switch (_context.prev = _context.next) {
1964
- case 0:
1965
- if ((0, _primitives.isRef)(obj)) {
1966
- _context.next = 2;
1967
- break;
1968
- }
1969
-
1970
- return _context.abrupt("return", obj);
1971
-
1972
- case 2:
1973
- return _context.abrupt("return", this.fetchAsync(obj, suppressEncryption));
1974
-
1975
- case 3:
1976
- case "end":
1977
- return _context.stop();
1978
- }
1979
- }
1980
- }, _callee, this);
1981
- }));
1982
-
1983
- function fetchIfRefAsync(_x, _x2) {
1984
- return _fetchIfRefAsync.apply(this, arguments);
1985
- }
1986
-
1987
- return fetchIfRefAsync;
1988
- }(),
1989
- fetchAsync: function () {
1990
- var _fetchAsync = _asyncToGenerator(
1991
- /*#__PURE__*/
1992
- _regenerator["default"].mark(function _callee2(ref, suppressEncryption) {
1993
- return _regenerator["default"].wrap(function _callee2$(_context2) {
1994
- while (1) {
1995
- switch (_context2.prev = _context2.next) {
1996
- case 0:
1997
- _context2.prev = 0;
1998
- return _context2.abrupt("return", this.fetch(ref, suppressEncryption));
1999
-
2000
- case 4:
2001
- _context2.prev = 4;
2002
- _context2.t0 = _context2["catch"](0);
2003
-
2004
- if (_context2.t0 instanceof _core_utils.MissingDataException) {
2005
- _context2.next = 8;
2006
- break;
2007
- }
2008
-
2009
- throw _context2.t0;
2010
2089
 
2011
- case 8:
2012
- _context2.next = 10;
2013
- return this.pdfManager.requestRange(_context2.t0.begin, _context2.t0.end);
2090
+ async fetchIfRefAsync(obj, suppressEncryption) {
2091
+ if (obj instanceof _primitives.Ref) {
2092
+ return this.fetchAsync(obj, suppressEncryption);
2093
+ }
2014
2094
 
2015
- case 10:
2016
- return _context2.abrupt("return", this.fetchAsync(ref, suppressEncryption));
2095
+ return obj;
2096
+ },
2017
2097
 
2018
- case 11:
2019
- case "end":
2020
- return _context2.stop();
2021
- }
2022
- }
2023
- }, _callee2, this, [[0, 4]]);
2024
- }));
2098
+ async fetchAsync(ref, suppressEncryption) {
2099
+ try {
2100
+ return this.fetch(ref, suppressEncryption);
2101
+ } catch (ex) {
2102
+ if (!(ex instanceof _core_utils.MissingDataException)) {
2103
+ throw ex;
2104
+ }
2025
2105
 
2026
- function fetchAsync(_x3, _x4) {
2027
- return _fetchAsync.apply(this, arguments);
2106
+ await this.pdfManager.requestRange(ex.begin, ex.end);
2107
+ return this.fetchAsync(ref, suppressEncryption);
2028
2108
  }
2109
+ },
2029
2110
 
2030
- return fetchAsync;
2031
- }(),
2032
2111
  getCatalogObj: function XRef_getCatalogObj() {
2033
2112
  return this.root;
2034
2113
  }
@@ -2038,14 +2117,10 @@ var XRef = function XRefClosure() {
2038
2117
 
2039
2118
  exports.XRef = XRef;
2040
2119
 
2041
- var NameOrNumberTree =
2042
- /*#__PURE__*/
2043
- function () {
2044
- function NameOrNumberTree(root, xref, type) {
2045
- _classCallCheck(this, NameOrNumberTree);
2046
-
2120
+ class NameOrNumberTree {
2121
+ constructor(root, xref, type) {
2047
2122
  if (this.constructor === NameOrNumberTree) {
2048
- (0, _util.unreachable)('Cannot initialize NameOrNumberTree.');
2123
+ (0, _util.unreachable)("Cannot initialize NameOrNumberTree.");
2049
2124
  }
2050
2125
 
2051
2126
  this.root = root;
@@ -2053,169 +2128,149 @@ function () {
2053
2128
  this._type = type;
2054
2129
  }
2055
2130
 
2056
- _createClass(NameOrNumberTree, [{
2057
- key: "getAll",
2058
- value: function getAll() {
2059
- var dict = Object.create(null);
2060
-
2061
- if (!this.root) {
2062
- return dict;
2063
- }
2131
+ getAll() {
2132
+ const dict = Object.create(null);
2064
2133
 
2065
- var xref = this.xref;
2066
- var processed = new _primitives.RefSet();
2067
- processed.put(this.root);
2068
- var queue = [this.root];
2134
+ if (!this.root) {
2135
+ return dict;
2136
+ }
2069
2137
 
2070
- while (queue.length > 0) {
2071
- var obj = xref.fetchIfRef(queue.shift());
2138
+ const xref = this.xref;
2139
+ const processed = new _primitives.RefSet();
2140
+ processed.put(this.root);
2141
+ const queue = [this.root];
2072
2142
 
2073
- if (!(0, _primitives.isDict)(obj)) {
2074
- continue;
2075
- }
2143
+ while (queue.length > 0) {
2144
+ const obj = xref.fetchIfRef(queue.shift());
2076
2145
 
2077
- if (obj.has('Kids')) {
2078
- var kids = obj.get('Kids');
2146
+ if (!(0, _primitives.isDict)(obj)) {
2147
+ continue;
2148
+ }
2079
2149
 
2080
- for (var i = 0, ii = kids.length; i < ii; i++) {
2081
- var kid = kids[i];
2150
+ if (obj.has("Kids")) {
2151
+ const kids = obj.get("Kids");
2082
2152
 
2083
- if (processed.has(kid)) {
2084
- throw new _util.FormatError("Duplicate entry in \"".concat(this._type, "\" tree."));
2085
- }
2153
+ for (let i = 0, ii = kids.length; i < ii; i++) {
2154
+ const kid = kids[i];
2086
2155
 
2087
- queue.push(kid);
2088
- processed.put(kid);
2156
+ if (processed.has(kid)) {
2157
+ throw new _util.FormatError(`Duplicate entry in "${this._type}" tree.`);
2089
2158
  }
2090
2159
 
2091
- continue;
2160
+ queue.push(kid);
2161
+ processed.put(kid);
2092
2162
  }
2093
2163
 
2094
- var entries = obj.get(this._type);
2164
+ continue;
2165
+ }
2166
+
2167
+ const entries = obj.get(this._type);
2095
2168
 
2096
- if (Array.isArray(entries)) {
2097
- for (var _i2 = 0, _ii = entries.length; _i2 < _ii; _i2 += 2) {
2098
- dict[xref.fetchIfRef(entries[_i2])] = xref.fetchIfRef(entries[_i2 + 1]);
2099
- }
2169
+ if (Array.isArray(entries)) {
2170
+ for (let i = 0, ii = entries.length; i < ii; i += 2) {
2171
+ dict[xref.fetchIfRef(entries[i])] = xref.fetchIfRef(entries[i + 1]);
2100
2172
  }
2101
2173
  }
2102
-
2103
- return dict;
2104
2174
  }
2105
- }, {
2106
- key: "get",
2107
- value: function get(key) {
2108
- if (!this.root) {
2109
- return null;
2110
- }
2111
2175
 
2112
- var xref = this.xref;
2113
- var kidsOrEntries = xref.fetchIfRef(this.root);
2114
- var loopCount = 0;
2115
- var MAX_LEVELS = 10;
2176
+ return dict;
2177
+ }
2116
2178
 
2117
- while (kidsOrEntries.has('Kids')) {
2118
- if (++loopCount > MAX_LEVELS) {
2119
- (0, _util.warn)("Search depth limit reached for \"".concat(this._type, "\" tree."));
2120
- return null;
2121
- }
2179
+ get(key) {
2180
+ if (!this.root) {
2181
+ return null;
2182
+ }
2122
2183
 
2123
- var kids = kidsOrEntries.get('Kids');
2184
+ const xref = this.xref;
2185
+ let kidsOrEntries = xref.fetchIfRef(this.root);
2186
+ let loopCount = 0;
2187
+ const MAX_LEVELS = 10;
2124
2188
 
2125
- if (!Array.isArray(kids)) {
2126
- return null;
2127
- }
2189
+ while (kidsOrEntries.has("Kids")) {
2190
+ if (++loopCount > MAX_LEVELS) {
2191
+ (0, _util.warn)(`Search depth limit reached for "${this._type}" tree.`);
2192
+ return null;
2193
+ }
2128
2194
 
2129
- var l = 0,
2130
- r = kids.length - 1;
2195
+ const kids = kidsOrEntries.get("Kids");
2131
2196
 
2132
- while (l <= r) {
2133
- var m = l + r >> 1;
2134
- var kid = xref.fetchIfRef(kids[m]);
2135
- var limits = kid.get('Limits');
2197
+ if (!Array.isArray(kids)) {
2198
+ return null;
2199
+ }
2136
2200
 
2137
- if (key < xref.fetchIfRef(limits[0])) {
2138
- r = m - 1;
2139
- } else if (key > xref.fetchIfRef(limits[1])) {
2140
- l = m + 1;
2141
- } else {
2142
- kidsOrEntries = xref.fetchIfRef(kids[m]);
2143
- break;
2144
- }
2145
- }
2201
+ let l = 0,
2202
+ r = kids.length - 1;
2146
2203
 
2147
- if (l > r) {
2148
- return null;
2204
+ while (l <= r) {
2205
+ const m = l + r >> 1;
2206
+ const kid = xref.fetchIfRef(kids[m]);
2207
+ const limits = kid.get("Limits");
2208
+
2209
+ if (key < xref.fetchIfRef(limits[0])) {
2210
+ r = m - 1;
2211
+ } else if (key > xref.fetchIfRef(limits[1])) {
2212
+ l = m + 1;
2213
+ } else {
2214
+ kidsOrEntries = xref.fetchIfRef(kids[m]);
2215
+ break;
2149
2216
  }
2150
2217
  }
2151
2218
 
2152
- var entries = kidsOrEntries.get(this._type);
2219
+ if (l > r) {
2220
+ return null;
2221
+ }
2222
+ }
2153
2223
 
2154
- if (Array.isArray(entries)) {
2155
- var _l = 0,
2156
- _r = entries.length - 2;
2224
+ const entries = kidsOrEntries.get(this._type);
2157
2225
 
2158
- while (_l <= _r) {
2159
- var tmp = _l + _r >> 1,
2160
- _m = tmp + (tmp & 1);
2226
+ if (Array.isArray(entries)) {
2227
+ let l = 0,
2228
+ r = entries.length - 2;
2161
2229
 
2162
- var currentKey = xref.fetchIfRef(entries[_m]);
2230
+ while (l <= r) {
2231
+ const tmp = l + r >> 1,
2232
+ m = tmp + (tmp & 1);
2233
+ const currentKey = xref.fetchIfRef(entries[m]);
2163
2234
 
2164
- if (key < currentKey) {
2165
- _r = _m - 2;
2166
- } else if (key > currentKey) {
2167
- _l = _m + 2;
2168
- } else {
2169
- return xref.fetchIfRef(entries[_m + 1]);
2170
- }
2235
+ if (key < currentKey) {
2236
+ r = m - 2;
2237
+ } else if (key > currentKey) {
2238
+ l = m + 2;
2239
+ } else {
2240
+ return xref.fetchIfRef(entries[m + 1]);
2171
2241
  }
2242
+ }
2172
2243
 
2173
- (0, _util.info)("Falling back to an exhaustive search, for key \"".concat(key, "\", ") + "in \"".concat(this._type, "\" tree."));
2244
+ (0, _util.info)(`Falling back to an exhaustive search, for key "${key}", ` + `in "${this._type}" tree.`);
2174
2245
 
2175
- for (var _m2 = 0, mm = entries.length; _m2 < mm; _m2 += 2) {
2176
- var _currentKey = xref.fetchIfRef(entries[_m2]);
2246
+ for (let m = 0, mm = entries.length; m < mm; m += 2) {
2247
+ const currentKey = xref.fetchIfRef(entries[m]);
2177
2248
 
2178
- if (_currentKey === key) {
2179
- (0, _util.warn)("The \"".concat(key, "\" key was found at an incorrect, ") + "i.e. out-of-order, position in \"".concat(this._type, "\" tree."));
2180
- return xref.fetchIfRef(entries[_m2 + 1]);
2181
- }
2249
+ if (currentKey === key) {
2250
+ (0, _util.warn)(`The "${key}" key was found at an incorrect, ` + `i.e. out-of-order, position in "${this._type}" tree.`);
2251
+ return xref.fetchIfRef(entries[m + 1]);
2182
2252
  }
2183
2253
  }
2184
-
2185
- return null;
2186
2254
  }
2187
- }]);
2188
-
2189
- return NameOrNumberTree;
2190
- }();
2191
-
2192
- var NameTree =
2193
- /*#__PURE__*/
2194
- function (_NameOrNumberTree) {
2195
- _inherits(NameTree, _NameOrNumberTree);
2196
2255
 
2197
- function NameTree(root, xref) {
2198
- _classCallCheck(this, NameTree);
2199
-
2200
- return _possibleConstructorReturn(this, _getPrototypeOf(NameTree).call(this, root, xref, 'Names'));
2256
+ return null;
2201
2257
  }
2202
2258
 
2203
- return NameTree;
2204
- }(NameOrNumberTree);
2259
+ }
2205
2260
 
2206
- var NumberTree =
2207
- /*#__PURE__*/
2208
- function (_NameOrNumberTree2) {
2209
- _inherits(NumberTree, _NameOrNumberTree2);
2261
+ class NameTree extends NameOrNumberTree {
2262
+ constructor(root, xref) {
2263
+ super(root, xref, "Names");
2264
+ }
2210
2265
 
2211
- function NumberTree(root, xref) {
2212
- _classCallCheck(this, NumberTree);
2266
+ }
2213
2267
 
2214
- return _possibleConstructorReturn(this, _getPrototypeOf(NumberTree).call(this, root, xref, 'Nums'));
2268
+ class NumberTree extends NameOrNumberTree {
2269
+ constructor(root, xref) {
2270
+ super(root, xref, "Nums");
2215
2271
  }
2216
2272
 
2217
- return NumberTree;
2218
- }(NameOrNumberTree);
2273
+ }
2219
2274
 
2220
2275
  var FileSpec = function FileSpecClosure() {
2221
2276
  function FileSpec(root, xref) {
@@ -2226,35 +2281,35 @@ var FileSpec = function FileSpecClosure() {
2226
2281
  this.xref = xref;
2227
2282
  this.root = root;
2228
2283
 
2229
- if (root.has('FS')) {
2230
- this.fs = root.get('FS');
2284
+ if (root.has("FS")) {
2285
+ this.fs = root.get("FS");
2231
2286
  }
2232
2287
 
2233
- this.description = root.has('Desc') ? (0, _util.stringToPDFString)(root.get('Desc')) : '';
2288
+ this.description = root.has("Desc") ? (0, _util.stringToPDFString)(root.get("Desc")) : "";
2234
2289
 
2235
- if (root.has('RF')) {
2236
- (0, _util.warn)('Related file specifications are not supported');
2290
+ if (root.has("RF")) {
2291
+ (0, _util.warn)("Related file specifications are not supported");
2237
2292
  }
2238
2293
 
2239
2294
  this.contentAvailable = true;
2240
2295
 
2241
- if (!root.has('EF')) {
2296
+ if (!root.has("EF")) {
2242
2297
  this.contentAvailable = false;
2243
- (0, _util.warn)('Non-embedded file specifications are not supported');
2298
+ (0, _util.warn)("Non-embedded file specifications are not supported");
2244
2299
  }
2245
2300
  }
2246
2301
 
2247
2302
  function pickPlatformItem(dict) {
2248
- if (dict.has('UF')) {
2249
- return dict.get('UF');
2250
- } else if (dict.has('F')) {
2251
- return dict.get('F');
2252
- } else if (dict.has('Unix')) {
2253
- return dict.get('Unix');
2254
- } else if (dict.has('Mac')) {
2255
- return dict.get('Mac');
2256
- } else if (dict.has('DOS')) {
2257
- return dict.get('DOS');
2303
+ if (dict.has("UF")) {
2304
+ return dict.get("UF");
2305
+ } else if (dict.has("F")) {
2306
+ return dict.get("F");
2307
+ } else if (dict.has("Unix")) {
2308
+ return dict.get("Unix");
2309
+ } else if (dict.has("Mac")) {
2310
+ return dict.get("Mac");
2311
+ } else if (dict.has("DOS")) {
2312
+ return dict.get("DOS");
2258
2313
  }
2259
2314
 
2260
2315
  return null;
@@ -2263,8 +2318,8 @@ var FileSpec = function FileSpecClosure() {
2263
2318
  FileSpec.prototype = {
2264
2319
  get filename() {
2265
2320
  if (!this._filename && this.root) {
2266
- var filename = pickPlatformItem(this.root) || 'unnamed';
2267
- this._filename = (0, _util.stringToPDFString)(filename).replace(/\\\\/g, '\\').replace(/\\\//g, '/').replace(/\\/g, '/');
2321
+ var filename = pickPlatformItem(this.root) || "unnamed";
2322
+ this._filename = (0, _util.stringToPDFString)(filename).replace(/\\\\/g, "\\").replace(/\\\//g, "/").replace(/\\/g, "/");
2268
2323
  }
2269
2324
 
2270
2325
  return this._filename;
@@ -2276,7 +2331,7 @@ var FileSpec = function FileSpecClosure() {
2276
2331
  }
2277
2332
 
2278
2333
  if (!this.contentRef && this.root) {
2279
- this.contentRef = pickPlatformItem(this.root.get('EF'));
2334
+ this.contentRef = pickPlatformItem(this.root.get("EF"));
2280
2335
  }
2281
2336
 
2282
2337
  var content = null;
@@ -2288,10 +2343,10 @@ var FileSpec = function FileSpecClosure() {
2288
2343
  if (fileObj && (0, _primitives.isStream)(fileObj)) {
2289
2344
  content = fileObj.getBytes();
2290
2345
  } else {
2291
- (0, _util.warn)('Embedded file specification points to non-existing/invalid ' + 'content');
2346
+ (0, _util.warn)("Embedded file specification points to non-existing/invalid " + "content");
2292
2347
  }
2293
2348
  } else {
2294
- (0, _util.warn)('Embedded file specification does not have a content');
2349
+ (0, _util.warn)("Embedded file specification does not have a content");
2295
2350
  }
2296
2351
 
2297
2352
  return content;
@@ -2310,30 +2365,23 @@ var FileSpec = function FileSpecClosure() {
2310
2365
 
2311
2366
  exports.FileSpec = FileSpec;
2312
2367
 
2313
- var ObjectLoader = function () {
2368
+ const ObjectLoader = function () {
2314
2369
  function mayHaveChildren(value) {
2315
- return (0, _primitives.isRef)(value) || (0, _primitives.isDict)(value) || Array.isArray(value) || (0, _primitives.isStream)(value);
2370
+ return value instanceof _primitives.Ref || value instanceof _primitives.Dict || Array.isArray(value) || (0, _primitives.isStream)(value);
2316
2371
  }
2317
2372
 
2318
2373
  function addChildren(node, nodesToVisit) {
2319
- if ((0, _primitives.isDict)(node) || (0, _primitives.isStream)(node)) {
2320
- var dict = (0, _primitives.isDict)(node) ? node : node.dict;
2321
- var dictKeys = dict.getKeys();
2322
-
2323
- for (var i = 0, ii = dictKeys.length; i < ii; i++) {
2324
- var rawValue = dict.getRaw(dictKeys[i]);
2325
-
2326
- if (mayHaveChildren(rawValue)) {
2327
- nodesToVisit.push(rawValue);
2328
- }
2329
- }
2330
- } else if (Array.isArray(node)) {
2331
- for (var _i3 = 0, _ii2 = node.length; _i3 < _ii2; _i3++) {
2332
- var value = node[_i3];
2374
+ if (node instanceof _primitives.Dict) {
2375
+ node = node.getRawValues();
2376
+ } else if ((0, _primitives.isStream)(node)) {
2377
+ node = node.dict.getRawValues();
2378
+ } else if (!Array.isArray(node)) {
2379
+ return;
2380
+ }
2333
2381
 
2334
- if (mayHaveChildren(value)) {
2335
- nodesToVisit.push(value);
2336
- }
2382
+ for (const rawValue of node) {
2383
+ if (mayHaveChildren(rawValue)) {
2384
+ nodesToVisit.push(rawValue);
2337
2385
  }
2338
2386
  }
2339
2387
  }
@@ -2343,45 +2391,40 @@ var ObjectLoader = function () {
2343
2391
  this.keys = keys;
2344
2392
  this.xref = xref;
2345
2393
  this.refSet = null;
2346
- this.capability = null;
2347
2394
  }
2348
2395
 
2349
2396
  ObjectLoader.prototype = {
2350
- load: function load() {
2351
- this.capability = (0, _util.createPromiseCapability)();
2352
-
2353
- if (!(this.xref.stream instanceof _chunked_stream.ChunkedStream) || this.xref.stream.getMissingChunks().length === 0) {
2354
- this.capability.resolve();
2355
- return this.capability.promise;
2397
+ async load() {
2398
+ if (!this.xref.stream.allChunksLoaded || this.xref.stream.allChunksLoaded()) {
2399
+ return undefined;
2356
2400
  }
2357
2401
 
2358
- var keys = this.keys,
2359
- dict = this.dict;
2402
+ const {
2403
+ keys,
2404
+ dict
2405
+ } = this;
2360
2406
  this.refSet = new _primitives.RefSet();
2361
- var nodesToVisit = [];
2407
+ const nodesToVisit = [];
2362
2408
 
2363
- for (var i = 0, ii = keys.length; i < ii; i++) {
2364
- var rawValue = dict.getRaw(keys[i]);
2409
+ for (let i = 0, ii = keys.length; i < ii; i++) {
2410
+ const rawValue = dict.getRaw(keys[i]);
2365
2411
 
2366
2412
  if (rawValue !== undefined) {
2367
2413
  nodesToVisit.push(rawValue);
2368
2414
  }
2369
2415
  }
2370
2416
 
2371
- this._walk(nodesToVisit);
2372
-
2373
- return this.capability.promise;
2417
+ return this._walk(nodesToVisit);
2374
2418
  },
2375
- _walk: function _walk(nodesToVisit) {
2376
- var _this3 = this;
2377
2419
 
2378
- var nodesToRevisit = [];
2379
- var pendingRequests = [];
2420
+ async _walk(nodesToVisit) {
2421
+ const nodesToRevisit = [];
2422
+ const pendingRequests = [];
2380
2423
 
2381
2424
  while (nodesToVisit.length) {
2382
- var currentNode = nodesToVisit.pop();
2425
+ let currentNode = nodesToVisit.pop();
2383
2426
 
2384
- if ((0, _primitives.isRef)(currentNode)) {
2427
+ if (currentNode instanceof _primitives.Ref) {
2385
2428
  if (this.refSet.has(currentNode)) {
2386
2429
  continue;
2387
2430
  }
@@ -2403,13 +2446,13 @@ var ObjectLoader = function () {
2403
2446
  }
2404
2447
 
2405
2448
  if (currentNode && currentNode.getBaseStreams) {
2406
- var baseStreams = currentNode.getBaseStreams();
2407
- var foundMissingData = false;
2449
+ const baseStreams = currentNode.getBaseStreams();
2450
+ let foundMissingData = false;
2408
2451
 
2409
- for (var i = 0, ii = baseStreams.length; i < ii; i++) {
2410
- var stream = baseStreams[i];
2452
+ for (let i = 0, ii = baseStreams.length; i < ii; i++) {
2453
+ const stream = baseStreams[i];
2411
2454
 
2412
- if (stream.getMissingChunks && stream.getMissingChunks().length) {
2455
+ if (stream.allChunksLoaded && !stream.allChunksLoaded()) {
2413
2456
  foundMissingData = true;
2414
2457
  pendingRequests.push({
2415
2458
  begin: stream.start,
@@ -2427,23 +2470,23 @@ var ObjectLoader = function () {
2427
2470
  }
2428
2471
 
2429
2472
  if (pendingRequests.length) {
2430
- this.xref.stream.manager.requestRanges(pendingRequests).then(function () {
2431
- for (var _i4 = 0, _ii3 = nodesToRevisit.length; _i4 < _ii3; _i4++) {
2432
- var node = nodesToRevisit[_i4];
2473
+ await this.xref.stream.manager.requestRanges(pendingRequests);
2433
2474
 
2434
- if ((0, _primitives.isRef)(node)) {
2435
- _this3.refSet.remove(node);
2436
- }
2475
+ for (let i = 0, ii = nodesToRevisit.length; i < ii; i++) {
2476
+ const node = nodesToRevisit[i];
2477
+
2478
+ if (node instanceof _primitives.Ref) {
2479
+ this.refSet.remove(node);
2437
2480
  }
2481
+ }
2438
2482
 
2439
- _this3._walk(nodesToRevisit);
2440
- }, this.capability.reject);
2441
- return;
2483
+ return this._walk(nodesToRevisit);
2442
2484
  }
2443
2485
 
2444
2486
  this.refSet = null;
2445
- this.capability.resolve();
2487
+ return undefined;
2446
2488
  }
2489
+
2447
2490
  };
2448
2491
  return ObjectLoader;
2449
2492
  }();