util.js 45 KB

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