util.js 42 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333
  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.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. if (x <= 0) {
  510. return 0;
  511. }
  512. return Math.ceil(Math.log2(x));
  513. }
  514. function readInt8(data, start) {
  515. return data[start] << 24 >> 24;
  516. }
  517. function readUint16(data, offset) {
  518. return data[offset] << 8 | data[offset + 1];
  519. }
  520. function readUint32(data, offset) {
  521. return (data[offset] << 24 | data[offset + 1] << 16 | data[offset + 2] << 8 | data[offset + 3]) >>> 0;
  522. }
  523. function isLittleEndian() {
  524. var buffer8 = new Uint8Array(4);
  525. buffer8[0] = 1;
  526. var view32 = new Uint32Array(buffer8.buffer, 0, 1);
  527. return view32[0] === 1;
  528. }
  529. function isEvalSupported() {
  530. try {
  531. new Function('');
  532. return true;
  533. } catch (e) {
  534. return false;
  535. }
  536. }
  537. var IDENTITY_MATRIX = [1, 0, 0, 1, 0, 0];
  538. var Util = function UtilClosure() {
  539. function Util() {}
  540. var rgbBuf = ['rgb(', 0, ',', 0, ',', 0, ')'];
  541. Util.makeCssRgb = function Util_makeCssRgb(r, g, b) {
  542. rgbBuf[1] = r;
  543. rgbBuf[3] = g;
  544. rgbBuf[5] = b;
  545. return rgbBuf.join('');
  546. };
  547. Util.transform = function Util_transform(m1, m2) {
  548. 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]];
  549. };
  550. Util.applyTransform = function Util_applyTransform(p, m) {
  551. var xt = p[0] * m[0] + p[1] * m[2] + m[4];
  552. var yt = p[0] * m[1] + p[1] * m[3] + m[5];
  553. return [xt, yt];
  554. };
  555. Util.applyInverseTransform = function Util_applyInverseTransform(p, m) {
  556. var d = m[0] * m[3] - m[1] * m[2];
  557. var xt = (p[0] * m[3] - p[1] * m[2] + m[2] * m[5] - m[4] * m[3]) / d;
  558. var yt = (-p[0] * m[1] + p[1] * m[0] + m[4] * m[1] - m[5] * m[0]) / d;
  559. return [xt, yt];
  560. };
  561. Util.getAxialAlignedBoundingBox = function Util_getAxialAlignedBoundingBox(r, m) {
  562. var p1 = Util.applyTransform(r, m);
  563. var p2 = Util.applyTransform(r.slice(2, 4), m);
  564. var p3 = Util.applyTransform([r[0], r[3]], m);
  565. var p4 = Util.applyTransform([r[2], r[1]], m);
  566. 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])];
  567. };
  568. Util.inverseTransform = function Util_inverseTransform(m) {
  569. var d = m[0] * m[3] - m[1] * m[2];
  570. 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];
  571. };
  572. Util.apply3dTransform = function Util_apply3dTransform(m, v) {
  573. 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]];
  574. };
  575. Util.singularValueDecompose2dScale = function Util_singularValueDecompose2dScale(m) {
  576. var transpose = [m[0], m[2], m[1], m[3]];
  577. var a = m[0] * transpose[0] + m[1] * transpose[2];
  578. var b = m[0] * transpose[1] + m[1] * transpose[3];
  579. var c = m[2] * transpose[0] + m[3] * transpose[2];
  580. var d = m[2] * transpose[1] + m[3] * transpose[3];
  581. var first = (a + d) / 2;
  582. var second = Math.sqrt((a + d) * (a + d) - 4 * (a * d - c * b)) / 2;
  583. var sx = first + second || 1;
  584. var sy = first - second || 1;
  585. return [Math.sqrt(sx), Math.sqrt(sy)];
  586. };
  587. Util.normalizeRect = function Util_normalizeRect(rect) {
  588. var r = rect.slice(0);
  589. if (rect[0] > rect[2]) {
  590. r[0] = rect[2];
  591. r[2] = rect[0];
  592. }
  593. if (rect[1] > rect[3]) {
  594. r[1] = rect[3];
  595. r[3] = rect[1];
  596. }
  597. return r;
  598. };
  599. Util.intersect = function Util_intersect(rect1, rect2) {
  600. function compare(a, b) {
  601. return a - b;
  602. }
  603. var orderedX = [rect1[0], rect1[2], rect2[0], rect2[2]].sort(compare),
  604. orderedY = [rect1[1], rect1[3], rect2[1], rect2[3]].sort(compare),
  605. result = [];
  606. rect1 = Util.normalizeRect(rect1);
  607. rect2 = Util.normalizeRect(rect2);
  608. if (orderedX[0] === rect1[0] && orderedX[1] === rect2[0] || orderedX[0] === rect2[0] && orderedX[1] === rect1[0]) {
  609. result[0] = orderedX[1];
  610. result[2] = orderedX[2];
  611. } else {
  612. return false;
  613. }
  614. if (orderedY[0] === rect1[1] && orderedY[1] === rect2[1] || orderedY[0] === rect2[1] && orderedY[1] === rect1[1]) {
  615. result[1] = orderedY[1];
  616. result[3] = orderedY[2];
  617. } else {
  618. return false;
  619. }
  620. return result;
  621. };
  622. 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'];
  623. Util.toRoman = function Util_toRoman(number, lowerCase) {
  624. assert(Number.isInteger(number) && number > 0, 'The number should be a positive integer.');
  625. var pos,
  626. romanBuf = [];
  627. while (number >= 1000) {
  628. number -= 1000;
  629. romanBuf.push('M');
  630. }
  631. pos = number / 100 | 0;
  632. number %= 100;
  633. romanBuf.push(ROMAN_NUMBER_MAP[pos]);
  634. pos = number / 10 | 0;
  635. number %= 10;
  636. romanBuf.push(ROMAN_NUMBER_MAP[10 + pos]);
  637. romanBuf.push(ROMAN_NUMBER_MAP[20 + number]);
  638. var romanStr = romanBuf.join('');
  639. return lowerCase ? romanStr.toLowerCase() : romanStr;
  640. };
  641. Util.appendToArray = function Util_appendToArray(arr1, arr2) {
  642. Array.prototype.push.apply(arr1, arr2);
  643. };
  644. Util.prependToArray = function Util_prependToArray(arr1, arr2) {
  645. Array.prototype.unshift.apply(arr1, arr2);
  646. };
  647. Util.extendObj = function extendObj(obj1, obj2) {
  648. for (var key in obj2) {
  649. obj1[key] = obj2[key];
  650. }
  651. };
  652. Util.getInheritableProperty = function Util_getInheritableProperty(dict, name, getArray) {
  653. while (dict && !dict.has(name)) {
  654. dict = dict.get('Parent');
  655. }
  656. if (!dict) {
  657. return null;
  658. }
  659. return getArray ? dict.getArray(name) : dict.get(name);
  660. };
  661. Util.inherit = function Util_inherit(sub, base, prototype) {
  662. sub.prototype = Object.create(base.prototype);
  663. sub.prototype.constructor = sub;
  664. for (var prop in prototype) {
  665. sub.prototype[prop] = prototype[prop];
  666. }
  667. };
  668. Util.loadScript = function Util_loadScript(src, callback) {
  669. var script = document.createElement('script');
  670. var loaded = false;
  671. script.setAttribute('src', src);
  672. if (callback) {
  673. script.onload = function () {
  674. if (!loaded) {
  675. callback();
  676. }
  677. loaded = true;
  678. };
  679. }
  680. document.getElementsByTagName('head')[0].appendChild(script);
  681. };
  682. return Util;
  683. }();
  684. var PageViewport = function PageViewportClosure() {
  685. function PageViewport(viewBox, scale, rotation, offsetX, offsetY, dontFlip) {
  686. this.viewBox = viewBox;
  687. this.scale = scale;
  688. this.rotation = rotation;
  689. this.offsetX = offsetX;
  690. this.offsetY = offsetY;
  691. var centerX = (viewBox[2] + viewBox[0]) / 2;
  692. var centerY = (viewBox[3] + viewBox[1]) / 2;
  693. var rotateA, rotateB, rotateC, rotateD;
  694. rotation = rotation % 360;
  695. rotation = rotation < 0 ? rotation + 360 : rotation;
  696. switch (rotation) {
  697. case 180:
  698. rotateA = -1;
  699. rotateB = 0;
  700. rotateC = 0;
  701. rotateD = 1;
  702. break;
  703. case 90:
  704. rotateA = 0;
  705. rotateB = 1;
  706. rotateC = 1;
  707. rotateD = 0;
  708. break;
  709. case 270:
  710. rotateA = 0;
  711. rotateB = -1;
  712. rotateC = -1;
  713. rotateD = 0;
  714. break;
  715. default:
  716. rotateA = 1;
  717. rotateB = 0;
  718. rotateC = 0;
  719. rotateD = -1;
  720. break;
  721. }
  722. if (dontFlip) {
  723. rotateC = -rotateC;
  724. rotateD = -rotateD;
  725. }
  726. var offsetCanvasX, offsetCanvasY;
  727. var width, height;
  728. if (rotateA === 0) {
  729. offsetCanvasX = Math.abs(centerY - viewBox[1]) * scale + offsetX;
  730. offsetCanvasY = Math.abs(centerX - viewBox[0]) * scale + offsetY;
  731. width = Math.abs(viewBox[3] - viewBox[1]) * scale;
  732. height = Math.abs(viewBox[2] - viewBox[0]) * scale;
  733. } else {
  734. offsetCanvasX = Math.abs(centerX - viewBox[0]) * scale + offsetX;
  735. offsetCanvasY = Math.abs(centerY - viewBox[1]) * scale + offsetY;
  736. width = Math.abs(viewBox[2] - viewBox[0]) * scale;
  737. height = Math.abs(viewBox[3] - viewBox[1]) * scale;
  738. }
  739. this.transform = [rotateA * scale, rotateB * scale, rotateC * scale, rotateD * scale, offsetCanvasX - rotateA * scale * centerX - rotateC * scale * centerY, offsetCanvasY - rotateB * scale * centerX - rotateD * scale * centerY];
  740. this.width = width;
  741. this.height = height;
  742. this.fontScale = scale;
  743. }
  744. PageViewport.prototype = {
  745. clone: function PageViewPort_clone(args) {
  746. args = args || {};
  747. var scale = 'scale' in args ? args.scale : this.scale;
  748. var rotation = 'rotation' in args ? args.rotation : this.rotation;
  749. return new PageViewport(this.viewBox.slice(), scale, rotation, this.offsetX, this.offsetY, args.dontFlip);
  750. },
  751. convertToViewportPoint: function PageViewport_convertToViewportPoint(x, y) {
  752. return Util.applyTransform([x, y], this.transform);
  753. },
  754. convertToViewportRectangle: function PageViewport_convertToViewportRectangle(rect) {
  755. var tl = Util.applyTransform([rect[0], rect[1]], this.transform);
  756. var br = Util.applyTransform([rect[2], rect[3]], this.transform);
  757. return [tl[0], tl[1], br[0], br[1]];
  758. },
  759. convertToPdfPoint: function PageViewport_convertToPdfPoint(x, y) {
  760. return Util.applyInverseTransform([x, y], this.transform);
  761. }
  762. };
  763. return PageViewport;
  764. }();
  765. 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];
  766. function stringToPDFString(str) {
  767. var i,
  768. n = str.length,
  769. strBuf = [];
  770. if (str[0] === '\xFE' && str[1] === '\xFF') {
  771. for (i = 2; i < n; i += 2) {
  772. strBuf.push(String.fromCharCode(str.charCodeAt(i) << 8 | str.charCodeAt(i + 1)));
  773. }
  774. } else {
  775. for (i = 0; i < n; ++i) {
  776. var code = PDFStringTranslateTable[str.charCodeAt(i)];
  777. strBuf.push(code ? String.fromCharCode(code) : str.charAt(i));
  778. }
  779. }
  780. return strBuf.join('');
  781. }
  782. function stringToUTF8String(str) {
  783. return decodeURIComponent(escape(str));
  784. }
  785. function utf8StringToString(str) {
  786. return unescape(encodeURIComponent(str));
  787. }
  788. function isEmptyObj(obj) {
  789. for (var key in obj) {
  790. return false;
  791. }
  792. return true;
  793. }
  794. function isBool(v) {
  795. return typeof v === 'boolean';
  796. }
  797. function isNum(v) {
  798. return typeof v === 'number';
  799. }
  800. function isString(v) {
  801. return typeof v === 'string';
  802. }
  803. function isArrayBuffer(v) {
  804. return (typeof v === 'undefined' ? 'undefined' : _typeof(v)) === 'object' && v !== null && v.byteLength !== undefined;
  805. }
  806. function isSpace(ch) {
  807. return ch === 0x20 || ch === 0x09 || ch === 0x0D || ch === 0x0A;
  808. }
  809. function isNodeJS() {
  810. return (typeof process === 'undefined' ? 'undefined' : _typeof(process)) === 'object' && process + '' === '[object process]';
  811. }
  812. function createPromiseCapability() {
  813. var capability = {};
  814. capability.promise = new Promise(function (resolve, reject) {
  815. capability.resolve = resolve;
  816. capability.reject = reject;
  817. });
  818. return capability;
  819. }
  820. var createBlob = function createBlob(data, contentType) {
  821. if (typeof Blob !== 'undefined') {
  822. return new Blob([data], { type: contentType });
  823. }
  824. throw new Error('The "Blob" constructor is not supported.');
  825. };
  826. var createObjectURL = function createObjectURLClosure() {
  827. var digits = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/=';
  828. return function createObjectURL(data, contentType) {
  829. var forceDataSchema = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : false;
  830. if (!forceDataSchema && URL.createObjectURL) {
  831. var blob = createBlob(data, contentType);
  832. return URL.createObjectURL(blob);
  833. }
  834. var buffer = 'data:' + contentType + ';base64,';
  835. for (var i = 0, ii = data.length; i < ii; i += 3) {
  836. var b1 = data[i] & 0xFF;
  837. var b2 = data[i + 1] & 0xFF;
  838. var b3 = data[i + 2] & 0xFF;
  839. var d1 = b1 >> 2,
  840. d2 = (b1 & 3) << 4 | b2 >> 4;
  841. var d3 = i + 1 < ii ? (b2 & 0xF) << 2 | b3 >> 6 : 64;
  842. var d4 = i + 2 < ii ? b3 & 0x3F : 64;
  843. buffer += digits[d1] + digits[d2] + digits[d3] + digits[d4];
  844. }
  845. return buffer;
  846. };
  847. }();
  848. function resolveCall(fn, args) {
  849. var thisArg = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : null;
  850. if (!fn) {
  851. return Promise.resolve(undefined);
  852. }
  853. return new Promise(function (resolve, reject) {
  854. resolve(fn.apply(thisArg, args));
  855. });
  856. }
  857. function wrapReason(reason) {
  858. if ((typeof reason === 'undefined' ? 'undefined' : _typeof(reason)) !== 'object') {
  859. return reason;
  860. }
  861. switch (reason.name) {
  862. case 'AbortException':
  863. return new AbortException(reason.message);
  864. case 'MissingPDFException':
  865. return new MissingPDFException(reason.message);
  866. case 'UnexpectedResponseException':
  867. return new UnexpectedResponseException(reason.message, reason.status);
  868. default:
  869. return new UnknownErrorException(reason.message, reason.details);
  870. }
  871. }
  872. function makeReasonSerializable(reason) {
  873. if (!(reason instanceof Error) || reason instanceof AbortException || reason instanceof MissingPDFException || reason instanceof UnexpectedResponseException || reason instanceof UnknownErrorException) {
  874. return reason;
  875. }
  876. return new UnknownErrorException(reason.message, reason.toString());
  877. }
  878. function resolveOrReject(capability, success, reason) {
  879. if (success) {
  880. capability.resolve();
  881. } else {
  882. capability.reject(reason);
  883. }
  884. }
  885. function finalize(promise) {
  886. return Promise.resolve(promise).catch(function () {});
  887. }
  888. function MessageHandler(sourceName, targetName, comObj) {
  889. var _this = this;
  890. this.sourceName = sourceName;
  891. this.targetName = targetName;
  892. this.comObj = comObj;
  893. this.callbackId = 1;
  894. this.streamId = 1;
  895. this.postMessageTransfers = true;
  896. this.streamSinks = Object.create(null);
  897. this.streamControllers = Object.create(null);
  898. var callbacksCapabilities = this.callbacksCapabilities = Object.create(null);
  899. var ah = this.actionHandler = Object.create(null);
  900. this._onComObjOnMessage = function (event) {
  901. var data = event.data;
  902. if (data.targetName !== _this.sourceName) {
  903. return;
  904. }
  905. if (data.stream) {
  906. _this._processStreamMessage(data);
  907. } else if (data.isReply) {
  908. var callbackId = data.callbackId;
  909. if (data.callbackId in callbacksCapabilities) {
  910. var callback = callbacksCapabilities[callbackId];
  911. delete callbacksCapabilities[callbackId];
  912. if ('error' in data) {
  913. callback.reject(wrapReason(data.error));
  914. } else {
  915. callback.resolve(data.data);
  916. }
  917. } else {
  918. throw new Error('Cannot resolve callback ' + callbackId);
  919. }
  920. } else if (data.action in ah) {
  921. var action = ah[data.action];
  922. if (data.callbackId) {
  923. var _sourceName = _this.sourceName;
  924. var _targetName = data.sourceName;
  925. Promise.resolve().then(function () {
  926. return action[0].call(action[1], data.data);
  927. }).then(function (result) {
  928. comObj.postMessage({
  929. sourceName: _sourceName,
  930. targetName: _targetName,
  931. isReply: true,
  932. callbackId: data.callbackId,
  933. data: result
  934. });
  935. }, function (reason) {
  936. comObj.postMessage({
  937. sourceName: _sourceName,
  938. targetName: _targetName,
  939. isReply: true,
  940. callbackId: data.callbackId,
  941. error: makeReasonSerializable(reason)
  942. });
  943. });
  944. } else if (data.streamId) {
  945. _this._createStreamSink(data);
  946. } else {
  947. action[0].call(action[1], data.data);
  948. }
  949. } else {
  950. throw new Error('Unknown action from worker: ' + data.action);
  951. }
  952. };
  953. comObj.addEventListener('message', this._onComObjOnMessage);
  954. }
  955. MessageHandler.prototype = {
  956. on: function on(actionName, handler, scope) {
  957. var ah = this.actionHandler;
  958. if (ah[actionName]) {
  959. throw new Error('There is already an actionName called "' + actionName + '"');
  960. }
  961. ah[actionName] = [handler, scope];
  962. },
  963. send: function send(actionName, data, transfers) {
  964. var message = {
  965. sourceName: this.sourceName,
  966. targetName: this.targetName,
  967. action: actionName,
  968. data: data
  969. };
  970. this.postMessage(message, transfers);
  971. },
  972. sendWithPromise: function sendWithPromise(actionName, data, transfers) {
  973. var callbackId = this.callbackId++;
  974. var message = {
  975. sourceName: this.sourceName,
  976. targetName: this.targetName,
  977. action: actionName,
  978. data: data,
  979. callbackId: callbackId
  980. };
  981. var capability = createPromiseCapability();
  982. this.callbacksCapabilities[callbackId] = capability;
  983. try {
  984. this.postMessage(message, transfers);
  985. } catch (e) {
  986. capability.reject(e);
  987. }
  988. return capability.promise;
  989. },
  990. sendWithStream: function sendWithStream(actionName, data, queueingStrategy, transfers) {
  991. var _this2 = this;
  992. var streamId = this.streamId++;
  993. var sourceName = this.sourceName;
  994. var targetName = this.targetName;
  995. return new _streams_polyfill.ReadableStream({
  996. start: function start(controller) {
  997. var startCapability = createPromiseCapability();
  998. _this2.streamControllers[streamId] = {
  999. controller: controller,
  1000. startCall: startCapability,
  1001. isClosed: false
  1002. };
  1003. _this2.postMessage({
  1004. sourceName: sourceName,
  1005. targetName: targetName,
  1006. action: actionName,
  1007. streamId: streamId,
  1008. data: data,
  1009. desiredSize: controller.desiredSize
  1010. });
  1011. return startCapability.promise;
  1012. },
  1013. pull: function pull(controller) {
  1014. var pullCapability = createPromiseCapability();
  1015. _this2.streamControllers[streamId].pullCall = pullCapability;
  1016. _this2.postMessage({
  1017. sourceName: sourceName,
  1018. targetName: targetName,
  1019. stream: 'pull',
  1020. streamId: streamId,
  1021. desiredSize: controller.desiredSize
  1022. });
  1023. return pullCapability.promise;
  1024. },
  1025. cancel: function cancel(reason) {
  1026. var cancelCapability = createPromiseCapability();
  1027. _this2.streamControllers[streamId].cancelCall = cancelCapability;
  1028. _this2.streamControllers[streamId].isClosed = true;
  1029. _this2.postMessage({
  1030. sourceName: sourceName,
  1031. targetName: targetName,
  1032. stream: 'cancel',
  1033. reason: reason,
  1034. streamId: streamId
  1035. });
  1036. return cancelCapability.promise;
  1037. }
  1038. }, queueingStrategy);
  1039. },
  1040. _createStreamSink: function _createStreamSink(data) {
  1041. var _this3 = this;
  1042. var self = this;
  1043. var action = this.actionHandler[data.action];
  1044. var streamId = data.streamId;
  1045. var desiredSize = data.desiredSize;
  1046. var sourceName = this.sourceName;
  1047. var targetName = data.sourceName;
  1048. var capability = createPromiseCapability();
  1049. var sendStreamRequest = function sendStreamRequest(_ref) {
  1050. var stream = _ref.stream,
  1051. chunk = _ref.chunk,
  1052. transfers = _ref.transfers,
  1053. success = _ref.success,
  1054. reason = _ref.reason;
  1055. _this3.postMessage({
  1056. sourceName: sourceName,
  1057. targetName: targetName,
  1058. stream: stream,
  1059. streamId: streamId,
  1060. chunk: chunk,
  1061. success: success,
  1062. reason: reason
  1063. }, transfers);
  1064. };
  1065. var streamSink = {
  1066. enqueue: function enqueue(chunk) {
  1067. var size = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : 1;
  1068. var transfers = arguments[2];
  1069. if (this.isCancelled) {
  1070. return;
  1071. }
  1072. var lastDesiredSize = this.desiredSize;
  1073. this.desiredSize -= size;
  1074. if (lastDesiredSize > 0 && this.desiredSize <= 0) {
  1075. this.sinkCapability = createPromiseCapability();
  1076. this.ready = this.sinkCapability.promise;
  1077. }
  1078. sendStreamRequest({
  1079. stream: 'enqueue',
  1080. chunk: chunk,
  1081. transfers: transfers
  1082. });
  1083. },
  1084. close: function close() {
  1085. if (this.isCancelled) {
  1086. return;
  1087. }
  1088. this.isCancelled = true;
  1089. sendStreamRequest({ stream: 'close' });
  1090. delete self.streamSinks[streamId];
  1091. },
  1092. error: function error(reason) {
  1093. if (this.isCancelled) {
  1094. return;
  1095. }
  1096. this.isCancelled = true;
  1097. sendStreamRequest({
  1098. stream: 'error',
  1099. reason: reason
  1100. });
  1101. },
  1102. sinkCapability: capability,
  1103. onPull: null,
  1104. onCancel: null,
  1105. isCancelled: false,
  1106. desiredSize: desiredSize,
  1107. ready: null
  1108. };
  1109. streamSink.sinkCapability.resolve();
  1110. streamSink.ready = streamSink.sinkCapability.promise;
  1111. this.streamSinks[streamId] = streamSink;
  1112. resolveCall(action[0], [data.data, streamSink], action[1]).then(function () {
  1113. sendStreamRequest({
  1114. stream: 'start_complete',
  1115. success: true
  1116. });
  1117. }, function (reason) {
  1118. sendStreamRequest({
  1119. stream: 'start_complete',
  1120. success: false,
  1121. reason: reason
  1122. });
  1123. });
  1124. },
  1125. _processStreamMessage: function _processStreamMessage(data) {
  1126. var _this4 = this;
  1127. var sourceName = this.sourceName;
  1128. var targetName = data.sourceName;
  1129. var streamId = data.streamId;
  1130. var sendStreamResponse = function sendStreamResponse(_ref2) {
  1131. var stream = _ref2.stream,
  1132. success = _ref2.success,
  1133. reason = _ref2.reason;
  1134. _this4.comObj.postMessage({
  1135. sourceName: sourceName,
  1136. targetName: targetName,
  1137. stream: stream,
  1138. success: success,
  1139. streamId: streamId,
  1140. reason: reason
  1141. });
  1142. };
  1143. var deleteStreamController = function deleteStreamController() {
  1144. Promise.all([_this4.streamControllers[data.streamId].startCall, _this4.streamControllers[data.streamId].pullCall, _this4.streamControllers[data.streamId].cancelCall].map(function (capability) {
  1145. return capability && finalize(capability.promise);
  1146. })).then(function () {
  1147. delete _this4.streamControllers[data.streamId];
  1148. });
  1149. };
  1150. switch (data.stream) {
  1151. case 'start_complete':
  1152. resolveOrReject(this.streamControllers[data.streamId].startCall, data.success, wrapReason(data.reason));
  1153. break;
  1154. case 'pull_complete':
  1155. resolveOrReject(this.streamControllers[data.streamId].pullCall, data.success, wrapReason(data.reason));
  1156. break;
  1157. case 'pull':
  1158. if (!this.streamSinks[data.streamId]) {
  1159. sendStreamResponse({
  1160. stream: 'pull_complete',
  1161. success: true
  1162. });
  1163. break;
  1164. }
  1165. if (this.streamSinks[data.streamId].desiredSize <= 0 && data.desiredSize > 0) {
  1166. this.streamSinks[data.streamId].sinkCapability.resolve();
  1167. }
  1168. this.streamSinks[data.streamId].desiredSize = data.desiredSize;
  1169. resolveCall(this.streamSinks[data.streamId].onPull).then(function () {
  1170. sendStreamResponse({
  1171. stream: 'pull_complete',
  1172. success: true
  1173. });
  1174. }, function (reason) {
  1175. sendStreamResponse({
  1176. stream: 'pull_complete',
  1177. success: false,
  1178. reason: reason
  1179. });
  1180. });
  1181. break;
  1182. case 'enqueue':
  1183. assert(this.streamControllers[data.streamId], 'enqueue should have stream controller');
  1184. if (!this.streamControllers[data.streamId].isClosed) {
  1185. this.streamControllers[data.streamId].controller.enqueue(data.chunk);
  1186. }
  1187. break;
  1188. case 'close':
  1189. assert(this.streamControllers[data.streamId], 'close should have stream controller');
  1190. if (this.streamControllers[data.streamId].isClosed) {
  1191. break;
  1192. }
  1193. this.streamControllers[data.streamId].isClosed = true;
  1194. this.streamControllers[data.streamId].controller.close();
  1195. deleteStreamController();
  1196. break;
  1197. case 'error':
  1198. assert(this.streamControllers[data.streamId], 'error should have stream controller');
  1199. this.streamControllers[data.streamId].controller.error(wrapReason(data.reason));
  1200. deleteStreamController();
  1201. break;
  1202. case 'cancel_complete':
  1203. resolveOrReject(this.streamControllers[data.streamId].cancelCall, data.success, wrapReason(data.reason));
  1204. deleteStreamController();
  1205. break;
  1206. case 'cancel':
  1207. if (!this.streamSinks[data.streamId]) {
  1208. break;
  1209. }
  1210. resolveCall(this.streamSinks[data.streamId].onCancel, [wrapReason(data.reason)]).then(function () {
  1211. sendStreamResponse({
  1212. stream: 'cancel_complete',
  1213. success: true
  1214. });
  1215. }, function (reason) {
  1216. sendStreamResponse({
  1217. stream: 'cancel_complete',
  1218. success: false,
  1219. reason: reason
  1220. });
  1221. });
  1222. this.streamSinks[data.streamId].sinkCapability.reject(wrapReason(data.reason));
  1223. this.streamSinks[data.streamId].isCancelled = true;
  1224. delete this.streamSinks[data.streamId];
  1225. break;
  1226. default:
  1227. throw new Error('Unexpected stream case');
  1228. }
  1229. },
  1230. postMessage: function postMessage(message, transfers) {
  1231. if (transfers && this.postMessageTransfers) {
  1232. this.comObj.postMessage(message, transfers);
  1233. } else {
  1234. this.comObj.postMessage(message);
  1235. }
  1236. },
  1237. destroy: function destroy() {
  1238. this.comObj.removeEventListener('message', this._onComObjOnMessage);
  1239. }
  1240. };
  1241. function loadJpegStream(id, imageUrl, objs) {
  1242. var img = new Image();
  1243. img.onload = function loadJpegStream_onloadClosure() {
  1244. objs.resolve(id, img);
  1245. };
  1246. img.onerror = function loadJpegStream_onerrorClosure() {
  1247. objs.resolve(id, null);
  1248. warn('Error during JPEG image loading');
  1249. };
  1250. img.src = imageUrl;
  1251. }
  1252. exports.FONT_IDENTITY_MATRIX = FONT_IDENTITY_MATRIX;
  1253. exports.IDENTITY_MATRIX = IDENTITY_MATRIX;
  1254. exports.OPS = OPS;
  1255. exports.VERBOSITY_LEVELS = VERBOSITY_LEVELS;
  1256. exports.UNSUPPORTED_FEATURES = UNSUPPORTED_FEATURES;
  1257. exports.AnnotationBorderStyleType = AnnotationBorderStyleType;
  1258. exports.AnnotationFieldFlag = AnnotationFieldFlag;
  1259. exports.AnnotationFlag = AnnotationFlag;
  1260. exports.AnnotationType = AnnotationType;
  1261. exports.FontType = FontType;
  1262. exports.ImageKind = ImageKind;
  1263. exports.CMapCompressionType = CMapCompressionType;
  1264. exports.AbortException = AbortException;
  1265. exports.InvalidPDFException = InvalidPDFException;
  1266. exports.MessageHandler = MessageHandler;
  1267. exports.MissingDataException = MissingDataException;
  1268. exports.MissingPDFException = MissingPDFException;
  1269. exports.NativeImageDecoding = NativeImageDecoding;
  1270. exports.NotImplementedException = NotImplementedException;
  1271. exports.PageViewport = PageViewport;
  1272. exports.PasswordException = PasswordException;
  1273. exports.PasswordResponses = PasswordResponses;
  1274. exports.StreamType = StreamType;
  1275. exports.TextRenderingMode = TextRenderingMode;
  1276. exports.UnexpectedResponseException = UnexpectedResponseException;
  1277. exports.UnknownErrorException = UnknownErrorException;
  1278. exports.Util = Util;
  1279. exports.XRefParseException = XRefParseException;
  1280. exports.FormatError = FormatError;
  1281. exports.arrayByteLength = arrayByteLength;
  1282. exports.arraysToBytes = arraysToBytes;
  1283. exports.assert = assert;
  1284. exports.bytesToString = bytesToString;
  1285. exports.createBlob = createBlob;
  1286. exports.createPromiseCapability = createPromiseCapability;
  1287. exports.createObjectURL = createObjectURL;
  1288. exports.deprecated = deprecated;
  1289. exports.getLookupTableFactory = getLookupTableFactory;
  1290. exports.getVerbosityLevel = getVerbosityLevel;
  1291. exports.info = info;
  1292. exports.isArrayBuffer = isArrayBuffer;
  1293. exports.isBool = isBool;
  1294. exports.isEmptyObj = isEmptyObj;
  1295. exports.isNum = isNum;
  1296. exports.isString = isString;
  1297. exports.isSpace = isSpace;
  1298. exports.isNodeJS = isNodeJS;
  1299. exports.isSameOrigin = isSameOrigin;
  1300. exports.createValidAbsoluteUrl = createValidAbsoluteUrl;
  1301. exports.isLittleEndian = isLittleEndian;
  1302. exports.isEvalSupported = isEvalSupported;
  1303. exports.loadJpegStream = loadJpegStream;
  1304. exports.log2 = log2;
  1305. exports.readInt8 = readInt8;
  1306. exports.readUint16 = readUint16;
  1307. exports.readUint32 = readUint32;
  1308. exports.removeNullCharacters = removeNullCharacters;
  1309. exports.ReadableStream = _streams_polyfill.ReadableStream;
  1310. exports.setVerbosityLevel = setVerbosityLevel;
  1311. exports.shadow = shadow;
  1312. exports.string32 = string32;
  1313. exports.stringToBytes = stringToBytes;
  1314. exports.stringToPDFString = stringToPDFString;
  1315. exports.stringToUTF8String = stringToUTF8String;
  1316. exports.utf8StringToString = utf8StringToString;
  1317. exports.warn = warn;
  1318. exports.unreachable = unreachable;