2
0

base_viewer.js 34 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173
  1. /**
  2. * @licstart The following is the entire license notice for the
  3. * Javascript code in this page
  4. *
  5. * Copyright 2019 Mozilla Foundation
  6. *
  7. * Licensed under the Apache License, Version 2.0 (the "License");
  8. * you may not use this file except in compliance with the License.
  9. * You may obtain a copy of the License at
  10. *
  11. * http://www.apache.org/licenses/LICENSE-2.0
  12. *
  13. * Unless required by applicable law or agreed to in writing, software
  14. * distributed under the License is distributed on an "AS IS" BASIS,
  15. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  16. * See the License for the specific language governing permissions and
  17. * limitations under the License.
  18. *
  19. * @licend The above is the entire license notice for the
  20. * Javascript code in this page
  21. */
  22. "use strict";
  23. Object.defineProperty(exports, "__esModule", {
  24. value: true
  25. });
  26. exports.BaseViewer = void 0;
  27. var _ui_utils = require("./ui_utils");
  28. var _pdf_rendering_queue = require("./pdf_rendering_queue");
  29. var _annotation_layer_builder = require("./annotation_layer_builder");
  30. var _pdf = require("../pdf");
  31. var _pdf_page_view = require("./pdf_page_view");
  32. var _pdf_link_service = require("./pdf_link_service");
  33. var _text_layer_builder = require("./text_layer_builder");
  34. function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } }
  35. function _defineProperties(target, props) { for (var i = 0; i < props.length; i++) { var descriptor = props[i]; descriptor.enumerable = descriptor.enumerable || false; descriptor.configurable = true; if ("value" in descriptor) descriptor.writable = true; Object.defineProperty(target, descriptor.key, descriptor); } }
  36. function _createClass(Constructor, protoProps, staticProps) { if (protoProps) _defineProperties(Constructor.prototype, protoProps); if (staticProps) _defineProperties(Constructor, staticProps); return Constructor; }
  37. var DEFAULT_CACHE_SIZE = 10;
  38. function PDFPageViewBuffer(size) {
  39. var data = [];
  40. this.push = function (view) {
  41. var i = data.indexOf(view);
  42. if (i >= 0) {
  43. data.splice(i, 1);
  44. }
  45. data.push(view);
  46. if (data.length > size) {
  47. data.shift().destroy();
  48. }
  49. };
  50. this.resize = function (newSize, pagesToKeep) {
  51. size = newSize;
  52. if (pagesToKeep) {
  53. var pageIdsToKeep = new Set();
  54. for (var i = 0, iMax = pagesToKeep.length; i < iMax; ++i) {
  55. pageIdsToKeep.add(pagesToKeep[i].id);
  56. }
  57. (0, _ui_utils.moveToEndOfArray)(data, function (page) {
  58. return pageIdsToKeep.has(page.id);
  59. });
  60. }
  61. while (data.length > size) {
  62. data.shift().destroy();
  63. }
  64. };
  65. }
  66. function isSameScale(oldScale, newScale) {
  67. if (newScale === oldScale) {
  68. return true;
  69. }
  70. if (Math.abs(newScale - oldScale) < 1e-15) {
  71. return true;
  72. }
  73. return false;
  74. }
  75. var BaseViewer =
  76. /*#__PURE__*/
  77. function () {
  78. function BaseViewer(options) {
  79. var _this = this;
  80. _classCallCheck(this, BaseViewer);
  81. if (this.constructor === BaseViewer) {
  82. throw new Error('Cannot initialize BaseViewer.');
  83. }
  84. this._name = this.constructor.name;
  85. this.container = options.container;
  86. this.viewer = options.viewer || options.container.firstElementChild;
  87. this.eventBus = options.eventBus || (0, _ui_utils.getGlobalEventBus)();
  88. this.linkService = options.linkService || new _pdf_link_service.SimpleLinkService();
  89. this.downloadManager = options.downloadManager || null;
  90. this.findController = options.findController || null;
  91. this.removePageBorders = options.removePageBorders || false;
  92. this.textLayerMode = Number.isInteger(options.textLayerMode) ? options.textLayerMode : _ui_utils.TextLayerMode.ENABLE;
  93. this.imageResourcesPath = options.imageResourcesPath || '';
  94. this.renderInteractiveForms = options.renderInteractiveForms || false;
  95. this.enablePrintAutoRotate = options.enablePrintAutoRotate || false;
  96. this.renderer = options.renderer || _ui_utils.RendererType.CANVAS;
  97. this.enableWebGL = options.enableWebGL || false;
  98. this.useOnlyCssZoom = options.useOnlyCssZoom || false;
  99. this.maxCanvasPixels = options.maxCanvasPixels;
  100. this.l10n = options.l10n || _ui_utils.NullL10n;
  101. this.defaultRenderingQueue = !options.renderingQueue;
  102. if (this.defaultRenderingQueue) {
  103. this.renderingQueue = new _pdf_rendering_queue.PDFRenderingQueue();
  104. this.renderingQueue.setViewer(this);
  105. } else {
  106. this.renderingQueue = options.renderingQueue;
  107. }
  108. this.scroll = (0, _ui_utils.watchScroll)(this.container, this._scrollUpdate.bind(this));
  109. this.presentationModeState = _ui_utils.PresentationModeState.UNKNOWN;
  110. this._onBeforeDraw = this._onAfterDraw = null;
  111. this._resetView();
  112. if (this.removePageBorders) {
  113. this.viewer.classList.add('removePageBorders');
  114. }
  115. Promise.resolve().then(function () {
  116. _this.eventBus.dispatch('baseviewerinit', {
  117. source: _this
  118. });
  119. });
  120. }
  121. _createClass(BaseViewer, [{
  122. key: "getPageView",
  123. value: function getPageView(index) {
  124. return this._pages[index];
  125. }
  126. }, {
  127. key: "_setCurrentPageNumber",
  128. value: function _setCurrentPageNumber(val) {
  129. var resetCurrentPageView = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : false;
  130. if (this._currentPageNumber === val) {
  131. if (resetCurrentPageView) {
  132. this._resetCurrentPageView();
  133. }
  134. return true;
  135. }
  136. if (!(0 < val && val <= this.pagesCount)) {
  137. return false;
  138. }
  139. this._currentPageNumber = val;
  140. this.eventBus.dispatch('pagechanging', {
  141. source: this,
  142. pageNumber: val,
  143. pageLabel: this._pageLabels && this._pageLabels[val - 1]
  144. });
  145. if (resetCurrentPageView) {
  146. this._resetCurrentPageView();
  147. }
  148. return true;
  149. }
  150. }, {
  151. key: "setDocument",
  152. value: function setDocument(pdfDocument) {
  153. var _this2 = this;
  154. if (this.pdfDocument) {
  155. this._cancelRendering();
  156. this._resetView();
  157. if (this.findController) {
  158. this.findController.setDocument(null);
  159. }
  160. }
  161. this.pdfDocument = pdfDocument;
  162. if (!pdfDocument) {
  163. return;
  164. }
  165. var pagesCount = pdfDocument.numPages;
  166. var pagesCapability = (0, _pdf.createPromiseCapability)();
  167. this.pagesPromise = pagesCapability.promise;
  168. pagesCapability.promise.then(function () {
  169. _this2._pageViewsReady = true;
  170. _this2.eventBus.dispatch('pagesloaded', {
  171. source: _this2,
  172. pagesCount: pagesCount
  173. });
  174. });
  175. var onePageRenderedCapability = (0, _pdf.createPromiseCapability)();
  176. this.onePageRendered = onePageRenderedCapability.promise;
  177. var firstPagePromise = pdfDocument.getPage(1);
  178. this.firstPagePromise = firstPagePromise;
  179. this._onBeforeDraw = function (evt) {
  180. var pageView = _this2._pages[evt.pageNumber - 1];
  181. if (!pageView) {
  182. return;
  183. }
  184. _this2._buffer.push(pageView);
  185. };
  186. this.eventBus.on('pagerender', this._onBeforeDraw);
  187. this._onAfterDraw = function (evt) {
  188. if (evt.cssTransform || onePageRenderedCapability.settled) {
  189. return;
  190. }
  191. onePageRenderedCapability.resolve();
  192. _this2.eventBus.off('pagerendered', _this2._onAfterDraw);
  193. _this2._onAfterDraw = null;
  194. };
  195. this.eventBus.on('pagerendered', this._onAfterDraw);
  196. firstPagePromise.then(function (pdfPage) {
  197. var scale = _this2.currentScale;
  198. var viewport = pdfPage.getViewport({
  199. scale: scale * _ui_utils.CSS_UNITS
  200. });
  201. for (var pageNum = 1; pageNum <= pagesCount; ++pageNum) {
  202. var textLayerFactory = null;
  203. if (_this2.textLayerMode !== _ui_utils.TextLayerMode.DISABLE) {
  204. textLayerFactory = _this2;
  205. }
  206. var pageView = new _pdf_page_view.PDFPageView({
  207. container: _this2._setDocumentViewerElement,
  208. eventBus: _this2.eventBus,
  209. id: pageNum,
  210. scale: scale,
  211. defaultViewport: viewport.clone(),
  212. renderingQueue: _this2.renderingQueue,
  213. textLayerFactory: textLayerFactory,
  214. textLayerMode: _this2.textLayerMode,
  215. annotationLayerFactory: _this2,
  216. imageResourcesPath: _this2.imageResourcesPath,
  217. renderInteractiveForms: _this2.renderInteractiveForms,
  218. renderer: _this2.renderer,
  219. enableWebGL: _this2.enableWebGL,
  220. useOnlyCssZoom: _this2.useOnlyCssZoom,
  221. maxCanvasPixels: _this2.maxCanvasPixels,
  222. l10n: _this2.l10n
  223. });
  224. _this2._pages.push(pageView);
  225. }
  226. if (_this2._spreadMode !== _ui_utils.SpreadMode.NONE) {
  227. _this2._updateSpreadMode();
  228. }
  229. onePageRenderedCapability.promise.then(function () {
  230. if (_this2.findController) {
  231. _this2.findController.setDocument(pdfDocument);
  232. }
  233. if (pdfDocument.loadingParams['disableAutoFetch']) {
  234. pagesCapability.resolve();
  235. return;
  236. }
  237. var getPagesLeft = pagesCount;
  238. var _loop = function _loop(_pageNum) {
  239. pdfDocument.getPage(_pageNum).then(function (pdfPage) {
  240. var pageView = _this2._pages[_pageNum - 1];
  241. if (!pageView.pdfPage) {
  242. pageView.setPdfPage(pdfPage);
  243. }
  244. _this2.linkService.cachePageRef(_pageNum, pdfPage.ref);
  245. if (--getPagesLeft === 0) {
  246. pagesCapability.resolve();
  247. }
  248. }, function (reason) {
  249. console.error("Unable to get page ".concat(_pageNum, " to initialize viewer"), reason);
  250. if (--getPagesLeft === 0) {
  251. pagesCapability.resolve();
  252. }
  253. });
  254. };
  255. for (var _pageNum = 1; _pageNum <= pagesCount; ++_pageNum) {
  256. _loop(_pageNum);
  257. }
  258. });
  259. _this2.eventBus.dispatch('pagesinit', {
  260. source: _this2
  261. });
  262. if (_this2.defaultRenderingQueue) {
  263. _this2.update();
  264. }
  265. })["catch"](function (reason) {
  266. console.error('Unable to initialize viewer', reason);
  267. });
  268. }
  269. }, {
  270. key: "setPageLabels",
  271. value: function setPageLabels(labels) {
  272. if (!this.pdfDocument) {
  273. return;
  274. }
  275. if (!labels) {
  276. this._pageLabels = null;
  277. } else if (!(Array.isArray(labels) && this.pdfDocument.numPages === labels.length)) {
  278. this._pageLabels = null;
  279. console.error("".concat(this._name, ".setPageLabels: Invalid page labels."));
  280. } else {
  281. this._pageLabels = labels;
  282. }
  283. for (var i = 0, ii = this._pages.length; i < ii; i++) {
  284. var pageView = this._pages[i];
  285. var label = this._pageLabels && this._pageLabels[i];
  286. pageView.setPageLabel(label);
  287. }
  288. }
  289. }, {
  290. key: "_resetView",
  291. value: function _resetView() {
  292. this._pages = [];
  293. this._currentPageNumber = 1;
  294. this._currentScale = _ui_utils.UNKNOWN_SCALE;
  295. this._currentScaleValue = null;
  296. this._pageLabels = null;
  297. this._buffer = new PDFPageViewBuffer(DEFAULT_CACHE_SIZE);
  298. this._location = null;
  299. this._pagesRotation = 0;
  300. this._pagesRequests = [];
  301. this._pageViewsReady = false;
  302. this._scrollMode = _ui_utils.ScrollMode.VERTICAL;
  303. this._spreadMode = _ui_utils.SpreadMode.NONE;
  304. if (this._onBeforeDraw) {
  305. this.eventBus.off('pagerender', this._onBeforeDraw);
  306. this._onBeforeDraw = null;
  307. }
  308. if (this._onAfterDraw) {
  309. this.eventBus.off('pagerendered', this._onAfterDraw);
  310. this._onAfterDraw = null;
  311. }
  312. this.viewer.textContent = '';
  313. this._updateScrollMode();
  314. }
  315. }, {
  316. key: "_scrollUpdate",
  317. value: function _scrollUpdate() {
  318. if (this.pagesCount === 0) {
  319. return;
  320. }
  321. this.update();
  322. }
  323. }, {
  324. key: "_scrollIntoView",
  325. value: function _scrollIntoView(_ref) {
  326. var pageDiv = _ref.pageDiv,
  327. _ref$pageSpot = _ref.pageSpot,
  328. pageSpot = _ref$pageSpot === void 0 ? null : _ref$pageSpot,
  329. _ref$pageNumber = _ref.pageNumber,
  330. pageNumber = _ref$pageNumber === void 0 ? null : _ref$pageNumber;
  331. (0, _ui_utils.scrollIntoView)(pageDiv, pageSpot);
  332. }
  333. }, {
  334. key: "_setScaleUpdatePages",
  335. value: function _setScaleUpdatePages(newScale, newValue) {
  336. var noScroll = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : false;
  337. var preset = arguments.length > 3 && arguments[3] !== undefined ? arguments[3] : false;
  338. this._currentScaleValue = newValue.toString();
  339. if (isSameScale(this._currentScale, newScale)) {
  340. if (preset) {
  341. this.eventBus.dispatch('scalechanging', {
  342. source: this,
  343. scale: newScale,
  344. presetValue: newValue
  345. });
  346. }
  347. return;
  348. }
  349. for (var i = 0, ii = this._pages.length; i < ii; i++) {
  350. this._pages[i].update(newScale);
  351. }
  352. this._currentScale = newScale;
  353. if (!noScroll) {
  354. var page = this._currentPageNumber,
  355. dest;
  356. if (this._location && !(this.isInPresentationMode || this.isChangingPresentationMode)) {
  357. page = this._location.pageNumber;
  358. dest = [null, {
  359. name: 'XYZ'
  360. }, this._location.left, this._location.top, null];
  361. }
  362. this.scrollPageIntoView({
  363. pageNumber: page,
  364. destArray: dest,
  365. allowNegativeOffset: true
  366. });
  367. }
  368. this.eventBus.dispatch('scalechanging', {
  369. source: this,
  370. scale: newScale,
  371. presetValue: preset ? newValue : undefined
  372. });
  373. if (this.defaultRenderingQueue) {
  374. this.update();
  375. }
  376. }
  377. }, {
  378. key: "_setScale",
  379. value: function _setScale(value) {
  380. var noScroll = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : false;
  381. var scale = parseFloat(value);
  382. if (scale > 0) {
  383. this._setScaleUpdatePages(scale, value, noScroll, false);
  384. } else {
  385. var currentPage = this._pages[this._currentPageNumber - 1];
  386. if (!currentPage) {
  387. return;
  388. }
  389. var noPadding = this.isInPresentationMode || this.removePageBorders;
  390. var hPadding = noPadding ? 0 : _ui_utils.SCROLLBAR_PADDING;
  391. var vPadding = noPadding ? 0 : _ui_utils.VERTICAL_PADDING;
  392. if (!noPadding && this._isScrollModeHorizontal) {
  393. var _ref2 = [vPadding, hPadding];
  394. hPadding = _ref2[0];
  395. vPadding = _ref2[1];
  396. }
  397. var pageWidthScale = (this.container.clientWidth - hPadding) / currentPage.width * currentPage.scale;
  398. var pageHeightScale = (this.container.clientHeight - vPadding) / currentPage.height * currentPage.scale;
  399. switch (value) {
  400. case 'page-actual':
  401. scale = 1;
  402. break;
  403. case 'page-width':
  404. scale = pageWidthScale;
  405. break;
  406. case 'page-height':
  407. scale = pageHeightScale;
  408. break;
  409. case 'page-fit':
  410. scale = Math.min(pageWidthScale, pageHeightScale);
  411. break;
  412. case 'auto':
  413. var horizontalScale = (0, _ui_utils.isPortraitOrientation)(currentPage) ? pageWidthScale : Math.min(pageHeightScale, pageWidthScale);
  414. scale = Math.min(_ui_utils.MAX_AUTO_SCALE, horizontalScale);
  415. break;
  416. default:
  417. console.error("".concat(this._name, "._setScale: \"").concat(value, "\" is an unknown zoom value."));
  418. return;
  419. }
  420. this._setScaleUpdatePages(scale, value, noScroll, true);
  421. }
  422. }
  423. }, {
  424. key: "_resetCurrentPageView",
  425. value: function _resetCurrentPageView() {
  426. if (this.isInPresentationMode) {
  427. this._setScale(this._currentScaleValue, true);
  428. }
  429. var pageView = this._pages[this._currentPageNumber - 1];
  430. this._scrollIntoView({
  431. pageDiv: pageView.div
  432. });
  433. }
  434. }, {
  435. key: "scrollPageIntoView",
  436. value: function scrollPageIntoView(_ref3) {
  437. var pageNumber = _ref3.pageNumber,
  438. _ref3$destArray = _ref3.destArray,
  439. destArray = _ref3$destArray === void 0 ? null : _ref3$destArray,
  440. _ref3$allowNegativeOf = _ref3.allowNegativeOffset,
  441. allowNegativeOffset = _ref3$allowNegativeOf === void 0 ? false : _ref3$allowNegativeOf;
  442. if (!this.pdfDocument) {
  443. return;
  444. }
  445. var pageView = Number.isInteger(pageNumber) && this._pages[pageNumber - 1];
  446. if (!pageView) {
  447. console.error("".concat(this._name, ".scrollPageIntoView: ") + "\"".concat(pageNumber, "\" is not a valid pageNumber parameter."));
  448. return;
  449. }
  450. if (this.isInPresentationMode || !destArray) {
  451. this._setCurrentPageNumber(pageNumber, true);
  452. return;
  453. }
  454. var x = 0,
  455. y = 0;
  456. var width = 0,
  457. height = 0,
  458. widthScale,
  459. heightScale;
  460. var changeOrientation = pageView.rotation % 180 === 0 ? false : true;
  461. var pageWidth = (changeOrientation ? pageView.height : pageView.width) / pageView.scale / _ui_utils.CSS_UNITS;
  462. var pageHeight = (changeOrientation ? pageView.width : pageView.height) / pageView.scale / _ui_utils.CSS_UNITS;
  463. var scale = 0;
  464. switch (destArray[1].name) {
  465. case 'XYZ':
  466. x = destArray[2];
  467. y = destArray[3];
  468. scale = destArray[4];
  469. x = x !== null ? x : 0;
  470. y = y !== null ? y : pageHeight;
  471. break;
  472. case 'Fit':
  473. case 'FitB':
  474. scale = 'page-fit';
  475. break;
  476. case 'FitH':
  477. case 'FitBH':
  478. y = destArray[2];
  479. scale = 'page-width';
  480. if (y === null && this._location) {
  481. x = this._location.left;
  482. y = this._location.top;
  483. }
  484. break;
  485. case 'FitV':
  486. case 'FitBV':
  487. x = destArray[2];
  488. width = pageWidth;
  489. height = pageHeight;
  490. scale = 'page-height';
  491. break;
  492. case 'FitR':
  493. x = destArray[2];
  494. y = destArray[3];
  495. width = destArray[4] - x;
  496. height = destArray[5] - y;
  497. var hPadding = this.removePageBorders ? 0 : _ui_utils.SCROLLBAR_PADDING;
  498. var vPadding = this.removePageBorders ? 0 : _ui_utils.VERTICAL_PADDING;
  499. widthScale = (this.container.clientWidth - hPadding) / width / _ui_utils.CSS_UNITS;
  500. heightScale = (this.container.clientHeight - vPadding) / height / _ui_utils.CSS_UNITS;
  501. scale = Math.min(Math.abs(widthScale), Math.abs(heightScale));
  502. break;
  503. default:
  504. console.error("".concat(this._name, ".scrollPageIntoView: ") + "\"".concat(destArray[1].name, "\" is not a valid destination type."));
  505. return;
  506. }
  507. if (scale && scale !== this._currentScale) {
  508. this.currentScaleValue = scale;
  509. } else if (this._currentScale === _ui_utils.UNKNOWN_SCALE) {
  510. this.currentScaleValue = _ui_utils.DEFAULT_SCALE_VALUE;
  511. }
  512. if (scale === 'page-fit' && !destArray[4]) {
  513. this._scrollIntoView({
  514. pageDiv: pageView.div,
  515. pageNumber: pageNumber
  516. });
  517. return;
  518. }
  519. var boundingRect = [pageView.viewport.convertToViewportPoint(x, y), pageView.viewport.convertToViewportPoint(x + width, y + height)];
  520. var left = Math.min(boundingRect[0][0], boundingRect[1][0]);
  521. var top = Math.min(boundingRect[0][1], boundingRect[1][1]);
  522. if (!allowNegativeOffset) {
  523. left = Math.max(left, 0);
  524. top = Math.max(top, 0);
  525. }
  526. this._scrollIntoView({
  527. pageDiv: pageView.div,
  528. pageSpot: {
  529. left: left,
  530. top: top
  531. },
  532. pageNumber: pageNumber
  533. });
  534. }
  535. }, {
  536. key: "_updateLocation",
  537. value: function _updateLocation(firstPage) {
  538. var currentScale = this._currentScale;
  539. var currentScaleValue = this._currentScaleValue;
  540. var normalizedScaleValue = parseFloat(currentScaleValue) === currentScale ? Math.round(currentScale * 10000) / 100 : currentScaleValue;
  541. var pageNumber = firstPage.id;
  542. var pdfOpenParams = '#page=' + pageNumber;
  543. pdfOpenParams += '&zoom=' + normalizedScaleValue;
  544. var currentPageView = this._pages[pageNumber - 1];
  545. var container = this.container;
  546. var topLeft = currentPageView.getPagePoint(container.scrollLeft - firstPage.x, container.scrollTop - firstPage.y);
  547. var intLeft = Math.round(topLeft[0]);
  548. var intTop = Math.round(topLeft[1]);
  549. pdfOpenParams += ',' + intLeft + ',' + intTop;
  550. this._location = {
  551. pageNumber: pageNumber,
  552. scale: normalizedScaleValue,
  553. top: intTop,
  554. left: intLeft,
  555. rotation: this._pagesRotation,
  556. pdfOpenParams: pdfOpenParams
  557. };
  558. }
  559. }, {
  560. key: "_updateHelper",
  561. value: function _updateHelper(visiblePages) {
  562. throw new Error('Not implemented: _updateHelper');
  563. }
  564. }, {
  565. key: "update",
  566. value: function update() {
  567. var visible = this._getVisiblePages();
  568. var visiblePages = visible.views,
  569. numVisiblePages = visiblePages.length;
  570. if (numVisiblePages === 0) {
  571. return;
  572. }
  573. var newCacheSize = Math.max(DEFAULT_CACHE_SIZE, 2 * numVisiblePages + 1);
  574. this._buffer.resize(newCacheSize, visiblePages);
  575. this.renderingQueue.renderHighestPriority(visible);
  576. this._updateHelper(visiblePages);
  577. this._updateLocation(visible.first);
  578. this.eventBus.dispatch('updateviewarea', {
  579. source: this,
  580. location: this._location
  581. });
  582. }
  583. }, {
  584. key: "containsElement",
  585. value: function containsElement(element) {
  586. return this.container.contains(element);
  587. }
  588. }, {
  589. key: "focus",
  590. value: function focus() {
  591. this.container.focus();
  592. }
  593. }, {
  594. key: "_getCurrentVisiblePage",
  595. value: function _getCurrentVisiblePage() {
  596. if (!this.pagesCount) {
  597. return {
  598. views: []
  599. };
  600. }
  601. var pageView = this._pages[this._currentPageNumber - 1];
  602. var element = pageView.div;
  603. var view = {
  604. id: pageView.id,
  605. x: element.offsetLeft + element.clientLeft,
  606. y: element.offsetTop + element.clientTop,
  607. view: pageView
  608. };
  609. return {
  610. first: view,
  611. last: view,
  612. views: [view]
  613. };
  614. }
  615. }, {
  616. key: "_getVisiblePages",
  617. value: function _getVisiblePages() {
  618. return (0, _ui_utils.getVisibleElements)(this.container, this._pages, true, this._isScrollModeHorizontal);
  619. }
  620. }, {
  621. key: "isPageVisible",
  622. value: function isPageVisible(pageNumber) {
  623. if (!this.pdfDocument) {
  624. return false;
  625. }
  626. if (this.pageNumber < 1 || pageNumber > this.pagesCount) {
  627. console.error("".concat(this._name, ".isPageVisible: \"").concat(pageNumber, "\" is out of bounds."));
  628. return false;
  629. }
  630. return this._getVisiblePages().views.some(function (view) {
  631. return view.id === pageNumber;
  632. });
  633. }
  634. }, {
  635. key: "cleanup",
  636. value: function cleanup() {
  637. for (var i = 0, ii = this._pages.length; i < ii; i++) {
  638. if (this._pages[i] && this._pages[i].renderingState !== _pdf_rendering_queue.RenderingStates.FINISHED) {
  639. this._pages[i].reset();
  640. }
  641. }
  642. }
  643. }, {
  644. key: "_cancelRendering",
  645. value: function _cancelRendering() {
  646. for (var i = 0, ii = this._pages.length; i < ii; i++) {
  647. if (this._pages[i]) {
  648. this._pages[i].cancelRendering();
  649. }
  650. }
  651. }
  652. }, {
  653. key: "_ensurePdfPageLoaded",
  654. value: function _ensurePdfPageLoaded(pageView) {
  655. var _this3 = this;
  656. if (pageView.pdfPage) {
  657. return Promise.resolve(pageView.pdfPage);
  658. }
  659. var pageNumber = pageView.id;
  660. if (this._pagesRequests[pageNumber]) {
  661. return this._pagesRequests[pageNumber];
  662. }
  663. var promise = this.pdfDocument.getPage(pageNumber).then(function (pdfPage) {
  664. if (!pageView.pdfPage) {
  665. pageView.setPdfPage(pdfPage);
  666. }
  667. _this3._pagesRequests[pageNumber] = null;
  668. return pdfPage;
  669. })["catch"](function (reason) {
  670. console.error('Unable to get page for page view', reason);
  671. _this3._pagesRequests[pageNumber] = null;
  672. });
  673. this._pagesRequests[pageNumber] = promise;
  674. return promise;
  675. }
  676. }, {
  677. key: "forceRendering",
  678. value: function forceRendering(currentlyVisiblePages) {
  679. var _this4 = this;
  680. var visiblePages = currentlyVisiblePages || this._getVisiblePages();
  681. var scrollAhead = this._isScrollModeHorizontal ? this.scroll.right : this.scroll.down;
  682. var pageView = this.renderingQueue.getHighestPriority(visiblePages, this._pages, scrollAhead);
  683. if (pageView) {
  684. this._ensurePdfPageLoaded(pageView).then(function () {
  685. _this4.renderingQueue.renderView(pageView);
  686. });
  687. return true;
  688. }
  689. return false;
  690. }
  691. }, {
  692. key: "createTextLayerBuilder",
  693. value: function createTextLayerBuilder(textLayerDiv, pageIndex, viewport) {
  694. var enhanceTextSelection = arguments.length > 3 && arguments[3] !== undefined ? arguments[3] : false;
  695. return new _text_layer_builder.TextLayerBuilder({
  696. textLayerDiv: textLayerDiv,
  697. eventBus: this.eventBus,
  698. pageIndex: pageIndex,
  699. viewport: viewport,
  700. findController: this.isInPresentationMode ? null : this.findController,
  701. enhanceTextSelection: this.isInPresentationMode ? false : enhanceTextSelection
  702. });
  703. }
  704. }, {
  705. key: "createAnnotationLayerBuilder",
  706. value: function createAnnotationLayerBuilder(pageDiv, pdfPage) {
  707. var imageResourcesPath = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : '';
  708. var renderInteractiveForms = arguments.length > 3 && arguments[3] !== undefined ? arguments[3] : false;
  709. var l10n = arguments.length > 4 && arguments[4] !== undefined ? arguments[4] : _ui_utils.NullL10n;
  710. return new _annotation_layer_builder.AnnotationLayerBuilder({
  711. pageDiv: pageDiv,
  712. pdfPage: pdfPage,
  713. imageResourcesPath: imageResourcesPath,
  714. renderInteractiveForms: renderInteractiveForms,
  715. linkService: this.linkService,
  716. downloadManager: this.downloadManager,
  717. l10n: l10n
  718. });
  719. }
  720. }, {
  721. key: "getPagesOverview",
  722. value: function getPagesOverview() {
  723. var pagesOverview = this._pages.map(function (pageView) {
  724. var viewport = pageView.pdfPage.getViewport({
  725. scale: 1
  726. });
  727. return {
  728. width: viewport.width,
  729. height: viewport.height,
  730. rotation: viewport.rotation
  731. };
  732. });
  733. if (!this.enablePrintAutoRotate) {
  734. return pagesOverview;
  735. }
  736. var isFirstPagePortrait = (0, _ui_utils.isPortraitOrientation)(pagesOverview[0]);
  737. return pagesOverview.map(function (size) {
  738. if (isFirstPagePortrait === (0, _ui_utils.isPortraitOrientation)(size)) {
  739. return size;
  740. }
  741. return {
  742. width: size.height,
  743. height: size.width,
  744. rotation: (size.rotation + 90) % 360
  745. };
  746. });
  747. }
  748. }, {
  749. key: "_updateScrollMode",
  750. value: function _updateScrollMode() {
  751. var pageNumber = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : null;
  752. var scrollMode = this._scrollMode,
  753. viewer = this.viewer;
  754. viewer.classList.toggle('scrollHorizontal', scrollMode === _ui_utils.ScrollMode.HORIZONTAL);
  755. viewer.classList.toggle('scrollWrapped', scrollMode === _ui_utils.ScrollMode.WRAPPED);
  756. if (!this.pdfDocument || !pageNumber) {
  757. return;
  758. }
  759. if (this._currentScaleValue && isNaN(this._currentScaleValue)) {
  760. this._setScale(this._currentScaleValue, true);
  761. }
  762. this._setCurrentPageNumber(pageNumber, true);
  763. this.update();
  764. }
  765. }, {
  766. key: "_updateSpreadMode",
  767. value: function _updateSpreadMode() {
  768. var pageNumber = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : null;
  769. if (!this.pdfDocument) {
  770. return;
  771. }
  772. var viewer = this.viewer,
  773. pages = this._pages;
  774. viewer.textContent = '';
  775. if (this._spreadMode === _ui_utils.SpreadMode.NONE) {
  776. for (var i = 0, iMax = pages.length; i < iMax; ++i) {
  777. viewer.appendChild(pages[i].div);
  778. }
  779. } else {
  780. var parity = this._spreadMode - 1;
  781. var spread = null;
  782. for (var _i = 0, _iMax = pages.length; _i < _iMax; ++_i) {
  783. if (spread === null) {
  784. spread = document.createElement('div');
  785. spread.className = 'spread';
  786. viewer.appendChild(spread);
  787. } else if (_i % 2 === parity) {
  788. spread = spread.cloneNode(false);
  789. viewer.appendChild(spread);
  790. }
  791. spread.appendChild(pages[_i].div);
  792. }
  793. }
  794. if (!pageNumber) {
  795. return;
  796. }
  797. this._setCurrentPageNumber(pageNumber, true);
  798. this.update();
  799. }
  800. }, {
  801. key: "pagesCount",
  802. get: function get() {
  803. return this._pages.length;
  804. }
  805. }, {
  806. key: "pageViewsReady",
  807. get: function get() {
  808. return this._pageViewsReady;
  809. }
  810. }, {
  811. key: "currentPageNumber",
  812. get: function get() {
  813. return this._currentPageNumber;
  814. },
  815. set: function set(val) {
  816. if (!Number.isInteger(val)) {
  817. throw new Error('Invalid page number.');
  818. }
  819. if (!this.pdfDocument) {
  820. return;
  821. }
  822. if (!this._setCurrentPageNumber(val, true)) {
  823. console.error("".concat(this._name, ".currentPageNumber: \"").concat(val, "\" is not a valid page."));
  824. }
  825. }
  826. }, {
  827. key: "currentPageLabel",
  828. get: function get() {
  829. return this._pageLabels && this._pageLabels[this._currentPageNumber - 1];
  830. },
  831. set: function set(val) {
  832. if (!this.pdfDocument) {
  833. return;
  834. }
  835. var page = val | 0;
  836. if (this._pageLabels) {
  837. var i = this._pageLabels.indexOf(val);
  838. if (i >= 0) {
  839. page = i + 1;
  840. }
  841. }
  842. if (!this._setCurrentPageNumber(page, true)) {
  843. console.error("".concat(this._name, ".currentPageLabel: \"").concat(val, "\" is not a valid page."));
  844. }
  845. }
  846. }, {
  847. key: "currentScale",
  848. get: function get() {
  849. return this._currentScale !== _ui_utils.UNKNOWN_SCALE ? this._currentScale : _ui_utils.DEFAULT_SCALE;
  850. },
  851. set: function set(val) {
  852. if (isNaN(val)) {
  853. throw new Error('Invalid numeric scale.');
  854. }
  855. if (!this.pdfDocument) {
  856. return;
  857. }
  858. this._setScale(val, false);
  859. }
  860. }, {
  861. key: "currentScaleValue",
  862. get: function get() {
  863. return this._currentScaleValue;
  864. },
  865. set: function set(val) {
  866. if (!this.pdfDocument) {
  867. return;
  868. }
  869. this._setScale(val, false);
  870. }
  871. }, {
  872. key: "pagesRotation",
  873. get: function get() {
  874. return this._pagesRotation;
  875. },
  876. set: function set(rotation) {
  877. if (!(0, _ui_utils.isValidRotation)(rotation)) {
  878. throw new Error('Invalid pages rotation angle.');
  879. }
  880. if (!this.pdfDocument) {
  881. return;
  882. }
  883. if (this._pagesRotation === rotation) {
  884. return;
  885. }
  886. this._pagesRotation = rotation;
  887. var pageNumber = this._currentPageNumber;
  888. for (var i = 0, ii = this._pages.length; i < ii; i++) {
  889. var pageView = this._pages[i];
  890. pageView.update(pageView.scale, rotation);
  891. }
  892. if (this._currentScaleValue) {
  893. this._setScale(this._currentScaleValue, true);
  894. }
  895. this.eventBus.dispatch('rotationchanging', {
  896. source: this,
  897. pagesRotation: rotation,
  898. pageNumber: pageNumber
  899. });
  900. if (this.defaultRenderingQueue) {
  901. this.update();
  902. }
  903. }
  904. }, {
  905. key: "_setDocumentViewerElement",
  906. get: function get() {
  907. throw new Error('Not implemented: _setDocumentViewerElement');
  908. }
  909. }, {
  910. key: "_isScrollModeHorizontal",
  911. get: function get() {
  912. return this.isInPresentationMode ? false : this._scrollMode === _ui_utils.ScrollMode.HORIZONTAL;
  913. }
  914. }, {
  915. key: "isInPresentationMode",
  916. get: function get() {
  917. return this.presentationModeState === _ui_utils.PresentationModeState.FULLSCREEN;
  918. }
  919. }, {
  920. key: "isChangingPresentationMode",
  921. get: function get() {
  922. return this.presentationModeState === _ui_utils.PresentationModeState.CHANGING;
  923. }
  924. }, {
  925. key: "isHorizontalScrollbarEnabled",
  926. get: function get() {
  927. return this.isInPresentationMode ? false : this.container.scrollWidth > this.container.clientWidth;
  928. }
  929. }, {
  930. key: "isVerticalScrollbarEnabled",
  931. get: function get() {
  932. return this.isInPresentationMode ? false : this.container.scrollHeight > this.container.clientHeight;
  933. }
  934. }, {
  935. key: "hasEqualPageSizes",
  936. get: function get() {
  937. var firstPageView = this._pages[0];
  938. for (var i = 1, ii = this._pages.length; i < ii; ++i) {
  939. var pageView = this._pages[i];
  940. if (pageView.width !== firstPageView.width || pageView.height !== firstPageView.height) {
  941. return false;
  942. }
  943. }
  944. return true;
  945. }
  946. }, {
  947. key: "scrollMode",
  948. get: function get() {
  949. return this._scrollMode;
  950. },
  951. set: function set(mode) {
  952. if (this._scrollMode === mode) {
  953. return;
  954. }
  955. if (!(0, _ui_utils.isValidScrollMode)(mode)) {
  956. throw new Error("Invalid scroll mode: ".concat(mode));
  957. }
  958. this._scrollMode = mode;
  959. this.eventBus.dispatch('scrollmodechanged', {
  960. source: this,
  961. mode: mode
  962. });
  963. this._updateScrollMode(this._currentPageNumber);
  964. }
  965. }, {
  966. key: "spreadMode",
  967. get: function get() {
  968. return this._spreadMode;
  969. },
  970. set: function set(mode) {
  971. if (this._spreadMode === mode) {
  972. return;
  973. }
  974. if (!(0, _ui_utils.isValidSpreadMode)(mode)) {
  975. throw new Error("Invalid spread mode: ".concat(mode));
  976. }
  977. this._spreadMode = mode;
  978. this.eventBus.dispatch('spreadmodechanged', {
  979. source: this,
  980. mode: mode
  981. });
  982. this._updateSpreadMode(this._currentPageNumber);
  983. }
  984. }]);
  985. return BaseViewer;
  986. }();
  987. exports.BaseViewer = BaseViewer;