util.js 34 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106
  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.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 globalScope = typeof window !== 'undefined' ? window : typeof global !== 'undefined' ? global : typeof self !== 'undefined' ? self : undefined;
  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 error(msg) {
  263. if (verbosity >= VERBOSITY_LEVELS.errors) {
  264. console.log('Error: ' + msg);
  265. console.log(backtrace());
  266. }
  267. throw new Error(msg);
  268. }
  269. function backtrace() {
  270. try {
  271. throw new Error();
  272. } catch (e) {
  273. return e.stack ? e.stack.split('\n').slice(2).join('\n') : '';
  274. }
  275. }
  276. function assert(cond, msg) {
  277. if (!cond) {
  278. error(msg);
  279. }
  280. }
  281. var UNSUPPORTED_FEATURES = {
  282. unknown: 'unknown',
  283. forms: 'forms',
  284. javaScript: 'javaScript',
  285. smask: 'smask',
  286. shadingPattern: 'shadingPattern',
  287. font: 'font'
  288. };
  289. function isSameOrigin(baseUrl, otherUrl) {
  290. try {
  291. var base = new URL(baseUrl);
  292. if (!base.origin || base.origin === 'null') {
  293. return false;
  294. }
  295. } catch (e) {
  296. return false;
  297. }
  298. var other = new URL(otherUrl, base);
  299. return base.origin === other.origin;
  300. }
  301. function isValidProtocol(url) {
  302. if (!url) {
  303. return false;
  304. }
  305. switch (url.protocol) {
  306. case 'http:':
  307. case 'https:':
  308. case 'ftp:':
  309. case 'mailto:':
  310. case 'tel:':
  311. return true;
  312. default:
  313. return false;
  314. }
  315. }
  316. function createValidAbsoluteUrl(url, baseUrl) {
  317. if (!url) {
  318. return null;
  319. }
  320. try {
  321. var absoluteUrl = baseUrl ? new URL(url, baseUrl) : new URL(url);
  322. if (isValidProtocol(absoluteUrl)) {
  323. return absoluteUrl;
  324. }
  325. } catch (ex) {}
  326. return null;
  327. }
  328. function shadow(obj, prop, value) {
  329. Object.defineProperty(obj, prop, {
  330. value: value,
  331. enumerable: true,
  332. configurable: true,
  333. writable: false
  334. });
  335. return value;
  336. }
  337. function getLookupTableFactory(initializer) {
  338. var lookup;
  339. return function () {
  340. if (initializer) {
  341. lookup = Object.create(null);
  342. initializer(lookup);
  343. initializer = null;
  344. }
  345. return lookup;
  346. };
  347. }
  348. var PasswordResponses = {
  349. NEED_PASSWORD: 1,
  350. INCORRECT_PASSWORD: 2
  351. };
  352. var PasswordException = function PasswordExceptionClosure() {
  353. function PasswordException(msg, code) {
  354. this.name = 'PasswordException';
  355. this.message = msg;
  356. this.code = code;
  357. }
  358. PasswordException.prototype = new Error();
  359. PasswordException.constructor = PasswordException;
  360. return PasswordException;
  361. }();
  362. var UnknownErrorException = function UnknownErrorExceptionClosure() {
  363. function UnknownErrorException(msg, details) {
  364. this.name = 'UnknownErrorException';
  365. this.message = msg;
  366. this.details = details;
  367. }
  368. UnknownErrorException.prototype = new Error();
  369. UnknownErrorException.constructor = UnknownErrorException;
  370. return UnknownErrorException;
  371. }();
  372. var InvalidPDFException = function InvalidPDFExceptionClosure() {
  373. function InvalidPDFException(msg) {
  374. this.name = 'InvalidPDFException';
  375. this.message = msg;
  376. }
  377. InvalidPDFException.prototype = new Error();
  378. InvalidPDFException.constructor = InvalidPDFException;
  379. return InvalidPDFException;
  380. }();
  381. var MissingPDFException = function MissingPDFExceptionClosure() {
  382. function MissingPDFException(msg) {
  383. this.name = 'MissingPDFException';
  384. this.message = msg;
  385. }
  386. MissingPDFException.prototype = new Error();
  387. MissingPDFException.constructor = MissingPDFException;
  388. return MissingPDFException;
  389. }();
  390. var UnexpectedResponseException = function UnexpectedResponseExceptionClosure() {
  391. function UnexpectedResponseException(msg, status) {
  392. this.name = 'UnexpectedResponseException';
  393. this.message = msg;
  394. this.status = status;
  395. }
  396. UnexpectedResponseException.prototype = new Error();
  397. UnexpectedResponseException.constructor = UnexpectedResponseException;
  398. return UnexpectedResponseException;
  399. }();
  400. var NotImplementedException = function NotImplementedExceptionClosure() {
  401. function NotImplementedException(msg) {
  402. this.message = msg;
  403. }
  404. NotImplementedException.prototype = new Error();
  405. NotImplementedException.prototype.name = 'NotImplementedException';
  406. NotImplementedException.constructor = NotImplementedException;
  407. return NotImplementedException;
  408. }();
  409. var MissingDataException = function MissingDataExceptionClosure() {
  410. function MissingDataException(begin, end) {
  411. this.begin = begin;
  412. this.end = end;
  413. this.message = 'Missing data [' + begin + ', ' + end + ')';
  414. }
  415. MissingDataException.prototype = new Error();
  416. MissingDataException.prototype.name = 'MissingDataException';
  417. MissingDataException.constructor = MissingDataException;
  418. return MissingDataException;
  419. }();
  420. var XRefParseException = function XRefParseExceptionClosure() {
  421. function XRefParseException(msg) {
  422. this.message = msg;
  423. }
  424. XRefParseException.prototype = new Error();
  425. XRefParseException.prototype.name = 'XRefParseException';
  426. XRefParseException.constructor = XRefParseException;
  427. return XRefParseException;
  428. }();
  429. var NullCharactersRegExp = /\x00/g;
  430. function removeNullCharacters(str) {
  431. if (typeof str !== 'string') {
  432. warn('The argument for removeNullCharacters must be a string.');
  433. return str;
  434. }
  435. return str.replace(NullCharactersRegExp, '');
  436. }
  437. function bytesToString(bytes) {
  438. assert(bytes !== null && (typeof bytes === 'undefined' ? 'undefined' : _typeof(bytes)) === 'object' && bytes.length !== undefined, 'Invalid argument for bytesToString');
  439. var length = bytes.length;
  440. var MAX_ARGUMENT_COUNT = 8192;
  441. if (length < MAX_ARGUMENT_COUNT) {
  442. return String.fromCharCode.apply(null, bytes);
  443. }
  444. var strBuf = [];
  445. for (var i = 0; i < length; i += MAX_ARGUMENT_COUNT) {
  446. var chunkEnd = Math.min(i + MAX_ARGUMENT_COUNT, length);
  447. var chunk = bytes.subarray(i, chunkEnd);
  448. strBuf.push(String.fromCharCode.apply(null, chunk));
  449. }
  450. return strBuf.join('');
  451. }
  452. function stringToBytes(str) {
  453. assert(typeof str === 'string', 'Invalid argument for stringToBytes');
  454. var length = str.length;
  455. var bytes = new Uint8Array(length);
  456. for (var i = 0; i < length; ++i) {
  457. bytes[i] = str.charCodeAt(i) & 0xFF;
  458. }
  459. return bytes;
  460. }
  461. function arrayByteLength(arr) {
  462. if (arr.length !== undefined) {
  463. return arr.length;
  464. }
  465. assert(arr.byteLength !== undefined);
  466. return arr.byteLength;
  467. }
  468. function arraysToBytes(arr) {
  469. if (arr.length === 1 && arr[0] instanceof Uint8Array) {
  470. return arr[0];
  471. }
  472. var resultLength = 0;
  473. var i,
  474. ii = arr.length;
  475. var item, itemLength;
  476. for (i = 0; i < ii; i++) {
  477. item = arr[i];
  478. itemLength = arrayByteLength(item);
  479. resultLength += itemLength;
  480. }
  481. var pos = 0;
  482. var data = new Uint8Array(resultLength);
  483. for (i = 0; i < ii; i++) {
  484. item = arr[i];
  485. if (!(item instanceof Uint8Array)) {
  486. if (typeof item === 'string') {
  487. item = stringToBytes(item);
  488. } else {
  489. item = new Uint8Array(item);
  490. }
  491. }
  492. itemLength = item.byteLength;
  493. data.set(item, pos);
  494. pos += itemLength;
  495. }
  496. return data;
  497. }
  498. function string32(value) {
  499. return String.fromCharCode(value >> 24 & 0xff, value >> 16 & 0xff, value >> 8 & 0xff, value & 0xff);
  500. }
  501. function log2(x) {
  502. var n = 1,
  503. i = 0;
  504. while (x > n) {
  505. n <<= 1;
  506. i++;
  507. }
  508. return i;
  509. }
  510. function readInt8(data, start) {
  511. return data[start] << 24 >> 24;
  512. }
  513. function readUint16(data, offset) {
  514. return data[offset] << 8 | data[offset + 1];
  515. }
  516. function readUint32(data, offset) {
  517. return (data[offset] << 24 | data[offset + 1] << 16 | data[offset + 2] << 8 | data[offset + 3]) >>> 0;
  518. }
  519. function isLittleEndian() {
  520. var buffer8 = new Uint8Array(4);
  521. buffer8[0] = 1;
  522. var view32 = new Uint32Array(buffer8.buffer, 0, 1);
  523. return view32[0] === 1;
  524. }
  525. function isEvalSupported() {
  526. try {
  527. new Function('');
  528. return true;
  529. } catch (e) {
  530. return false;
  531. }
  532. }
  533. var IDENTITY_MATRIX = [1, 0, 0, 1, 0, 0];
  534. var Util = function UtilClosure() {
  535. function Util() {}
  536. var rgbBuf = ['rgb(', 0, ',', 0, ',', 0, ')'];
  537. Util.makeCssRgb = function Util_makeCssRgb(r, g, b) {
  538. rgbBuf[1] = r;
  539. rgbBuf[3] = g;
  540. rgbBuf[5] = b;
  541. return rgbBuf.join('');
  542. };
  543. Util.transform = function Util_transform(m1, m2) {
  544. 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]];
  545. };
  546. Util.applyTransform = function Util_applyTransform(p, m) {
  547. var xt = p[0] * m[0] + p[1] * m[2] + m[4];
  548. var yt = p[0] * m[1] + p[1] * m[3] + m[5];
  549. return [xt, yt];
  550. };
  551. Util.applyInverseTransform = function Util_applyInverseTransform(p, m) {
  552. var d = m[0] * m[3] - m[1] * m[2];
  553. var xt = (p[0] * m[3] - p[1] * m[2] + m[2] * m[5] - m[4] * m[3]) / d;
  554. var yt = (-p[0] * m[1] + p[1] * m[0] + m[4] * m[1] - m[5] * m[0]) / d;
  555. return [xt, yt];
  556. };
  557. Util.getAxialAlignedBoundingBox = function Util_getAxialAlignedBoundingBox(r, m) {
  558. var p1 = Util.applyTransform(r, m);
  559. var p2 = Util.applyTransform(r.slice(2, 4), m);
  560. var p3 = Util.applyTransform([r[0], r[3]], m);
  561. var p4 = Util.applyTransform([r[2], r[1]], m);
  562. 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])];
  563. };
  564. Util.inverseTransform = function Util_inverseTransform(m) {
  565. var d = m[0] * m[3] - m[1] * m[2];
  566. 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];
  567. };
  568. Util.apply3dTransform = function Util_apply3dTransform(m, v) {
  569. 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]];
  570. };
  571. Util.singularValueDecompose2dScale = function Util_singularValueDecompose2dScale(m) {
  572. var transpose = [m[0], m[2], m[1], m[3]];
  573. var a = m[0] * transpose[0] + m[1] * transpose[2];
  574. var b = m[0] * transpose[1] + m[1] * transpose[3];
  575. var c = m[2] * transpose[0] + m[3] * transpose[2];
  576. var d = m[2] * transpose[1] + m[3] * transpose[3];
  577. var first = (a + d) / 2;
  578. var second = Math.sqrt((a + d) * (a + d) - 4 * (a * d - c * b)) / 2;
  579. var sx = first + second || 1;
  580. var sy = first - second || 1;
  581. return [Math.sqrt(sx), Math.sqrt(sy)];
  582. };
  583. Util.normalizeRect = function Util_normalizeRect(rect) {
  584. var r = rect.slice(0);
  585. if (rect[0] > rect[2]) {
  586. r[0] = rect[2];
  587. r[2] = rect[0];
  588. }
  589. if (rect[1] > rect[3]) {
  590. r[1] = rect[3];
  591. r[3] = rect[1];
  592. }
  593. return r;
  594. };
  595. Util.intersect = function Util_intersect(rect1, rect2) {
  596. function compare(a, b) {
  597. return a - b;
  598. }
  599. var orderedX = [rect1[0], rect1[2], rect2[0], rect2[2]].sort(compare),
  600. orderedY = [rect1[1], rect1[3], rect2[1], rect2[3]].sort(compare),
  601. result = [];
  602. rect1 = Util.normalizeRect(rect1);
  603. rect2 = Util.normalizeRect(rect2);
  604. if (orderedX[0] === rect1[0] && orderedX[1] === rect2[0] || orderedX[0] === rect2[0] && orderedX[1] === rect1[0]) {
  605. result[0] = orderedX[1];
  606. result[2] = orderedX[2];
  607. } else {
  608. return false;
  609. }
  610. if (orderedY[0] === rect1[1] && orderedY[1] === rect2[1] || orderedY[0] === rect2[1] && orderedY[1] === rect1[1]) {
  611. result[1] = orderedY[1];
  612. result[3] = orderedY[2];
  613. } else {
  614. return false;
  615. }
  616. return result;
  617. };
  618. Util.sign = function Util_sign(num) {
  619. return num < 0 ? -1 : 1;
  620. };
  621. 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'];
  622. Util.toRoman = function Util_toRoman(number, lowerCase) {
  623. assert(isInt(number) && number > 0, 'The number should be a positive integer.');
  624. var pos,
  625. romanBuf = [];
  626. while (number >= 1000) {
  627. number -= 1000;
  628. romanBuf.push('M');
  629. }
  630. pos = number / 100 | 0;
  631. number %= 100;
  632. romanBuf.push(ROMAN_NUMBER_MAP[pos]);
  633. pos = number / 10 | 0;
  634. number %= 10;
  635. romanBuf.push(ROMAN_NUMBER_MAP[10 + pos]);
  636. romanBuf.push(ROMAN_NUMBER_MAP[20 + number]);
  637. var romanStr = romanBuf.join('');
  638. return lowerCase ? romanStr.toLowerCase() : romanStr;
  639. };
  640. Util.appendToArray = function Util_appendToArray(arr1, arr2) {
  641. Array.prototype.push.apply(arr1, arr2);
  642. };
  643. Util.prependToArray = function Util_prependToArray(arr1, arr2) {
  644. Array.prototype.unshift.apply(arr1, arr2);
  645. };
  646. Util.extendObj = function extendObj(obj1, obj2) {
  647. for (var key in obj2) {
  648. obj1[key] = obj2[key];
  649. }
  650. };
  651. Util.getInheritableProperty = function Util_getInheritableProperty(dict, name, getArray) {
  652. while (dict && !dict.has(name)) {
  653. dict = dict.get('Parent');
  654. }
  655. if (!dict) {
  656. return null;
  657. }
  658. return getArray ? dict.getArray(name) : dict.get(name);
  659. };
  660. Util.inherit = function Util_inherit(sub, base, prototype) {
  661. sub.prototype = Object.create(base.prototype);
  662. sub.prototype.constructor = sub;
  663. for (var prop in prototype) {
  664. sub.prototype[prop] = prototype[prop];
  665. }
  666. };
  667. Util.loadScript = function Util_loadScript(src, callback) {
  668. var script = document.createElement('script');
  669. var loaded = false;
  670. script.setAttribute('src', src);
  671. if (callback) {
  672. script.onload = function () {
  673. if (!loaded) {
  674. callback();
  675. }
  676. loaded = true;
  677. };
  678. }
  679. document.getElementsByTagName('head')[0].appendChild(script);
  680. };
  681. return Util;
  682. }();
  683. var PageViewport = function PageViewportClosure() {
  684. function PageViewport(viewBox, scale, rotation, offsetX, offsetY, dontFlip) {
  685. this.viewBox = viewBox;
  686. this.scale = scale;
  687. this.rotation = rotation;
  688. this.offsetX = offsetX;
  689. this.offsetY = offsetY;
  690. var centerX = (viewBox[2] + viewBox[0]) / 2;
  691. var centerY = (viewBox[3] + viewBox[1]) / 2;
  692. var rotateA, rotateB, rotateC, rotateD;
  693. rotation = rotation % 360;
  694. rotation = rotation < 0 ? rotation + 360 : rotation;
  695. switch (rotation) {
  696. case 180:
  697. rotateA = -1;
  698. rotateB = 0;
  699. rotateC = 0;
  700. rotateD = 1;
  701. break;
  702. case 90:
  703. rotateA = 0;
  704. rotateB = 1;
  705. rotateC = 1;
  706. rotateD = 0;
  707. break;
  708. case 270:
  709. rotateA = 0;
  710. rotateB = -1;
  711. rotateC = -1;
  712. rotateD = 0;
  713. break;
  714. default:
  715. rotateA = 1;
  716. rotateB = 0;
  717. rotateC = 0;
  718. rotateD = -1;
  719. break;
  720. }
  721. if (dontFlip) {
  722. rotateC = -rotateC;
  723. rotateD = -rotateD;
  724. }
  725. var offsetCanvasX, offsetCanvasY;
  726. var width, height;
  727. if (rotateA === 0) {
  728. offsetCanvasX = Math.abs(centerY - viewBox[1]) * scale + offsetX;
  729. offsetCanvasY = Math.abs(centerX - viewBox[0]) * scale + offsetY;
  730. width = Math.abs(viewBox[3] - viewBox[1]) * scale;
  731. height = Math.abs(viewBox[2] - viewBox[0]) * scale;
  732. } else {
  733. offsetCanvasX = Math.abs(centerX - viewBox[0]) * scale + offsetX;
  734. offsetCanvasY = Math.abs(centerY - viewBox[1]) * scale + offsetY;
  735. width = Math.abs(viewBox[2] - viewBox[0]) * scale;
  736. height = Math.abs(viewBox[3] - viewBox[1]) * scale;
  737. }
  738. this.transform = [rotateA * scale, rotateB * scale, rotateC * scale, rotateD * scale, offsetCanvasX - rotateA * scale * centerX - rotateC * scale * centerY, offsetCanvasY - rotateB * scale * centerX - rotateD * scale * centerY];
  739. this.width = width;
  740. this.height = height;
  741. this.fontScale = scale;
  742. }
  743. PageViewport.prototype = {
  744. clone: function PageViewPort_clone(args) {
  745. args = args || {};
  746. var scale = 'scale' in args ? args.scale : this.scale;
  747. var rotation = 'rotation' in args ? args.rotation : this.rotation;
  748. return new PageViewport(this.viewBox.slice(), scale, rotation, this.offsetX, this.offsetY, args.dontFlip);
  749. },
  750. convertToViewportPoint: function PageViewport_convertToViewportPoint(x, y) {
  751. return Util.applyTransform([x, y], this.transform);
  752. },
  753. convertToViewportRectangle: function PageViewport_convertToViewportRectangle(rect) {
  754. var tl = Util.applyTransform([rect[0], rect[1]], this.transform);
  755. var br = Util.applyTransform([rect[2], rect[3]], this.transform);
  756. return [tl[0], tl[1], br[0], br[1]];
  757. },
  758. convertToPdfPoint: function PageViewport_convertToPdfPoint(x, y) {
  759. return Util.applyInverseTransform([x, y], this.transform);
  760. }
  761. };
  762. return PageViewport;
  763. }();
  764. 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];
  765. function stringToPDFString(str) {
  766. var i,
  767. n = str.length,
  768. strBuf = [];
  769. if (str[0] === '\xFE' && str[1] === '\xFF') {
  770. for (i = 2; i < n; i += 2) {
  771. strBuf.push(String.fromCharCode(str.charCodeAt(i) << 8 | str.charCodeAt(i + 1)));
  772. }
  773. } else {
  774. for (i = 0; i < n; ++i) {
  775. var code = PDFStringTranslateTable[str.charCodeAt(i)];
  776. strBuf.push(code ? String.fromCharCode(code) : str.charAt(i));
  777. }
  778. }
  779. return strBuf.join('');
  780. }
  781. function stringToUTF8String(str) {
  782. return decodeURIComponent(escape(str));
  783. }
  784. function utf8StringToString(str) {
  785. return unescape(encodeURIComponent(str));
  786. }
  787. function isEmptyObj(obj) {
  788. for (var key in obj) {
  789. return false;
  790. }
  791. return true;
  792. }
  793. function isBool(v) {
  794. return typeof v === 'boolean';
  795. }
  796. function isInt(v) {
  797. return typeof v === 'number' && (v | 0) === v;
  798. }
  799. function isNum(v) {
  800. return typeof v === 'number';
  801. }
  802. function isString(v) {
  803. return typeof v === 'string';
  804. }
  805. function isArray(v) {
  806. return v instanceof Array;
  807. }
  808. function isArrayBuffer(v) {
  809. return (typeof v === 'undefined' ? 'undefined' : _typeof(v)) === 'object' && v !== null && v.byteLength !== undefined;
  810. }
  811. function isSpace(ch) {
  812. return ch === 0x20 || ch === 0x09 || ch === 0x0D || ch === 0x0A;
  813. }
  814. function isNodeJS() {
  815. if (typeof __pdfjsdev_webpack__ === 'undefined') {
  816. return (typeof process === 'undefined' ? 'undefined' : _typeof(process)) === 'object' && process + '' === '[object process]';
  817. }
  818. return false;
  819. }
  820. function createPromiseCapability() {
  821. var capability = {};
  822. capability.promise = new Promise(function (resolve, reject) {
  823. capability.resolve = resolve;
  824. capability.reject = reject;
  825. });
  826. return capability;
  827. }
  828. var StatTimer = function StatTimerClosure() {
  829. function rpad(str, pad, length) {
  830. while (str.length < length) {
  831. str += pad;
  832. }
  833. return str;
  834. }
  835. function StatTimer() {
  836. this.started = Object.create(null);
  837. this.times = [];
  838. this.enabled = true;
  839. }
  840. StatTimer.prototype = {
  841. time: function StatTimer_time(name) {
  842. if (!this.enabled) {
  843. return;
  844. }
  845. if (name in this.started) {
  846. warn('Timer is already running for ' + name);
  847. }
  848. this.started[name] = Date.now();
  849. },
  850. timeEnd: function StatTimer_timeEnd(name) {
  851. if (!this.enabled) {
  852. return;
  853. }
  854. if (!(name in this.started)) {
  855. warn('Timer has not been started for ' + name);
  856. }
  857. this.times.push({
  858. 'name': name,
  859. 'start': this.started[name],
  860. 'end': Date.now()
  861. });
  862. delete this.started[name];
  863. },
  864. toString: function StatTimer_toString() {
  865. var i, ii;
  866. var times = this.times;
  867. var out = '';
  868. var longest = 0;
  869. for (i = 0, ii = times.length; i < ii; ++i) {
  870. var name = times[i]['name'];
  871. if (name.length > longest) {
  872. longest = name.length;
  873. }
  874. }
  875. for (i = 0, ii = times.length; i < ii; ++i) {
  876. var span = times[i];
  877. var duration = span.end - span.start;
  878. out += rpad(span['name'], ' ', longest) + ' ' + duration + 'ms\n';
  879. }
  880. return out;
  881. }
  882. };
  883. return StatTimer;
  884. }();
  885. var createBlob = function createBlob(data, contentType) {
  886. if (typeof Blob !== 'undefined') {
  887. return new Blob([data], { type: contentType });
  888. }
  889. throw new Error('The "Blob" constructor is not supported.');
  890. };
  891. var createObjectURL = function createObjectURLClosure() {
  892. var digits = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/=';
  893. return function createObjectURL(data, contentType) {
  894. var forceDataSchema = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : false;
  895. if (!forceDataSchema && URL.createObjectURL) {
  896. var blob = createBlob(data, contentType);
  897. return URL.createObjectURL(blob);
  898. }
  899. var buffer = 'data:' + contentType + ';base64,';
  900. for (var i = 0, ii = data.length; i < ii; i += 3) {
  901. var b1 = data[i] & 0xFF;
  902. var b2 = data[i + 1] & 0xFF;
  903. var b3 = data[i + 2] & 0xFF;
  904. var d1 = b1 >> 2,
  905. d2 = (b1 & 3) << 4 | b2 >> 4;
  906. var d3 = i + 1 < ii ? (b2 & 0xF) << 2 | b3 >> 6 : 64;
  907. var d4 = i + 2 < ii ? b3 & 0x3F : 64;
  908. buffer += digits[d1] + digits[d2] + digits[d3] + digits[d4];
  909. }
  910. return buffer;
  911. };
  912. }();
  913. function MessageHandler(sourceName, targetName, comObj) {
  914. var _this = this;
  915. this.sourceName = sourceName;
  916. this.targetName = targetName;
  917. this.comObj = comObj;
  918. this.callbackIndex = 1;
  919. this.postMessageTransfers = true;
  920. var callbacksCapabilities = this.callbacksCapabilities = Object.create(null);
  921. var ah = this.actionHandler = Object.create(null);
  922. this._onComObjOnMessage = function (event) {
  923. var data = event.data;
  924. if (data.targetName !== _this.sourceName) {
  925. return;
  926. }
  927. if (data.isReply) {
  928. var callbackId = data.callbackId;
  929. if (data.callbackId in callbacksCapabilities) {
  930. var callback = callbacksCapabilities[callbackId];
  931. delete callbacksCapabilities[callbackId];
  932. if ('error' in data) {
  933. callback.reject(data.error);
  934. } else {
  935. callback.resolve(data.data);
  936. }
  937. } else {
  938. error('Cannot resolve callback ' + callbackId);
  939. }
  940. } else if (data.action in ah) {
  941. var action = ah[data.action];
  942. if (data.callbackId) {
  943. var sourceName = _this.sourceName;
  944. var targetName = data.sourceName;
  945. Promise.resolve().then(function () {
  946. return action[0].call(action[1], data.data);
  947. }).then(function (result) {
  948. comObj.postMessage({
  949. sourceName: sourceName,
  950. targetName: targetName,
  951. isReply: true,
  952. callbackId: data.callbackId,
  953. data: result
  954. });
  955. }, function (reason) {
  956. if (reason instanceof Error) {
  957. reason = reason + '';
  958. }
  959. comObj.postMessage({
  960. sourceName: sourceName,
  961. targetName: targetName,
  962. isReply: true,
  963. callbackId: data.callbackId,
  964. error: reason
  965. });
  966. });
  967. } else {
  968. action[0].call(action[1], data.data);
  969. }
  970. } else {
  971. error('Unknown action from worker: ' + data.action);
  972. }
  973. };
  974. comObj.addEventListener('message', this._onComObjOnMessage);
  975. }
  976. MessageHandler.prototype = {
  977. on: function on(actionName, handler, scope) {
  978. var ah = this.actionHandler;
  979. if (ah[actionName]) {
  980. error('There is already an actionName called "' + actionName + '"');
  981. }
  982. ah[actionName] = [handler, scope];
  983. },
  984. send: function send(actionName, data, transfers) {
  985. var message = {
  986. sourceName: this.sourceName,
  987. targetName: this.targetName,
  988. action: actionName,
  989. data: data
  990. };
  991. this.postMessage(message, transfers);
  992. },
  993. sendWithPromise: function sendWithPromise(actionName, data, transfers) {
  994. var callbackId = this.callbackIndex++;
  995. var message = {
  996. sourceName: this.sourceName,
  997. targetName: this.targetName,
  998. action: actionName,
  999. data: data,
  1000. callbackId: callbackId
  1001. };
  1002. var capability = createPromiseCapability();
  1003. this.callbacksCapabilities[callbackId] = capability;
  1004. try {
  1005. this.postMessage(message, transfers);
  1006. } catch (e) {
  1007. capability.reject(e);
  1008. }
  1009. return capability.promise;
  1010. },
  1011. postMessage: function postMessage(message, transfers) {
  1012. if (transfers && this.postMessageTransfers) {
  1013. this.comObj.postMessage(message, transfers);
  1014. } else {
  1015. this.comObj.postMessage(message);
  1016. }
  1017. },
  1018. destroy: function destroy() {
  1019. this.comObj.removeEventListener('message', this._onComObjOnMessage);
  1020. }
  1021. };
  1022. function loadJpegStream(id, imageUrl, objs) {
  1023. var img = new Image();
  1024. img.onload = function loadJpegStream_onloadClosure() {
  1025. objs.resolve(id, img);
  1026. };
  1027. img.onerror = function loadJpegStream_onerrorClosure() {
  1028. objs.resolve(id, null);
  1029. warn('Error during JPEG image loading');
  1030. };
  1031. img.src = imageUrl;
  1032. }
  1033. exports.FONT_IDENTITY_MATRIX = FONT_IDENTITY_MATRIX;
  1034. exports.IDENTITY_MATRIX = IDENTITY_MATRIX;
  1035. exports.OPS = OPS;
  1036. exports.VERBOSITY_LEVELS = VERBOSITY_LEVELS;
  1037. exports.UNSUPPORTED_FEATURES = UNSUPPORTED_FEATURES;
  1038. exports.AnnotationBorderStyleType = AnnotationBorderStyleType;
  1039. exports.AnnotationFieldFlag = AnnotationFieldFlag;
  1040. exports.AnnotationFlag = AnnotationFlag;
  1041. exports.AnnotationType = AnnotationType;
  1042. exports.FontType = FontType;
  1043. exports.ImageKind = ImageKind;
  1044. exports.CMapCompressionType = CMapCompressionType;
  1045. exports.InvalidPDFException = InvalidPDFException;
  1046. exports.MessageHandler = MessageHandler;
  1047. exports.MissingDataException = MissingDataException;
  1048. exports.MissingPDFException = MissingPDFException;
  1049. exports.NativeImageDecoding = NativeImageDecoding;
  1050. exports.NotImplementedException = NotImplementedException;
  1051. exports.PageViewport = PageViewport;
  1052. exports.PasswordException = PasswordException;
  1053. exports.PasswordResponses = PasswordResponses;
  1054. exports.StatTimer = StatTimer;
  1055. exports.StreamType = StreamType;
  1056. exports.TextRenderingMode = TextRenderingMode;
  1057. exports.UnexpectedResponseException = UnexpectedResponseException;
  1058. exports.UnknownErrorException = UnknownErrorException;
  1059. exports.Util = Util;
  1060. exports.XRefParseException = XRefParseException;
  1061. exports.arrayByteLength = arrayByteLength;
  1062. exports.arraysToBytes = arraysToBytes;
  1063. exports.assert = assert;
  1064. exports.bytesToString = bytesToString;
  1065. exports.createBlob = createBlob;
  1066. exports.createPromiseCapability = createPromiseCapability;
  1067. exports.createObjectURL = createObjectURL;
  1068. exports.deprecated = deprecated;
  1069. exports.error = error;
  1070. exports.getLookupTableFactory = getLookupTableFactory;
  1071. exports.getVerbosityLevel = getVerbosityLevel;
  1072. exports.globalScope = globalScope;
  1073. exports.info = info;
  1074. exports.isArray = isArray;
  1075. exports.isArrayBuffer = isArrayBuffer;
  1076. exports.isBool = isBool;
  1077. exports.isEmptyObj = isEmptyObj;
  1078. exports.isInt = isInt;
  1079. exports.isNum = isNum;
  1080. exports.isString = isString;
  1081. exports.isSpace = isSpace;
  1082. exports.isNodeJS = isNodeJS;
  1083. exports.isSameOrigin = isSameOrigin;
  1084. exports.createValidAbsoluteUrl = createValidAbsoluteUrl;
  1085. exports.isLittleEndian = isLittleEndian;
  1086. exports.isEvalSupported = isEvalSupported;
  1087. exports.loadJpegStream = loadJpegStream;
  1088. exports.log2 = log2;
  1089. exports.readInt8 = readInt8;
  1090. exports.readUint16 = readUint16;
  1091. exports.readUint32 = readUint32;
  1092. exports.removeNullCharacters = removeNullCharacters;
  1093. exports.setVerbosityLevel = setVerbosityLevel;
  1094. exports.shadow = shadow;
  1095. exports.string32 = string32;
  1096. exports.stringToBytes = stringToBytes;
  1097. exports.stringToPDFString = stringToPDFString;
  1098. exports.stringToUTF8String = stringToUTF8String;
  1099. exports.utf8StringToString = utf8StringToString;
  1100. exports.warn = warn;