2
0

util.js 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916
  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. TYPE1C: "TYPE1C",
  235. CIDFONTTYPE0: "CIDFONTTYPE0",
  236. CIDFONTTYPE0C: "CIDFONTTYPE0C",
  237. TRUETYPE: "TRUETYPE",
  238. CIDFONTTYPE2: "CIDFONTTYPE2",
  239. TYPE3: "TYPE3",
  240. OPENTYPE: "OPENTYPE",
  241. TYPE0: "TYPE0",
  242. MMTYPE1: "MMTYPE1"
  243. };
  244. exports.FontType = FontType;
  245. const VerbosityLevel = {
  246. ERRORS: 0,
  247. WARNINGS: 1,
  248. INFOS: 5
  249. };
  250. exports.VerbosityLevel = VerbosityLevel;
  251. const CMapCompressionType = {
  252. NONE: 0,
  253. BINARY: 1,
  254. STREAM: 2
  255. };
  256. exports.CMapCompressionType = CMapCompressionType;
  257. const OPS = {
  258. dependency: 1,
  259. setLineWidth: 2,
  260. setLineCap: 3,
  261. setLineJoin: 4,
  262. setMiterLimit: 5,
  263. setDash: 6,
  264. setRenderingIntent: 7,
  265. setFlatness: 8,
  266. setGState: 9,
  267. save: 10,
  268. restore: 11,
  269. transform: 12,
  270. moveTo: 13,
  271. lineTo: 14,
  272. curveTo: 15,
  273. curveTo2: 16,
  274. curveTo3: 17,
  275. closePath: 18,
  276. rectangle: 19,
  277. stroke: 20,
  278. closeStroke: 21,
  279. fill: 22,
  280. eoFill: 23,
  281. fillStroke: 24,
  282. eoFillStroke: 25,
  283. closeFillStroke: 26,
  284. closeEOFillStroke: 27,
  285. endPath: 28,
  286. clip: 29,
  287. eoClip: 30,
  288. beginText: 31,
  289. endText: 32,
  290. setCharSpacing: 33,
  291. setWordSpacing: 34,
  292. setHScale: 35,
  293. setLeading: 36,
  294. setFont: 37,
  295. setTextRenderingMode: 38,
  296. setTextRise: 39,
  297. moveText: 40,
  298. setLeadingMoveText: 41,
  299. setTextMatrix: 42,
  300. nextLine: 43,
  301. showText: 44,
  302. showSpacedText: 45,
  303. nextLineShowText: 46,
  304. nextLineSetSpacingShowText: 47,
  305. setCharWidth: 48,
  306. setCharWidthAndBounds: 49,
  307. setStrokeColorSpace: 50,
  308. setFillColorSpace: 51,
  309. setStrokeColor: 52,
  310. setStrokeColorN: 53,
  311. setFillColor: 54,
  312. setFillColorN: 55,
  313. setStrokeGray: 56,
  314. setFillGray: 57,
  315. setStrokeRGBColor: 58,
  316. setFillRGBColor: 59,
  317. setStrokeCMYKColor: 60,
  318. setFillCMYKColor: 61,
  319. shadingFill: 62,
  320. beginInlineImage: 63,
  321. beginImageData: 64,
  322. endInlineImage: 65,
  323. paintXObject: 66,
  324. markPoint: 67,
  325. markPointProps: 68,
  326. beginMarkedContent: 69,
  327. beginMarkedContentProps: 70,
  328. endMarkedContent: 71,
  329. beginCompat: 72,
  330. endCompat: 73,
  331. paintFormXObjectBegin: 74,
  332. paintFormXObjectEnd: 75,
  333. beginGroup: 76,
  334. endGroup: 77,
  335. beginAnnotations: 78,
  336. endAnnotations: 79,
  337. beginAnnotation: 80,
  338. endAnnotation: 81,
  339. paintJpegXObject: 82,
  340. paintImageMaskXObject: 83,
  341. paintImageMaskXObjectGroup: 84,
  342. paintImageXObject: 85,
  343. paintInlineImageXObject: 86,
  344. paintInlineImageXObjectGroup: 87,
  345. paintImageXObjectRepeat: 88,
  346. paintImageMaskXObjectRepeat: 89,
  347. paintSolidColorImageMask: 90,
  348. constructPath: 91
  349. };
  350. exports.OPS = OPS;
  351. const UNSUPPORTED_FEATURES = {
  352. unknown: "unknown",
  353. forms: "forms",
  354. javaScript: "javaScript",
  355. signatures: "signatures",
  356. smask: "smask",
  357. shadingPattern: "shadingPattern",
  358. font: "font",
  359. errorTilingPattern: "errorTilingPattern",
  360. errorExtGState: "errorExtGState",
  361. errorXObject: "errorXObject",
  362. errorFontLoadType3: "errorFontLoadType3",
  363. errorFontState: "errorFontState",
  364. errorFontMissing: "errorFontMissing",
  365. errorFontTranslate: "errorFontTranslate",
  366. errorColorSpace: "errorColorSpace",
  367. errorOperatorList: "errorOperatorList",
  368. errorFontToUnicode: "errorFontToUnicode",
  369. errorFontLoadNative: "errorFontLoadNative",
  370. errorFontBuildPath: "errorFontBuildPath",
  371. errorFontGetPath: "errorFontGetPath",
  372. errorMarkedContent: "errorMarkedContent"
  373. };
  374. exports.UNSUPPORTED_FEATURES = UNSUPPORTED_FEATURES;
  375. const PasswordResponses = {
  376. NEED_PASSWORD: 1,
  377. INCORRECT_PASSWORD: 2
  378. };
  379. exports.PasswordResponses = PasswordResponses;
  380. let verbosity = VerbosityLevel.WARNINGS;
  381. function setVerbosityLevel(level) {
  382. if (Number.isInteger(level)) {
  383. verbosity = level;
  384. }
  385. }
  386. function getVerbosityLevel() {
  387. return verbosity;
  388. }
  389. function info(msg) {
  390. if (verbosity >= VerbosityLevel.INFOS) {
  391. console.log(`Info: ${msg}`);
  392. }
  393. }
  394. function warn(msg) {
  395. if (verbosity >= VerbosityLevel.WARNINGS) {
  396. console.log(`Warning: ${msg}`);
  397. }
  398. }
  399. function unreachable(msg) {
  400. throw new Error(msg);
  401. }
  402. function assert(cond, msg) {
  403. if (!cond) {
  404. unreachable(msg);
  405. }
  406. }
  407. function isSameOrigin(baseUrl, otherUrl) {
  408. let base;
  409. try {
  410. base = new URL(baseUrl);
  411. if (!base.origin || base.origin === "null") {
  412. return false;
  413. }
  414. } catch (e) {
  415. return false;
  416. }
  417. const other = new URL(otherUrl, base);
  418. return base.origin === other.origin;
  419. }
  420. function _isValidProtocol(url) {
  421. if (!url) {
  422. return false;
  423. }
  424. switch (url.protocol) {
  425. case "http:":
  426. case "https:":
  427. case "ftp:":
  428. case "mailto:":
  429. case "tel:":
  430. return true;
  431. default:
  432. return false;
  433. }
  434. }
  435. function createValidAbsoluteUrl(url, baseUrl) {
  436. if (!url) {
  437. return null;
  438. }
  439. try {
  440. const absoluteUrl = baseUrl ? new URL(url, baseUrl) : new URL(url);
  441. if (_isValidProtocol(absoluteUrl)) {
  442. return absoluteUrl;
  443. }
  444. } catch (ex) {}
  445. return null;
  446. }
  447. function shadow(obj, prop, value) {
  448. Object.defineProperty(obj, prop, {
  449. value,
  450. enumerable: true,
  451. configurable: true,
  452. writable: false
  453. });
  454. return value;
  455. }
  456. const BaseException = function BaseExceptionClosure() {
  457. function BaseException(message) {
  458. if (this.constructor === BaseException) {
  459. unreachable("Cannot initialize BaseException.");
  460. }
  461. this.message = message;
  462. this.name = this.constructor.name;
  463. }
  464. BaseException.prototype = new Error();
  465. BaseException.constructor = BaseException;
  466. return BaseException;
  467. }();
  468. exports.BaseException = BaseException;
  469. class PasswordException extends BaseException {
  470. constructor(msg, code) {
  471. super(msg);
  472. this.code = code;
  473. }
  474. }
  475. exports.PasswordException = PasswordException;
  476. class UnknownErrorException extends BaseException {
  477. constructor(msg, details) {
  478. super(msg);
  479. this.details = details;
  480. }
  481. }
  482. exports.UnknownErrorException = UnknownErrorException;
  483. class InvalidPDFException extends BaseException {}
  484. exports.InvalidPDFException = InvalidPDFException;
  485. class MissingPDFException extends BaseException {}
  486. exports.MissingPDFException = MissingPDFException;
  487. class UnexpectedResponseException extends BaseException {
  488. constructor(msg, status) {
  489. super(msg);
  490. this.status = status;
  491. }
  492. }
  493. exports.UnexpectedResponseException = UnexpectedResponseException;
  494. class FormatError extends BaseException {}
  495. exports.FormatError = FormatError;
  496. class AbortException extends BaseException {}
  497. exports.AbortException = AbortException;
  498. const NullCharactersRegExp = /\x00/g;
  499. function removeNullCharacters(str) {
  500. if (typeof str !== "string") {
  501. warn("The argument for removeNullCharacters must be a string.");
  502. return str;
  503. }
  504. return str.replace(NullCharactersRegExp, "");
  505. }
  506. function bytesToString(bytes) {
  507. assert(bytes !== null && typeof bytes === "object" && bytes.length !== undefined, "Invalid argument for bytesToString");
  508. const length = bytes.length;
  509. const MAX_ARGUMENT_COUNT = 8192;
  510. if (length < MAX_ARGUMENT_COUNT) {
  511. return String.fromCharCode.apply(null, bytes);
  512. }
  513. const strBuf = [];
  514. for (let i = 0; i < length; i += MAX_ARGUMENT_COUNT) {
  515. const chunkEnd = Math.min(i + MAX_ARGUMENT_COUNT, length);
  516. const chunk = bytes.subarray(i, chunkEnd);
  517. strBuf.push(String.fromCharCode.apply(null, chunk));
  518. }
  519. return strBuf.join("");
  520. }
  521. function stringToBytes(str) {
  522. assert(typeof str === "string", "Invalid argument for stringToBytes");
  523. const length = str.length;
  524. const bytes = new Uint8Array(length);
  525. for (let i = 0; i < length; ++i) {
  526. bytes[i] = str.charCodeAt(i) & 0xff;
  527. }
  528. return bytes;
  529. }
  530. function arrayByteLength(arr) {
  531. if (arr.length !== undefined) {
  532. return arr.length;
  533. }
  534. assert(arr.byteLength !== undefined, "arrayByteLength - invalid argument.");
  535. return arr.byteLength;
  536. }
  537. function arraysToBytes(arr) {
  538. const length = arr.length;
  539. if (length === 1 && arr[0] instanceof Uint8Array) {
  540. return arr[0];
  541. }
  542. let resultLength = 0;
  543. for (let i = 0; i < length; i++) {
  544. resultLength += arrayByteLength(arr[i]);
  545. }
  546. let pos = 0;
  547. const data = new Uint8Array(resultLength);
  548. for (let i = 0; i < length; i++) {
  549. let item = arr[i];
  550. if (!(item instanceof Uint8Array)) {
  551. if (typeof item === "string") {
  552. item = stringToBytes(item);
  553. } else {
  554. item = new Uint8Array(item);
  555. }
  556. }
  557. const itemLength = item.byteLength;
  558. data.set(item, pos);
  559. pos += itemLength;
  560. }
  561. return data;
  562. }
  563. function string32(value) {
  564. return String.fromCharCode(value >> 24 & 0xff, value >> 16 & 0xff, value >> 8 & 0xff, value & 0xff);
  565. }
  566. function objectSize(obj) {
  567. return Object.keys(obj).length;
  568. }
  569. function objectFromMap(map) {
  570. const obj = Object.create(null);
  571. for (const [key, value] of map) {
  572. obj[key] = value;
  573. }
  574. return obj;
  575. }
  576. function isLittleEndian() {
  577. const buffer8 = new Uint8Array(4);
  578. buffer8[0] = 1;
  579. const view32 = new Uint32Array(buffer8.buffer, 0, 1);
  580. return view32[0] === 1;
  581. }
  582. const IsLittleEndianCached = {
  583. get value() {
  584. return shadow(this, "value", isLittleEndian());
  585. }
  586. };
  587. exports.IsLittleEndianCached = IsLittleEndianCached;
  588. function isEvalSupported() {
  589. try {
  590. new Function("");
  591. return true;
  592. } catch (e) {
  593. return false;
  594. }
  595. }
  596. const IsEvalSupportedCached = {
  597. get value() {
  598. return shadow(this, "value", isEvalSupported());
  599. }
  600. };
  601. exports.IsEvalSupportedCached = IsEvalSupportedCached;
  602. const hexNumbers = [...Array(256).keys()].map(n => n.toString(16).padStart(2, "0"));
  603. class Util {
  604. static makeHexColor(r, g, b) {
  605. return `#${hexNumbers[r]}${hexNumbers[g]}${hexNumbers[b]}`;
  606. }
  607. static transform(m1, m2) {
  608. 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]];
  609. }
  610. static applyTransform(p, m) {
  611. const xt = p[0] * m[0] + p[1] * m[2] + m[4];
  612. const yt = p[0] * m[1] + p[1] * m[3] + m[5];
  613. return [xt, yt];
  614. }
  615. static applyInverseTransform(p, m) {
  616. const d = m[0] * m[3] - m[1] * m[2];
  617. const xt = (p[0] * m[3] - p[1] * m[2] + m[2] * m[5] - m[4] * m[3]) / d;
  618. const yt = (-p[0] * m[1] + p[1] * m[0] + m[4] * m[1] - m[5] * m[0]) / d;
  619. return [xt, yt];
  620. }
  621. static getAxialAlignedBoundingBox(r, m) {
  622. const p1 = Util.applyTransform(r, m);
  623. const p2 = Util.applyTransform(r.slice(2, 4), m);
  624. const p3 = Util.applyTransform([r[0], r[3]], m);
  625. const p4 = Util.applyTransform([r[2], r[1]], m);
  626. 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])];
  627. }
  628. static inverseTransform(m) {
  629. const d = m[0] * m[3] - m[1] * m[2];
  630. 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];
  631. }
  632. static apply3dTransform(m, v) {
  633. 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]];
  634. }
  635. static singularValueDecompose2dScale(m) {
  636. const transpose = [m[0], m[2], m[1], m[3]];
  637. const a = m[0] * transpose[0] + m[1] * transpose[2];
  638. const b = m[0] * transpose[1] + m[1] * transpose[3];
  639. const c = m[2] * transpose[0] + m[3] * transpose[2];
  640. const d = m[2] * transpose[1] + m[3] * transpose[3];
  641. const first = (a + d) / 2;
  642. const second = Math.sqrt((a + d) ** 2 - 4 * (a * d - c * b)) / 2;
  643. const sx = first + second || 1;
  644. const sy = first - second || 1;
  645. return [Math.sqrt(sx), Math.sqrt(sy)];
  646. }
  647. static normalizeRect(rect) {
  648. const r = rect.slice(0);
  649. if (rect[0] > rect[2]) {
  650. r[0] = rect[2];
  651. r[2] = rect[0];
  652. }
  653. if (rect[1] > rect[3]) {
  654. r[1] = rect[3];
  655. r[3] = rect[1];
  656. }
  657. return r;
  658. }
  659. static intersect(rect1, rect2) {
  660. function compare(a, b) {
  661. return a - b;
  662. }
  663. const orderedX = [rect1[0], rect1[2], rect2[0], rect2[2]].sort(compare);
  664. const orderedY = [rect1[1], rect1[3], rect2[1], rect2[3]].sort(compare);
  665. const result = [];
  666. rect1 = Util.normalizeRect(rect1);
  667. rect2 = Util.normalizeRect(rect2);
  668. if (orderedX[0] === rect1[0] && orderedX[1] === rect2[0] || orderedX[0] === rect2[0] && orderedX[1] === rect1[0]) {
  669. result[0] = orderedX[1];
  670. result[2] = orderedX[2];
  671. } else {
  672. return null;
  673. }
  674. if (orderedY[0] === rect1[1] && orderedY[1] === rect2[1] || orderedY[0] === rect2[1] && orderedY[1] === rect1[1]) {
  675. result[1] = orderedY[1];
  676. result[3] = orderedY[2];
  677. } else {
  678. return null;
  679. }
  680. return result;
  681. }
  682. }
  683. exports.Util = Util;
  684. 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];
  685. function stringToPDFString(str) {
  686. const length = str.length,
  687. strBuf = [];
  688. if (str[0] === "\xFE" && str[1] === "\xFF") {
  689. for (let i = 2; i < length; i += 2) {
  690. strBuf.push(String.fromCharCode(str.charCodeAt(i) << 8 | str.charCodeAt(i + 1)));
  691. }
  692. } else if (str[0] === "\xFF" && str[1] === "\xFE") {
  693. for (let i = 2; i < length; i += 2) {
  694. strBuf.push(String.fromCharCode(str.charCodeAt(i + 1) << 8 | str.charCodeAt(i)));
  695. }
  696. } else {
  697. for (let i = 0; i < length; ++i) {
  698. const code = PDFStringTranslateTable[str.charCodeAt(i)];
  699. strBuf.push(code ? String.fromCharCode(code) : str.charAt(i));
  700. }
  701. }
  702. return strBuf.join("");
  703. }
  704. function escapeString(str) {
  705. return str.replace(/([()\\\n\r])/g, match => {
  706. if (match === "\n") {
  707. return "\\n";
  708. } else if (match === "\r") {
  709. return "\\r";
  710. }
  711. return `\\${match}`;
  712. });
  713. }
  714. function isAscii(str) {
  715. return /^[\x00-\x7F]*$/.test(str);
  716. }
  717. function stringToUTF16BEString(str) {
  718. const buf = ["\xFE\xFF"];
  719. for (let i = 0, ii = str.length; i < ii; i++) {
  720. const char = str.charCodeAt(i);
  721. buf.push(String.fromCharCode(char >> 8 & 0xff), String.fromCharCode(char & 0xff));
  722. }
  723. return buf.join("");
  724. }
  725. function stringToUTF8String(str) {
  726. return decodeURIComponent(escape(str));
  727. }
  728. function utf8StringToString(str) {
  729. return unescape(encodeURIComponent(str));
  730. }
  731. function isBool(v) {
  732. return typeof v === "boolean";
  733. }
  734. function isNum(v) {
  735. return typeof v === "number";
  736. }
  737. function isString(v) {
  738. return typeof v === "string";
  739. }
  740. function isArrayBuffer(v) {
  741. return typeof v === "object" && v !== null && v.byteLength !== undefined;
  742. }
  743. function isArrayEqual(arr1, arr2) {
  744. if (arr1.length !== arr2.length) {
  745. return false;
  746. }
  747. for (let i = 0, ii = arr1.length; i < ii; i++) {
  748. if (arr1[i] !== arr2[i]) {
  749. return false;
  750. }
  751. }
  752. return true;
  753. }
  754. function getModificationDate(date = new Date()) {
  755. 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")];
  756. return buffer.join("");
  757. }
  758. function createPromiseCapability() {
  759. const capability = Object.create(null);
  760. let isSettled = false;
  761. Object.defineProperty(capability, "settled", {
  762. get() {
  763. return isSettled;
  764. }
  765. });
  766. capability.promise = new Promise(function (resolve, reject) {
  767. capability.resolve = function (data) {
  768. isSettled = true;
  769. resolve(data);
  770. };
  771. capability.reject = function (reason) {
  772. isSettled = true;
  773. reject(reason);
  774. };
  775. });
  776. return capability;
  777. }
  778. function createObjectURL(data, contentType = "", forceDataSchema = false) {
  779. if (URL.createObjectURL && !forceDataSchema) {
  780. return URL.createObjectURL(new Blob([data], {
  781. type: contentType
  782. }));
  783. }
  784. const digits = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/=";
  785. let buffer = `data:${contentType};base64,`;
  786. for (let i = 0, ii = data.length; i < ii; i += 3) {
  787. const b1 = data[i] & 0xff;
  788. const b2 = data[i + 1] & 0xff;
  789. const b3 = data[i + 2] & 0xff;
  790. const d1 = b1 >> 2,
  791. d2 = (b1 & 3) << 4 | b2 >> 4;
  792. const d3 = i + 1 < ii ? (b2 & 0xf) << 2 | b3 >> 6 : 64;
  793. const d4 = i + 2 < ii ? b3 & 0x3f : 64;
  794. buffer += digits[d1] + digits[d2] + digits[d3] + digits[d4];
  795. }
  796. return buffer;
  797. }