2
0

util.js 33 KB

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