util.js 26 KB

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