2
0

util.js 32 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088
  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(4);
  511. buffer8[0] = 1;
  512. var view32 = new Uint32Array(buffer8.buffer, 0, 1);
  513. return view32[0] === 1;
  514. }
  515. function isEvalSupported() {
  516. try {
  517. new Function('');
  518. return true;
  519. } catch (e) {
  520. return false;
  521. }
  522. }
  523. var IDENTITY_MATRIX = [1, 0, 0, 1, 0, 0];
  524. var Util = function UtilClosure() {
  525. function Util() {}
  526. var rgbBuf = ['rgb(', 0, ',', 0, ',', 0, ')'];
  527. Util.makeCssRgb = function Util_makeCssRgb(r, g, b) {
  528. rgbBuf[1] = r;
  529. rgbBuf[3] = g;
  530. rgbBuf[5] = b;
  531. return rgbBuf.join('');
  532. };
  533. Util.transform = function Util_transform(m1, m2) {
  534. 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]];
  535. };
  536. Util.applyTransform = function Util_applyTransform(p, m) {
  537. var xt = p[0] * m[0] + p[1] * m[2] + m[4];
  538. var yt = p[0] * m[1] + p[1] * m[3] + m[5];
  539. return [xt, yt];
  540. };
  541. Util.applyInverseTransform = function Util_applyInverseTransform(p, m) {
  542. var d = m[0] * m[3] - m[1] * m[2];
  543. var xt = (p[0] * m[3] - p[1] * m[2] + m[2] * m[5] - m[4] * m[3]) / d;
  544. var yt = (-p[0] * m[1] + p[1] * m[0] + m[4] * m[1] - m[5] * m[0]) / d;
  545. return [xt, yt];
  546. };
  547. Util.getAxialAlignedBoundingBox = function Util_getAxialAlignedBoundingBox(r, m) {
  548. var p1 = Util.applyTransform(r, m);
  549. var p2 = Util.applyTransform(r.slice(2, 4), m);
  550. var p3 = Util.applyTransform([r[0], r[3]], m);
  551. var p4 = Util.applyTransform([r[2], r[1]], m);
  552. 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])];
  553. };
  554. Util.inverseTransform = function Util_inverseTransform(m) {
  555. var d = m[0] * m[3] - m[1] * m[2];
  556. 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];
  557. };
  558. Util.apply3dTransform = function Util_apply3dTransform(m, v) {
  559. 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]];
  560. };
  561. Util.singularValueDecompose2dScale = function Util_singularValueDecompose2dScale(m) {
  562. var transpose = [m[0], m[2], m[1], m[3]];
  563. var a = m[0] * transpose[0] + m[1] * transpose[2];
  564. var b = m[0] * transpose[1] + m[1] * transpose[3];
  565. var c = m[2] * transpose[0] + m[3] * transpose[2];
  566. var d = m[2] * transpose[1] + m[3] * transpose[3];
  567. var first = (a + d) / 2;
  568. var second = Math.sqrt((a + d) * (a + d) - 4 * (a * d - c * b)) / 2;
  569. var sx = first + second || 1;
  570. var sy = first - second || 1;
  571. return [Math.sqrt(sx), Math.sqrt(sy)];
  572. };
  573. Util.normalizeRect = function Util_normalizeRect(rect) {
  574. var r = rect.slice(0);
  575. if (rect[0] > rect[2]) {
  576. r[0] = rect[2];
  577. r[2] = rect[0];
  578. }
  579. if (rect[1] > rect[3]) {
  580. r[1] = rect[3];
  581. r[3] = rect[1];
  582. }
  583. return r;
  584. };
  585. Util.intersect = function Util_intersect(rect1, rect2) {
  586. function compare(a, b) {
  587. return a - b;
  588. }
  589. var orderedX = [rect1[0], rect1[2], rect2[0], rect2[2]].sort(compare),
  590. orderedY = [rect1[1], rect1[3], rect2[1], rect2[3]].sort(compare),
  591. result = [];
  592. rect1 = Util.normalizeRect(rect1);
  593. rect2 = Util.normalizeRect(rect2);
  594. if (orderedX[0] === rect1[0] && orderedX[1] === rect2[0] || orderedX[0] === rect2[0] && orderedX[1] === rect1[0]) {
  595. result[0] = orderedX[1];
  596. result[2] = orderedX[2];
  597. } else {
  598. return false;
  599. }
  600. if (orderedY[0] === rect1[1] && orderedY[1] === rect2[1] || orderedY[0] === rect2[1] && orderedY[1] === rect1[1]) {
  601. result[1] = orderedY[1];
  602. result[3] = orderedY[2];
  603. } else {
  604. return false;
  605. }
  606. return result;
  607. };
  608. Util.sign = function Util_sign(num) {
  609. return num < 0 ? -1 : 1;
  610. };
  611. 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'];
  612. Util.toRoman = function Util_toRoman(number, lowerCase) {
  613. assert(isInt(number) && number > 0, 'The number should be a positive integer.');
  614. var pos,
  615. romanBuf = [];
  616. while (number >= 1000) {
  617. number -= 1000;
  618. romanBuf.push('M');
  619. }
  620. pos = number / 100 | 0;
  621. number %= 100;
  622. romanBuf.push(ROMAN_NUMBER_MAP[pos]);
  623. pos = number / 10 | 0;
  624. number %= 10;
  625. romanBuf.push(ROMAN_NUMBER_MAP[10 + pos]);
  626. romanBuf.push(ROMAN_NUMBER_MAP[20 + number]);
  627. var romanStr = romanBuf.join('');
  628. return lowerCase ? romanStr.toLowerCase() : romanStr;
  629. };
  630. Util.appendToArray = function Util_appendToArray(arr1, arr2) {
  631. Array.prototype.push.apply(arr1, arr2);
  632. };
  633. Util.prependToArray = function Util_prependToArray(arr1, arr2) {
  634. Array.prototype.unshift.apply(arr1, arr2);
  635. };
  636. Util.extendObj = function extendObj(obj1, obj2) {
  637. for (var key in obj2) {
  638. obj1[key] = obj2[key];
  639. }
  640. };
  641. Util.getInheritableProperty = function Util_getInheritableProperty(dict, name, getArray) {
  642. while (dict && !dict.has(name)) {
  643. dict = dict.get('Parent');
  644. }
  645. if (!dict) {
  646. return null;
  647. }
  648. return getArray ? dict.getArray(name) : dict.get(name);
  649. };
  650. Util.inherit = function Util_inherit(sub, base, prototype) {
  651. sub.prototype = Object.create(base.prototype);
  652. sub.prototype.constructor = sub;
  653. for (var prop in prototype) {
  654. sub.prototype[prop] = prototype[prop];
  655. }
  656. };
  657. Util.loadScript = function Util_loadScript(src, callback) {
  658. var script = document.createElement('script');
  659. var loaded = false;
  660. script.setAttribute('src', src);
  661. if (callback) {
  662. script.onload = function () {
  663. if (!loaded) {
  664. callback();
  665. }
  666. loaded = true;
  667. };
  668. }
  669. document.getElementsByTagName('head')[0].appendChild(script);
  670. };
  671. return Util;
  672. }();
  673. var PageViewport = function PageViewportClosure() {
  674. function PageViewport(viewBox, scale, rotation, offsetX, offsetY, dontFlip) {
  675. this.viewBox = viewBox;
  676. this.scale = scale;
  677. this.rotation = rotation;
  678. this.offsetX = offsetX;
  679. this.offsetY = offsetY;
  680. var centerX = (viewBox[2] + viewBox[0]) / 2;
  681. var centerY = (viewBox[3] + viewBox[1]) / 2;
  682. var rotateA, rotateB, rotateC, rotateD;
  683. rotation = rotation % 360;
  684. rotation = rotation < 0 ? rotation + 360 : rotation;
  685. switch (rotation) {
  686. case 180:
  687. rotateA = -1;
  688. rotateB = 0;
  689. rotateC = 0;
  690. rotateD = 1;
  691. break;
  692. case 90:
  693. rotateA = 0;
  694. rotateB = 1;
  695. rotateC = 1;
  696. rotateD = 0;
  697. break;
  698. case 270:
  699. rotateA = 0;
  700. rotateB = -1;
  701. rotateC = -1;
  702. rotateD = 0;
  703. break;
  704. default:
  705. rotateA = 1;
  706. rotateB = 0;
  707. rotateC = 0;
  708. rotateD = -1;
  709. break;
  710. }
  711. if (dontFlip) {
  712. rotateC = -rotateC;
  713. rotateD = -rotateD;
  714. }
  715. var offsetCanvasX, offsetCanvasY;
  716. var width, height;
  717. if (rotateA === 0) {
  718. offsetCanvasX = Math.abs(centerY - viewBox[1]) * scale + offsetX;
  719. offsetCanvasY = Math.abs(centerX - viewBox[0]) * scale + offsetY;
  720. width = Math.abs(viewBox[3] - viewBox[1]) * scale;
  721. height = Math.abs(viewBox[2] - viewBox[0]) * scale;
  722. } else {
  723. offsetCanvasX = Math.abs(centerX - viewBox[0]) * scale + offsetX;
  724. offsetCanvasY = Math.abs(centerY - viewBox[1]) * scale + offsetY;
  725. width = Math.abs(viewBox[2] - viewBox[0]) * scale;
  726. height = Math.abs(viewBox[3] - viewBox[1]) * scale;
  727. }
  728. this.transform = [rotateA * scale, rotateB * scale, rotateC * scale, rotateD * scale, offsetCanvasX - rotateA * scale * centerX - rotateC * scale * centerY, offsetCanvasY - rotateB * scale * centerX - rotateD * scale * centerY];
  729. this.width = width;
  730. this.height = height;
  731. this.fontScale = scale;
  732. }
  733. PageViewport.prototype = {
  734. clone: function PageViewPort_clone(args) {
  735. args = args || {};
  736. var scale = 'scale' in args ? args.scale : this.scale;
  737. var rotation = 'rotation' in args ? args.rotation : this.rotation;
  738. return new PageViewport(this.viewBox.slice(), scale, rotation, this.offsetX, this.offsetY, args.dontFlip);
  739. },
  740. convertToViewportPoint: function PageViewport_convertToViewportPoint(x, y) {
  741. return Util.applyTransform([x, y], this.transform);
  742. },
  743. convertToViewportRectangle: function PageViewport_convertToViewportRectangle(rect) {
  744. var tl = Util.applyTransform([rect[0], rect[1]], this.transform);
  745. var br = Util.applyTransform([rect[2], rect[3]], this.transform);
  746. return [tl[0], tl[1], br[0], br[1]];
  747. },
  748. convertToPdfPoint: function PageViewport_convertToPdfPoint(x, y) {
  749. return Util.applyInverseTransform([x, y], this.transform);
  750. }
  751. };
  752. return PageViewport;
  753. }();
  754. 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];
  755. function stringToPDFString(str) {
  756. var i,
  757. n = str.length,
  758. strBuf = [];
  759. if (str[0] === '\xFE' && str[1] === '\xFF') {
  760. for (i = 2; i < n; i += 2) {
  761. strBuf.push(String.fromCharCode(str.charCodeAt(i) << 8 | str.charCodeAt(i + 1)));
  762. }
  763. } else {
  764. for (i = 0; i < n; ++i) {
  765. var code = PDFStringTranslateTable[str.charCodeAt(i)];
  766. strBuf.push(code ? String.fromCharCode(code) : str.charAt(i));
  767. }
  768. }
  769. return strBuf.join('');
  770. }
  771. function stringToUTF8String(str) {
  772. return decodeURIComponent(escape(str));
  773. }
  774. function utf8StringToString(str) {
  775. return unescape(encodeURIComponent(str));
  776. }
  777. function isEmptyObj(obj) {
  778. for (var key in obj) {
  779. return false;
  780. }
  781. return true;
  782. }
  783. function isBool(v) {
  784. return typeof v === 'boolean';
  785. }
  786. function isInt(v) {
  787. return typeof v === 'number' && (v | 0) === v;
  788. }
  789. function isNum(v) {
  790. return typeof v === 'number';
  791. }
  792. function isString(v) {
  793. return typeof v === 'string';
  794. }
  795. function isArray(v) {
  796. return v instanceof Array;
  797. }
  798. function isArrayBuffer(v) {
  799. return typeof v === 'object' && v !== null && v.byteLength !== undefined;
  800. }
  801. function isSpace(ch) {
  802. return ch === 0x20 || ch === 0x09 || ch === 0x0D || ch === 0x0A;
  803. }
  804. function isNodeJS() {
  805. if (typeof __pdfjsdev_webpack__ === 'undefined') {
  806. return typeof process === 'object' && process + '' === '[object process]';
  807. }
  808. return false;
  809. }
  810. function createPromiseCapability() {
  811. var capability = {};
  812. capability.promise = new Promise(function (resolve, reject) {
  813. capability.resolve = resolve;
  814. capability.reject = reject;
  815. });
  816. return capability;
  817. }
  818. var StatTimer = function StatTimerClosure() {
  819. function rpad(str, pad, length) {
  820. while (str.length < length) {
  821. str += pad;
  822. }
  823. return str;
  824. }
  825. function StatTimer() {
  826. this.started = Object.create(null);
  827. this.times = [];
  828. this.enabled = true;
  829. }
  830. StatTimer.prototype = {
  831. time: function StatTimer_time(name) {
  832. if (!this.enabled) {
  833. return;
  834. }
  835. if (name in this.started) {
  836. warn('Timer is already running for ' + name);
  837. }
  838. this.started[name] = Date.now();
  839. },
  840. timeEnd: function StatTimer_timeEnd(name) {
  841. if (!this.enabled) {
  842. return;
  843. }
  844. if (!(name in this.started)) {
  845. warn('Timer has not been started for ' + name);
  846. }
  847. this.times.push({
  848. 'name': name,
  849. 'start': this.started[name],
  850. 'end': Date.now()
  851. });
  852. delete this.started[name];
  853. },
  854. toString: function StatTimer_toString() {
  855. var i, ii;
  856. var times = this.times;
  857. var out = '';
  858. var longest = 0;
  859. for (i = 0, ii = times.length; i < ii; ++i) {
  860. var name = times[i]['name'];
  861. if (name.length > longest) {
  862. longest = name.length;
  863. }
  864. }
  865. for (i = 0, ii = times.length; i < ii; ++i) {
  866. var span = times[i];
  867. var duration = span.end - span.start;
  868. out += rpad(span['name'], ' ', longest) + ' ' + duration + 'ms\n';
  869. }
  870. return out;
  871. }
  872. };
  873. return StatTimer;
  874. }();
  875. var createBlob = function createBlob(data, contentType) {
  876. if (typeof Blob !== 'undefined') {
  877. return new Blob([data], { type: contentType });
  878. }
  879. warn('The "Blob" constructor is not supported.');
  880. };
  881. var createObjectURL = function createObjectURLClosure() {
  882. var digits = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/=';
  883. return function createObjectURL(data, contentType, forceDataSchema) {
  884. if (!forceDataSchema && typeof URL !== 'undefined' && URL.createObjectURL) {
  885. var blob = createBlob(data, contentType);
  886. return URL.createObjectURL(blob);
  887. }
  888. var buffer = 'data:' + contentType + ';base64,';
  889. for (var i = 0, ii = data.length; i < ii; i += 3) {
  890. var b1 = data[i] & 0xFF;
  891. var b2 = data[i + 1] & 0xFF;
  892. var b3 = data[i + 2] & 0xFF;
  893. var d1 = b1 >> 2,
  894. d2 = (b1 & 3) << 4 | b2 >> 4;
  895. var d3 = i + 1 < ii ? (b2 & 0xF) << 2 | b3 >> 6 : 64;
  896. var d4 = i + 2 < ii ? b3 & 0x3F : 64;
  897. buffer += digits[d1] + digits[d2] + digits[d3] + digits[d4];
  898. }
  899. return buffer;
  900. };
  901. }();
  902. function MessageHandler(sourceName, targetName, comObj) {
  903. this.sourceName = sourceName;
  904. this.targetName = targetName;
  905. this.comObj = comObj;
  906. this.callbackIndex = 1;
  907. this.postMessageTransfers = true;
  908. var callbacksCapabilities = this.callbacksCapabilities = Object.create(null);
  909. var ah = this.actionHandler = Object.create(null);
  910. this._onComObjOnMessage = function messageHandlerComObjOnMessage(event) {
  911. var data = event.data;
  912. if (data.targetName !== this.sourceName) {
  913. return;
  914. }
  915. if (data.isReply) {
  916. var callbackId = data.callbackId;
  917. if (data.callbackId in callbacksCapabilities) {
  918. var callback = callbacksCapabilities[callbackId];
  919. delete callbacksCapabilities[callbackId];
  920. if ('error' in data) {
  921. callback.reject(data.error);
  922. } else {
  923. callback.resolve(data.data);
  924. }
  925. } else {
  926. error('Cannot resolve callback ' + callbackId);
  927. }
  928. } else if (data.action in ah) {
  929. var action = ah[data.action];
  930. if (data.callbackId) {
  931. var sourceName = this.sourceName;
  932. var targetName = data.sourceName;
  933. Promise.resolve().then(function () {
  934. return action[0].call(action[1], data.data);
  935. }).then(function (result) {
  936. comObj.postMessage({
  937. sourceName: sourceName,
  938. targetName: targetName,
  939. isReply: true,
  940. callbackId: data.callbackId,
  941. data: result
  942. });
  943. }, function (reason) {
  944. if (reason instanceof Error) {
  945. reason = reason + '';
  946. }
  947. comObj.postMessage({
  948. sourceName: sourceName,
  949. targetName: targetName,
  950. isReply: true,
  951. callbackId: data.callbackId,
  952. error: reason
  953. });
  954. });
  955. } else {
  956. action[0].call(action[1], data.data);
  957. }
  958. } else {
  959. error('Unknown action from worker: ' + data.action);
  960. }
  961. }.bind(this);
  962. comObj.addEventListener('message', this._onComObjOnMessage);
  963. }
  964. MessageHandler.prototype = {
  965. on: function messageHandlerOn(actionName, handler, scope) {
  966. var ah = this.actionHandler;
  967. if (ah[actionName]) {
  968. error('There is already an actionName called "' + actionName + '"');
  969. }
  970. ah[actionName] = [handler, scope];
  971. },
  972. send: function messageHandlerSend(actionName, data, transfers) {
  973. var message = {
  974. sourceName: this.sourceName,
  975. targetName: this.targetName,
  976. action: actionName,
  977. data: data
  978. };
  979. this.postMessage(message, transfers);
  980. },
  981. sendWithPromise: function messageHandlerSendWithPromise(actionName, data, transfers) {
  982. var callbackId = this.callbackIndex++;
  983. var message = {
  984. sourceName: this.sourceName,
  985. targetName: this.targetName,
  986. action: actionName,
  987. data: data,
  988. callbackId: callbackId
  989. };
  990. var capability = createPromiseCapability();
  991. this.callbacksCapabilities[callbackId] = capability;
  992. try {
  993. this.postMessage(message, transfers);
  994. } catch (e) {
  995. capability.reject(e);
  996. }
  997. return capability.promise;
  998. },
  999. postMessage: function (message, transfers) {
  1000. if (transfers && this.postMessageTransfers) {
  1001. this.comObj.postMessage(message, transfers);
  1002. } else {
  1003. this.comObj.postMessage(message);
  1004. }
  1005. },
  1006. destroy: function () {
  1007. this.comObj.removeEventListener('message', this._onComObjOnMessage);
  1008. }
  1009. };
  1010. function loadJpegStream(id, imageUrl, objs) {
  1011. var img = new Image();
  1012. img.onload = function loadJpegStream_onloadClosure() {
  1013. objs.resolve(id, img);
  1014. };
  1015. img.onerror = function loadJpegStream_onerrorClosure() {
  1016. objs.resolve(id, null);
  1017. warn('Error during JPEG image loading');
  1018. };
  1019. img.src = imageUrl;
  1020. }
  1021. exports.FONT_IDENTITY_MATRIX = FONT_IDENTITY_MATRIX;
  1022. exports.IDENTITY_MATRIX = IDENTITY_MATRIX;
  1023. exports.OPS = OPS;
  1024. exports.VERBOSITY_LEVELS = VERBOSITY_LEVELS;
  1025. exports.UNSUPPORTED_FEATURES = UNSUPPORTED_FEATURES;
  1026. exports.AnnotationBorderStyleType = AnnotationBorderStyleType;
  1027. exports.AnnotationFieldFlag = AnnotationFieldFlag;
  1028. exports.AnnotationFlag = AnnotationFlag;
  1029. exports.AnnotationType = AnnotationType;
  1030. exports.FontType = FontType;
  1031. exports.ImageKind = ImageKind;
  1032. exports.CMapCompressionType = CMapCompressionType;
  1033. exports.InvalidPDFException = InvalidPDFException;
  1034. exports.MessageHandler = MessageHandler;
  1035. exports.MissingDataException = MissingDataException;
  1036. exports.MissingPDFException = MissingPDFException;
  1037. exports.NotImplementedException = NotImplementedException;
  1038. exports.PageViewport = PageViewport;
  1039. exports.PasswordException = PasswordException;
  1040. exports.PasswordResponses = PasswordResponses;
  1041. exports.StatTimer = StatTimer;
  1042. exports.StreamType = StreamType;
  1043. exports.TextRenderingMode = TextRenderingMode;
  1044. exports.UnexpectedResponseException = UnexpectedResponseException;
  1045. exports.UnknownErrorException = UnknownErrorException;
  1046. exports.Util = Util;
  1047. exports.XRefParseException = XRefParseException;
  1048. exports.arrayByteLength = arrayByteLength;
  1049. exports.arraysToBytes = arraysToBytes;
  1050. exports.assert = assert;
  1051. exports.bytesToString = bytesToString;
  1052. exports.createBlob = createBlob;
  1053. exports.createPromiseCapability = createPromiseCapability;
  1054. exports.createObjectURL = createObjectURL;
  1055. exports.deprecated = deprecated;
  1056. exports.error = error;
  1057. exports.getLookupTableFactory = getLookupTableFactory;
  1058. exports.getVerbosityLevel = getVerbosityLevel;
  1059. exports.globalScope = globalScope;
  1060. exports.info = info;
  1061. exports.isArray = isArray;
  1062. exports.isArrayBuffer = isArrayBuffer;
  1063. exports.isBool = isBool;
  1064. exports.isEmptyObj = isEmptyObj;
  1065. exports.isInt = isInt;
  1066. exports.isNum = isNum;
  1067. exports.isString = isString;
  1068. exports.isSpace = isSpace;
  1069. exports.isNodeJS = isNodeJS;
  1070. exports.isSameOrigin = isSameOrigin;
  1071. exports.createValidAbsoluteUrl = createValidAbsoluteUrl;
  1072. exports.isLittleEndian = isLittleEndian;
  1073. exports.isEvalSupported = isEvalSupported;
  1074. exports.loadJpegStream = loadJpegStream;
  1075. exports.log2 = log2;
  1076. exports.readInt8 = readInt8;
  1077. exports.readUint16 = readUint16;
  1078. exports.readUint32 = readUint32;
  1079. exports.removeNullCharacters = removeNullCharacters;
  1080. exports.setVerbosityLevel = setVerbosityLevel;
  1081. exports.shadow = shadow;
  1082. exports.string32 = string32;
  1083. exports.stringToBytes = stringToBytes;
  1084. exports.stringToPDFString = stringToPDFString;
  1085. exports.stringToUTF8String = stringToUTF8String;
  1086. exports.utf8StringToString = utf8StringToString;
  1087. exports.warn = warn;