util.js 32 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090
  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. warn('The "Blob" constructor is not supported.');
  881. };
  882. var createObjectURL = function createObjectURLClosure() {
  883. var digits = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/=';
  884. return function createObjectURL(data, contentType, forceDataSchema) {
  885. if (!forceDataSchema && typeof URL !== 'undefined' && URL.createObjectURL) {
  886. var blob = createBlob(data, contentType);
  887. return URL.createObjectURL(blob);
  888. }
  889. var buffer = 'data:' + contentType + ';base64,';
  890. for (var i = 0, ii = data.length; i < ii; i += 3) {
  891. var b1 = data[i] & 0xFF;
  892. var b2 = data[i + 1] & 0xFF;
  893. var b3 = data[i + 2] & 0xFF;
  894. var d1 = b1 >> 2,
  895. d2 = (b1 & 3) << 4 | b2 >> 4;
  896. var d3 = i + 1 < ii ? (b2 & 0xF) << 2 | b3 >> 6 : 64;
  897. var d4 = i + 2 < ii ? b3 & 0x3F : 64;
  898. buffer += digits[d1] + digits[d2] + digits[d3] + digits[d4];
  899. }
  900. return buffer;
  901. };
  902. }();
  903. function MessageHandler(sourceName, targetName, comObj) {
  904. this.sourceName = sourceName;
  905. this.targetName = targetName;
  906. this.comObj = comObj;
  907. this.callbackIndex = 1;
  908. this.postMessageTransfers = true;
  909. var callbacksCapabilities = this.callbacksCapabilities = Object.create(null);
  910. var ah = this.actionHandler = Object.create(null);
  911. this._onComObjOnMessage = function messageHandlerComObjOnMessage(event) {
  912. var data = event.data;
  913. if (data.targetName !== this.sourceName) {
  914. return;
  915. }
  916. if (data.isReply) {
  917. var callbackId = data.callbackId;
  918. if (data.callbackId in callbacksCapabilities) {
  919. var callback = callbacksCapabilities[callbackId];
  920. delete callbacksCapabilities[callbackId];
  921. if ('error' in data) {
  922. callback.reject(data.error);
  923. } else {
  924. callback.resolve(data.data);
  925. }
  926. } else {
  927. error('Cannot resolve callback ' + callbackId);
  928. }
  929. } else if (data.action in ah) {
  930. var action = ah[data.action];
  931. if (data.callbackId) {
  932. var sourceName = this.sourceName;
  933. var targetName = data.sourceName;
  934. Promise.resolve().then(function () {
  935. return action[0].call(action[1], data.data);
  936. }).then(function (result) {
  937. comObj.postMessage({
  938. sourceName: sourceName,
  939. targetName: targetName,
  940. isReply: true,
  941. callbackId: data.callbackId,
  942. data: result
  943. });
  944. }, function (reason) {
  945. if (reason instanceof Error) {
  946. reason = reason + '';
  947. }
  948. comObj.postMessage({
  949. sourceName: sourceName,
  950. targetName: targetName,
  951. isReply: true,
  952. callbackId: data.callbackId,
  953. error: reason
  954. });
  955. });
  956. } else {
  957. action[0].call(action[1], data.data);
  958. }
  959. } else {
  960. error('Unknown action from worker: ' + data.action);
  961. }
  962. }.bind(this);
  963. comObj.addEventListener('message', this._onComObjOnMessage);
  964. }
  965. MessageHandler.prototype = {
  966. on: function messageHandlerOn(actionName, handler, scope) {
  967. var ah = this.actionHandler;
  968. if (ah[actionName]) {
  969. error('There is already an actionName called "' + actionName + '"');
  970. }
  971. ah[actionName] = [handler, scope];
  972. },
  973. send: function messageHandlerSend(actionName, data, transfers) {
  974. var message = {
  975. sourceName: this.sourceName,
  976. targetName: this.targetName,
  977. action: actionName,
  978. data: data
  979. };
  980. this.postMessage(message, transfers);
  981. },
  982. sendWithPromise: function messageHandlerSendWithPromise(actionName, data, transfers) {
  983. var callbackId = this.callbackIndex++;
  984. var message = {
  985. sourceName: this.sourceName,
  986. targetName: this.targetName,
  987. action: actionName,
  988. data: data,
  989. callbackId: callbackId
  990. };
  991. var capability = createPromiseCapability();
  992. this.callbacksCapabilities[callbackId] = capability;
  993. try {
  994. this.postMessage(message, transfers);
  995. } catch (e) {
  996. capability.reject(e);
  997. }
  998. return capability.promise;
  999. },
  1000. postMessage: function postMessage(message, transfers) {
  1001. if (transfers && this.postMessageTransfers) {
  1002. this.comObj.postMessage(message, transfers);
  1003. } else {
  1004. this.comObj.postMessage(message);
  1005. }
  1006. },
  1007. destroy: function destroy() {
  1008. this.comObj.removeEventListener('message', this._onComObjOnMessage);
  1009. }
  1010. };
  1011. function loadJpegStream(id, imageUrl, objs) {
  1012. var img = new Image();
  1013. img.onload = function loadJpegStream_onloadClosure() {
  1014. objs.resolve(id, img);
  1015. };
  1016. img.onerror = function loadJpegStream_onerrorClosure() {
  1017. objs.resolve(id, null);
  1018. warn('Error during JPEG image loading');
  1019. };
  1020. img.src = imageUrl;
  1021. }
  1022. exports.FONT_IDENTITY_MATRIX = FONT_IDENTITY_MATRIX;
  1023. exports.IDENTITY_MATRIX = IDENTITY_MATRIX;
  1024. exports.OPS = OPS;
  1025. exports.VERBOSITY_LEVELS = VERBOSITY_LEVELS;
  1026. exports.UNSUPPORTED_FEATURES = UNSUPPORTED_FEATURES;
  1027. exports.AnnotationBorderStyleType = AnnotationBorderStyleType;
  1028. exports.AnnotationFieldFlag = AnnotationFieldFlag;
  1029. exports.AnnotationFlag = AnnotationFlag;
  1030. exports.AnnotationType = AnnotationType;
  1031. exports.FontType = FontType;
  1032. exports.ImageKind = ImageKind;
  1033. exports.CMapCompressionType = CMapCompressionType;
  1034. exports.InvalidPDFException = InvalidPDFException;
  1035. exports.MessageHandler = MessageHandler;
  1036. exports.MissingDataException = MissingDataException;
  1037. exports.MissingPDFException = MissingPDFException;
  1038. exports.NotImplementedException = NotImplementedException;
  1039. exports.PageViewport = PageViewport;
  1040. exports.PasswordException = PasswordException;
  1041. exports.PasswordResponses = PasswordResponses;
  1042. exports.StatTimer = StatTimer;
  1043. exports.StreamType = StreamType;
  1044. exports.TextRenderingMode = TextRenderingMode;
  1045. exports.UnexpectedResponseException = UnexpectedResponseException;
  1046. exports.UnknownErrorException = UnknownErrorException;
  1047. exports.Util = Util;
  1048. exports.XRefParseException = XRefParseException;
  1049. exports.arrayByteLength = arrayByteLength;
  1050. exports.arraysToBytes = arraysToBytes;
  1051. exports.assert = assert;
  1052. exports.bytesToString = bytesToString;
  1053. exports.createBlob = createBlob;
  1054. exports.createPromiseCapability = createPromiseCapability;
  1055. exports.createObjectURL = createObjectURL;
  1056. exports.deprecated = deprecated;
  1057. exports.error = error;
  1058. exports.getLookupTableFactory = getLookupTableFactory;
  1059. exports.getVerbosityLevel = getVerbosityLevel;
  1060. exports.globalScope = globalScope;
  1061. exports.info = info;
  1062. exports.isArray = isArray;
  1063. exports.isArrayBuffer = isArrayBuffer;
  1064. exports.isBool = isBool;
  1065. exports.isEmptyObj = isEmptyObj;
  1066. exports.isInt = isInt;
  1067. exports.isNum = isNum;
  1068. exports.isString = isString;
  1069. exports.isSpace = isSpace;
  1070. exports.isNodeJS = isNodeJS;
  1071. exports.isSameOrigin = isSameOrigin;
  1072. exports.createValidAbsoluteUrl = createValidAbsoluteUrl;
  1073. exports.isLittleEndian = isLittleEndian;
  1074. exports.isEvalSupported = isEvalSupported;
  1075. exports.loadJpegStream = loadJpegStream;
  1076. exports.log2 = log2;
  1077. exports.readInt8 = readInt8;
  1078. exports.readUint16 = readUint16;
  1079. exports.readUint32 = readUint32;
  1080. exports.removeNullCharacters = removeNullCharacters;
  1081. exports.setVerbosityLevel = setVerbosityLevel;
  1082. exports.shadow = shadow;
  1083. exports.string32 = string32;
  1084. exports.stringToBytes = stringToBytes;
  1085. exports.stringToPDFString = stringToPDFString;
  1086. exports.stringToUTF8String = stringToUTF8String;
  1087. exports.utf8StringToString = utf8StringToString;
  1088. exports.warn = warn;