util.js 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917
  1. /**
  2. * @licstart The following is the entire license notice for the
  3. * Javascript code in this page
  4. *
  5. * Copyright 2021 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.arrayByteLength = arrayByteLength;
  27. exports.arraysToBytes = arraysToBytes;
  28. exports.assert = assert;
  29. exports.bytesToString = bytesToString;
  30. exports.createObjectURL = createObjectURL;
  31. exports.createPromiseCapability = createPromiseCapability;
  32. exports.createValidAbsoluteUrl = createValidAbsoluteUrl;
  33. exports.escapeString = escapeString;
  34. exports.getModificationDate = getModificationDate;
  35. exports.getVerbosityLevel = getVerbosityLevel;
  36. exports.info = info;
  37. exports.isArrayBuffer = isArrayBuffer;
  38. exports.isArrayEqual = isArrayEqual;
  39. exports.isAscii = isAscii;
  40. exports.isBool = isBool;
  41. exports.isNum = isNum;
  42. exports.isSameOrigin = isSameOrigin;
  43. exports.isString = isString;
  44. exports.objectFromMap = objectFromMap;
  45. exports.objectSize = objectSize;
  46. exports.removeNullCharacters = removeNullCharacters;
  47. exports.setVerbosityLevel = setVerbosityLevel;
  48. exports.shadow = shadow;
  49. exports.string32 = string32;
  50. exports.stringToBytes = stringToBytes;
  51. exports.stringToPDFString = stringToPDFString;
  52. exports.stringToUTF16BEString = stringToUTF16BEString;
  53. exports.stringToUTF8String = stringToUTF8String;
  54. exports.unreachable = unreachable;
  55. exports.utf8StringToString = utf8StringToString;
  56. exports.warn = warn;
  57. exports.VerbosityLevel = exports.Util = exports.UNSUPPORTED_FEATURES = exports.UnknownErrorException = exports.UnexpectedResponseException = exports.TextRenderingMode = exports.StreamType = exports.PermissionFlag = exports.PasswordResponses = exports.PasswordException = exports.PageActionEventType = exports.OPS = exports.MissingPDFException = exports.IsLittleEndianCached = exports.IsEvalSupportedCached = exports.InvalidPDFException = exports.ImageKind = exports.IDENTITY_MATRIX = exports.FormatError = exports.FontType = exports.FONT_IDENTITY_MATRIX = exports.DocumentActionEventType = exports.CMapCompressionType = exports.BaseException = exports.AnnotationType = exports.AnnotationStateModelType = exports.AnnotationReviewState = exports.AnnotationReplyType = exports.AnnotationMarkedState = exports.AnnotationFlag = exports.AnnotationFieldFlag = exports.AnnotationBorderStyleType = exports.AnnotationActionEventType = exports.AbortException = void 0;
  58. require("./compatibility.js");
  59. const IDENTITY_MATRIX = [1, 0, 0, 1, 0, 0];
  60. exports.IDENTITY_MATRIX = IDENTITY_MATRIX;
  61. const FONT_IDENTITY_MATRIX = [0.001, 0, 0, 0.001, 0, 0];
  62. exports.FONT_IDENTITY_MATRIX = FONT_IDENTITY_MATRIX;
  63. const PermissionFlag = {
  64. PRINT: 0x04,
  65. MODIFY_CONTENTS: 0x08,
  66. COPY: 0x10,
  67. MODIFY_ANNOTATIONS: 0x20,
  68. FILL_INTERACTIVE_FORMS: 0x100,
  69. COPY_FOR_ACCESSIBILITY: 0x200,
  70. ASSEMBLE: 0x400,
  71. PRINT_HIGH_QUALITY: 0x800
  72. };
  73. exports.PermissionFlag = PermissionFlag;
  74. const TextRenderingMode = {
  75. FILL: 0,
  76. STROKE: 1,
  77. FILL_STROKE: 2,
  78. INVISIBLE: 3,
  79. FILL_ADD_TO_PATH: 4,
  80. STROKE_ADD_TO_PATH: 5,
  81. FILL_STROKE_ADD_TO_PATH: 6,
  82. ADD_TO_PATH: 7,
  83. FILL_STROKE_MASK: 3,
  84. ADD_TO_PATH_FLAG: 4
  85. };
  86. exports.TextRenderingMode = TextRenderingMode;
  87. const ImageKind = {
  88. GRAYSCALE_1BPP: 1,
  89. RGB_24BPP: 2,
  90. RGBA_32BPP: 3
  91. };
  92. exports.ImageKind = ImageKind;
  93. const AnnotationType = {
  94. TEXT: 1,
  95. LINK: 2,
  96. FREETEXT: 3,
  97. LINE: 4,
  98. SQUARE: 5,
  99. CIRCLE: 6,
  100. POLYGON: 7,
  101. POLYLINE: 8,
  102. HIGHLIGHT: 9,
  103. UNDERLINE: 10,
  104. SQUIGGLY: 11,
  105. STRIKEOUT: 12,
  106. STAMP: 13,
  107. CARET: 14,
  108. INK: 15,
  109. POPUP: 16,
  110. FILEATTACHMENT: 17,
  111. SOUND: 18,
  112. MOVIE: 19,
  113. WIDGET: 20,
  114. SCREEN: 21,
  115. PRINTERMARK: 22,
  116. TRAPNET: 23,
  117. WATERMARK: 24,
  118. THREED: 25,
  119. REDACT: 26
  120. };
  121. exports.AnnotationType = AnnotationType;
  122. const AnnotationStateModelType = {
  123. MARKED: "Marked",
  124. REVIEW: "Review"
  125. };
  126. exports.AnnotationStateModelType = AnnotationStateModelType;
  127. const AnnotationMarkedState = {
  128. MARKED: "Marked",
  129. UNMARKED: "Unmarked"
  130. };
  131. exports.AnnotationMarkedState = AnnotationMarkedState;
  132. const AnnotationReviewState = {
  133. ACCEPTED: "Accepted",
  134. REJECTED: "Rejected",
  135. CANCELLED: "Cancelled",
  136. COMPLETED: "Completed",
  137. NONE: "None"
  138. };
  139. exports.AnnotationReviewState = AnnotationReviewState;
  140. const AnnotationReplyType = {
  141. GROUP: "Group",
  142. REPLY: "R"
  143. };
  144. exports.AnnotationReplyType = AnnotationReplyType;
  145. const AnnotationFlag = {
  146. INVISIBLE: 0x01,
  147. HIDDEN: 0x02,
  148. PRINT: 0x04,
  149. NOZOOM: 0x08,
  150. NOROTATE: 0x10,
  151. NOVIEW: 0x20,
  152. READONLY: 0x40,
  153. LOCKED: 0x80,
  154. TOGGLENOVIEW: 0x100,
  155. LOCKEDCONTENTS: 0x200
  156. };
  157. exports.AnnotationFlag = AnnotationFlag;
  158. const AnnotationFieldFlag = {
  159. READONLY: 0x0000001,
  160. REQUIRED: 0x0000002,
  161. NOEXPORT: 0x0000004,
  162. MULTILINE: 0x0001000,
  163. PASSWORD: 0x0002000,
  164. NOTOGGLETOOFF: 0x0004000,
  165. RADIO: 0x0008000,
  166. PUSHBUTTON: 0x0010000,
  167. COMBO: 0x0020000,
  168. EDIT: 0x0040000,
  169. SORT: 0x0080000,
  170. FILESELECT: 0x0100000,
  171. MULTISELECT: 0x0200000,
  172. DONOTSPELLCHECK: 0x0400000,
  173. DONOTSCROLL: 0x0800000,
  174. COMB: 0x1000000,
  175. RICHTEXT: 0x2000000,
  176. RADIOSINUNISON: 0x2000000,
  177. COMMITONSELCHANGE: 0x4000000
  178. };
  179. exports.AnnotationFieldFlag = AnnotationFieldFlag;
  180. const AnnotationBorderStyleType = {
  181. SOLID: 1,
  182. DASHED: 2,
  183. BEVELED: 3,
  184. INSET: 4,
  185. UNDERLINE: 5
  186. };
  187. exports.AnnotationBorderStyleType = AnnotationBorderStyleType;
  188. const AnnotationActionEventType = {
  189. E: "Mouse Enter",
  190. X: "Mouse Exit",
  191. D: "Mouse Down",
  192. U: "Mouse Up",
  193. Fo: "Focus",
  194. Bl: "Blur",
  195. PO: "PageOpen",
  196. PC: "PageClose",
  197. PV: "PageVisible",
  198. PI: "PageInvisible",
  199. K: "Keystroke",
  200. F: "Format",
  201. V: "Validate",
  202. C: "Calculate"
  203. };
  204. exports.AnnotationActionEventType = AnnotationActionEventType;
  205. const DocumentActionEventType = {
  206. WC: "WillClose",
  207. WS: "WillSave",
  208. DS: "DidSave",
  209. WP: "WillPrint",
  210. DP: "DidPrint"
  211. };
  212. exports.DocumentActionEventType = DocumentActionEventType;
  213. const PageActionEventType = {
  214. O: "PageOpen",
  215. C: "PageClose"
  216. };
  217. exports.PageActionEventType = PageActionEventType;
  218. const StreamType = {
  219. UNKNOWN: "UNKNOWN",
  220. FLATE: "FLATE",
  221. LZW: "LZW",
  222. DCT: "DCT",
  223. JPX: "JPX",
  224. JBIG: "JBIG",
  225. A85: "A85",
  226. AHX: "AHX",
  227. CCF: "CCF",
  228. RLX: "RLX"
  229. };
  230. exports.StreamType = StreamType;
  231. const FontType = {
  232. UNKNOWN: "UNKNOWN",
  233. TYPE1: "TYPE1",
  234. TYPE1STANDARD: "TYPE1STANDARD",
  235. TYPE1C: "TYPE1C",
  236. CIDFONTTYPE0: "CIDFONTTYPE0",
  237. CIDFONTTYPE0C: "CIDFONTTYPE0C",
  238. TRUETYPE: "TRUETYPE",
  239. CIDFONTTYPE2: "CIDFONTTYPE2",
  240. TYPE3: "TYPE3",
  241. OPENTYPE: "OPENTYPE",
  242. TYPE0: "TYPE0",
  243. MMTYPE1: "MMTYPE1"
  244. };
  245. exports.FontType = FontType;
  246. const VerbosityLevel = {
  247. ERRORS: 0,
  248. WARNINGS: 1,
  249. INFOS: 5
  250. };
  251. exports.VerbosityLevel = VerbosityLevel;
  252. const CMapCompressionType = {
  253. NONE: 0,
  254. BINARY: 1,
  255. STREAM: 2
  256. };
  257. exports.CMapCompressionType = CMapCompressionType;
  258. const OPS = {
  259. dependency: 1,
  260. setLineWidth: 2,
  261. setLineCap: 3,
  262. setLineJoin: 4,
  263. setMiterLimit: 5,
  264. setDash: 6,
  265. setRenderingIntent: 7,
  266. setFlatness: 8,
  267. setGState: 9,
  268. save: 10,
  269. restore: 11,
  270. transform: 12,
  271. moveTo: 13,
  272. lineTo: 14,
  273. curveTo: 15,
  274. curveTo2: 16,
  275. curveTo3: 17,
  276. closePath: 18,
  277. rectangle: 19,
  278. stroke: 20,
  279. closeStroke: 21,
  280. fill: 22,
  281. eoFill: 23,
  282. fillStroke: 24,
  283. eoFillStroke: 25,
  284. closeFillStroke: 26,
  285. closeEOFillStroke: 27,
  286. endPath: 28,
  287. clip: 29,
  288. eoClip: 30,
  289. beginText: 31,
  290. endText: 32,
  291. setCharSpacing: 33,
  292. setWordSpacing: 34,
  293. setHScale: 35,
  294. setLeading: 36,
  295. setFont: 37,
  296. setTextRenderingMode: 38,
  297. setTextRise: 39,
  298. moveText: 40,
  299. setLeadingMoveText: 41,
  300. setTextMatrix: 42,
  301. nextLine: 43,
  302. showText: 44,
  303. showSpacedText: 45,
  304. nextLineShowText: 46,
  305. nextLineSetSpacingShowText: 47,
  306. setCharWidth: 48,
  307. setCharWidthAndBounds: 49,
  308. setStrokeColorSpace: 50,
  309. setFillColorSpace: 51,
  310. setStrokeColor: 52,
  311. setStrokeColorN: 53,
  312. setFillColor: 54,
  313. setFillColorN: 55,
  314. setStrokeGray: 56,
  315. setFillGray: 57,
  316. setStrokeRGBColor: 58,
  317. setFillRGBColor: 59,
  318. setStrokeCMYKColor: 60,
  319. setFillCMYKColor: 61,
  320. shadingFill: 62,
  321. beginInlineImage: 63,
  322. beginImageData: 64,
  323. endInlineImage: 65,
  324. paintXObject: 66,
  325. markPoint: 67,
  326. markPointProps: 68,
  327. beginMarkedContent: 69,
  328. beginMarkedContentProps: 70,
  329. endMarkedContent: 71,
  330. beginCompat: 72,
  331. endCompat: 73,
  332. paintFormXObjectBegin: 74,
  333. paintFormXObjectEnd: 75,
  334. beginGroup: 76,
  335. endGroup: 77,
  336. beginAnnotations: 78,
  337. endAnnotations: 79,
  338. beginAnnotation: 80,
  339. endAnnotation: 81,
  340. paintJpegXObject: 82,
  341. paintImageMaskXObject: 83,
  342. paintImageMaskXObjectGroup: 84,
  343. paintImageXObject: 85,
  344. paintInlineImageXObject: 86,
  345. paintInlineImageXObjectGroup: 87,
  346. paintImageXObjectRepeat: 88,
  347. paintImageMaskXObjectRepeat: 89,
  348. paintSolidColorImageMask: 90,
  349. constructPath: 91
  350. };
  351. exports.OPS = OPS;
  352. const UNSUPPORTED_FEATURES = {
  353. unknown: "unknown",
  354. forms: "forms",
  355. javaScript: "javaScript",
  356. signatures: "signatures",
  357. smask: "smask",
  358. shadingPattern: "shadingPattern",
  359. font: "font",
  360. errorTilingPattern: "errorTilingPattern",
  361. errorExtGState: "errorExtGState",
  362. errorXObject: "errorXObject",
  363. errorFontLoadType3: "errorFontLoadType3",
  364. errorFontState: "errorFontState",
  365. errorFontMissing: "errorFontMissing",
  366. errorFontTranslate: "errorFontTranslate",
  367. errorColorSpace: "errorColorSpace",
  368. errorOperatorList: "errorOperatorList",
  369. errorFontToUnicode: "errorFontToUnicode",
  370. errorFontLoadNative: "errorFontLoadNative",
  371. errorFontBuildPath: "errorFontBuildPath",
  372. errorFontGetPath: "errorFontGetPath",
  373. errorMarkedContent: "errorMarkedContent"
  374. };
  375. exports.UNSUPPORTED_FEATURES = UNSUPPORTED_FEATURES;
  376. const PasswordResponses = {
  377. NEED_PASSWORD: 1,
  378. INCORRECT_PASSWORD: 2
  379. };
  380. exports.PasswordResponses = PasswordResponses;
  381. let verbosity = VerbosityLevel.WARNINGS;
  382. function setVerbosityLevel(level) {
  383. if (Number.isInteger(level)) {
  384. verbosity = level;
  385. }
  386. }
  387. function getVerbosityLevel() {
  388. return verbosity;
  389. }
  390. function info(msg) {
  391. if (verbosity >= VerbosityLevel.INFOS) {
  392. console.log(`Info: ${msg}`);
  393. }
  394. }
  395. function warn(msg) {
  396. if (verbosity >= VerbosityLevel.WARNINGS) {
  397. console.log(`Warning: ${msg}`);
  398. }
  399. }
  400. function unreachable(msg) {
  401. throw new Error(msg);
  402. }
  403. function assert(cond, msg) {
  404. if (!cond) {
  405. unreachable(msg);
  406. }
  407. }
  408. function isSameOrigin(baseUrl, otherUrl) {
  409. let base;
  410. try {
  411. base = new URL(baseUrl);
  412. if (!base.origin || base.origin === "null") {
  413. return false;
  414. }
  415. } catch (e) {
  416. return false;
  417. }
  418. const other = new URL(otherUrl, base);
  419. return base.origin === other.origin;
  420. }
  421. function _isValidProtocol(url) {
  422. if (!url) {
  423. return false;
  424. }
  425. switch (url.protocol) {
  426. case "http:":
  427. case "https:":
  428. case "ftp:":
  429. case "mailto:":
  430. case "tel:":
  431. return true;
  432. default:
  433. return false;
  434. }
  435. }
  436. function createValidAbsoluteUrl(url, baseUrl) {
  437. if (!url) {
  438. return null;
  439. }
  440. try {
  441. const absoluteUrl = baseUrl ? new URL(url, baseUrl) : new URL(url);
  442. if (_isValidProtocol(absoluteUrl)) {
  443. return absoluteUrl;
  444. }
  445. } catch (ex) {}
  446. return null;
  447. }
  448. function shadow(obj, prop, value) {
  449. Object.defineProperty(obj, prop, {
  450. value,
  451. enumerable: true,
  452. configurable: true,
  453. writable: false
  454. });
  455. return value;
  456. }
  457. const BaseException = function BaseExceptionClosure() {
  458. function BaseException(message) {
  459. if (this.constructor === BaseException) {
  460. unreachable("Cannot initialize BaseException.");
  461. }
  462. this.message = message;
  463. this.name = this.constructor.name;
  464. }
  465. BaseException.prototype = new Error();
  466. BaseException.constructor = BaseException;
  467. return BaseException;
  468. }();
  469. exports.BaseException = BaseException;
  470. class PasswordException extends BaseException {
  471. constructor(msg, code) {
  472. super(msg);
  473. this.code = code;
  474. }
  475. }
  476. exports.PasswordException = PasswordException;
  477. class UnknownErrorException extends BaseException {
  478. constructor(msg, details) {
  479. super(msg);
  480. this.details = details;
  481. }
  482. }
  483. exports.UnknownErrorException = UnknownErrorException;
  484. class InvalidPDFException extends BaseException {}
  485. exports.InvalidPDFException = InvalidPDFException;
  486. class MissingPDFException extends BaseException {}
  487. exports.MissingPDFException = MissingPDFException;
  488. class UnexpectedResponseException extends BaseException {
  489. constructor(msg, status) {
  490. super(msg);
  491. this.status = status;
  492. }
  493. }
  494. exports.UnexpectedResponseException = UnexpectedResponseException;
  495. class FormatError extends BaseException {}
  496. exports.FormatError = FormatError;
  497. class AbortException extends BaseException {}
  498. exports.AbortException = AbortException;
  499. const NullCharactersRegExp = /\x00/g;
  500. function removeNullCharacters(str) {
  501. if (typeof str !== "string") {
  502. warn("The argument for removeNullCharacters must be a string.");
  503. return str;
  504. }
  505. return str.replace(NullCharactersRegExp, "");
  506. }
  507. function bytesToString(bytes) {
  508. assert(bytes !== null && typeof bytes === "object" && bytes.length !== undefined, "Invalid argument for bytesToString");
  509. const length = bytes.length;
  510. const MAX_ARGUMENT_COUNT = 8192;
  511. if (length < MAX_ARGUMENT_COUNT) {
  512. return String.fromCharCode.apply(null, bytes);
  513. }
  514. const strBuf = [];
  515. for (let i = 0; i < length; i += MAX_ARGUMENT_COUNT) {
  516. const chunkEnd = Math.min(i + MAX_ARGUMENT_COUNT, length);
  517. const chunk = bytes.subarray(i, chunkEnd);
  518. strBuf.push(String.fromCharCode.apply(null, chunk));
  519. }
  520. return strBuf.join("");
  521. }
  522. function stringToBytes(str) {
  523. assert(typeof str === "string", "Invalid argument for stringToBytes");
  524. const length = str.length;
  525. const bytes = new Uint8Array(length);
  526. for (let i = 0; i < length; ++i) {
  527. bytes[i] = str.charCodeAt(i) & 0xff;
  528. }
  529. return bytes;
  530. }
  531. function arrayByteLength(arr) {
  532. if (arr.length !== undefined) {
  533. return arr.length;
  534. }
  535. assert(arr.byteLength !== undefined, "arrayByteLength - invalid argument.");
  536. return arr.byteLength;
  537. }
  538. function arraysToBytes(arr) {
  539. const length = arr.length;
  540. if (length === 1 && arr[0] instanceof Uint8Array) {
  541. return arr[0];
  542. }
  543. let resultLength = 0;
  544. for (let i = 0; i < length; i++) {
  545. resultLength += arrayByteLength(arr[i]);
  546. }
  547. let pos = 0;
  548. const data = new Uint8Array(resultLength);
  549. for (let i = 0; i < length; i++) {
  550. let item = arr[i];
  551. if (!(item instanceof Uint8Array)) {
  552. if (typeof item === "string") {
  553. item = stringToBytes(item);
  554. } else {
  555. item = new Uint8Array(item);
  556. }
  557. }
  558. const itemLength = item.byteLength;
  559. data.set(item, pos);
  560. pos += itemLength;
  561. }
  562. return data;
  563. }
  564. function string32(value) {
  565. return String.fromCharCode(value >> 24 & 0xff, value >> 16 & 0xff, value >> 8 & 0xff, value & 0xff);
  566. }
  567. function objectSize(obj) {
  568. return Object.keys(obj).length;
  569. }
  570. function objectFromMap(map) {
  571. const obj = Object.create(null);
  572. for (const [key, value] of map) {
  573. obj[key] = value;
  574. }
  575. return obj;
  576. }
  577. function isLittleEndian() {
  578. const buffer8 = new Uint8Array(4);
  579. buffer8[0] = 1;
  580. const view32 = new Uint32Array(buffer8.buffer, 0, 1);
  581. return view32[0] === 1;
  582. }
  583. const IsLittleEndianCached = {
  584. get value() {
  585. return shadow(this, "value", isLittleEndian());
  586. }
  587. };
  588. exports.IsLittleEndianCached = IsLittleEndianCached;
  589. function isEvalSupported() {
  590. try {
  591. new Function("");
  592. return true;
  593. } catch (e) {
  594. return false;
  595. }
  596. }
  597. const IsEvalSupportedCached = {
  598. get value() {
  599. return shadow(this, "value", isEvalSupported());
  600. }
  601. };
  602. exports.IsEvalSupportedCached = IsEvalSupportedCached;
  603. const hexNumbers = [...Array(256).keys()].map(n => n.toString(16).padStart(2, "0"));
  604. class Util {
  605. static makeHexColor(r, g, b) {
  606. return `#${hexNumbers[r]}${hexNumbers[g]}${hexNumbers[b]}`;
  607. }
  608. static transform(m1, m2) {
  609. return [m1[0] * m2[0] + m1[2] * m2[1], m1[1] * m2[0] + m1[3] * m2[1], m1[0] * m2[2] + m1[2] * m2[3], m1[1] * m2[2] + m1[3] * m2[3], m1[0] * m2[4] + m1[2] * m2[5] + m1[4], m1[1] * m2[4] + m1[3] * m2[5] + m1[5]];
  610. }
  611. static applyTransform(p, m) {
  612. const xt = p[0] * m[0] + p[1] * m[2] + m[4];
  613. const yt = p[0] * m[1] + p[1] * m[3] + m[5];
  614. return [xt, yt];
  615. }
  616. static applyInverseTransform(p, m) {
  617. const d = m[0] * m[3] - m[1] * m[2];
  618. const xt = (p[0] * m[3] - p[1] * m[2] + m[2] * m[5] - m[4] * m[3]) / d;
  619. const yt = (-p[0] * m[1] + p[1] * m[0] + m[4] * m[1] - m[5] * m[0]) / d;
  620. return [xt, yt];
  621. }
  622. static getAxialAlignedBoundingBox(r, m) {
  623. const p1 = Util.applyTransform(r, m);
  624. const p2 = Util.applyTransform(r.slice(2, 4), m);
  625. const p3 = Util.applyTransform([r[0], r[3]], m);
  626. const p4 = Util.applyTransform([r[2], r[1]], m);
  627. return [Math.min(p1[0], p2[0], p3[0], p4[0]), Math.min(p1[1], p2[1], p3[1], p4[1]), Math.max(p1[0], p2[0], p3[0], p4[0]), Math.max(p1[1], p2[1], p3[1], p4[1])];
  628. }
  629. static inverseTransform(m) {
  630. const d = m[0] * m[3] - m[1] * m[2];
  631. return [m[3] / d, -m[1] / d, -m[2] / d, m[0] / d, (m[2] * m[5] - m[4] * m[3]) / d, (m[4] * m[1] - m[5] * m[0]) / d];
  632. }
  633. static apply3dTransform(m, v) {
  634. return [m[0] * v[0] + m[1] * v[1] + m[2] * v[2], m[3] * v[0] + m[4] * v[1] + m[5] * v[2], m[6] * v[0] + m[7] * v[1] + m[8] * v[2]];
  635. }
  636. static singularValueDecompose2dScale(m) {
  637. const transpose = [m[0], m[2], m[1], m[3]];
  638. const a = m[0] * transpose[0] + m[1] * transpose[2];
  639. const b = m[0] * transpose[1] + m[1] * transpose[3];
  640. const c = m[2] * transpose[0] + m[3] * transpose[2];
  641. const d = m[2] * transpose[1] + m[3] * transpose[3];
  642. const first = (a + d) / 2;
  643. const second = Math.sqrt((a + d) ** 2 - 4 * (a * d - c * b)) / 2;
  644. const sx = first + second || 1;
  645. const sy = first - second || 1;
  646. return [Math.sqrt(sx), Math.sqrt(sy)];
  647. }
  648. static normalizeRect(rect) {
  649. const r = rect.slice(0);
  650. if (rect[0] > rect[2]) {
  651. r[0] = rect[2];
  652. r[2] = rect[0];
  653. }
  654. if (rect[1] > rect[3]) {
  655. r[1] = rect[3];
  656. r[3] = rect[1];
  657. }
  658. return r;
  659. }
  660. static intersect(rect1, rect2) {
  661. function compare(a, b) {
  662. return a - b;
  663. }
  664. const orderedX = [rect1[0], rect1[2], rect2[0], rect2[2]].sort(compare);
  665. const orderedY = [rect1[1], rect1[3], rect2[1], rect2[3]].sort(compare);
  666. const result = [];
  667. rect1 = Util.normalizeRect(rect1);
  668. rect2 = Util.normalizeRect(rect2);
  669. if (orderedX[0] === rect1[0] && orderedX[1] === rect2[0] || orderedX[0] === rect2[0] && orderedX[1] === rect1[0]) {
  670. result[0] = orderedX[1];
  671. result[2] = orderedX[2];
  672. } else {
  673. return null;
  674. }
  675. if (orderedY[0] === rect1[1] && orderedY[1] === rect2[1] || orderedY[0] === rect2[1] && orderedY[1] === rect1[1]) {
  676. result[1] = orderedY[1];
  677. result[3] = orderedY[2];
  678. } else {
  679. return null;
  680. }
  681. return result;
  682. }
  683. }
  684. exports.Util = Util;
  685. const PDFStringTranslateTable = [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0x2d8, 0x2c7, 0x2c6, 0x2d9, 0x2dd, 0x2db, 0x2da, 0x2dc, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0x2022, 0x2020, 0x2021, 0x2026, 0x2014, 0x2013, 0x192, 0x2044, 0x2039, 0x203a, 0x2212, 0x2030, 0x201e, 0x201c, 0x201d, 0x2018, 0x2019, 0x201a, 0x2122, 0xfb01, 0xfb02, 0x141, 0x152, 0x160, 0x178, 0x17d, 0x131, 0x142, 0x153, 0x161, 0x17e, 0, 0x20ac];
  686. function stringToPDFString(str) {
  687. const length = str.length,
  688. strBuf = [];
  689. if (str[0] === "\xFE" && str[1] === "\xFF") {
  690. for (let i = 2; i < length; i += 2) {
  691. strBuf.push(String.fromCharCode(str.charCodeAt(i) << 8 | str.charCodeAt(i + 1)));
  692. }
  693. } else if (str[0] === "\xFF" && str[1] === "\xFE") {
  694. for (let i = 2; i < length; i += 2) {
  695. strBuf.push(String.fromCharCode(str.charCodeAt(i + 1) << 8 | str.charCodeAt(i)));
  696. }
  697. } else {
  698. for (let i = 0; i < length; ++i) {
  699. const code = PDFStringTranslateTable[str.charCodeAt(i)];
  700. strBuf.push(code ? String.fromCharCode(code) : str.charAt(i));
  701. }
  702. }
  703. return strBuf.join("");
  704. }
  705. function escapeString(str) {
  706. return str.replace(/([()\\\n\r])/g, match => {
  707. if (match === "\n") {
  708. return "\\n";
  709. } else if (match === "\r") {
  710. return "\\r";
  711. }
  712. return `\\${match}`;
  713. });
  714. }
  715. function isAscii(str) {
  716. return /^[\x00-\x7F]*$/.test(str);
  717. }
  718. function stringToUTF16BEString(str) {
  719. const buf = ["\xFE\xFF"];
  720. for (let i = 0, ii = str.length; i < ii; i++) {
  721. const char = str.charCodeAt(i);
  722. buf.push(String.fromCharCode(char >> 8 & 0xff), String.fromCharCode(char & 0xff));
  723. }
  724. return buf.join("");
  725. }
  726. function stringToUTF8String(str) {
  727. return decodeURIComponent(escape(str));
  728. }
  729. function utf8StringToString(str) {
  730. return unescape(encodeURIComponent(str));
  731. }
  732. function isBool(v) {
  733. return typeof v === "boolean";
  734. }
  735. function isNum(v) {
  736. return typeof v === "number";
  737. }
  738. function isString(v) {
  739. return typeof v === "string";
  740. }
  741. function isArrayBuffer(v) {
  742. return typeof v === "object" && v !== null && v.byteLength !== undefined;
  743. }
  744. function isArrayEqual(arr1, arr2) {
  745. if (arr1.length !== arr2.length) {
  746. return false;
  747. }
  748. for (let i = 0, ii = arr1.length; i < ii; i++) {
  749. if (arr1[i] !== arr2[i]) {
  750. return false;
  751. }
  752. }
  753. return true;
  754. }
  755. function getModificationDate(date = new Date()) {
  756. const buffer = [date.getUTCFullYear().toString(), (date.getUTCMonth() + 1).toString().padStart(2, "0"), date.getUTCDate().toString().padStart(2, "0"), date.getUTCHours().toString().padStart(2, "0"), date.getUTCMinutes().toString().padStart(2, "0"), date.getUTCSeconds().toString().padStart(2, "0")];
  757. return buffer.join("");
  758. }
  759. function createPromiseCapability() {
  760. const capability = Object.create(null);
  761. let isSettled = false;
  762. Object.defineProperty(capability, "settled", {
  763. get() {
  764. return isSettled;
  765. }
  766. });
  767. capability.promise = new Promise(function (resolve, reject) {
  768. capability.resolve = function (data) {
  769. isSettled = true;
  770. resolve(data);
  771. };
  772. capability.reject = function (reason) {
  773. isSettled = true;
  774. reject(reason);
  775. };
  776. });
  777. return capability;
  778. }
  779. function createObjectURL(data, contentType = "", forceDataSchema = false) {
  780. if (URL.createObjectURL && !forceDataSchema) {
  781. return URL.createObjectURL(new Blob([data], {
  782. type: contentType
  783. }));
  784. }
  785. const digits = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/=";
  786. let buffer = `data:${contentType};base64,`;
  787. for (let i = 0, ii = data.length; i < ii; i += 3) {
  788. const b1 = data[i] & 0xff;
  789. const b2 = data[i + 1] & 0xff;
  790. const b3 = data[i + 2] & 0xff;
  791. const d1 = b1 >> 2,
  792. d2 = (b1 & 3) << 4 | b2 >> 4;
  793. const d3 = i + 1 < ii ? (b2 & 0xf) << 2 | b3 >> 6 : 64;
  794. const d4 = i + 2 < ii ? b3 & 0x3f : 64;
  795. buffer += digits[d1] + digits[d2] + digits[d3] + digits[d4];
  796. }
  797. return buffer;
  798. }