util.js 26 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045
  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.VerbosityLevel = exports.Util = exports.UnknownErrorException = exports.UnexpectedResponseException = exports.UNSUPPORTED_FEATURES = 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;
  27. exports.arrayByteLength = arrayByteLength;
  28. exports.arraysToBytes = arraysToBytes;
  29. exports.assert = assert;
  30. exports.bytesToString = bytesToString;
  31. exports.createObjectURL = createObjectURL;
  32. exports.createPromiseCapability = createPromiseCapability;
  33. exports.createValidAbsoluteUrl = createValidAbsoluteUrl;
  34. exports.escapeString = escapeString;
  35. exports.getModificationDate = getModificationDate;
  36. exports.getVerbosityLevel = getVerbosityLevel;
  37. exports.info = info;
  38. exports.isArrayBuffer = isArrayBuffer;
  39. exports.isArrayEqual = isArrayEqual;
  40. exports.isAscii = isAscii;
  41. exports.isBool = isBool;
  42. exports.isNum = isNum;
  43. exports.isSameOrigin = isSameOrigin;
  44. exports.isString = isString;
  45. exports.objectFromMap = objectFromMap;
  46. exports.objectSize = objectSize;
  47. exports.removeNullCharacters = removeNullCharacters;
  48. exports.setVerbosityLevel = setVerbosityLevel;
  49. exports.shadow = shadow;
  50. exports.string32 = string32;
  51. exports.stringToBytes = stringToBytes;
  52. exports.stringToPDFString = stringToPDFString;
  53. exports.stringToUTF16BEString = stringToUTF16BEString;
  54. exports.stringToUTF8String = stringToUTF8String;
  55. exports.unreachable = unreachable;
  56. exports.utf8StringToString = utf8StringToString;
  57. exports.warn = warn;
  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. const InvisibleCharactersRegExp = /[\x01-\x1F]/g;
  548. function removeNullCharacters(str, replaceInvisible = false) {
  549. if (typeof str !== "string") {
  550. warn("The argument for removeNullCharacters must be a string.");
  551. return str;
  552. }
  553. if (replaceInvisible) {
  554. str = str.replace(InvisibleCharactersRegExp, " ");
  555. }
  556. return str.replace(NullCharactersRegExp, "");
  557. }
  558. function bytesToString(bytes) {
  559. assert(bytes !== null && typeof bytes === "object" && bytes.length !== undefined, "Invalid argument for bytesToString");
  560. const length = bytes.length;
  561. const MAX_ARGUMENT_COUNT = 8192;
  562. if (length < MAX_ARGUMENT_COUNT) {
  563. return String.fromCharCode.apply(null, bytes);
  564. }
  565. const strBuf = [];
  566. for (let i = 0; i < length; i += MAX_ARGUMENT_COUNT) {
  567. const chunkEnd = Math.min(i + MAX_ARGUMENT_COUNT, length);
  568. const chunk = bytes.subarray(i, chunkEnd);
  569. strBuf.push(String.fromCharCode.apply(null, chunk));
  570. }
  571. return strBuf.join("");
  572. }
  573. function stringToBytes(str) {
  574. assert(typeof str === "string", "Invalid argument for stringToBytes");
  575. const length = str.length;
  576. const bytes = new Uint8Array(length);
  577. for (let i = 0; i < length; ++i) {
  578. bytes[i] = str.charCodeAt(i) & 0xff;
  579. }
  580. return bytes;
  581. }
  582. function arrayByteLength(arr) {
  583. if (arr.length !== undefined) {
  584. return arr.length;
  585. }
  586. assert(arr.byteLength !== undefined, "arrayByteLength - invalid argument.");
  587. return arr.byteLength;
  588. }
  589. function arraysToBytes(arr) {
  590. const length = arr.length;
  591. if (length === 1 && arr[0] instanceof Uint8Array) {
  592. return arr[0];
  593. }
  594. let resultLength = 0;
  595. for (let i = 0; i < length; i++) {
  596. resultLength += arrayByteLength(arr[i]);
  597. }
  598. let pos = 0;
  599. const data = new Uint8Array(resultLength);
  600. for (let i = 0; i < length; i++) {
  601. let item = arr[i];
  602. if (!(item instanceof Uint8Array)) {
  603. if (typeof item === "string") {
  604. item = stringToBytes(item);
  605. } else {
  606. item = new Uint8Array(item);
  607. }
  608. }
  609. const itemLength = item.byteLength;
  610. data.set(item, pos);
  611. pos += itemLength;
  612. }
  613. return data;
  614. }
  615. function string32(value) {
  616. return String.fromCharCode(value >> 24 & 0xff, value >> 16 & 0xff, value >> 8 & 0xff, value & 0xff);
  617. }
  618. function objectSize(obj) {
  619. return Object.keys(obj).length;
  620. }
  621. function objectFromMap(map) {
  622. const obj = Object.create(null);
  623. for (const [key, value] of map) {
  624. obj[key] = value;
  625. }
  626. return obj;
  627. }
  628. function isLittleEndian() {
  629. const buffer8 = new Uint8Array(4);
  630. buffer8[0] = 1;
  631. const view32 = new Uint32Array(buffer8.buffer, 0, 1);
  632. return view32[0] === 1;
  633. }
  634. const IsLittleEndianCached = {
  635. get value() {
  636. return shadow(this, "value", isLittleEndian());
  637. }
  638. };
  639. exports.IsLittleEndianCached = IsLittleEndianCached;
  640. function isEvalSupported() {
  641. try {
  642. new Function("");
  643. return true;
  644. } catch (e) {
  645. return false;
  646. }
  647. }
  648. const IsEvalSupportedCached = {
  649. get value() {
  650. return shadow(this, "value", isEvalSupported());
  651. }
  652. };
  653. exports.IsEvalSupportedCached = IsEvalSupportedCached;
  654. const hexNumbers = [...Array(256).keys()].map(n => n.toString(16).padStart(2, "0"));
  655. class Util {
  656. static makeHexColor(r, g, b) {
  657. return `#${hexNumbers[r]}${hexNumbers[g]}${hexNumbers[b]}`;
  658. }
  659. static transform(m1, m2) {
  660. 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]];
  661. }
  662. static applyTransform(p, m) {
  663. const xt = p[0] * m[0] + p[1] * m[2] + m[4];
  664. const yt = p[0] * m[1] + p[1] * m[3] + m[5];
  665. return [xt, yt];
  666. }
  667. static applyInverseTransform(p, m) {
  668. const d = m[0] * m[3] - m[1] * m[2];
  669. const xt = (p[0] * m[3] - p[1] * m[2] + m[2] * m[5] - m[4] * m[3]) / d;
  670. const yt = (-p[0] * m[1] + p[1] * m[0] + m[4] * m[1] - m[5] * m[0]) / d;
  671. return [xt, yt];
  672. }
  673. static getAxialAlignedBoundingBox(r, m) {
  674. const p1 = Util.applyTransform(r, m);
  675. const p2 = Util.applyTransform(r.slice(2, 4), m);
  676. const p3 = Util.applyTransform([r[0], r[3]], m);
  677. const p4 = Util.applyTransform([r[2], r[1]], m);
  678. 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])];
  679. }
  680. static inverseTransform(m) {
  681. const d = m[0] * m[3] - m[1] * m[2];
  682. 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];
  683. }
  684. static apply3dTransform(m, v) {
  685. 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]];
  686. }
  687. static singularValueDecompose2dScale(m) {
  688. const transpose = [m[0], m[2], m[1], m[3]];
  689. const a = m[0] * transpose[0] + m[1] * transpose[2];
  690. const b = m[0] * transpose[1] + m[1] * transpose[3];
  691. const c = m[2] * transpose[0] + m[3] * transpose[2];
  692. const d = m[2] * transpose[1] + m[3] * transpose[3];
  693. const first = (a + d) / 2;
  694. const second = Math.sqrt((a + d) ** 2 - 4 * (a * d - c * b)) / 2;
  695. const sx = first + second || 1;
  696. const sy = first - second || 1;
  697. return [Math.sqrt(sx), Math.sqrt(sy)];
  698. }
  699. static normalizeRect(rect) {
  700. const r = rect.slice(0);
  701. if (rect[0] > rect[2]) {
  702. r[0] = rect[2];
  703. r[2] = rect[0];
  704. }
  705. if (rect[1] > rect[3]) {
  706. r[1] = rect[3];
  707. r[3] = rect[1];
  708. }
  709. return r;
  710. }
  711. static intersect(rect1, rect2) {
  712. function compare(a, b) {
  713. return a - b;
  714. }
  715. const orderedX = [rect1[0], rect1[2], rect2[0], rect2[2]].sort(compare);
  716. const orderedY = [rect1[1], rect1[3], rect2[1], rect2[3]].sort(compare);
  717. const result = [];
  718. rect1 = Util.normalizeRect(rect1);
  719. rect2 = Util.normalizeRect(rect2);
  720. if (orderedX[0] === rect1[0] && orderedX[1] === rect2[0] || orderedX[0] === rect2[0] && orderedX[1] === rect1[0]) {
  721. result[0] = orderedX[1];
  722. result[2] = orderedX[2];
  723. } else {
  724. return null;
  725. }
  726. if (orderedY[0] === rect1[1] && orderedY[1] === rect2[1] || orderedY[0] === rect2[1] && orderedY[1] === rect1[1]) {
  727. result[1] = orderedY[1];
  728. result[3] = orderedY[2];
  729. } else {
  730. return null;
  731. }
  732. return result;
  733. }
  734. static bezierBoundingBox(x0, y0, x1, y1, x2, y2, x3, y3) {
  735. const tvalues = [],
  736. bounds = [[], []];
  737. let a, b, c, t, t1, t2, b2ac, sqrtb2ac;
  738. for (let i = 0; i < 2; ++i) {
  739. if (i === 0) {
  740. b = 6 * x0 - 12 * x1 + 6 * x2;
  741. a = -3 * x0 + 9 * x1 - 9 * x2 + 3 * x3;
  742. c = 3 * x1 - 3 * x0;
  743. } else {
  744. b = 6 * y0 - 12 * y1 + 6 * y2;
  745. a = -3 * y0 + 9 * y1 - 9 * y2 + 3 * y3;
  746. c = 3 * y1 - 3 * y0;
  747. }
  748. if (Math.abs(a) < 1e-12) {
  749. if (Math.abs(b) < 1e-12) {
  750. continue;
  751. }
  752. t = -c / b;
  753. if (0 < t && t < 1) {
  754. tvalues.push(t);
  755. }
  756. continue;
  757. }
  758. b2ac = b * b - 4 * c * a;
  759. sqrtb2ac = Math.sqrt(b2ac);
  760. if (b2ac < 0) {
  761. continue;
  762. }
  763. t1 = (-b + sqrtb2ac) / (2 * a);
  764. if (0 < t1 && t1 < 1) {
  765. tvalues.push(t1);
  766. }
  767. t2 = (-b - sqrtb2ac) / (2 * a);
  768. if (0 < t2 && t2 < 1) {
  769. tvalues.push(t2);
  770. }
  771. }
  772. let j = tvalues.length,
  773. mt;
  774. const jlen = j;
  775. while (j--) {
  776. t = tvalues[j];
  777. mt = 1 - t;
  778. bounds[0][j] = mt * mt * mt * x0 + 3 * mt * mt * t * x1 + 3 * mt * t * t * x2 + t * t * t * x3;
  779. bounds[1][j] = mt * mt * mt * y0 + 3 * mt * mt * t * y1 + 3 * mt * t * t * y2 + t * t * t * y3;
  780. }
  781. bounds[0][jlen] = x0;
  782. bounds[1][jlen] = y0;
  783. bounds[0][jlen + 1] = x3;
  784. bounds[1][jlen + 1] = y3;
  785. bounds[0].length = bounds[1].length = jlen + 2;
  786. return [Math.min(...bounds[0]), Math.min(...bounds[1]), Math.max(...bounds[0]), Math.max(...bounds[1])];
  787. }
  788. }
  789. exports.Util = Util;
  790. 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];
  791. function stringToPDFString(str) {
  792. const length = str.length,
  793. strBuf = [];
  794. if (str[0] === "\xFE" && str[1] === "\xFF") {
  795. for (let i = 2; i < length; i += 2) {
  796. strBuf.push(String.fromCharCode(str.charCodeAt(i) << 8 | str.charCodeAt(i + 1)));
  797. }
  798. } else if (str[0] === "\xFF" && str[1] === "\xFE") {
  799. for (let i = 2; i < length; i += 2) {
  800. strBuf.push(String.fromCharCode(str.charCodeAt(i + 1) << 8 | str.charCodeAt(i)));
  801. }
  802. } else {
  803. for (let i = 0; i < length; ++i) {
  804. const code = PDFStringTranslateTable[str.charCodeAt(i)];
  805. strBuf.push(code ? String.fromCharCode(code) : str.charAt(i));
  806. }
  807. }
  808. return strBuf.join("");
  809. }
  810. function escapeString(str) {
  811. return str.replace(/([()\\\n\r])/g, match => {
  812. if (match === "\n") {
  813. return "\\n";
  814. } else if (match === "\r") {
  815. return "\\r";
  816. }
  817. return `\\${match}`;
  818. });
  819. }
  820. function isAscii(str) {
  821. return /^[\x00-\x7F]*$/.test(str);
  822. }
  823. function stringToUTF16BEString(str) {
  824. const buf = ["\xFE\xFF"];
  825. for (let i = 0, ii = str.length; i < ii; i++) {
  826. const char = str.charCodeAt(i);
  827. buf.push(String.fromCharCode(char >> 8 & 0xff), String.fromCharCode(char & 0xff));
  828. }
  829. return buf.join("");
  830. }
  831. function stringToUTF8String(str) {
  832. return decodeURIComponent(escape(str));
  833. }
  834. function utf8StringToString(str) {
  835. return unescape(encodeURIComponent(str));
  836. }
  837. function isBool(v) {
  838. return typeof v === "boolean";
  839. }
  840. function isNum(v) {
  841. return typeof v === "number";
  842. }
  843. function isString(v) {
  844. return typeof v === "string";
  845. }
  846. function isArrayBuffer(v) {
  847. return typeof v === "object" && v !== null && v.byteLength !== undefined;
  848. }
  849. function isArrayEqual(arr1, arr2) {
  850. if (arr1.length !== arr2.length) {
  851. return false;
  852. }
  853. for (let i = 0, ii = arr1.length; i < ii; i++) {
  854. if (arr1[i] !== arr2[i]) {
  855. return false;
  856. }
  857. }
  858. return true;
  859. }
  860. function getModificationDate(date = new Date()) {
  861. 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")];
  862. return buffer.join("");
  863. }
  864. function createPromiseCapability() {
  865. const capability = Object.create(null);
  866. let isSettled = false;
  867. Object.defineProperty(capability, "settled", {
  868. get() {
  869. return isSettled;
  870. }
  871. });
  872. capability.promise = new Promise(function (resolve, reject) {
  873. capability.resolve = function (data) {
  874. isSettled = true;
  875. resolve(data);
  876. };
  877. capability.reject = function (reason) {
  878. isSettled = true;
  879. reject(reason);
  880. };
  881. });
  882. return capability;
  883. }
  884. function createObjectURL(data, contentType = "", forceDataSchema = false) {
  885. if (URL.createObjectURL && typeof Blob !== "undefined" && !forceDataSchema) {
  886. return URL.createObjectURL(new Blob([data], {
  887. type: contentType
  888. }));
  889. }
  890. const digits = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/=";
  891. let buffer = `data:${contentType};base64,`;
  892. for (let i = 0, ii = data.length; i < ii; i += 3) {
  893. const b1 = data[i] & 0xff;
  894. const b2 = data[i + 1] & 0xff;
  895. const b3 = data[i + 2] & 0xff;
  896. const d1 = b1 >> 2,
  897. d2 = (b1 & 3) << 4 | b2 >> 4;
  898. const d3 = i + 1 < ii ? (b2 & 0xf) << 2 | b3 >> 6 : 64;
  899. const d4 = i + 2 < ii ? b3 & 0x3f : 64;
  900. buffer += digits[d1] + digits[d2] + digits[d3] + digits[d4];
  901. }
  902. return buffer;
  903. }