2
0

util.js 24 KB

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