catalog.js 41 KB

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