util.js 42 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326
  1. /**
  2. * @licstart The following is the entire license notice for the
  3. * Javascript code in this page
  4. *
  5. * Copyright 2017 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.unreachable = exports.warn = exports.utf8StringToString = exports.stringToUTF8String = exports.stringToPDFString = exports.stringToBytes = exports.string32 = exports.shadow = exports.setVerbosityLevel = exports.ReadableStream = exports.removeNullCharacters = exports.readUint32 = exports.readUint16 = exports.readInt8 = exports.log2 = exports.isEvalSupported = exports.isLittleEndian = exports.createValidAbsoluteUrl = exports.isSameOrigin = exports.isSpace = exports.isString = exports.isNum = exports.isEmptyObj = exports.isBool = exports.isArrayBuffer = exports.info = exports.getVerbosityLevel = exports.getLookupTableFactory = exports.deprecated = exports.createObjectURL = exports.createPromiseCapability = exports.createBlob = exports.bytesToString = exports.assert = exports.arraysToBytes = exports.arrayByteLength = exports.FormatError = exports.XRefParseException = exports.Util = exports.UnknownErrorException = exports.UnexpectedResponseException = exports.TextRenderingMode = exports.StreamType = exports.PasswordResponses = exports.PasswordException = exports.PageViewport = exports.NotImplementedException = exports.NativeImageDecoding = exports.MissingPDFException = exports.MissingDataException = exports.MessageHandler = exports.InvalidPDFException = exports.AbortException = exports.CMapCompressionType = exports.ImageKind = exports.FontType = exports.AnnotationType = exports.AnnotationFlag = exports.AnnotationFieldFlag = exports.AnnotationBorderStyleType = exports.UNSUPPORTED_FEATURES = exports.VerbosityLevel = exports.OPS = exports.IDENTITY_MATRIX = exports.FONT_IDENTITY_MATRIX = undefined;
  27. var _typeof = typeof Symbol === "function" && typeof Symbol.iterator === "symbol" ? function (obj) { return typeof obj; } : function (obj) { return obj && typeof Symbol === "function" && obj.constructor === Symbol && obj !== Symbol.prototype ? "symbol" : typeof obj; };
  28. require('./compatibility');
  29. var _streams_polyfill = require('./streams_polyfill');
  30. var FONT_IDENTITY_MATRIX = [0.001, 0, 0, 0.001, 0, 0];
  31. var NativeImageDecoding = {
  32. NONE: 'none',
  33. DECODE: 'decode',
  34. DISPLAY: 'display'
  35. };
  36. var TextRenderingMode = {
  37. FILL: 0,
  38. STROKE: 1,
  39. FILL_STROKE: 2,
  40. INVISIBLE: 3,
  41. FILL_ADD_TO_PATH: 4,
  42. STROKE_ADD_TO_PATH: 5,
  43. FILL_STROKE_ADD_TO_PATH: 6,
  44. ADD_TO_PATH: 7,
  45. FILL_STROKE_MASK: 3,
  46. ADD_TO_PATH_FLAG: 4
  47. };
  48. var ImageKind = {
  49. GRAYSCALE_1BPP: 1,
  50. RGB_24BPP: 2,
  51. RGBA_32BPP: 3
  52. };
  53. var AnnotationType = {
  54. TEXT: 1,
  55. LINK: 2,
  56. FREETEXT: 3,
  57. LINE: 4,
  58. SQUARE: 5,
  59. CIRCLE: 6,
  60. POLYGON: 7,
  61. POLYLINE: 8,
  62. HIGHLIGHT: 9,
  63. UNDERLINE: 10,
  64. SQUIGGLY: 11,
  65. STRIKEOUT: 12,
  66. STAMP: 13,
  67. CARET: 14,
  68. INK: 15,
  69. POPUP: 16,
  70. FILEATTACHMENT: 17,
  71. SOUND: 18,
  72. MOVIE: 19,
  73. WIDGET: 20,
  74. SCREEN: 21,
  75. PRINTERMARK: 22,
  76. TRAPNET: 23,
  77. WATERMARK: 24,
  78. THREED: 25,
  79. REDACT: 26
  80. };
  81. var AnnotationFlag = {
  82. INVISIBLE: 0x01,
  83. HIDDEN: 0x02,
  84. PRINT: 0x04,
  85. NOZOOM: 0x08,
  86. NOROTATE: 0x10,
  87. NOVIEW: 0x20,
  88. READONLY: 0x40,
  89. LOCKED: 0x80,
  90. TOGGLENOVIEW: 0x100,
  91. LOCKEDCONTENTS: 0x200
  92. };
  93. var AnnotationFieldFlag = {
  94. READONLY: 0x0000001,
  95. REQUIRED: 0x0000002,
  96. NOEXPORT: 0x0000004,
  97. MULTILINE: 0x0001000,
  98. PASSWORD: 0x0002000,
  99. NOTOGGLETOOFF: 0x0004000,
  100. RADIO: 0x0008000,
  101. PUSHBUTTON: 0x0010000,
  102. COMBO: 0x0020000,
  103. EDIT: 0x0040000,
  104. SORT: 0x0080000,
  105. FILESELECT: 0x0100000,
  106. MULTISELECT: 0x0200000,
  107. DONOTSPELLCHECK: 0x0400000,
  108. DONOTSCROLL: 0x0800000,
  109. COMB: 0x1000000,
  110. RICHTEXT: 0x2000000,
  111. RADIOSINUNISON: 0x2000000,
  112. COMMITONSELCHANGE: 0x4000000
  113. };
  114. var AnnotationBorderStyleType = {
  115. SOLID: 1,
  116. DASHED: 2,
  117. BEVELED: 3,
  118. INSET: 4,
  119. UNDERLINE: 5
  120. };
  121. var StreamType = {
  122. UNKNOWN: 0,
  123. FLATE: 1,
  124. LZW: 2,
  125. DCT: 3,
  126. JPX: 4,
  127. JBIG: 5,
  128. A85: 6,
  129. AHX: 7,
  130. CCF: 8,
  131. RL: 9
  132. };
  133. var FontType = {
  134. UNKNOWN: 0,
  135. TYPE1: 1,
  136. TYPE1C: 2,
  137. CIDFONTTYPE0: 3,
  138. CIDFONTTYPE0C: 4,
  139. TRUETYPE: 5,
  140. CIDFONTTYPE2: 6,
  141. TYPE3: 7,
  142. OPENTYPE: 8,
  143. TYPE0: 9,
  144. MMTYPE1: 10
  145. };
  146. var VerbosityLevel = {
  147. ERRORS: 0,
  148. WARNINGS: 1,
  149. INFOS: 5
  150. };
  151. var CMapCompressionType = {
  152. NONE: 0,
  153. BINARY: 1,
  154. STREAM: 2
  155. };
  156. var OPS = {
  157. dependency: 1,
  158. setLineWidth: 2,
  159. setLineCap: 3,
  160. setLineJoin: 4,
  161. setMiterLimit: 5,
  162. setDash: 6,
  163. setRenderingIntent: 7,
  164. setFlatness: 8,
  165. setGState: 9,
  166. save: 10,
  167. restore: 11,
  168. transform: 12,
  169. moveTo: 13,
  170. lineTo: 14,
  171. curveTo: 15,
  172. curveTo2: 16,
  173. curveTo3: 17,
  174. closePath: 18,
  175. rectangle: 19,
  176. stroke: 20,
  177. closeStroke: 21,
  178. fill: 22,
  179. eoFill: 23,
  180. fillStroke: 24,
  181. eoFillStroke: 25,
  182. closeFillStroke: 26,
  183. closeEOFillStroke: 27,
  184. endPath: 28,
  185. clip: 29,
  186. eoClip: 30,
  187. beginText: 31,
  188. endText: 32,
  189. setCharSpacing: 33,
  190. setWordSpacing: 34,
  191. setHScale: 35,
  192. setLeading: 36,
  193. setFont: 37,
  194. setTextRenderingMode: 38,
  195. setTextRise: 39,
  196. moveText: 40,
  197. setLeadingMoveText: 41,
  198. setTextMatrix: 42,
  199. nextLine: 43,
  200. showText: 44,
  201. showSpacedText: 45,
  202. nextLineShowText: 46,
  203. nextLineSetSpacingShowText: 47,
  204. setCharWidth: 48,
  205. setCharWidthAndBounds: 49,
  206. setStrokeColorSpace: 50,
  207. setFillColorSpace: 51,
  208. setStrokeColor: 52,
  209. setStrokeColorN: 53,
  210. setFillColor: 54,
  211. setFillColorN: 55,
  212. setStrokeGray: 56,
  213. setFillGray: 57,
  214. setStrokeRGBColor: 58,
  215. setFillRGBColor: 59,
  216. setStrokeCMYKColor: 60,
  217. setFillCMYKColor: 61,
  218. shadingFill: 62,
  219. beginInlineImage: 63,
  220. beginImageData: 64,
  221. endInlineImage: 65,
  222. paintXObject: 66,
  223. markPoint: 67,
  224. markPointProps: 68,
  225. beginMarkedContent: 69,
  226. beginMarkedContentProps: 70,
  227. endMarkedContent: 71,
  228. beginCompat: 72,
  229. endCompat: 73,
  230. paintFormXObjectBegin: 74,
  231. paintFormXObjectEnd: 75,
  232. beginGroup: 76,
  233. endGroup: 77,
  234. beginAnnotations: 78,
  235. endAnnotations: 79,
  236. beginAnnotation: 80,
  237. endAnnotation: 81,
  238. paintJpegXObject: 82,
  239. paintImageMaskXObject: 83,
  240. paintImageMaskXObjectGroup: 84,
  241. paintImageXObject: 85,
  242. paintInlineImageXObject: 86,
  243. paintInlineImageXObjectGroup: 87,
  244. paintImageXObjectRepeat: 88,
  245. paintImageMaskXObjectRepeat: 89,
  246. paintSolidColorImageMask: 90,
  247. constructPath: 91
  248. };
  249. var verbosity = VerbosityLevel.WARNINGS;
  250. function setVerbosityLevel(level) {
  251. if (Number.isInteger(level)) {
  252. verbosity = level;
  253. }
  254. }
  255. function getVerbosityLevel() {
  256. return verbosity;
  257. }
  258. function info(msg) {
  259. if (verbosity >= VerbosityLevel.INFOS) {
  260. console.log('Info: ' + msg);
  261. }
  262. }
  263. function warn(msg) {
  264. if (verbosity >= VerbosityLevel.WARNINGS) {
  265. console.log('Warning: ' + msg);
  266. }
  267. }
  268. function deprecated(details) {
  269. console.log('Deprecated API usage: ' + details);
  270. }
  271. function unreachable(msg) {
  272. throw new Error(msg);
  273. }
  274. function assert(cond, msg) {
  275. if (!cond) {
  276. unreachable(msg);
  277. }
  278. }
  279. var UNSUPPORTED_FEATURES = {
  280. unknown: 'unknown',
  281. forms: 'forms',
  282. javaScript: 'javaScript',
  283. smask: 'smask',
  284. shadingPattern: 'shadingPattern',
  285. font: 'font'
  286. };
  287. function isSameOrigin(baseUrl, otherUrl) {
  288. try {
  289. var base = new URL(baseUrl);
  290. if (!base.origin || base.origin === 'null') {
  291. return false;
  292. }
  293. } catch (e) {
  294. return false;
  295. }
  296. var other = new URL(otherUrl, base);
  297. return base.origin === other.origin;
  298. }
  299. function isValidProtocol(url) {
  300. if (!url) {
  301. return false;
  302. }
  303. switch (url.protocol) {
  304. case 'http:':
  305. case 'https:':
  306. case 'ftp:':
  307. case 'mailto:':
  308. case 'tel:':
  309. return true;
  310. default:
  311. return false;
  312. }
  313. }
  314. function createValidAbsoluteUrl(url, baseUrl) {
  315. if (!url) {
  316. return null;
  317. }
  318. try {
  319. var absoluteUrl = baseUrl ? new URL(url, baseUrl) : new URL(url);
  320. if (isValidProtocol(absoluteUrl)) {
  321. return absoluteUrl;
  322. }
  323. } catch (ex) {}
  324. return null;
  325. }
  326. function shadow(obj, prop, value) {
  327. Object.defineProperty(obj, prop, {
  328. value: value,
  329. enumerable: true,
  330. configurable: true,
  331. writable: false
  332. });
  333. return value;
  334. }
  335. function getLookupTableFactory(initializer) {
  336. var lookup;
  337. return function () {
  338. if (initializer) {
  339. lookup = Object.create(null);
  340. initializer(lookup);
  341. initializer = null;
  342. }
  343. return lookup;
  344. };
  345. }
  346. var PasswordResponses = {
  347. NEED_PASSWORD: 1,
  348. INCORRECT_PASSWORD: 2
  349. };
  350. var PasswordException = function PasswordExceptionClosure() {
  351. function PasswordException(msg, code) {
  352. this.name = 'PasswordException';
  353. this.message = msg;
  354. this.code = code;
  355. }
  356. PasswordException.prototype = new Error();
  357. PasswordException.constructor = PasswordException;
  358. return PasswordException;
  359. }();
  360. var UnknownErrorException = function UnknownErrorExceptionClosure() {
  361. function UnknownErrorException(msg, details) {
  362. this.name = 'UnknownErrorException';
  363. this.message = msg;
  364. this.details = details;
  365. }
  366. UnknownErrorException.prototype = new Error();
  367. UnknownErrorException.constructor = UnknownErrorException;
  368. return UnknownErrorException;
  369. }();
  370. var InvalidPDFException = function InvalidPDFExceptionClosure() {
  371. function InvalidPDFException(msg) {
  372. this.name = 'InvalidPDFException';
  373. this.message = msg;
  374. }
  375. InvalidPDFException.prototype = new Error();
  376. InvalidPDFException.constructor = InvalidPDFException;
  377. return InvalidPDFException;
  378. }();
  379. var MissingPDFException = function MissingPDFExceptionClosure() {
  380. function MissingPDFException(msg) {
  381. this.name = 'MissingPDFException';
  382. this.message = msg;
  383. }
  384. MissingPDFException.prototype = new Error();
  385. MissingPDFException.constructor = MissingPDFException;
  386. return MissingPDFException;
  387. }();
  388. var UnexpectedResponseException = function UnexpectedResponseExceptionClosure() {
  389. function UnexpectedResponseException(msg, status) {
  390. this.name = 'UnexpectedResponseException';
  391. this.message = msg;
  392. this.status = status;
  393. }
  394. UnexpectedResponseException.prototype = new Error();
  395. UnexpectedResponseException.constructor = UnexpectedResponseException;
  396. return UnexpectedResponseException;
  397. }();
  398. var NotImplementedException = function NotImplementedExceptionClosure() {
  399. function NotImplementedException(msg) {
  400. this.message = msg;
  401. }
  402. NotImplementedException.prototype = new Error();
  403. NotImplementedException.prototype.name = 'NotImplementedException';
  404. NotImplementedException.constructor = NotImplementedException;
  405. return NotImplementedException;
  406. }();
  407. var MissingDataException = function MissingDataExceptionClosure() {
  408. function MissingDataException(begin, end) {
  409. this.begin = begin;
  410. this.end = end;
  411. this.message = 'Missing data [' + begin + ', ' + end + ')';
  412. }
  413. MissingDataException.prototype = new Error();
  414. MissingDataException.prototype.name = 'MissingDataException';
  415. MissingDataException.constructor = MissingDataException;
  416. return MissingDataException;
  417. }();
  418. var XRefParseException = function XRefParseExceptionClosure() {
  419. function XRefParseException(msg) {
  420. this.message = msg;
  421. }
  422. XRefParseException.prototype = new Error();
  423. XRefParseException.prototype.name = 'XRefParseException';
  424. XRefParseException.constructor = XRefParseException;
  425. return XRefParseException;
  426. }();
  427. var FormatError = function FormatErrorClosure() {
  428. function FormatError(msg) {
  429. this.message = msg;
  430. }
  431. FormatError.prototype = new Error();
  432. FormatError.prototype.name = 'FormatError';
  433. FormatError.constructor = FormatError;
  434. return FormatError;
  435. }();
  436. var AbortException = function AbortExceptionClosure() {
  437. function AbortException(msg) {
  438. this.name = 'AbortException';
  439. this.message = msg;
  440. }
  441. AbortException.prototype = new Error();
  442. AbortException.constructor = AbortException;
  443. return AbortException;
  444. }();
  445. var NullCharactersRegExp = /\x00/g;
  446. function removeNullCharacters(str) {
  447. if (typeof str !== 'string') {
  448. warn('The argument for removeNullCharacters must be a string.');
  449. return str;
  450. }
  451. return str.replace(NullCharactersRegExp, '');
  452. }
  453. function bytesToString(bytes) {
  454. assert(bytes !== null && (typeof bytes === 'undefined' ? 'undefined' : _typeof(bytes)) === 'object' && bytes.length !== undefined, 'Invalid argument for bytesToString');
  455. var length = bytes.length;
  456. var MAX_ARGUMENT_COUNT = 8192;
  457. if (length < MAX_ARGUMENT_COUNT) {
  458. return String.fromCharCode.apply(null, bytes);
  459. }
  460. var strBuf = [];
  461. for (var i = 0; i < length; i += MAX_ARGUMENT_COUNT) {
  462. var chunkEnd = Math.min(i + MAX_ARGUMENT_COUNT, length);
  463. var chunk = bytes.subarray(i, chunkEnd);
  464. strBuf.push(String.fromCharCode.apply(null, chunk));
  465. }
  466. return strBuf.join('');
  467. }
  468. function stringToBytes(str) {
  469. assert(typeof str === 'string', 'Invalid argument for stringToBytes');
  470. var length = str.length;
  471. var bytes = new Uint8Array(length);
  472. for (var i = 0; i < length; ++i) {
  473. bytes[i] = str.charCodeAt(i) & 0xFF;
  474. }
  475. return bytes;
  476. }
  477. function arrayByteLength(arr) {
  478. if (arr.length !== undefined) {
  479. return arr.length;
  480. }
  481. assert(arr.byteLength !== undefined);
  482. return arr.byteLength;
  483. }
  484. function arraysToBytes(arr) {
  485. if (arr.length === 1 && arr[0] instanceof Uint8Array) {
  486. return arr[0];
  487. }
  488. var resultLength = 0;
  489. var i,
  490. ii = arr.length;
  491. var item, itemLength;
  492. for (i = 0; i < ii; i++) {
  493. item = arr[i];
  494. itemLength = arrayByteLength(item);
  495. resultLength += itemLength;
  496. }
  497. var pos = 0;
  498. var data = new Uint8Array(resultLength);
  499. for (i = 0; i < ii; i++) {
  500. item = arr[i];
  501. if (!(item instanceof Uint8Array)) {
  502. if (typeof item === 'string') {
  503. item = stringToBytes(item);
  504. } else {
  505. item = new Uint8Array(item);
  506. }
  507. }
  508. itemLength = item.byteLength;
  509. data.set(item, pos);
  510. pos += itemLength;
  511. }
  512. return data;
  513. }
  514. function string32(value) {
  515. return String.fromCharCode(value >> 24 & 0xff, value >> 16 & 0xff, value >> 8 & 0xff, value & 0xff);
  516. }
  517. function log2(x) {
  518. if (x <= 0) {
  519. return 0;
  520. }
  521. return Math.ceil(Math.log2(x));
  522. }
  523. function readInt8(data, start) {
  524. return data[start] << 24 >> 24;
  525. }
  526. function readUint16(data, offset) {
  527. return data[offset] << 8 | data[offset + 1];
  528. }
  529. function readUint32(data, offset) {
  530. return (data[offset] << 24 | data[offset + 1] << 16 | data[offset + 2] << 8 | data[offset + 3]) >>> 0;
  531. }
  532. function isLittleEndian() {
  533. var buffer8 = new Uint8Array(4);
  534. buffer8[0] = 1;
  535. var view32 = new Uint32Array(buffer8.buffer, 0, 1);
  536. return view32[0] === 1;
  537. }
  538. function isEvalSupported() {
  539. try {
  540. new Function('');
  541. return true;
  542. } catch (e) {
  543. return false;
  544. }
  545. }
  546. var IDENTITY_MATRIX = [1, 0, 0, 1, 0, 0];
  547. var Util = function UtilClosure() {
  548. function Util() {}
  549. var rgbBuf = ['rgb(', 0, ',', 0, ',', 0, ')'];
  550. Util.makeCssRgb = function Util_makeCssRgb(r, g, b) {
  551. rgbBuf[1] = r;
  552. rgbBuf[3] = g;
  553. rgbBuf[5] = b;
  554. return rgbBuf.join('');
  555. };
  556. Util.transform = function Util_transform(m1, m2) {
  557. 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]];
  558. };
  559. Util.applyTransform = function Util_applyTransform(p, m) {
  560. var xt = p[0] * m[0] + p[1] * m[2] + m[4];
  561. var yt = p[0] * m[1] + p[1] * m[3] + m[5];
  562. return [xt, yt];
  563. };
  564. Util.applyInverseTransform = function Util_applyInverseTransform(p, m) {
  565. var d = m[0] * m[3] - m[1] * m[2];
  566. var xt = (p[0] * m[3] - p[1] * m[2] + m[2] * m[5] - m[4] * m[3]) / d;
  567. var yt = (-p[0] * m[1] + p[1] * m[0] + m[4] * m[1] - m[5] * m[0]) / d;
  568. return [xt, yt];
  569. };
  570. Util.getAxialAlignedBoundingBox = function Util_getAxialAlignedBoundingBox(r, m) {
  571. var p1 = Util.applyTransform(r, m);
  572. var p2 = Util.applyTransform(r.slice(2, 4), m);
  573. var p3 = Util.applyTransform([r[0], r[3]], m);
  574. var p4 = Util.applyTransform([r[2], r[1]], m);
  575. 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])];
  576. };
  577. Util.inverseTransform = function Util_inverseTransform(m) {
  578. var d = m[0] * m[3] - m[1] * m[2];
  579. 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];
  580. };
  581. Util.apply3dTransform = function Util_apply3dTransform(m, v) {
  582. 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]];
  583. };
  584. Util.singularValueDecompose2dScale = function Util_singularValueDecompose2dScale(m) {
  585. var transpose = [m[0], m[2], m[1], m[3]];
  586. var a = m[0] * transpose[0] + m[1] * transpose[2];
  587. var b = m[0] * transpose[1] + m[1] * transpose[3];
  588. var c = m[2] * transpose[0] + m[3] * transpose[2];
  589. var d = m[2] * transpose[1] + m[3] * transpose[3];
  590. var first = (a + d) / 2;
  591. var second = Math.sqrt((a + d) * (a + d) - 4 * (a * d - c * b)) / 2;
  592. var sx = first + second || 1;
  593. var sy = first - second || 1;
  594. return [Math.sqrt(sx), Math.sqrt(sy)];
  595. };
  596. Util.normalizeRect = function Util_normalizeRect(rect) {
  597. var r = rect.slice(0);
  598. if (rect[0] > rect[2]) {
  599. r[0] = rect[2];
  600. r[2] = rect[0];
  601. }
  602. if (rect[1] > rect[3]) {
  603. r[1] = rect[3];
  604. r[3] = rect[1];
  605. }
  606. return r;
  607. };
  608. Util.intersect = function Util_intersect(rect1, rect2) {
  609. function compare(a, b) {
  610. return a - b;
  611. }
  612. var orderedX = [rect1[0], rect1[2], rect2[0], rect2[2]].sort(compare),
  613. orderedY = [rect1[1], rect1[3], rect2[1], rect2[3]].sort(compare),
  614. result = [];
  615. rect1 = Util.normalizeRect(rect1);
  616. rect2 = Util.normalizeRect(rect2);
  617. if (orderedX[0] === rect1[0] && orderedX[1] === rect2[0] || orderedX[0] === rect2[0] && orderedX[1] === rect1[0]) {
  618. result[0] = orderedX[1];
  619. result[2] = orderedX[2];
  620. } else {
  621. return false;
  622. }
  623. if (orderedY[0] === rect1[1] && orderedY[1] === rect2[1] || orderedY[0] === rect2[1] && orderedY[1] === rect1[1]) {
  624. result[1] = orderedY[1];
  625. result[3] = orderedY[2];
  626. } else {
  627. return false;
  628. }
  629. return result;
  630. };
  631. var ROMAN_NUMBER_MAP = ['', 'C', 'CC', 'CCC', 'CD', 'D', 'DC', 'DCC', 'DCCC', 'CM', '', 'X', 'XX', 'XXX', 'XL', 'L', 'LX', 'LXX', 'LXXX', 'XC', '', 'I', 'II', 'III', 'IV', 'V', 'VI', 'VII', 'VIII', 'IX'];
  632. Util.toRoman = function Util_toRoman(number, lowerCase) {
  633. assert(Number.isInteger(number) && number > 0, 'The number should be a positive integer.');
  634. var pos,
  635. romanBuf = [];
  636. while (number >= 1000) {
  637. number -= 1000;
  638. romanBuf.push('M');
  639. }
  640. pos = number / 100 | 0;
  641. number %= 100;
  642. romanBuf.push(ROMAN_NUMBER_MAP[pos]);
  643. pos = number / 10 | 0;
  644. number %= 10;
  645. romanBuf.push(ROMAN_NUMBER_MAP[10 + pos]);
  646. romanBuf.push(ROMAN_NUMBER_MAP[20 + number]);
  647. var romanStr = romanBuf.join('');
  648. return lowerCase ? romanStr.toLowerCase() : romanStr;
  649. };
  650. Util.appendToArray = function Util_appendToArray(arr1, arr2) {
  651. Array.prototype.push.apply(arr1, arr2);
  652. };
  653. Util.prependToArray = function Util_prependToArray(arr1, arr2) {
  654. Array.prototype.unshift.apply(arr1, arr2);
  655. };
  656. Util.extendObj = function extendObj(obj1, obj2) {
  657. for (var key in obj2) {
  658. obj1[key] = obj2[key];
  659. }
  660. };
  661. Util.getInheritableProperty = function Util_getInheritableProperty(dict, name, getArray) {
  662. while (dict && !dict.has(name)) {
  663. dict = dict.get('Parent');
  664. }
  665. if (!dict) {
  666. return null;
  667. }
  668. return getArray ? dict.getArray(name) : dict.get(name);
  669. };
  670. Util.inherit = function Util_inherit(sub, base, prototype) {
  671. sub.prototype = Object.create(base.prototype);
  672. sub.prototype.constructor = sub;
  673. for (var prop in prototype) {
  674. sub.prototype[prop] = prototype[prop];
  675. }
  676. };
  677. Util.loadScript = function Util_loadScript(src, callback) {
  678. var script = document.createElement('script');
  679. var loaded = false;
  680. script.setAttribute('src', src);
  681. if (callback) {
  682. script.onload = function () {
  683. if (!loaded) {
  684. callback();
  685. }
  686. loaded = true;
  687. };
  688. }
  689. document.getElementsByTagName('head')[0].appendChild(script);
  690. };
  691. return Util;
  692. }();
  693. var PageViewport = function PageViewportClosure() {
  694. function PageViewport(viewBox, scale, rotation, offsetX, offsetY, dontFlip) {
  695. this.viewBox = viewBox;
  696. this.scale = scale;
  697. this.rotation = rotation;
  698. this.offsetX = offsetX;
  699. this.offsetY = offsetY;
  700. var centerX = (viewBox[2] + viewBox[0]) / 2;
  701. var centerY = (viewBox[3] + viewBox[1]) / 2;
  702. var rotateA, rotateB, rotateC, rotateD;
  703. rotation = rotation % 360;
  704. rotation = rotation < 0 ? rotation + 360 : rotation;
  705. switch (rotation) {
  706. case 180:
  707. rotateA = -1;
  708. rotateB = 0;
  709. rotateC = 0;
  710. rotateD = 1;
  711. break;
  712. case 90:
  713. rotateA = 0;
  714. rotateB = 1;
  715. rotateC = 1;
  716. rotateD = 0;
  717. break;
  718. case 270:
  719. rotateA = 0;
  720. rotateB = -1;
  721. rotateC = -1;
  722. rotateD = 0;
  723. break;
  724. default:
  725. rotateA = 1;
  726. rotateB = 0;
  727. rotateC = 0;
  728. rotateD = -1;
  729. break;
  730. }
  731. if (dontFlip) {
  732. rotateC = -rotateC;
  733. rotateD = -rotateD;
  734. }
  735. var offsetCanvasX, offsetCanvasY;
  736. var width, height;
  737. if (rotateA === 0) {
  738. offsetCanvasX = Math.abs(centerY - viewBox[1]) * scale + offsetX;
  739. offsetCanvasY = Math.abs(centerX - viewBox[0]) * scale + offsetY;
  740. width = Math.abs(viewBox[3] - viewBox[1]) * scale;
  741. height = Math.abs(viewBox[2] - viewBox[0]) * scale;
  742. } else {
  743. offsetCanvasX = Math.abs(centerX - viewBox[0]) * scale + offsetX;
  744. offsetCanvasY = Math.abs(centerY - viewBox[1]) * scale + offsetY;
  745. width = Math.abs(viewBox[2] - viewBox[0]) * scale;
  746. height = Math.abs(viewBox[3] - viewBox[1]) * scale;
  747. }
  748. this.transform = [rotateA * scale, rotateB * scale, rotateC * scale, rotateD * scale, offsetCanvasX - rotateA * scale * centerX - rotateC * scale * centerY, offsetCanvasY - rotateB * scale * centerX - rotateD * scale * centerY];
  749. this.width = width;
  750. this.height = height;
  751. this.fontScale = scale;
  752. }
  753. PageViewport.prototype = {
  754. clone: function PageViewPort_clone(args) {
  755. args = args || {};
  756. var scale = 'scale' in args ? args.scale : this.scale;
  757. var rotation = 'rotation' in args ? args.rotation : this.rotation;
  758. return new PageViewport(this.viewBox.slice(), scale, rotation, this.offsetX, this.offsetY, args.dontFlip);
  759. },
  760. convertToViewportPoint: function PageViewport_convertToViewportPoint(x, y) {
  761. return Util.applyTransform([x, y], this.transform);
  762. },
  763. convertToViewportRectangle: function PageViewport_convertToViewportRectangle(rect) {
  764. var tl = Util.applyTransform([rect[0], rect[1]], this.transform);
  765. var br = Util.applyTransform([rect[2], rect[3]], this.transform);
  766. return [tl[0], tl[1], br[0], br[1]];
  767. },
  768. convertToPdfPoint: function PageViewport_convertToPdfPoint(x, y) {
  769. return Util.applyInverseTransform([x, y], this.transform);
  770. }
  771. };
  772. return PageViewport;
  773. }();
  774. var 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];
  775. function stringToPDFString(str) {
  776. var i,
  777. n = str.length,
  778. strBuf = [];
  779. if (str[0] === '\xFE' && str[1] === '\xFF') {
  780. for (i = 2; i < n; i += 2) {
  781. strBuf.push(String.fromCharCode(str.charCodeAt(i) << 8 | str.charCodeAt(i + 1)));
  782. }
  783. } else {
  784. for (i = 0; i < n; ++i) {
  785. var code = PDFStringTranslateTable[str.charCodeAt(i)];
  786. strBuf.push(code ? String.fromCharCode(code) : str.charAt(i));
  787. }
  788. }
  789. return strBuf.join('');
  790. }
  791. function stringToUTF8String(str) {
  792. return decodeURIComponent(escape(str));
  793. }
  794. function utf8StringToString(str) {
  795. return unescape(encodeURIComponent(str));
  796. }
  797. function isEmptyObj(obj) {
  798. for (var key in obj) {
  799. return false;
  800. }
  801. return true;
  802. }
  803. function isBool(v) {
  804. return typeof v === 'boolean';
  805. }
  806. function isNum(v) {
  807. return typeof v === 'number';
  808. }
  809. function isString(v) {
  810. return typeof v === 'string';
  811. }
  812. function isArrayBuffer(v) {
  813. return (typeof v === 'undefined' ? 'undefined' : _typeof(v)) === 'object' && v !== null && v.byteLength !== undefined;
  814. }
  815. function isSpace(ch) {
  816. return ch === 0x20 || ch === 0x09 || ch === 0x0D || ch === 0x0A;
  817. }
  818. function createPromiseCapability() {
  819. var capability = {};
  820. capability.promise = new Promise(function (resolve, reject) {
  821. capability.resolve = resolve;
  822. capability.reject = reject;
  823. });
  824. return capability;
  825. }
  826. var createBlob = function createBlob(data, contentType) {
  827. if (typeof Blob !== 'undefined') {
  828. return new Blob([data], { type: contentType });
  829. }
  830. throw new Error('The "Blob" constructor is not supported.');
  831. };
  832. var createObjectURL = function createObjectURLClosure() {
  833. var digits = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/=';
  834. return function createObjectURL(data, contentType) {
  835. var forceDataSchema = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : false;
  836. if (!forceDataSchema && URL.createObjectURL) {
  837. var blob = createBlob(data, contentType);
  838. return URL.createObjectURL(blob);
  839. }
  840. var buffer = 'data:' + contentType + ';base64,';
  841. for (var i = 0, ii = data.length; i < ii; i += 3) {
  842. var b1 = data[i] & 0xFF;
  843. var b2 = data[i + 1] & 0xFF;
  844. var b3 = data[i + 2] & 0xFF;
  845. var d1 = b1 >> 2,
  846. d2 = (b1 & 3) << 4 | b2 >> 4;
  847. var d3 = i + 1 < ii ? (b2 & 0xF) << 2 | b3 >> 6 : 64;
  848. var d4 = i + 2 < ii ? b3 & 0x3F : 64;
  849. buffer += digits[d1] + digits[d2] + digits[d3] + digits[d4];
  850. }
  851. return buffer;
  852. };
  853. }();
  854. function resolveCall(fn, args) {
  855. var thisArg = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : null;
  856. if (!fn) {
  857. return Promise.resolve(undefined);
  858. }
  859. return new Promise(function (resolve, reject) {
  860. resolve(fn.apply(thisArg, args));
  861. });
  862. }
  863. function wrapReason(reason) {
  864. if ((typeof reason === 'undefined' ? 'undefined' : _typeof(reason)) !== 'object') {
  865. return reason;
  866. }
  867. switch (reason.name) {
  868. case 'AbortException':
  869. return new AbortException(reason.message);
  870. case 'MissingPDFException':
  871. return new MissingPDFException(reason.message);
  872. case 'UnexpectedResponseException':
  873. return new UnexpectedResponseException(reason.message, reason.status);
  874. default:
  875. return new UnknownErrorException(reason.message, reason.details);
  876. }
  877. }
  878. function makeReasonSerializable(reason) {
  879. if (!(reason instanceof Error) || reason instanceof AbortException || reason instanceof MissingPDFException || reason instanceof UnexpectedResponseException || reason instanceof UnknownErrorException) {
  880. return reason;
  881. }
  882. return new UnknownErrorException(reason.message, reason.toString());
  883. }
  884. function resolveOrReject(capability, success, reason) {
  885. if (success) {
  886. capability.resolve();
  887. } else {
  888. capability.reject(reason);
  889. }
  890. }
  891. function finalize(promise) {
  892. return Promise.resolve(promise).catch(function () {});
  893. }
  894. function MessageHandler(sourceName, targetName, comObj) {
  895. var _this = this;
  896. this.sourceName = sourceName;
  897. this.targetName = targetName;
  898. this.comObj = comObj;
  899. this.callbackId = 1;
  900. this.streamId = 1;
  901. this.postMessageTransfers = true;
  902. this.streamSinks = Object.create(null);
  903. this.streamControllers = Object.create(null);
  904. var callbacksCapabilities = this.callbacksCapabilities = Object.create(null);
  905. var ah = this.actionHandler = Object.create(null);
  906. this._onComObjOnMessage = function (event) {
  907. var data = event.data;
  908. if (data.targetName !== _this.sourceName) {
  909. return;
  910. }
  911. if (data.stream) {
  912. _this._processStreamMessage(data);
  913. } else if (data.isReply) {
  914. var callbackId = data.callbackId;
  915. if (data.callbackId in callbacksCapabilities) {
  916. var callback = callbacksCapabilities[callbackId];
  917. delete callbacksCapabilities[callbackId];
  918. if ('error' in data) {
  919. callback.reject(wrapReason(data.error));
  920. } else {
  921. callback.resolve(data.data);
  922. }
  923. } else {
  924. throw new Error('Cannot resolve callback ' + callbackId);
  925. }
  926. } else if (data.action in ah) {
  927. var action = ah[data.action];
  928. if (data.callbackId) {
  929. var _sourceName = _this.sourceName;
  930. var _targetName = data.sourceName;
  931. Promise.resolve().then(function () {
  932. return action[0].call(action[1], data.data);
  933. }).then(function (result) {
  934. comObj.postMessage({
  935. sourceName: _sourceName,
  936. targetName: _targetName,
  937. isReply: true,
  938. callbackId: data.callbackId,
  939. data: result
  940. });
  941. }, function (reason) {
  942. comObj.postMessage({
  943. sourceName: _sourceName,
  944. targetName: _targetName,
  945. isReply: true,
  946. callbackId: data.callbackId,
  947. error: makeReasonSerializable(reason)
  948. });
  949. });
  950. } else if (data.streamId) {
  951. _this._createStreamSink(data);
  952. } else {
  953. action[0].call(action[1], data.data);
  954. }
  955. } else {
  956. throw new Error('Unknown action from worker: ' + data.action);
  957. }
  958. };
  959. comObj.addEventListener('message', this._onComObjOnMessage);
  960. }
  961. MessageHandler.prototype = {
  962. on: function on(actionName, handler, scope) {
  963. var ah = this.actionHandler;
  964. if (ah[actionName]) {
  965. throw new Error('There is already an actionName called "' + actionName + '"');
  966. }
  967. ah[actionName] = [handler, scope];
  968. },
  969. send: function send(actionName, data, transfers) {
  970. var message = {
  971. sourceName: this.sourceName,
  972. targetName: this.targetName,
  973. action: actionName,
  974. data: data
  975. };
  976. this.postMessage(message, transfers);
  977. },
  978. sendWithPromise: function sendWithPromise(actionName, data, transfers) {
  979. var callbackId = this.callbackId++;
  980. var message = {
  981. sourceName: this.sourceName,
  982. targetName: this.targetName,
  983. action: actionName,
  984. data: data,
  985. callbackId: callbackId
  986. };
  987. var capability = createPromiseCapability();
  988. this.callbacksCapabilities[callbackId] = capability;
  989. try {
  990. this.postMessage(message, transfers);
  991. } catch (e) {
  992. capability.reject(e);
  993. }
  994. return capability.promise;
  995. },
  996. sendWithStream: function sendWithStream(actionName, data, queueingStrategy, transfers) {
  997. var _this2 = this;
  998. var streamId = this.streamId++;
  999. var sourceName = this.sourceName;
  1000. var targetName = this.targetName;
  1001. return new _streams_polyfill.ReadableStream({
  1002. start: function start(controller) {
  1003. var startCapability = createPromiseCapability();
  1004. _this2.streamControllers[streamId] = {
  1005. controller: controller,
  1006. startCall: startCapability,
  1007. isClosed: false
  1008. };
  1009. _this2.postMessage({
  1010. sourceName: sourceName,
  1011. targetName: targetName,
  1012. action: actionName,
  1013. streamId: streamId,
  1014. data: data,
  1015. desiredSize: controller.desiredSize
  1016. });
  1017. return startCapability.promise;
  1018. },
  1019. pull: function pull(controller) {
  1020. var pullCapability = createPromiseCapability();
  1021. _this2.streamControllers[streamId].pullCall = pullCapability;
  1022. _this2.postMessage({
  1023. sourceName: sourceName,
  1024. targetName: targetName,
  1025. stream: 'pull',
  1026. streamId: streamId,
  1027. desiredSize: controller.desiredSize
  1028. });
  1029. return pullCapability.promise;
  1030. },
  1031. cancel: function cancel(reason) {
  1032. var cancelCapability = createPromiseCapability();
  1033. _this2.streamControllers[streamId].cancelCall = cancelCapability;
  1034. _this2.streamControllers[streamId].isClosed = true;
  1035. _this2.postMessage({
  1036. sourceName: sourceName,
  1037. targetName: targetName,
  1038. stream: 'cancel',
  1039. reason: reason,
  1040. streamId: streamId
  1041. });
  1042. return cancelCapability.promise;
  1043. }
  1044. }, queueingStrategy);
  1045. },
  1046. _createStreamSink: function _createStreamSink(data) {
  1047. var _this3 = this;
  1048. var self = this;
  1049. var action = this.actionHandler[data.action];
  1050. var streamId = data.streamId;
  1051. var desiredSize = data.desiredSize;
  1052. var sourceName = this.sourceName;
  1053. var targetName = data.sourceName;
  1054. var capability = createPromiseCapability();
  1055. var sendStreamRequest = function sendStreamRequest(_ref) {
  1056. var stream = _ref.stream,
  1057. chunk = _ref.chunk,
  1058. transfers = _ref.transfers,
  1059. success = _ref.success,
  1060. reason = _ref.reason;
  1061. _this3.postMessage({
  1062. sourceName: sourceName,
  1063. targetName: targetName,
  1064. stream: stream,
  1065. streamId: streamId,
  1066. chunk: chunk,
  1067. success: success,
  1068. reason: reason
  1069. }, transfers);
  1070. };
  1071. var streamSink = {
  1072. enqueue: function enqueue(chunk) {
  1073. var size = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : 1;
  1074. var transfers = arguments[2];
  1075. if (this.isCancelled) {
  1076. return;
  1077. }
  1078. var lastDesiredSize = this.desiredSize;
  1079. this.desiredSize -= size;
  1080. if (lastDesiredSize > 0 && this.desiredSize <= 0) {
  1081. this.sinkCapability = createPromiseCapability();
  1082. this.ready = this.sinkCapability.promise;
  1083. }
  1084. sendStreamRequest({
  1085. stream: 'enqueue',
  1086. chunk: chunk,
  1087. transfers: transfers
  1088. });
  1089. },
  1090. close: function close() {
  1091. if (this.isCancelled) {
  1092. return;
  1093. }
  1094. this.isCancelled = true;
  1095. sendStreamRequest({ stream: 'close' });
  1096. delete self.streamSinks[streamId];
  1097. },
  1098. error: function error(reason) {
  1099. if (this.isCancelled) {
  1100. return;
  1101. }
  1102. this.isCancelled = true;
  1103. sendStreamRequest({
  1104. stream: 'error',
  1105. reason: reason
  1106. });
  1107. },
  1108. sinkCapability: capability,
  1109. onPull: null,
  1110. onCancel: null,
  1111. isCancelled: false,
  1112. desiredSize: desiredSize,
  1113. ready: null
  1114. };
  1115. streamSink.sinkCapability.resolve();
  1116. streamSink.ready = streamSink.sinkCapability.promise;
  1117. this.streamSinks[streamId] = streamSink;
  1118. resolveCall(action[0], [data.data, streamSink], action[1]).then(function () {
  1119. sendStreamRequest({
  1120. stream: 'start_complete',
  1121. success: true
  1122. });
  1123. }, function (reason) {
  1124. sendStreamRequest({
  1125. stream: 'start_complete',
  1126. success: false,
  1127. reason: reason
  1128. });
  1129. });
  1130. },
  1131. _processStreamMessage: function _processStreamMessage(data) {
  1132. var _this4 = this;
  1133. var sourceName = this.sourceName;
  1134. var targetName = data.sourceName;
  1135. var streamId = data.streamId;
  1136. var sendStreamResponse = function sendStreamResponse(_ref2) {
  1137. var stream = _ref2.stream,
  1138. success = _ref2.success,
  1139. reason = _ref2.reason;
  1140. _this4.comObj.postMessage({
  1141. sourceName: sourceName,
  1142. targetName: targetName,
  1143. stream: stream,
  1144. success: success,
  1145. streamId: streamId,
  1146. reason: reason
  1147. });
  1148. };
  1149. var deleteStreamController = function deleteStreamController() {
  1150. Promise.all([_this4.streamControllers[data.streamId].startCall, _this4.streamControllers[data.streamId].pullCall, _this4.streamControllers[data.streamId].cancelCall].map(function (capability) {
  1151. return capability && finalize(capability.promise);
  1152. })).then(function () {
  1153. delete _this4.streamControllers[data.streamId];
  1154. });
  1155. };
  1156. switch (data.stream) {
  1157. case 'start_complete':
  1158. resolveOrReject(this.streamControllers[data.streamId].startCall, data.success, wrapReason(data.reason));
  1159. break;
  1160. case 'pull_complete':
  1161. resolveOrReject(this.streamControllers[data.streamId].pullCall, data.success, wrapReason(data.reason));
  1162. break;
  1163. case 'pull':
  1164. if (!this.streamSinks[data.streamId]) {
  1165. sendStreamResponse({
  1166. stream: 'pull_complete',
  1167. success: true
  1168. });
  1169. break;
  1170. }
  1171. if (this.streamSinks[data.streamId].desiredSize <= 0 && data.desiredSize > 0) {
  1172. this.streamSinks[data.streamId].sinkCapability.resolve();
  1173. }
  1174. this.streamSinks[data.streamId].desiredSize = data.desiredSize;
  1175. resolveCall(this.streamSinks[data.streamId].onPull).then(function () {
  1176. sendStreamResponse({
  1177. stream: 'pull_complete',
  1178. success: true
  1179. });
  1180. }, function (reason) {
  1181. sendStreamResponse({
  1182. stream: 'pull_complete',
  1183. success: false,
  1184. reason: reason
  1185. });
  1186. });
  1187. break;
  1188. case 'enqueue':
  1189. assert(this.streamControllers[data.streamId], 'enqueue should have stream controller');
  1190. if (!this.streamControllers[data.streamId].isClosed) {
  1191. this.streamControllers[data.streamId].controller.enqueue(data.chunk);
  1192. }
  1193. break;
  1194. case 'close':
  1195. assert(this.streamControllers[data.streamId], 'close should have stream controller');
  1196. if (this.streamControllers[data.streamId].isClosed) {
  1197. break;
  1198. }
  1199. this.streamControllers[data.streamId].isClosed = true;
  1200. this.streamControllers[data.streamId].controller.close();
  1201. deleteStreamController();
  1202. break;
  1203. case 'error':
  1204. assert(this.streamControllers[data.streamId], 'error should have stream controller');
  1205. this.streamControllers[data.streamId].controller.error(wrapReason(data.reason));
  1206. deleteStreamController();
  1207. break;
  1208. case 'cancel_complete':
  1209. resolveOrReject(this.streamControllers[data.streamId].cancelCall, data.success, wrapReason(data.reason));
  1210. deleteStreamController();
  1211. break;
  1212. case 'cancel':
  1213. if (!this.streamSinks[data.streamId]) {
  1214. break;
  1215. }
  1216. resolveCall(this.streamSinks[data.streamId].onCancel, [wrapReason(data.reason)]).then(function () {
  1217. sendStreamResponse({
  1218. stream: 'cancel_complete',
  1219. success: true
  1220. });
  1221. }, function (reason) {
  1222. sendStreamResponse({
  1223. stream: 'cancel_complete',
  1224. success: false,
  1225. reason: reason
  1226. });
  1227. });
  1228. this.streamSinks[data.streamId].sinkCapability.reject(wrapReason(data.reason));
  1229. this.streamSinks[data.streamId].isCancelled = true;
  1230. delete this.streamSinks[data.streamId];
  1231. break;
  1232. default:
  1233. throw new Error('Unexpected stream case');
  1234. }
  1235. },
  1236. postMessage: function postMessage(message, transfers) {
  1237. if (transfers && this.postMessageTransfers) {
  1238. this.comObj.postMessage(message, transfers);
  1239. } else {
  1240. this.comObj.postMessage(message);
  1241. }
  1242. },
  1243. destroy: function destroy() {
  1244. this.comObj.removeEventListener('message', this._onComObjOnMessage);
  1245. }
  1246. };
  1247. exports.FONT_IDENTITY_MATRIX = FONT_IDENTITY_MATRIX;
  1248. exports.IDENTITY_MATRIX = IDENTITY_MATRIX;
  1249. exports.OPS = OPS;
  1250. exports.VerbosityLevel = VerbosityLevel;
  1251. exports.UNSUPPORTED_FEATURES = UNSUPPORTED_FEATURES;
  1252. exports.AnnotationBorderStyleType = AnnotationBorderStyleType;
  1253. exports.AnnotationFieldFlag = AnnotationFieldFlag;
  1254. exports.AnnotationFlag = AnnotationFlag;
  1255. exports.AnnotationType = AnnotationType;
  1256. exports.FontType = FontType;
  1257. exports.ImageKind = ImageKind;
  1258. exports.CMapCompressionType = CMapCompressionType;
  1259. exports.AbortException = AbortException;
  1260. exports.InvalidPDFException = InvalidPDFException;
  1261. exports.MessageHandler = MessageHandler;
  1262. exports.MissingDataException = MissingDataException;
  1263. exports.MissingPDFException = MissingPDFException;
  1264. exports.NativeImageDecoding = NativeImageDecoding;
  1265. exports.NotImplementedException = NotImplementedException;
  1266. exports.PageViewport = PageViewport;
  1267. exports.PasswordException = PasswordException;
  1268. exports.PasswordResponses = PasswordResponses;
  1269. exports.StreamType = StreamType;
  1270. exports.TextRenderingMode = TextRenderingMode;
  1271. exports.UnexpectedResponseException = UnexpectedResponseException;
  1272. exports.UnknownErrorException = UnknownErrorException;
  1273. exports.Util = Util;
  1274. exports.XRefParseException = XRefParseException;
  1275. exports.FormatError = FormatError;
  1276. exports.arrayByteLength = arrayByteLength;
  1277. exports.arraysToBytes = arraysToBytes;
  1278. exports.assert = assert;
  1279. exports.bytesToString = bytesToString;
  1280. exports.createBlob = createBlob;
  1281. exports.createPromiseCapability = createPromiseCapability;
  1282. exports.createObjectURL = createObjectURL;
  1283. exports.deprecated = deprecated;
  1284. exports.getLookupTableFactory = getLookupTableFactory;
  1285. exports.getVerbosityLevel = getVerbosityLevel;
  1286. exports.info = info;
  1287. exports.isArrayBuffer = isArrayBuffer;
  1288. exports.isBool = isBool;
  1289. exports.isEmptyObj = isEmptyObj;
  1290. exports.isNum = isNum;
  1291. exports.isString = isString;
  1292. exports.isSpace = isSpace;
  1293. exports.isSameOrigin = isSameOrigin;
  1294. exports.createValidAbsoluteUrl = createValidAbsoluteUrl;
  1295. exports.isLittleEndian = isLittleEndian;
  1296. exports.isEvalSupported = isEvalSupported;
  1297. exports.log2 = log2;
  1298. exports.readInt8 = readInt8;
  1299. exports.readUint16 = readUint16;
  1300. exports.readUint32 = readUint32;
  1301. exports.removeNullCharacters = removeNullCharacters;
  1302. exports.ReadableStream = _streams_polyfill.ReadableStream;
  1303. exports.setVerbosityLevel = setVerbosityLevel;
  1304. exports.shadow = shadow;
  1305. exports.string32 = string32;
  1306. exports.stringToBytes = stringToBytes;
  1307. exports.stringToPDFString = stringToPDFString;
  1308. exports.stringToUTF8String = stringToUTF8String;
  1309. exports.utf8StringToString = utf8StringToString;
  1310. exports.warn = warn;
  1311. exports.unreachable = unreachable;