2
0

util.js 43 KB

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