util.js 25 KB

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