base_viewer.js 49 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885
  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 _l10n_utils = require("./l10n_utils.js");
  32. var _pdf_page_view = require("./pdf_page_view.js");
  33. var _pdf_rendering_queue = require("./pdf_rendering_queue.js");
  34. var _pdf_link_service = require("./pdf_link_service.js");
  35. var _struct_tree_layer_builder = require("./struct_tree_layer_builder.js");
  36. var _text_highlighter = require("./text_highlighter.js");
  37. var _text_layer_builder = require("./text_layer_builder.js");
  38. var _xfa_layer_builder = require("./xfa_layer_builder.js");
  39. const DEFAULT_CACHE_SIZE = 10;
  40. const ENABLE_PERMISSIONS_CLASS = "enablePermissions";
  41. const PagesCountLimit = {
  42. FORCE_SCROLL_MODE_PAGE: 15000,
  43. FORCE_LAZY_PAGE_INIT: 7500,
  44. PAUSE_EAGER_PAGE_INIT: 250
  45. };
  46. exports.PagesCountLimit = PagesCountLimit;
  47. function isValidAnnotationEditorMode(mode) {
  48. return Object.values(_pdf.AnnotationEditorType).includes(mode) && mode !== _pdf.AnnotationEditorType.DISABLE;
  49. }
  50. class PDFPageViewBuffer {
  51. #buf = new Set();
  52. #size = 0;
  53. constructor(size) {
  54. this.#size = size;
  55. }
  56. push(view) {
  57. const buf = this.#buf;
  58. if (buf.has(view)) {
  59. buf.delete(view);
  60. }
  61. buf.add(view);
  62. if (buf.size > this.#size) {
  63. this.#destroyFirstView();
  64. }
  65. }
  66. resize(newSize, idsToKeep = null) {
  67. this.#size = newSize;
  68. const buf = this.#buf;
  69. if (idsToKeep) {
  70. const ii = buf.size;
  71. let i = 1;
  72. for (const view of buf) {
  73. if (idsToKeep.has(view.id)) {
  74. buf.delete(view);
  75. buf.add(view);
  76. }
  77. if (++i > ii) {
  78. break;
  79. }
  80. }
  81. }
  82. while (buf.size > this.#size) {
  83. this.#destroyFirstView();
  84. }
  85. }
  86. has(view) {
  87. return this.#buf.has(view);
  88. }
  89. [Symbol.iterator]() {
  90. return this.#buf.keys();
  91. }
  92. #destroyFirstView() {
  93. const firstView = this.#buf.keys().next().value;
  94. firstView?.destroy();
  95. this.#buf.delete(firstView);
  96. }
  97. }
  98. exports.PDFPageViewBuffer = PDFPageViewBuffer;
  99. class BaseViewer {
  100. #buffer = null;
  101. #annotationEditorMode = _pdf.AnnotationEditorType.DISABLE;
  102. #annotationEditorUIManager = null;
  103. #annotationMode = _pdf.AnnotationMode.ENABLE_FORMS;
  104. #enablePermissions = false;
  105. #previousContainerHeight = 0;
  106. #scrollModePageState = null;
  107. #onVisibilityChange = null;
  108. constructor(options) {
  109. if (this.constructor === BaseViewer) {
  110. throw new Error("Cannot initialize BaseViewer.");
  111. }
  112. const viewerVersion = '2.16.105';
  113. if (_pdf.version !== viewerVersion) {
  114. throw new Error(`The API version "${_pdf.version}" does not match the Viewer version "${viewerVersion}".`);
  115. }
  116. this.container = options.container;
  117. this.viewer = options.viewer || options.container.firstElementChild;
  118. if (!(this.container?.tagName.toUpperCase() === "DIV" && this.viewer?.tagName.toUpperCase() === "DIV")) {
  119. throw new Error("Invalid `container` and/or `viewer` option.");
  120. }
  121. if (this.container.offsetParent && getComputedStyle(this.container).position !== "absolute") {
  122. throw new Error("The `container` must be absolutely positioned.");
  123. }
  124. this.eventBus = options.eventBus;
  125. this.linkService = options.linkService || new _pdf_link_service.SimpleLinkService();
  126. this.downloadManager = options.downloadManager || null;
  127. this.findController = options.findController || null;
  128. this._scriptingManager = options.scriptingManager || null;
  129. this.removePageBorders = options.removePageBorders || false;
  130. this.textLayerMode = options.textLayerMode ?? _ui_utils.TextLayerMode.ENABLE;
  131. this.#annotationMode = options.annotationMode ?? _pdf.AnnotationMode.ENABLE_FORMS;
  132. this.#annotationEditorMode = options.annotationEditorMode ?? _pdf.AnnotationEditorType.DISABLE;
  133. this.imageResourcesPath = options.imageResourcesPath || "";
  134. this.enablePrintAutoRotate = options.enablePrintAutoRotate || false;
  135. this.renderer = options.renderer || _ui_utils.RendererType.CANVAS;
  136. this.useOnlyCssZoom = options.useOnlyCssZoom || false;
  137. this.maxCanvasPixels = options.maxCanvasPixels;
  138. this.l10n = options.l10n || _l10n_utils.NullL10n;
  139. this.#enablePermissions = options.enablePermissions || false;
  140. this.pageColors = options.pageColors || null;
  141. if (this.pageColors && !(CSS.supports("color", this.pageColors.background) && CSS.supports("color", this.pageColors.foreground))) {
  142. if (this.pageColors.background || this.pageColors.foreground) {
  143. console.warn("BaseViewer: Ignoring `pageColors`-option, since the browser doesn't support the values used.");
  144. }
  145. this.pageColors = null;
  146. }
  147. this.defaultRenderingQueue = !options.renderingQueue;
  148. if (this.defaultRenderingQueue) {
  149. this.renderingQueue = new _pdf_rendering_queue.PDFRenderingQueue();
  150. this.renderingQueue.setViewer(this);
  151. } else {
  152. this.renderingQueue = options.renderingQueue;
  153. }
  154. this.scroll = (0, _ui_utils.watchScroll)(this.container, this._scrollUpdate.bind(this));
  155. this.presentationModeState = _ui_utils.PresentationModeState.UNKNOWN;
  156. this._onBeforeDraw = this._onAfterDraw = null;
  157. this._resetView();
  158. if (this.removePageBorders) {
  159. this.viewer.classList.add("removePageBorders");
  160. }
  161. this.updateContainerHeightCss();
  162. }
  163. get pagesCount() {
  164. return this._pages.length;
  165. }
  166. getPageView(index) {
  167. return this._pages[index];
  168. }
  169. get pageViewsReady() {
  170. if (!this._pagesCapability.settled) {
  171. return false;
  172. }
  173. return this._pages.every(function (pageView) {
  174. return pageView?.pdfPage;
  175. });
  176. }
  177. get renderForms() {
  178. return this.#annotationMode === _pdf.AnnotationMode.ENABLE_FORMS;
  179. }
  180. get enableScripting() {
  181. return !!this._scriptingManager;
  182. }
  183. get currentPageNumber() {
  184. return this._currentPageNumber;
  185. }
  186. set currentPageNumber(val) {
  187. if (!Number.isInteger(val)) {
  188. throw new Error("Invalid page number.");
  189. }
  190. if (!this.pdfDocument) {
  191. return;
  192. }
  193. if (!this._setCurrentPageNumber(val, true)) {
  194. console.error(`currentPageNumber: "${val}" is not a valid page.`);
  195. }
  196. }
  197. _setCurrentPageNumber(val, resetCurrentPageView = false) {
  198. if (this._currentPageNumber === val) {
  199. if (resetCurrentPageView) {
  200. this.#resetCurrentPageView();
  201. }
  202. return true;
  203. }
  204. if (!(0 < val && val <= this.pagesCount)) {
  205. return false;
  206. }
  207. const previous = this._currentPageNumber;
  208. this._currentPageNumber = val;
  209. this.eventBus.dispatch("pagechanging", {
  210. source: this,
  211. pageNumber: val,
  212. pageLabel: this._pageLabels?.[val - 1] ?? null,
  213. previous
  214. });
  215. if (resetCurrentPageView) {
  216. this.#resetCurrentPageView();
  217. }
  218. return true;
  219. }
  220. get currentPageLabel() {
  221. return this._pageLabels?.[this._currentPageNumber - 1] ?? null;
  222. }
  223. set currentPageLabel(val) {
  224. if (!this.pdfDocument) {
  225. return;
  226. }
  227. let page = val | 0;
  228. if (this._pageLabels) {
  229. const i = this._pageLabels.indexOf(val);
  230. if (i >= 0) {
  231. page = i + 1;
  232. }
  233. }
  234. if (!this._setCurrentPageNumber(page, true)) {
  235. console.error(`currentPageLabel: "${val}" is not a valid page.`);
  236. }
  237. }
  238. get currentScale() {
  239. return this._currentScale !== _ui_utils.UNKNOWN_SCALE ? this._currentScale : _ui_utils.DEFAULT_SCALE;
  240. }
  241. set currentScale(val) {
  242. if (isNaN(val)) {
  243. throw new Error("Invalid numeric scale.");
  244. }
  245. if (!this.pdfDocument) {
  246. return;
  247. }
  248. this._setScale(val, false);
  249. }
  250. get currentScaleValue() {
  251. return this._currentScaleValue;
  252. }
  253. set currentScaleValue(val) {
  254. if (!this.pdfDocument) {
  255. return;
  256. }
  257. this._setScale(val, false);
  258. }
  259. get pagesRotation() {
  260. return this._pagesRotation;
  261. }
  262. set pagesRotation(rotation) {
  263. if (!(0, _ui_utils.isValidRotation)(rotation)) {
  264. throw new Error("Invalid pages rotation angle.");
  265. }
  266. if (!this.pdfDocument) {
  267. return;
  268. }
  269. rotation %= 360;
  270. if (rotation < 0) {
  271. rotation += 360;
  272. }
  273. if (this._pagesRotation === rotation) {
  274. return;
  275. }
  276. this._pagesRotation = rotation;
  277. const pageNumber = this._currentPageNumber;
  278. const updateArgs = {
  279. rotation
  280. };
  281. for (const pageView of this._pages) {
  282. pageView.update(updateArgs);
  283. }
  284. if (this._currentScaleValue) {
  285. this._setScale(this._currentScaleValue, true);
  286. }
  287. this.eventBus.dispatch("rotationchanging", {
  288. source: this,
  289. pagesRotation: rotation,
  290. pageNumber
  291. });
  292. if (this.defaultRenderingQueue) {
  293. this.update();
  294. }
  295. }
  296. get firstPagePromise() {
  297. return this.pdfDocument ? this._firstPageCapability.promise : null;
  298. }
  299. get onePageRendered() {
  300. return this.pdfDocument ? this._onePageRenderedCapability.promise : null;
  301. }
  302. get pagesPromise() {
  303. return this.pdfDocument ? this._pagesCapability.promise : null;
  304. }
  305. #initializePermissions(permissions) {
  306. const params = {
  307. annotationEditorMode: this.#annotationEditorMode,
  308. annotationMode: this.#annotationMode,
  309. textLayerMode: this.textLayerMode
  310. };
  311. if (!permissions) {
  312. return params;
  313. }
  314. if (!permissions.includes(_pdf.PermissionFlag.COPY)) {
  315. this.viewer.classList.add(ENABLE_PERMISSIONS_CLASS);
  316. }
  317. if (!permissions.includes(_pdf.PermissionFlag.MODIFY_CONTENTS)) {
  318. params.annotationEditorMode = _pdf.AnnotationEditorType.DISABLE;
  319. }
  320. if (!permissions.includes(_pdf.PermissionFlag.MODIFY_ANNOTATIONS) && !permissions.includes(_pdf.PermissionFlag.FILL_INTERACTIVE_FORMS) && this.#annotationMode === _pdf.AnnotationMode.ENABLE_FORMS) {
  321. params.annotationMode = _pdf.AnnotationMode.ENABLE;
  322. }
  323. return params;
  324. }
  325. #onePageRenderedOrForceFetch() {
  326. if (document.visibilityState === "hidden" || !this.container.offsetParent || this._getVisiblePages().views.length === 0) {
  327. return Promise.resolve();
  328. }
  329. const visibilityChangePromise = new Promise(resolve => {
  330. this.#onVisibilityChange = () => {
  331. if (document.visibilityState !== "hidden") {
  332. return;
  333. }
  334. resolve();
  335. document.removeEventListener("visibilitychange", this.#onVisibilityChange);
  336. this.#onVisibilityChange = null;
  337. };
  338. document.addEventListener("visibilitychange", this.#onVisibilityChange);
  339. });
  340. return Promise.race([this._onePageRenderedCapability.promise, visibilityChangePromise]);
  341. }
  342. setDocument(pdfDocument) {
  343. if (this.pdfDocument) {
  344. this.eventBus.dispatch("pagesdestroy", {
  345. source: this
  346. });
  347. this._cancelRendering();
  348. this._resetView();
  349. if (this.findController) {
  350. this.findController.setDocument(null);
  351. }
  352. if (this._scriptingManager) {
  353. this._scriptingManager.setDocument(null);
  354. }
  355. if (this.#annotationEditorUIManager) {
  356. this.#annotationEditorUIManager.destroy();
  357. this.#annotationEditorUIManager = null;
  358. }
  359. }
  360. this.pdfDocument = pdfDocument;
  361. if (!pdfDocument) {
  362. return;
  363. }
  364. const isPureXfa = pdfDocument.isPureXfa;
  365. const pagesCount = pdfDocument.numPages;
  366. const firstPagePromise = pdfDocument.getPage(1);
  367. const optionalContentConfigPromise = pdfDocument.getOptionalContentConfig();
  368. const permissionsPromise = this.#enablePermissions ? pdfDocument.getPermissions() : Promise.resolve();
  369. if (pagesCount > PagesCountLimit.FORCE_SCROLL_MODE_PAGE) {
  370. console.warn("Forcing PAGE-scrolling for performance reasons, given the length of the document.");
  371. const mode = this._scrollMode = _ui_utils.ScrollMode.PAGE;
  372. this.eventBus.dispatch("scrollmodechanged", {
  373. source: this,
  374. mode
  375. });
  376. }
  377. this._pagesCapability.promise.then(() => {
  378. this.eventBus.dispatch("pagesloaded", {
  379. source: this,
  380. pagesCount
  381. });
  382. }, () => {});
  383. this._onBeforeDraw = evt => {
  384. const pageView = this._pages[evt.pageNumber - 1];
  385. if (!pageView) {
  386. return;
  387. }
  388. this.#buffer.push(pageView);
  389. };
  390. this.eventBus._on("pagerender", this._onBeforeDraw);
  391. this._onAfterDraw = evt => {
  392. if (evt.cssTransform || this._onePageRenderedCapability.settled) {
  393. return;
  394. }
  395. this._onePageRenderedCapability.resolve({
  396. timestamp: evt.timestamp
  397. });
  398. this.eventBus._off("pagerendered", this._onAfterDraw);
  399. this._onAfterDraw = null;
  400. if (this.#onVisibilityChange) {
  401. document.removeEventListener("visibilitychange", this.#onVisibilityChange);
  402. this.#onVisibilityChange = null;
  403. }
  404. };
  405. this.eventBus._on("pagerendered", this._onAfterDraw);
  406. Promise.all([firstPagePromise, permissionsPromise]).then(([firstPdfPage, permissions]) => {
  407. if (pdfDocument !== this.pdfDocument) {
  408. return;
  409. }
  410. this._firstPageCapability.resolve(firstPdfPage);
  411. this._optionalContentConfigPromise = optionalContentConfigPromise;
  412. const {
  413. annotationEditorMode,
  414. annotationMode,
  415. textLayerMode
  416. } = this.#initializePermissions(permissions);
  417. if (annotationEditorMode !== _pdf.AnnotationEditorType.DISABLE) {
  418. const mode = annotationEditorMode;
  419. if (isPureXfa) {
  420. console.warn("Warning: XFA-editing is not implemented.");
  421. } else if (isValidAnnotationEditorMode(mode)) {
  422. this.#annotationEditorUIManager = new _pdf.AnnotationEditorUIManager(this.container, this.eventBus);
  423. if (mode !== _pdf.AnnotationEditorType.NONE) {
  424. this.#annotationEditorUIManager.updateMode(mode);
  425. }
  426. } else {
  427. console.error(`Invalid AnnotationEditor mode: ${mode}`);
  428. }
  429. }
  430. const viewerElement = this._scrollMode === _ui_utils.ScrollMode.PAGE ? null : this.viewer;
  431. const scale = this.currentScale;
  432. const viewport = firstPdfPage.getViewport({
  433. scale: scale * _pdf.PixelsPerInch.PDF_TO_CSS_UNITS
  434. });
  435. const textLayerFactory = textLayerMode !== _ui_utils.TextLayerMode.DISABLE && !isPureXfa ? this : null;
  436. const annotationLayerFactory = annotationMode !== _pdf.AnnotationMode.DISABLE ? this : null;
  437. const xfaLayerFactory = isPureXfa ? this : null;
  438. const annotationEditorLayerFactory = this.#annotationEditorUIManager ? this : null;
  439. for (let pageNum = 1; pageNum <= pagesCount; ++pageNum) {
  440. const pageView = new _pdf_page_view.PDFPageView({
  441. container: viewerElement,
  442. eventBus: this.eventBus,
  443. id: pageNum,
  444. scale,
  445. defaultViewport: viewport.clone(),
  446. optionalContentConfigPromise,
  447. renderingQueue: this.renderingQueue,
  448. textLayerFactory,
  449. textLayerMode,
  450. annotationLayerFactory,
  451. annotationMode,
  452. xfaLayerFactory,
  453. annotationEditorLayerFactory,
  454. textHighlighterFactory: this,
  455. structTreeLayerFactory: this,
  456. imageResourcesPath: this.imageResourcesPath,
  457. renderer: this.renderer,
  458. useOnlyCssZoom: this.useOnlyCssZoom,
  459. maxCanvasPixels: this.maxCanvasPixels,
  460. pageColors: this.pageColors,
  461. l10n: this.l10n
  462. });
  463. this._pages.push(pageView);
  464. }
  465. const firstPageView = this._pages[0];
  466. if (firstPageView) {
  467. firstPageView.setPdfPage(firstPdfPage);
  468. this.linkService.cachePageRef(1, firstPdfPage.ref);
  469. }
  470. if (this._scrollMode === _ui_utils.ScrollMode.PAGE) {
  471. this.#ensurePageViewVisible();
  472. } else if (this._spreadMode !== _ui_utils.SpreadMode.NONE) {
  473. this._updateSpreadMode();
  474. }
  475. this.#onePageRenderedOrForceFetch().then(async () => {
  476. if (this.findController) {
  477. this.findController.setDocument(pdfDocument);
  478. }
  479. if (this._scriptingManager) {
  480. this._scriptingManager.setDocument(pdfDocument);
  481. }
  482. if (this.#annotationEditorUIManager) {
  483. this.eventBus.dispatch("annotationeditormodechanged", {
  484. source: this,
  485. mode: this.#annotationEditorMode
  486. });
  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. accessibilityManager = null
  1065. }) {
  1066. return new _text_layer_builder.TextLayerBuilder({
  1067. textLayerDiv,
  1068. eventBus,
  1069. pageIndex,
  1070. viewport,
  1071. enhanceTextSelection: this.isInPresentationMode ? false : enhanceTextSelection,
  1072. highlighter,
  1073. accessibilityManager
  1074. });
  1075. }
  1076. createTextHighlighter({
  1077. pageIndex,
  1078. eventBus
  1079. }) {
  1080. return new _text_highlighter.TextHighlighter({
  1081. eventBus,
  1082. pageIndex,
  1083. findController: this.isInPresentationMode ? null : this.findController
  1084. });
  1085. }
  1086. createAnnotationLayerBuilder({
  1087. pageDiv,
  1088. pdfPage,
  1089. annotationStorage = this.pdfDocument?.annotationStorage,
  1090. imageResourcesPath = "",
  1091. renderForms = true,
  1092. l10n = _l10n_utils.NullL10n,
  1093. enableScripting = this.enableScripting,
  1094. hasJSActionsPromise = this.pdfDocument?.hasJSActions(),
  1095. mouseState = this._scriptingManager?.mouseState,
  1096. fieldObjectsPromise = this.pdfDocument?.getFieldObjects(),
  1097. annotationCanvasMap = null,
  1098. accessibilityManager = null
  1099. }) {
  1100. return new _annotation_layer_builder.AnnotationLayerBuilder({
  1101. pageDiv,
  1102. pdfPage,
  1103. annotationStorage,
  1104. imageResourcesPath,
  1105. renderForms,
  1106. linkService: this.linkService,
  1107. downloadManager: this.downloadManager,
  1108. l10n,
  1109. enableScripting,
  1110. hasJSActionsPromise,
  1111. mouseState,
  1112. fieldObjectsPromise,
  1113. annotationCanvasMap,
  1114. accessibilityManager
  1115. });
  1116. }
  1117. createAnnotationEditorLayerBuilder({
  1118. uiManager = this.#annotationEditorUIManager,
  1119. pageDiv,
  1120. pdfPage,
  1121. accessibilityManager = null,
  1122. l10n,
  1123. annotationStorage = this.pdfDocument?.annotationStorage
  1124. }) {
  1125. return new _annotation_editor_layer_builder.AnnotationEditorLayerBuilder({
  1126. uiManager,
  1127. pageDiv,
  1128. pdfPage,
  1129. annotationStorage,
  1130. accessibilityManager,
  1131. l10n
  1132. });
  1133. }
  1134. createXfaLayerBuilder({
  1135. pageDiv,
  1136. pdfPage,
  1137. annotationStorage = this.pdfDocument?.annotationStorage
  1138. }) {
  1139. return new _xfa_layer_builder.XfaLayerBuilder({
  1140. pageDiv,
  1141. pdfPage,
  1142. annotationStorage,
  1143. linkService: this.linkService
  1144. });
  1145. }
  1146. createStructTreeLayerBuilder({
  1147. pdfPage
  1148. }) {
  1149. return new _struct_tree_layer_builder.StructTreeLayerBuilder({
  1150. pdfPage
  1151. });
  1152. }
  1153. get hasEqualPageSizes() {
  1154. const firstPageView = this._pages[0];
  1155. for (let i = 1, ii = this._pages.length; i < ii; ++i) {
  1156. const pageView = this._pages[i];
  1157. if (pageView.width !== firstPageView.width || pageView.height !== firstPageView.height) {
  1158. return false;
  1159. }
  1160. }
  1161. return true;
  1162. }
  1163. getPagesOverview() {
  1164. return this._pages.map(pageView => {
  1165. const viewport = pageView.pdfPage.getViewport({
  1166. scale: 1
  1167. });
  1168. if (!this.enablePrintAutoRotate || (0, _ui_utils.isPortraitOrientation)(viewport)) {
  1169. return {
  1170. width: viewport.width,
  1171. height: viewport.height,
  1172. rotation: viewport.rotation
  1173. };
  1174. }
  1175. return {
  1176. width: viewport.height,
  1177. height: viewport.width,
  1178. rotation: (viewport.rotation - 90) % 360
  1179. };
  1180. });
  1181. }
  1182. get optionalContentConfigPromise() {
  1183. if (!this.pdfDocument) {
  1184. return Promise.resolve(null);
  1185. }
  1186. if (!this._optionalContentConfigPromise) {
  1187. console.error("optionalContentConfigPromise: Not initialized yet.");
  1188. return this.pdfDocument.getOptionalContentConfig();
  1189. }
  1190. return this._optionalContentConfigPromise;
  1191. }
  1192. set optionalContentConfigPromise(promise) {
  1193. if (!(promise instanceof Promise)) {
  1194. throw new Error(`Invalid optionalContentConfigPromise: ${promise}`);
  1195. }
  1196. if (!this.pdfDocument) {
  1197. return;
  1198. }
  1199. if (!this._optionalContentConfigPromise) {
  1200. return;
  1201. }
  1202. this._optionalContentConfigPromise = promise;
  1203. const updateArgs = {
  1204. optionalContentConfigPromise: promise
  1205. };
  1206. for (const pageView of this._pages) {
  1207. pageView.update(updateArgs);
  1208. }
  1209. this.update();
  1210. this.eventBus.dispatch("optionalcontentconfigchanged", {
  1211. source: this,
  1212. promise
  1213. });
  1214. }
  1215. get scrollMode() {
  1216. return this._scrollMode;
  1217. }
  1218. set scrollMode(mode) {
  1219. if (this._scrollMode === mode) {
  1220. return;
  1221. }
  1222. if (!(0, _ui_utils.isValidScrollMode)(mode)) {
  1223. throw new Error(`Invalid scroll mode: ${mode}`);
  1224. }
  1225. if (this.pagesCount > PagesCountLimit.FORCE_SCROLL_MODE_PAGE) {
  1226. return;
  1227. }
  1228. this._previousScrollMode = this._scrollMode;
  1229. this._scrollMode = mode;
  1230. this.eventBus.dispatch("scrollmodechanged", {
  1231. source: this,
  1232. mode
  1233. });
  1234. this._updateScrollMode(this._currentPageNumber);
  1235. }
  1236. _updateScrollMode(pageNumber = null) {
  1237. const scrollMode = this._scrollMode,
  1238. viewer = this.viewer;
  1239. viewer.classList.toggle("scrollHorizontal", scrollMode === _ui_utils.ScrollMode.HORIZONTAL);
  1240. viewer.classList.toggle("scrollWrapped", scrollMode === _ui_utils.ScrollMode.WRAPPED);
  1241. if (!this.pdfDocument || !pageNumber) {
  1242. return;
  1243. }
  1244. if (scrollMode === _ui_utils.ScrollMode.PAGE) {
  1245. this.#ensurePageViewVisible();
  1246. } else if (this._previousScrollMode === _ui_utils.ScrollMode.PAGE) {
  1247. this._updateSpreadMode();
  1248. }
  1249. if (this._currentScaleValue && isNaN(this._currentScaleValue)) {
  1250. this._setScale(this._currentScaleValue, true);
  1251. }
  1252. this._setCurrentPageNumber(pageNumber, true);
  1253. this.update();
  1254. }
  1255. get spreadMode() {
  1256. return this._spreadMode;
  1257. }
  1258. set spreadMode(mode) {
  1259. if (this._spreadMode === mode) {
  1260. return;
  1261. }
  1262. if (!(0, _ui_utils.isValidSpreadMode)(mode)) {
  1263. throw new Error(`Invalid spread mode: ${mode}`);
  1264. }
  1265. this._spreadMode = mode;
  1266. this.eventBus.dispatch("spreadmodechanged", {
  1267. source: this,
  1268. mode
  1269. });
  1270. this._updateSpreadMode(this._currentPageNumber);
  1271. }
  1272. _updateSpreadMode(pageNumber = null) {
  1273. if (!this.pdfDocument) {
  1274. return;
  1275. }
  1276. const viewer = this.viewer,
  1277. pages = this._pages;
  1278. if (this._scrollMode === _ui_utils.ScrollMode.PAGE) {
  1279. this.#ensurePageViewVisible();
  1280. } else {
  1281. viewer.textContent = "";
  1282. if (this._spreadMode === _ui_utils.SpreadMode.NONE) {
  1283. for (const pageView of this._pages) {
  1284. viewer.append(pageView.div);
  1285. }
  1286. } else {
  1287. const parity = this._spreadMode - 1;
  1288. let spread = null;
  1289. for (let i = 0, ii = pages.length; i < ii; ++i) {
  1290. if (spread === null) {
  1291. spread = document.createElement("div");
  1292. spread.className = "spread";
  1293. viewer.append(spread);
  1294. } else if (i % 2 === parity) {
  1295. spread = spread.cloneNode(false);
  1296. viewer.append(spread);
  1297. }
  1298. spread.append(pages[i].div);
  1299. }
  1300. }
  1301. }
  1302. if (!pageNumber) {
  1303. return;
  1304. }
  1305. if (this._currentScaleValue && isNaN(this._currentScaleValue)) {
  1306. this._setScale(this._currentScaleValue, true);
  1307. }
  1308. this._setCurrentPageNumber(pageNumber, true);
  1309. this.update();
  1310. }
  1311. _getPageAdvance(currentPageNumber, previous = false) {
  1312. switch (this._scrollMode) {
  1313. case _ui_utils.ScrollMode.WRAPPED:
  1314. {
  1315. const {
  1316. views
  1317. } = this._getVisiblePages(),
  1318. pageLayout = new Map();
  1319. for (const {
  1320. id,
  1321. y,
  1322. percent,
  1323. widthPercent
  1324. } of views) {
  1325. if (percent === 0 || widthPercent < 100) {
  1326. continue;
  1327. }
  1328. let yArray = pageLayout.get(y);
  1329. if (!yArray) {
  1330. pageLayout.set(y, yArray ||= []);
  1331. }
  1332. yArray.push(id);
  1333. }
  1334. for (const yArray of pageLayout.values()) {
  1335. const currentIndex = yArray.indexOf(currentPageNumber);
  1336. if (currentIndex === -1) {
  1337. continue;
  1338. }
  1339. const numPages = yArray.length;
  1340. if (numPages === 1) {
  1341. break;
  1342. }
  1343. if (previous) {
  1344. for (let i = currentIndex - 1, ii = 0; i >= ii; i--) {
  1345. const currentId = yArray[i],
  1346. expectedId = yArray[i + 1] - 1;
  1347. if (currentId < expectedId) {
  1348. return currentPageNumber - expectedId;
  1349. }
  1350. }
  1351. } else {
  1352. for (let i = currentIndex + 1, ii = numPages; i < ii; i++) {
  1353. const currentId = yArray[i],
  1354. expectedId = yArray[i - 1] + 1;
  1355. if (currentId > expectedId) {
  1356. return expectedId - currentPageNumber;
  1357. }
  1358. }
  1359. }
  1360. if (previous) {
  1361. const firstId = yArray[0];
  1362. if (firstId < currentPageNumber) {
  1363. return currentPageNumber - firstId + 1;
  1364. }
  1365. } else {
  1366. const lastId = yArray[numPages - 1];
  1367. if (lastId > currentPageNumber) {
  1368. return lastId - currentPageNumber + 1;
  1369. }
  1370. }
  1371. break;
  1372. }
  1373. break;
  1374. }
  1375. case _ui_utils.ScrollMode.HORIZONTAL:
  1376. {
  1377. break;
  1378. }
  1379. case _ui_utils.ScrollMode.PAGE:
  1380. case _ui_utils.ScrollMode.VERTICAL:
  1381. {
  1382. if (this._spreadMode === _ui_utils.SpreadMode.NONE) {
  1383. break;
  1384. }
  1385. const parity = this._spreadMode - 1;
  1386. if (previous && currentPageNumber % 2 !== parity) {
  1387. break;
  1388. } else if (!previous && currentPageNumber % 2 === parity) {
  1389. break;
  1390. }
  1391. const {
  1392. views
  1393. } = this._getVisiblePages(),
  1394. expectedId = previous ? currentPageNumber - 1 : currentPageNumber + 1;
  1395. for (const {
  1396. id,
  1397. percent,
  1398. widthPercent
  1399. } of views) {
  1400. if (id !== expectedId) {
  1401. continue;
  1402. }
  1403. if (percent > 0 && widthPercent === 100) {
  1404. return 2;
  1405. }
  1406. break;
  1407. }
  1408. break;
  1409. }
  1410. }
  1411. return 1;
  1412. }
  1413. nextPage() {
  1414. const currentPageNumber = this._currentPageNumber,
  1415. pagesCount = this.pagesCount;
  1416. if (currentPageNumber >= pagesCount) {
  1417. return false;
  1418. }
  1419. const advance = this._getPageAdvance(currentPageNumber, false) || 1;
  1420. this.currentPageNumber = Math.min(currentPageNumber + advance, pagesCount);
  1421. return true;
  1422. }
  1423. previousPage() {
  1424. const currentPageNumber = this._currentPageNumber;
  1425. if (currentPageNumber <= 1) {
  1426. return false;
  1427. }
  1428. const advance = this._getPageAdvance(currentPageNumber, true) || 1;
  1429. this.currentPageNumber = Math.max(currentPageNumber - advance, 1);
  1430. return true;
  1431. }
  1432. increaseScale(steps = 1) {
  1433. let newScale = this._currentScale;
  1434. do {
  1435. newScale = (newScale * _ui_utils.DEFAULT_SCALE_DELTA).toFixed(2);
  1436. newScale = Math.ceil(newScale * 10) / 10;
  1437. newScale = Math.min(_ui_utils.MAX_SCALE, newScale);
  1438. } while (--steps > 0 && newScale < _ui_utils.MAX_SCALE);
  1439. this.currentScaleValue = newScale;
  1440. }
  1441. decreaseScale(steps = 1) {
  1442. let newScale = this._currentScale;
  1443. do {
  1444. newScale = (newScale / _ui_utils.DEFAULT_SCALE_DELTA).toFixed(2);
  1445. newScale = Math.floor(newScale * 10) / 10;
  1446. newScale = Math.max(_ui_utils.MIN_SCALE, newScale);
  1447. } while (--steps > 0 && newScale > _ui_utils.MIN_SCALE);
  1448. this.currentScaleValue = newScale;
  1449. }
  1450. updateContainerHeightCss() {
  1451. const height = this.container.clientHeight;
  1452. if (height !== this.#previousContainerHeight) {
  1453. this.#previousContainerHeight = height;
  1454. _ui_utils.docStyle.setProperty("--viewer-container-height", `${height}px`);
  1455. }
  1456. }
  1457. get annotationEditorMode() {
  1458. return this.#annotationEditorUIManager ? this.#annotationEditorMode : _pdf.AnnotationEditorType.DISABLE;
  1459. }
  1460. set annotationEditorMode(mode) {
  1461. if (!this.#annotationEditorUIManager) {
  1462. throw new Error(`The AnnotationEditor is not enabled.`);
  1463. }
  1464. if (this.#annotationEditorMode === mode) {
  1465. return;
  1466. }
  1467. if (!isValidAnnotationEditorMode(mode)) {
  1468. throw new Error(`Invalid AnnotationEditor mode: ${mode}`);
  1469. }
  1470. if (!this.pdfDocument) {
  1471. return;
  1472. }
  1473. this.#annotationEditorMode = mode;
  1474. this.eventBus.dispatch("annotationeditormodechanged", {
  1475. source: this,
  1476. mode
  1477. });
  1478. this.#annotationEditorUIManager.updateMode(mode);
  1479. }
  1480. set annotationEditorParams({
  1481. type,
  1482. value
  1483. }) {
  1484. if (!this.#annotationEditorUIManager) {
  1485. throw new Error(`The AnnotationEditor is not enabled.`);
  1486. }
  1487. this.#annotationEditorUIManager.updateParams(type, value);
  1488. }
  1489. refresh() {
  1490. if (!this.pdfDocument) {
  1491. return;
  1492. }
  1493. const updateArgs = {};
  1494. for (const pageView of this._pages) {
  1495. pageView.update(updateArgs);
  1496. }
  1497. this.update();
  1498. }
  1499. }
  1500. exports.BaseViewer = BaseViewer;