util.js 21 KB

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