base_viewer.js 37 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448
  1. /**
  2. * @licstart The following is the entire license notice for the
  3. * Javascript code in this page
  4. *
  5. * Copyright 2021 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 _pdf = require("../pdf");
  28. var _ui_utils = require("./ui_utils.js");
  29. var _pdf_rendering_queue = require("./pdf_rendering_queue.js");
  30. var _annotation_layer_builder = require("./annotation_layer_builder.js");
  31. var _l10n_utils = require("./l10n_utils.js");
  32. var _pdf_page_view = require("./pdf_page_view.js");
  33. var _pdf_link_service = require("./pdf_link_service.js");
  34. var _struct_tree_layer_builder = require("./struct_tree_layer_builder.js");
  35. var _text_layer_builder = require("./text_layer_builder.js");
  36. var _xfa_layer_builder = require("./xfa_layer_builder.js");
  37. const DEFAULT_CACHE_SIZE = 10;
  38. function PDFPageViewBuffer(size) {
  39. const data = [];
  40. this.push = function (view) {
  41. const 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. const pageIdsToKeep = new Set();
  54. for (let 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. this.has = function (view) {
  66. return data.includes(view);
  67. };
  68. }
  69. function isSameScale(oldScale, newScale) {
  70. if (newScale === oldScale) {
  71. return true;
  72. }
  73. if (Math.abs(newScale - oldScale) < 1e-15) {
  74. return true;
  75. }
  76. return false;
  77. }
  78. class BaseViewer {
  79. constructor(options) {
  80. if (this.constructor === BaseViewer) {
  81. throw new Error("Cannot initialize BaseViewer.");
  82. }
  83. const viewerVersion = '2.10.377';
  84. if (_pdf.version !== viewerVersion) {
  85. throw new Error(`The API version "${_pdf.version}" does not match the Viewer version "${viewerVersion}".`);
  86. }
  87. this._name = this.constructor.name;
  88. this.container = options.container;
  89. this.viewer = options.viewer || options.container.firstElementChild;
  90. if (!(this.container?.tagName.toUpperCase() === "DIV" && this.viewer?.tagName.toUpperCase() === "DIV")) {
  91. throw new Error("Invalid `container` and/or `viewer` option.");
  92. }
  93. if (this.container.offsetParent && getComputedStyle(this.container).position !== "absolute") {
  94. throw new Error("The `container` must be absolutely positioned.");
  95. }
  96. this.eventBus = options.eventBus;
  97. this.linkService = options.linkService || new _pdf_link_service.SimpleLinkService();
  98. this.downloadManager = options.downloadManager || null;
  99. this.findController = options.findController || null;
  100. this._scriptingManager = options.scriptingManager || null;
  101. this.removePageBorders = options.removePageBorders || false;
  102. this.textLayerMode = Number.isInteger(options.textLayerMode) ? options.textLayerMode : _ui_utils.TextLayerMode.ENABLE;
  103. this.imageResourcesPath = options.imageResourcesPath || "";
  104. this.renderInteractiveForms = options.renderInteractiveForms !== false;
  105. this.enablePrintAutoRotate = options.enablePrintAutoRotate || false;
  106. this.renderer = options.renderer || _ui_utils.RendererType.CANVAS;
  107. this.useOnlyCssZoom = options.useOnlyCssZoom || false;
  108. this.maxCanvasPixels = options.maxCanvasPixels;
  109. this.l10n = options.l10n || _l10n_utils.NullL10n;
  110. this.enableScripting = options.enableScripting === true && !!this._scriptingManager;
  111. this.defaultRenderingQueue = !options.renderingQueue;
  112. if (this.defaultRenderingQueue) {
  113. this.renderingQueue = new _pdf_rendering_queue.PDFRenderingQueue();
  114. this.renderingQueue.setViewer(this);
  115. } else {
  116. this.renderingQueue = options.renderingQueue;
  117. }
  118. this.scroll = (0, _ui_utils.watchScroll)(this.container, this._scrollUpdate.bind(this));
  119. this.presentationModeState = _ui_utils.PresentationModeState.UNKNOWN;
  120. this._onBeforeDraw = this._onAfterDraw = null;
  121. this._resetView();
  122. if (this.removePageBorders) {
  123. this.viewer.classList.add("removePageBorders");
  124. }
  125. Promise.resolve().then(() => {
  126. this.eventBus.dispatch("baseviewerinit", {
  127. source: this
  128. });
  129. });
  130. }
  131. get pagesCount() {
  132. return this._pages.length;
  133. }
  134. getPageView(index) {
  135. return this._pages[index];
  136. }
  137. get pageViewsReady() {
  138. if (!this._pagesCapability.settled) {
  139. return false;
  140. }
  141. return this._pages.every(function (pageView) {
  142. return pageView?.pdfPage;
  143. });
  144. }
  145. get currentPageNumber() {
  146. return this._currentPageNumber;
  147. }
  148. set currentPageNumber(val) {
  149. if (!Number.isInteger(val)) {
  150. throw new Error("Invalid page number.");
  151. }
  152. if (!this.pdfDocument) {
  153. return;
  154. }
  155. if (!this._setCurrentPageNumber(val, true)) {
  156. console.error(`${this._name}.currentPageNumber: "${val}" is not a valid page.`);
  157. }
  158. }
  159. _setCurrentPageNumber(val, resetCurrentPageView = false) {
  160. if (this._currentPageNumber === val) {
  161. if (resetCurrentPageView) {
  162. this._resetCurrentPageView();
  163. }
  164. return true;
  165. }
  166. if (!(0 < val && val <= this.pagesCount)) {
  167. return false;
  168. }
  169. const previous = this._currentPageNumber;
  170. this._currentPageNumber = val;
  171. this.eventBus.dispatch("pagechanging", {
  172. source: this,
  173. pageNumber: val,
  174. pageLabel: this._pageLabels?.[val - 1] ?? null,
  175. previous
  176. });
  177. if (resetCurrentPageView) {
  178. this._resetCurrentPageView();
  179. }
  180. return true;
  181. }
  182. get currentPageLabel() {
  183. return this._pageLabels?.[this._currentPageNumber - 1] ?? null;
  184. }
  185. set currentPageLabel(val) {
  186. if (!this.pdfDocument) {
  187. return;
  188. }
  189. let page = val | 0;
  190. if (this._pageLabels) {
  191. const i = this._pageLabels.indexOf(val);
  192. if (i >= 0) {
  193. page = i + 1;
  194. }
  195. }
  196. if (!this._setCurrentPageNumber(page, true)) {
  197. console.error(`${this._name}.currentPageLabel: "${val}" is not a valid page.`);
  198. }
  199. }
  200. get currentScale() {
  201. return this._currentScale !== _ui_utils.UNKNOWN_SCALE ? this._currentScale : _ui_utils.DEFAULT_SCALE;
  202. }
  203. set currentScale(val) {
  204. if (isNaN(val)) {
  205. throw new Error("Invalid numeric scale.");
  206. }
  207. if (!this.pdfDocument) {
  208. return;
  209. }
  210. this._setScale(val, false);
  211. }
  212. get currentScaleValue() {
  213. return this._currentScaleValue;
  214. }
  215. set currentScaleValue(val) {
  216. if (!this.pdfDocument) {
  217. return;
  218. }
  219. this._setScale(val, false);
  220. }
  221. get pagesRotation() {
  222. return this._pagesRotation;
  223. }
  224. set pagesRotation(rotation) {
  225. if (!(0, _ui_utils.isValidRotation)(rotation)) {
  226. throw new Error("Invalid pages rotation angle.");
  227. }
  228. if (!this.pdfDocument) {
  229. return;
  230. }
  231. rotation %= 360;
  232. if (rotation < 0) {
  233. rotation += 360;
  234. }
  235. if (this._pagesRotation === rotation) {
  236. return;
  237. }
  238. this._pagesRotation = rotation;
  239. const pageNumber = this._currentPageNumber;
  240. for (let i = 0, ii = this._pages.length; i < ii; i++) {
  241. const pageView = this._pages[i];
  242. pageView.update(pageView.scale, rotation);
  243. }
  244. if (this._currentScaleValue) {
  245. this._setScale(this._currentScaleValue, true);
  246. }
  247. this.eventBus.dispatch("rotationchanging", {
  248. source: this,
  249. pagesRotation: rotation,
  250. pageNumber
  251. });
  252. if (this.defaultRenderingQueue) {
  253. this.update();
  254. }
  255. }
  256. get firstPagePromise() {
  257. return this.pdfDocument ? this._firstPageCapability.promise : null;
  258. }
  259. get onePageRendered() {
  260. return this.pdfDocument ? this._onePageRenderedCapability.promise : null;
  261. }
  262. get pagesPromise() {
  263. return this.pdfDocument ? this._pagesCapability.promise : null;
  264. }
  265. get _viewerElement() {
  266. throw new Error("Not implemented: _viewerElement");
  267. }
  268. _onePageRenderedOrForceFetch() {
  269. if (!this.container.offsetParent || this._getVisiblePages().views.length === 0) {
  270. return Promise.resolve();
  271. }
  272. return this._onePageRenderedCapability.promise;
  273. }
  274. setDocument(pdfDocument) {
  275. if (this.pdfDocument) {
  276. this.eventBus.dispatch("pagesdestroy", {
  277. source: this
  278. });
  279. this._cancelRendering();
  280. this._resetView();
  281. if (this.findController) {
  282. this.findController.setDocument(null);
  283. }
  284. if (this._scriptingManager) {
  285. this._scriptingManager.setDocument(null);
  286. }
  287. }
  288. this.pdfDocument = pdfDocument;
  289. if (!pdfDocument) {
  290. return;
  291. }
  292. const isPureXfa = pdfDocument.isPureXfa;
  293. const pagesCount = pdfDocument.numPages;
  294. const firstPagePromise = pdfDocument.getPage(1);
  295. const optionalContentConfigPromise = pdfDocument.getOptionalContentConfig();
  296. this._pagesCapability.promise.then(() => {
  297. this.eventBus.dispatch("pagesloaded", {
  298. source: this,
  299. pagesCount
  300. });
  301. });
  302. this._onBeforeDraw = evt => {
  303. const pageView = this._pages[evt.pageNumber - 1];
  304. if (!pageView) {
  305. return;
  306. }
  307. this._buffer.push(pageView);
  308. };
  309. this.eventBus._on("pagerender", this._onBeforeDraw);
  310. this._onAfterDraw = evt => {
  311. if (evt.cssTransform || this._onePageRenderedCapability.settled) {
  312. return;
  313. }
  314. this._onePageRenderedCapability.resolve();
  315. this.eventBus._off("pagerendered", this._onAfterDraw);
  316. this._onAfterDraw = null;
  317. };
  318. this.eventBus._on("pagerendered", this._onAfterDraw);
  319. firstPagePromise.then(firstPdfPage => {
  320. this._firstPageCapability.resolve(firstPdfPage);
  321. this._optionalContentConfigPromise = optionalContentConfigPromise;
  322. const scale = this.currentScale;
  323. const viewport = firstPdfPage.getViewport({
  324. scale: scale * _ui_utils.CSS_UNITS
  325. });
  326. const textLayerFactory = this.textLayerMode !== _ui_utils.TextLayerMode.DISABLE ? this : null;
  327. const xfaLayerFactory = isPureXfa ? this : null;
  328. for (let pageNum = 1; pageNum <= pagesCount; ++pageNum) {
  329. const pageView = new _pdf_page_view.PDFPageView({
  330. container: this._viewerElement,
  331. eventBus: this.eventBus,
  332. id: pageNum,
  333. scale,
  334. defaultViewport: viewport.clone(),
  335. optionalContentConfigPromise,
  336. renderingQueue: this.renderingQueue,
  337. textLayerFactory,
  338. textLayerMode: this.textLayerMode,
  339. annotationLayerFactory: this,
  340. xfaLayerFactory,
  341. structTreeLayerFactory: this,
  342. imageResourcesPath: this.imageResourcesPath,
  343. renderInteractiveForms: this.renderInteractiveForms,
  344. renderer: this.renderer,
  345. useOnlyCssZoom: this.useOnlyCssZoom,
  346. maxCanvasPixels: this.maxCanvasPixels,
  347. l10n: this.l10n
  348. });
  349. this._pages.push(pageView);
  350. }
  351. const firstPageView = this._pages[0];
  352. if (firstPageView) {
  353. firstPageView.setPdfPage(firstPdfPage);
  354. this.linkService.cachePageRef(1, firstPdfPage.ref);
  355. }
  356. if (this._spreadMode !== _ui_utils.SpreadMode.NONE) {
  357. this._updateSpreadMode();
  358. }
  359. this._onePageRenderedOrForceFetch().then(() => {
  360. if (this.findController) {
  361. this.findController.setDocument(pdfDocument);
  362. }
  363. if (this.enableScripting) {
  364. this._scriptingManager.setDocument(pdfDocument);
  365. }
  366. if (pdfDocument.loadingParams.disableAutoFetch || pagesCount > 7500) {
  367. this._pagesCapability.resolve();
  368. return;
  369. }
  370. let getPagesLeft = pagesCount - 1;
  371. if (getPagesLeft <= 0) {
  372. this._pagesCapability.resolve();
  373. return;
  374. }
  375. for (let pageNum = 2; pageNum <= pagesCount; ++pageNum) {
  376. pdfDocument.getPage(pageNum).then(pdfPage => {
  377. const pageView = this._pages[pageNum - 1];
  378. if (!pageView.pdfPage) {
  379. pageView.setPdfPage(pdfPage);
  380. }
  381. this.linkService.cachePageRef(pageNum, pdfPage.ref);
  382. if (--getPagesLeft === 0) {
  383. this._pagesCapability.resolve();
  384. }
  385. }, reason => {
  386. console.error(`Unable to get page ${pageNum} to initialize viewer`, reason);
  387. if (--getPagesLeft === 0) {
  388. this._pagesCapability.resolve();
  389. }
  390. });
  391. }
  392. });
  393. this.eventBus.dispatch("pagesinit", {
  394. source: this
  395. });
  396. if (this.defaultRenderingQueue) {
  397. this.update();
  398. }
  399. }).catch(reason => {
  400. console.error("Unable to initialize viewer", reason);
  401. });
  402. }
  403. setPageLabels(labels) {
  404. if (!this.pdfDocument) {
  405. return;
  406. }
  407. if (!labels) {
  408. this._pageLabels = null;
  409. } else if (!(Array.isArray(labels) && this.pdfDocument.numPages === labels.length)) {
  410. this._pageLabels = null;
  411. console.error(`${this._name}.setPageLabels: Invalid page labels.`);
  412. } else {
  413. this._pageLabels = labels;
  414. }
  415. for (let i = 0, ii = this._pages.length; i < ii; i++) {
  416. this._pages[i].setPageLabel(this._pageLabels?.[i] ?? null);
  417. }
  418. }
  419. _resetView() {
  420. this._pages = [];
  421. this._currentPageNumber = 1;
  422. this._currentScale = _ui_utils.UNKNOWN_SCALE;
  423. this._currentScaleValue = null;
  424. this._pageLabels = null;
  425. this._buffer = new PDFPageViewBuffer(DEFAULT_CACHE_SIZE);
  426. this._location = null;
  427. this._pagesRotation = 0;
  428. this._optionalContentConfigPromise = null;
  429. this._pagesRequests = new WeakMap();
  430. this._firstPageCapability = (0, _pdf.createPromiseCapability)();
  431. this._onePageRenderedCapability = (0, _pdf.createPromiseCapability)();
  432. this._pagesCapability = (0, _pdf.createPromiseCapability)();
  433. this._scrollMode = _ui_utils.ScrollMode.VERTICAL;
  434. this._spreadMode = _ui_utils.SpreadMode.NONE;
  435. if (this._onBeforeDraw) {
  436. this.eventBus._off("pagerender", this._onBeforeDraw);
  437. this._onBeforeDraw = null;
  438. }
  439. if (this._onAfterDraw) {
  440. this.eventBus._off("pagerendered", this._onAfterDraw);
  441. this._onAfterDraw = null;
  442. }
  443. this.viewer.textContent = "";
  444. this._updateScrollMode();
  445. }
  446. _scrollUpdate() {
  447. if (this.pagesCount === 0) {
  448. return;
  449. }
  450. this.update();
  451. }
  452. _scrollIntoView({
  453. pageDiv,
  454. pageSpot = null,
  455. pageNumber = null
  456. }) {
  457. (0, _ui_utils.scrollIntoView)(pageDiv, pageSpot);
  458. }
  459. _setScaleUpdatePages(newScale, newValue, noScroll = false, preset = false) {
  460. this._currentScaleValue = newValue.toString();
  461. if (isSameScale(this._currentScale, newScale)) {
  462. if (preset) {
  463. this.eventBus.dispatch("scalechanging", {
  464. source: this,
  465. scale: newScale,
  466. presetValue: newValue
  467. });
  468. }
  469. return;
  470. }
  471. for (let i = 0, ii = this._pages.length; i < ii; i++) {
  472. this._pages[i].update(newScale);
  473. }
  474. this._currentScale = newScale;
  475. if (!noScroll) {
  476. let page = this._currentPageNumber,
  477. dest;
  478. if (this._location && !(this.isInPresentationMode || this.isChangingPresentationMode)) {
  479. page = this._location.pageNumber;
  480. dest = [null, {
  481. name: "XYZ"
  482. }, this._location.left, this._location.top, null];
  483. }
  484. this.scrollPageIntoView({
  485. pageNumber: page,
  486. destArray: dest,
  487. allowNegativeOffset: true
  488. });
  489. }
  490. this.eventBus.dispatch("scalechanging", {
  491. source: this,
  492. scale: newScale,
  493. presetValue: preset ? newValue : undefined
  494. });
  495. if (this.defaultRenderingQueue) {
  496. this.update();
  497. }
  498. }
  499. get _pageWidthScaleFactor() {
  500. if (this._spreadMode !== _ui_utils.SpreadMode.NONE && this._scrollMode !== _ui_utils.ScrollMode.HORIZONTAL && !this.isInPresentationMode) {
  501. return 2;
  502. }
  503. return 1;
  504. }
  505. _setScale(value, noScroll = false) {
  506. let scale = parseFloat(value);
  507. if (scale > 0) {
  508. this._setScaleUpdatePages(scale, value, noScroll, false);
  509. } else {
  510. const currentPage = this._pages[this._currentPageNumber - 1];
  511. if (!currentPage) {
  512. return;
  513. }
  514. const noPadding = this.isInPresentationMode || this.removePageBorders;
  515. let hPadding = noPadding ? 0 : _ui_utils.SCROLLBAR_PADDING;
  516. let vPadding = noPadding ? 0 : _ui_utils.VERTICAL_PADDING;
  517. if (!noPadding && this._isScrollModeHorizontal) {
  518. [hPadding, vPadding] = [vPadding, hPadding];
  519. }
  520. const pageWidthScale = (this.container.clientWidth - hPadding) / currentPage.width * currentPage.scale / this._pageWidthScaleFactor;
  521. const pageHeightScale = (this.container.clientHeight - vPadding) / currentPage.height * currentPage.scale;
  522. switch (value) {
  523. case "page-actual":
  524. scale = 1;
  525. break;
  526. case "page-width":
  527. scale = pageWidthScale;
  528. break;
  529. case "page-height":
  530. scale = pageHeightScale;
  531. break;
  532. case "page-fit":
  533. scale = Math.min(pageWidthScale, pageHeightScale);
  534. break;
  535. case "auto":
  536. const horizontalScale = (0, _ui_utils.isPortraitOrientation)(currentPage) ? pageWidthScale : Math.min(pageHeightScale, pageWidthScale);
  537. scale = Math.min(_ui_utils.MAX_AUTO_SCALE, horizontalScale);
  538. break;
  539. default:
  540. console.error(`${this._name}._setScale: "${value}" is an unknown zoom value.`);
  541. return;
  542. }
  543. this._setScaleUpdatePages(scale, value, noScroll, true);
  544. }
  545. }
  546. _resetCurrentPageView() {
  547. if (this.isInPresentationMode) {
  548. this._setScale(this._currentScaleValue, true);
  549. }
  550. const pageView = this._pages[this._currentPageNumber - 1];
  551. this._scrollIntoView({
  552. pageDiv: pageView.div
  553. });
  554. }
  555. pageLabelToPageNumber(label) {
  556. if (!this._pageLabels) {
  557. return null;
  558. }
  559. const i = this._pageLabels.indexOf(label);
  560. if (i < 0) {
  561. return null;
  562. }
  563. return i + 1;
  564. }
  565. scrollPageIntoView({
  566. pageNumber,
  567. destArray = null,
  568. allowNegativeOffset = false,
  569. ignoreDestinationZoom = false
  570. }) {
  571. if (!this.pdfDocument) {
  572. return;
  573. }
  574. const pageView = Number.isInteger(pageNumber) && this._pages[pageNumber - 1];
  575. if (!pageView) {
  576. console.error(`${this._name}.scrollPageIntoView: ` + `"${pageNumber}" is not a valid pageNumber parameter.`);
  577. return;
  578. }
  579. if (this.isInPresentationMode || !destArray) {
  580. this._setCurrentPageNumber(pageNumber, true);
  581. return;
  582. }
  583. let x = 0,
  584. y = 0;
  585. let width = 0,
  586. height = 0,
  587. widthScale,
  588. heightScale;
  589. const changeOrientation = pageView.rotation % 180 !== 0;
  590. const pageWidth = (changeOrientation ? pageView.height : pageView.width) / pageView.scale / _ui_utils.CSS_UNITS;
  591. const pageHeight = (changeOrientation ? pageView.width : pageView.height) / pageView.scale / _ui_utils.CSS_UNITS;
  592. let scale = 0;
  593. switch (destArray[1].name) {
  594. case "XYZ":
  595. x = destArray[2];
  596. y = destArray[3];
  597. scale = destArray[4];
  598. x = x !== null ? x : 0;
  599. y = y !== null ? y : pageHeight;
  600. break;
  601. case "Fit":
  602. case "FitB":
  603. scale = "page-fit";
  604. break;
  605. case "FitH":
  606. case "FitBH":
  607. y = destArray[2];
  608. scale = "page-width";
  609. if (y === null && this._location) {
  610. x = this._location.left;
  611. y = this._location.top;
  612. } else if (typeof y !== "number") {
  613. y = pageHeight;
  614. }
  615. break;
  616. case "FitV":
  617. case "FitBV":
  618. x = destArray[2];
  619. width = pageWidth;
  620. height = pageHeight;
  621. scale = "page-height";
  622. break;
  623. case "FitR":
  624. x = destArray[2];
  625. y = destArray[3];
  626. width = destArray[4] - x;
  627. height = destArray[5] - y;
  628. const hPadding = this.removePageBorders ? 0 : _ui_utils.SCROLLBAR_PADDING;
  629. const vPadding = this.removePageBorders ? 0 : _ui_utils.VERTICAL_PADDING;
  630. widthScale = (this.container.clientWidth - hPadding) / width / _ui_utils.CSS_UNITS;
  631. heightScale = (this.container.clientHeight - vPadding) / height / _ui_utils.CSS_UNITS;
  632. scale = Math.min(Math.abs(widthScale), Math.abs(heightScale));
  633. break;
  634. default:
  635. console.error(`${this._name}.scrollPageIntoView: ` + `"${destArray[1].name}" is not a valid destination type.`);
  636. return;
  637. }
  638. if (!ignoreDestinationZoom) {
  639. if (scale && scale !== this._currentScale) {
  640. this.currentScaleValue = scale;
  641. } else if (this._currentScale === _ui_utils.UNKNOWN_SCALE) {
  642. this.currentScaleValue = _ui_utils.DEFAULT_SCALE_VALUE;
  643. }
  644. }
  645. if (scale === "page-fit" && !destArray[4]) {
  646. this._scrollIntoView({
  647. pageDiv: pageView.div,
  648. pageNumber
  649. });
  650. return;
  651. }
  652. const boundingRect = [pageView.viewport.convertToViewportPoint(x, y), pageView.viewport.convertToViewportPoint(x + width, y + height)];
  653. let left = Math.min(boundingRect[0][0], boundingRect[1][0]);
  654. let top = Math.min(boundingRect[0][1], boundingRect[1][1]);
  655. if (!allowNegativeOffset) {
  656. left = Math.max(left, 0);
  657. top = Math.max(top, 0);
  658. }
  659. this._scrollIntoView({
  660. pageDiv: pageView.div,
  661. pageSpot: {
  662. left,
  663. top
  664. },
  665. pageNumber
  666. });
  667. }
  668. _updateLocation(firstPage) {
  669. const currentScale = this._currentScale;
  670. const currentScaleValue = this._currentScaleValue;
  671. const normalizedScaleValue = parseFloat(currentScaleValue) === currentScale ? Math.round(currentScale * 10000) / 100 : currentScaleValue;
  672. const pageNumber = firstPage.id;
  673. let pdfOpenParams = "#page=" + pageNumber;
  674. pdfOpenParams += "&zoom=" + normalizedScaleValue;
  675. const currentPageView = this._pages[pageNumber - 1];
  676. const container = this.container;
  677. const topLeft = currentPageView.getPagePoint(container.scrollLeft - firstPage.x, container.scrollTop - firstPage.y);
  678. const intLeft = Math.round(topLeft[0]);
  679. const intTop = Math.round(topLeft[1]);
  680. pdfOpenParams += "," + intLeft + "," + intTop;
  681. this._location = {
  682. pageNumber,
  683. scale: normalizedScaleValue,
  684. top: intTop,
  685. left: intLeft,
  686. rotation: this._pagesRotation,
  687. pdfOpenParams
  688. };
  689. }
  690. _updateHelper(visiblePages) {
  691. throw new Error("Not implemented: _updateHelper");
  692. }
  693. update() {
  694. const visible = this._getVisiblePages();
  695. const visiblePages = visible.views,
  696. numVisiblePages = visiblePages.length;
  697. if (numVisiblePages === 0) {
  698. return;
  699. }
  700. const newCacheSize = Math.max(DEFAULT_CACHE_SIZE, 2 * numVisiblePages + 1);
  701. this._buffer.resize(newCacheSize, visiblePages);
  702. this.renderingQueue.renderHighestPriority(visible);
  703. this._updateHelper(visiblePages);
  704. this._updateLocation(visible.first);
  705. this.eventBus.dispatch("updateviewarea", {
  706. source: this,
  707. location: this._location
  708. });
  709. }
  710. containsElement(element) {
  711. return this.container.contains(element);
  712. }
  713. focus() {
  714. this.container.focus();
  715. }
  716. get _isScrollModeHorizontal() {
  717. return this.isInPresentationMode ? false : this._scrollMode === _ui_utils.ScrollMode.HORIZONTAL;
  718. }
  719. get _isContainerRtl() {
  720. return getComputedStyle(this.container).direction === "rtl";
  721. }
  722. get isInPresentationMode() {
  723. return this.presentationModeState === _ui_utils.PresentationModeState.FULLSCREEN;
  724. }
  725. get isChangingPresentationMode() {
  726. return this.presentationModeState === _ui_utils.PresentationModeState.CHANGING;
  727. }
  728. get isHorizontalScrollbarEnabled() {
  729. return this.isInPresentationMode ? false : this.container.scrollWidth > this.container.clientWidth;
  730. }
  731. get isVerticalScrollbarEnabled() {
  732. return this.isInPresentationMode ? false : this.container.scrollHeight > this.container.clientHeight;
  733. }
  734. _getCurrentVisiblePage() {
  735. if (!this.pagesCount) {
  736. return {
  737. views: []
  738. };
  739. }
  740. const pageView = this._pages[this._currentPageNumber - 1];
  741. const element = pageView.div;
  742. const view = {
  743. id: pageView.id,
  744. x: element.offsetLeft + element.clientLeft,
  745. y: element.offsetTop + element.clientTop,
  746. view: pageView
  747. };
  748. return {
  749. first: view,
  750. last: view,
  751. views: [view]
  752. };
  753. }
  754. _getVisiblePages() {
  755. return (0, _ui_utils.getVisibleElements)({
  756. scrollEl: this.container,
  757. views: this._pages,
  758. sortByVisibility: true,
  759. horizontal: this._isScrollModeHorizontal,
  760. rtl: this._isScrollModeHorizontal && this._isContainerRtl
  761. });
  762. }
  763. isPageVisible(pageNumber) {
  764. if (!this.pdfDocument) {
  765. return false;
  766. }
  767. if (!(Number.isInteger(pageNumber) && pageNumber > 0 && pageNumber <= this.pagesCount)) {
  768. console.error(`${this._name}.isPageVisible: "${pageNumber}" is not a valid page.`);
  769. return false;
  770. }
  771. return this._getVisiblePages().views.some(function (view) {
  772. return view.id === pageNumber;
  773. });
  774. }
  775. isPageCached(pageNumber) {
  776. if (!this.pdfDocument || !this._buffer) {
  777. return false;
  778. }
  779. if (!(Number.isInteger(pageNumber) && pageNumber > 0 && pageNumber <= this.pagesCount)) {
  780. console.error(`${this._name}.isPageCached: "${pageNumber}" is not a valid page.`);
  781. return false;
  782. }
  783. const pageView = this._pages[pageNumber - 1];
  784. if (!pageView) {
  785. return false;
  786. }
  787. return this._buffer.has(pageView);
  788. }
  789. cleanup() {
  790. for (let i = 0, ii = this._pages.length; i < ii; i++) {
  791. if (this._pages[i] && this._pages[i].renderingState !== _pdf_rendering_queue.RenderingStates.FINISHED) {
  792. this._pages[i].reset();
  793. }
  794. }
  795. }
  796. _cancelRendering() {
  797. for (let i = 0, ii = this._pages.length; i < ii; i++) {
  798. if (this._pages[i]) {
  799. this._pages[i].cancelRendering();
  800. }
  801. }
  802. }
  803. _ensurePdfPageLoaded(pageView) {
  804. if (pageView.pdfPage) {
  805. return Promise.resolve(pageView.pdfPage);
  806. }
  807. if (this._pagesRequests.has(pageView)) {
  808. return this._pagesRequests.get(pageView);
  809. }
  810. const promise = this.pdfDocument.getPage(pageView.id).then(pdfPage => {
  811. if (!pageView.pdfPage) {
  812. pageView.setPdfPage(pdfPage);
  813. }
  814. this._pagesRequests.delete(pageView);
  815. return pdfPage;
  816. }).catch(reason => {
  817. console.error("Unable to get page for page view", reason);
  818. this._pagesRequests.delete(pageView);
  819. });
  820. this._pagesRequests.set(pageView, promise);
  821. return promise;
  822. }
  823. forceRendering(currentlyVisiblePages) {
  824. const visiblePages = currentlyVisiblePages || this._getVisiblePages();
  825. const scrollAhead = this._isScrollModeHorizontal ? this.scroll.right : this.scroll.down;
  826. const pageView = this.renderingQueue.getHighestPriority(visiblePages, this._pages, scrollAhead);
  827. if (pageView) {
  828. this._ensurePdfPageLoaded(pageView).then(() => {
  829. this.renderingQueue.renderView(pageView);
  830. });
  831. return true;
  832. }
  833. return false;
  834. }
  835. createTextLayerBuilder(textLayerDiv, pageIndex, viewport, enhanceTextSelection = false, eventBus) {
  836. return new _text_layer_builder.TextLayerBuilder({
  837. textLayerDiv,
  838. eventBus,
  839. pageIndex,
  840. viewport,
  841. findController: this.isInPresentationMode ? null : this.findController,
  842. enhanceTextSelection: this.isInPresentationMode ? false : enhanceTextSelection
  843. });
  844. }
  845. createAnnotationLayerBuilder(pageDiv, pdfPage, annotationStorage = null, imageResourcesPath = "", renderInteractiveForms = false, l10n = _l10n_utils.NullL10n, enableScripting = null, hasJSActionsPromise = null, mouseState = null) {
  846. return new _annotation_layer_builder.AnnotationLayerBuilder({
  847. pageDiv,
  848. pdfPage,
  849. annotationStorage: annotationStorage || this.pdfDocument?.annotationStorage,
  850. imageResourcesPath,
  851. renderInteractiveForms,
  852. linkService: this.linkService,
  853. downloadManager: this.downloadManager,
  854. l10n,
  855. enableScripting: enableScripting ?? this.enableScripting,
  856. hasJSActionsPromise: hasJSActionsPromise || this.pdfDocument?.hasJSActions(),
  857. mouseState: mouseState || this._scriptingManager?.mouseState
  858. });
  859. }
  860. createXfaLayerBuilder(pageDiv, pdfPage, annotationStorage = null) {
  861. return new _xfa_layer_builder.XfaLayerBuilder({
  862. pageDiv,
  863. pdfPage,
  864. annotationStorage: annotationStorage || this.pdfDocument?.annotationStorage
  865. });
  866. }
  867. createStructTreeLayerBuilder(pdfPage) {
  868. return new _struct_tree_layer_builder.StructTreeLayerBuilder({
  869. pdfPage
  870. });
  871. }
  872. get hasEqualPageSizes() {
  873. const firstPageView = this._pages[0];
  874. for (let i = 1, ii = this._pages.length; i < ii; ++i) {
  875. const pageView = this._pages[i];
  876. if (pageView.width !== firstPageView.width || pageView.height !== firstPageView.height) {
  877. return false;
  878. }
  879. }
  880. return true;
  881. }
  882. getPagesOverview() {
  883. return this._pages.map(pageView => {
  884. const viewport = pageView.pdfPage.getViewport({
  885. scale: 1
  886. });
  887. if (!this.enablePrintAutoRotate || (0, _ui_utils.isPortraitOrientation)(viewport)) {
  888. return {
  889. width: viewport.width,
  890. height: viewport.height,
  891. rotation: viewport.rotation
  892. };
  893. }
  894. return {
  895. width: viewport.height,
  896. height: viewport.width,
  897. rotation: (viewport.rotation - 90) % 360
  898. };
  899. });
  900. }
  901. get optionalContentConfigPromise() {
  902. if (!this.pdfDocument) {
  903. return Promise.resolve(null);
  904. }
  905. if (!this._optionalContentConfigPromise) {
  906. return this.pdfDocument.getOptionalContentConfig();
  907. }
  908. return this._optionalContentConfigPromise;
  909. }
  910. set optionalContentConfigPromise(promise) {
  911. if (!(promise instanceof Promise)) {
  912. throw new Error(`Invalid optionalContentConfigPromise: ${promise}`);
  913. }
  914. if (!this.pdfDocument) {
  915. return;
  916. }
  917. if (!this._optionalContentConfigPromise) {
  918. return;
  919. }
  920. this._optionalContentConfigPromise = promise;
  921. for (const pageView of this._pages) {
  922. pageView.update(pageView.scale, pageView.rotation, promise);
  923. }
  924. this.update();
  925. this.eventBus.dispatch("optionalcontentconfigchanged", {
  926. source: this,
  927. promise
  928. });
  929. }
  930. get scrollMode() {
  931. return this._scrollMode;
  932. }
  933. set scrollMode(mode) {
  934. if (this._scrollMode === mode) {
  935. return;
  936. }
  937. if (!(0, _ui_utils.isValidScrollMode)(mode)) {
  938. throw new Error(`Invalid scroll mode: ${mode}`);
  939. }
  940. this._scrollMode = mode;
  941. this.eventBus.dispatch("scrollmodechanged", {
  942. source: this,
  943. mode
  944. });
  945. this._updateScrollMode(this._currentPageNumber);
  946. }
  947. _updateScrollMode(pageNumber = null) {
  948. const scrollMode = this._scrollMode,
  949. viewer = this.viewer;
  950. viewer.classList.toggle("scrollHorizontal", scrollMode === _ui_utils.ScrollMode.HORIZONTAL);
  951. viewer.classList.toggle("scrollWrapped", scrollMode === _ui_utils.ScrollMode.WRAPPED);
  952. if (!this.pdfDocument || !pageNumber) {
  953. return;
  954. }
  955. if (this._currentScaleValue && isNaN(this._currentScaleValue)) {
  956. this._setScale(this._currentScaleValue, true);
  957. }
  958. this._setCurrentPageNumber(pageNumber, true);
  959. this.update();
  960. }
  961. get spreadMode() {
  962. return this._spreadMode;
  963. }
  964. set spreadMode(mode) {
  965. if (this._spreadMode === mode) {
  966. return;
  967. }
  968. if (!(0, _ui_utils.isValidSpreadMode)(mode)) {
  969. throw new Error(`Invalid spread mode: ${mode}`);
  970. }
  971. this._spreadMode = mode;
  972. this.eventBus.dispatch("spreadmodechanged", {
  973. source: this,
  974. mode
  975. });
  976. this._updateSpreadMode(this._currentPageNumber);
  977. }
  978. _updateSpreadMode(pageNumber = null) {
  979. if (!this.pdfDocument) {
  980. return;
  981. }
  982. const viewer = this.viewer,
  983. pages = this._pages;
  984. viewer.textContent = "";
  985. if (this._spreadMode === _ui_utils.SpreadMode.NONE) {
  986. for (let i = 0, iMax = pages.length; i < iMax; ++i) {
  987. viewer.appendChild(pages[i].div);
  988. }
  989. } else {
  990. const parity = this._spreadMode - 1;
  991. let spread = null;
  992. for (let i = 0, iMax = pages.length; i < iMax; ++i) {
  993. if (spread === null) {
  994. spread = document.createElement("div");
  995. spread.className = "spread";
  996. viewer.appendChild(spread);
  997. } else if (i % 2 === parity) {
  998. spread = spread.cloneNode(false);
  999. viewer.appendChild(spread);
  1000. }
  1001. spread.appendChild(pages[i].div);
  1002. }
  1003. }
  1004. if (!pageNumber) {
  1005. return;
  1006. }
  1007. if (this._currentScaleValue && isNaN(this._currentScaleValue)) {
  1008. this._setScale(this._currentScaleValue, true);
  1009. }
  1010. this._setCurrentPageNumber(pageNumber, true);
  1011. this.update();
  1012. }
  1013. _getPageAdvance(currentPageNumber, previous = false) {
  1014. if (this.isInPresentationMode) {
  1015. return 1;
  1016. }
  1017. switch (this._scrollMode) {
  1018. case _ui_utils.ScrollMode.WRAPPED:
  1019. {
  1020. const {
  1021. views
  1022. } = this._getVisiblePages(),
  1023. pageLayout = new Map();
  1024. for (const {
  1025. id,
  1026. y,
  1027. percent,
  1028. widthPercent
  1029. } of views) {
  1030. if (percent === 0 || widthPercent < 100) {
  1031. continue;
  1032. }
  1033. let yArray = pageLayout.get(y);
  1034. if (!yArray) {
  1035. pageLayout.set(y, yArray || (yArray = []));
  1036. }
  1037. yArray.push(id);
  1038. }
  1039. for (const yArray of pageLayout.values()) {
  1040. const currentIndex = yArray.indexOf(currentPageNumber);
  1041. if (currentIndex === -1) {
  1042. continue;
  1043. }
  1044. const numPages = yArray.length;
  1045. if (numPages === 1) {
  1046. break;
  1047. }
  1048. if (previous) {
  1049. for (let i = currentIndex - 1, ii = 0; i >= ii; i--) {
  1050. const currentId = yArray[i],
  1051. expectedId = yArray[i + 1] - 1;
  1052. if (currentId < expectedId) {
  1053. return currentPageNumber - expectedId;
  1054. }
  1055. }
  1056. } else {
  1057. for (let i = currentIndex + 1, ii = numPages; i < ii; i++) {
  1058. const currentId = yArray[i],
  1059. expectedId = yArray[i - 1] + 1;
  1060. if (currentId > expectedId) {
  1061. return expectedId - currentPageNumber;
  1062. }
  1063. }
  1064. }
  1065. if (previous) {
  1066. const firstId = yArray[0];
  1067. if (firstId < currentPageNumber) {
  1068. return currentPageNumber - firstId + 1;
  1069. }
  1070. } else {
  1071. const lastId = yArray[numPages - 1];
  1072. if (lastId > currentPageNumber) {
  1073. return lastId - currentPageNumber + 1;
  1074. }
  1075. }
  1076. break;
  1077. }
  1078. break;
  1079. }
  1080. case _ui_utils.ScrollMode.HORIZONTAL:
  1081. {
  1082. break;
  1083. }
  1084. case _ui_utils.ScrollMode.VERTICAL:
  1085. {
  1086. if (this._spreadMode === _ui_utils.SpreadMode.NONE) {
  1087. break;
  1088. }
  1089. const parity = this._spreadMode - 1;
  1090. if (previous && currentPageNumber % 2 !== parity) {
  1091. break;
  1092. } else if (!previous && currentPageNumber % 2 === parity) {
  1093. break;
  1094. }
  1095. const {
  1096. views
  1097. } = this._getVisiblePages(),
  1098. expectedId = previous ? currentPageNumber - 1 : currentPageNumber + 1;
  1099. for (const {
  1100. id,
  1101. percent,
  1102. widthPercent
  1103. } of views) {
  1104. if (id !== expectedId) {
  1105. continue;
  1106. }
  1107. if (percent > 0 && widthPercent === 100) {
  1108. return 2;
  1109. }
  1110. break;
  1111. }
  1112. break;
  1113. }
  1114. }
  1115. return 1;
  1116. }
  1117. nextPage() {
  1118. const currentPageNumber = this._currentPageNumber,
  1119. pagesCount = this.pagesCount;
  1120. if (currentPageNumber >= pagesCount) {
  1121. return false;
  1122. }
  1123. const advance = this._getPageAdvance(currentPageNumber, false) || 1;
  1124. this.currentPageNumber = Math.min(currentPageNumber + advance, pagesCount);
  1125. return true;
  1126. }
  1127. previousPage() {
  1128. const currentPageNumber = this._currentPageNumber;
  1129. if (currentPageNumber <= 1) {
  1130. return false;
  1131. }
  1132. const advance = this._getPageAdvance(currentPageNumber, true) || 1;
  1133. this.currentPageNumber = Math.max(currentPageNumber - advance, 1);
  1134. return true;
  1135. }
  1136. }
  1137. exports.BaseViewer = BaseViewer;