base_viewer.js 29 KB

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