2
0

util.js 32 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094
  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. 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; };
  17. var compatibility = require('./compatibility.js');
  18. var globalScope = typeof window !== 'undefined' ? window : typeof global !== 'undefined' ? global : typeof self !== 'undefined' ? self : undefined;
  19. var FONT_IDENTITY_MATRIX = [0.001, 0, 0, 0.001, 0, 0];
  20. var TextRenderingMode = {
  21. FILL: 0,
  22. STROKE: 1,
  23. FILL_STROKE: 2,
  24. INVISIBLE: 3,
  25. FILL_ADD_TO_PATH: 4,
  26. STROKE_ADD_TO_PATH: 5,
  27. FILL_STROKE_ADD_TO_PATH: 6,
  28. ADD_TO_PATH: 7,
  29. FILL_STROKE_MASK: 3,
  30. ADD_TO_PATH_FLAG: 4
  31. };
  32. var ImageKind = {
  33. GRAYSCALE_1BPP: 1,
  34. RGB_24BPP: 2,
  35. RGBA_32BPP: 3
  36. };
  37. var AnnotationType = {
  38. TEXT: 1,
  39. LINK: 2,
  40. FREETEXT: 3,
  41. LINE: 4,
  42. SQUARE: 5,
  43. CIRCLE: 6,
  44. POLYGON: 7,
  45. POLYLINE: 8,
  46. HIGHLIGHT: 9,
  47. UNDERLINE: 10,
  48. SQUIGGLY: 11,
  49. STRIKEOUT: 12,
  50. STAMP: 13,
  51. CARET: 14,
  52. INK: 15,
  53. POPUP: 16,
  54. FILEATTACHMENT: 17,
  55. SOUND: 18,
  56. MOVIE: 19,
  57. WIDGET: 20,
  58. SCREEN: 21,
  59. PRINTERMARK: 22,
  60. TRAPNET: 23,
  61. WATERMARK: 24,
  62. THREED: 25,
  63. REDACT: 26
  64. };
  65. var AnnotationFlag = {
  66. INVISIBLE: 0x01,
  67. HIDDEN: 0x02,
  68. PRINT: 0x04,
  69. NOZOOM: 0x08,
  70. NOROTATE: 0x10,
  71. NOVIEW: 0x20,
  72. READONLY: 0x40,
  73. LOCKED: 0x80,
  74. TOGGLENOVIEW: 0x100,
  75. LOCKEDCONTENTS: 0x200
  76. };
  77. var AnnotationFieldFlag = {
  78. READONLY: 0x0000001,
  79. REQUIRED: 0x0000002,
  80. NOEXPORT: 0x0000004,
  81. MULTILINE: 0x0001000,
  82. PASSWORD: 0x0002000,
  83. NOTOGGLETOOFF: 0x0004000,
  84. RADIO: 0x0008000,
  85. PUSHBUTTON: 0x0010000,
  86. COMBO: 0x0020000,
  87. EDIT: 0x0040000,
  88. SORT: 0x0080000,
  89. FILESELECT: 0x0100000,
  90. MULTISELECT: 0x0200000,
  91. DONOTSPELLCHECK: 0x0400000,
  92. DONOTSCROLL: 0x0800000,
  93. COMB: 0x1000000,
  94. RICHTEXT: 0x2000000,
  95. RADIOSINUNISON: 0x2000000,
  96. COMMITONSELCHANGE: 0x4000000
  97. };
  98. var AnnotationBorderStyleType = {
  99. SOLID: 1,
  100. DASHED: 2,
  101. BEVELED: 3,
  102. INSET: 4,
  103. UNDERLINE: 5
  104. };
  105. var StreamType = {
  106. UNKNOWN: 0,
  107. FLATE: 1,
  108. LZW: 2,
  109. DCT: 3,
  110. JPX: 4,
  111. JBIG: 5,
  112. A85: 6,
  113. AHX: 7,
  114. CCF: 8,
  115. RL: 9
  116. };
  117. var FontType = {
  118. UNKNOWN: 0,
  119. TYPE1: 1,
  120. TYPE1C: 2,
  121. CIDFONTTYPE0: 3,
  122. CIDFONTTYPE0C: 4,
  123. TRUETYPE: 5,
  124. CIDFONTTYPE2: 6,
  125. TYPE3: 7,
  126. OPENTYPE: 8,
  127. TYPE0: 9,
  128. MMTYPE1: 10
  129. };
  130. var VERBOSITY_LEVELS = {
  131. errors: 0,
  132. warnings: 1,
  133. infos: 5
  134. };
  135. var CMapCompressionType = {
  136. NONE: 0,
  137. BINARY: 1,
  138. STREAM: 2
  139. };
  140. var OPS = {
  141. dependency: 1,
  142. setLineWidth: 2,
  143. setLineCap: 3,
  144. setLineJoin: 4,
  145. setMiterLimit: 5,
  146. setDash: 6,
  147. setRenderingIntent: 7,
  148. setFlatness: 8,
  149. setGState: 9,
  150. save: 10,
  151. restore: 11,
  152. transform: 12,
  153. moveTo: 13,
  154. lineTo: 14,
  155. curveTo: 15,
  156. curveTo2: 16,
  157. curveTo3: 17,
  158. closePath: 18,
  159. rectangle: 19,
  160. stroke: 20,
  161. closeStroke: 21,
  162. fill: 22,
  163. eoFill: 23,
  164. fillStroke: 24,
  165. eoFillStroke: 25,
  166. closeFillStroke: 26,
  167. closeEOFillStroke: 27,
  168. endPath: 28,
  169. clip: 29,
  170. eoClip: 30,
  171. beginText: 31,
  172. endText: 32,
  173. setCharSpacing: 33,
  174. setWordSpacing: 34,
  175. setHScale: 35,
  176. setLeading: 36,
  177. setFont: 37,
  178. setTextRenderingMode: 38,
  179. setTextRise: 39,
  180. moveText: 40,
  181. setLeadingMoveText: 41,
  182. setTextMatrix: 42,
  183. nextLine: 43,
  184. showText: 44,
  185. showSpacedText: 45,
  186. nextLineShowText: 46,
  187. nextLineSetSpacingShowText: 47,
  188. setCharWidth: 48,
  189. setCharWidthAndBounds: 49,
  190. setStrokeColorSpace: 50,
  191. setFillColorSpace: 51,
  192. setStrokeColor: 52,
  193. setStrokeColorN: 53,
  194. setFillColor: 54,
  195. setFillColorN: 55,
  196. setStrokeGray: 56,
  197. setFillGray: 57,
  198. setStrokeRGBColor: 58,
  199. setFillRGBColor: 59,
  200. setStrokeCMYKColor: 60,
  201. setFillCMYKColor: 61,
  202. shadingFill: 62,
  203. beginInlineImage: 63,
  204. beginImageData: 64,
  205. endInlineImage: 65,
  206. paintXObject: 66,
  207. markPoint: 67,
  208. markPointProps: 68,
  209. beginMarkedContent: 69,
  210. beginMarkedContentProps: 70,
  211. endMarkedContent: 71,
  212. beginCompat: 72,
  213. endCompat: 73,
  214. paintFormXObjectBegin: 74,
  215. paintFormXObjectEnd: 75,
  216. beginGroup: 76,
  217. endGroup: 77,
  218. beginAnnotations: 78,
  219. endAnnotations: 79,
  220. beginAnnotation: 80,
  221. endAnnotation: 81,
  222. paintJpegXObject: 82,
  223. paintImageMaskXObject: 83,
  224. paintImageMaskXObjectGroup: 84,
  225. paintImageXObject: 85,
  226. paintInlineImageXObject: 86,
  227. paintInlineImageXObjectGroup: 87,
  228. paintImageXObjectRepeat: 88,
  229. paintImageMaskXObjectRepeat: 89,
  230. paintSolidColorImageMask: 90,
  231. constructPath: 91
  232. };
  233. var verbosity = VERBOSITY_LEVELS.warnings;
  234. function setVerbosityLevel(level) {
  235. verbosity = level;
  236. }
  237. function getVerbosityLevel() {
  238. return verbosity;
  239. }
  240. function info(msg) {
  241. if (verbosity >= VERBOSITY_LEVELS.infos) {
  242. console.log('Info: ' + msg);
  243. }
  244. }
  245. function warn(msg) {
  246. if (verbosity >= VERBOSITY_LEVELS.warnings) {
  247. console.log('Warning: ' + msg);
  248. }
  249. }
  250. function deprecated(details) {
  251. console.log('Deprecated API usage: ' + details);
  252. }
  253. function error(msg) {
  254. if (verbosity >= VERBOSITY_LEVELS.errors) {
  255. console.log('Error: ' + msg);
  256. console.log(backtrace());
  257. }
  258. throw new Error(msg);
  259. }
  260. function backtrace() {
  261. try {
  262. throw new Error();
  263. } catch (e) {
  264. return e.stack ? e.stack.split('\n').slice(2).join('\n') : '';
  265. }
  266. }
  267. function assert(cond, msg) {
  268. if (!cond) {
  269. error(msg);
  270. }
  271. }
  272. var UNSUPPORTED_FEATURES = {
  273. unknown: 'unknown',
  274. forms: 'forms',
  275. javaScript: 'javaScript',
  276. smask: 'smask',
  277. shadingPattern: 'shadingPattern',
  278. font: 'font'
  279. };
  280. function isSameOrigin(baseUrl, otherUrl) {
  281. try {
  282. var base = new URL(baseUrl);
  283. if (!base.origin || base.origin === 'null') {
  284. return false;
  285. }
  286. } catch (e) {
  287. return false;
  288. }
  289. var other = new URL(otherUrl, base);
  290. return base.origin === other.origin;
  291. }
  292. function isValidProtocol(url) {
  293. if (!url) {
  294. return false;
  295. }
  296. switch (url.protocol) {
  297. case 'http:':
  298. case 'https:':
  299. case 'ftp:':
  300. case 'mailto:':
  301. case 'tel:':
  302. return true;
  303. default:
  304. return false;
  305. }
  306. }
  307. function createValidAbsoluteUrl(url, baseUrl) {
  308. if (!url) {
  309. return null;
  310. }
  311. try {
  312. var absoluteUrl = baseUrl ? new URL(url, baseUrl) : new URL(url);
  313. if (isValidProtocol(absoluteUrl)) {
  314. return absoluteUrl;
  315. }
  316. } catch (ex) {}
  317. return null;
  318. }
  319. function shadow(obj, prop, value) {
  320. Object.defineProperty(obj, prop, {
  321. value: value,
  322. enumerable: true,
  323. configurable: true,
  324. writable: false
  325. });
  326. return value;
  327. }
  328. function getLookupTableFactory(initializer) {
  329. var lookup;
  330. return function () {
  331. if (initializer) {
  332. lookup = Object.create(null);
  333. initializer(lookup);
  334. initializer = null;
  335. }
  336. return lookup;
  337. };
  338. }
  339. var PasswordResponses = {
  340. NEED_PASSWORD: 1,
  341. INCORRECT_PASSWORD: 2
  342. };
  343. var PasswordException = function PasswordExceptionClosure() {
  344. function PasswordException(msg, code) {
  345. this.name = 'PasswordException';
  346. this.message = msg;
  347. this.code = code;
  348. }
  349. PasswordException.prototype = new Error();
  350. PasswordException.constructor = PasswordException;
  351. return PasswordException;
  352. }();
  353. var UnknownErrorException = function UnknownErrorExceptionClosure() {
  354. function UnknownErrorException(msg, details) {
  355. this.name = 'UnknownErrorException';
  356. this.message = msg;
  357. this.details = details;
  358. }
  359. UnknownErrorException.prototype = new Error();
  360. UnknownErrorException.constructor = UnknownErrorException;
  361. return UnknownErrorException;
  362. }();
  363. var InvalidPDFException = function InvalidPDFExceptionClosure() {
  364. function InvalidPDFException(msg) {
  365. this.name = 'InvalidPDFException';
  366. this.message = msg;
  367. }
  368. InvalidPDFException.prototype = new Error();
  369. InvalidPDFException.constructor = InvalidPDFException;
  370. return InvalidPDFException;
  371. }();
  372. var MissingPDFException = function MissingPDFExceptionClosure() {
  373. function MissingPDFException(msg) {
  374. this.name = 'MissingPDFException';
  375. this.message = msg;
  376. }
  377. MissingPDFException.prototype = new Error();
  378. MissingPDFException.constructor = MissingPDFException;
  379. return MissingPDFException;
  380. }();
  381. var UnexpectedResponseException = function UnexpectedResponseExceptionClosure() {
  382. function UnexpectedResponseException(msg, status) {
  383. this.name = 'UnexpectedResponseException';
  384. this.message = msg;
  385. this.status = status;
  386. }
  387. UnexpectedResponseException.prototype = new Error();
  388. UnexpectedResponseException.constructor = UnexpectedResponseException;
  389. return UnexpectedResponseException;
  390. }();
  391. var NotImplementedException = function NotImplementedExceptionClosure() {
  392. function NotImplementedException(msg) {
  393. this.message = msg;
  394. }
  395. NotImplementedException.prototype = new Error();
  396. NotImplementedException.prototype.name = 'NotImplementedException';
  397. NotImplementedException.constructor = NotImplementedException;
  398. return NotImplementedException;
  399. }();
  400. var MissingDataException = function MissingDataExceptionClosure() {
  401. function MissingDataException(begin, end) {
  402. this.begin = begin;
  403. this.end = end;
  404. this.message = 'Missing data [' + begin + ', ' + end + ')';
  405. }
  406. MissingDataException.prototype = new Error();
  407. MissingDataException.prototype.name = 'MissingDataException';
  408. MissingDataException.constructor = MissingDataException;
  409. return MissingDataException;
  410. }();
  411. var XRefParseException = function XRefParseExceptionClosure() {
  412. function XRefParseException(msg) {
  413. this.message = msg;
  414. }
  415. XRefParseException.prototype = new Error();
  416. XRefParseException.prototype.name = 'XRefParseException';
  417. XRefParseException.constructor = XRefParseException;
  418. return XRefParseException;
  419. }();
  420. var NullCharactersRegExp = /\x00/g;
  421. function removeNullCharacters(str) {
  422. if (typeof str !== 'string') {
  423. warn('The argument for removeNullCharacters must be a string.');
  424. return str;
  425. }
  426. return str.replace(NullCharactersRegExp, '');
  427. }
  428. function bytesToString(bytes) {
  429. assert(bytes !== null && (typeof bytes === 'undefined' ? 'undefined' : _typeof(bytes)) === 'object' && bytes.length !== undefined, 'Invalid argument for bytesToString');
  430. var length = bytes.length;
  431. var MAX_ARGUMENT_COUNT = 8192;
  432. if (length < MAX_ARGUMENT_COUNT) {
  433. return String.fromCharCode.apply(null, bytes);
  434. }
  435. var strBuf = [];
  436. for (var i = 0; i < length; i += MAX_ARGUMENT_COUNT) {
  437. var chunkEnd = Math.min(i + MAX_ARGUMENT_COUNT, length);
  438. var chunk = bytes.subarray(i, chunkEnd);
  439. strBuf.push(String.fromCharCode.apply(null, chunk));
  440. }
  441. return strBuf.join('');
  442. }
  443. function stringToBytes(str) {
  444. assert(typeof str === 'string', 'Invalid argument for stringToBytes');
  445. var length = str.length;
  446. var bytes = new Uint8Array(length);
  447. for (var i = 0; i < length; ++i) {
  448. bytes[i] = str.charCodeAt(i) & 0xFF;
  449. }
  450. return bytes;
  451. }
  452. function arrayByteLength(arr) {
  453. if (arr.length !== undefined) {
  454. return arr.length;
  455. }
  456. assert(arr.byteLength !== undefined);
  457. return arr.byteLength;
  458. }
  459. function arraysToBytes(arr) {
  460. if (arr.length === 1 && arr[0] instanceof Uint8Array) {
  461. return arr[0];
  462. }
  463. var resultLength = 0;
  464. var i,
  465. ii = arr.length;
  466. var item, itemLength;
  467. for (i = 0; i < ii; i++) {
  468. item = arr[i];
  469. itemLength = arrayByteLength(item);
  470. resultLength += itemLength;
  471. }
  472. var pos = 0;
  473. var data = new Uint8Array(resultLength);
  474. for (i = 0; i < ii; i++) {
  475. item = arr[i];
  476. if (!(item instanceof Uint8Array)) {
  477. if (typeof item === 'string') {
  478. item = stringToBytes(item);
  479. } else {
  480. item = new Uint8Array(item);
  481. }
  482. }
  483. itemLength = item.byteLength;
  484. data.set(item, pos);
  485. pos += itemLength;
  486. }
  487. return data;
  488. }
  489. function string32(value) {
  490. return String.fromCharCode(value >> 24 & 0xff, value >> 16 & 0xff, value >> 8 & 0xff, value & 0xff);
  491. }
  492. function log2(x) {
  493. var n = 1,
  494. i = 0;
  495. while (x > n) {
  496. n <<= 1;
  497. i++;
  498. }
  499. return i;
  500. }
  501. function readInt8(data, start) {
  502. return data[start] << 24 >> 24;
  503. }
  504. function readUint16(data, offset) {
  505. return data[offset] << 8 | data[offset + 1];
  506. }
  507. function readUint32(data, offset) {
  508. return (data[offset] << 24 | data[offset + 1] << 16 | data[offset + 2] << 8 | data[offset + 3]) >>> 0;
  509. }
  510. function isLittleEndian() {
  511. var buffer8 = new Uint8Array(4);
  512. buffer8[0] = 1;
  513. var view32 = new Uint32Array(buffer8.buffer, 0, 1);
  514. return view32[0] === 1;
  515. }
  516. function isEvalSupported() {
  517. try {
  518. new Function('');
  519. return true;
  520. } catch (e) {
  521. return false;
  522. }
  523. }
  524. var IDENTITY_MATRIX = [1, 0, 0, 1, 0, 0];
  525. var Util = function UtilClosure() {
  526. function Util() {}
  527. var rgbBuf = ['rgb(', 0, ',', 0, ',', 0, ')'];
  528. Util.makeCssRgb = function Util_makeCssRgb(r, g, b) {
  529. rgbBuf[1] = r;
  530. rgbBuf[3] = g;
  531. rgbBuf[5] = b;
  532. return rgbBuf.join('');
  533. };
  534. Util.transform = function Util_transform(m1, m2) {
  535. 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]];
  536. };
  537. Util.applyTransform = function Util_applyTransform(p, m) {
  538. var xt = p[0] * m[0] + p[1] * m[2] + m[4];
  539. var yt = p[0] * m[1] + p[1] * m[3] + m[5];
  540. return [xt, yt];
  541. };
  542. Util.applyInverseTransform = function Util_applyInverseTransform(p, m) {
  543. var d = m[0] * m[3] - m[1] * m[2];
  544. var xt = (p[0] * m[3] - p[1] * m[2] + m[2] * m[5] - m[4] * m[3]) / d;
  545. var yt = (-p[0] * m[1] + p[1] * m[0] + m[4] * m[1] - m[5] * m[0]) / d;
  546. return [xt, yt];
  547. };
  548. Util.getAxialAlignedBoundingBox = function Util_getAxialAlignedBoundingBox(r, m) {
  549. var p1 = Util.applyTransform(r, m);
  550. var p2 = Util.applyTransform(r.slice(2, 4), m);
  551. var p3 = Util.applyTransform([r[0], r[3]], m);
  552. var p4 = Util.applyTransform([r[2], r[1]], m);
  553. 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])];
  554. };
  555. Util.inverseTransform = function Util_inverseTransform(m) {
  556. var d = m[0] * m[3] - m[1] * m[2];
  557. 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];
  558. };
  559. Util.apply3dTransform = function Util_apply3dTransform(m, v) {
  560. 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]];
  561. };
  562. Util.singularValueDecompose2dScale = function Util_singularValueDecompose2dScale(m) {
  563. var transpose = [m[0], m[2], m[1], m[3]];
  564. var a = m[0] * transpose[0] + m[1] * transpose[2];
  565. var b = m[0] * transpose[1] + m[1] * transpose[3];
  566. var c = m[2] * transpose[0] + m[3] * transpose[2];
  567. var d = m[2] * transpose[1] + m[3] * transpose[3];
  568. var first = (a + d) / 2;
  569. var second = Math.sqrt((a + d) * (a + d) - 4 * (a * d - c * b)) / 2;
  570. var sx = first + second || 1;
  571. var sy = first - second || 1;
  572. return [Math.sqrt(sx), Math.sqrt(sy)];
  573. };
  574. Util.normalizeRect = function Util_normalizeRect(rect) {
  575. var r = rect.slice(0);
  576. if (rect[0] > rect[2]) {
  577. r[0] = rect[2];
  578. r[2] = rect[0];
  579. }
  580. if (rect[1] > rect[3]) {
  581. r[1] = rect[3];
  582. r[3] = rect[1];
  583. }
  584. return r;
  585. };
  586. Util.intersect = function Util_intersect(rect1, rect2) {
  587. function compare(a, b) {
  588. return a - b;
  589. }
  590. var orderedX = [rect1[0], rect1[2], rect2[0], rect2[2]].sort(compare),
  591. orderedY = [rect1[1], rect1[3], rect2[1], rect2[3]].sort(compare),
  592. result = [];
  593. rect1 = Util.normalizeRect(rect1);
  594. rect2 = Util.normalizeRect(rect2);
  595. if (orderedX[0] === rect1[0] && orderedX[1] === rect2[0] || orderedX[0] === rect2[0] && orderedX[1] === rect1[0]) {
  596. result[0] = orderedX[1];
  597. result[2] = orderedX[2];
  598. } else {
  599. return false;
  600. }
  601. if (orderedY[0] === rect1[1] && orderedY[1] === rect2[1] || orderedY[0] === rect2[1] && orderedY[1] === rect1[1]) {
  602. result[1] = orderedY[1];
  603. result[3] = orderedY[2];
  604. } else {
  605. return false;
  606. }
  607. return result;
  608. };
  609. Util.sign = function Util_sign(num) {
  610. return num < 0 ? -1 : 1;
  611. };
  612. 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'];
  613. Util.toRoman = function Util_toRoman(number, lowerCase) {
  614. assert(isInt(number) && number > 0, 'The number should be a positive integer.');
  615. var pos,
  616. romanBuf = [];
  617. while (number >= 1000) {
  618. number -= 1000;
  619. romanBuf.push('M');
  620. }
  621. pos = number / 100 | 0;
  622. number %= 100;
  623. romanBuf.push(ROMAN_NUMBER_MAP[pos]);
  624. pos = number / 10 | 0;
  625. number %= 10;
  626. romanBuf.push(ROMAN_NUMBER_MAP[10 + pos]);
  627. romanBuf.push(ROMAN_NUMBER_MAP[20 + number]);
  628. var romanStr = romanBuf.join('');
  629. return lowerCase ? romanStr.toLowerCase() : romanStr;
  630. };
  631. Util.appendToArray = function Util_appendToArray(arr1, arr2) {
  632. Array.prototype.push.apply(arr1, arr2);
  633. };
  634. Util.prependToArray = function Util_prependToArray(arr1, arr2) {
  635. Array.prototype.unshift.apply(arr1, arr2);
  636. };
  637. Util.extendObj = function extendObj(obj1, obj2) {
  638. for (var key in obj2) {
  639. obj1[key] = obj2[key];
  640. }
  641. };
  642. Util.getInheritableProperty = function Util_getInheritableProperty(dict, name, getArray) {
  643. while (dict && !dict.has(name)) {
  644. dict = dict.get('Parent');
  645. }
  646. if (!dict) {
  647. return null;
  648. }
  649. return getArray ? dict.getArray(name) : dict.get(name);
  650. };
  651. Util.inherit = function Util_inherit(sub, base, prototype) {
  652. sub.prototype = Object.create(base.prototype);
  653. sub.prototype.constructor = sub;
  654. for (var prop in prototype) {
  655. sub.prototype[prop] = prototype[prop];
  656. }
  657. };
  658. Util.loadScript = function Util_loadScript(src, callback) {
  659. var script = document.createElement('script');
  660. var loaded = false;
  661. script.setAttribute('src', src);
  662. if (callback) {
  663. script.onload = function () {
  664. if (!loaded) {
  665. callback();
  666. }
  667. loaded = true;
  668. };
  669. }
  670. document.getElementsByTagName('head')[0].appendChild(script);
  671. };
  672. return Util;
  673. }();
  674. var PageViewport = function PageViewportClosure() {
  675. function PageViewport(viewBox, scale, rotation, offsetX, offsetY, dontFlip) {
  676. this.viewBox = viewBox;
  677. this.scale = scale;
  678. this.rotation = rotation;
  679. this.offsetX = offsetX;
  680. this.offsetY = offsetY;
  681. var centerX = (viewBox[2] + viewBox[0]) / 2;
  682. var centerY = (viewBox[3] + viewBox[1]) / 2;
  683. var rotateA, rotateB, rotateC, rotateD;
  684. rotation = rotation % 360;
  685. rotation = rotation < 0 ? rotation + 360 : rotation;
  686. switch (rotation) {
  687. case 180:
  688. rotateA = -1;
  689. rotateB = 0;
  690. rotateC = 0;
  691. rotateD = 1;
  692. break;
  693. case 90:
  694. rotateA = 0;
  695. rotateB = 1;
  696. rotateC = 1;
  697. rotateD = 0;
  698. break;
  699. case 270:
  700. rotateA = 0;
  701. rotateB = -1;
  702. rotateC = -1;
  703. rotateD = 0;
  704. break;
  705. default:
  706. rotateA = 1;
  707. rotateB = 0;
  708. rotateC = 0;
  709. rotateD = -1;
  710. break;
  711. }
  712. if (dontFlip) {
  713. rotateC = -rotateC;
  714. rotateD = -rotateD;
  715. }
  716. var offsetCanvasX, offsetCanvasY;
  717. var width, height;
  718. if (rotateA === 0) {
  719. offsetCanvasX = Math.abs(centerY - viewBox[1]) * scale + offsetX;
  720. offsetCanvasY = Math.abs(centerX - viewBox[0]) * scale + offsetY;
  721. width = Math.abs(viewBox[3] - viewBox[1]) * scale;
  722. height = Math.abs(viewBox[2] - viewBox[0]) * scale;
  723. } else {
  724. offsetCanvasX = Math.abs(centerX - viewBox[0]) * scale + offsetX;
  725. offsetCanvasY = Math.abs(centerY - viewBox[1]) * scale + offsetY;
  726. width = Math.abs(viewBox[2] - viewBox[0]) * scale;
  727. height = Math.abs(viewBox[3] - viewBox[1]) * scale;
  728. }
  729. this.transform = [rotateA * scale, rotateB * scale, rotateC * scale, rotateD * scale, offsetCanvasX - rotateA * scale * centerX - rotateC * scale * centerY, offsetCanvasY - rotateB * scale * centerX - rotateD * scale * centerY];
  730. this.width = width;
  731. this.height = height;
  732. this.fontScale = scale;
  733. }
  734. PageViewport.prototype = {
  735. clone: function PageViewPort_clone(args) {
  736. args = args || {};
  737. var scale = 'scale' in args ? args.scale : this.scale;
  738. var rotation = 'rotation' in args ? args.rotation : this.rotation;
  739. return new PageViewport(this.viewBox.slice(), scale, rotation, this.offsetX, this.offsetY, args.dontFlip);
  740. },
  741. convertToViewportPoint: function PageViewport_convertToViewportPoint(x, y) {
  742. return Util.applyTransform([x, y], this.transform);
  743. },
  744. convertToViewportRectangle: function PageViewport_convertToViewportRectangle(rect) {
  745. var tl = Util.applyTransform([rect[0], rect[1]], this.transform);
  746. var br = Util.applyTransform([rect[2], rect[3]], this.transform);
  747. return [tl[0], tl[1], br[0], br[1]];
  748. },
  749. convertToPdfPoint: function PageViewport_convertToPdfPoint(x, y) {
  750. return Util.applyInverseTransform([x, y], this.transform);
  751. }
  752. };
  753. return PageViewport;
  754. }();
  755. 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];
  756. function stringToPDFString(str) {
  757. var i,
  758. n = str.length,
  759. strBuf = [];
  760. if (str[0] === '\xFE' && str[1] === '\xFF') {
  761. for (i = 2; i < n; i += 2) {
  762. strBuf.push(String.fromCharCode(str.charCodeAt(i) << 8 | str.charCodeAt(i + 1)));
  763. }
  764. } else {
  765. for (i = 0; i < n; ++i) {
  766. var code = PDFStringTranslateTable[str.charCodeAt(i)];
  767. strBuf.push(code ? String.fromCharCode(code) : str.charAt(i));
  768. }
  769. }
  770. return strBuf.join('');
  771. }
  772. function stringToUTF8String(str) {
  773. return decodeURIComponent(escape(str));
  774. }
  775. function utf8StringToString(str) {
  776. return unescape(encodeURIComponent(str));
  777. }
  778. function isEmptyObj(obj) {
  779. for (var key in obj) {
  780. return false;
  781. }
  782. return true;
  783. }
  784. function isBool(v) {
  785. return typeof v === 'boolean';
  786. }
  787. function isInt(v) {
  788. return typeof v === 'number' && (v | 0) === v;
  789. }
  790. function isNum(v) {
  791. return typeof v === 'number';
  792. }
  793. function isString(v) {
  794. return typeof v === 'string';
  795. }
  796. function isArray(v) {
  797. return v instanceof Array;
  798. }
  799. function isArrayBuffer(v) {
  800. return (typeof v === 'undefined' ? 'undefined' : _typeof(v)) === 'object' && v !== null && v.byteLength !== undefined;
  801. }
  802. function isSpace(ch) {
  803. return ch === 0x20 || ch === 0x09 || ch === 0x0D || ch === 0x0A;
  804. }
  805. function isNodeJS() {
  806. if (typeof __pdfjsdev_webpack__ === 'undefined') {
  807. return (typeof process === 'undefined' ? 'undefined' : _typeof(process)) === 'object' && process + '' === '[object process]';
  808. }
  809. return false;
  810. }
  811. function createPromiseCapability() {
  812. var capability = {};
  813. capability.promise = new Promise(function (resolve, reject) {
  814. capability.resolve = resolve;
  815. capability.reject = reject;
  816. });
  817. return capability;
  818. }
  819. var StatTimer = function StatTimerClosure() {
  820. function rpad(str, pad, length) {
  821. while (str.length < length) {
  822. str += pad;
  823. }
  824. return str;
  825. }
  826. function StatTimer() {
  827. this.started = Object.create(null);
  828. this.times = [];
  829. this.enabled = true;
  830. }
  831. StatTimer.prototype = {
  832. time: function StatTimer_time(name) {
  833. if (!this.enabled) {
  834. return;
  835. }
  836. if (name in this.started) {
  837. warn('Timer is already running for ' + name);
  838. }
  839. this.started[name] = Date.now();
  840. },
  841. timeEnd: function StatTimer_timeEnd(name) {
  842. if (!this.enabled) {
  843. return;
  844. }
  845. if (!(name in this.started)) {
  846. warn('Timer has not been started for ' + name);
  847. }
  848. this.times.push({
  849. 'name': name,
  850. 'start': this.started[name],
  851. 'end': Date.now()
  852. });
  853. delete this.started[name];
  854. },
  855. toString: function StatTimer_toString() {
  856. var i, ii;
  857. var times = this.times;
  858. var out = '';
  859. var longest = 0;
  860. for (i = 0, ii = times.length; i < ii; ++i) {
  861. var name = times[i]['name'];
  862. if (name.length > longest) {
  863. longest = name.length;
  864. }
  865. }
  866. for (i = 0, ii = times.length; i < ii; ++i) {
  867. var span = times[i];
  868. var duration = span.end - span.start;
  869. out += rpad(span['name'], ' ', longest) + ' ' + duration + 'ms\n';
  870. }
  871. return out;
  872. }
  873. };
  874. return StatTimer;
  875. }();
  876. var createBlob = function createBlob(data, contentType) {
  877. if (typeof Blob !== 'undefined') {
  878. return new Blob([data], { type: contentType });
  879. }
  880. throw new Error('The "Blob" constructor is not supported.');
  881. };
  882. var createObjectURL = function createObjectURLClosure() {
  883. var digits = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/=';
  884. return function createObjectURL(data, contentType) {
  885. var forceDataSchema = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : false;
  886. if (!forceDataSchema && URL.createObjectURL) {
  887. var blob = createBlob(data, contentType);
  888. return URL.createObjectURL(blob);
  889. }
  890. var buffer = 'data:' + contentType + ';base64,';
  891. for (var i = 0, ii = data.length; i < ii; i += 3) {
  892. var b1 = data[i] & 0xFF;
  893. var b2 = data[i + 1] & 0xFF;
  894. var b3 = data[i + 2] & 0xFF;
  895. var d1 = b1 >> 2,
  896. d2 = (b1 & 3) << 4 | b2 >> 4;
  897. var d3 = i + 1 < ii ? (b2 & 0xF) << 2 | b3 >> 6 : 64;
  898. var d4 = i + 2 < ii ? b3 & 0x3F : 64;
  899. buffer += digits[d1] + digits[d2] + digits[d3] + digits[d4];
  900. }
  901. return buffer;
  902. };
  903. }();
  904. function MessageHandler(sourceName, targetName, comObj) {
  905. var _this = this;
  906. this.sourceName = sourceName;
  907. this.targetName = targetName;
  908. this.comObj = comObj;
  909. this.callbackIndex = 1;
  910. this.postMessageTransfers = true;
  911. var callbacksCapabilities = this.callbacksCapabilities = Object.create(null);
  912. var ah = this.actionHandler = Object.create(null);
  913. this._onComObjOnMessage = function (event) {
  914. var data = event.data;
  915. if (data.targetName !== _this.sourceName) {
  916. return;
  917. }
  918. if (data.isReply) {
  919. var callbackId = data.callbackId;
  920. if (data.callbackId in callbacksCapabilities) {
  921. var callback = callbacksCapabilities[callbackId];
  922. delete callbacksCapabilities[callbackId];
  923. if ('error' in data) {
  924. callback.reject(data.error);
  925. } else {
  926. callback.resolve(data.data);
  927. }
  928. } else {
  929. error('Cannot resolve callback ' + callbackId);
  930. }
  931. } else if (data.action in ah) {
  932. var action = ah[data.action];
  933. if (data.callbackId) {
  934. var sourceName = _this.sourceName;
  935. var targetName = data.sourceName;
  936. Promise.resolve().then(function () {
  937. return action[0].call(action[1], data.data);
  938. }).then(function (result) {
  939. comObj.postMessage({
  940. sourceName: sourceName,
  941. targetName: targetName,
  942. isReply: true,
  943. callbackId: data.callbackId,
  944. data: result
  945. });
  946. }, function (reason) {
  947. if (reason instanceof Error) {
  948. reason = reason + '';
  949. }
  950. comObj.postMessage({
  951. sourceName: sourceName,
  952. targetName: targetName,
  953. isReply: true,
  954. callbackId: data.callbackId,
  955. error: reason
  956. });
  957. });
  958. } else {
  959. action[0].call(action[1], data.data);
  960. }
  961. } else {
  962. error('Unknown action from worker: ' + data.action);
  963. }
  964. };
  965. comObj.addEventListener('message', this._onComObjOnMessage);
  966. }
  967. MessageHandler.prototype = {
  968. on: function on(actionName, handler, scope) {
  969. var ah = this.actionHandler;
  970. if (ah[actionName]) {
  971. error('There is already an actionName called "' + actionName + '"');
  972. }
  973. ah[actionName] = [handler, scope];
  974. },
  975. send: function send(actionName, data, transfers) {
  976. var message = {
  977. sourceName: this.sourceName,
  978. targetName: this.targetName,
  979. action: actionName,
  980. data: data
  981. };
  982. this.postMessage(message, transfers);
  983. },
  984. sendWithPromise: function sendWithPromise(actionName, data, transfers) {
  985. var callbackId = this.callbackIndex++;
  986. var message = {
  987. sourceName: this.sourceName,
  988. targetName: this.targetName,
  989. action: actionName,
  990. data: data,
  991. callbackId: callbackId
  992. };
  993. var capability = createPromiseCapability();
  994. this.callbacksCapabilities[callbackId] = capability;
  995. try {
  996. this.postMessage(message, transfers);
  997. } catch (e) {
  998. capability.reject(e);
  999. }
  1000. return capability.promise;
  1001. },
  1002. postMessage: function postMessage(message, transfers) {
  1003. if (transfers && this.postMessageTransfers) {
  1004. this.comObj.postMessage(message, transfers);
  1005. } else {
  1006. this.comObj.postMessage(message);
  1007. }
  1008. },
  1009. destroy: function destroy() {
  1010. this.comObj.removeEventListener('message', this._onComObjOnMessage);
  1011. }
  1012. };
  1013. function loadJpegStream(id, imageUrl, objs) {
  1014. var img = new Image();
  1015. img.onload = function loadJpegStream_onloadClosure() {
  1016. objs.resolve(id, img);
  1017. };
  1018. img.onerror = function loadJpegStream_onerrorClosure() {
  1019. objs.resolve(id, null);
  1020. warn('Error during JPEG image loading');
  1021. };
  1022. img.src = imageUrl;
  1023. }
  1024. exports.FONT_IDENTITY_MATRIX = FONT_IDENTITY_MATRIX;
  1025. exports.IDENTITY_MATRIX = IDENTITY_MATRIX;
  1026. exports.OPS = OPS;
  1027. exports.VERBOSITY_LEVELS = VERBOSITY_LEVELS;
  1028. exports.UNSUPPORTED_FEATURES = UNSUPPORTED_FEATURES;
  1029. exports.AnnotationBorderStyleType = AnnotationBorderStyleType;
  1030. exports.AnnotationFieldFlag = AnnotationFieldFlag;
  1031. exports.AnnotationFlag = AnnotationFlag;
  1032. exports.AnnotationType = AnnotationType;
  1033. exports.FontType = FontType;
  1034. exports.ImageKind = ImageKind;
  1035. exports.CMapCompressionType = CMapCompressionType;
  1036. exports.InvalidPDFException = InvalidPDFException;
  1037. exports.MessageHandler = MessageHandler;
  1038. exports.MissingDataException = MissingDataException;
  1039. exports.MissingPDFException = MissingPDFException;
  1040. exports.NotImplementedException = NotImplementedException;
  1041. exports.PageViewport = PageViewport;
  1042. exports.PasswordException = PasswordException;
  1043. exports.PasswordResponses = PasswordResponses;
  1044. exports.StatTimer = StatTimer;
  1045. exports.StreamType = StreamType;
  1046. exports.TextRenderingMode = TextRenderingMode;
  1047. exports.UnexpectedResponseException = UnexpectedResponseException;
  1048. exports.UnknownErrorException = UnknownErrorException;
  1049. exports.Util = Util;
  1050. exports.XRefParseException = XRefParseException;
  1051. exports.arrayByteLength = arrayByteLength;
  1052. exports.arraysToBytes = arraysToBytes;
  1053. exports.assert = assert;
  1054. exports.bytesToString = bytesToString;
  1055. exports.createBlob = createBlob;
  1056. exports.createPromiseCapability = createPromiseCapability;
  1057. exports.createObjectURL = createObjectURL;
  1058. exports.deprecated = deprecated;
  1059. exports.error = error;
  1060. exports.getLookupTableFactory = getLookupTableFactory;
  1061. exports.getVerbosityLevel = getVerbosityLevel;
  1062. exports.globalScope = globalScope;
  1063. exports.info = info;
  1064. exports.isArray = isArray;
  1065. exports.isArrayBuffer = isArrayBuffer;
  1066. exports.isBool = isBool;
  1067. exports.isEmptyObj = isEmptyObj;
  1068. exports.isInt = isInt;
  1069. exports.isNum = isNum;
  1070. exports.isString = isString;
  1071. exports.isSpace = isSpace;
  1072. exports.isNodeJS = isNodeJS;
  1073. exports.isSameOrigin = isSameOrigin;
  1074. exports.createValidAbsoluteUrl = createValidAbsoluteUrl;
  1075. exports.isLittleEndian = isLittleEndian;
  1076. exports.isEvalSupported = isEvalSupported;
  1077. exports.loadJpegStream = loadJpegStream;
  1078. exports.log2 = log2;
  1079. exports.readInt8 = readInt8;
  1080. exports.readUint16 = readUint16;
  1081. exports.readUint32 = readUint32;
  1082. exports.removeNullCharacters = removeNullCharacters;
  1083. exports.setVerbosityLevel = setVerbosityLevel;
  1084. exports.shadow = shadow;
  1085. exports.string32 = string32;
  1086. exports.stringToBytes = stringToBytes;
  1087. exports.stringToPDFString = stringToPDFString;
  1088. exports.stringToUTF8String = stringToUTF8String;
  1089. exports.utf8StringToString = utf8StringToString;
  1090. exports.warn = warn;