base_viewer.js 47 KB

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