util.js 21 KB

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