util.js 42 KB

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