2
0

util.js 44 KB

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