util.js 42 KB

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