util.js 21 KB

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