base_viewer.js 49 KB

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