catalog.js 41 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671
  1. /**
  2. * @licstart The following is the entire license notice for the
  3. * JavaScript code in this page
  4. *
  5. * Copyright 2022 Mozilla Foundation
  6. *
  7. * Licensed under the Apache License, Version 2.0 (the "License");
  8. * you may not use this file except in compliance with the License.
  9. * You may obtain a copy of the License at
  10. *
  11. * http://www.apache.org/licenses/LICENSE-2.0
  12. *
  13. * Unless required by applicable law or agreed to in writing, software
  14. * distributed under the License is distributed on an "AS IS" BASIS,
  15. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  16. * See the License for the specific language governing permissions and
  17. * limitations under the License.
  18. *
  19. * @licend The above is the entire license notice for the
  20. * JavaScript code in this page
  21. */
  22. "use strict";
  23. Object.defineProperty(exports, "__esModule", {
  24. value: true
  25. });
  26. exports.Catalog = void 0;
  27. var _core_utils = require("./core_utils.js");
  28. var _util = require("../shared/util.js");
  29. var _primitives = require("./primitives.js");
  30. var _name_number_tree = require("./name_number_tree.js");
  31. var _base_stream = require("./base_stream.js");
  32. var _cleanup_helper = require("./cleanup_helper.js");
  33. var _colorspace = require("./colorspace.js");
  34. var _file_spec = require("./file_spec.js");
  35. var _image_utils = require("./image_utils.js");
  36. var _metadata_parser = require("./metadata_parser.js");
  37. var _struct_tree = require("./struct_tree.js");
  38. function fetchDestination(dest) {
  39. if (dest instanceof _primitives.Dict) {
  40. dest = dest.get("D");
  41. }
  42. return Array.isArray(dest) ? dest : null;
  43. }
  44. class Catalog {
  45. constructor(pdfManager, xref) {
  46. this.pdfManager = pdfManager;
  47. this.xref = xref;
  48. this._catDict = xref.getCatalogObj();
  49. if (!(this._catDict instanceof _primitives.Dict)) {
  50. throw new _util.FormatError("Catalog object is not a dictionary.");
  51. }
  52. this.toplevelPagesDict;
  53. this._actualNumPages = null;
  54. this.fontCache = new _primitives.RefSetCache();
  55. this.builtInCMapCache = new Map();
  56. this.standardFontDataCache = new Map();
  57. this.globalImageCache = new _image_utils.GlobalImageCache();
  58. this.pageKidsCountCache = new _primitives.RefSetCache();
  59. this.pageIndexCache = new _primitives.RefSetCache();
  60. this.nonBlendModesSet = new _primitives.RefSet();
  61. }
  62. get version() {
  63. const version = this._catDict.get("Version");
  64. return (0, _util.shadow)(this, "version", version instanceof _primitives.Name ? version.name : null);
  65. }
  66. get lang() {
  67. const lang = this._catDict.get("Lang");
  68. return (0, _util.shadow)(this, "lang", typeof lang === "string" ? (0, _util.stringToPDFString)(lang) : null);
  69. }
  70. get needsRendering() {
  71. const needsRendering = this._catDict.get("NeedsRendering");
  72. return (0, _util.shadow)(this, "needsRendering", typeof needsRendering === "boolean" ? needsRendering : false);
  73. }
  74. get collection() {
  75. let collection = null;
  76. try {
  77. const obj = this._catDict.get("Collection");
  78. if (obj instanceof _primitives.Dict && obj.size > 0) {
  79. collection = obj;
  80. }
  81. } catch (ex) {
  82. if (ex instanceof _core_utils.MissingDataException) {
  83. throw ex;
  84. }
  85. (0, _util.info)("Cannot fetch Collection entry; assuming no collection is present.");
  86. }
  87. return (0, _util.shadow)(this, "collection", collection);
  88. }
  89. get acroForm() {
  90. let acroForm = null;
  91. try {
  92. const obj = this._catDict.get("AcroForm");
  93. if (obj instanceof _primitives.Dict && obj.size > 0) {
  94. acroForm = obj;
  95. }
  96. } catch (ex) {
  97. if (ex instanceof _core_utils.MissingDataException) {
  98. throw ex;
  99. }
  100. (0, _util.info)("Cannot fetch AcroForm entry; assuming no forms are present.");
  101. }
  102. return (0, _util.shadow)(this, "acroForm", acroForm);
  103. }
  104. get acroFormRef() {
  105. const value = this._catDict.getRaw("AcroForm");
  106. return (0, _util.shadow)(this, "acroFormRef", value instanceof _primitives.Ref ? value : null);
  107. }
  108. get metadata() {
  109. const streamRef = this._catDict.getRaw("Metadata");
  110. if (!(streamRef instanceof _primitives.Ref)) {
  111. return (0, _util.shadow)(this, "metadata", null);
  112. }
  113. let metadata = null;
  114. try {
  115. const suppressEncryption = !(this.xref.encrypt && this.xref.encrypt.encryptMetadata);
  116. const stream = this.xref.fetch(streamRef, suppressEncryption);
  117. if (stream instanceof _base_stream.BaseStream && stream.dict instanceof _primitives.Dict) {
  118. const type = stream.dict.get("Type");
  119. const subtype = stream.dict.get("Subtype");
  120. if ((0, _primitives.isName)(type, "Metadata") && (0, _primitives.isName)(subtype, "XML")) {
  121. const data = (0, _util.stringToUTF8String)(stream.getString());
  122. if (data) {
  123. metadata = new _metadata_parser.MetadataParser(data).serializable;
  124. }
  125. }
  126. }
  127. } catch (ex) {
  128. if (ex instanceof _core_utils.MissingDataException) {
  129. throw ex;
  130. }
  131. (0, _util.info)(`Skipping invalid Metadata: "${ex}".`);
  132. }
  133. return (0, _util.shadow)(this, "metadata", metadata);
  134. }
  135. get markInfo() {
  136. let markInfo = null;
  137. try {
  138. markInfo = this._readMarkInfo();
  139. } catch (ex) {
  140. if (ex instanceof _core_utils.MissingDataException) {
  141. throw ex;
  142. }
  143. (0, _util.warn)("Unable to read mark info.");
  144. }
  145. return (0, _util.shadow)(this, "markInfo", markInfo);
  146. }
  147. _readMarkInfo() {
  148. const obj = this._catDict.get("MarkInfo");
  149. if (!(obj instanceof _primitives.Dict)) {
  150. return null;
  151. }
  152. const markInfo = {
  153. Marked: false,
  154. UserProperties: false,
  155. Suspects: false
  156. };
  157. for (const key in markInfo) {
  158. const value = obj.get(key);
  159. if (typeof value === "boolean") {
  160. markInfo[key] = value;
  161. }
  162. }
  163. return markInfo;
  164. }
  165. get structTreeRoot() {
  166. let structTree = null;
  167. try {
  168. structTree = this._readStructTreeRoot();
  169. } catch (ex) {
  170. if (ex instanceof _core_utils.MissingDataException) {
  171. throw ex;
  172. }
  173. (0, _util.warn)("Unable read to structTreeRoot info.");
  174. }
  175. return (0, _util.shadow)(this, "structTreeRoot", structTree);
  176. }
  177. _readStructTreeRoot() {
  178. const obj = this._catDict.get("StructTreeRoot");
  179. if (!(obj instanceof _primitives.Dict)) {
  180. return null;
  181. }
  182. const root = new _struct_tree.StructTreeRoot(obj);
  183. root.init();
  184. return root;
  185. }
  186. get toplevelPagesDict() {
  187. const pagesObj = this._catDict.get("Pages");
  188. if (!(pagesObj instanceof _primitives.Dict)) {
  189. throw new _util.FormatError("Invalid top-level pages dictionary.");
  190. }
  191. return (0, _util.shadow)(this, "toplevelPagesDict", pagesObj);
  192. }
  193. get documentOutline() {
  194. let obj = null;
  195. try {
  196. obj = this._readDocumentOutline();
  197. } catch (ex) {
  198. if (ex instanceof _core_utils.MissingDataException) {
  199. throw ex;
  200. }
  201. (0, _util.warn)("Unable to read document outline.");
  202. }
  203. return (0, _util.shadow)(this, "documentOutline", obj);
  204. }
  205. _readDocumentOutline() {
  206. let obj = this._catDict.get("Outlines");
  207. if (!(obj instanceof _primitives.Dict)) {
  208. return null;
  209. }
  210. obj = obj.getRaw("First");
  211. if (!(obj instanceof _primitives.Ref)) {
  212. return null;
  213. }
  214. const root = {
  215. items: []
  216. };
  217. const queue = [{
  218. obj,
  219. parent: root
  220. }];
  221. const processed = new _primitives.RefSet();
  222. processed.put(obj);
  223. const xref = this.xref,
  224. blackColor = new Uint8ClampedArray(3);
  225. while (queue.length > 0) {
  226. const i = queue.shift();
  227. const outlineDict = xref.fetchIfRef(i.obj);
  228. if (outlineDict === null) {
  229. continue;
  230. }
  231. if (!outlineDict.has("Title")) {
  232. throw new _util.FormatError("Invalid outline item encountered.");
  233. }
  234. const data = {
  235. url: null,
  236. dest: null
  237. };
  238. Catalog.parseDestDictionary({
  239. destDict: outlineDict,
  240. resultObj: data,
  241. docBaseUrl: this.pdfManager.docBaseUrl
  242. });
  243. const title = outlineDict.get("Title");
  244. const flags = outlineDict.get("F") || 0;
  245. const color = outlineDict.getArray("C");
  246. const count = outlineDict.get("Count");
  247. let rgbColor = blackColor;
  248. if (Array.isArray(color) && color.length === 3 && (color[0] !== 0 || color[1] !== 0 || color[2] !== 0)) {
  249. rgbColor = _colorspace.ColorSpace.singletons.rgb.getRgb(color, 0);
  250. }
  251. const outlineItem = {
  252. dest: data.dest,
  253. url: data.url,
  254. unsafeUrl: data.unsafeUrl,
  255. newWindow: data.newWindow,
  256. title: (0, _util.stringToPDFString)(title),
  257. color: rgbColor,
  258. count: Number.isInteger(count) ? count : undefined,
  259. bold: !!(flags & 2),
  260. italic: !!(flags & 1),
  261. items: []
  262. };
  263. i.parent.items.push(outlineItem);
  264. obj = outlineDict.getRaw("First");
  265. if (obj instanceof _primitives.Ref && !processed.has(obj)) {
  266. queue.push({
  267. obj,
  268. parent: outlineItem
  269. });
  270. processed.put(obj);
  271. }
  272. obj = outlineDict.getRaw("Next");
  273. if (obj instanceof _primitives.Ref && !processed.has(obj)) {
  274. queue.push({
  275. obj,
  276. parent: i.parent
  277. });
  278. processed.put(obj);
  279. }
  280. }
  281. return root.items.length > 0 ? root.items : null;
  282. }
  283. get permissions() {
  284. let permissions = null;
  285. try {
  286. permissions = this._readPermissions();
  287. } catch (ex) {
  288. if (ex instanceof _core_utils.MissingDataException) {
  289. throw ex;
  290. }
  291. (0, _util.warn)("Unable to read permissions.");
  292. }
  293. return (0, _util.shadow)(this, "permissions", permissions);
  294. }
  295. _readPermissions() {
  296. const encrypt = this.xref.trailer.get("Encrypt");
  297. if (!(encrypt instanceof _primitives.Dict)) {
  298. return null;
  299. }
  300. let flags = encrypt.get("P");
  301. if (typeof flags !== "number") {
  302. return null;
  303. }
  304. flags += 2 ** 32;
  305. const permissions = [];
  306. for (const key in _util.PermissionFlag) {
  307. const value = _util.PermissionFlag[key];
  308. if (flags & value) {
  309. permissions.push(value);
  310. }
  311. }
  312. return permissions;
  313. }
  314. get optionalContentConfig() {
  315. let config = null;
  316. try {
  317. const properties = this._catDict.get("OCProperties");
  318. if (!properties) {
  319. return (0, _util.shadow)(this, "optionalContentConfig", null);
  320. }
  321. const defaultConfig = properties.get("D");
  322. if (!defaultConfig) {
  323. return (0, _util.shadow)(this, "optionalContentConfig", null);
  324. }
  325. const groupsData = properties.get("OCGs");
  326. if (!Array.isArray(groupsData)) {
  327. return (0, _util.shadow)(this, "optionalContentConfig", null);
  328. }
  329. const groups = [];
  330. const groupRefs = [];
  331. for (const groupRef of groupsData) {
  332. if (!(groupRef instanceof _primitives.Ref)) {
  333. continue;
  334. }
  335. groupRefs.push(groupRef);
  336. const group = this.xref.fetchIfRef(groupRef);
  337. groups.push({
  338. id: groupRef.toString(),
  339. name: typeof group.get("Name") === "string" ? (0, _util.stringToPDFString)(group.get("Name")) : null,
  340. intent: typeof group.get("Intent") === "string" ? (0, _util.stringToPDFString)(group.get("Intent")) : null
  341. });
  342. }
  343. config = this._readOptionalContentConfig(defaultConfig, groupRefs);
  344. config.groups = groups;
  345. } catch (ex) {
  346. if (ex instanceof _core_utils.MissingDataException) {
  347. throw ex;
  348. }
  349. (0, _util.warn)(`Unable to read optional content config: ${ex}`);
  350. }
  351. return (0, _util.shadow)(this, "optionalContentConfig", config);
  352. }
  353. _readOptionalContentConfig(config, contentGroupRefs) {
  354. function parseOnOff(refs) {
  355. const onParsed = [];
  356. if (Array.isArray(refs)) {
  357. for (const value of refs) {
  358. if (!(value instanceof _primitives.Ref)) {
  359. continue;
  360. }
  361. if (contentGroupRefs.includes(value)) {
  362. onParsed.push(value.toString());
  363. }
  364. }
  365. }
  366. return onParsed;
  367. }
  368. function parseOrder(refs, nestedLevels = 0) {
  369. if (!Array.isArray(refs)) {
  370. return null;
  371. }
  372. const order = [];
  373. for (const value of refs) {
  374. if (value instanceof _primitives.Ref && contentGroupRefs.includes(value)) {
  375. parsedOrderRefs.put(value);
  376. order.push(value.toString());
  377. continue;
  378. }
  379. const nestedOrder = parseNestedOrder(value, nestedLevels);
  380. if (nestedOrder) {
  381. order.push(nestedOrder);
  382. }
  383. }
  384. if (nestedLevels > 0) {
  385. return order;
  386. }
  387. const hiddenGroups = [];
  388. for (const groupRef of contentGroupRefs) {
  389. if (parsedOrderRefs.has(groupRef)) {
  390. continue;
  391. }
  392. hiddenGroups.push(groupRef.toString());
  393. }
  394. if (hiddenGroups.length) {
  395. order.push({
  396. name: null,
  397. order: hiddenGroups
  398. });
  399. }
  400. return order;
  401. }
  402. function parseNestedOrder(ref, nestedLevels) {
  403. if (++nestedLevels > MAX_NESTED_LEVELS) {
  404. (0, _util.warn)("parseNestedOrder - reached MAX_NESTED_LEVELS.");
  405. return null;
  406. }
  407. const value = xref.fetchIfRef(ref);
  408. if (!Array.isArray(value)) {
  409. return null;
  410. }
  411. const nestedName = xref.fetchIfRef(value[0]);
  412. if (typeof nestedName !== "string") {
  413. return null;
  414. }
  415. const nestedOrder = parseOrder(value.slice(1), nestedLevels);
  416. if (!nestedOrder || !nestedOrder.length) {
  417. return null;
  418. }
  419. return {
  420. name: (0, _util.stringToPDFString)(nestedName),
  421. order: nestedOrder
  422. };
  423. }
  424. const xref = this.xref,
  425. parsedOrderRefs = new _primitives.RefSet(),
  426. MAX_NESTED_LEVELS = 10;
  427. return {
  428. name: typeof config.get("Name") === "string" ? (0, _util.stringToPDFString)(config.get("Name")) : null,
  429. creator: typeof config.get("Creator") === "string" ? (0, _util.stringToPDFString)(config.get("Creator")) : null,
  430. baseState: config.get("BaseState") instanceof _primitives.Name ? config.get("BaseState").name : null,
  431. on: parseOnOff(config.get("ON")),
  432. off: parseOnOff(config.get("OFF")),
  433. order: parseOrder(config.get("Order")),
  434. groups: null
  435. };
  436. }
  437. setActualNumPages(num = null) {
  438. this._actualNumPages = num;
  439. }
  440. get hasActualNumPages() {
  441. return this._actualNumPages !== null;
  442. }
  443. get _pagesCount() {
  444. const obj = this.toplevelPagesDict.get("Count");
  445. if (!Number.isInteger(obj)) {
  446. throw new _util.FormatError("Page count in top-level pages dictionary is not an integer.");
  447. }
  448. return (0, _util.shadow)(this, "_pagesCount", obj);
  449. }
  450. get numPages() {
  451. return this.hasActualNumPages ? this._actualNumPages : this._pagesCount;
  452. }
  453. get destinations() {
  454. const obj = this._readDests(),
  455. dests = Object.create(null);
  456. if (obj instanceof _name_number_tree.NameTree) {
  457. for (const [key, value] of obj.getAll()) {
  458. const dest = fetchDestination(value);
  459. if (dest) {
  460. dests[(0, _util.stringToPDFString)(key)] = dest;
  461. }
  462. }
  463. } else if (obj instanceof _primitives.Dict) {
  464. obj.forEach(function (key, value) {
  465. const dest = fetchDestination(value);
  466. if (dest) {
  467. dests[key] = dest;
  468. }
  469. });
  470. }
  471. return (0, _util.shadow)(this, "destinations", dests);
  472. }
  473. getDestination(id) {
  474. const obj = this._readDests();
  475. if (obj instanceof _name_number_tree.NameTree) {
  476. const dest = fetchDestination(obj.get(id));
  477. if (dest) {
  478. return dest;
  479. }
  480. const allDest = this.destinations[id];
  481. if (allDest) {
  482. (0, _util.warn)(`Found "${id}" at an incorrect position in the NameTree.`);
  483. return allDest;
  484. }
  485. } else if (obj instanceof _primitives.Dict) {
  486. const dest = fetchDestination(obj.get(id));
  487. if (dest) {
  488. return dest;
  489. }
  490. }
  491. return null;
  492. }
  493. _readDests() {
  494. const obj = this._catDict.get("Names");
  495. if (obj && obj.has("Dests")) {
  496. return new _name_number_tree.NameTree(obj.getRaw("Dests"), this.xref);
  497. } else if (this._catDict.has("Dests")) {
  498. return this._catDict.get("Dests");
  499. }
  500. return undefined;
  501. }
  502. get pageLabels() {
  503. let obj = null;
  504. try {
  505. obj = this._readPageLabels();
  506. } catch (ex) {
  507. if (ex instanceof _core_utils.MissingDataException) {
  508. throw ex;
  509. }
  510. (0, _util.warn)("Unable to read page labels.");
  511. }
  512. return (0, _util.shadow)(this, "pageLabels", obj);
  513. }
  514. _readPageLabels() {
  515. const obj = this._catDict.getRaw("PageLabels");
  516. if (!obj) {
  517. return null;
  518. }
  519. const pageLabels = new Array(this.numPages);
  520. let style = null,
  521. prefix = "";
  522. const numberTree = new _name_number_tree.NumberTree(obj, this.xref);
  523. const nums = numberTree.getAll();
  524. let currentLabel = "",
  525. currentIndex = 1;
  526. for (let i = 0, ii = this.numPages; i < ii; i++) {
  527. const labelDict = nums.get(i);
  528. if (labelDict !== undefined) {
  529. if (!(labelDict instanceof _primitives.Dict)) {
  530. throw new _util.FormatError("PageLabel is not a dictionary.");
  531. }
  532. if (labelDict.has("Type") && !(0, _primitives.isName)(labelDict.get("Type"), "PageLabel")) {
  533. throw new _util.FormatError("Invalid type in PageLabel dictionary.");
  534. }
  535. if (labelDict.has("S")) {
  536. const s = labelDict.get("S");
  537. if (!(s instanceof _primitives.Name)) {
  538. throw new _util.FormatError("Invalid style in PageLabel dictionary.");
  539. }
  540. style = s.name;
  541. } else {
  542. style = null;
  543. }
  544. if (labelDict.has("P")) {
  545. const p = labelDict.get("P");
  546. if (typeof p !== "string") {
  547. throw new _util.FormatError("Invalid prefix in PageLabel dictionary.");
  548. }
  549. prefix = (0, _util.stringToPDFString)(p);
  550. } else {
  551. prefix = "";
  552. }
  553. if (labelDict.has("St")) {
  554. const st = labelDict.get("St");
  555. if (!(Number.isInteger(st) && st >= 1)) {
  556. throw new _util.FormatError("Invalid start in PageLabel dictionary.");
  557. }
  558. currentIndex = st;
  559. } else {
  560. currentIndex = 1;
  561. }
  562. }
  563. switch (style) {
  564. case "D":
  565. currentLabel = currentIndex;
  566. break;
  567. case "R":
  568. case "r":
  569. currentLabel = (0, _core_utils.toRomanNumerals)(currentIndex, style === "r");
  570. break;
  571. case "A":
  572. case "a":
  573. const LIMIT = 26;
  574. const A_UPPER_CASE = 0x41,
  575. A_LOWER_CASE = 0x61;
  576. const baseCharCode = style === "a" ? A_LOWER_CASE : A_UPPER_CASE;
  577. const letterIndex = currentIndex - 1;
  578. const character = String.fromCharCode(baseCharCode + letterIndex % LIMIT);
  579. currentLabel = character.repeat(Math.floor(letterIndex / LIMIT) + 1);
  580. break;
  581. default:
  582. if (style) {
  583. throw new _util.FormatError(`Invalid style "${style}" in PageLabel dictionary.`);
  584. }
  585. currentLabel = "";
  586. }
  587. pageLabels[i] = prefix + currentLabel;
  588. currentIndex++;
  589. }
  590. return pageLabels;
  591. }
  592. get pageLayout() {
  593. const obj = this._catDict.get("PageLayout");
  594. let pageLayout = "";
  595. if (obj instanceof _primitives.Name) {
  596. switch (obj.name) {
  597. case "SinglePage":
  598. case "OneColumn":
  599. case "TwoColumnLeft":
  600. case "TwoColumnRight":
  601. case "TwoPageLeft":
  602. case "TwoPageRight":
  603. pageLayout = obj.name;
  604. }
  605. }
  606. return (0, _util.shadow)(this, "pageLayout", pageLayout);
  607. }
  608. get pageMode() {
  609. const obj = this._catDict.get("PageMode");
  610. let pageMode = "UseNone";
  611. if (obj instanceof _primitives.Name) {
  612. switch (obj.name) {
  613. case "UseNone":
  614. case "UseOutlines":
  615. case "UseThumbs":
  616. case "FullScreen":
  617. case "UseOC":
  618. case "UseAttachments":
  619. pageMode = obj.name;
  620. }
  621. }
  622. return (0, _util.shadow)(this, "pageMode", pageMode);
  623. }
  624. get viewerPreferences() {
  625. const obj = this._catDict.get("ViewerPreferences");
  626. if (!(obj instanceof _primitives.Dict)) {
  627. return (0, _util.shadow)(this, "viewerPreferences", null);
  628. }
  629. let prefs = null;
  630. for (const key of obj.getKeys()) {
  631. const value = obj.get(key);
  632. let prefValue;
  633. switch (key) {
  634. case "HideToolbar":
  635. case "HideMenubar":
  636. case "HideWindowUI":
  637. case "FitWindow":
  638. case "CenterWindow":
  639. case "DisplayDocTitle":
  640. case "PickTrayByPDFSize":
  641. if (typeof value === "boolean") {
  642. prefValue = value;
  643. }
  644. break;
  645. case "NonFullScreenPageMode":
  646. if (value instanceof _primitives.Name) {
  647. switch (value.name) {
  648. case "UseNone":
  649. case "UseOutlines":
  650. case "UseThumbs":
  651. case "UseOC":
  652. prefValue = value.name;
  653. break;
  654. default:
  655. prefValue = "UseNone";
  656. }
  657. }
  658. break;
  659. case "Direction":
  660. if (value instanceof _primitives.Name) {
  661. switch (value.name) {
  662. case "L2R":
  663. case "R2L":
  664. prefValue = value.name;
  665. break;
  666. default:
  667. prefValue = "L2R";
  668. }
  669. }
  670. break;
  671. case "ViewArea":
  672. case "ViewClip":
  673. case "PrintArea":
  674. case "PrintClip":
  675. if (value instanceof _primitives.Name) {
  676. switch (value.name) {
  677. case "MediaBox":
  678. case "CropBox":
  679. case "BleedBox":
  680. case "TrimBox":
  681. case "ArtBox":
  682. prefValue = value.name;
  683. break;
  684. default:
  685. prefValue = "CropBox";
  686. }
  687. }
  688. break;
  689. case "PrintScaling":
  690. if (value instanceof _primitives.Name) {
  691. switch (value.name) {
  692. case "None":
  693. case "AppDefault":
  694. prefValue = value.name;
  695. break;
  696. default:
  697. prefValue = "AppDefault";
  698. }
  699. }
  700. break;
  701. case "Duplex":
  702. if (value instanceof _primitives.Name) {
  703. switch (value.name) {
  704. case "Simplex":
  705. case "DuplexFlipShortEdge":
  706. case "DuplexFlipLongEdge":
  707. prefValue = value.name;
  708. break;
  709. default:
  710. prefValue = "None";
  711. }
  712. }
  713. break;
  714. case "PrintPageRange":
  715. if (Array.isArray(value) && value.length % 2 === 0) {
  716. const isValid = value.every((page, i, arr) => {
  717. return Number.isInteger(page) && page > 0 && (i === 0 || page >= arr[i - 1]) && page <= this.numPages;
  718. });
  719. if (isValid) {
  720. prefValue = value;
  721. }
  722. }
  723. break;
  724. case "NumCopies":
  725. if (Number.isInteger(value) && value > 0) {
  726. prefValue = value;
  727. }
  728. break;
  729. default:
  730. (0, _util.warn)(`Ignoring non-standard key in ViewerPreferences: ${key}.`);
  731. continue;
  732. }
  733. if (prefValue === undefined) {
  734. (0, _util.warn)(`Bad value, for key "${key}", in ViewerPreferences: ${value}.`);
  735. continue;
  736. }
  737. if (!prefs) {
  738. prefs = Object.create(null);
  739. }
  740. prefs[key] = prefValue;
  741. }
  742. return (0, _util.shadow)(this, "viewerPreferences", prefs);
  743. }
  744. get openAction() {
  745. const obj = this._catDict.get("OpenAction");
  746. const openAction = Object.create(null);
  747. if (obj instanceof _primitives.Dict) {
  748. const destDict = new _primitives.Dict(this.xref);
  749. destDict.set("A", obj);
  750. const resultObj = {
  751. url: null,
  752. dest: null,
  753. action: null
  754. };
  755. Catalog.parseDestDictionary({
  756. destDict,
  757. resultObj
  758. });
  759. if (Array.isArray(resultObj.dest)) {
  760. openAction.dest = resultObj.dest;
  761. } else if (resultObj.action) {
  762. openAction.action = resultObj.action;
  763. }
  764. } else if (Array.isArray(obj)) {
  765. openAction.dest = obj;
  766. }
  767. return (0, _util.shadow)(this, "openAction", (0, _util.objectSize)(openAction) > 0 ? openAction : null);
  768. }
  769. get attachments() {
  770. const obj = this._catDict.get("Names");
  771. let attachments = null;
  772. if (obj instanceof _primitives.Dict && obj.has("EmbeddedFiles")) {
  773. const nameTree = new _name_number_tree.NameTree(obj.getRaw("EmbeddedFiles"), this.xref);
  774. for (const [key, value] of nameTree.getAll()) {
  775. const fs = new _file_spec.FileSpec(value, this.xref);
  776. if (!attachments) {
  777. attachments = Object.create(null);
  778. }
  779. attachments[(0, _util.stringToPDFString)(key)] = fs.serializable;
  780. }
  781. }
  782. return (0, _util.shadow)(this, "attachments", attachments);
  783. }
  784. get xfaImages() {
  785. const obj = this._catDict.get("Names");
  786. let xfaImages = null;
  787. if (obj instanceof _primitives.Dict && obj.has("XFAImages")) {
  788. const nameTree = new _name_number_tree.NameTree(obj.getRaw("XFAImages"), this.xref);
  789. for (const [key, value] of nameTree.getAll()) {
  790. if (!xfaImages) {
  791. xfaImages = new _primitives.Dict(this.xref);
  792. }
  793. xfaImages.set((0, _util.stringToPDFString)(key), value);
  794. }
  795. }
  796. return (0, _util.shadow)(this, "xfaImages", xfaImages);
  797. }
  798. _collectJavaScript() {
  799. const obj = this._catDict.get("Names");
  800. let javaScript = null;
  801. function appendIfJavaScriptDict(name, jsDict) {
  802. if (!(jsDict instanceof _primitives.Dict)) {
  803. return;
  804. }
  805. if (!(0, _primitives.isName)(jsDict.get("S"), "JavaScript")) {
  806. return;
  807. }
  808. let js = jsDict.get("JS");
  809. if (js instanceof _base_stream.BaseStream) {
  810. js = js.getString();
  811. } else if (typeof js !== "string") {
  812. return;
  813. }
  814. if (javaScript === null) {
  815. javaScript = new Map();
  816. }
  817. javaScript.set(name, (0, _util.stringToPDFString)(js));
  818. }
  819. if (obj instanceof _primitives.Dict && obj.has("JavaScript")) {
  820. const nameTree = new _name_number_tree.NameTree(obj.getRaw("JavaScript"), this.xref);
  821. for (const [key, value] of nameTree.getAll()) {
  822. appendIfJavaScriptDict((0, _util.stringToPDFString)(key), value);
  823. }
  824. }
  825. const openAction = this._catDict.get("OpenAction");
  826. if (openAction) {
  827. appendIfJavaScriptDict("OpenAction", openAction);
  828. }
  829. return javaScript;
  830. }
  831. get javaScript() {
  832. const javaScript = this._collectJavaScript();
  833. return (0, _util.shadow)(this, "javaScript", javaScript ? [...javaScript.values()] : null);
  834. }
  835. get jsActions() {
  836. const javaScript = this._collectJavaScript();
  837. let actions = (0, _core_utils.collectActions)(this.xref, this._catDict, _util.DocumentActionEventType);
  838. if (javaScript) {
  839. if (!actions) {
  840. actions = Object.create(null);
  841. }
  842. for (const [key, val] of javaScript) {
  843. if (key in actions) {
  844. actions[key].push(val);
  845. } else {
  846. actions[key] = [val];
  847. }
  848. }
  849. }
  850. return (0, _util.shadow)(this, "jsActions", actions);
  851. }
  852. async fontFallback(id, handler) {
  853. const translatedFonts = await Promise.all(this.fontCache);
  854. for (const translatedFont of translatedFonts) {
  855. if (translatedFont.loadedName === id) {
  856. translatedFont.fallback(handler);
  857. return;
  858. }
  859. }
  860. }
  861. async cleanup(manuallyTriggered = false) {
  862. (0, _cleanup_helper.clearGlobalCaches)();
  863. this.globalImageCache.clear(manuallyTriggered);
  864. this.pageKidsCountCache.clear();
  865. this.pageIndexCache.clear();
  866. this.nonBlendModesSet.clear();
  867. const translatedFonts = await Promise.all(this.fontCache);
  868. for (const {
  869. dict
  870. } of translatedFonts) {
  871. delete dict.cacheKey;
  872. }
  873. this.fontCache.clear();
  874. this.builtInCMapCache.clear();
  875. this.standardFontDataCache.clear();
  876. }
  877. async getPageDict(pageIndex) {
  878. const nodesToVisit = [this.toplevelPagesDict];
  879. const visitedNodes = new _primitives.RefSet();
  880. const pagesRef = this._catDict.getRaw("Pages");
  881. if (pagesRef instanceof _primitives.Ref) {
  882. visitedNodes.put(pagesRef);
  883. }
  884. const xref = this.xref,
  885. pageKidsCountCache = this.pageKidsCountCache,
  886. pageIndexCache = this.pageIndexCache;
  887. let currentPageIndex = 0;
  888. while (nodesToVisit.length) {
  889. const currentNode = nodesToVisit.pop();
  890. if (currentNode instanceof _primitives.Ref) {
  891. const count = pageKidsCountCache.get(currentNode);
  892. if (count >= 0 && currentPageIndex + count <= pageIndex) {
  893. currentPageIndex += count;
  894. continue;
  895. }
  896. if (visitedNodes.has(currentNode)) {
  897. throw new _util.FormatError("Pages tree contains circular reference.");
  898. }
  899. visitedNodes.put(currentNode);
  900. const obj = await xref.fetchAsync(currentNode);
  901. if (obj instanceof _primitives.Dict) {
  902. let type = obj.getRaw("Type");
  903. if (type instanceof _primitives.Ref) {
  904. type = await xref.fetchAsync(type);
  905. }
  906. if ((0, _primitives.isName)(type, "Page") || !obj.has("Kids")) {
  907. if (!pageKidsCountCache.has(currentNode)) {
  908. pageKidsCountCache.put(currentNode, 1);
  909. }
  910. if (!pageIndexCache.has(currentNode)) {
  911. pageIndexCache.put(currentNode, currentPageIndex);
  912. }
  913. if (currentPageIndex === pageIndex) {
  914. return [obj, currentNode];
  915. }
  916. currentPageIndex++;
  917. continue;
  918. }
  919. }
  920. nodesToVisit.push(obj);
  921. continue;
  922. }
  923. if (!(currentNode instanceof _primitives.Dict)) {
  924. throw new _util.FormatError("Page dictionary kid reference points to wrong type of object.");
  925. }
  926. const {
  927. objId
  928. } = currentNode;
  929. let count = currentNode.getRaw("Count");
  930. if (count instanceof _primitives.Ref) {
  931. count = await xref.fetchAsync(count);
  932. }
  933. if (Number.isInteger(count) && count >= 0) {
  934. if (objId && !pageKidsCountCache.has(objId)) {
  935. pageKidsCountCache.put(objId, count);
  936. }
  937. if (currentPageIndex + count <= pageIndex) {
  938. currentPageIndex += count;
  939. continue;
  940. }
  941. }
  942. let kids = currentNode.getRaw("Kids");
  943. if (kids instanceof _primitives.Ref) {
  944. kids = await xref.fetchAsync(kids);
  945. }
  946. if (!Array.isArray(kids)) {
  947. let type = currentNode.getRaw("Type");
  948. if (type instanceof _primitives.Ref) {
  949. type = await xref.fetchAsync(type);
  950. }
  951. if ((0, _primitives.isName)(type, "Page") || !currentNode.has("Kids")) {
  952. if (currentPageIndex === pageIndex) {
  953. return [currentNode, null];
  954. }
  955. currentPageIndex++;
  956. continue;
  957. }
  958. throw new _util.FormatError("Page dictionary kids object is not an array.");
  959. }
  960. for (let last = kids.length - 1; last >= 0; last--) {
  961. nodesToVisit.push(kids[last]);
  962. }
  963. }
  964. throw new Error(`Page index ${pageIndex} not found.`);
  965. }
  966. async getAllPageDicts(recoveryMode = false) {
  967. const queue = [{
  968. currentNode: this.toplevelPagesDict,
  969. posInKids: 0
  970. }];
  971. const visitedNodes = new _primitives.RefSet();
  972. const pagesRef = this._catDict.getRaw("Pages");
  973. if (pagesRef instanceof _primitives.Ref) {
  974. visitedNodes.put(pagesRef);
  975. }
  976. const map = new Map(),
  977. xref = this.xref,
  978. pageIndexCache = this.pageIndexCache;
  979. let pageIndex = 0;
  980. function addPageDict(pageDict, pageRef) {
  981. if (pageRef && !pageIndexCache.has(pageRef)) {
  982. pageIndexCache.put(pageRef, pageIndex);
  983. }
  984. map.set(pageIndex++, [pageDict, pageRef]);
  985. }
  986. function addPageError(error) {
  987. if (error instanceof _core_utils.XRefEntryException && !recoveryMode) {
  988. throw error;
  989. }
  990. map.set(pageIndex++, [error, null]);
  991. }
  992. while (queue.length > 0) {
  993. const queueItem = queue[queue.length - 1];
  994. const {
  995. currentNode,
  996. posInKids
  997. } = queueItem;
  998. let kids = currentNode.getRaw("Kids");
  999. if (kids instanceof _primitives.Ref) {
  1000. try {
  1001. kids = await xref.fetchAsync(kids);
  1002. } catch (ex) {
  1003. addPageError(ex);
  1004. break;
  1005. }
  1006. }
  1007. if (!Array.isArray(kids)) {
  1008. addPageError(new _util.FormatError("Page dictionary kids object is not an array."));
  1009. break;
  1010. }
  1011. if (posInKids >= kids.length) {
  1012. queue.pop();
  1013. continue;
  1014. }
  1015. const kidObj = kids[posInKids];
  1016. let obj;
  1017. if (kidObj instanceof _primitives.Ref) {
  1018. if (visitedNodes.has(kidObj)) {
  1019. addPageError(new _util.FormatError("Pages tree contains circular reference."));
  1020. break;
  1021. }
  1022. visitedNodes.put(kidObj);
  1023. try {
  1024. obj = await xref.fetchAsync(kidObj);
  1025. } catch (ex) {
  1026. addPageError(ex);
  1027. break;
  1028. }
  1029. } else {
  1030. obj = kidObj;
  1031. }
  1032. if (!(obj instanceof _primitives.Dict)) {
  1033. addPageError(new _util.FormatError("Page dictionary kid reference points to wrong type of object."));
  1034. break;
  1035. }
  1036. let type = obj.getRaw("Type");
  1037. if (type instanceof _primitives.Ref) {
  1038. try {
  1039. type = await xref.fetchAsync(type);
  1040. } catch (ex) {
  1041. addPageError(ex);
  1042. break;
  1043. }
  1044. }
  1045. if ((0, _primitives.isName)(type, "Page") || !obj.has("Kids")) {
  1046. addPageDict(obj, kidObj instanceof _primitives.Ref ? kidObj : null);
  1047. } else {
  1048. queue.push({
  1049. currentNode: obj,
  1050. posInKids: 0
  1051. });
  1052. }
  1053. queueItem.posInKids++;
  1054. }
  1055. return map;
  1056. }
  1057. getPageIndex(pageRef) {
  1058. const cachedPageIndex = this.pageIndexCache.get(pageRef);
  1059. if (cachedPageIndex !== undefined) {
  1060. return Promise.resolve(cachedPageIndex);
  1061. }
  1062. const xref = this.xref;
  1063. function pagesBeforeRef(kidRef) {
  1064. let total = 0,
  1065. parentRef;
  1066. return xref.fetchAsync(kidRef).then(function (node) {
  1067. if ((0, _primitives.isRefsEqual)(kidRef, pageRef) && !(0, _primitives.isDict)(node, "Page") && !(node instanceof _primitives.Dict && !node.has("Type") && node.has("Contents"))) {
  1068. throw new _util.FormatError("The reference does not point to a /Page dictionary.");
  1069. }
  1070. if (!node) {
  1071. return null;
  1072. }
  1073. if (!(node instanceof _primitives.Dict)) {
  1074. throw new _util.FormatError("Node must be a dictionary.");
  1075. }
  1076. parentRef = node.getRaw("Parent");
  1077. return node.getAsync("Parent");
  1078. }).then(function (parent) {
  1079. if (!parent) {
  1080. return null;
  1081. }
  1082. if (!(parent instanceof _primitives.Dict)) {
  1083. throw new _util.FormatError("Parent must be a dictionary.");
  1084. }
  1085. return parent.getAsync("Kids");
  1086. }).then(function (kids) {
  1087. if (!kids) {
  1088. return null;
  1089. }
  1090. const kidPromises = [];
  1091. let found = false;
  1092. for (let i = 0, ii = kids.length; i < ii; i++) {
  1093. const kid = kids[i];
  1094. if (!(kid instanceof _primitives.Ref)) {
  1095. throw new _util.FormatError("Kid must be a reference.");
  1096. }
  1097. if ((0, _primitives.isRefsEqual)(kid, kidRef)) {
  1098. found = true;
  1099. break;
  1100. }
  1101. kidPromises.push(xref.fetchAsync(kid).then(function (obj) {
  1102. if (!(obj instanceof _primitives.Dict)) {
  1103. throw new _util.FormatError("Kid node must be a dictionary.");
  1104. }
  1105. if (obj.has("Count")) {
  1106. total += obj.get("Count");
  1107. } else {
  1108. total++;
  1109. }
  1110. }));
  1111. }
  1112. if (!found) {
  1113. throw new _util.FormatError("Kid reference not found in parent's kids.");
  1114. }
  1115. return Promise.all(kidPromises).then(function () {
  1116. return [total, parentRef];
  1117. });
  1118. });
  1119. }
  1120. let total = 0;
  1121. const next = ref => pagesBeforeRef(ref).then(args => {
  1122. if (!args) {
  1123. this.pageIndexCache.put(pageRef, total);
  1124. return total;
  1125. }
  1126. const [count, parentRef] = args;
  1127. total += count;
  1128. return next(parentRef);
  1129. });
  1130. return next(pageRef);
  1131. }
  1132. get baseUrl() {
  1133. const uri = this._catDict.get("URI");
  1134. if (uri instanceof _primitives.Dict) {
  1135. const base = uri.get("Base");
  1136. if (typeof base === "string") {
  1137. const absoluteUrl = (0, _util.createValidAbsoluteUrl)(base, null, {
  1138. tryConvertEncoding: true
  1139. });
  1140. if (absoluteUrl) {
  1141. return (0, _util.shadow)(this, "baseUrl", absoluteUrl.href);
  1142. }
  1143. }
  1144. }
  1145. return (0, _util.shadow)(this, "baseUrl", null);
  1146. }
  1147. static parseDestDictionary(params) {
  1148. const destDict = params.destDict;
  1149. if (!(destDict instanceof _primitives.Dict)) {
  1150. (0, _util.warn)("parseDestDictionary: `destDict` must be a dictionary.");
  1151. return;
  1152. }
  1153. const resultObj = params.resultObj;
  1154. if (typeof resultObj !== "object") {
  1155. (0, _util.warn)("parseDestDictionary: `resultObj` must be an object.");
  1156. return;
  1157. }
  1158. const docBaseUrl = params.docBaseUrl || null;
  1159. let action = destDict.get("A"),
  1160. url,
  1161. dest;
  1162. if (!(action instanceof _primitives.Dict)) {
  1163. if (destDict.has("Dest")) {
  1164. action = destDict.get("Dest");
  1165. } else {
  1166. action = destDict.get("AA");
  1167. if (action instanceof _primitives.Dict) {
  1168. if (action.has("D")) {
  1169. action = action.get("D");
  1170. } else if (action.has("U")) {
  1171. action = action.get("U");
  1172. }
  1173. }
  1174. }
  1175. }
  1176. if (action instanceof _primitives.Dict) {
  1177. const actionType = action.get("S");
  1178. if (!(actionType instanceof _primitives.Name)) {
  1179. (0, _util.warn)("parseDestDictionary: Invalid type in Action dictionary.");
  1180. return;
  1181. }
  1182. const actionName = actionType.name;
  1183. switch (actionName) {
  1184. case "ResetForm":
  1185. const flags = action.get("Flags");
  1186. const include = ((typeof flags === "number" ? flags : 0) & 1) === 0;
  1187. const fields = [];
  1188. const refs = [];
  1189. for (const obj of action.get("Fields") || []) {
  1190. if (obj instanceof _primitives.Ref) {
  1191. refs.push(obj.toString());
  1192. } else if (typeof obj === "string") {
  1193. fields.push((0, _util.stringToPDFString)(obj));
  1194. }
  1195. }
  1196. resultObj.resetForm = {
  1197. fields,
  1198. refs,
  1199. include
  1200. };
  1201. break;
  1202. case "URI":
  1203. url = action.get("URI");
  1204. if (url instanceof _primitives.Name) {
  1205. url = "/" + url.name;
  1206. }
  1207. break;
  1208. case "GoTo":
  1209. dest = action.get("D");
  1210. break;
  1211. case "Launch":
  1212. case "GoToR":
  1213. const urlDict = action.get("F");
  1214. if (urlDict instanceof _primitives.Dict) {
  1215. url = urlDict.get("F") || null;
  1216. } else if (typeof urlDict === "string") {
  1217. url = urlDict;
  1218. }
  1219. let remoteDest = action.get("D");
  1220. if (remoteDest) {
  1221. if (remoteDest instanceof _primitives.Name) {
  1222. remoteDest = remoteDest.name;
  1223. }
  1224. if (typeof url === "string") {
  1225. const baseUrl = url.split("#")[0];
  1226. if (typeof remoteDest === "string") {
  1227. url = baseUrl + "#" + remoteDest;
  1228. } else if (Array.isArray(remoteDest)) {
  1229. url = baseUrl + "#" + JSON.stringify(remoteDest);
  1230. }
  1231. }
  1232. }
  1233. const newWindow = action.get("NewWindow");
  1234. if (typeof newWindow === "boolean") {
  1235. resultObj.newWindow = newWindow;
  1236. }
  1237. break;
  1238. case "Named":
  1239. const namedAction = action.get("N");
  1240. if (namedAction instanceof _primitives.Name) {
  1241. resultObj.action = namedAction.name;
  1242. }
  1243. break;
  1244. case "JavaScript":
  1245. const jsAction = action.get("JS");
  1246. let js;
  1247. if (jsAction instanceof _base_stream.BaseStream) {
  1248. js = jsAction.getString();
  1249. } else if (typeof jsAction === "string") {
  1250. js = jsAction;
  1251. }
  1252. const jsURL = js && (0, _core_utils.recoverJsURL)((0, _util.stringToPDFString)(js));
  1253. if (jsURL) {
  1254. url = jsURL.url;
  1255. resultObj.newWindow = jsURL.newWindow;
  1256. break;
  1257. }
  1258. default:
  1259. if (actionName === "JavaScript" || actionName === "SubmitForm") {
  1260. break;
  1261. }
  1262. (0, _util.warn)(`parseDestDictionary - unsupported action: "${actionName}".`);
  1263. break;
  1264. }
  1265. } else if (destDict.has("Dest")) {
  1266. dest = destDict.get("Dest");
  1267. }
  1268. if (typeof url === "string") {
  1269. const absoluteUrl = (0, _util.createValidAbsoluteUrl)(url, docBaseUrl, {
  1270. addDefaultProtocol: true,
  1271. tryConvertEncoding: true
  1272. });
  1273. if (absoluteUrl) {
  1274. resultObj.url = absoluteUrl.href;
  1275. }
  1276. resultObj.unsafeUrl = url;
  1277. }
  1278. if (dest) {
  1279. if (dest instanceof _primitives.Name) {
  1280. dest = dest.name;
  1281. }
  1282. if (typeof dest === "string") {
  1283. resultObj.dest = (0, _util.stringToPDFString)(dest);
  1284. } else if (Array.isArray(dest)) {
  1285. resultObj.dest = dest;
  1286. }
  1287. }
  1288. }
  1289. }
  1290. exports.Catalog = Catalog;