util.js 42 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336
  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.loadJpegStream = exports.isEvalSupported = exports.isLittleEndian = exports.createValidAbsoluteUrl = exports.isSameOrigin = exports.isSpace = exports.isString = exports.isNum = exports.isEmptyObj = exports.isBool = exports.isArrayBuffer = exports.info = exports.getVerbosityLevel = exports.getLookupTableFactory = exports.deprecated = exports.createObjectURL = exports.createPromiseCapability = exports.createBlob = exports.bytesToString = exports.assert = exports.arraysToBytes = exports.arrayByteLength = exports.FormatError = exports.XRefParseException = exports.Util = exports.UnknownErrorException = exports.UnexpectedResponseException = exports.TextRenderingMode = exports.StreamType = exports.PasswordResponses = exports.PasswordException = exports.PageViewport = exports.NotImplementedException = exports.NativeImageDecoding = exports.MissingPDFException = exports.MissingDataException = exports.MessageHandler = exports.InvalidPDFException = exports.AbortException = exports.CMapCompressionType = exports.ImageKind = exports.FontType = exports.AnnotationType = exports.AnnotationFlag = exports.AnnotationFieldFlag = exports.AnnotationBorderStyleType = exports.UNSUPPORTED_FEATURES = exports.VERBOSITY_LEVELS = 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 VERBOSITY_LEVELS = {
  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 = VERBOSITY_LEVELS.warnings;
  250. function setVerbosityLevel(level) {
  251. verbosity = level;
  252. }
  253. function getVerbosityLevel() {
  254. return verbosity;
  255. }
  256. function info(msg) {
  257. if (verbosity >= VERBOSITY_LEVELS.infos) {
  258. console.log('Info: ' + msg);
  259. }
  260. }
  261. function warn(msg) {
  262. if (verbosity >= VERBOSITY_LEVELS.warnings) {
  263. console.log('Warning: ' + msg);
  264. }
  265. }
  266. function deprecated(details) {
  267. console.log('Deprecated API usage: ' + details);
  268. }
  269. function unreachable(msg) {
  270. throw new Error(msg);
  271. }
  272. function assert(cond, msg) {
  273. if (!cond) {
  274. unreachable(msg);
  275. }
  276. }
  277. var UNSUPPORTED_FEATURES = {
  278. unknown: 'unknown',
  279. forms: 'forms',
  280. javaScript: 'javaScript',
  281. smask: 'smask',
  282. shadingPattern: 'shadingPattern',
  283. font: 'font'
  284. };
  285. function isSameOrigin(baseUrl, otherUrl) {
  286. try {
  287. var base = new URL(baseUrl);
  288. if (!base.origin || base.origin === 'null') {
  289. return false;
  290. }
  291. } catch (e) {
  292. return false;
  293. }
  294. var other = new URL(otherUrl, base);
  295. return base.origin === other.origin;
  296. }
  297. function isValidProtocol(url) {
  298. if (!url) {
  299. return false;
  300. }
  301. switch (url.protocol) {
  302. case 'http:':
  303. case 'https:':
  304. case 'ftp:':
  305. case 'mailto:':
  306. case 'tel:':
  307. return true;
  308. default:
  309. return false;
  310. }
  311. }
  312. function createValidAbsoluteUrl(url, baseUrl) {
  313. if (!url) {
  314. return null;
  315. }
  316. try {
  317. var absoluteUrl = baseUrl ? new URL(url, baseUrl) : new URL(url);
  318. if (isValidProtocol(absoluteUrl)) {
  319. return absoluteUrl;
  320. }
  321. } catch (ex) {}
  322. return null;
  323. }
  324. function shadow(obj, prop, value) {
  325. Object.defineProperty(obj, prop, {
  326. value: value,
  327. enumerable: true,
  328. configurable: true,
  329. writable: false
  330. });
  331. return value;
  332. }
  333. function getLookupTableFactory(initializer) {
  334. var lookup;
  335. return function () {
  336. if (initializer) {
  337. lookup = Object.create(null);
  338. initializer(lookup);
  339. initializer = null;
  340. }
  341. return lookup;
  342. };
  343. }
  344. var PasswordResponses = {
  345. NEED_PASSWORD: 1,
  346. INCORRECT_PASSWORD: 2
  347. };
  348. var PasswordException = function PasswordExceptionClosure() {
  349. function PasswordException(msg, code) {
  350. this.name = 'PasswordException';
  351. this.message = msg;
  352. this.code = code;
  353. }
  354. PasswordException.prototype = new Error();
  355. PasswordException.constructor = PasswordException;
  356. return PasswordException;
  357. }();
  358. var UnknownErrorException = function UnknownErrorExceptionClosure() {
  359. function UnknownErrorException(msg, details) {
  360. this.name = 'UnknownErrorException';
  361. this.message = msg;
  362. this.details = details;
  363. }
  364. UnknownErrorException.prototype = new Error();
  365. UnknownErrorException.constructor = UnknownErrorException;
  366. return UnknownErrorException;
  367. }();
  368. var InvalidPDFException = function InvalidPDFExceptionClosure() {
  369. function InvalidPDFException(msg) {
  370. this.name = 'InvalidPDFException';
  371. this.message = msg;
  372. }
  373. InvalidPDFException.prototype = new Error();
  374. InvalidPDFException.constructor = InvalidPDFException;
  375. return InvalidPDFException;
  376. }();
  377. var MissingPDFException = function MissingPDFExceptionClosure() {
  378. function MissingPDFException(msg) {
  379. this.name = 'MissingPDFException';
  380. this.message = msg;
  381. }
  382. MissingPDFException.prototype = new Error();
  383. MissingPDFException.constructor = MissingPDFException;
  384. return MissingPDFException;
  385. }();
  386. var UnexpectedResponseException = function UnexpectedResponseExceptionClosure() {
  387. function UnexpectedResponseException(msg, status) {
  388. this.name = 'UnexpectedResponseException';
  389. this.message = msg;
  390. this.status = status;
  391. }
  392. UnexpectedResponseException.prototype = new Error();
  393. UnexpectedResponseException.constructor = UnexpectedResponseException;
  394. return UnexpectedResponseException;
  395. }();
  396. var NotImplementedException = function NotImplementedExceptionClosure() {
  397. function NotImplementedException(msg) {
  398. this.message = msg;
  399. }
  400. NotImplementedException.prototype = new Error();
  401. NotImplementedException.prototype.name = 'NotImplementedException';
  402. NotImplementedException.constructor = NotImplementedException;
  403. return NotImplementedException;
  404. }();
  405. var MissingDataException = function MissingDataExceptionClosure() {
  406. function MissingDataException(begin, end) {
  407. this.begin = begin;
  408. this.end = end;
  409. this.message = 'Missing data [' + begin + ', ' + end + ')';
  410. }
  411. MissingDataException.prototype = new Error();
  412. MissingDataException.prototype.name = 'MissingDataException';
  413. MissingDataException.constructor = MissingDataException;
  414. return MissingDataException;
  415. }();
  416. var XRefParseException = function XRefParseExceptionClosure() {
  417. function XRefParseException(msg) {
  418. this.message = msg;
  419. }
  420. XRefParseException.prototype = new Error();
  421. XRefParseException.prototype.name = 'XRefParseException';
  422. XRefParseException.constructor = XRefParseException;
  423. return XRefParseException;
  424. }();
  425. var FormatError = function FormatErrorClosure() {
  426. function FormatError(msg) {
  427. this.message = msg;
  428. }
  429. FormatError.prototype = new Error();
  430. FormatError.prototype.name = 'FormatError';
  431. FormatError.constructor = FormatError;
  432. return FormatError;
  433. }();
  434. var AbortException = function AbortExceptionClosure() {
  435. function AbortException(msg) {
  436. this.name = 'AbortException';
  437. this.message = msg;
  438. }
  439. AbortException.prototype = new Error();
  440. AbortException.constructor = AbortException;
  441. return AbortException;
  442. }();
  443. var NullCharactersRegExp = /\x00/g;
  444. function removeNullCharacters(str) {
  445. if (typeof str !== 'string') {
  446. warn('The argument for removeNullCharacters must be a string.');
  447. return str;
  448. }
  449. return str.replace(NullCharactersRegExp, '');
  450. }
  451. function bytesToString(bytes) {
  452. assert(bytes !== null && (typeof bytes === 'undefined' ? 'undefined' : _typeof(bytes)) === 'object' && bytes.length !== undefined, 'Invalid argument for bytesToString');
  453. var length = bytes.length;
  454. var MAX_ARGUMENT_COUNT = 8192;
  455. if (length < MAX_ARGUMENT_COUNT) {
  456. return String.fromCharCode.apply(null, bytes);
  457. }
  458. var strBuf = [];
  459. for (var i = 0; i < length; i += MAX_ARGUMENT_COUNT) {
  460. var chunkEnd = Math.min(i + MAX_ARGUMENT_COUNT, length);
  461. var chunk = bytes.subarray(i, chunkEnd);
  462. strBuf.push(String.fromCharCode.apply(null, chunk));
  463. }
  464. return strBuf.join('');
  465. }
  466. function stringToBytes(str) {
  467. assert(typeof str === 'string', 'Invalid argument for stringToBytes');
  468. var length = str.length;
  469. var bytes = new Uint8Array(length);
  470. for (var i = 0; i < length; ++i) {
  471. bytes[i] = str.charCodeAt(i) & 0xFF;
  472. }
  473. return bytes;
  474. }
  475. function arrayByteLength(arr) {
  476. if (arr.length !== undefined) {
  477. return arr.length;
  478. }
  479. assert(arr.byteLength !== undefined);
  480. return arr.byteLength;
  481. }
  482. function arraysToBytes(arr) {
  483. if (arr.length === 1 && arr[0] instanceof Uint8Array) {
  484. return arr[0];
  485. }
  486. var resultLength = 0;
  487. var i,
  488. ii = arr.length;
  489. var item, itemLength;
  490. for (i = 0; i < ii; i++) {
  491. item = arr[i];
  492. itemLength = arrayByteLength(item);
  493. resultLength += itemLength;
  494. }
  495. var pos = 0;
  496. var data = new Uint8Array(resultLength);
  497. for (i = 0; i < ii; i++) {
  498. item = arr[i];
  499. if (!(item instanceof Uint8Array)) {
  500. if (typeof item === 'string') {
  501. item = stringToBytes(item);
  502. } else {
  503. item = new Uint8Array(item);
  504. }
  505. }
  506. itemLength = item.byteLength;
  507. data.set(item, pos);
  508. pos += itemLength;
  509. }
  510. return data;
  511. }
  512. function string32(value) {
  513. return String.fromCharCode(value >> 24 & 0xff, value >> 16 & 0xff, value >> 8 & 0xff, value & 0xff);
  514. }
  515. function log2(x) {
  516. if (x <= 0) {
  517. return 0;
  518. }
  519. return Math.ceil(Math.log2(x));
  520. }
  521. function readInt8(data, start) {
  522. return data[start] << 24 >> 24;
  523. }
  524. function readUint16(data, offset) {
  525. return data[offset] << 8 | data[offset + 1];
  526. }
  527. function readUint32(data, offset) {
  528. return (data[offset] << 24 | data[offset + 1] << 16 | data[offset + 2] << 8 | data[offset + 3]) >>> 0;
  529. }
  530. function isLittleEndian() {
  531. var buffer8 = new Uint8Array(4);
  532. buffer8[0] = 1;
  533. var view32 = new Uint32Array(buffer8.buffer, 0, 1);
  534. return view32[0] === 1;
  535. }
  536. function isEvalSupported() {
  537. try {
  538. new Function('');
  539. return true;
  540. } catch (e) {
  541. return false;
  542. }
  543. }
  544. var IDENTITY_MATRIX = [1, 0, 0, 1, 0, 0];
  545. var Util = function UtilClosure() {
  546. function Util() {}
  547. var rgbBuf = ['rgb(', 0, ',', 0, ',', 0, ')'];
  548. Util.makeCssRgb = function Util_makeCssRgb(r, g, b) {
  549. rgbBuf[1] = r;
  550. rgbBuf[3] = g;
  551. rgbBuf[5] = b;
  552. return rgbBuf.join('');
  553. };
  554. Util.transform = function Util_transform(m1, m2) {
  555. 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]];
  556. };
  557. Util.applyTransform = function Util_applyTransform(p, m) {
  558. var xt = p[0] * m[0] + p[1] * m[2] + m[4];
  559. var yt = p[0] * m[1] + p[1] * m[3] + m[5];
  560. return [xt, yt];
  561. };
  562. Util.applyInverseTransform = function Util_applyInverseTransform(p, m) {
  563. var d = m[0] * m[3] - m[1] * m[2];
  564. var xt = (p[0] * m[3] - p[1] * m[2] + m[2] * m[5] - m[4] * m[3]) / d;
  565. var yt = (-p[0] * m[1] + p[1] * m[0] + m[4] * m[1] - m[5] * m[0]) / d;
  566. return [xt, yt];
  567. };
  568. Util.getAxialAlignedBoundingBox = function Util_getAxialAlignedBoundingBox(r, m) {
  569. var p1 = Util.applyTransform(r, m);
  570. var p2 = Util.applyTransform(r.slice(2, 4), m);
  571. var p3 = Util.applyTransform([r[0], r[3]], m);
  572. var p4 = Util.applyTransform([r[2], r[1]], m);
  573. 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])];
  574. };
  575. Util.inverseTransform = function Util_inverseTransform(m) {
  576. var d = m[0] * m[3] - m[1] * m[2];
  577. 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];
  578. };
  579. Util.apply3dTransform = function Util_apply3dTransform(m, v) {
  580. 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]];
  581. };
  582. Util.singularValueDecompose2dScale = function Util_singularValueDecompose2dScale(m) {
  583. var transpose = [m[0], m[2], m[1], m[3]];
  584. var a = m[0] * transpose[0] + m[1] * transpose[2];
  585. var b = m[0] * transpose[1] + m[1] * transpose[3];
  586. var c = m[2] * transpose[0] + m[3] * transpose[2];
  587. var d = m[2] * transpose[1] + m[3] * transpose[3];
  588. var first = (a + d) / 2;
  589. var second = Math.sqrt((a + d) * (a + d) - 4 * (a * d - c * b)) / 2;
  590. var sx = first + second || 1;
  591. var sy = first - second || 1;
  592. return [Math.sqrt(sx), Math.sqrt(sy)];
  593. };
  594. Util.normalizeRect = function Util_normalizeRect(rect) {
  595. var r = rect.slice(0);
  596. if (rect[0] > rect[2]) {
  597. r[0] = rect[2];
  598. r[2] = rect[0];
  599. }
  600. if (rect[1] > rect[3]) {
  601. r[1] = rect[3];
  602. r[3] = rect[1];
  603. }
  604. return r;
  605. };
  606. Util.intersect = function Util_intersect(rect1, rect2) {
  607. function compare(a, b) {
  608. return a - b;
  609. }
  610. var orderedX = [rect1[0], rect1[2], rect2[0], rect2[2]].sort(compare),
  611. orderedY = [rect1[1], rect1[3], rect2[1], rect2[3]].sort(compare),
  612. result = [];
  613. rect1 = Util.normalizeRect(rect1);
  614. rect2 = Util.normalizeRect(rect2);
  615. if (orderedX[0] === rect1[0] && orderedX[1] === rect2[0] || orderedX[0] === rect2[0] && orderedX[1] === rect1[0]) {
  616. result[0] = orderedX[1];
  617. result[2] = orderedX[2];
  618. } else {
  619. return false;
  620. }
  621. if (orderedY[0] === rect1[1] && orderedY[1] === rect2[1] || orderedY[0] === rect2[1] && orderedY[1] === rect1[1]) {
  622. result[1] = orderedY[1];
  623. result[3] = orderedY[2];
  624. } else {
  625. return false;
  626. }
  627. return result;
  628. };
  629. 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'];
  630. Util.toRoman = function Util_toRoman(number, lowerCase) {
  631. assert(Number.isInteger(number) && number > 0, 'The number should be a positive integer.');
  632. var pos,
  633. romanBuf = [];
  634. while (number >= 1000) {
  635. number -= 1000;
  636. romanBuf.push('M');
  637. }
  638. pos = number / 100 | 0;
  639. number %= 100;
  640. romanBuf.push(ROMAN_NUMBER_MAP[pos]);
  641. pos = number / 10 | 0;
  642. number %= 10;
  643. romanBuf.push(ROMAN_NUMBER_MAP[10 + pos]);
  644. romanBuf.push(ROMAN_NUMBER_MAP[20 + number]);
  645. var romanStr = romanBuf.join('');
  646. return lowerCase ? romanStr.toLowerCase() : romanStr;
  647. };
  648. Util.appendToArray = function Util_appendToArray(arr1, arr2) {
  649. Array.prototype.push.apply(arr1, arr2);
  650. };
  651. Util.prependToArray = function Util_prependToArray(arr1, arr2) {
  652. Array.prototype.unshift.apply(arr1, arr2);
  653. };
  654. Util.extendObj = function extendObj(obj1, obj2) {
  655. for (var key in obj2) {
  656. obj1[key] = obj2[key];
  657. }
  658. };
  659. Util.getInheritableProperty = function Util_getInheritableProperty(dict, name, getArray) {
  660. while (dict && !dict.has(name)) {
  661. dict = dict.get('Parent');
  662. }
  663. if (!dict) {
  664. return null;
  665. }
  666. return getArray ? dict.getArray(name) : dict.get(name);
  667. };
  668. Util.inherit = function Util_inherit(sub, base, prototype) {
  669. sub.prototype = Object.create(base.prototype);
  670. sub.prototype.constructor = sub;
  671. for (var prop in prototype) {
  672. sub.prototype[prop] = prototype[prop];
  673. }
  674. };
  675. Util.loadScript = function Util_loadScript(src, callback) {
  676. var script = document.createElement('script');
  677. var loaded = false;
  678. script.setAttribute('src', src);
  679. if (callback) {
  680. script.onload = function () {
  681. if (!loaded) {
  682. callback();
  683. }
  684. loaded = true;
  685. };
  686. }
  687. document.getElementsByTagName('head')[0].appendChild(script);
  688. };
  689. return Util;
  690. }();
  691. var PageViewport = function PageViewportClosure() {
  692. function PageViewport(viewBox, scale, rotation, offsetX, offsetY, dontFlip) {
  693. this.viewBox = viewBox;
  694. this.scale = scale;
  695. this.rotation = rotation;
  696. this.offsetX = offsetX;
  697. this.offsetY = offsetY;
  698. var centerX = (viewBox[2] + viewBox[0]) / 2;
  699. var centerY = (viewBox[3] + viewBox[1]) / 2;
  700. var rotateA, rotateB, rotateC, rotateD;
  701. rotation = rotation % 360;
  702. rotation = rotation < 0 ? rotation + 360 : rotation;
  703. switch (rotation) {
  704. case 180:
  705. rotateA = -1;
  706. rotateB = 0;
  707. rotateC = 0;
  708. rotateD = 1;
  709. break;
  710. case 90:
  711. rotateA = 0;
  712. rotateB = 1;
  713. rotateC = 1;
  714. rotateD = 0;
  715. break;
  716. case 270:
  717. rotateA = 0;
  718. rotateB = -1;
  719. rotateC = -1;
  720. rotateD = 0;
  721. break;
  722. default:
  723. rotateA = 1;
  724. rotateB = 0;
  725. rotateC = 0;
  726. rotateD = -1;
  727. break;
  728. }
  729. if (dontFlip) {
  730. rotateC = -rotateC;
  731. rotateD = -rotateD;
  732. }
  733. var offsetCanvasX, offsetCanvasY;
  734. var width, height;
  735. if (rotateA === 0) {
  736. offsetCanvasX = Math.abs(centerY - viewBox[1]) * scale + offsetX;
  737. offsetCanvasY = Math.abs(centerX - viewBox[0]) * scale + offsetY;
  738. width = Math.abs(viewBox[3] - viewBox[1]) * scale;
  739. height = Math.abs(viewBox[2] - viewBox[0]) * scale;
  740. } else {
  741. offsetCanvasX = Math.abs(centerX - viewBox[0]) * scale + offsetX;
  742. offsetCanvasY = Math.abs(centerY - viewBox[1]) * scale + offsetY;
  743. width = Math.abs(viewBox[2] - viewBox[0]) * scale;
  744. height = Math.abs(viewBox[3] - viewBox[1]) * scale;
  745. }
  746. this.transform = [rotateA * scale, rotateB * scale, rotateC * scale, rotateD * scale, offsetCanvasX - rotateA * scale * centerX - rotateC * scale * centerY, offsetCanvasY - rotateB * scale * centerX - rotateD * scale * centerY];
  747. this.width = width;
  748. this.height = height;
  749. this.fontScale = scale;
  750. }
  751. PageViewport.prototype = {
  752. clone: function PageViewPort_clone(args) {
  753. args = args || {};
  754. var scale = 'scale' in args ? args.scale : this.scale;
  755. var rotation = 'rotation' in args ? args.rotation : this.rotation;
  756. return new PageViewport(this.viewBox.slice(), scale, rotation, this.offsetX, this.offsetY, args.dontFlip);
  757. },
  758. convertToViewportPoint: function PageViewport_convertToViewportPoint(x, y) {
  759. return Util.applyTransform([x, y], this.transform);
  760. },
  761. convertToViewportRectangle: function PageViewport_convertToViewportRectangle(rect) {
  762. var tl = Util.applyTransform([rect[0], rect[1]], this.transform);
  763. var br = Util.applyTransform([rect[2], rect[3]], this.transform);
  764. return [tl[0], tl[1], br[0], br[1]];
  765. },
  766. convertToPdfPoint: function PageViewport_convertToPdfPoint(x, y) {
  767. return Util.applyInverseTransform([x, y], this.transform);
  768. }
  769. };
  770. return PageViewport;
  771. }();
  772. 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];
  773. function stringToPDFString(str) {
  774. var i,
  775. n = str.length,
  776. strBuf = [];
  777. if (str[0] === '\xFE' && str[1] === '\xFF') {
  778. for (i = 2; i < n; i += 2) {
  779. strBuf.push(String.fromCharCode(str.charCodeAt(i) << 8 | str.charCodeAt(i + 1)));
  780. }
  781. } else {
  782. for (i = 0; i < n; ++i) {
  783. var code = PDFStringTranslateTable[str.charCodeAt(i)];
  784. strBuf.push(code ? String.fromCharCode(code) : str.charAt(i));
  785. }
  786. }
  787. return strBuf.join('');
  788. }
  789. function stringToUTF8String(str) {
  790. return decodeURIComponent(escape(str));
  791. }
  792. function utf8StringToString(str) {
  793. return unescape(encodeURIComponent(str));
  794. }
  795. function isEmptyObj(obj) {
  796. for (var key in obj) {
  797. return false;
  798. }
  799. return true;
  800. }
  801. function isBool(v) {
  802. return typeof v === 'boolean';
  803. }
  804. function isNum(v) {
  805. return typeof v === 'number';
  806. }
  807. function isString(v) {
  808. return typeof v === 'string';
  809. }
  810. function isArrayBuffer(v) {
  811. return (typeof v === 'undefined' ? 'undefined' : _typeof(v)) === 'object' && v !== null && v.byteLength !== undefined;
  812. }
  813. function isSpace(ch) {
  814. return ch === 0x20 || ch === 0x09 || ch === 0x0D || ch === 0x0A;
  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 createBlob = function createBlob(data, contentType) {
  825. if (typeof Blob !== 'undefined') {
  826. return new Blob([data], { type: contentType });
  827. }
  828. throw new Error('The "Blob" constructor is not supported.');
  829. };
  830. var createObjectURL = function createObjectURLClosure() {
  831. var digits = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/=';
  832. return function createObjectURL(data, contentType) {
  833. var forceDataSchema = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : false;
  834. if (!forceDataSchema && URL.createObjectURL) {
  835. var blob = createBlob(data, contentType);
  836. return URL.createObjectURL(blob);
  837. }
  838. var buffer = 'data:' + contentType + ';base64,';
  839. for (var i = 0, ii = data.length; i < ii; i += 3) {
  840. var b1 = data[i] & 0xFF;
  841. var b2 = data[i + 1] & 0xFF;
  842. var b3 = data[i + 2] & 0xFF;
  843. var d1 = b1 >> 2,
  844. d2 = (b1 & 3) << 4 | b2 >> 4;
  845. var d3 = i + 1 < ii ? (b2 & 0xF) << 2 | b3 >> 6 : 64;
  846. var d4 = i + 2 < ii ? b3 & 0x3F : 64;
  847. buffer += digits[d1] + digits[d2] + digits[d3] + digits[d4];
  848. }
  849. return buffer;
  850. };
  851. }();
  852. function resolveCall(fn, args) {
  853. var thisArg = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : null;
  854. if (!fn) {
  855. return Promise.resolve(undefined);
  856. }
  857. return new Promise(function (resolve, reject) {
  858. resolve(fn.apply(thisArg, args));
  859. });
  860. }
  861. function wrapReason(reason) {
  862. if ((typeof reason === 'undefined' ? 'undefined' : _typeof(reason)) !== 'object') {
  863. return reason;
  864. }
  865. switch (reason.name) {
  866. case 'AbortException':
  867. return new AbortException(reason.message);
  868. case 'MissingPDFException':
  869. return new MissingPDFException(reason.message);
  870. case 'UnexpectedResponseException':
  871. return new UnexpectedResponseException(reason.message, reason.status);
  872. default:
  873. return new UnknownErrorException(reason.message, reason.details);
  874. }
  875. }
  876. function makeReasonSerializable(reason) {
  877. if (!(reason instanceof Error) || reason instanceof AbortException || reason instanceof MissingPDFException || reason instanceof UnexpectedResponseException || reason instanceof UnknownErrorException) {
  878. return reason;
  879. }
  880. return new UnknownErrorException(reason.message, reason.toString());
  881. }
  882. function resolveOrReject(capability, success, reason) {
  883. if (success) {
  884. capability.resolve();
  885. } else {
  886. capability.reject(reason);
  887. }
  888. }
  889. function finalize(promise) {
  890. return Promise.resolve(promise).catch(function () {});
  891. }
  892. function MessageHandler(sourceName, targetName, comObj) {
  893. var _this = this;
  894. this.sourceName = sourceName;
  895. this.targetName = targetName;
  896. this.comObj = comObj;
  897. this.callbackId = 1;
  898. this.streamId = 1;
  899. this.postMessageTransfers = true;
  900. this.streamSinks = Object.create(null);
  901. this.streamControllers = Object.create(null);
  902. var callbacksCapabilities = this.callbacksCapabilities = Object.create(null);
  903. var ah = this.actionHandler = Object.create(null);
  904. this._onComObjOnMessage = function (event) {
  905. var data = event.data;
  906. if (data.targetName !== _this.sourceName) {
  907. return;
  908. }
  909. if (data.stream) {
  910. _this._processStreamMessage(data);
  911. } else if (data.isReply) {
  912. var callbackId = data.callbackId;
  913. if (data.callbackId in callbacksCapabilities) {
  914. var callback = callbacksCapabilities[callbackId];
  915. delete callbacksCapabilities[callbackId];
  916. if ('error' in data) {
  917. callback.reject(wrapReason(data.error));
  918. } else {
  919. callback.resolve(data.data);
  920. }
  921. } else {
  922. throw new Error('Cannot resolve callback ' + callbackId);
  923. }
  924. } else if (data.action in ah) {
  925. var action = ah[data.action];
  926. if (data.callbackId) {
  927. var _sourceName = _this.sourceName;
  928. var _targetName = data.sourceName;
  929. Promise.resolve().then(function () {
  930. return action[0].call(action[1], data.data);
  931. }).then(function (result) {
  932. comObj.postMessage({
  933. sourceName: _sourceName,
  934. targetName: _targetName,
  935. isReply: true,
  936. callbackId: data.callbackId,
  937. data: result
  938. });
  939. }, function (reason) {
  940. comObj.postMessage({
  941. sourceName: _sourceName,
  942. targetName: _targetName,
  943. isReply: true,
  944. callbackId: data.callbackId,
  945. error: makeReasonSerializable(reason)
  946. });
  947. });
  948. } else if (data.streamId) {
  949. _this._createStreamSink(data);
  950. } else {
  951. action[0].call(action[1], data.data);
  952. }
  953. } else {
  954. throw new Error('Unknown action from worker: ' + data.action);
  955. }
  956. };
  957. comObj.addEventListener('message', this._onComObjOnMessage);
  958. }
  959. MessageHandler.prototype = {
  960. on: function on(actionName, handler, scope) {
  961. var ah = this.actionHandler;
  962. if (ah[actionName]) {
  963. throw new Error('There is already an actionName called "' + actionName + '"');
  964. }
  965. ah[actionName] = [handler, scope];
  966. },
  967. send: function send(actionName, data, transfers) {
  968. var message = {
  969. sourceName: this.sourceName,
  970. targetName: this.targetName,
  971. action: actionName,
  972. data: data
  973. };
  974. this.postMessage(message, transfers);
  975. },
  976. sendWithPromise: function sendWithPromise(actionName, data, transfers) {
  977. var callbackId = this.callbackId++;
  978. var message = {
  979. sourceName: this.sourceName,
  980. targetName: this.targetName,
  981. action: actionName,
  982. data: data,
  983. callbackId: callbackId
  984. };
  985. var capability = createPromiseCapability();
  986. this.callbacksCapabilities[callbackId] = capability;
  987. try {
  988. this.postMessage(message, transfers);
  989. } catch (e) {
  990. capability.reject(e);
  991. }
  992. return capability.promise;
  993. },
  994. sendWithStream: function sendWithStream(actionName, data, queueingStrategy, transfers) {
  995. var _this2 = this;
  996. var streamId = this.streamId++;
  997. var sourceName = this.sourceName;
  998. var targetName = this.targetName;
  999. return new _streams_polyfill.ReadableStream({
  1000. start: function start(controller) {
  1001. var startCapability = createPromiseCapability();
  1002. _this2.streamControllers[streamId] = {
  1003. controller: controller,
  1004. startCall: startCapability,
  1005. isClosed: false
  1006. };
  1007. _this2.postMessage({
  1008. sourceName: sourceName,
  1009. targetName: targetName,
  1010. action: actionName,
  1011. streamId: streamId,
  1012. data: data,
  1013. desiredSize: controller.desiredSize
  1014. });
  1015. return startCapability.promise;
  1016. },
  1017. pull: function pull(controller) {
  1018. var pullCapability = createPromiseCapability();
  1019. _this2.streamControllers[streamId].pullCall = pullCapability;
  1020. _this2.postMessage({
  1021. sourceName: sourceName,
  1022. targetName: targetName,
  1023. stream: 'pull',
  1024. streamId: streamId,
  1025. desiredSize: controller.desiredSize
  1026. });
  1027. return pullCapability.promise;
  1028. },
  1029. cancel: function cancel(reason) {
  1030. var cancelCapability = createPromiseCapability();
  1031. _this2.streamControllers[streamId].cancelCall = cancelCapability;
  1032. _this2.streamControllers[streamId].isClosed = true;
  1033. _this2.postMessage({
  1034. sourceName: sourceName,
  1035. targetName: targetName,
  1036. stream: 'cancel',
  1037. reason: reason,
  1038. streamId: streamId
  1039. });
  1040. return cancelCapability.promise;
  1041. }
  1042. }, queueingStrategy);
  1043. },
  1044. _createStreamSink: function _createStreamSink(data) {
  1045. var _this3 = this;
  1046. var self = this;
  1047. var action = this.actionHandler[data.action];
  1048. var streamId = data.streamId;
  1049. var desiredSize = data.desiredSize;
  1050. var sourceName = this.sourceName;
  1051. var targetName = data.sourceName;
  1052. var capability = createPromiseCapability();
  1053. var sendStreamRequest = function sendStreamRequest(_ref) {
  1054. var stream = _ref.stream,
  1055. chunk = _ref.chunk,
  1056. transfers = _ref.transfers,
  1057. success = _ref.success,
  1058. reason = _ref.reason;
  1059. _this3.postMessage({
  1060. sourceName: sourceName,
  1061. targetName: targetName,
  1062. stream: stream,
  1063. streamId: streamId,
  1064. chunk: chunk,
  1065. success: success,
  1066. reason: reason
  1067. }, transfers);
  1068. };
  1069. var streamSink = {
  1070. enqueue: function enqueue(chunk) {
  1071. var size = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : 1;
  1072. var transfers = arguments[2];
  1073. if (this.isCancelled) {
  1074. return;
  1075. }
  1076. var lastDesiredSize = this.desiredSize;
  1077. this.desiredSize -= size;
  1078. if (lastDesiredSize > 0 && this.desiredSize <= 0) {
  1079. this.sinkCapability = createPromiseCapability();
  1080. this.ready = this.sinkCapability.promise;
  1081. }
  1082. sendStreamRequest({
  1083. stream: 'enqueue',
  1084. chunk: chunk,
  1085. transfers: transfers
  1086. });
  1087. },
  1088. close: function close() {
  1089. if (this.isCancelled) {
  1090. return;
  1091. }
  1092. this.isCancelled = true;
  1093. sendStreamRequest({ stream: 'close' });
  1094. delete self.streamSinks[streamId];
  1095. },
  1096. error: function error(reason) {
  1097. if (this.isCancelled) {
  1098. return;
  1099. }
  1100. this.isCancelled = true;
  1101. sendStreamRequest({
  1102. stream: 'error',
  1103. reason: reason
  1104. });
  1105. },
  1106. sinkCapability: capability,
  1107. onPull: null,
  1108. onCancel: null,
  1109. isCancelled: false,
  1110. desiredSize: desiredSize,
  1111. ready: null
  1112. };
  1113. streamSink.sinkCapability.resolve();
  1114. streamSink.ready = streamSink.sinkCapability.promise;
  1115. this.streamSinks[streamId] = streamSink;
  1116. resolveCall(action[0], [data.data, streamSink], action[1]).then(function () {
  1117. sendStreamRequest({
  1118. stream: 'start_complete',
  1119. success: true
  1120. });
  1121. }, function (reason) {
  1122. sendStreamRequest({
  1123. stream: 'start_complete',
  1124. success: false,
  1125. reason: reason
  1126. });
  1127. });
  1128. },
  1129. _processStreamMessage: function _processStreamMessage(data) {
  1130. var _this4 = this;
  1131. var sourceName = this.sourceName;
  1132. var targetName = data.sourceName;
  1133. var streamId = data.streamId;
  1134. var sendStreamResponse = function sendStreamResponse(_ref2) {
  1135. var stream = _ref2.stream,
  1136. success = _ref2.success,
  1137. reason = _ref2.reason;
  1138. _this4.comObj.postMessage({
  1139. sourceName: sourceName,
  1140. targetName: targetName,
  1141. stream: stream,
  1142. success: success,
  1143. streamId: streamId,
  1144. reason: reason
  1145. });
  1146. };
  1147. var deleteStreamController = function deleteStreamController() {
  1148. Promise.all([_this4.streamControllers[data.streamId].startCall, _this4.streamControllers[data.streamId].pullCall, _this4.streamControllers[data.streamId].cancelCall].map(function (capability) {
  1149. return capability && finalize(capability.promise);
  1150. })).then(function () {
  1151. delete _this4.streamControllers[data.streamId];
  1152. });
  1153. };
  1154. switch (data.stream) {
  1155. case 'start_complete':
  1156. resolveOrReject(this.streamControllers[data.streamId].startCall, data.success, wrapReason(data.reason));
  1157. break;
  1158. case 'pull_complete':
  1159. resolveOrReject(this.streamControllers[data.streamId].pullCall, data.success, wrapReason(data.reason));
  1160. break;
  1161. case 'pull':
  1162. if (!this.streamSinks[data.streamId]) {
  1163. sendStreamResponse({
  1164. stream: 'pull_complete',
  1165. success: true
  1166. });
  1167. break;
  1168. }
  1169. if (this.streamSinks[data.streamId].desiredSize <= 0 && data.desiredSize > 0) {
  1170. this.streamSinks[data.streamId].sinkCapability.resolve();
  1171. }
  1172. this.streamSinks[data.streamId].desiredSize = data.desiredSize;
  1173. resolveCall(this.streamSinks[data.streamId].onPull).then(function () {
  1174. sendStreamResponse({
  1175. stream: 'pull_complete',
  1176. success: true
  1177. });
  1178. }, function (reason) {
  1179. sendStreamResponse({
  1180. stream: 'pull_complete',
  1181. success: false,
  1182. reason: reason
  1183. });
  1184. });
  1185. break;
  1186. case 'enqueue':
  1187. assert(this.streamControllers[data.streamId], 'enqueue should have stream controller');
  1188. if (!this.streamControllers[data.streamId].isClosed) {
  1189. this.streamControllers[data.streamId].controller.enqueue(data.chunk);
  1190. }
  1191. break;
  1192. case 'close':
  1193. assert(this.streamControllers[data.streamId], 'close should have stream controller');
  1194. if (this.streamControllers[data.streamId].isClosed) {
  1195. break;
  1196. }
  1197. this.streamControllers[data.streamId].isClosed = true;
  1198. this.streamControllers[data.streamId].controller.close();
  1199. deleteStreamController();
  1200. break;
  1201. case 'error':
  1202. assert(this.streamControllers[data.streamId], 'error should have stream controller');
  1203. this.streamControllers[data.streamId].controller.error(wrapReason(data.reason));
  1204. deleteStreamController();
  1205. break;
  1206. case 'cancel_complete':
  1207. resolveOrReject(this.streamControllers[data.streamId].cancelCall, data.success, wrapReason(data.reason));
  1208. deleteStreamController();
  1209. break;
  1210. case 'cancel':
  1211. if (!this.streamSinks[data.streamId]) {
  1212. break;
  1213. }
  1214. resolveCall(this.streamSinks[data.streamId].onCancel, [wrapReason(data.reason)]).then(function () {
  1215. sendStreamResponse({
  1216. stream: 'cancel_complete',
  1217. success: true
  1218. });
  1219. }, function (reason) {
  1220. sendStreamResponse({
  1221. stream: 'cancel_complete',
  1222. success: false,
  1223. reason: reason
  1224. });
  1225. });
  1226. this.streamSinks[data.streamId].sinkCapability.reject(wrapReason(data.reason));
  1227. this.streamSinks[data.streamId].isCancelled = true;
  1228. delete this.streamSinks[data.streamId];
  1229. break;
  1230. default:
  1231. throw new Error('Unexpected stream case');
  1232. }
  1233. },
  1234. postMessage: function postMessage(message, transfers) {
  1235. if (transfers && this.postMessageTransfers) {
  1236. this.comObj.postMessage(message, transfers);
  1237. } else {
  1238. this.comObj.postMessage(message);
  1239. }
  1240. },
  1241. destroy: function destroy() {
  1242. this.comObj.removeEventListener('message', this._onComObjOnMessage);
  1243. }
  1244. };
  1245. function loadJpegStream(id, imageUrl, objs) {
  1246. var img = new Image();
  1247. img.onload = function loadJpegStream_onloadClosure() {
  1248. objs.resolve(id, img);
  1249. };
  1250. img.onerror = function loadJpegStream_onerrorClosure() {
  1251. objs.resolve(id, null);
  1252. warn('Error during JPEG image loading');
  1253. };
  1254. img.src = imageUrl;
  1255. }
  1256. exports.FONT_IDENTITY_MATRIX = FONT_IDENTITY_MATRIX;
  1257. exports.IDENTITY_MATRIX = IDENTITY_MATRIX;
  1258. exports.OPS = OPS;
  1259. exports.VERBOSITY_LEVELS = VERBOSITY_LEVELS;
  1260. exports.UNSUPPORTED_FEATURES = UNSUPPORTED_FEATURES;
  1261. exports.AnnotationBorderStyleType = AnnotationBorderStyleType;
  1262. exports.AnnotationFieldFlag = AnnotationFieldFlag;
  1263. exports.AnnotationFlag = AnnotationFlag;
  1264. exports.AnnotationType = AnnotationType;
  1265. exports.FontType = FontType;
  1266. exports.ImageKind = ImageKind;
  1267. exports.CMapCompressionType = CMapCompressionType;
  1268. exports.AbortException = AbortException;
  1269. exports.InvalidPDFException = InvalidPDFException;
  1270. exports.MessageHandler = MessageHandler;
  1271. exports.MissingDataException = MissingDataException;
  1272. exports.MissingPDFException = MissingPDFException;
  1273. exports.NativeImageDecoding = NativeImageDecoding;
  1274. exports.NotImplementedException = NotImplementedException;
  1275. exports.PageViewport = PageViewport;
  1276. exports.PasswordException = PasswordException;
  1277. exports.PasswordResponses = PasswordResponses;
  1278. exports.StreamType = StreamType;
  1279. exports.TextRenderingMode = TextRenderingMode;
  1280. exports.UnexpectedResponseException = UnexpectedResponseException;
  1281. exports.UnknownErrorException = UnknownErrorException;
  1282. exports.Util = Util;
  1283. exports.XRefParseException = XRefParseException;
  1284. exports.FormatError = FormatError;
  1285. exports.arrayByteLength = arrayByteLength;
  1286. exports.arraysToBytes = arraysToBytes;
  1287. exports.assert = assert;
  1288. exports.bytesToString = bytesToString;
  1289. exports.createBlob = createBlob;
  1290. exports.createPromiseCapability = createPromiseCapability;
  1291. exports.createObjectURL = createObjectURL;
  1292. exports.deprecated = deprecated;
  1293. exports.getLookupTableFactory = getLookupTableFactory;
  1294. exports.getVerbosityLevel = getVerbosityLevel;
  1295. exports.info = info;
  1296. exports.isArrayBuffer = isArrayBuffer;
  1297. exports.isBool = isBool;
  1298. exports.isEmptyObj = isEmptyObj;
  1299. exports.isNum = isNum;
  1300. exports.isString = isString;
  1301. exports.isSpace = isSpace;
  1302. exports.isSameOrigin = isSameOrigin;
  1303. exports.createValidAbsoluteUrl = createValidAbsoluteUrl;
  1304. exports.isLittleEndian = isLittleEndian;
  1305. exports.isEvalSupported = isEvalSupported;
  1306. exports.loadJpegStream = loadJpegStream;
  1307. exports.log2 = log2;
  1308. exports.readInt8 = readInt8;
  1309. exports.readUint16 = readUint16;
  1310. exports.readUint32 = readUint32;
  1311. exports.removeNullCharacters = removeNullCharacters;
  1312. exports.ReadableStream = _streams_polyfill.ReadableStream;
  1313. exports.setVerbosityLevel = setVerbosityLevel;
  1314. exports.shadow = shadow;
  1315. exports.string32 = string32;
  1316. exports.stringToBytes = stringToBytes;
  1317. exports.stringToPDFString = stringToPDFString;
  1318. exports.stringToUTF8String = stringToUTF8String;
  1319. exports.utf8StringToString = utf8StringToString;
  1320. exports.warn = warn;
  1321. exports.unreachable = unreachable;