util.js 43 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348
  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.getInheritableProperty = 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. function getInheritableProperty(_ref) {
  547. var dict = _ref.dict,
  548. key = _ref.key,
  549. _ref$getArray = _ref.getArray,
  550. getArray = _ref$getArray === undefined ? false : _ref$getArray,
  551. _ref$stopWhenFound = _ref.stopWhenFound,
  552. stopWhenFound = _ref$stopWhenFound === undefined ? true : _ref$stopWhenFound;
  553. var LOOP_LIMIT = 100;
  554. var loopCount = 0;
  555. var values = void 0;
  556. while (dict) {
  557. var value = getArray ? dict.getArray(key) : dict.get(key);
  558. if (value !== undefined) {
  559. if (stopWhenFound) {
  560. return value;
  561. }
  562. if (!values) {
  563. values = [];
  564. }
  565. values.push(value);
  566. }
  567. if (++loopCount > LOOP_LIMIT) {
  568. warn('getInheritableProperty: maximum loop count exceeded for "' + key + '"');
  569. break;
  570. }
  571. dict = dict.get('Parent');
  572. }
  573. return values;
  574. }
  575. var IDENTITY_MATRIX = [1, 0, 0, 1, 0, 0];
  576. var Util = function UtilClosure() {
  577. function Util() {}
  578. var rgbBuf = ['rgb(', 0, ',', 0, ',', 0, ')'];
  579. Util.makeCssRgb = function Util_makeCssRgb(r, g, b) {
  580. rgbBuf[1] = r;
  581. rgbBuf[3] = g;
  582. rgbBuf[5] = b;
  583. return rgbBuf.join('');
  584. };
  585. Util.transform = function Util_transform(m1, m2) {
  586. 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]];
  587. };
  588. Util.applyTransform = function Util_applyTransform(p, m) {
  589. var xt = p[0] * m[0] + p[1] * m[2] + m[4];
  590. var yt = p[0] * m[1] + p[1] * m[3] + m[5];
  591. return [xt, yt];
  592. };
  593. Util.applyInverseTransform = function Util_applyInverseTransform(p, m) {
  594. var d = m[0] * m[3] - m[1] * m[2];
  595. var xt = (p[0] * m[3] - p[1] * m[2] + m[2] * m[5] - m[4] * m[3]) / d;
  596. var yt = (-p[0] * m[1] + p[1] * m[0] + m[4] * m[1] - m[5] * m[0]) / d;
  597. return [xt, yt];
  598. };
  599. Util.getAxialAlignedBoundingBox = function Util_getAxialAlignedBoundingBox(r, m) {
  600. var p1 = Util.applyTransform(r, m);
  601. var p2 = Util.applyTransform(r.slice(2, 4), m);
  602. var p3 = Util.applyTransform([r[0], r[3]], m);
  603. var p4 = Util.applyTransform([r[2], r[1]], m);
  604. 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])];
  605. };
  606. Util.inverseTransform = function Util_inverseTransform(m) {
  607. var d = m[0] * m[3] - m[1] * m[2];
  608. 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];
  609. };
  610. Util.apply3dTransform = function Util_apply3dTransform(m, v) {
  611. 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]];
  612. };
  613. Util.singularValueDecompose2dScale = function Util_singularValueDecompose2dScale(m) {
  614. var transpose = [m[0], m[2], m[1], m[3]];
  615. var a = m[0] * transpose[0] + m[1] * transpose[2];
  616. var b = m[0] * transpose[1] + m[1] * transpose[3];
  617. var c = m[2] * transpose[0] + m[3] * transpose[2];
  618. var d = m[2] * transpose[1] + m[3] * transpose[3];
  619. var first = (a + d) / 2;
  620. var second = Math.sqrt((a + d) * (a + d) - 4 * (a * d - c * b)) / 2;
  621. var sx = first + second || 1;
  622. var sy = first - second || 1;
  623. return [Math.sqrt(sx), Math.sqrt(sy)];
  624. };
  625. Util.normalizeRect = function Util_normalizeRect(rect) {
  626. var r = rect.slice(0);
  627. if (rect[0] > rect[2]) {
  628. r[0] = rect[2];
  629. r[2] = rect[0];
  630. }
  631. if (rect[1] > rect[3]) {
  632. r[1] = rect[3];
  633. r[3] = rect[1];
  634. }
  635. return r;
  636. };
  637. Util.intersect = function Util_intersect(rect1, rect2) {
  638. function compare(a, b) {
  639. return a - b;
  640. }
  641. var orderedX = [rect1[0], rect1[2], rect2[0], rect2[2]].sort(compare),
  642. orderedY = [rect1[1], rect1[3], rect2[1], rect2[3]].sort(compare),
  643. result = [];
  644. rect1 = Util.normalizeRect(rect1);
  645. rect2 = Util.normalizeRect(rect2);
  646. if (orderedX[0] === rect1[0] && orderedX[1] === rect2[0] || orderedX[0] === rect2[0] && orderedX[1] === rect1[0]) {
  647. result[0] = orderedX[1];
  648. result[2] = orderedX[2];
  649. } else {
  650. return false;
  651. }
  652. if (orderedY[0] === rect1[1] && orderedY[1] === rect2[1] || orderedY[0] === rect2[1] && orderedY[1] === rect1[1]) {
  653. result[1] = orderedY[1];
  654. result[3] = orderedY[2];
  655. } else {
  656. return false;
  657. }
  658. return result;
  659. };
  660. 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'];
  661. Util.toRoman = function Util_toRoman(number, lowerCase) {
  662. assert(Number.isInteger(number) && number > 0, 'The number should be a positive integer.');
  663. var pos,
  664. romanBuf = [];
  665. while (number >= 1000) {
  666. number -= 1000;
  667. romanBuf.push('M');
  668. }
  669. pos = number / 100 | 0;
  670. number %= 100;
  671. romanBuf.push(ROMAN_NUMBER_MAP[pos]);
  672. pos = number / 10 | 0;
  673. number %= 10;
  674. romanBuf.push(ROMAN_NUMBER_MAP[10 + pos]);
  675. romanBuf.push(ROMAN_NUMBER_MAP[20 + number]);
  676. var romanStr = romanBuf.join('');
  677. return lowerCase ? romanStr.toLowerCase() : romanStr;
  678. };
  679. Util.appendToArray = function Util_appendToArray(arr1, arr2) {
  680. Array.prototype.push.apply(arr1, arr2);
  681. };
  682. Util.prependToArray = function Util_prependToArray(arr1, arr2) {
  683. Array.prototype.unshift.apply(arr1, arr2);
  684. };
  685. Util.extendObj = function extendObj(obj1, obj2) {
  686. for (var key in obj2) {
  687. obj1[key] = obj2[key];
  688. }
  689. };
  690. Util.inherit = function Util_inherit(sub, base, prototype) {
  691. sub.prototype = Object.create(base.prototype);
  692. sub.prototype.constructor = sub;
  693. for (var prop in prototype) {
  694. sub.prototype[prop] = prototype[prop];
  695. }
  696. };
  697. Util.loadScript = function Util_loadScript(src, callback) {
  698. var script = document.createElement('script');
  699. var loaded = false;
  700. script.setAttribute('src', src);
  701. if (callback) {
  702. script.onload = function () {
  703. if (!loaded) {
  704. callback();
  705. }
  706. loaded = true;
  707. };
  708. }
  709. document.getElementsByTagName('head')[0].appendChild(script);
  710. };
  711. return Util;
  712. }();
  713. var PageViewport = function PageViewportClosure() {
  714. function PageViewport(viewBox, scale, rotation, offsetX, offsetY, dontFlip) {
  715. this.viewBox = viewBox;
  716. this.scale = scale;
  717. this.rotation = rotation;
  718. this.offsetX = offsetX;
  719. this.offsetY = offsetY;
  720. var centerX = (viewBox[2] + viewBox[0]) / 2;
  721. var centerY = (viewBox[3] + viewBox[1]) / 2;
  722. var rotateA, rotateB, rotateC, rotateD;
  723. rotation = rotation % 360;
  724. rotation = rotation < 0 ? rotation + 360 : rotation;
  725. switch (rotation) {
  726. case 180:
  727. rotateA = -1;
  728. rotateB = 0;
  729. rotateC = 0;
  730. rotateD = 1;
  731. break;
  732. case 90:
  733. rotateA = 0;
  734. rotateB = 1;
  735. rotateC = 1;
  736. rotateD = 0;
  737. break;
  738. case 270:
  739. rotateA = 0;
  740. rotateB = -1;
  741. rotateC = -1;
  742. rotateD = 0;
  743. break;
  744. default:
  745. rotateA = 1;
  746. rotateB = 0;
  747. rotateC = 0;
  748. rotateD = -1;
  749. break;
  750. }
  751. if (dontFlip) {
  752. rotateC = -rotateC;
  753. rotateD = -rotateD;
  754. }
  755. var offsetCanvasX, offsetCanvasY;
  756. var width, height;
  757. if (rotateA === 0) {
  758. offsetCanvasX = Math.abs(centerY - viewBox[1]) * scale + offsetX;
  759. offsetCanvasY = Math.abs(centerX - viewBox[0]) * scale + offsetY;
  760. width = Math.abs(viewBox[3] - viewBox[1]) * scale;
  761. height = Math.abs(viewBox[2] - viewBox[0]) * scale;
  762. } else {
  763. offsetCanvasX = Math.abs(centerX - viewBox[0]) * scale + offsetX;
  764. offsetCanvasY = Math.abs(centerY - viewBox[1]) * scale + offsetY;
  765. width = Math.abs(viewBox[2] - viewBox[0]) * scale;
  766. height = Math.abs(viewBox[3] - viewBox[1]) * scale;
  767. }
  768. this.transform = [rotateA * scale, rotateB * scale, rotateC * scale, rotateD * scale, offsetCanvasX - rotateA * scale * centerX - rotateC * scale * centerY, offsetCanvasY - rotateB * scale * centerX - rotateD * scale * centerY];
  769. this.width = width;
  770. this.height = height;
  771. this.fontScale = scale;
  772. }
  773. PageViewport.prototype = {
  774. clone: function PageViewPort_clone(args) {
  775. args = args || {};
  776. var scale = 'scale' in args ? args.scale : this.scale;
  777. var rotation = 'rotation' in args ? args.rotation : this.rotation;
  778. return new PageViewport(this.viewBox.slice(), scale, rotation, this.offsetX, this.offsetY, args.dontFlip);
  779. },
  780. convertToViewportPoint: function PageViewport_convertToViewportPoint(x, y) {
  781. return Util.applyTransform([x, y], this.transform);
  782. },
  783. convertToViewportRectangle: function PageViewport_convertToViewportRectangle(rect) {
  784. var tl = Util.applyTransform([rect[0], rect[1]], this.transform);
  785. var br = Util.applyTransform([rect[2], rect[3]], this.transform);
  786. return [tl[0], tl[1], br[0], br[1]];
  787. },
  788. convertToPdfPoint: function PageViewport_convertToPdfPoint(x, y) {
  789. return Util.applyInverseTransform([x, y], this.transform);
  790. }
  791. };
  792. return PageViewport;
  793. }();
  794. 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];
  795. function stringToPDFString(str) {
  796. var i,
  797. n = str.length,
  798. strBuf = [];
  799. if (str[0] === '\xFE' && str[1] === '\xFF') {
  800. for (i = 2; i < n; i += 2) {
  801. strBuf.push(String.fromCharCode(str.charCodeAt(i) << 8 | str.charCodeAt(i + 1)));
  802. }
  803. } else {
  804. for (i = 0; i < n; ++i) {
  805. var code = PDFStringTranslateTable[str.charCodeAt(i)];
  806. strBuf.push(code ? String.fromCharCode(code) : str.charAt(i));
  807. }
  808. }
  809. return strBuf.join('');
  810. }
  811. function stringToUTF8String(str) {
  812. return decodeURIComponent(escape(str));
  813. }
  814. function utf8StringToString(str) {
  815. return unescape(encodeURIComponent(str));
  816. }
  817. function isEmptyObj(obj) {
  818. for (var key in obj) {
  819. return false;
  820. }
  821. return true;
  822. }
  823. function isBool(v) {
  824. return typeof v === 'boolean';
  825. }
  826. function isNum(v) {
  827. return typeof v === 'number';
  828. }
  829. function isString(v) {
  830. return typeof v === 'string';
  831. }
  832. function isArrayBuffer(v) {
  833. return (typeof v === 'undefined' ? 'undefined' : _typeof(v)) === 'object' && v !== null && v.byteLength !== undefined;
  834. }
  835. function isSpace(ch) {
  836. return ch === 0x20 || ch === 0x09 || ch === 0x0D || ch === 0x0A;
  837. }
  838. function createPromiseCapability() {
  839. var capability = {};
  840. capability.promise = new Promise(function (resolve, reject) {
  841. capability.resolve = resolve;
  842. capability.reject = reject;
  843. });
  844. return capability;
  845. }
  846. var createBlob = function createBlob(data, contentType) {
  847. if (typeof Blob !== 'undefined') {
  848. return new Blob([data], { type: contentType });
  849. }
  850. throw new Error('The "Blob" constructor is not supported.');
  851. };
  852. var createObjectURL = function createObjectURLClosure() {
  853. var digits = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/=';
  854. return function createObjectURL(data, contentType) {
  855. var forceDataSchema = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : false;
  856. if (!forceDataSchema && URL.createObjectURL) {
  857. var blob = createBlob(data, contentType);
  858. return URL.createObjectURL(blob);
  859. }
  860. var buffer = 'data:' + contentType + ';base64,';
  861. for (var i = 0, ii = data.length; i < ii; i += 3) {
  862. var b1 = data[i] & 0xFF;
  863. var b2 = data[i + 1] & 0xFF;
  864. var b3 = data[i + 2] & 0xFF;
  865. var d1 = b1 >> 2,
  866. d2 = (b1 & 3) << 4 | b2 >> 4;
  867. var d3 = i + 1 < ii ? (b2 & 0xF) << 2 | b3 >> 6 : 64;
  868. var d4 = i + 2 < ii ? b3 & 0x3F : 64;
  869. buffer += digits[d1] + digits[d2] + digits[d3] + digits[d4];
  870. }
  871. return buffer;
  872. };
  873. }();
  874. function resolveCall(fn, args) {
  875. var thisArg = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : null;
  876. if (!fn) {
  877. return Promise.resolve(undefined);
  878. }
  879. return new Promise(function (resolve, reject) {
  880. resolve(fn.apply(thisArg, args));
  881. });
  882. }
  883. function wrapReason(reason) {
  884. if ((typeof reason === 'undefined' ? 'undefined' : _typeof(reason)) !== 'object') {
  885. return reason;
  886. }
  887. switch (reason.name) {
  888. case 'AbortException':
  889. return new AbortException(reason.message);
  890. case 'MissingPDFException':
  891. return new MissingPDFException(reason.message);
  892. case 'UnexpectedResponseException':
  893. return new UnexpectedResponseException(reason.message, reason.status);
  894. default:
  895. return new UnknownErrorException(reason.message, reason.details);
  896. }
  897. }
  898. function makeReasonSerializable(reason) {
  899. if (!(reason instanceof Error) || reason instanceof AbortException || reason instanceof MissingPDFException || reason instanceof UnexpectedResponseException || reason instanceof UnknownErrorException) {
  900. return reason;
  901. }
  902. return new UnknownErrorException(reason.message, reason.toString());
  903. }
  904. function resolveOrReject(capability, success, reason) {
  905. if (success) {
  906. capability.resolve();
  907. } else {
  908. capability.reject(reason);
  909. }
  910. }
  911. function finalize(promise) {
  912. return Promise.resolve(promise).catch(function () {});
  913. }
  914. function MessageHandler(sourceName, targetName, comObj) {
  915. var _this = this;
  916. this.sourceName = sourceName;
  917. this.targetName = targetName;
  918. this.comObj = comObj;
  919. this.callbackId = 1;
  920. this.streamId = 1;
  921. this.postMessageTransfers = true;
  922. this.streamSinks = Object.create(null);
  923. this.streamControllers = Object.create(null);
  924. var callbacksCapabilities = this.callbacksCapabilities = Object.create(null);
  925. var ah = this.actionHandler = Object.create(null);
  926. this._onComObjOnMessage = function (event) {
  927. var data = event.data;
  928. if (data.targetName !== _this.sourceName) {
  929. return;
  930. }
  931. if (data.stream) {
  932. _this._processStreamMessage(data);
  933. } else if (data.isReply) {
  934. var callbackId = data.callbackId;
  935. if (data.callbackId in callbacksCapabilities) {
  936. var callback = callbacksCapabilities[callbackId];
  937. delete callbacksCapabilities[callbackId];
  938. if ('error' in data) {
  939. callback.reject(wrapReason(data.error));
  940. } else {
  941. callback.resolve(data.data);
  942. }
  943. } else {
  944. throw new Error('Cannot resolve callback ' + callbackId);
  945. }
  946. } else if (data.action in ah) {
  947. var action = ah[data.action];
  948. if (data.callbackId) {
  949. var _sourceName = _this.sourceName;
  950. var _targetName = data.sourceName;
  951. Promise.resolve().then(function () {
  952. return action[0].call(action[1], data.data);
  953. }).then(function (result) {
  954. comObj.postMessage({
  955. sourceName: _sourceName,
  956. targetName: _targetName,
  957. isReply: true,
  958. callbackId: data.callbackId,
  959. data: result
  960. });
  961. }, function (reason) {
  962. comObj.postMessage({
  963. sourceName: _sourceName,
  964. targetName: _targetName,
  965. isReply: true,
  966. callbackId: data.callbackId,
  967. error: makeReasonSerializable(reason)
  968. });
  969. });
  970. } else if (data.streamId) {
  971. _this._createStreamSink(data);
  972. } else {
  973. action[0].call(action[1], data.data);
  974. }
  975. } else {
  976. throw new Error('Unknown action from worker: ' + data.action);
  977. }
  978. };
  979. comObj.addEventListener('message', this._onComObjOnMessage);
  980. }
  981. MessageHandler.prototype = {
  982. on: function on(actionName, handler, scope) {
  983. var ah = this.actionHandler;
  984. if (ah[actionName]) {
  985. throw new Error('There is already an actionName called "' + actionName + '"');
  986. }
  987. ah[actionName] = [handler, scope];
  988. },
  989. send: function send(actionName, data, transfers) {
  990. var message = {
  991. sourceName: this.sourceName,
  992. targetName: this.targetName,
  993. action: actionName,
  994. data: data
  995. };
  996. this.postMessage(message, transfers);
  997. },
  998. sendWithPromise: function sendWithPromise(actionName, data, transfers) {
  999. var callbackId = this.callbackId++;
  1000. var message = {
  1001. sourceName: this.sourceName,
  1002. targetName: this.targetName,
  1003. action: actionName,
  1004. data: data,
  1005. callbackId: callbackId
  1006. };
  1007. var capability = createPromiseCapability();
  1008. this.callbacksCapabilities[callbackId] = capability;
  1009. try {
  1010. this.postMessage(message, transfers);
  1011. } catch (e) {
  1012. capability.reject(e);
  1013. }
  1014. return capability.promise;
  1015. },
  1016. sendWithStream: function sendWithStream(actionName, data, queueingStrategy, transfers) {
  1017. var _this2 = this;
  1018. var streamId = this.streamId++;
  1019. var sourceName = this.sourceName;
  1020. var targetName = this.targetName;
  1021. return new _streams_polyfill.ReadableStream({
  1022. start: function start(controller) {
  1023. var startCapability = createPromiseCapability();
  1024. _this2.streamControllers[streamId] = {
  1025. controller: controller,
  1026. startCall: startCapability,
  1027. isClosed: false
  1028. };
  1029. _this2.postMessage({
  1030. sourceName: sourceName,
  1031. targetName: targetName,
  1032. action: actionName,
  1033. streamId: streamId,
  1034. data: data,
  1035. desiredSize: controller.desiredSize
  1036. });
  1037. return startCapability.promise;
  1038. },
  1039. pull: function pull(controller) {
  1040. var pullCapability = createPromiseCapability();
  1041. _this2.streamControllers[streamId].pullCall = pullCapability;
  1042. _this2.postMessage({
  1043. sourceName: sourceName,
  1044. targetName: targetName,
  1045. stream: 'pull',
  1046. streamId: streamId,
  1047. desiredSize: controller.desiredSize
  1048. });
  1049. return pullCapability.promise;
  1050. },
  1051. cancel: function cancel(reason) {
  1052. var cancelCapability = createPromiseCapability();
  1053. _this2.streamControllers[streamId].cancelCall = cancelCapability;
  1054. _this2.streamControllers[streamId].isClosed = true;
  1055. _this2.postMessage({
  1056. sourceName: sourceName,
  1057. targetName: targetName,
  1058. stream: 'cancel',
  1059. reason: reason,
  1060. streamId: streamId
  1061. });
  1062. return cancelCapability.promise;
  1063. }
  1064. }, queueingStrategy);
  1065. },
  1066. _createStreamSink: function _createStreamSink(data) {
  1067. var _this3 = this;
  1068. var self = this;
  1069. var action = this.actionHandler[data.action];
  1070. var streamId = data.streamId;
  1071. var desiredSize = data.desiredSize;
  1072. var sourceName = this.sourceName;
  1073. var targetName = data.sourceName;
  1074. var capability = createPromiseCapability();
  1075. var sendStreamRequest = function sendStreamRequest(_ref2) {
  1076. var stream = _ref2.stream,
  1077. chunk = _ref2.chunk,
  1078. transfers = _ref2.transfers,
  1079. success = _ref2.success,
  1080. reason = _ref2.reason;
  1081. _this3.postMessage({
  1082. sourceName: sourceName,
  1083. targetName: targetName,
  1084. stream: stream,
  1085. streamId: streamId,
  1086. chunk: chunk,
  1087. success: success,
  1088. reason: reason
  1089. }, transfers);
  1090. };
  1091. var streamSink = {
  1092. enqueue: function enqueue(chunk) {
  1093. var size = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : 1;
  1094. var transfers = arguments[2];
  1095. if (this.isCancelled) {
  1096. return;
  1097. }
  1098. var lastDesiredSize = this.desiredSize;
  1099. this.desiredSize -= size;
  1100. if (lastDesiredSize > 0 && this.desiredSize <= 0) {
  1101. this.sinkCapability = createPromiseCapability();
  1102. this.ready = this.sinkCapability.promise;
  1103. }
  1104. sendStreamRequest({
  1105. stream: 'enqueue',
  1106. chunk: chunk,
  1107. transfers: transfers
  1108. });
  1109. },
  1110. close: function close() {
  1111. if (this.isCancelled) {
  1112. return;
  1113. }
  1114. this.isCancelled = true;
  1115. sendStreamRequest({ stream: 'close' });
  1116. delete self.streamSinks[streamId];
  1117. },
  1118. error: function error(reason) {
  1119. if (this.isCancelled) {
  1120. return;
  1121. }
  1122. this.isCancelled = true;
  1123. sendStreamRequest({
  1124. stream: 'error',
  1125. reason: reason
  1126. });
  1127. },
  1128. sinkCapability: capability,
  1129. onPull: null,
  1130. onCancel: null,
  1131. isCancelled: false,
  1132. desiredSize: desiredSize,
  1133. ready: null
  1134. };
  1135. streamSink.sinkCapability.resolve();
  1136. streamSink.ready = streamSink.sinkCapability.promise;
  1137. this.streamSinks[streamId] = streamSink;
  1138. resolveCall(action[0], [data.data, streamSink], action[1]).then(function () {
  1139. sendStreamRequest({
  1140. stream: 'start_complete',
  1141. success: true
  1142. });
  1143. }, function (reason) {
  1144. sendStreamRequest({
  1145. stream: 'start_complete',
  1146. success: false,
  1147. reason: reason
  1148. });
  1149. });
  1150. },
  1151. _processStreamMessage: function _processStreamMessage(data) {
  1152. var _this4 = this;
  1153. var sourceName = this.sourceName;
  1154. var targetName = data.sourceName;
  1155. var streamId = data.streamId;
  1156. var sendStreamResponse = function sendStreamResponse(_ref3) {
  1157. var stream = _ref3.stream,
  1158. success = _ref3.success,
  1159. reason = _ref3.reason;
  1160. _this4.comObj.postMessage({
  1161. sourceName: sourceName,
  1162. targetName: targetName,
  1163. stream: stream,
  1164. success: success,
  1165. streamId: streamId,
  1166. reason: reason
  1167. });
  1168. };
  1169. var deleteStreamController = function deleteStreamController() {
  1170. Promise.all([_this4.streamControllers[data.streamId].startCall, _this4.streamControllers[data.streamId].pullCall, _this4.streamControllers[data.streamId].cancelCall].map(function (capability) {
  1171. return capability && finalize(capability.promise);
  1172. })).then(function () {
  1173. delete _this4.streamControllers[data.streamId];
  1174. });
  1175. };
  1176. switch (data.stream) {
  1177. case 'start_complete':
  1178. resolveOrReject(this.streamControllers[data.streamId].startCall, data.success, wrapReason(data.reason));
  1179. break;
  1180. case 'pull_complete':
  1181. resolveOrReject(this.streamControllers[data.streamId].pullCall, data.success, wrapReason(data.reason));
  1182. break;
  1183. case 'pull':
  1184. if (!this.streamSinks[data.streamId]) {
  1185. sendStreamResponse({
  1186. stream: 'pull_complete',
  1187. success: true
  1188. });
  1189. break;
  1190. }
  1191. if (this.streamSinks[data.streamId].desiredSize <= 0 && data.desiredSize > 0) {
  1192. this.streamSinks[data.streamId].sinkCapability.resolve();
  1193. }
  1194. this.streamSinks[data.streamId].desiredSize = data.desiredSize;
  1195. resolveCall(this.streamSinks[data.streamId].onPull).then(function () {
  1196. sendStreamResponse({
  1197. stream: 'pull_complete',
  1198. success: true
  1199. });
  1200. }, function (reason) {
  1201. sendStreamResponse({
  1202. stream: 'pull_complete',
  1203. success: false,
  1204. reason: reason
  1205. });
  1206. });
  1207. break;
  1208. case 'enqueue':
  1209. assert(this.streamControllers[data.streamId], 'enqueue should have stream controller');
  1210. if (!this.streamControllers[data.streamId].isClosed) {
  1211. this.streamControllers[data.streamId].controller.enqueue(data.chunk);
  1212. }
  1213. break;
  1214. case 'close':
  1215. assert(this.streamControllers[data.streamId], 'close should have stream controller');
  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. assert(this.streamControllers[data.streamId], 'error should have stream controller');
  1225. this.streamControllers[data.streamId].controller.error(wrapReason(data.reason));
  1226. deleteStreamController();
  1227. break;
  1228. case 'cancel_complete':
  1229. resolveOrReject(this.streamControllers[data.streamId].cancelCall, data.success, wrapReason(data.reason));
  1230. deleteStreamController();
  1231. break;
  1232. case 'cancel':
  1233. if (!this.streamSinks[data.streamId]) {
  1234. break;
  1235. }
  1236. resolveCall(this.streamSinks[data.streamId].onCancel, [wrapReason(data.reason)]).then(function () {
  1237. sendStreamResponse({
  1238. stream: 'cancel_complete',
  1239. success: true
  1240. });
  1241. }, function (reason) {
  1242. sendStreamResponse({
  1243. stream: 'cancel_complete',
  1244. success: false,
  1245. reason: reason
  1246. });
  1247. });
  1248. this.streamSinks[data.streamId].sinkCapability.reject(wrapReason(data.reason));
  1249. this.streamSinks[data.streamId].isCancelled = true;
  1250. delete this.streamSinks[data.streamId];
  1251. break;
  1252. default:
  1253. throw new Error('Unexpected stream case');
  1254. }
  1255. },
  1256. postMessage: function postMessage(message, transfers) {
  1257. if (transfers && this.postMessageTransfers) {
  1258. this.comObj.postMessage(message, transfers);
  1259. } else {
  1260. this.comObj.postMessage(message);
  1261. }
  1262. },
  1263. destroy: function destroy() {
  1264. this.comObj.removeEventListener('message', this._onComObjOnMessage);
  1265. }
  1266. };
  1267. exports.FONT_IDENTITY_MATRIX = FONT_IDENTITY_MATRIX;
  1268. exports.IDENTITY_MATRIX = IDENTITY_MATRIX;
  1269. exports.OPS = OPS;
  1270. exports.VerbosityLevel = VerbosityLevel;
  1271. exports.UNSUPPORTED_FEATURES = UNSUPPORTED_FEATURES;
  1272. exports.AnnotationBorderStyleType = AnnotationBorderStyleType;
  1273. exports.AnnotationFieldFlag = AnnotationFieldFlag;
  1274. exports.AnnotationFlag = AnnotationFlag;
  1275. exports.AnnotationType = AnnotationType;
  1276. exports.FontType = FontType;
  1277. exports.ImageKind = ImageKind;
  1278. exports.CMapCompressionType = CMapCompressionType;
  1279. exports.AbortException = AbortException;
  1280. exports.InvalidPDFException = InvalidPDFException;
  1281. exports.MessageHandler = MessageHandler;
  1282. exports.MissingDataException = MissingDataException;
  1283. exports.MissingPDFException = MissingPDFException;
  1284. exports.NativeImageDecoding = NativeImageDecoding;
  1285. exports.NotImplementedException = NotImplementedException;
  1286. exports.PageViewport = PageViewport;
  1287. exports.PasswordException = PasswordException;
  1288. exports.PasswordResponses = PasswordResponses;
  1289. exports.StreamType = StreamType;
  1290. exports.TextRenderingMode = TextRenderingMode;
  1291. exports.UnexpectedResponseException = UnexpectedResponseException;
  1292. exports.UnknownErrorException = UnknownErrorException;
  1293. exports.Util = Util;
  1294. exports.XRefParseException = XRefParseException;
  1295. exports.FormatError = FormatError;
  1296. exports.arrayByteLength = arrayByteLength;
  1297. exports.arraysToBytes = arraysToBytes;
  1298. exports.assert = assert;
  1299. exports.bytesToString = bytesToString;
  1300. exports.createBlob = createBlob;
  1301. exports.createPromiseCapability = createPromiseCapability;
  1302. exports.createObjectURL = createObjectURL;
  1303. exports.deprecated = deprecated;
  1304. exports.getInheritableProperty = getInheritableProperty;
  1305. exports.getLookupTableFactory = getLookupTableFactory;
  1306. exports.getVerbosityLevel = getVerbosityLevel;
  1307. exports.info = info;
  1308. exports.isArrayBuffer = isArrayBuffer;
  1309. exports.isBool = isBool;
  1310. exports.isEmptyObj = isEmptyObj;
  1311. exports.isNum = isNum;
  1312. exports.isString = isString;
  1313. exports.isSpace = isSpace;
  1314. exports.isSameOrigin = isSameOrigin;
  1315. exports.createValidAbsoluteUrl = createValidAbsoluteUrl;
  1316. exports.isLittleEndian = isLittleEndian;
  1317. exports.isEvalSupported = isEvalSupported;
  1318. exports.log2 = log2;
  1319. exports.readInt8 = readInt8;
  1320. exports.readUint16 = readUint16;
  1321. exports.readUint32 = readUint32;
  1322. exports.removeNullCharacters = removeNullCharacters;
  1323. exports.ReadableStream = _streams_polyfill.ReadableStream;
  1324. exports.setVerbosityLevel = setVerbosityLevel;
  1325. exports.shadow = shadow;
  1326. exports.string32 = string32;
  1327. exports.stringToBytes = stringToBytes;
  1328. exports.stringToPDFString = stringToPDFString;
  1329. exports.stringToUTF8String = stringToUTF8String;
  1330. exports.utf8StringToString = utf8StringToString;
  1331. exports.warn = warn;
  1332. exports.unreachable = unreachable;