base_viewer.js 46 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757
  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.PagesCountLimit = exports.PDFPageViewBuffer = exports.BaseViewer = void 0;
  27. var _pdf = require("../pdf");
  28. var _ui_utils = require("./ui_utils.js");
  29. var _annotation_layer_builder = require("./annotation_layer_builder.js");
  30. var _l10n_utils = require("./l10n_utils.js");
  31. var _pdf_page_view = require("./pdf_page_view.js");
  32. var _pdf_rendering_queue = require("./pdf_rendering_queue.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_highlighter = require("./text_highlighter.js");
  36. var _text_layer_builder = require("./text_layer_builder.js");
  37. var _xfa_layer_builder = require("./xfa_layer_builder.js");
  38. const DEFAULT_CACHE_SIZE = 10;
  39. const ENABLE_PERMISSIONS_CLASS = "enablePermissions";
  40. const PagesCountLimit = {
  41. FORCE_SCROLL_MODE_PAGE: 15000,
  42. FORCE_LAZY_PAGE_INIT: 7500,
  43. PAUSE_EAGER_PAGE_INIT: 500
  44. };
  45. exports.PagesCountLimit = PagesCountLimit;
  46. class PDFPageViewBuffer {
  47. #buf = new Set();
  48. #size = 0;
  49. constructor(size) {
  50. this.#size = size;
  51. }
  52. push(view) {
  53. const buf = this.#buf;
  54. if (buf.has(view)) {
  55. buf.delete(view);
  56. }
  57. buf.add(view);
  58. if (buf.size > this.#size) {
  59. this.#destroyFirstView();
  60. }
  61. }
  62. resize(newSize, idsToKeep = null) {
  63. this.#size = newSize;
  64. const buf = this.#buf;
  65. if (idsToKeep) {
  66. const ii = buf.size;
  67. let i = 1;
  68. for (const view of buf) {
  69. if (idsToKeep.has(view.id)) {
  70. buf.delete(view);
  71. buf.add(view);
  72. }
  73. if (++i > ii) {
  74. break;
  75. }
  76. }
  77. }
  78. while (buf.size > this.#size) {
  79. this.#destroyFirstView();
  80. }
  81. }
  82. has(view) {
  83. return this.#buf.has(view);
  84. }
  85. [Symbol.iterator]() {
  86. return this.#buf.keys();
  87. }
  88. #destroyFirstView() {
  89. const firstView = this.#buf.keys().next().value;
  90. firstView?.destroy();
  91. this.#buf.delete(firstView);
  92. }
  93. }
  94. exports.PDFPageViewBuffer = PDFPageViewBuffer;
  95. class BaseViewer {
  96. #buffer = null;
  97. #annotationMode = _pdf.AnnotationMode.ENABLE_FORMS;
  98. #previousAnnotationMode = null;
  99. #enablePermissions = false;
  100. #previousContainerHeight = 0;
  101. #scrollModePageState = null;
  102. constructor(options) {
  103. if (this.constructor === BaseViewer) {
  104. throw new Error("Cannot initialize BaseViewer.");
  105. }
  106. const viewerVersion = '2.12.313';
  107. if (_pdf.version !== viewerVersion) {
  108. throw new Error(`The API version "${_pdf.version}" does not match the Viewer version "${viewerVersion}".`);
  109. }
  110. this.container = options.container;
  111. this.viewer = options.viewer || options.container.firstElementChild;
  112. if (!(this.container?.tagName.toUpperCase() === "DIV" && this.viewer?.tagName.toUpperCase() === "DIV")) {
  113. throw new Error("Invalid `container` and/or `viewer` option.");
  114. }
  115. if (this.container.offsetParent && getComputedStyle(this.container).position !== "absolute") {
  116. throw new Error("The `container` must be absolutely positioned.");
  117. }
  118. this.eventBus = options.eventBus;
  119. this.linkService = options.linkService || new _pdf_link_service.SimpleLinkService();
  120. this.downloadManager = options.downloadManager || null;
  121. this.findController = options.findController || null;
  122. this._scriptingManager = options.scriptingManager || null;
  123. this.removePageBorders = options.removePageBorders || false;
  124. this.textLayerMode = options.textLayerMode ?? _ui_utils.TextLayerMode.ENABLE;
  125. this.#annotationMode = options.annotationMode ?? _pdf.AnnotationMode.ENABLE_FORMS;
  126. this.imageResourcesPath = options.imageResourcesPath || "";
  127. this.enablePrintAutoRotate = options.enablePrintAutoRotate || false;
  128. this.renderer = options.renderer || _ui_utils.RendererType.CANVAS;
  129. this.useOnlyCssZoom = options.useOnlyCssZoom || false;
  130. this.maxCanvasPixels = options.maxCanvasPixels;
  131. this.l10n = options.l10n || _l10n_utils.NullL10n;
  132. this.#enablePermissions = options.enablePermissions || false;
  133. this.defaultRenderingQueue = !options.renderingQueue;
  134. if (this.defaultRenderingQueue) {
  135. this.renderingQueue = new _pdf_rendering_queue.PDFRenderingQueue();
  136. this.renderingQueue.setViewer(this);
  137. } else {
  138. this.renderingQueue = options.renderingQueue;
  139. }
  140. this._doc = document.documentElement;
  141. this.scroll = (0, _ui_utils.watchScroll)(this.container, this._scrollUpdate.bind(this));
  142. this.presentationModeState = _ui_utils.PresentationModeState.UNKNOWN;
  143. this._onBeforeDraw = this._onAfterDraw = null;
  144. this._resetView();
  145. if (this.removePageBorders) {
  146. this.viewer.classList.add("removePageBorders");
  147. }
  148. Promise.resolve().then(() => {
  149. this.eventBus.dispatch("baseviewerinit", {
  150. source: this
  151. });
  152. });
  153. }
  154. get pagesCount() {
  155. return this._pages.length;
  156. }
  157. getPageView(index) {
  158. return this._pages[index];
  159. }
  160. get pageViewsReady() {
  161. if (!this._pagesCapability.settled) {
  162. return false;
  163. }
  164. return this._pages.every(function (pageView) {
  165. return pageView?.pdfPage;
  166. });
  167. }
  168. get renderForms() {
  169. return this.#annotationMode === _pdf.AnnotationMode.ENABLE_FORMS;
  170. }
  171. get enableScripting() {
  172. return !!this._scriptingManager;
  173. }
  174. get currentPageNumber() {
  175. return this._currentPageNumber;
  176. }
  177. set currentPageNumber(val) {
  178. if (!Number.isInteger(val)) {
  179. throw new Error("Invalid page number.");
  180. }
  181. if (!this.pdfDocument) {
  182. return;
  183. }
  184. if (!this._setCurrentPageNumber(val, true)) {
  185. console.error(`currentPageNumber: "${val}" is not a valid page.`);
  186. }
  187. }
  188. _setCurrentPageNumber(val, resetCurrentPageView = false) {
  189. if (this._currentPageNumber === val) {
  190. if (resetCurrentPageView) {
  191. this._resetCurrentPageView();
  192. }
  193. return true;
  194. }
  195. if (!(0 < val && val <= this.pagesCount)) {
  196. return false;
  197. }
  198. const previous = this._currentPageNumber;
  199. this._currentPageNumber = val;
  200. this.eventBus.dispatch("pagechanging", {
  201. source: this,
  202. pageNumber: val,
  203. pageLabel: this._pageLabels?.[val - 1] ?? null,
  204. previous
  205. });
  206. if (resetCurrentPageView) {
  207. this._resetCurrentPageView();
  208. }
  209. return true;
  210. }
  211. get currentPageLabel() {
  212. return this._pageLabels?.[this._currentPageNumber - 1] ?? null;
  213. }
  214. set currentPageLabel(val) {
  215. if (!this.pdfDocument) {
  216. return;
  217. }
  218. let page = val | 0;
  219. if (this._pageLabels) {
  220. const i = this._pageLabels.indexOf(val);
  221. if (i >= 0) {
  222. page = i + 1;
  223. }
  224. }
  225. if (!this._setCurrentPageNumber(page, true)) {
  226. console.error(`currentPageLabel: "${val}" is not a valid page.`);
  227. }
  228. }
  229. get currentScale() {
  230. return this._currentScale !== _ui_utils.UNKNOWN_SCALE ? this._currentScale : _ui_utils.DEFAULT_SCALE;
  231. }
  232. set currentScale(val) {
  233. if (isNaN(val)) {
  234. throw new Error("Invalid numeric scale.");
  235. }
  236. if (!this.pdfDocument) {
  237. return;
  238. }
  239. this._setScale(val, false);
  240. }
  241. get currentScaleValue() {
  242. return this._currentScaleValue;
  243. }
  244. set currentScaleValue(val) {
  245. if (!this.pdfDocument) {
  246. return;
  247. }
  248. this._setScale(val, false);
  249. }
  250. get pagesRotation() {
  251. return this._pagesRotation;
  252. }
  253. set pagesRotation(rotation) {
  254. if (!(0, _ui_utils.isValidRotation)(rotation)) {
  255. throw new Error("Invalid pages rotation angle.");
  256. }
  257. if (!this.pdfDocument) {
  258. return;
  259. }
  260. rotation %= 360;
  261. if (rotation < 0) {
  262. rotation += 360;
  263. }
  264. if (this._pagesRotation === rotation) {
  265. return;
  266. }
  267. this._pagesRotation = rotation;
  268. const pageNumber = this._currentPageNumber;
  269. const updateArgs = {
  270. rotation
  271. };
  272. for (const pageView of this._pages) {
  273. pageView.update(updateArgs);
  274. }
  275. if (this._currentScaleValue) {
  276. this._setScale(this._currentScaleValue, true);
  277. }
  278. this.eventBus.dispatch("rotationchanging", {
  279. source: this,
  280. pagesRotation: rotation,
  281. pageNumber
  282. });
  283. if (this.defaultRenderingQueue) {
  284. this.update();
  285. }
  286. }
  287. get firstPagePromise() {
  288. return this.pdfDocument ? this._firstPageCapability.promise : null;
  289. }
  290. get onePageRendered() {
  291. return this.pdfDocument ? this._onePageRenderedCapability.promise : null;
  292. }
  293. get pagesPromise() {
  294. return this.pdfDocument ? this._pagesCapability.promise : null;
  295. }
  296. #initializePermissions(permissions) {
  297. if (!permissions) {
  298. return;
  299. }
  300. if (!permissions.includes(_pdf.PermissionFlag.COPY)) {
  301. this.viewer.classList.add(ENABLE_PERMISSIONS_CLASS);
  302. }
  303. if (!permissions.includes(_pdf.PermissionFlag.MODIFY_ANNOTATIONS) && !permissions.includes(_pdf.PermissionFlag.FILL_INTERACTIVE_FORMS)) {
  304. if (this.#annotationMode === _pdf.AnnotationMode.ENABLE_FORMS) {
  305. this.#previousAnnotationMode = this.#annotationMode;
  306. this.#annotationMode = _pdf.AnnotationMode.ENABLE;
  307. }
  308. }
  309. }
  310. #onePageRenderedOrForceFetch() {
  311. if (!this.container.offsetParent || this._getVisiblePages().views.length === 0) {
  312. return Promise.resolve();
  313. }
  314. return this._onePageRenderedCapability.promise;
  315. }
  316. setDocument(pdfDocument) {
  317. if (this.pdfDocument) {
  318. this.eventBus.dispatch("pagesdestroy", {
  319. source: this
  320. });
  321. this._cancelRendering();
  322. this._resetView();
  323. if (this.findController) {
  324. this.findController.setDocument(null);
  325. }
  326. if (this._scriptingManager) {
  327. this._scriptingManager.setDocument(null);
  328. }
  329. }
  330. this.pdfDocument = pdfDocument;
  331. if (!pdfDocument) {
  332. return;
  333. }
  334. const isPureXfa = pdfDocument.isPureXfa;
  335. const pagesCount = pdfDocument.numPages;
  336. const firstPagePromise = pdfDocument.getPage(1);
  337. const optionalContentConfigPromise = pdfDocument.getOptionalContentConfig();
  338. const permissionsPromise = this.#enablePermissions ? pdfDocument.getPermissions() : Promise.resolve();
  339. if (pagesCount > PagesCountLimit.FORCE_SCROLL_MODE_PAGE) {
  340. console.warn("Forcing PAGE-scrolling for performance reasons, given the length of the document.");
  341. const mode = this._scrollMode = _ui_utils.ScrollMode.PAGE;
  342. this.eventBus.dispatch("scrollmodechanged", {
  343. source: this,
  344. mode
  345. });
  346. }
  347. this._pagesCapability.promise.then(() => {
  348. this.eventBus.dispatch("pagesloaded", {
  349. source: this,
  350. pagesCount
  351. });
  352. }, () => {});
  353. this._onBeforeDraw = evt => {
  354. const pageView = this._pages[evt.pageNumber - 1];
  355. if (!pageView) {
  356. return;
  357. }
  358. this.#buffer.push(pageView);
  359. };
  360. this.eventBus._on("pagerender", this._onBeforeDraw);
  361. this._onAfterDraw = evt => {
  362. if (evt.cssTransform || this._onePageRenderedCapability.settled) {
  363. return;
  364. }
  365. this._onePageRenderedCapability.resolve({
  366. timestamp: evt.timestamp
  367. });
  368. this.eventBus._off("pagerendered", this._onAfterDraw);
  369. this._onAfterDraw = null;
  370. };
  371. this.eventBus._on("pagerendered", this._onAfterDraw);
  372. Promise.all([firstPagePromise, permissionsPromise]).then(([firstPdfPage, permissions]) => {
  373. if (pdfDocument !== this.pdfDocument) {
  374. return;
  375. }
  376. this._firstPageCapability.resolve(firstPdfPage);
  377. this._optionalContentConfigPromise = optionalContentConfigPromise;
  378. this.#initializePermissions(permissions);
  379. const viewerElement = this._scrollMode === _ui_utils.ScrollMode.PAGE ? null : this.viewer;
  380. const scale = this.currentScale;
  381. const viewport = firstPdfPage.getViewport({
  382. scale: scale * _pdf.PixelsPerInch.PDF_TO_CSS_UNITS
  383. });
  384. const textLayerFactory = this.textLayerMode !== _ui_utils.TextLayerMode.DISABLE && !isPureXfa ? this : null;
  385. const annotationLayerFactory = this.#annotationMode !== _pdf.AnnotationMode.DISABLE ? this : null;
  386. const xfaLayerFactory = isPureXfa ? this : null;
  387. for (let pageNum = 1; pageNum <= pagesCount; ++pageNum) {
  388. const pageView = new _pdf_page_view.PDFPageView({
  389. container: viewerElement,
  390. eventBus: this.eventBus,
  391. id: pageNum,
  392. scale,
  393. defaultViewport: viewport.clone(),
  394. optionalContentConfigPromise,
  395. renderingQueue: this.renderingQueue,
  396. textLayerFactory,
  397. textLayerMode: this.textLayerMode,
  398. annotationLayerFactory,
  399. annotationMode: this.#annotationMode,
  400. xfaLayerFactory,
  401. textHighlighterFactory: this,
  402. structTreeLayerFactory: this,
  403. imageResourcesPath: this.imageResourcesPath,
  404. renderer: this.renderer,
  405. useOnlyCssZoom: this.useOnlyCssZoom,
  406. maxCanvasPixels: this.maxCanvasPixels,
  407. l10n: this.l10n
  408. });
  409. this._pages.push(pageView);
  410. }
  411. const firstPageView = this._pages[0];
  412. if (firstPageView) {
  413. firstPageView.setPdfPage(firstPdfPage);
  414. this.linkService.cachePageRef(1, firstPdfPage.ref);
  415. }
  416. if (this._scrollMode === _ui_utils.ScrollMode.PAGE) {
  417. this.#ensurePageViewVisible();
  418. } else if (this._spreadMode !== _ui_utils.SpreadMode.NONE) {
  419. this._updateSpreadMode();
  420. }
  421. this.#onePageRenderedOrForceFetch().then(async () => {
  422. if (this.findController) {
  423. this.findController.setDocument(pdfDocument);
  424. }
  425. if (this._scriptingManager) {
  426. this._scriptingManager.setDocument(pdfDocument);
  427. }
  428. if (pdfDocument.loadingParams.disableAutoFetch || pagesCount > PagesCountLimit.FORCE_LAZY_PAGE_INIT) {
  429. this._pagesCapability.resolve();
  430. return;
  431. }
  432. let getPagesLeft = pagesCount - 1;
  433. if (getPagesLeft <= 0) {
  434. this._pagesCapability.resolve();
  435. return;
  436. }
  437. for (let pageNum = 2; pageNum <= pagesCount; ++pageNum) {
  438. const promise = pdfDocument.getPage(pageNum).then(pdfPage => {
  439. const pageView = this._pages[pageNum - 1];
  440. if (!pageView.pdfPage) {
  441. pageView.setPdfPage(pdfPage);
  442. }
  443. this.linkService.cachePageRef(pageNum, pdfPage.ref);
  444. if (--getPagesLeft === 0) {
  445. this._pagesCapability.resolve();
  446. }
  447. }, reason => {
  448. console.error(`Unable to get page ${pageNum} to initialize viewer`, reason);
  449. if (--getPagesLeft === 0) {
  450. this._pagesCapability.resolve();
  451. }
  452. });
  453. if (pageNum % PagesCountLimit.PAUSE_EAGER_PAGE_INIT === 0) {
  454. await promise;
  455. }
  456. }
  457. });
  458. this.eventBus.dispatch("pagesinit", {
  459. source: this
  460. });
  461. pdfDocument.getMetadata().then(({
  462. info
  463. }) => {
  464. if (pdfDocument !== this.pdfDocument) {
  465. return;
  466. }
  467. if (info.Language) {
  468. this.viewer.lang = info.Language;
  469. }
  470. });
  471. if (this.defaultRenderingQueue) {
  472. this.update();
  473. }
  474. }).catch(reason => {
  475. console.error("Unable to initialize viewer", reason);
  476. this._pagesCapability.reject(reason);
  477. });
  478. }
  479. setPageLabels(labels) {
  480. if (!this.pdfDocument) {
  481. return;
  482. }
  483. if (!labels) {
  484. this._pageLabels = null;
  485. } else if (!(Array.isArray(labels) && this.pdfDocument.numPages === labels.length)) {
  486. this._pageLabels = null;
  487. console.error(`setPageLabels: Invalid page labels.`);
  488. } else {
  489. this._pageLabels = labels;
  490. }
  491. for (let i = 0, ii = this._pages.length; i < ii; i++) {
  492. this._pages[i].setPageLabel(this._pageLabels?.[i] ?? null);
  493. }
  494. }
  495. _resetView() {
  496. this._pages = [];
  497. this._currentPageNumber = 1;
  498. this._currentScale = _ui_utils.UNKNOWN_SCALE;
  499. this._currentScaleValue = null;
  500. this._pageLabels = null;
  501. this.#buffer = new PDFPageViewBuffer(DEFAULT_CACHE_SIZE);
  502. this._location = null;
  503. this._pagesRotation = 0;
  504. this._optionalContentConfigPromise = null;
  505. this._firstPageCapability = (0, _pdf.createPromiseCapability)();
  506. this._onePageRenderedCapability = (0, _pdf.createPromiseCapability)();
  507. this._pagesCapability = (0, _pdf.createPromiseCapability)();
  508. this._scrollMode = _ui_utils.ScrollMode.VERTICAL;
  509. this._previousScrollMode = _ui_utils.ScrollMode.UNKNOWN;
  510. this._spreadMode = _ui_utils.SpreadMode.NONE;
  511. this.#scrollModePageState = {
  512. previousPageNumber: 1,
  513. scrollDown: true,
  514. pages: []
  515. };
  516. if (this._onBeforeDraw) {
  517. this.eventBus._off("pagerender", this._onBeforeDraw);
  518. this._onBeforeDraw = null;
  519. }
  520. if (this._onAfterDraw) {
  521. this.eventBus._off("pagerendered", this._onAfterDraw);
  522. this._onAfterDraw = null;
  523. }
  524. this.viewer.textContent = "";
  525. this._updateScrollMode();
  526. this.viewer.removeAttribute("lang");
  527. this.viewer.classList.remove(ENABLE_PERMISSIONS_CLASS);
  528. if (this.#previousAnnotationMode !== null) {
  529. this.#annotationMode = this.#previousAnnotationMode;
  530. this.#previousAnnotationMode = null;
  531. }
  532. }
  533. #ensurePageViewVisible() {
  534. if (this._scrollMode !== _ui_utils.ScrollMode.PAGE) {
  535. throw new Error("#ensurePageViewVisible: Invalid scrollMode value.");
  536. }
  537. const pageNumber = this._currentPageNumber,
  538. state = this.#scrollModePageState,
  539. viewer = this.viewer;
  540. viewer.textContent = "";
  541. state.pages.length = 0;
  542. if (this._spreadMode === _ui_utils.SpreadMode.NONE) {
  543. const pageView = this._pages[pageNumber - 1];
  544. if (this.isInPresentationMode) {
  545. const spread = document.createElement("div");
  546. spread.className = "spread";
  547. const dummyPage = document.createElement("div");
  548. dummyPage.className = "dummyPage";
  549. dummyPage.style.height = `${this.container.clientHeight}px`;
  550. spread.appendChild(dummyPage);
  551. spread.appendChild(pageView.div);
  552. viewer.appendChild(spread);
  553. } else {
  554. viewer.appendChild(pageView.div);
  555. }
  556. state.pages.push(pageView);
  557. } else {
  558. const pageIndexSet = new Set(),
  559. parity = this._spreadMode - 1;
  560. if (pageNumber % 2 !== parity) {
  561. pageIndexSet.add(pageNumber - 1);
  562. pageIndexSet.add(pageNumber);
  563. } else {
  564. pageIndexSet.add(pageNumber - 2);
  565. pageIndexSet.add(pageNumber - 1);
  566. }
  567. let spread = null;
  568. for (const i of pageIndexSet) {
  569. const pageView = this._pages[i];
  570. if (!pageView) {
  571. continue;
  572. }
  573. if (spread === null) {
  574. spread = document.createElement("div");
  575. spread.className = "spread";
  576. viewer.appendChild(spread);
  577. } else if (i % 2 === parity) {
  578. spread = spread.cloneNode(false);
  579. viewer.appendChild(spread);
  580. }
  581. spread.appendChild(pageView.div);
  582. state.pages.push(pageView);
  583. }
  584. }
  585. state.scrollDown = pageNumber >= state.previousPageNumber;
  586. state.previousPageNumber = pageNumber;
  587. }
  588. _scrollUpdate() {
  589. if (this.pagesCount === 0) {
  590. return;
  591. }
  592. this.update();
  593. }
  594. _scrollIntoView({
  595. pageDiv,
  596. pageSpot = null,
  597. pageNumber = null
  598. }) {
  599. if (this._scrollMode === _ui_utils.ScrollMode.PAGE) {
  600. if (pageNumber) {
  601. this._setCurrentPageNumber(pageNumber);
  602. }
  603. this.#ensurePageViewVisible();
  604. this.update();
  605. }
  606. if (!pageSpot && !this.isInPresentationMode) {
  607. const left = pageDiv.offsetLeft + pageDiv.clientLeft;
  608. const right = left + pageDiv.clientWidth;
  609. const {
  610. scrollLeft,
  611. clientWidth
  612. } = this.container;
  613. if (this._scrollMode === _ui_utils.ScrollMode.HORIZONTAL || left < scrollLeft || right > scrollLeft + clientWidth) {
  614. pageSpot = {
  615. left: 0,
  616. top: 0
  617. };
  618. }
  619. }
  620. (0, _ui_utils.scrollIntoView)(pageDiv, pageSpot);
  621. }
  622. #isSameScale(newScale) {
  623. if (this.isInPresentationMode && this.container.clientHeight !== this.#previousContainerHeight) {
  624. return false;
  625. }
  626. return newScale === this._currentScale || Math.abs(newScale - this._currentScale) < 1e-15;
  627. }
  628. _setScaleUpdatePages(newScale, newValue, noScroll = false, preset = false) {
  629. this._currentScaleValue = newValue.toString();
  630. if (this.#isSameScale(newScale)) {
  631. if (preset) {
  632. this.eventBus.dispatch("scalechanging", {
  633. source: this,
  634. scale: newScale,
  635. presetValue: newValue
  636. });
  637. }
  638. return;
  639. }
  640. this._doc.style.setProperty("--zoom-factor", newScale);
  641. this._doc.style.setProperty("--viewport-scale-factor", newScale * _pdf.PixelsPerInch.PDF_TO_CSS_UNITS);
  642. const updateArgs = {
  643. scale: newScale
  644. };
  645. for (const pageView of this._pages) {
  646. pageView.update(updateArgs);
  647. }
  648. this._currentScale = newScale;
  649. if (!noScroll) {
  650. let page = this._currentPageNumber,
  651. dest;
  652. if (this._location && !(this.isInPresentationMode || this.isChangingPresentationMode)) {
  653. page = this._location.pageNumber;
  654. dest = [null, {
  655. name: "XYZ"
  656. }, this._location.left, this._location.top, null];
  657. }
  658. this.scrollPageIntoView({
  659. pageNumber: page,
  660. destArray: dest,
  661. allowNegativeOffset: true
  662. });
  663. }
  664. this.eventBus.dispatch("scalechanging", {
  665. source: this,
  666. scale: newScale,
  667. presetValue: preset ? newValue : undefined
  668. });
  669. if (this.defaultRenderingQueue) {
  670. this.update();
  671. }
  672. this.#previousContainerHeight = this.container.clientHeight;
  673. }
  674. get _pageWidthScaleFactor() {
  675. if (this._spreadMode !== _ui_utils.SpreadMode.NONE && this._scrollMode !== _ui_utils.ScrollMode.HORIZONTAL) {
  676. return 2;
  677. }
  678. return 1;
  679. }
  680. _setScale(value, noScroll = false) {
  681. let scale = parseFloat(value);
  682. if (scale > 0) {
  683. this._setScaleUpdatePages(scale, value, noScroll, false);
  684. } else {
  685. const currentPage = this._pages[this._currentPageNumber - 1];
  686. if (!currentPage) {
  687. return;
  688. }
  689. let hPadding = _ui_utils.SCROLLBAR_PADDING,
  690. vPadding = _ui_utils.VERTICAL_PADDING;
  691. if (this.isInPresentationMode) {
  692. hPadding = vPadding = 4;
  693. } else if (this.removePageBorders) {
  694. hPadding = vPadding = 0;
  695. }
  696. if (this._scrollMode === _ui_utils.ScrollMode.HORIZONTAL) {
  697. [hPadding, vPadding] = [vPadding, hPadding];
  698. }
  699. const pageWidthScale = (this.container.clientWidth - hPadding) / currentPage.width * currentPage.scale / this._pageWidthScaleFactor;
  700. const pageHeightScale = (this.container.clientHeight - vPadding) / currentPage.height * currentPage.scale;
  701. switch (value) {
  702. case "page-actual":
  703. scale = 1;
  704. break;
  705. case "page-width":
  706. scale = pageWidthScale;
  707. break;
  708. case "page-height":
  709. scale = pageHeightScale;
  710. break;
  711. case "page-fit":
  712. scale = Math.min(pageWidthScale, pageHeightScale);
  713. break;
  714. case "auto":
  715. const horizontalScale = (0, _ui_utils.isPortraitOrientation)(currentPage) ? pageWidthScale : Math.min(pageHeightScale, pageWidthScale);
  716. scale = Math.min(_ui_utils.MAX_AUTO_SCALE, horizontalScale);
  717. break;
  718. default:
  719. console.error(`_setScale: "${value}" is an unknown zoom value.`);
  720. return;
  721. }
  722. this._setScaleUpdatePages(scale, value, noScroll, true);
  723. }
  724. }
  725. _resetCurrentPageView() {
  726. if (this.isInPresentationMode) {
  727. this._setScale(this._currentScaleValue, true);
  728. }
  729. const pageView = this._pages[this._currentPageNumber - 1];
  730. this._scrollIntoView({
  731. pageDiv: pageView.div
  732. });
  733. }
  734. pageLabelToPageNumber(label) {
  735. if (!this._pageLabels) {
  736. return null;
  737. }
  738. const i = this._pageLabels.indexOf(label);
  739. if (i < 0) {
  740. return null;
  741. }
  742. return i + 1;
  743. }
  744. scrollPageIntoView({
  745. pageNumber,
  746. destArray = null,
  747. allowNegativeOffset = false,
  748. ignoreDestinationZoom = false
  749. }) {
  750. if (!this.pdfDocument) {
  751. return;
  752. }
  753. const pageView = Number.isInteger(pageNumber) && this._pages[pageNumber - 1];
  754. if (!pageView) {
  755. console.error(`scrollPageIntoView: "${pageNumber}" is not a valid pageNumber parameter.`);
  756. return;
  757. }
  758. if (this.isInPresentationMode || !destArray) {
  759. this._setCurrentPageNumber(pageNumber, true);
  760. return;
  761. }
  762. let x = 0,
  763. y = 0;
  764. let width = 0,
  765. height = 0,
  766. widthScale,
  767. heightScale;
  768. const changeOrientation = pageView.rotation % 180 !== 0;
  769. const pageWidth = (changeOrientation ? pageView.height : pageView.width) / pageView.scale / _pdf.PixelsPerInch.PDF_TO_CSS_UNITS;
  770. const pageHeight = (changeOrientation ? pageView.width : pageView.height) / pageView.scale / _pdf.PixelsPerInch.PDF_TO_CSS_UNITS;
  771. let scale = 0;
  772. switch (destArray[1].name) {
  773. case "XYZ":
  774. x = destArray[2];
  775. y = destArray[3];
  776. scale = destArray[4];
  777. x = x !== null ? x : 0;
  778. y = y !== null ? y : pageHeight;
  779. break;
  780. case "Fit":
  781. case "FitB":
  782. scale = "page-fit";
  783. break;
  784. case "FitH":
  785. case "FitBH":
  786. y = destArray[2];
  787. scale = "page-width";
  788. if (y === null && this._location) {
  789. x = this._location.left;
  790. y = this._location.top;
  791. } else if (typeof y !== "number" || y < 0) {
  792. y = pageHeight;
  793. }
  794. break;
  795. case "FitV":
  796. case "FitBV":
  797. x = destArray[2];
  798. width = pageWidth;
  799. height = pageHeight;
  800. scale = "page-height";
  801. break;
  802. case "FitR":
  803. x = destArray[2];
  804. y = destArray[3];
  805. width = destArray[4] - x;
  806. height = destArray[5] - y;
  807. const hPadding = this.removePageBorders ? 0 : _ui_utils.SCROLLBAR_PADDING;
  808. const vPadding = this.removePageBorders ? 0 : _ui_utils.VERTICAL_PADDING;
  809. widthScale = (this.container.clientWidth - hPadding) / width / _pdf.PixelsPerInch.PDF_TO_CSS_UNITS;
  810. heightScale = (this.container.clientHeight - vPadding) / height / _pdf.PixelsPerInch.PDF_TO_CSS_UNITS;
  811. scale = Math.min(Math.abs(widthScale), Math.abs(heightScale));
  812. break;
  813. default:
  814. console.error(`scrollPageIntoView: "${destArray[1].name}" is not a valid destination type.`);
  815. return;
  816. }
  817. if (!ignoreDestinationZoom) {
  818. if (scale && scale !== this._currentScale) {
  819. this.currentScaleValue = scale;
  820. } else if (this._currentScale === _ui_utils.UNKNOWN_SCALE) {
  821. this.currentScaleValue = _ui_utils.DEFAULT_SCALE_VALUE;
  822. }
  823. }
  824. if (scale === "page-fit" && !destArray[4]) {
  825. this._scrollIntoView({
  826. pageDiv: pageView.div,
  827. pageNumber
  828. });
  829. return;
  830. }
  831. const boundingRect = [pageView.viewport.convertToViewportPoint(x, y), pageView.viewport.convertToViewportPoint(x + width, y + height)];
  832. let left = Math.min(boundingRect[0][0], boundingRect[1][0]);
  833. let top = Math.min(boundingRect[0][1], boundingRect[1][1]);
  834. if (!allowNegativeOffset) {
  835. left = Math.max(left, 0);
  836. top = Math.max(top, 0);
  837. }
  838. this._scrollIntoView({
  839. pageDiv: pageView.div,
  840. pageSpot: {
  841. left,
  842. top
  843. },
  844. pageNumber
  845. });
  846. }
  847. _updateLocation(firstPage) {
  848. const currentScale = this._currentScale;
  849. const currentScaleValue = this._currentScaleValue;
  850. const normalizedScaleValue = parseFloat(currentScaleValue) === currentScale ? Math.round(currentScale * 10000) / 100 : currentScaleValue;
  851. const pageNumber = firstPage.id;
  852. let pdfOpenParams = "#page=" + pageNumber;
  853. pdfOpenParams += "&zoom=" + normalizedScaleValue;
  854. const currentPageView = this._pages[pageNumber - 1];
  855. const container = this.container;
  856. const topLeft = currentPageView.getPagePoint(container.scrollLeft - firstPage.x, container.scrollTop - firstPage.y);
  857. const intLeft = Math.round(topLeft[0]);
  858. const intTop = Math.round(topLeft[1]);
  859. pdfOpenParams += "," + intLeft + "," + intTop;
  860. this._location = {
  861. pageNumber,
  862. scale: normalizedScaleValue,
  863. top: intTop,
  864. left: intLeft,
  865. rotation: this._pagesRotation,
  866. pdfOpenParams
  867. };
  868. }
  869. update() {
  870. const visible = this._getVisiblePages();
  871. const visiblePages = visible.views,
  872. numVisiblePages = visiblePages.length;
  873. if (numVisiblePages === 0) {
  874. return;
  875. }
  876. const newCacheSize = Math.max(DEFAULT_CACHE_SIZE, 2 * numVisiblePages + 1);
  877. this.#buffer.resize(newCacheSize, visible.ids);
  878. this.renderingQueue.renderHighestPriority(visible);
  879. if (!this.isInPresentationMode) {
  880. const isSimpleLayout = this._spreadMode === _ui_utils.SpreadMode.NONE && (this._scrollMode === _ui_utils.ScrollMode.PAGE || this._scrollMode === _ui_utils.ScrollMode.VERTICAL);
  881. let currentId = this._currentPageNumber;
  882. let stillFullyVisible = false;
  883. for (const page of visiblePages) {
  884. if (page.percent < 100) {
  885. break;
  886. }
  887. if (page.id === currentId && isSimpleLayout) {
  888. stillFullyVisible = true;
  889. break;
  890. }
  891. }
  892. if (!stillFullyVisible) {
  893. currentId = visiblePages[0].id;
  894. }
  895. this._setCurrentPageNumber(currentId);
  896. }
  897. this._updateLocation(visible.first);
  898. this.eventBus.dispatch("updateviewarea", {
  899. source: this,
  900. location: this._location
  901. });
  902. }
  903. containsElement(element) {
  904. return this.container.contains(element);
  905. }
  906. focus() {
  907. this.container.focus();
  908. }
  909. get _isContainerRtl() {
  910. return getComputedStyle(this.container).direction === "rtl";
  911. }
  912. get isInPresentationMode() {
  913. return this.presentationModeState === _ui_utils.PresentationModeState.FULLSCREEN;
  914. }
  915. get isChangingPresentationMode() {
  916. return this.presentationModeState === _ui_utils.PresentationModeState.CHANGING;
  917. }
  918. get isHorizontalScrollbarEnabled() {
  919. return this.isInPresentationMode ? false : this.container.scrollWidth > this.container.clientWidth;
  920. }
  921. get isVerticalScrollbarEnabled() {
  922. return this.isInPresentationMode ? false : this.container.scrollHeight > this.container.clientHeight;
  923. }
  924. _getCurrentVisiblePage() {
  925. if (!this.pagesCount) {
  926. return {
  927. views: []
  928. };
  929. }
  930. const pageView = this._pages[this._currentPageNumber - 1];
  931. const element = pageView.div;
  932. const view = {
  933. id: pageView.id,
  934. x: element.offsetLeft + element.clientLeft,
  935. y: element.offsetTop + element.clientTop,
  936. view: pageView
  937. };
  938. const ids = new Set([pageView.id]);
  939. return {
  940. first: view,
  941. last: view,
  942. views: [view],
  943. ids
  944. };
  945. }
  946. _getVisiblePages() {
  947. if (this.isInPresentationMode) {
  948. return this._getCurrentVisiblePage();
  949. }
  950. const views = this._scrollMode === _ui_utils.ScrollMode.PAGE ? this.#scrollModePageState.pages : this._pages,
  951. horizontal = this._scrollMode === _ui_utils.ScrollMode.HORIZONTAL,
  952. rtl = horizontal && this._isContainerRtl;
  953. return (0, _ui_utils.getVisibleElements)({
  954. scrollEl: this.container,
  955. views,
  956. sortByVisibility: true,
  957. horizontal,
  958. rtl
  959. });
  960. }
  961. isPageVisible(pageNumber) {
  962. if (!this.pdfDocument) {
  963. return false;
  964. }
  965. if (!(Number.isInteger(pageNumber) && pageNumber > 0 && pageNumber <= this.pagesCount)) {
  966. console.error(`isPageVisible: "${pageNumber}" is not a valid page.`);
  967. return false;
  968. }
  969. return this._getVisiblePages().ids.has(pageNumber);
  970. }
  971. isPageCached(pageNumber) {
  972. if (!this.pdfDocument) {
  973. return false;
  974. }
  975. if (!(Number.isInteger(pageNumber) && pageNumber > 0 && pageNumber <= this.pagesCount)) {
  976. console.error(`isPageCached: "${pageNumber}" is not a valid page.`);
  977. return false;
  978. }
  979. const pageView = this._pages[pageNumber - 1];
  980. return this.#buffer.has(pageView);
  981. }
  982. cleanup() {
  983. for (let i = 0, ii = this._pages.length; i < ii; i++) {
  984. if (this._pages[i] && this._pages[i].renderingState !== _ui_utils.RenderingStates.FINISHED) {
  985. this._pages[i].reset();
  986. }
  987. }
  988. }
  989. _cancelRendering() {
  990. for (let i = 0, ii = this._pages.length; i < ii; i++) {
  991. if (this._pages[i]) {
  992. this._pages[i].cancelRendering();
  993. }
  994. }
  995. }
  996. async #ensurePdfPageLoaded(pageView) {
  997. if (pageView.pdfPage) {
  998. return pageView.pdfPage;
  999. }
  1000. try {
  1001. const pdfPage = await this.pdfDocument.getPage(pageView.id);
  1002. if (!pageView.pdfPage) {
  1003. pageView.setPdfPage(pdfPage);
  1004. }
  1005. if (!this.linkService._cachedPageNumber(pdfPage.ref)) {
  1006. this.linkService.cachePageRef(pageView.id, pdfPage.ref);
  1007. }
  1008. return pdfPage;
  1009. } catch (reason) {
  1010. console.error("Unable to get page for page view", reason);
  1011. return null;
  1012. }
  1013. }
  1014. #getScrollAhead(visible) {
  1015. if (visible.first?.id === 1) {
  1016. return true;
  1017. } else if (visible.last?.id === this.pagesCount) {
  1018. return false;
  1019. }
  1020. switch (this._scrollMode) {
  1021. case _ui_utils.ScrollMode.PAGE:
  1022. return this.#scrollModePageState.scrollDown;
  1023. case _ui_utils.ScrollMode.HORIZONTAL:
  1024. return this.scroll.right;
  1025. }
  1026. return this.scroll.down;
  1027. }
  1028. #toggleLoadingIconSpinner(visibleIds) {
  1029. for (const id of visibleIds) {
  1030. const pageView = this._pages[id - 1];
  1031. pageView?.toggleLoadingIconSpinner(true);
  1032. }
  1033. for (const pageView of this.#buffer) {
  1034. if (visibleIds.has(pageView.id)) {
  1035. continue;
  1036. }
  1037. pageView.toggleLoadingIconSpinner(false);
  1038. }
  1039. }
  1040. forceRendering(currentlyVisiblePages) {
  1041. const visiblePages = currentlyVisiblePages || this._getVisiblePages();
  1042. const scrollAhead = this.#getScrollAhead(visiblePages);
  1043. const preRenderExtra = this._spreadMode !== _ui_utils.SpreadMode.NONE && this._scrollMode !== _ui_utils.ScrollMode.HORIZONTAL;
  1044. const pageView = this.renderingQueue.getHighestPriority(visiblePages, this._pages, scrollAhead, preRenderExtra);
  1045. this.#toggleLoadingIconSpinner(visiblePages.ids);
  1046. if (pageView) {
  1047. this.#ensurePdfPageLoaded(pageView).then(() => {
  1048. this.renderingQueue.renderView(pageView);
  1049. });
  1050. return true;
  1051. }
  1052. return false;
  1053. }
  1054. createTextLayerBuilder(textLayerDiv, pageIndex, viewport, enhanceTextSelection = false, eventBus, highlighter) {
  1055. return new _text_layer_builder.TextLayerBuilder({
  1056. textLayerDiv,
  1057. eventBus,
  1058. pageIndex,
  1059. viewport,
  1060. enhanceTextSelection: this.isInPresentationMode ? false : enhanceTextSelection,
  1061. highlighter
  1062. });
  1063. }
  1064. createTextHighlighter(pageIndex, eventBus) {
  1065. return new _text_highlighter.TextHighlighter({
  1066. eventBus,
  1067. pageIndex,
  1068. findController: this.isInPresentationMode ? null : this.findController
  1069. });
  1070. }
  1071. createAnnotationLayerBuilder(pageDiv, pdfPage, annotationStorage = null, imageResourcesPath = "", renderForms = true, l10n = _l10n_utils.NullL10n, enableScripting = null, hasJSActionsPromise = null, mouseState = null, fieldObjectsPromise = null, annotationCanvasMap = null) {
  1072. return new _annotation_layer_builder.AnnotationLayerBuilder({
  1073. pageDiv,
  1074. pdfPage,
  1075. annotationStorage: annotationStorage || this.pdfDocument?.annotationStorage,
  1076. imageResourcesPath,
  1077. renderForms,
  1078. linkService: this.linkService,
  1079. downloadManager: this.downloadManager,
  1080. l10n,
  1081. enableScripting: enableScripting ?? this.enableScripting,
  1082. hasJSActionsPromise: hasJSActionsPromise || this.pdfDocument?.hasJSActions(),
  1083. fieldObjectsPromise: fieldObjectsPromise || this.pdfDocument?.getFieldObjects(),
  1084. mouseState: mouseState || this._scriptingManager?.mouseState,
  1085. annotationCanvasMap
  1086. });
  1087. }
  1088. createXfaLayerBuilder(pageDiv, pdfPage, annotationStorage = null) {
  1089. return new _xfa_layer_builder.XfaLayerBuilder({
  1090. pageDiv,
  1091. pdfPage,
  1092. annotationStorage: annotationStorage || this.pdfDocument?.annotationStorage,
  1093. linkService: this.linkService
  1094. });
  1095. }
  1096. createStructTreeLayerBuilder(pdfPage) {
  1097. return new _struct_tree_layer_builder.StructTreeLayerBuilder({
  1098. pdfPage
  1099. });
  1100. }
  1101. get hasEqualPageSizes() {
  1102. const firstPageView = this._pages[0];
  1103. for (let i = 1, ii = this._pages.length; i < ii; ++i) {
  1104. const pageView = this._pages[i];
  1105. if (pageView.width !== firstPageView.width || pageView.height !== firstPageView.height) {
  1106. return false;
  1107. }
  1108. }
  1109. return true;
  1110. }
  1111. getPagesOverview() {
  1112. return this._pages.map(pageView => {
  1113. const viewport = pageView.pdfPage.getViewport({
  1114. scale: 1
  1115. });
  1116. if (!this.enablePrintAutoRotate || (0, _ui_utils.isPortraitOrientation)(viewport)) {
  1117. return {
  1118. width: viewport.width,
  1119. height: viewport.height,
  1120. rotation: viewport.rotation
  1121. };
  1122. }
  1123. return {
  1124. width: viewport.height,
  1125. height: viewport.width,
  1126. rotation: (viewport.rotation - 90) % 360
  1127. };
  1128. });
  1129. }
  1130. get optionalContentConfigPromise() {
  1131. if (!this.pdfDocument) {
  1132. return Promise.resolve(null);
  1133. }
  1134. if (!this._optionalContentConfigPromise) {
  1135. return this.pdfDocument.getOptionalContentConfig();
  1136. }
  1137. return this._optionalContentConfigPromise;
  1138. }
  1139. set optionalContentConfigPromise(promise) {
  1140. if (!(promise instanceof Promise)) {
  1141. throw new Error(`Invalid optionalContentConfigPromise: ${promise}`);
  1142. }
  1143. if (!this.pdfDocument) {
  1144. return;
  1145. }
  1146. if (!this._optionalContentConfigPromise) {
  1147. return;
  1148. }
  1149. this._optionalContentConfigPromise = promise;
  1150. const updateArgs = {
  1151. optionalContentConfigPromise: promise
  1152. };
  1153. for (const pageView of this._pages) {
  1154. pageView.update(updateArgs);
  1155. }
  1156. this.update();
  1157. this.eventBus.dispatch("optionalcontentconfigchanged", {
  1158. source: this,
  1159. promise
  1160. });
  1161. }
  1162. get scrollMode() {
  1163. return this._scrollMode;
  1164. }
  1165. set scrollMode(mode) {
  1166. if (this._scrollMode === mode) {
  1167. return;
  1168. }
  1169. if (!(0, _ui_utils.isValidScrollMode)(mode)) {
  1170. throw new Error(`Invalid scroll mode: ${mode}`);
  1171. }
  1172. if (this.pagesCount > PagesCountLimit.FORCE_SCROLL_MODE_PAGE) {
  1173. return;
  1174. }
  1175. this._previousScrollMode = this._scrollMode;
  1176. this._scrollMode = mode;
  1177. this.eventBus.dispatch("scrollmodechanged", {
  1178. source: this,
  1179. mode
  1180. });
  1181. this._updateScrollMode(this._currentPageNumber);
  1182. }
  1183. _updateScrollMode(pageNumber = null) {
  1184. const scrollMode = this._scrollMode,
  1185. viewer = this.viewer;
  1186. viewer.classList.toggle("scrollHorizontal", scrollMode === _ui_utils.ScrollMode.HORIZONTAL);
  1187. viewer.classList.toggle("scrollWrapped", scrollMode === _ui_utils.ScrollMode.WRAPPED);
  1188. if (!this.pdfDocument || !pageNumber) {
  1189. return;
  1190. }
  1191. if (scrollMode === _ui_utils.ScrollMode.PAGE) {
  1192. this.#ensurePageViewVisible();
  1193. } else if (this._previousScrollMode === _ui_utils.ScrollMode.PAGE) {
  1194. this._updateSpreadMode();
  1195. }
  1196. if (this._currentScaleValue && isNaN(this._currentScaleValue)) {
  1197. this._setScale(this._currentScaleValue, true);
  1198. }
  1199. this._setCurrentPageNumber(pageNumber, true);
  1200. this.update();
  1201. }
  1202. get spreadMode() {
  1203. return this._spreadMode;
  1204. }
  1205. set spreadMode(mode) {
  1206. if (this._spreadMode === mode) {
  1207. return;
  1208. }
  1209. if (!(0, _ui_utils.isValidSpreadMode)(mode)) {
  1210. throw new Error(`Invalid spread mode: ${mode}`);
  1211. }
  1212. this._spreadMode = mode;
  1213. this.eventBus.dispatch("spreadmodechanged", {
  1214. source: this,
  1215. mode
  1216. });
  1217. this._updateSpreadMode(this._currentPageNumber);
  1218. }
  1219. _updateSpreadMode(pageNumber = null) {
  1220. if (!this.pdfDocument) {
  1221. return;
  1222. }
  1223. const viewer = this.viewer,
  1224. pages = this._pages;
  1225. if (this._scrollMode === _ui_utils.ScrollMode.PAGE) {
  1226. this.#ensurePageViewVisible();
  1227. } else {
  1228. viewer.textContent = "";
  1229. if (this._spreadMode === _ui_utils.SpreadMode.NONE) {
  1230. for (let i = 0, ii = pages.length; i < ii; ++i) {
  1231. viewer.appendChild(pages[i].div);
  1232. }
  1233. } else {
  1234. const parity = this._spreadMode - 1;
  1235. let spread = null;
  1236. for (let i = 0, ii = pages.length; i < ii; ++i) {
  1237. if (spread === null) {
  1238. spread = document.createElement("div");
  1239. spread.className = "spread";
  1240. viewer.appendChild(spread);
  1241. } else if (i % 2 === parity) {
  1242. spread = spread.cloneNode(false);
  1243. viewer.appendChild(spread);
  1244. }
  1245. spread.appendChild(pages[i].div);
  1246. }
  1247. }
  1248. }
  1249. if (!pageNumber) {
  1250. return;
  1251. }
  1252. if (this._currentScaleValue && isNaN(this._currentScaleValue)) {
  1253. this._setScale(this._currentScaleValue, true);
  1254. }
  1255. this._setCurrentPageNumber(pageNumber, true);
  1256. this.update();
  1257. }
  1258. _getPageAdvance(currentPageNumber, previous = false) {
  1259. switch (this._scrollMode) {
  1260. case _ui_utils.ScrollMode.WRAPPED:
  1261. {
  1262. const {
  1263. views
  1264. } = this._getVisiblePages(),
  1265. pageLayout = new Map();
  1266. for (const {
  1267. id,
  1268. y,
  1269. percent,
  1270. widthPercent
  1271. } of views) {
  1272. if (percent === 0 || widthPercent < 100) {
  1273. continue;
  1274. }
  1275. let yArray = pageLayout.get(y);
  1276. if (!yArray) {
  1277. pageLayout.set(y, yArray ||= []);
  1278. }
  1279. yArray.push(id);
  1280. }
  1281. for (const yArray of pageLayout.values()) {
  1282. const currentIndex = yArray.indexOf(currentPageNumber);
  1283. if (currentIndex === -1) {
  1284. continue;
  1285. }
  1286. const numPages = yArray.length;
  1287. if (numPages === 1) {
  1288. break;
  1289. }
  1290. if (previous) {
  1291. for (let i = currentIndex - 1, ii = 0; i >= ii; i--) {
  1292. const currentId = yArray[i],
  1293. expectedId = yArray[i + 1] - 1;
  1294. if (currentId < expectedId) {
  1295. return currentPageNumber - expectedId;
  1296. }
  1297. }
  1298. } else {
  1299. for (let i = currentIndex + 1, ii = numPages; i < ii; i++) {
  1300. const currentId = yArray[i],
  1301. expectedId = yArray[i - 1] + 1;
  1302. if (currentId > expectedId) {
  1303. return expectedId - currentPageNumber;
  1304. }
  1305. }
  1306. }
  1307. if (previous) {
  1308. const firstId = yArray[0];
  1309. if (firstId < currentPageNumber) {
  1310. return currentPageNumber - firstId + 1;
  1311. }
  1312. } else {
  1313. const lastId = yArray[numPages - 1];
  1314. if (lastId > currentPageNumber) {
  1315. return lastId - currentPageNumber + 1;
  1316. }
  1317. }
  1318. break;
  1319. }
  1320. break;
  1321. }
  1322. case _ui_utils.ScrollMode.HORIZONTAL:
  1323. {
  1324. break;
  1325. }
  1326. case _ui_utils.ScrollMode.PAGE:
  1327. case _ui_utils.ScrollMode.VERTICAL:
  1328. {
  1329. if (this._spreadMode === _ui_utils.SpreadMode.NONE) {
  1330. break;
  1331. }
  1332. const parity = this._spreadMode - 1;
  1333. if (previous && currentPageNumber % 2 !== parity) {
  1334. break;
  1335. } else if (!previous && currentPageNumber % 2 === parity) {
  1336. break;
  1337. }
  1338. const {
  1339. views
  1340. } = this._getVisiblePages(),
  1341. expectedId = previous ? currentPageNumber - 1 : currentPageNumber + 1;
  1342. for (const {
  1343. id,
  1344. percent,
  1345. widthPercent
  1346. } of views) {
  1347. if (id !== expectedId) {
  1348. continue;
  1349. }
  1350. if (percent > 0 && widthPercent === 100) {
  1351. return 2;
  1352. }
  1353. break;
  1354. }
  1355. break;
  1356. }
  1357. }
  1358. return 1;
  1359. }
  1360. nextPage() {
  1361. const currentPageNumber = this._currentPageNumber,
  1362. pagesCount = this.pagesCount;
  1363. if (currentPageNumber >= pagesCount) {
  1364. return false;
  1365. }
  1366. const advance = this._getPageAdvance(currentPageNumber, false) || 1;
  1367. this.currentPageNumber = Math.min(currentPageNumber + advance, pagesCount);
  1368. return true;
  1369. }
  1370. previousPage() {
  1371. const currentPageNumber = this._currentPageNumber;
  1372. if (currentPageNumber <= 1) {
  1373. return false;
  1374. }
  1375. const advance = this._getPageAdvance(currentPageNumber, true) || 1;
  1376. this.currentPageNumber = Math.max(currentPageNumber - advance, 1);
  1377. return true;
  1378. }
  1379. increaseScale(steps = 1) {
  1380. let newScale = this._currentScale;
  1381. do {
  1382. newScale = (newScale * _ui_utils.DEFAULT_SCALE_DELTA).toFixed(2);
  1383. newScale = Math.ceil(newScale * 10) / 10;
  1384. newScale = Math.min(_ui_utils.MAX_SCALE, newScale);
  1385. } while (--steps > 0 && newScale < _ui_utils.MAX_SCALE);
  1386. this.currentScaleValue = newScale;
  1387. }
  1388. decreaseScale(steps = 1) {
  1389. let newScale = this._currentScale;
  1390. do {
  1391. newScale = (newScale / _ui_utils.DEFAULT_SCALE_DELTA).toFixed(2);
  1392. newScale = Math.floor(newScale * 10) / 10;
  1393. newScale = Math.max(_ui_utils.MIN_SCALE, newScale);
  1394. } while (--steps > 0 && newScale > _ui_utils.MIN_SCALE);
  1395. this.currentScaleValue = newScale;
  1396. }
  1397. }
  1398. exports.BaseViewer = BaseViewer;