2
0

util.js 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915
  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. smask: "smask",
  356. shadingPattern: "shadingPattern",
  357. font: "font",
  358. errorTilingPattern: "errorTilingPattern",
  359. errorExtGState: "errorExtGState",
  360. errorXObject: "errorXObject",
  361. errorFontLoadType3: "errorFontLoadType3",
  362. errorFontState: "errorFontState",
  363. errorFontMissing: "errorFontMissing",
  364. errorFontTranslate: "errorFontTranslate",
  365. errorColorSpace: "errorColorSpace",
  366. errorOperatorList: "errorOperatorList",
  367. errorFontToUnicode: "errorFontToUnicode",
  368. errorFontLoadNative: "errorFontLoadNative",
  369. errorFontGetPath: "errorFontGetPath",
  370. errorMarkedContent: "errorMarkedContent"
  371. };
  372. exports.UNSUPPORTED_FEATURES = UNSUPPORTED_FEATURES;
  373. const PasswordResponses = {
  374. NEED_PASSWORD: 1,
  375. INCORRECT_PASSWORD: 2
  376. };
  377. exports.PasswordResponses = PasswordResponses;
  378. let verbosity = VerbosityLevel.WARNINGS;
  379. function setVerbosityLevel(level) {
  380. if (Number.isInteger(level)) {
  381. verbosity = level;
  382. }
  383. }
  384. function getVerbosityLevel() {
  385. return verbosity;
  386. }
  387. function info(msg) {
  388. if (verbosity >= VerbosityLevel.INFOS) {
  389. console.log(`Info: ${msg}`);
  390. }
  391. }
  392. function warn(msg) {
  393. if (verbosity >= VerbosityLevel.WARNINGS) {
  394. console.log(`Warning: ${msg}`);
  395. }
  396. }
  397. function unreachable(msg) {
  398. throw new Error(msg);
  399. }
  400. function assert(cond, msg) {
  401. if (!cond) {
  402. unreachable(msg);
  403. }
  404. }
  405. function isSameOrigin(baseUrl, otherUrl) {
  406. let base;
  407. try {
  408. base = new URL(baseUrl);
  409. if (!base.origin || base.origin === "null") {
  410. return false;
  411. }
  412. } catch (e) {
  413. return false;
  414. }
  415. const other = new URL(otherUrl, base);
  416. return base.origin === other.origin;
  417. }
  418. function _isValidProtocol(url) {
  419. if (!url) {
  420. return false;
  421. }
  422. switch (url.protocol) {
  423. case "http:":
  424. case "https:":
  425. case "ftp:":
  426. case "mailto:":
  427. case "tel:":
  428. return true;
  429. default:
  430. return false;
  431. }
  432. }
  433. function createValidAbsoluteUrl(url, baseUrl) {
  434. if (!url) {
  435. return null;
  436. }
  437. try {
  438. const absoluteUrl = baseUrl ? new URL(url, baseUrl) : new URL(url);
  439. if (_isValidProtocol(absoluteUrl)) {
  440. return absoluteUrl;
  441. }
  442. } catch (ex) {}
  443. return null;
  444. }
  445. function shadow(obj, prop, value) {
  446. Object.defineProperty(obj, prop, {
  447. value,
  448. enumerable: true,
  449. configurable: true,
  450. writable: false
  451. });
  452. return value;
  453. }
  454. const BaseException = function BaseExceptionClosure() {
  455. function BaseException(message) {
  456. if (this.constructor === BaseException) {
  457. unreachable("Cannot initialize BaseException.");
  458. }
  459. this.message = message;
  460. this.name = this.constructor.name;
  461. }
  462. BaseException.prototype = new Error();
  463. BaseException.constructor = BaseException;
  464. return BaseException;
  465. }();
  466. exports.BaseException = BaseException;
  467. class PasswordException extends BaseException {
  468. constructor(msg, code) {
  469. super(msg);
  470. this.code = code;
  471. }
  472. }
  473. exports.PasswordException = PasswordException;
  474. class UnknownErrorException extends BaseException {
  475. constructor(msg, details) {
  476. super(msg);
  477. this.details = details;
  478. }
  479. }
  480. exports.UnknownErrorException = UnknownErrorException;
  481. class InvalidPDFException extends BaseException {}
  482. exports.InvalidPDFException = InvalidPDFException;
  483. class MissingPDFException extends BaseException {}
  484. exports.MissingPDFException = MissingPDFException;
  485. class UnexpectedResponseException extends BaseException {
  486. constructor(msg, status) {
  487. super(msg);
  488. this.status = status;
  489. }
  490. }
  491. exports.UnexpectedResponseException = UnexpectedResponseException;
  492. class FormatError extends BaseException {}
  493. exports.FormatError = FormatError;
  494. class AbortException extends BaseException {}
  495. exports.AbortException = AbortException;
  496. const NullCharactersRegExp = /\x00/g;
  497. function removeNullCharacters(str) {
  498. if (typeof str !== "string") {
  499. warn("The argument for removeNullCharacters must be a string.");
  500. return str;
  501. }
  502. return str.replace(NullCharactersRegExp, "");
  503. }
  504. function bytesToString(bytes) {
  505. assert(bytes !== null && typeof bytes === "object" && bytes.length !== undefined, "Invalid argument for bytesToString");
  506. const length = bytes.length;
  507. const MAX_ARGUMENT_COUNT = 8192;
  508. if (length < MAX_ARGUMENT_COUNT) {
  509. return String.fromCharCode.apply(null, bytes);
  510. }
  511. const strBuf = [];
  512. for (let i = 0; i < length; i += MAX_ARGUMENT_COUNT) {
  513. const chunkEnd = Math.min(i + MAX_ARGUMENT_COUNT, length);
  514. const chunk = bytes.subarray(i, chunkEnd);
  515. strBuf.push(String.fromCharCode.apply(null, chunk));
  516. }
  517. return strBuf.join("");
  518. }
  519. function stringToBytes(str) {
  520. assert(typeof str === "string", "Invalid argument for stringToBytes");
  521. const length = str.length;
  522. const bytes = new Uint8Array(length);
  523. for (let i = 0; i < length; ++i) {
  524. bytes[i] = str.charCodeAt(i) & 0xff;
  525. }
  526. return bytes;
  527. }
  528. function arrayByteLength(arr) {
  529. if (arr.length !== undefined) {
  530. return arr.length;
  531. }
  532. assert(arr.byteLength !== undefined, "arrayByteLength - invalid argument.");
  533. return arr.byteLength;
  534. }
  535. function arraysToBytes(arr) {
  536. const length = arr.length;
  537. if (length === 1 && arr[0] instanceof Uint8Array) {
  538. return arr[0];
  539. }
  540. let resultLength = 0;
  541. for (let i = 0; i < length; i++) {
  542. resultLength += arrayByteLength(arr[i]);
  543. }
  544. let pos = 0;
  545. const data = new Uint8Array(resultLength);
  546. for (let i = 0; i < length; i++) {
  547. let item = arr[i];
  548. if (!(item instanceof Uint8Array)) {
  549. if (typeof item === "string") {
  550. item = stringToBytes(item);
  551. } else {
  552. item = new Uint8Array(item);
  553. }
  554. }
  555. const itemLength = item.byteLength;
  556. data.set(item, pos);
  557. pos += itemLength;
  558. }
  559. return data;
  560. }
  561. function string32(value) {
  562. return String.fromCharCode(value >> 24 & 0xff, value >> 16 & 0xff, value >> 8 & 0xff, value & 0xff);
  563. }
  564. function objectSize(obj) {
  565. return Object.keys(obj).length;
  566. }
  567. function objectFromMap(map) {
  568. const obj = Object.create(null);
  569. for (const [key, value] of map) {
  570. obj[key] = value;
  571. }
  572. return obj;
  573. }
  574. function isLittleEndian() {
  575. const buffer8 = new Uint8Array(4);
  576. buffer8[0] = 1;
  577. const view32 = new Uint32Array(buffer8.buffer, 0, 1);
  578. return view32[0] === 1;
  579. }
  580. const IsLittleEndianCached = {
  581. get value() {
  582. return shadow(this, "value", isLittleEndian());
  583. }
  584. };
  585. exports.IsLittleEndianCached = IsLittleEndianCached;
  586. function isEvalSupported() {
  587. try {
  588. new Function("");
  589. return true;
  590. } catch (e) {
  591. return false;
  592. }
  593. }
  594. const IsEvalSupportedCached = {
  595. get value() {
  596. return shadow(this, "value", isEvalSupported());
  597. }
  598. };
  599. exports.IsEvalSupportedCached = IsEvalSupportedCached;
  600. const hexNumbers = [...Array(256).keys()].map(n => n.toString(16).padStart(2, "0"));
  601. class Util {
  602. static makeHexColor(r, g, b) {
  603. return `#${hexNumbers[r]}${hexNumbers[g]}${hexNumbers[b]}`;
  604. }
  605. static transform(m1, m2) {
  606. 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]];
  607. }
  608. static applyTransform(p, m) {
  609. const xt = p[0] * m[0] + p[1] * m[2] + m[4];
  610. const yt = p[0] * m[1] + p[1] * m[3] + m[5];
  611. return [xt, yt];
  612. }
  613. static applyInverseTransform(p, m) {
  614. const d = m[0] * m[3] - m[1] * m[2];
  615. const xt = (p[0] * m[3] - p[1] * m[2] + m[2] * m[5] - m[4] * m[3]) / d;
  616. const yt = (-p[0] * m[1] + p[1] * m[0] + m[4] * m[1] - m[5] * m[0]) / d;
  617. return [xt, yt];
  618. }
  619. static getAxialAlignedBoundingBox(r, m) {
  620. const p1 = Util.applyTransform(r, m);
  621. const p2 = Util.applyTransform(r.slice(2, 4), m);
  622. const p3 = Util.applyTransform([r[0], r[3]], m);
  623. const p4 = Util.applyTransform([r[2], r[1]], m);
  624. 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])];
  625. }
  626. static inverseTransform(m) {
  627. const d = m[0] * m[3] - m[1] * m[2];
  628. 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];
  629. }
  630. static apply3dTransform(m, v) {
  631. 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]];
  632. }
  633. static singularValueDecompose2dScale(m) {
  634. const transpose = [m[0], m[2], m[1], m[3]];
  635. const a = m[0] * transpose[0] + m[1] * transpose[2];
  636. const b = m[0] * transpose[1] + m[1] * transpose[3];
  637. const c = m[2] * transpose[0] + m[3] * transpose[2];
  638. const d = m[2] * transpose[1] + m[3] * transpose[3];
  639. const first = (a + d) / 2;
  640. const second = Math.sqrt((a + d) ** 2 - 4 * (a * d - c * b)) / 2;
  641. const sx = first + second || 1;
  642. const sy = first - second || 1;
  643. return [Math.sqrt(sx), Math.sqrt(sy)];
  644. }
  645. static normalizeRect(rect) {
  646. const r = rect.slice(0);
  647. if (rect[0] > rect[2]) {
  648. r[0] = rect[2];
  649. r[2] = rect[0];
  650. }
  651. if (rect[1] > rect[3]) {
  652. r[1] = rect[3];
  653. r[3] = rect[1];
  654. }
  655. return r;
  656. }
  657. static intersect(rect1, rect2) {
  658. function compare(a, b) {
  659. return a - b;
  660. }
  661. const orderedX = [rect1[0], rect1[2], rect2[0], rect2[2]].sort(compare);
  662. const orderedY = [rect1[1], rect1[3], rect2[1], rect2[3]].sort(compare);
  663. const result = [];
  664. rect1 = Util.normalizeRect(rect1);
  665. rect2 = Util.normalizeRect(rect2);
  666. if (orderedX[0] === rect1[0] && orderedX[1] === rect2[0] || orderedX[0] === rect2[0] && orderedX[1] === rect1[0]) {
  667. result[0] = orderedX[1];
  668. result[2] = orderedX[2];
  669. } else {
  670. return null;
  671. }
  672. if (orderedY[0] === rect1[1] && orderedY[1] === rect2[1] || orderedY[0] === rect2[1] && orderedY[1] === rect1[1]) {
  673. result[1] = orderedY[1];
  674. result[3] = orderedY[2];
  675. } else {
  676. return null;
  677. }
  678. return result;
  679. }
  680. }
  681. exports.Util = Util;
  682. 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];
  683. function stringToPDFString(str) {
  684. const length = str.length,
  685. strBuf = [];
  686. if (str[0] === "\xFE" && str[1] === "\xFF") {
  687. for (let i = 2; i < length; i += 2) {
  688. strBuf.push(String.fromCharCode(str.charCodeAt(i) << 8 | str.charCodeAt(i + 1)));
  689. }
  690. } else if (str[0] === "\xFF" && str[1] === "\xFE") {
  691. for (let i = 2; i < length; i += 2) {
  692. strBuf.push(String.fromCharCode(str.charCodeAt(i + 1) << 8 | str.charCodeAt(i)));
  693. }
  694. } else {
  695. for (let i = 0; i < length; ++i) {
  696. const code = PDFStringTranslateTable[str.charCodeAt(i)];
  697. strBuf.push(code ? String.fromCharCode(code) : str.charAt(i));
  698. }
  699. }
  700. return strBuf.join("");
  701. }
  702. function escapeString(str) {
  703. return str.replace(/([()\\\n\r])/g, match => {
  704. if (match === "\n") {
  705. return "\\n";
  706. } else if (match === "\r") {
  707. return "\\r";
  708. }
  709. return `\\${match}`;
  710. });
  711. }
  712. function isAscii(str) {
  713. return /^[\x00-\x7F]*$/.test(str);
  714. }
  715. function stringToUTF16BEString(str) {
  716. const buf = ["\xFE\xFF"];
  717. for (let i = 0, ii = str.length; i < ii; i++) {
  718. const char = str.charCodeAt(i);
  719. buf.push(String.fromCharCode(char >> 8 & 0xff));
  720. buf.push(String.fromCharCode(char & 0xff));
  721. }
  722. return buf.join("");
  723. }
  724. function stringToUTF8String(str) {
  725. return decodeURIComponent(escape(str));
  726. }
  727. function utf8StringToString(str) {
  728. return unescape(encodeURIComponent(str));
  729. }
  730. function isBool(v) {
  731. return typeof v === "boolean";
  732. }
  733. function isNum(v) {
  734. return typeof v === "number";
  735. }
  736. function isString(v) {
  737. return typeof v === "string";
  738. }
  739. function isArrayBuffer(v) {
  740. return typeof v === "object" && v !== null && v.byteLength !== undefined;
  741. }
  742. function isArrayEqual(arr1, arr2) {
  743. if (arr1.length !== arr2.length) {
  744. return false;
  745. }
  746. for (let i = 0, ii = arr1.length; i < ii; i++) {
  747. if (arr1[i] !== arr2[i]) {
  748. return false;
  749. }
  750. }
  751. return true;
  752. }
  753. function getModificationDate(date = new Date()) {
  754. 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")];
  755. return buffer.join("");
  756. }
  757. function createPromiseCapability() {
  758. const capability = Object.create(null);
  759. let isSettled = false;
  760. Object.defineProperty(capability, "settled", {
  761. get() {
  762. return isSettled;
  763. }
  764. });
  765. capability.promise = new Promise(function (resolve, reject) {
  766. capability.resolve = function (data) {
  767. isSettled = true;
  768. resolve(data);
  769. };
  770. capability.reject = function (reason) {
  771. isSettled = true;
  772. reject(reason);
  773. };
  774. });
  775. return capability;
  776. }
  777. function createObjectURL(data, contentType = "", forceDataSchema = false) {
  778. if (URL.createObjectURL && !forceDataSchema) {
  779. return URL.createObjectURL(new Blob([data], {
  780. type: contentType
  781. }));
  782. }
  783. const digits = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/=";
  784. let buffer = `data:${contentType};base64,`;
  785. for (let i = 0, ii = data.length; i < ii; i += 3) {
  786. const b1 = data[i] & 0xff;
  787. const b2 = data[i + 1] & 0xff;
  788. const b3 = data[i + 2] & 0xff;
  789. const d1 = b1 >> 2,
  790. d2 = (b1 & 3) << 4 | b2 >> 4;
  791. const d3 = i + 1 < ii ? (b2 & 0xf) << 2 | b3 >> 6 : 64;
  792. const d4 = i + 2 < ii ? b3 & 0x3f : 64;
  793. buffer += digits[d1] + digits[d2] + digits[d3] + digits[d4];
  794. }
  795. return buffer;
  796. }