util.js 26 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060
  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. 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 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. STREAM: 2
  289. };
  290. exports.CMapCompressionType = CMapCompressionType;
  291. const OPS = {
  292. dependency: 1,
  293. setLineWidth: 2,
  294. setLineCap: 3,
  295. setLineJoin: 4,
  296. setMiterLimit: 5,
  297. setDash: 6,
  298. setRenderingIntent: 7,
  299. setFlatness: 8,
  300. setGState: 9,
  301. save: 10,
  302. restore: 11,
  303. transform: 12,
  304. moveTo: 13,
  305. lineTo: 14,
  306. curveTo: 15,
  307. curveTo2: 16,
  308. curveTo3: 17,
  309. closePath: 18,
  310. rectangle: 19,
  311. stroke: 20,
  312. closeStroke: 21,
  313. fill: 22,
  314. eoFill: 23,
  315. fillStroke: 24,
  316. eoFillStroke: 25,
  317. closeFillStroke: 26,
  318. closeEOFillStroke: 27,
  319. endPath: 28,
  320. clip: 29,
  321. eoClip: 30,
  322. beginText: 31,
  323. endText: 32,
  324. setCharSpacing: 33,
  325. setWordSpacing: 34,
  326. setHScale: 35,
  327. setLeading: 36,
  328. setFont: 37,
  329. setTextRenderingMode: 38,
  330. setTextRise: 39,
  331. moveText: 40,
  332. setLeadingMoveText: 41,
  333. setTextMatrix: 42,
  334. nextLine: 43,
  335. showText: 44,
  336. showSpacedText: 45,
  337. nextLineShowText: 46,
  338. nextLineSetSpacingShowText: 47,
  339. setCharWidth: 48,
  340. setCharWidthAndBounds: 49,
  341. setStrokeColorSpace: 50,
  342. setFillColorSpace: 51,
  343. setStrokeColor: 52,
  344. setStrokeColorN: 53,
  345. setFillColor: 54,
  346. setFillColorN: 55,
  347. setStrokeGray: 56,
  348. setFillGray: 57,
  349. setStrokeRGBColor: 58,
  350. setFillRGBColor: 59,
  351. setStrokeCMYKColor: 60,
  352. setFillCMYKColor: 61,
  353. shadingFill: 62,
  354. beginInlineImage: 63,
  355. beginImageData: 64,
  356. endInlineImage: 65,
  357. paintXObject: 66,
  358. markPoint: 67,
  359. markPointProps: 68,
  360. beginMarkedContent: 69,
  361. beginMarkedContentProps: 70,
  362. endMarkedContent: 71,
  363. beginCompat: 72,
  364. endCompat: 73,
  365. paintFormXObjectBegin: 74,
  366. paintFormXObjectEnd: 75,
  367. beginGroup: 76,
  368. endGroup: 77,
  369. beginAnnotations: 78,
  370. endAnnotations: 79,
  371. beginAnnotation: 80,
  372. endAnnotation: 81,
  373. paintJpegXObject: 82,
  374. paintImageMaskXObject: 83,
  375. paintImageMaskXObjectGroup: 84,
  376. paintImageXObject: 85,
  377. paintInlineImageXObject: 86,
  378. paintInlineImageXObjectGroup: 87,
  379. paintImageXObjectRepeat: 88,
  380. paintImageMaskXObjectRepeat: 89,
  381. paintSolidColorImageMask: 90,
  382. constructPath: 91
  383. };
  384. exports.OPS = OPS;
  385. const UNSUPPORTED_FEATURES = {
  386. unknown: "unknown",
  387. forms: "forms",
  388. javaScript: "javaScript",
  389. signatures: "signatures",
  390. smask: "smask",
  391. shadingPattern: "shadingPattern",
  392. font: "font",
  393. errorTilingPattern: "errorTilingPattern",
  394. errorExtGState: "errorExtGState",
  395. errorXObject: "errorXObject",
  396. errorFontLoadType3: "errorFontLoadType3",
  397. errorFontState: "errorFontState",
  398. errorFontMissing: "errorFontMissing",
  399. errorFontTranslate: "errorFontTranslate",
  400. errorColorSpace: "errorColorSpace",
  401. errorOperatorList: "errorOperatorList",
  402. errorFontToUnicode: "errorFontToUnicode",
  403. errorFontLoadNative: "errorFontLoadNative",
  404. errorFontBuildPath: "errorFontBuildPath",
  405. errorFontGetPath: "errorFontGetPath",
  406. errorMarkedContent: "errorMarkedContent",
  407. errorContentSubStream: "errorContentSubStream"
  408. };
  409. exports.UNSUPPORTED_FEATURES = UNSUPPORTED_FEATURES;
  410. const PasswordResponses = {
  411. NEED_PASSWORD: 1,
  412. INCORRECT_PASSWORD: 2
  413. };
  414. exports.PasswordResponses = PasswordResponses;
  415. let verbosity = VerbosityLevel.WARNINGS;
  416. function setVerbosityLevel(level) {
  417. if (Number.isInteger(level)) {
  418. verbosity = level;
  419. }
  420. }
  421. function getVerbosityLevel() {
  422. return verbosity;
  423. }
  424. function info(msg) {
  425. if (verbosity >= VerbosityLevel.INFOS) {
  426. console.log(`Info: ${msg}`);
  427. }
  428. }
  429. function warn(msg) {
  430. if (verbosity >= VerbosityLevel.WARNINGS) {
  431. console.log(`Warning: ${msg}`);
  432. }
  433. }
  434. function unreachable(msg) {
  435. throw new Error(msg);
  436. }
  437. function assert(cond, msg) {
  438. if (!cond) {
  439. unreachable(msg);
  440. }
  441. }
  442. function _isValidProtocol(url) {
  443. if (!url) {
  444. return false;
  445. }
  446. switch (url.protocol) {
  447. case "http:":
  448. case "https:":
  449. case "ftp:":
  450. case "mailto:":
  451. case "tel:":
  452. return true;
  453. default:
  454. return false;
  455. }
  456. }
  457. function createValidAbsoluteUrl(url, baseUrl = null, options = null) {
  458. if (!url) {
  459. return null;
  460. }
  461. try {
  462. if (options && typeof url === "string") {
  463. if (options.addDefaultProtocol && url.startsWith("www.")) {
  464. const dots = url.match(/\./g);
  465. if (dots && dots.length >= 2) {
  466. url = `http://${url}`;
  467. }
  468. }
  469. if (options.tryConvertEncoding) {
  470. try {
  471. url = stringToUTF8String(url);
  472. } catch (ex) {}
  473. }
  474. }
  475. const absoluteUrl = baseUrl ? new URL(url, baseUrl) : new URL(url);
  476. if (_isValidProtocol(absoluteUrl)) {
  477. return absoluteUrl;
  478. }
  479. } catch (ex) {}
  480. return null;
  481. }
  482. function shadow(obj, prop, value) {
  483. Object.defineProperty(obj, prop, {
  484. value,
  485. enumerable: true,
  486. configurable: true,
  487. writable: false
  488. });
  489. return value;
  490. }
  491. const BaseException = function BaseExceptionClosure() {
  492. function BaseException(message, name) {
  493. if (this.constructor === BaseException) {
  494. unreachable("Cannot initialize BaseException.");
  495. }
  496. this.message = message;
  497. this.name = name;
  498. }
  499. BaseException.prototype = new Error();
  500. BaseException.constructor = BaseException;
  501. return BaseException;
  502. }();
  503. exports.BaseException = BaseException;
  504. class PasswordException extends BaseException {
  505. constructor(msg, code) {
  506. super(msg, "PasswordException");
  507. this.code = code;
  508. }
  509. }
  510. exports.PasswordException = PasswordException;
  511. class UnknownErrorException extends BaseException {
  512. constructor(msg, details) {
  513. super(msg, "UnknownErrorException");
  514. this.details = details;
  515. }
  516. }
  517. exports.UnknownErrorException = UnknownErrorException;
  518. class InvalidPDFException extends BaseException {
  519. constructor(msg) {
  520. super(msg, "InvalidPDFException");
  521. }
  522. }
  523. exports.InvalidPDFException = InvalidPDFException;
  524. class MissingPDFException extends BaseException {
  525. constructor(msg) {
  526. super(msg, "MissingPDFException");
  527. }
  528. }
  529. exports.MissingPDFException = MissingPDFException;
  530. class UnexpectedResponseException extends BaseException {
  531. constructor(msg, status) {
  532. super(msg, "UnexpectedResponseException");
  533. this.status = status;
  534. }
  535. }
  536. exports.UnexpectedResponseException = UnexpectedResponseException;
  537. class FormatError extends BaseException {
  538. constructor(msg) {
  539. super(msg, "FormatError");
  540. }
  541. }
  542. exports.FormatError = FormatError;
  543. class AbortException extends BaseException {
  544. constructor(msg) {
  545. super(msg, "AbortException");
  546. }
  547. }
  548. exports.AbortException = AbortException;
  549. function bytesToString(bytes) {
  550. if (typeof bytes !== "object" || bytes === null || bytes.length === undefined) {
  551. unreachable("Invalid argument for bytesToString");
  552. }
  553. const length = bytes.length;
  554. const MAX_ARGUMENT_COUNT = 8192;
  555. if (length < MAX_ARGUMENT_COUNT) {
  556. return String.fromCharCode.apply(null, bytes);
  557. }
  558. const strBuf = [];
  559. for (let i = 0; i < length; i += MAX_ARGUMENT_COUNT) {
  560. const chunkEnd = Math.min(i + MAX_ARGUMENT_COUNT, length);
  561. const chunk = bytes.subarray(i, chunkEnd);
  562. strBuf.push(String.fromCharCode.apply(null, chunk));
  563. }
  564. return strBuf.join("");
  565. }
  566. function stringToBytes(str) {
  567. if (typeof str !== "string") {
  568. unreachable("Invalid argument for stringToBytes");
  569. }
  570. const length = str.length;
  571. const bytes = new Uint8Array(length);
  572. for (let i = 0; i < length; ++i) {
  573. bytes[i] = str.charCodeAt(i) & 0xff;
  574. }
  575. return bytes;
  576. }
  577. function arrayByteLength(arr) {
  578. if (arr.length !== undefined) {
  579. return arr.length;
  580. }
  581. if (arr.byteLength !== undefined) {
  582. return arr.byteLength;
  583. }
  584. unreachable("Invalid argument for arrayByteLength");
  585. }
  586. function arraysToBytes(arr) {
  587. const length = arr.length;
  588. if (length === 1 && arr[0] instanceof Uint8Array) {
  589. return arr[0];
  590. }
  591. let resultLength = 0;
  592. for (let i = 0; i < length; i++) {
  593. resultLength += arrayByteLength(arr[i]);
  594. }
  595. let pos = 0;
  596. const data = new Uint8Array(resultLength);
  597. for (let i = 0; i < length; i++) {
  598. let item = arr[i];
  599. if (!(item instanceof Uint8Array)) {
  600. if (typeof item === "string") {
  601. item = stringToBytes(item);
  602. } else {
  603. item = new Uint8Array(item);
  604. }
  605. }
  606. const itemLength = item.byteLength;
  607. data.set(item, pos);
  608. pos += itemLength;
  609. }
  610. return data;
  611. }
  612. function string32(value) {
  613. return String.fromCharCode(value >> 24 & 0xff, value >> 16 & 0xff, value >> 8 & 0xff, value & 0xff);
  614. }
  615. function objectSize(obj) {
  616. return Object.keys(obj).length;
  617. }
  618. function objectFromMap(map) {
  619. const obj = Object.create(null);
  620. for (const [key, value] of map) {
  621. obj[key] = value;
  622. }
  623. return obj;
  624. }
  625. function isLittleEndian() {
  626. const buffer8 = new Uint8Array(4);
  627. buffer8[0] = 1;
  628. const view32 = new Uint32Array(buffer8.buffer, 0, 1);
  629. return view32[0] === 1;
  630. }
  631. function isEvalSupported() {
  632. try {
  633. new Function("");
  634. return true;
  635. } catch (e) {
  636. return false;
  637. }
  638. }
  639. class FeatureTest {
  640. static get isLittleEndian() {
  641. return shadow(this, "isLittleEndian", isLittleEndian());
  642. }
  643. static get isEvalSupported() {
  644. return shadow(this, "isEvalSupported", isEvalSupported());
  645. }
  646. static get isOffscreenCanvasSupported() {
  647. return shadow(this, "isOffscreenCanvasSupported", typeof OffscreenCanvas !== "undefined");
  648. }
  649. }
  650. exports.FeatureTest = FeatureTest;
  651. const hexNumbers = [...Array(256).keys()].map(n => n.toString(16).padStart(2, "0"));
  652. class Util {
  653. static makeHexColor(r, g, b) {
  654. return `#${hexNumbers[r]}${hexNumbers[g]}${hexNumbers[b]}`;
  655. }
  656. static scaleMinMax(transform, minMax) {
  657. let temp;
  658. if (transform[0]) {
  659. if (transform[0] < 0) {
  660. temp = minMax[0];
  661. minMax[0] = minMax[1];
  662. minMax[1] = temp;
  663. }
  664. minMax[0] *= transform[0];
  665. minMax[1] *= transform[0];
  666. if (transform[3] < 0) {
  667. temp = minMax[2];
  668. minMax[2] = minMax[3];
  669. minMax[3] = temp;
  670. }
  671. minMax[2] *= transform[3];
  672. minMax[3] *= transform[3];
  673. } else {
  674. temp = minMax[0];
  675. minMax[0] = minMax[2];
  676. minMax[2] = temp;
  677. temp = minMax[1];
  678. minMax[1] = minMax[3];
  679. minMax[3] = temp;
  680. if (transform[1] < 0) {
  681. temp = minMax[2];
  682. minMax[2] = minMax[3];
  683. minMax[3] = temp;
  684. }
  685. minMax[2] *= transform[1];
  686. minMax[3] *= transform[1];
  687. if (transform[2] < 0) {
  688. temp = minMax[0];
  689. minMax[0] = minMax[1];
  690. minMax[1] = temp;
  691. }
  692. minMax[0] *= transform[2];
  693. minMax[1] *= transform[2];
  694. }
  695. minMax[0] += transform[4];
  696. minMax[1] += transform[4];
  697. minMax[2] += transform[5];
  698. minMax[3] += transform[5];
  699. }
  700. static transform(m1, m2) {
  701. 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]];
  702. }
  703. static applyTransform(p, m) {
  704. const xt = p[0] * m[0] + p[1] * m[2] + m[4];
  705. const yt = p[0] * m[1] + p[1] * m[3] + m[5];
  706. return [xt, yt];
  707. }
  708. static applyInverseTransform(p, m) {
  709. const d = m[0] * m[3] - m[1] * m[2];
  710. const xt = (p[0] * m[3] - p[1] * m[2] + m[2] * m[5] - m[4] * m[3]) / d;
  711. const yt = (-p[0] * m[1] + p[1] * m[0] + m[4] * m[1] - m[5] * m[0]) / d;
  712. return [xt, yt];
  713. }
  714. static getAxialAlignedBoundingBox(r, m) {
  715. const p1 = Util.applyTransform(r, m);
  716. const p2 = Util.applyTransform(r.slice(2, 4), m);
  717. const p3 = Util.applyTransform([r[0], r[3]], m);
  718. const p4 = Util.applyTransform([r[2], r[1]], m);
  719. 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])];
  720. }
  721. static inverseTransform(m) {
  722. const d = m[0] * m[3] - m[1] * m[2];
  723. 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];
  724. }
  725. static apply3dTransform(m, v) {
  726. 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]];
  727. }
  728. static singularValueDecompose2dScale(m) {
  729. const transpose = [m[0], m[2], m[1], m[3]];
  730. const a = m[0] * transpose[0] + m[1] * transpose[2];
  731. const b = m[0] * transpose[1] + m[1] * transpose[3];
  732. const c = m[2] * transpose[0] + m[3] * transpose[2];
  733. const d = m[2] * transpose[1] + m[3] * transpose[3];
  734. const first = (a + d) / 2;
  735. const second = Math.sqrt((a + d) ** 2 - 4 * (a * d - c * b)) / 2;
  736. const sx = first + second || 1;
  737. const sy = first - second || 1;
  738. return [Math.sqrt(sx), Math.sqrt(sy)];
  739. }
  740. static normalizeRect(rect) {
  741. const r = rect.slice(0);
  742. if (rect[0] > rect[2]) {
  743. r[0] = rect[2];
  744. r[2] = rect[0];
  745. }
  746. if (rect[1] > rect[3]) {
  747. r[1] = rect[3];
  748. r[3] = rect[1];
  749. }
  750. return r;
  751. }
  752. static intersect(rect1, rect2) {
  753. const xLow = Math.max(Math.min(rect1[0], rect1[2]), Math.min(rect2[0], rect2[2]));
  754. const xHigh = Math.min(Math.max(rect1[0], rect1[2]), Math.max(rect2[0], rect2[2]));
  755. if (xLow > xHigh) {
  756. return null;
  757. }
  758. const yLow = Math.max(Math.min(rect1[1], rect1[3]), Math.min(rect2[1], rect2[3]));
  759. const yHigh = Math.min(Math.max(rect1[1], rect1[3]), Math.max(rect2[1], rect2[3]));
  760. if (yLow > yHigh) {
  761. return null;
  762. }
  763. return [xLow, yLow, xHigh, yHigh];
  764. }
  765. static bezierBoundingBox(x0, y0, x1, y1, x2, y2, x3, y3) {
  766. const tvalues = [],
  767. bounds = [[], []];
  768. let a, b, c, t, t1, t2, b2ac, sqrtb2ac;
  769. for (let i = 0; i < 2; ++i) {
  770. if (i === 0) {
  771. b = 6 * x0 - 12 * x1 + 6 * x2;
  772. a = -3 * x0 + 9 * x1 - 9 * x2 + 3 * x3;
  773. c = 3 * x1 - 3 * x0;
  774. } else {
  775. b = 6 * y0 - 12 * y1 + 6 * y2;
  776. a = -3 * y0 + 9 * y1 - 9 * y2 + 3 * y3;
  777. c = 3 * y1 - 3 * y0;
  778. }
  779. if (Math.abs(a) < 1e-12) {
  780. if (Math.abs(b) < 1e-12) {
  781. continue;
  782. }
  783. t = -c / b;
  784. if (0 < t && t < 1) {
  785. tvalues.push(t);
  786. }
  787. continue;
  788. }
  789. b2ac = b * b - 4 * c * a;
  790. sqrtb2ac = Math.sqrt(b2ac);
  791. if (b2ac < 0) {
  792. continue;
  793. }
  794. t1 = (-b + sqrtb2ac) / (2 * a);
  795. if (0 < t1 && t1 < 1) {
  796. tvalues.push(t1);
  797. }
  798. t2 = (-b - sqrtb2ac) / (2 * a);
  799. if (0 < t2 && t2 < 1) {
  800. tvalues.push(t2);
  801. }
  802. }
  803. let j = tvalues.length,
  804. mt;
  805. const jlen = j;
  806. while (j--) {
  807. t = tvalues[j];
  808. mt = 1 - t;
  809. bounds[0][j] = mt * mt * mt * x0 + 3 * mt * mt * t * x1 + 3 * mt * t * t * x2 + t * t * t * x3;
  810. bounds[1][j] = mt * mt * mt * y0 + 3 * mt * mt * t * y1 + 3 * mt * t * t * y2 + t * t * t * y3;
  811. }
  812. bounds[0][jlen] = x0;
  813. bounds[1][jlen] = y0;
  814. bounds[0][jlen + 1] = x3;
  815. bounds[1][jlen + 1] = y3;
  816. bounds[0].length = bounds[1].length = jlen + 2;
  817. return [Math.min(...bounds[0]), Math.min(...bounds[1]), Math.max(...bounds[0]), Math.max(...bounds[1])];
  818. }
  819. }
  820. exports.Util = Util;
  821. 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];
  822. function stringToPDFString(str) {
  823. if (str[0] >= "\xEF") {
  824. let encoding;
  825. if (str[0] === "\xFE" && str[1] === "\xFF") {
  826. encoding = "utf-16be";
  827. } else if (str[0] === "\xFF" && str[1] === "\xFE") {
  828. encoding = "utf-16le";
  829. } else if (str[0] === "\xEF" && str[1] === "\xBB" && str[2] === "\xBF") {
  830. encoding = "utf-8";
  831. }
  832. if (encoding) {
  833. try {
  834. const decoder = new TextDecoder(encoding, {
  835. fatal: true
  836. });
  837. const buffer = stringToBytes(str);
  838. return decoder.decode(buffer);
  839. } catch (ex) {
  840. warn(`stringToPDFString: "${ex}".`);
  841. }
  842. }
  843. }
  844. const strBuf = [];
  845. for (let i = 0, ii = str.length; i < ii; i++) {
  846. const code = PDFStringTranslateTable[str.charCodeAt(i)];
  847. strBuf.push(code ? String.fromCharCode(code) : str.charAt(i));
  848. }
  849. return strBuf.join("");
  850. }
  851. function escapeString(str) {
  852. return str.replace(/([()\\\n\r])/g, match => {
  853. if (match === "\n") {
  854. return "\\n";
  855. } else if (match === "\r") {
  856. return "\\r";
  857. }
  858. return `\\${match}`;
  859. });
  860. }
  861. function isAscii(str) {
  862. return /^[\x00-\x7F]*$/.test(str);
  863. }
  864. function stringToUTF16BEString(str) {
  865. const buf = ["\xFE\xFF"];
  866. for (let i = 0, ii = str.length; i < ii; i++) {
  867. const char = str.charCodeAt(i);
  868. buf.push(String.fromCharCode(char >> 8 & 0xff), String.fromCharCode(char & 0xff));
  869. }
  870. return buf.join("");
  871. }
  872. function stringToUTF8String(str) {
  873. return decodeURIComponent(escape(str));
  874. }
  875. function utf8StringToString(str) {
  876. return unescape(encodeURIComponent(str));
  877. }
  878. function isArrayBuffer(v) {
  879. return typeof v === "object" && v !== null && v.byteLength !== undefined;
  880. }
  881. function isArrayEqual(arr1, arr2) {
  882. if (arr1.length !== arr2.length) {
  883. return false;
  884. }
  885. for (let i = 0, ii = arr1.length; i < ii; i++) {
  886. if (arr1[i] !== arr2[i]) {
  887. return false;
  888. }
  889. }
  890. return true;
  891. }
  892. function getModificationDate(date = new Date()) {
  893. 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")];
  894. return buffer.join("");
  895. }
  896. function createPromiseCapability() {
  897. const capability = Object.create(null);
  898. let isSettled = false;
  899. Object.defineProperty(capability, "settled", {
  900. get() {
  901. return isSettled;
  902. }
  903. });
  904. capability.promise = new Promise(function (resolve, reject) {
  905. capability.resolve = function (data) {
  906. isSettled = true;
  907. resolve(data);
  908. };
  909. capability.reject = function (reason) {
  910. isSettled = true;
  911. reject(reason);
  912. };
  913. });
  914. return capability;
  915. }