util.js 44 KB

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