catalog.js 42 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672
  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. js = (0, _util.stringToPDFString)(js).replace(/\u0000/g, "");
  818. javaScript.set(name, js);
  819. }
  820. if (obj instanceof _primitives.Dict && obj.has("JavaScript")) {
  821. const nameTree = new _name_number_tree.NameTree(obj.getRaw("JavaScript"), this.xref);
  822. for (const [key, value] of nameTree.getAll()) {
  823. appendIfJavaScriptDict((0, _util.stringToPDFString)(key), value);
  824. }
  825. }
  826. const openAction = this._catDict.get("OpenAction");
  827. if (openAction) {
  828. appendIfJavaScriptDict("OpenAction", openAction);
  829. }
  830. return javaScript;
  831. }
  832. get javaScript() {
  833. const javaScript = this._collectJavaScript();
  834. return (0, _util.shadow)(this, "javaScript", javaScript ? [...javaScript.values()] : null);
  835. }
  836. get jsActions() {
  837. const javaScript = this._collectJavaScript();
  838. let actions = (0, _core_utils.collectActions)(this.xref, this._catDict, _util.DocumentActionEventType);
  839. if (javaScript) {
  840. if (!actions) {
  841. actions = Object.create(null);
  842. }
  843. for (const [key, val] of javaScript) {
  844. if (key in actions) {
  845. actions[key].push(val);
  846. } else {
  847. actions[key] = [val];
  848. }
  849. }
  850. }
  851. return (0, _util.shadow)(this, "jsActions", actions);
  852. }
  853. async fontFallback(id, handler) {
  854. const translatedFonts = await Promise.all(this.fontCache);
  855. for (const translatedFont of translatedFonts) {
  856. if (translatedFont.loadedName === id) {
  857. translatedFont.fallback(handler);
  858. return;
  859. }
  860. }
  861. }
  862. async cleanup(manuallyTriggered = false) {
  863. (0, _cleanup_helper.clearGlobalCaches)();
  864. this.globalImageCache.clear(manuallyTriggered);
  865. this.pageKidsCountCache.clear();
  866. this.pageIndexCache.clear();
  867. this.nonBlendModesSet.clear();
  868. const translatedFonts = await Promise.all(this.fontCache);
  869. for (const {
  870. dict
  871. } of translatedFonts) {
  872. delete dict.cacheKey;
  873. }
  874. this.fontCache.clear();
  875. this.builtInCMapCache.clear();
  876. this.standardFontDataCache.clear();
  877. }
  878. async getPageDict(pageIndex) {
  879. const nodesToVisit = [this.toplevelPagesDict];
  880. const visitedNodes = new _primitives.RefSet();
  881. const pagesRef = this._catDict.getRaw("Pages");
  882. if (pagesRef instanceof _primitives.Ref) {
  883. visitedNodes.put(pagesRef);
  884. }
  885. const xref = this.xref,
  886. pageKidsCountCache = this.pageKidsCountCache,
  887. pageIndexCache = this.pageIndexCache;
  888. let currentPageIndex = 0;
  889. while (nodesToVisit.length) {
  890. const currentNode = nodesToVisit.pop();
  891. if (currentNode instanceof _primitives.Ref) {
  892. const count = pageKidsCountCache.get(currentNode);
  893. if (count >= 0 && currentPageIndex + count <= pageIndex) {
  894. currentPageIndex += count;
  895. continue;
  896. }
  897. if (visitedNodes.has(currentNode)) {
  898. throw new _util.FormatError("Pages tree contains circular reference.");
  899. }
  900. visitedNodes.put(currentNode);
  901. const obj = await xref.fetchAsync(currentNode);
  902. if (obj instanceof _primitives.Dict) {
  903. let type = obj.getRaw("Type");
  904. if (type instanceof _primitives.Ref) {
  905. type = await xref.fetchAsync(type);
  906. }
  907. if ((0, _primitives.isName)(type, "Page") || !obj.has("Kids")) {
  908. if (!pageKidsCountCache.has(currentNode)) {
  909. pageKidsCountCache.put(currentNode, 1);
  910. }
  911. if (!pageIndexCache.has(currentNode)) {
  912. pageIndexCache.put(currentNode, currentPageIndex);
  913. }
  914. if (currentPageIndex === pageIndex) {
  915. return [obj, currentNode];
  916. }
  917. currentPageIndex++;
  918. continue;
  919. }
  920. }
  921. nodesToVisit.push(obj);
  922. continue;
  923. }
  924. if (!(currentNode instanceof _primitives.Dict)) {
  925. throw new _util.FormatError("Page dictionary kid reference points to wrong type of object.");
  926. }
  927. const {
  928. objId
  929. } = currentNode;
  930. let count = currentNode.getRaw("Count");
  931. if (count instanceof _primitives.Ref) {
  932. count = await xref.fetchAsync(count);
  933. }
  934. if (Number.isInteger(count) && count >= 0) {
  935. if (objId && !pageKidsCountCache.has(objId)) {
  936. pageKidsCountCache.put(objId, count);
  937. }
  938. if (currentPageIndex + count <= pageIndex) {
  939. currentPageIndex += count;
  940. continue;
  941. }
  942. }
  943. let kids = currentNode.getRaw("Kids");
  944. if (kids instanceof _primitives.Ref) {
  945. kids = await xref.fetchAsync(kids);
  946. }
  947. if (!Array.isArray(kids)) {
  948. let type = currentNode.getRaw("Type");
  949. if (type instanceof _primitives.Ref) {
  950. type = await xref.fetchAsync(type);
  951. }
  952. if ((0, _primitives.isName)(type, "Page") || !currentNode.has("Kids")) {
  953. if (currentPageIndex === pageIndex) {
  954. return [currentNode, null];
  955. }
  956. currentPageIndex++;
  957. continue;
  958. }
  959. throw new _util.FormatError("Page dictionary kids object is not an array.");
  960. }
  961. for (let last = kids.length - 1; last >= 0; last--) {
  962. nodesToVisit.push(kids[last]);
  963. }
  964. }
  965. throw new Error(`Page index ${pageIndex} not found.`);
  966. }
  967. async getAllPageDicts(recoveryMode = false) {
  968. const queue = [{
  969. currentNode: this.toplevelPagesDict,
  970. posInKids: 0
  971. }];
  972. const visitedNodes = new _primitives.RefSet();
  973. const pagesRef = this._catDict.getRaw("Pages");
  974. if (pagesRef instanceof _primitives.Ref) {
  975. visitedNodes.put(pagesRef);
  976. }
  977. const map = new Map(),
  978. xref = this.xref,
  979. pageIndexCache = this.pageIndexCache;
  980. let pageIndex = 0;
  981. function addPageDict(pageDict, pageRef) {
  982. if (pageRef && !pageIndexCache.has(pageRef)) {
  983. pageIndexCache.put(pageRef, pageIndex);
  984. }
  985. map.set(pageIndex++, [pageDict, pageRef]);
  986. }
  987. function addPageError(error) {
  988. if (error instanceof _core_utils.XRefEntryException && !recoveryMode) {
  989. throw error;
  990. }
  991. map.set(pageIndex++, [error, null]);
  992. }
  993. while (queue.length > 0) {
  994. const queueItem = queue.at(-1);
  995. const {
  996. currentNode,
  997. posInKids
  998. } = queueItem;
  999. let kids = currentNode.getRaw("Kids");
  1000. if (kids instanceof _primitives.Ref) {
  1001. try {
  1002. kids = await xref.fetchAsync(kids);
  1003. } catch (ex) {
  1004. addPageError(ex);
  1005. break;
  1006. }
  1007. }
  1008. if (!Array.isArray(kids)) {
  1009. addPageError(new _util.FormatError("Page dictionary kids object is not an array."));
  1010. break;
  1011. }
  1012. if (posInKids >= kids.length) {
  1013. queue.pop();
  1014. continue;
  1015. }
  1016. const kidObj = kids[posInKids];
  1017. let obj;
  1018. if (kidObj instanceof _primitives.Ref) {
  1019. if (visitedNodes.has(kidObj)) {
  1020. addPageError(new _util.FormatError("Pages tree contains circular reference."));
  1021. break;
  1022. }
  1023. visitedNodes.put(kidObj);
  1024. try {
  1025. obj = await xref.fetchAsync(kidObj);
  1026. } catch (ex) {
  1027. addPageError(ex);
  1028. break;
  1029. }
  1030. } else {
  1031. obj = kidObj;
  1032. }
  1033. if (!(obj instanceof _primitives.Dict)) {
  1034. addPageError(new _util.FormatError("Page dictionary kid reference points to wrong type of object."));
  1035. break;
  1036. }
  1037. let type = obj.getRaw("Type");
  1038. if (type instanceof _primitives.Ref) {
  1039. try {
  1040. type = await xref.fetchAsync(type);
  1041. } catch (ex) {
  1042. addPageError(ex);
  1043. break;
  1044. }
  1045. }
  1046. if ((0, _primitives.isName)(type, "Page") || !obj.has("Kids")) {
  1047. addPageDict(obj, kidObj instanceof _primitives.Ref ? kidObj : null);
  1048. } else {
  1049. queue.push({
  1050. currentNode: obj,
  1051. posInKids: 0
  1052. });
  1053. }
  1054. queueItem.posInKids++;
  1055. }
  1056. return map;
  1057. }
  1058. getPageIndex(pageRef) {
  1059. const cachedPageIndex = this.pageIndexCache.get(pageRef);
  1060. if (cachedPageIndex !== undefined) {
  1061. return Promise.resolve(cachedPageIndex);
  1062. }
  1063. const xref = this.xref;
  1064. function pagesBeforeRef(kidRef) {
  1065. let total = 0,
  1066. parentRef;
  1067. return xref.fetchAsync(kidRef).then(function (node) {
  1068. if ((0, _primitives.isRefsEqual)(kidRef, pageRef) && !(0, _primitives.isDict)(node, "Page") && !(node instanceof _primitives.Dict && !node.has("Type") && node.has("Contents"))) {
  1069. throw new _util.FormatError("The reference does not point to a /Page dictionary.");
  1070. }
  1071. if (!node) {
  1072. return null;
  1073. }
  1074. if (!(node instanceof _primitives.Dict)) {
  1075. throw new _util.FormatError("Node must be a dictionary.");
  1076. }
  1077. parentRef = node.getRaw("Parent");
  1078. return node.getAsync("Parent");
  1079. }).then(function (parent) {
  1080. if (!parent) {
  1081. return null;
  1082. }
  1083. if (!(parent instanceof _primitives.Dict)) {
  1084. throw new _util.FormatError("Parent must be a dictionary.");
  1085. }
  1086. return parent.getAsync("Kids");
  1087. }).then(function (kids) {
  1088. if (!kids) {
  1089. return null;
  1090. }
  1091. const kidPromises = [];
  1092. let found = false;
  1093. for (let i = 0, ii = kids.length; i < ii; i++) {
  1094. const kid = kids[i];
  1095. if (!(kid instanceof _primitives.Ref)) {
  1096. throw new _util.FormatError("Kid must be a reference.");
  1097. }
  1098. if ((0, _primitives.isRefsEqual)(kid, kidRef)) {
  1099. found = true;
  1100. break;
  1101. }
  1102. kidPromises.push(xref.fetchAsync(kid).then(function (obj) {
  1103. if (!(obj instanceof _primitives.Dict)) {
  1104. throw new _util.FormatError("Kid node must be a dictionary.");
  1105. }
  1106. if (obj.has("Count")) {
  1107. total += obj.get("Count");
  1108. } else {
  1109. total++;
  1110. }
  1111. }));
  1112. }
  1113. if (!found) {
  1114. throw new _util.FormatError("Kid reference not found in parent's kids.");
  1115. }
  1116. return Promise.all(kidPromises).then(function () {
  1117. return [total, parentRef];
  1118. });
  1119. });
  1120. }
  1121. let total = 0;
  1122. const next = ref => pagesBeforeRef(ref).then(args => {
  1123. if (!args) {
  1124. this.pageIndexCache.put(pageRef, total);
  1125. return total;
  1126. }
  1127. const [count, parentRef] = args;
  1128. total += count;
  1129. return next(parentRef);
  1130. });
  1131. return next(pageRef);
  1132. }
  1133. get baseUrl() {
  1134. const uri = this._catDict.get("URI");
  1135. if (uri instanceof _primitives.Dict) {
  1136. const base = uri.get("Base");
  1137. if (typeof base === "string") {
  1138. const absoluteUrl = (0, _util.createValidAbsoluteUrl)(base, null, {
  1139. tryConvertEncoding: true
  1140. });
  1141. if (absoluteUrl) {
  1142. return (0, _util.shadow)(this, "baseUrl", absoluteUrl.href);
  1143. }
  1144. }
  1145. }
  1146. return (0, _util.shadow)(this, "baseUrl", null);
  1147. }
  1148. static parseDestDictionary(params) {
  1149. const destDict = params.destDict;
  1150. if (!(destDict instanceof _primitives.Dict)) {
  1151. (0, _util.warn)("parseDestDictionary: `destDict` must be a dictionary.");
  1152. return;
  1153. }
  1154. const resultObj = params.resultObj;
  1155. if (typeof resultObj !== "object") {
  1156. (0, _util.warn)("parseDestDictionary: `resultObj` must be an object.");
  1157. return;
  1158. }
  1159. const docBaseUrl = params.docBaseUrl || null;
  1160. let action = destDict.get("A"),
  1161. url,
  1162. dest;
  1163. if (!(action instanceof _primitives.Dict)) {
  1164. if (destDict.has("Dest")) {
  1165. action = destDict.get("Dest");
  1166. } else {
  1167. action = destDict.get("AA");
  1168. if (action instanceof _primitives.Dict) {
  1169. if (action.has("D")) {
  1170. action = action.get("D");
  1171. } else if (action.has("U")) {
  1172. action = action.get("U");
  1173. }
  1174. }
  1175. }
  1176. }
  1177. if (action instanceof _primitives.Dict) {
  1178. const actionType = action.get("S");
  1179. if (!(actionType instanceof _primitives.Name)) {
  1180. (0, _util.warn)("parseDestDictionary: Invalid type in Action dictionary.");
  1181. return;
  1182. }
  1183. const actionName = actionType.name;
  1184. switch (actionName) {
  1185. case "ResetForm":
  1186. const flags = action.get("Flags");
  1187. const include = ((typeof flags === "number" ? flags : 0) & 1) === 0;
  1188. const fields = [];
  1189. const refs = [];
  1190. for (const obj of action.get("Fields") || []) {
  1191. if (obj instanceof _primitives.Ref) {
  1192. refs.push(obj.toString());
  1193. } else if (typeof obj === "string") {
  1194. fields.push((0, _util.stringToPDFString)(obj));
  1195. }
  1196. }
  1197. resultObj.resetForm = {
  1198. fields,
  1199. refs,
  1200. include
  1201. };
  1202. break;
  1203. case "URI":
  1204. url = action.get("URI");
  1205. if (url instanceof _primitives.Name) {
  1206. url = "/" + url.name;
  1207. }
  1208. break;
  1209. case "GoTo":
  1210. dest = action.get("D");
  1211. break;
  1212. case "Launch":
  1213. case "GoToR":
  1214. const urlDict = action.get("F");
  1215. if (urlDict instanceof _primitives.Dict) {
  1216. url = urlDict.get("F") || null;
  1217. } else if (typeof urlDict === "string") {
  1218. url = urlDict;
  1219. }
  1220. let remoteDest = action.get("D");
  1221. if (remoteDest) {
  1222. if (remoteDest instanceof _primitives.Name) {
  1223. remoteDest = remoteDest.name;
  1224. }
  1225. if (typeof url === "string") {
  1226. const baseUrl = url.split("#")[0];
  1227. if (typeof remoteDest === "string") {
  1228. url = baseUrl + "#" + remoteDest;
  1229. } else if (Array.isArray(remoteDest)) {
  1230. url = baseUrl + "#" + JSON.stringify(remoteDest);
  1231. }
  1232. }
  1233. }
  1234. const newWindow = action.get("NewWindow");
  1235. if (typeof newWindow === "boolean") {
  1236. resultObj.newWindow = newWindow;
  1237. }
  1238. break;
  1239. case "Named":
  1240. const namedAction = action.get("N");
  1241. if (namedAction instanceof _primitives.Name) {
  1242. resultObj.action = namedAction.name;
  1243. }
  1244. break;
  1245. case "JavaScript":
  1246. const jsAction = action.get("JS");
  1247. let js;
  1248. if (jsAction instanceof _base_stream.BaseStream) {
  1249. js = jsAction.getString();
  1250. } else if (typeof jsAction === "string") {
  1251. js = jsAction;
  1252. }
  1253. const jsURL = js && (0, _core_utils.recoverJsURL)((0, _util.stringToPDFString)(js));
  1254. if (jsURL) {
  1255. url = jsURL.url;
  1256. resultObj.newWindow = jsURL.newWindow;
  1257. break;
  1258. }
  1259. default:
  1260. if (actionName === "JavaScript" || actionName === "SubmitForm") {
  1261. break;
  1262. }
  1263. (0, _util.warn)(`parseDestDictionary - unsupported action: "${actionName}".`);
  1264. break;
  1265. }
  1266. } else if (destDict.has("Dest")) {
  1267. dest = destDict.get("Dest");
  1268. }
  1269. if (typeof url === "string") {
  1270. const absoluteUrl = (0, _util.createValidAbsoluteUrl)(url, docBaseUrl, {
  1271. addDefaultProtocol: true,
  1272. tryConvertEncoding: true
  1273. });
  1274. if (absoluteUrl) {
  1275. resultObj.url = absoluteUrl.href;
  1276. }
  1277. resultObj.unsafeUrl = url;
  1278. }
  1279. if (dest) {
  1280. if (dest instanceof _primitives.Name) {
  1281. dest = dest.name;
  1282. }
  1283. if (typeof dest === "string") {
  1284. resultObj.dest = (0, _util.stringToPDFString)(dest);
  1285. } else if (Array.isArray(dest)) {
  1286. resultObj.dest = dest;
  1287. }
  1288. }
  1289. }
  1290. }
  1291. exports.Catalog = Catalog;