pdf_viewer.js 63 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122
  1. /* Copyright 2014 Mozilla Foundation
  2. *
  3. * Licensed under the Apache License, Version 2.0 (the "License");
  4. * you may not use this file except in compliance with the License.
  5. * You may obtain a copy of the License at
  6. *
  7. * http://www.apache.org/licenses/LICENSE-2.0
  8. *
  9. * Unless required by applicable law or agreed to in writing, software
  10. * distributed under the License is distributed on an "AS IS" BASIS,
  11. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. * See the License for the specific language governing permissions and
  13. * limitations under the License.
  14. */
  15. /** @typedef {import("../src/display/api").PDFDocumentProxy} PDFDocumentProxy */
  16. /** @typedef {import("../src/display/api").PDFPageProxy} PDFPageProxy */
  17. // eslint-disable-next-line max-len
  18. /** @typedef {import("../src/display/display_utils").PageViewport} PageViewport */
  19. // eslint-disable-next-line max-len
  20. /** @typedef {import("../src/display/optional_content_config").OptionalContentConfig} OptionalContentConfig */
  21. /** @typedef {import("./event_utils").EventBus} EventBus */
  22. /** @typedef {import("./interfaces").IDownloadManager} IDownloadManager */
  23. /** @typedef {import("./interfaces").IL10n} IL10n */
  24. /** @typedef {import("./interfaces").IPDFLinkService} IPDFLinkService */
  25. import {
  26. AnnotationEditorType,
  27. AnnotationEditorUIManager,
  28. AnnotationMode,
  29. createPromiseCapability,
  30. PermissionFlag,
  31. PixelsPerInch,
  32. version,
  33. } from "pdfjs-lib";
  34. import {
  35. DEFAULT_SCALE,
  36. DEFAULT_SCALE_DELTA,
  37. DEFAULT_SCALE_VALUE,
  38. docStyle,
  39. getVisibleElements,
  40. isPortraitOrientation,
  41. isValidRotation,
  42. isValidScrollMode,
  43. isValidSpreadMode,
  44. MAX_AUTO_SCALE,
  45. MAX_SCALE,
  46. MIN_SCALE,
  47. PresentationModeState,
  48. RendererType,
  49. RenderingStates,
  50. SCROLLBAR_PADDING,
  51. scrollIntoView,
  52. ScrollMode,
  53. SpreadMode,
  54. TextLayerMode,
  55. UNKNOWN_SCALE,
  56. VERTICAL_PADDING,
  57. watchScroll,
  58. } from "./ui_utils.js";
  59. import { NullL10n } from "./l10n_utils.js";
  60. import { PDFPageView } from "./pdf_page_view.js";
  61. import { PDFRenderingQueue } from "./pdf_rendering_queue.js";
  62. import { SimpleLinkService } from "./pdf_link_service.js";
  63. const DEFAULT_CACHE_SIZE = 10;
  64. const ENABLE_PERMISSIONS_CLASS = "enablePermissions";
  65. const PagesCountLimit = {
  66. FORCE_SCROLL_MODE_PAGE: 15000,
  67. FORCE_LAZY_PAGE_INIT: 7500,
  68. PAUSE_EAGER_PAGE_INIT: 250,
  69. };
  70. function isValidAnnotationEditorMode(mode) {
  71. return (
  72. Object.values(AnnotationEditorType).includes(mode) &&
  73. mode !== AnnotationEditorType.DISABLE
  74. );
  75. }
  76. /**
  77. * @typedef {Object} PDFViewerOptions
  78. * @property {HTMLDivElement} container - The container for the viewer element.
  79. * @property {HTMLDivElement} [viewer] - The viewer element.
  80. * @property {EventBus} eventBus - The application event bus.
  81. * @property {IPDFLinkService} linkService - The navigation/linking service.
  82. * @property {IDownloadManager} [downloadManager] - The download manager
  83. * component.
  84. * @property {PDFFindController} [findController] - The find controller
  85. * component.
  86. * @property {PDFScriptingManager} [scriptingManager] - The scripting manager
  87. * component.
  88. * @property {PDFRenderingQueue} [renderingQueue] - The rendering queue object.
  89. * @property {boolean} [removePageBorders] - Removes the border shadow around
  90. * the pages. The default value is `false`.
  91. * @property {number} [textLayerMode] - Controls if the text layer used for
  92. * selection and searching is created. The constants from {TextLayerMode}
  93. * should be used. The default value is `TextLayerMode.ENABLE`.
  94. * @property {number} [annotationMode] - Controls if the annotation layer is
  95. * created, and if interactive form elements or `AnnotationStorage`-data are
  96. * being rendered. The constants from {@link AnnotationMode} should be used;
  97. * see also {@link RenderParameters} and {@link GetOperatorListParameters}.
  98. * The default value is `AnnotationMode.ENABLE_FORMS`.
  99. * @property {number} [annotationEditorMode] - Enables the creation and editing
  100. * of new Annotations. The constants from {@link AnnotationEditorType} should
  101. * be used. The default value is `AnnotationEditorType.NONE`.
  102. * @property {string} [imageResourcesPath] - Path for image resources, mainly
  103. * mainly for annotation icons. Include trailing slash.
  104. * @property {boolean} [enablePrintAutoRotate] - Enables automatic rotation of
  105. * landscape pages upon printing. The default is `false`.
  106. * @property {boolean} [useOnlyCssZoom] - Enables CSS only zooming. The default
  107. * value is `false`.
  108. * @property {boolean} [isOffscreenCanvasSupported] - Allows to use an
  109. * OffscreenCanvas if needed.
  110. * @property {number} [maxCanvasPixels] - The maximum supported canvas size in
  111. * total pixels, i.e. width * height. Use -1 for no limit. The default value
  112. * is 4096 * 4096 (16 mega-pixels).
  113. * @property {IL10n} l10n - Localization service.
  114. * @property {boolean} [enablePermissions] - Enables PDF document permissions,
  115. * when they exist. The default value is `false`.
  116. * @property {Object} [pageColors] - Overwrites background and foreground colors
  117. * with user defined ones in order to improve readability in high contrast
  118. * mode.
  119. */
  120. class PDFPageViewBuffer {
  121. // Here we rely on the fact that `Set`s preserve the insertion order.
  122. #buf = new Set();
  123. #size = 0;
  124. constructor(size) {
  125. this.#size = size;
  126. }
  127. push(view) {
  128. const buf = this.#buf;
  129. if (buf.has(view)) {
  130. buf.delete(view); // Move the view to the "end" of the buffer.
  131. }
  132. buf.add(view);
  133. if (buf.size > this.#size) {
  134. this.#destroyFirstView();
  135. }
  136. }
  137. /**
  138. * After calling resize, the size of the buffer will be `newSize`.
  139. * The optional parameter `idsToKeep` is, if present, a Set of page-ids to
  140. * push to the back of the buffer, delaying their destruction. The size of
  141. * `idsToKeep` has no impact on the final size of the buffer; if `idsToKeep`
  142. * is larger than `newSize`, some of those pages will be destroyed anyway.
  143. */
  144. resize(newSize, idsToKeep = null) {
  145. this.#size = newSize;
  146. const buf = this.#buf;
  147. if (idsToKeep) {
  148. const ii = buf.size;
  149. let i = 1;
  150. for (const view of buf) {
  151. if (idsToKeep.has(view.id)) {
  152. buf.delete(view); // Move the view to the "end" of the buffer.
  153. buf.add(view);
  154. }
  155. if (++i > ii) {
  156. break;
  157. }
  158. }
  159. }
  160. while (buf.size > this.#size) {
  161. this.#destroyFirstView();
  162. }
  163. }
  164. has(view) {
  165. return this.#buf.has(view);
  166. }
  167. [Symbol.iterator]() {
  168. return this.#buf.keys();
  169. }
  170. #destroyFirstView() {
  171. const firstView = this.#buf.keys().next().value;
  172. firstView?.destroy();
  173. this.#buf.delete(firstView);
  174. }
  175. }
  176. /**
  177. * Simple viewer control to display PDF content/pages.
  178. */
  179. class PDFViewer {
  180. #buffer = null;
  181. #annotationEditorMode = AnnotationEditorType.NONE;
  182. #annotationEditorUIManager = null;
  183. #annotationMode = AnnotationMode.ENABLE_FORMS;
  184. #containerTopLeft = null;
  185. #enablePermissions = false;
  186. #previousContainerHeight = 0;
  187. #resizeObserver = new ResizeObserver(this.#resizeObserverCallback.bind(this));
  188. #scrollModePageState = null;
  189. #onVisibilityChange = null;
  190. #scaleTimeoutId = null;
  191. /**
  192. * @param {PDFViewerOptions} options
  193. */
  194. constructor(options) {
  195. const viewerVersion =
  196. typeof PDFJSDev !== "undefined" ? PDFJSDev.eval("BUNDLE_VERSION") : null;
  197. if (version !== viewerVersion) {
  198. throw new Error(
  199. `The API version "${version}" does not match the Viewer version "${viewerVersion}".`
  200. );
  201. }
  202. this.container = options.container;
  203. this.#resizeObserver.observe(this.container);
  204. this.viewer = options.viewer || options.container.firstElementChild;
  205. if (
  206. typeof PDFJSDev === "undefined" ||
  207. PDFJSDev.test("!PRODUCTION || GENERIC")
  208. ) {
  209. if (
  210. !(
  211. this.container?.tagName.toUpperCase() === "DIV" &&
  212. this.viewer?.tagName.toUpperCase() === "DIV"
  213. )
  214. ) {
  215. throw new Error("Invalid `container` and/or `viewer` option.");
  216. }
  217. if (
  218. this.container.offsetParent &&
  219. getComputedStyle(this.container).position !== "absolute"
  220. ) {
  221. throw new Error("The `container` must be absolutely positioned.");
  222. }
  223. }
  224. this.eventBus = options.eventBus;
  225. this.linkService = options.linkService || new SimpleLinkService();
  226. this.downloadManager = options.downloadManager || null;
  227. this.findController = options.findController || null;
  228. this._scriptingManager = options.scriptingManager || null;
  229. this.removePageBorders = options.removePageBorders || false;
  230. this.textLayerMode = options.textLayerMode ?? TextLayerMode.ENABLE;
  231. this.#annotationMode =
  232. options.annotationMode ?? AnnotationMode.ENABLE_FORMS;
  233. this.#annotationEditorMode =
  234. options.annotationEditorMode ?? AnnotationEditorType.NONE;
  235. this.imageResourcesPath = options.imageResourcesPath || "";
  236. this.enablePrintAutoRotate = options.enablePrintAutoRotate || false;
  237. if (
  238. typeof PDFJSDev === "undefined" ||
  239. PDFJSDev.test("!PRODUCTION || GENERIC")
  240. ) {
  241. this.renderer = options.renderer || RendererType.CANVAS;
  242. }
  243. this.useOnlyCssZoom = options.useOnlyCssZoom || false;
  244. this.isOffscreenCanvasSupported =
  245. options.isOffscreenCanvasSupported ?? true;
  246. this.maxCanvasPixels = options.maxCanvasPixels;
  247. this.l10n = options.l10n || NullL10n;
  248. this.#enablePermissions = options.enablePermissions || false;
  249. this.pageColors = options.pageColors || null;
  250. if (typeof PDFJSDev === "undefined" || !PDFJSDev.test("MOZCENTRAL")) {
  251. if (
  252. this.pageColors &&
  253. !(
  254. CSS.supports("color", this.pageColors.background) &&
  255. CSS.supports("color", this.pageColors.foreground)
  256. )
  257. ) {
  258. if (this.pageColors.background || this.pageColors.foreground) {
  259. console.warn(
  260. "PDFViewer: Ignoring `pageColors`-option, since the browser doesn't support the values used."
  261. );
  262. }
  263. this.pageColors = null;
  264. }
  265. }
  266. this.defaultRenderingQueue = !options.renderingQueue;
  267. if (this.defaultRenderingQueue) {
  268. // Custom rendering queue is not specified, using default one
  269. this.renderingQueue = new PDFRenderingQueue();
  270. this.renderingQueue.setViewer(this);
  271. } else {
  272. this.renderingQueue = options.renderingQueue;
  273. }
  274. this.scroll = watchScroll(this.container, this._scrollUpdate.bind(this));
  275. this.presentationModeState = PresentationModeState.UNKNOWN;
  276. this._onBeforeDraw = this._onAfterDraw = null;
  277. this._resetView();
  278. if (this.removePageBorders) {
  279. this.viewer.classList.add("removePageBorders");
  280. }
  281. this.#updateContainerHeightCss();
  282. }
  283. get pagesCount() {
  284. return this._pages.length;
  285. }
  286. getPageView(index) {
  287. return this._pages[index];
  288. }
  289. /**
  290. * @type {boolean} - True if all {PDFPageView} objects are initialized.
  291. */
  292. get pageViewsReady() {
  293. if (!this._pagesCapability.settled) {
  294. return false;
  295. }
  296. // Prevent printing errors when 'disableAutoFetch' is set, by ensuring
  297. // that *all* pages have in fact been completely loaded.
  298. return this._pages.every(function (pageView) {
  299. return pageView?.pdfPage;
  300. });
  301. }
  302. /**
  303. * @type {boolean}
  304. */
  305. get renderForms() {
  306. return this.#annotationMode === AnnotationMode.ENABLE_FORMS;
  307. }
  308. /**
  309. * @type {boolean}
  310. */
  311. get enableScripting() {
  312. return !!this._scriptingManager;
  313. }
  314. /**
  315. * @type {number}
  316. */
  317. get currentPageNumber() {
  318. return this._currentPageNumber;
  319. }
  320. /**
  321. * @param {number} val - The page number.
  322. */
  323. set currentPageNumber(val) {
  324. if (!Number.isInteger(val)) {
  325. throw new Error("Invalid page number.");
  326. }
  327. if (!this.pdfDocument) {
  328. return;
  329. }
  330. // The intent can be to just reset a scroll position and/or scale.
  331. if (!this._setCurrentPageNumber(val, /* resetCurrentPageView = */ true)) {
  332. console.error(`currentPageNumber: "${val}" is not a valid page.`);
  333. }
  334. }
  335. /**
  336. * @returns {boolean} Whether the pageNumber is valid (within bounds).
  337. * @private
  338. */
  339. _setCurrentPageNumber(val, resetCurrentPageView = false) {
  340. if (this._currentPageNumber === val) {
  341. if (resetCurrentPageView) {
  342. this.#resetCurrentPageView();
  343. }
  344. return true;
  345. }
  346. if (!(0 < val && val <= this.pagesCount)) {
  347. return false;
  348. }
  349. const previous = this._currentPageNumber;
  350. this._currentPageNumber = val;
  351. this.eventBus.dispatch("pagechanging", {
  352. source: this,
  353. pageNumber: val,
  354. pageLabel: this._pageLabels?.[val - 1] ?? null,
  355. previous,
  356. });
  357. if (resetCurrentPageView) {
  358. this.#resetCurrentPageView();
  359. }
  360. return true;
  361. }
  362. /**
  363. * @type {string|null} Returns the current page label, or `null` if no page
  364. * labels exist.
  365. */
  366. get currentPageLabel() {
  367. return this._pageLabels?.[this._currentPageNumber - 1] ?? null;
  368. }
  369. /**
  370. * @param {string} val - The page label.
  371. */
  372. set currentPageLabel(val) {
  373. if (!this.pdfDocument) {
  374. return;
  375. }
  376. let page = val | 0; // Fallback page number.
  377. if (this._pageLabels) {
  378. const i = this._pageLabels.indexOf(val);
  379. if (i >= 0) {
  380. page = i + 1;
  381. }
  382. }
  383. // The intent can be to just reset a scroll position and/or scale.
  384. if (!this._setCurrentPageNumber(page, /* resetCurrentPageView = */ true)) {
  385. console.error(`currentPageLabel: "${val}" is not a valid page.`);
  386. }
  387. }
  388. /**
  389. * @type {number}
  390. */
  391. get currentScale() {
  392. return this._currentScale !== UNKNOWN_SCALE
  393. ? this._currentScale
  394. : DEFAULT_SCALE;
  395. }
  396. /**
  397. * @param {number} val - Scale of the pages in percents.
  398. */
  399. set currentScale(val) {
  400. if (isNaN(val)) {
  401. throw new Error("Invalid numeric scale.");
  402. }
  403. if (!this.pdfDocument) {
  404. return;
  405. }
  406. this._setScale(val, { noScroll: false });
  407. }
  408. /**
  409. * @type {string}
  410. */
  411. get currentScaleValue() {
  412. return this._currentScaleValue;
  413. }
  414. /**
  415. * @param val - The scale of the pages (in percent or predefined value).
  416. */
  417. set currentScaleValue(val) {
  418. if (!this.pdfDocument) {
  419. return;
  420. }
  421. this._setScale(val, { noScroll: false });
  422. }
  423. /**
  424. * @type {number}
  425. */
  426. get pagesRotation() {
  427. return this._pagesRotation;
  428. }
  429. /**
  430. * @param {number} rotation - The rotation of the pages (0, 90, 180, 270).
  431. */
  432. set pagesRotation(rotation) {
  433. if (!isValidRotation(rotation)) {
  434. throw new Error("Invalid pages rotation angle.");
  435. }
  436. if (!this.pdfDocument) {
  437. return;
  438. }
  439. // Normalize the rotation, by clamping it to the [0, 360) range.
  440. rotation %= 360;
  441. if (rotation < 0) {
  442. rotation += 360;
  443. }
  444. if (this._pagesRotation === rotation) {
  445. return; // The rotation didn't change.
  446. }
  447. this._pagesRotation = rotation;
  448. const pageNumber = this._currentPageNumber;
  449. this.refresh(true, { rotation });
  450. // Prevent errors in case the rotation changes *before* the scale has been
  451. // set to a non-default value.
  452. if (this._currentScaleValue) {
  453. this._setScale(this._currentScaleValue, { noScroll: true });
  454. }
  455. this.eventBus.dispatch("rotationchanging", {
  456. source: this,
  457. pagesRotation: rotation,
  458. pageNumber,
  459. });
  460. if (this.defaultRenderingQueue) {
  461. this.update();
  462. }
  463. }
  464. get firstPagePromise() {
  465. return this.pdfDocument ? this._firstPageCapability.promise : null;
  466. }
  467. get onePageRendered() {
  468. return this.pdfDocument ? this._onePageRenderedCapability.promise : null;
  469. }
  470. get pagesPromise() {
  471. return this.pdfDocument ? this._pagesCapability.promise : null;
  472. }
  473. #layerProperties() {
  474. const self = this;
  475. return {
  476. get annotationEditorUIManager() {
  477. return self.#annotationEditorUIManager;
  478. },
  479. get annotationStorage() {
  480. return self.pdfDocument?.annotationStorage;
  481. },
  482. get downloadManager() {
  483. return self.downloadManager;
  484. },
  485. get enableScripting() {
  486. return !!self._scriptingManager;
  487. },
  488. get fieldObjectsPromise() {
  489. return self.pdfDocument?.getFieldObjects();
  490. },
  491. get findController() {
  492. return self.findController;
  493. },
  494. get hasJSActionsPromise() {
  495. return self.pdfDocument?.hasJSActions();
  496. },
  497. get linkService() {
  498. return self.linkService;
  499. },
  500. };
  501. }
  502. /**
  503. * Currently only *some* permissions are supported.
  504. * @returns {Object}
  505. */
  506. #initializePermissions(permissions) {
  507. const params = {
  508. annotationEditorMode: this.#annotationEditorMode,
  509. annotationMode: this.#annotationMode,
  510. textLayerMode: this.textLayerMode,
  511. };
  512. if (!permissions) {
  513. return params;
  514. }
  515. if (!permissions.includes(PermissionFlag.COPY)) {
  516. this.viewer.classList.add(ENABLE_PERMISSIONS_CLASS);
  517. }
  518. if (!permissions.includes(PermissionFlag.MODIFY_CONTENTS)) {
  519. params.annotationEditorMode = AnnotationEditorType.DISABLE;
  520. }
  521. if (
  522. !permissions.includes(PermissionFlag.MODIFY_ANNOTATIONS) &&
  523. !permissions.includes(PermissionFlag.FILL_INTERACTIVE_FORMS) &&
  524. this.#annotationMode === AnnotationMode.ENABLE_FORMS
  525. ) {
  526. params.annotationMode = AnnotationMode.ENABLE;
  527. }
  528. return params;
  529. }
  530. #onePageRenderedOrForceFetch() {
  531. // Unless the viewer *and* its pages are visible, rendering won't start and
  532. // `this._onePageRenderedCapability` thus won't be resolved.
  533. // To ensure that automatic printing, on document load, still works even in
  534. // those cases we force-allow fetching of all pages when:
  535. // - The current window/tab is inactive, which will prevent rendering since
  536. // `requestAnimationFrame` is being used; fixes bug 1746213.
  537. // - The viewer is hidden in the DOM, e.g. in a `display: none` <iframe>
  538. // element; fixes bug 1618621.
  539. // - The viewer is visible, but none of the pages are (e.g. if the
  540. // viewer is very small); fixes bug 1618955.
  541. if (
  542. document.visibilityState === "hidden" ||
  543. !this.container.offsetParent ||
  544. this._getVisiblePages().views.length === 0
  545. ) {
  546. return Promise.resolve();
  547. }
  548. // Handle the window/tab becoming inactive *after* rendering has started;
  549. // fixes (another part of) bug 1746213.
  550. const visibilityChangePromise = new Promise(resolve => {
  551. this.#onVisibilityChange = () => {
  552. if (document.visibilityState !== "hidden") {
  553. return;
  554. }
  555. resolve();
  556. document.removeEventListener(
  557. "visibilitychange",
  558. this.#onVisibilityChange
  559. );
  560. this.#onVisibilityChange = null;
  561. };
  562. document.addEventListener("visibilitychange", this.#onVisibilityChange);
  563. });
  564. return Promise.race([
  565. this._onePageRenderedCapability.promise,
  566. visibilityChangePromise,
  567. ]);
  568. }
  569. /**
  570. * @param {PDFDocumentProxy} pdfDocument
  571. */
  572. setDocument(pdfDocument) {
  573. if (this.pdfDocument) {
  574. this.eventBus.dispatch("pagesdestroy", { source: this });
  575. this._cancelRendering();
  576. this._resetView();
  577. this.findController?.setDocument(null);
  578. this._scriptingManager?.setDocument(null);
  579. if (this.#annotationEditorUIManager) {
  580. this.#annotationEditorUIManager.destroy();
  581. this.#annotationEditorUIManager = null;
  582. }
  583. }
  584. this.pdfDocument = pdfDocument;
  585. if (!pdfDocument) {
  586. return;
  587. }
  588. const pagesCount = pdfDocument.numPages;
  589. const firstPagePromise = pdfDocument.getPage(1);
  590. // Rendering (potentially) depends on this, hence fetching it immediately.
  591. const optionalContentConfigPromise = pdfDocument.getOptionalContentConfig();
  592. const permissionsPromise = this.#enablePermissions
  593. ? pdfDocument.getPermissions()
  594. : Promise.resolve();
  595. // Given that browsers don't handle huge amounts of DOM-elements very well,
  596. // enforce usage of PAGE-scrolling when loading *very* long/large documents.
  597. if (pagesCount > PagesCountLimit.FORCE_SCROLL_MODE_PAGE) {
  598. console.warn(
  599. "Forcing PAGE-scrolling for performance reasons, given the length of the document."
  600. );
  601. const mode = (this._scrollMode = ScrollMode.PAGE);
  602. this.eventBus.dispatch("scrollmodechanged", { source: this, mode });
  603. }
  604. this._pagesCapability.promise.then(
  605. () => {
  606. this.eventBus.dispatch("pagesloaded", { source: this, pagesCount });
  607. },
  608. () => {
  609. /* Prevent "Uncaught (in promise)"-messages in the console. */
  610. }
  611. );
  612. this._onBeforeDraw = evt => {
  613. const pageView = this._pages[evt.pageNumber - 1];
  614. if (!pageView) {
  615. return;
  616. }
  617. // Add the page to the buffer at the start of drawing. That way it can be
  618. // evicted from the buffer and destroyed even if we pause its rendering.
  619. this.#buffer.push(pageView);
  620. };
  621. this.eventBus._on("pagerender", this._onBeforeDraw);
  622. this._onAfterDraw = evt => {
  623. if (evt.cssTransform || this._onePageRenderedCapability.settled) {
  624. return;
  625. }
  626. this._onePageRenderedCapability.resolve({ timestamp: evt.timestamp });
  627. this.eventBus._off("pagerendered", this._onAfterDraw);
  628. this._onAfterDraw = null;
  629. if (this.#onVisibilityChange) {
  630. document.removeEventListener(
  631. "visibilitychange",
  632. this.#onVisibilityChange
  633. );
  634. this.#onVisibilityChange = null;
  635. }
  636. };
  637. this.eventBus._on("pagerendered", this._onAfterDraw);
  638. // Fetch a single page so we can get a viewport that will be the default
  639. // viewport for all pages
  640. Promise.all([firstPagePromise, permissionsPromise])
  641. .then(([firstPdfPage, permissions]) => {
  642. if (pdfDocument !== this.pdfDocument) {
  643. return; // The document was closed while the first page resolved.
  644. }
  645. this._firstPageCapability.resolve(firstPdfPage);
  646. this._optionalContentConfigPromise = optionalContentConfigPromise;
  647. const { annotationEditorMode, annotationMode, textLayerMode } =
  648. this.#initializePermissions(permissions);
  649. if (annotationEditorMode !== AnnotationEditorType.DISABLE) {
  650. const mode = annotationEditorMode;
  651. if (pdfDocument.isPureXfa) {
  652. console.warn("Warning: XFA-editing is not implemented.");
  653. } else if (isValidAnnotationEditorMode(mode)) {
  654. this.#annotationEditorUIManager = new AnnotationEditorUIManager(
  655. this.container,
  656. this.eventBus,
  657. pdfDocument?.annotationStorage
  658. );
  659. if (mode !== AnnotationEditorType.NONE) {
  660. this.#annotationEditorUIManager.updateMode(mode);
  661. }
  662. } else {
  663. console.error(`Invalid AnnotationEditor mode: ${mode}`);
  664. }
  665. }
  666. const layerProperties = this.#layerProperties.bind(this);
  667. const viewerElement =
  668. this._scrollMode === ScrollMode.PAGE ? null : this.viewer;
  669. const scale = this.currentScale;
  670. const viewport = firstPdfPage.getViewport({
  671. scale: scale * PixelsPerInch.PDF_TO_CSS_UNITS,
  672. });
  673. // Ensure that the various layers always get the correct initial size,
  674. // see issue 15795.
  675. docStyle.setProperty("--scale-factor", viewport.scale);
  676. for (let pageNum = 1; pageNum <= pagesCount; ++pageNum) {
  677. const pageView = new PDFPageView({
  678. container: viewerElement,
  679. eventBus: this.eventBus,
  680. id: pageNum,
  681. scale,
  682. defaultViewport: viewport.clone(),
  683. optionalContentConfigPromise,
  684. renderingQueue: this.renderingQueue,
  685. textLayerMode,
  686. annotationMode,
  687. imageResourcesPath: this.imageResourcesPath,
  688. renderer:
  689. typeof PDFJSDev === "undefined" ||
  690. PDFJSDev.test("!PRODUCTION || GENERIC")
  691. ? this.renderer
  692. : null,
  693. useOnlyCssZoom: this.useOnlyCssZoom,
  694. isOffscreenCanvasSupported: this.isOffscreenCanvasSupported,
  695. maxCanvasPixels: this.maxCanvasPixels,
  696. pageColors: this.pageColors,
  697. l10n: this.l10n,
  698. layerProperties,
  699. });
  700. this._pages.push(pageView);
  701. }
  702. // Set the first `pdfPage` immediately, since it's already loaded,
  703. // rather than having to repeat the `PDFDocumentProxy.getPage` call in
  704. // the `this.#ensurePdfPageLoaded` method before rendering can start.
  705. const firstPageView = this._pages[0];
  706. if (firstPageView) {
  707. firstPageView.setPdfPage(firstPdfPage);
  708. this.linkService.cachePageRef(1, firstPdfPage.ref);
  709. }
  710. if (this._scrollMode === ScrollMode.PAGE) {
  711. // Ensure that the current page becomes visible on document load.
  712. this.#ensurePageViewVisible();
  713. } else if (this._spreadMode !== SpreadMode.NONE) {
  714. this._updateSpreadMode();
  715. }
  716. // Fetch all the pages since the viewport is needed before printing
  717. // starts to create the correct size canvas. Wait until one page is
  718. // rendered so we don't tie up too many resources early on.
  719. this.#onePageRenderedOrForceFetch().then(async () => {
  720. this.findController?.setDocument(pdfDocument); // Enable searching.
  721. this._scriptingManager?.setDocument(pdfDocument); // Enable scripting.
  722. if (this.#annotationEditorUIManager) {
  723. // Ensure that the Editor buttons, in the toolbar, are updated.
  724. this.eventBus.dispatch("annotationeditormodechanged", {
  725. source: this,
  726. mode: this.#annotationEditorMode,
  727. });
  728. }
  729. // In addition to 'disableAutoFetch' being set, also attempt to reduce
  730. // resource usage when loading *very* long/large documents.
  731. if (
  732. pdfDocument.loadingParams.disableAutoFetch ||
  733. pagesCount > PagesCountLimit.FORCE_LAZY_PAGE_INIT
  734. ) {
  735. // XXX: Printing is semi-broken with auto fetch disabled.
  736. this._pagesCapability.resolve();
  737. return;
  738. }
  739. let getPagesLeft = pagesCount - 1; // The first page was already loaded.
  740. if (getPagesLeft <= 0) {
  741. this._pagesCapability.resolve();
  742. return;
  743. }
  744. for (let pageNum = 2; pageNum <= pagesCount; ++pageNum) {
  745. const promise = pdfDocument.getPage(pageNum).then(
  746. pdfPage => {
  747. const pageView = this._pages[pageNum - 1];
  748. if (!pageView.pdfPage) {
  749. pageView.setPdfPage(pdfPage);
  750. }
  751. this.linkService.cachePageRef(pageNum, pdfPage.ref);
  752. if (--getPagesLeft === 0) {
  753. this._pagesCapability.resolve();
  754. }
  755. },
  756. reason => {
  757. console.error(
  758. `Unable to get page ${pageNum} to initialize viewer`,
  759. reason
  760. );
  761. if (--getPagesLeft === 0) {
  762. this._pagesCapability.resolve();
  763. }
  764. }
  765. );
  766. if (pageNum % PagesCountLimit.PAUSE_EAGER_PAGE_INIT === 0) {
  767. await promise;
  768. }
  769. }
  770. });
  771. this.eventBus.dispatch("pagesinit", { source: this });
  772. pdfDocument.getMetadata().then(({ info }) => {
  773. if (pdfDocument !== this.pdfDocument) {
  774. return; // The document was closed while the metadata resolved.
  775. }
  776. if (info.Language) {
  777. this.viewer.lang = info.Language;
  778. }
  779. });
  780. if (this.defaultRenderingQueue) {
  781. this.update();
  782. }
  783. })
  784. .catch(reason => {
  785. console.error("Unable to initialize viewer", reason);
  786. this._pagesCapability.reject(reason);
  787. });
  788. }
  789. /**
  790. * @param {Array|null} labels
  791. */
  792. setPageLabels(labels) {
  793. if (!this.pdfDocument) {
  794. return;
  795. }
  796. if (!labels) {
  797. this._pageLabels = null;
  798. } else if (
  799. !(Array.isArray(labels) && this.pdfDocument.numPages === labels.length)
  800. ) {
  801. this._pageLabels = null;
  802. console.error(`setPageLabels: Invalid page labels.`);
  803. } else {
  804. this._pageLabels = labels;
  805. }
  806. // Update all the `PDFPageView` instances.
  807. for (let i = 0, ii = this._pages.length; i < ii; i++) {
  808. this._pages[i].setPageLabel(this._pageLabels?.[i] ?? null);
  809. }
  810. }
  811. _resetView() {
  812. this._pages = [];
  813. this._currentPageNumber = 1;
  814. this._currentScale = UNKNOWN_SCALE;
  815. this._currentScaleValue = null;
  816. this._pageLabels = null;
  817. this.#buffer = new PDFPageViewBuffer(DEFAULT_CACHE_SIZE);
  818. this._location = null;
  819. this._pagesRotation = 0;
  820. this._optionalContentConfigPromise = null;
  821. this._firstPageCapability = createPromiseCapability();
  822. this._onePageRenderedCapability = createPromiseCapability();
  823. this._pagesCapability = createPromiseCapability();
  824. this._scrollMode = ScrollMode.VERTICAL;
  825. this._previousScrollMode = ScrollMode.UNKNOWN;
  826. this._spreadMode = SpreadMode.NONE;
  827. this.#scrollModePageState = {
  828. previousPageNumber: 1,
  829. scrollDown: true,
  830. pages: [],
  831. };
  832. if (this._onBeforeDraw) {
  833. this.eventBus._off("pagerender", this._onBeforeDraw);
  834. this._onBeforeDraw = null;
  835. }
  836. if (this._onAfterDraw) {
  837. this.eventBus._off("pagerendered", this._onAfterDraw);
  838. this._onAfterDraw = null;
  839. }
  840. if (this.#onVisibilityChange) {
  841. document.removeEventListener(
  842. "visibilitychange",
  843. this.#onVisibilityChange
  844. );
  845. this.#onVisibilityChange = null;
  846. }
  847. // Remove the pages from the DOM...
  848. this.viewer.textContent = "";
  849. // ... and reset the Scroll mode CSS class(es) afterwards.
  850. this._updateScrollMode();
  851. this.viewer.removeAttribute("lang");
  852. // Reset all PDF document permissions.
  853. this.viewer.classList.remove(ENABLE_PERMISSIONS_CLASS);
  854. }
  855. #ensurePageViewVisible() {
  856. if (this._scrollMode !== ScrollMode.PAGE) {
  857. throw new Error("#ensurePageViewVisible: Invalid scrollMode value.");
  858. }
  859. const pageNumber = this._currentPageNumber,
  860. state = this.#scrollModePageState,
  861. viewer = this.viewer;
  862. // Temporarily remove all the pages from the DOM...
  863. viewer.textContent = "";
  864. // ... and clear out the active ones.
  865. state.pages.length = 0;
  866. if (this._spreadMode === SpreadMode.NONE && !this.isInPresentationMode) {
  867. // Finally, append the new page to the viewer.
  868. const pageView = this._pages[pageNumber - 1];
  869. viewer.append(pageView.div);
  870. state.pages.push(pageView);
  871. } else {
  872. const pageIndexSet = new Set(),
  873. parity = this._spreadMode - 1;
  874. // Determine the pageIndices in the new spread.
  875. if (parity === -1) {
  876. // PresentationMode is active, with `SpreadMode.NONE` set.
  877. pageIndexSet.add(pageNumber - 1);
  878. } else if (pageNumber % 2 !== parity) {
  879. // Left-hand side page.
  880. pageIndexSet.add(pageNumber - 1);
  881. pageIndexSet.add(pageNumber);
  882. } else {
  883. // Right-hand side page.
  884. pageIndexSet.add(pageNumber - 2);
  885. pageIndexSet.add(pageNumber - 1);
  886. }
  887. // Finally, append the new pages to the viewer and apply the spreadMode.
  888. const spread = document.createElement("div");
  889. spread.className = "spread";
  890. if (this.isInPresentationMode) {
  891. const dummyPage = document.createElement("div");
  892. dummyPage.className = "dummyPage";
  893. spread.append(dummyPage);
  894. }
  895. for (const i of pageIndexSet) {
  896. const pageView = this._pages[i];
  897. if (!pageView) {
  898. continue;
  899. }
  900. spread.append(pageView.div);
  901. state.pages.push(pageView);
  902. }
  903. viewer.append(spread);
  904. }
  905. state.scrollDown = pageNumber >= state.previousPageNumber;
  906. state.previousPageNumber = pageNumber;
  907. }
  908. _scrollUpdate() {
  909. if (this.pagesCount === 0) {
  910. return;
  911. }
  912. this.update();
  913. }
  914. #scrollIntoView(pageView, pageSpot = null) {
  915. const { div, id } = pageView;
  916. // Ensure that `this._currentPageNumber` is correct, when `#scrollIntoView`
  917. // is called directly (and not from `#resetCurrentPageView`).
  918. if (this._currentPageNumber !== id) {
  919. this._setCurrentPageNumber(id);
  920. }
  921. if (this._scrollMode === ScrollMode.PAGE) {
  922. this.#ensurePageViewVisible();
  923. // Ensure that rendering always occurs, to avoid showing a blank page,
  924. // even if the current position doesn't change when the page is scrolled.
  925. this.update();
  926. }
  927. if (!pageSpot && !this.isInPresentationMode) {
  928. const left = div.offsetLeft + div.clientLeft,
  929. right = left + div.clientWidth;
  930. const { scrollLeft, clientWidth } = this.container;
  931. if (
  932. this._scrollMode === ScrollMode.HORIZONTAL ||
  933. left < scrollLeft ||
  934. right > scrollLeft + clientWidth
  935. ) {
  936. pageSpot = { left: 0, top: 0 };
  937. }
  938. }
  939. scrollIntoView(div, pageSpot);
  940. // Ensure that the correct *initial* document position is set, when any
  941. // OpenParameters are used, for documents with non-default Scroll/Spread
  942. // modes (fixes issue 15695). This is necessary since the scroll-handler
  943. // invokes the `update`-method asynchronously, and `this._location` could
  944. // thus be wrong when the initial zooming occurs in the default viewer.
  945. if (!this._currentScaleValue && this._location) {
  946. this._location = null;
  947. }
  948. }
  949. /**
  950. * Prevent unnecessary re-rendering of all pages when the scale changes
  951. * only because of limited numerical precision.
  952. */
  953. #isSameScale(newScale) {
  954. return (
  955. newScale === this._currentScale ||
  956. Math.abs(newScale - this._currentScale) < 1e-15
  957. );
  958. }
  959. _setScaleUpdatePages(
  960. newScale,
  961. newValue,
  962. { noScroll = false, preset = false, delay: drawingDelay = -1 }
  963. ) {
  964. this._currentScaleValue = newValue.toString();
  965. if (this.#isSameScale(newScale)) {
  966. if (preset) {
  967. this.eventBus.dispatch("scalechanging", {
  968. source: this,
  969. scale: newScale,
  970. presetValue: newValue,
  971. });
  972. }
  973. return;
  974. }
  975. docStyle.setProperty(
  976. "--scale-factor",
  977. newScale * PixelsPerInch.PDF_TO_CSS_UNITS
  978. );
  979. const mustPostponeDrawing = drawingDelay >= 0 && drawingDelay < 1000;
  980. const updateArgs = {
  981. scale: newScale,
  982. };
  983. if (mustPostponeDrawing) {
  984. updateArgs.drawingDelay = drawingDelay;
  985. }
  986. this.refresh(true, updateArgs);
  987. if (mustPostponeDrawing) {
  988. this.#scaleTimeoutId = setTimeout(() => {
  989. this.#scaleTimeoutId = null;
  990. this.refresh();
  991. }, drawingDelay);
  992. }
  993. this._currentScale = newScale;
  994. if (!noScroll) {
  995. let page = this._currentPageNumber,
  996. dest;
  997. if (
  998. this._location &&
  999. !(this.isInPresentationMode || this.isChangingPresentationMode)
  1000. ) {
  1001. page = this._location.pageNumber;
  1002. dest = [
  1003. null,
  1004. { name: "XYZ" },
  1005. this._location.left,
  1006. this._location.top,
  1007. null,
  1008. ];
  1009. }
  1010. this.scrollPageIntoView({
  1011. pageNumber: page,
  1012. destArray: dest,
  1013. allowNegativeOffset: true,
  1014. });
  1015. }
  1016. this.eventBus.dispatch("scalechanging", {
  1017. source: this,
  1018. scale: newScale,
  1019. presetValue: preset ? newValue : undefined,
  1020. });
  1021. if (this.defaultRenderingQueue) {
  1022. this.update();
  1023. }
  1024. }
  1025. /**
  1026. * @private
  1027. */
  1028. get _pageWidthScaleFactor() {
  1029. if (
  1030. this._spreadMode !== SpreadMode.NONE &&
  1031. this._scrollMode !== ScrollMode.HORIZONTAL
  1032. ) {
  1033. return 2;
  1034. }
  1035. return 1;
  1036. }
  1037. _setScale(value, options) {
  1038. let scale = parseFloat(value);
  1039. if (scale > 0) {
  1040. options.preset = false;
  1041. this._setScaleUpdatePages(scale, value, options);
  1042. } else {
  1043. const currentPage = this._pages[this._currentPageNumber - 1];
  1044. if (!currentPage) {
  1045. return;
  1046. }
  1047. let hPadding = SCROLLBAR_PADDING,
  1048. vPadding = VERTICAL_PADDING;
  1049. if (this.isInPresentationMode) {
  1050. // Pages have a 2px (transparent) border in PresentationMode, see
  1051. // the `web/pdf_viewer.css` file.
  1052. hPadding = vPadding = 4; // 2 * 2px
  1053. if (this._spreadMode !== SpreadMode.NONE) {
  1054. // Account for two pages being visible in PresentationMode, thus
  1055. // "doubling" the total border width.
  1056. hPadding *= 2;
  1057. }
  1058. } else if (this.removePageBorders) {
  1059. hPadding = vPadding = 0;
  1060. } else if (this._scrollMode === ScrollMode.HORIZONTAL) {
  1061. [hPadding, vPadding] = [vPadding, hPadding]; // Swap the padding values.
  1062. }
  1063. const pageWidthScale =
  1064. (((this.container.clientWidth - hPadding) / currentPage.width) *
  1065. currentPage.scale) /
  1066. this._pageWidthScaleFactor;
  1067. const pageHeightScale =
  1068. ((this.container.clientHeight - vPadding) / currentPage.height) *
  1069. currentPage.scale;
  1070. switch (value) {
  1071. case "page-actual":
  1072. scale = 1;
  1073. break;
  1074. case "page-width":
  1075. scale = pageWidthScale;
  1076. break;
  1077. case "page-height":
  1078. scale = pageHeightScale;
  1079. break;
  1080. case "page-fit":
  1081. scale = Math.min(pageWidthScale, pageHeightScale);
  1082. break;
  1083. case "auto":
  1084. // For pages in landscape mode, fit the page height to the viewer
  1085. // *unless* the page would thus become too wide to fit horizontally.
  1086. const horizontalScale = isPortraitOrientation(currentPage)
  1087. ? pageWidthScale
  1088. : Math.min(pageHeightScale, pageWidthScale);
  1089. scale = Math.min(MAX_AUTO_SCALE, horizontalScale);
  1090. break;
  1091. default:
  1092. console.error(`_setScale: "${value}" is an unknown zoom value.`);
  1093. return;
  1094. }
  1095. options.preset = true;
  1096. this._setScaleUpdatePages(scale, value, options);
  1097. }
  1098. }
  1099. /**
  1100. * Refreshes page view: scrolls to the current page and updates the scale.
  1101. */
  1102. #resetCurrentPageView() {
  1103. const pageView = this._pages[this._currentPageNumber - 1];
  1104. if (this.isInPresentationMode) {
  1105. // Fixes the case when PDF has different page sizes.
  1106. this._setScale(this._currentScaleValue, { noScroll: true });
  1107. }
  1108. this.#scrollIntoView(pageView);
  1109. }
  1110. /**
  1111. * @param {string} label - The page label.
  1112. * @returns {number|null} The page number corresponding to the page label,
  1113. * or `null` when no page labels exist and/or the input is invalid.
  1114. */
  1115. pageLabelToPageNumber(label) {
  1116. if (!this._pageLabels) {
  1117. return null;
  1118. }
  1119. const i = this._pageLabels.indexOf(label);
  1120. if (i < 0) {
  1121. return null;
  1122. }
  1123. return i + 1;
  1124. }
  1125. /**
  1126. * @typedef {Object} ScrollPageIntoViewParameters
  1127. * @property {number} pageNumber - The page number.
  1128. * @property {Array} [destArray] - The original PDF destination array, in the
  1129. * format: <page-ref> </XYZ|/FitXXX> <args..>
  1130. * @property {boolean} [allowNegativeOffset] - Allow negative page offsets.
  1131. * The default value is `false`.
  1132. * @property {boolean} [ignoreDestinationZoom] - Ignore the zoom argument in
  1133. * the destination array. The default value is `false`.
  1134. */
  1135. /**
  1136. * Scrolls page into view.
  1137. * @param {ScrollPageIntoViewParameters} params
  1138. */
  1139. scrollPageIntoView({
  1140. pageNumber,
  1141. destArray = null,
  1142. allowNegativeOffset = false,
  1143. ignoreDestinationZoom = false,
  1144. }) {
  1145. if (!this.pdfDocument) {
  1146. return;
  1147. }
  1148. const pageView =
  1149. Number.isInteger(pageNumber) && this._pages[pageNumber - 1];
  1150. if (!pageView) {
  1151. console.error(
  1152. `scrollPageIntoView: "${pageNumber}" is not a valid pageNumber parameter.`
  1153. );
  1154. return;
  1155. }
  1156. if (this.isInPresentationMode || !destArray) {
  1157. this._setCurrentPageNumber(pageNumber, /* resetCurrentPageView = */ true);
  1158. return;
  1159. }
  1160. let x = 0,
  1161. y = 0;
  1162. let width = 0,
  1163. height = 0,
  1164. widthScale,
  1165. heightScale;
  1166. const changeOrientation = pageView.rotation % 180 !== 0;
  1167. const pageWidth =
  1168. (changeOrientation ? pageView.height : pageView.width) /
  1169. pageView.scale /
  1170. PixelsPerInch.PDF_TO_CSS_UNITS;
  1171. const pageHeight =
  1172. (changeOrientation ? pageView.width : pageView.height) /
  1173. pageView.scale /
  1174. PixelsPerInch.PDF_TO_CSS_UNITS;
  1175. let scale = 0;
  1176. switch (destArray[1].name) {
  1177. case "XYZ":
  1178. x = destArray[2];
  1179. y = destArray[3];
  1180. scale = destArray[4];
  1181. // If x and/or y coordinates are not supplied, default to
  1182. // _top_ left of the page (not the obvious bottom left,
  1183. // since aligning the bottom of the intended page with the
  1184. // top of the window is rarely helpful).
  1185. x = x !== null ? x : 0;
  1186. y = y !== null ? y : pageHeight;
  1187. break;
  1188. case "Fit":
  1189. case "FitB":
  1190. scale = "page-fit";
  1191. break;
  1192. case "FitH":
  1193. case "FitBH":
  1194. y = destArray[2];
  1195. scale = "page-width";
  1196. // According to the PDF spec, section 12.3.2.2, a `null` value in the
  1197. // parameter should maintain the position relative to the new page.
  1198. if (y === null && this._location) {
  1199. x = this._location.left;
  1200. y = this._location.top;
  1201. } else if (typeof y !== "number" || y < 0) {
  1202. // The "top" value isn't optional, according to the spec, however some
  1203. // bad PDF generators will pretend that it is (fixes bug 1663390).
  1204. y = pageHeight;
  1205. }
  1206. break;
  1207. case "FitV":
  1208. case "FitBV":
  1209. x = destArray[2];
  1210. width = pageWidth;
  1211. height = pageHeight;
  1212. scale = "page-height";
  1213. break;
  1214. case "FitR":
  1215. x = destArray[2];
  1216. y = destArray[3];
  1217. width = destArray[4] - x;
  1218. height = destArray[5] - y;
  1219. const hPadding = this.removePageBorders ? 0 : SCROLLBAR_PADDING;
  1220. const vPadding = this.removePageBorders ? 0 : VERTICAL_PADDING;
  1221. widthScale =
  1222. (this.container.clientWidth - hPadding) /
  1223. width /
  1224. PixelsPerInch.PDF_TO_CSS_UNITS;
  1225. heightScale =
  1226. (this.container.clientHeight - vPadding) /
  1227. height /
  1228. PixelsPerInch.PDF_TO_CSS_UNITS;
  1229. scale = Math.min(Math.abs(widthScale), Math.abs(heightScale));
  1230. break;
  1231. default:
  1232. console.error(
  1233. `scrollPageIntoView: "${destArray[1].name}" is not a valid destination type.`
  1234. );
  1235. return;
  1236. }
  1237. if (!ignoreDestinationZoom) {
  1238. if (scale && scale !== this._currentScale) {
  1239. this.currentScaleValue = scale;
  1240. } else if (this._currentScale === UNKNOWN_SCALE) {
  1241. this.currentScaleValue = DEFAULT_SCALE_VALUE;
  1242. }
  1243. }
  1244. if (scale === "page-fit" && !destArray[4]) {
  1245. this.#scrollIntoView(pageView);
  1246. return;
  1247. }
  1248. const boundingRect = [
  1249. pageView.viewport.convertToViewportPoint(x, y),
  1250. pageView.viewport.convertToViewportPoint(x + width, y + height),
  1251. ];
  1252. let left = Math.min(boundingRect[0][0], boundingRect[1][0]);
  1253. let top = Math.min(boundingRect[0][1], boundingRect[1][1]);
  1254. if (!allowNegativeOffset) {
  1255. // Some bad PDF generators will create destinations with e.g. top values
  1256. // that exceeds the page height. Ensure that offsets are not negative,
  1257. // to prevent a previous page from becoming visible (fixes bug 874482).
  1258. left = Math.max(left, 0);
  1259. top = Math.max(top, 0);
  1260. }
  1261. this.#scrollIntoView(pageView, /* pageSpot = */ { left, top });
  1262. }
  1263. _updateLocation(firstPage) {
  1264. const currentScale = this._currentScale;
  1265. const currentScaleValue = this._currentScaleValue;
  1266. const normalizedScaleValue =
  1267. parseFloat(currentScaleValue) === currentScale
  1268. ? Math.round(currentScale * 10000) / 100
  1269. : currentScaleValue;
  1270. const pageNumber = firstPage.id;
  1271. const currentPageView = this._pages[pageNumber - 1];
  1272. const container = this.container;
  1273. const topLeft = currentPageView.getPagePoint(
  1274. container.scrollLeft - firstPage.x,
  1275. container.scrollTop - firstPage.y
  1276. );
  1277. const intLeft = Math.round(topLeft[0]);
  1278. const intTop = Math.round(topLeft[1]);
  1279. let pdfOpenParams = `#page=${pageNumber}`;
  1280. if (!this.isInPresentationMode) {
  1281. pdfOpenParams += `&zoom=${normalizedScaleValue},${intLeft},${intTop}`;
  1282. }
  1283. this._location = {
  1284. pageNumber,
  1285. scale: normalizedScaleValue,
  1286. top: intTop,
  1287. left: intLeft,
  1288. rotation: this._pagesRotation,
  1289. pdfOpenParams,
  1290. };
  1291. }
  1292. update() {
  1293. const visible = this._getVisiblePages();
  1294. const visiblePages = visible.views,
  1295. numVisiblePages = visiblePages.length;
  1296. if (numVisiblePages === 0) {
  1297. return;
  1298. }
  1299. const newCacheSize = Math.max(DEFAULT_CACHE_SIZE, 2 * numVisiblePages + 1);
  1300. this.#buffer.resize(newCacheSize, visible.ids);
  1301. this.renderingQueue.renderHighestPriority(visible);
  1302. const isSimpleLayout =
  1303. this._spreadMode === SpreadMode.NONE &&
  1304. (this._scrollMode === ScrollMode.PAGE ||
  1305. this._scrollMode === ScrollMode.VERTICAL);
  1306. const currentId = this._currentPageNumber;
  1307. let stillFullyVisible = false;
  1308. for (const page of visiblePages) {
  1309. if (page.percent < 100) {
  1310. break;
  1311. }
  1312. if (page.id === currentId && isSimpleLayout) {
  1313. stillFullyVisible = true;
  1314. break;
  1315. }
  1316. }
  1317. this._setCurrentPageNumber(
  1318. stillFullyVisible ? currentId : visiblePages[0].id
  1319. );
  1320. this._updateLocation(visible.first);
  1321. this.eventBus.dispatch("updateviewarea", {
  1322. source: this,
  1323. location: this._location,
  1324. });
  1325. }
  1326. containsElement(element) {
  1327. return this.container.contains(element);
  1328. }
  1329. focus() {
  1330. this.container.focus();
  1331. }
  1332. get _isContainerRtl() {
  1333. return getComputedStyle(this.container).direction === "rtl";
  1334. }
  1335. get isInPresentationMode() {
  1336. return this.presentationModeState === PresentationModeState.FULLSCREEN;
  1337. }
  1338. get isChangingPresentationMode() {
  1339. return this.presentationModeState === PresentationModeState.CHANGING;
  1340. }
  1341. get isHorizontalScrollbarEnabled() {
  1342. return this.isInPresentationMode
  1343. ? false
  1344. : this.container.scrollWidth > this.container.clientWidth;
  1345. }
  1346. get isVerticalScrollbarEnabled() {
  1347. return this.isInPresentationMode
  1348. ? false
  1349. : this.container.scrollHeight > this.container.clientHeight;
  1350. }
  1351. _getVisiblePages() {
  1352. const views =
  1353. this._scrollMode === ScrollMode.PAGE
  1354. ? this.#scrollModePageState.pages
  1355. : this._pages,
  1356. horizontal = this._scrollMode === ScrollMode.HORIZONTAL,
  1357. rtl = horizontal && this._isContainerRtl;
  1358. return getVisibleElements({
  1359. scrollEl: this.container,
  1360. views,
  1361. sortByVisibility: true,
  1362. horizontal,
  1363. rtl,
  1364. });
  1365. }
  1366. /**
  1367. * @param {number} pageNumber
  1368. */
  1369. isPageVisible(pageNumber) {
  1370. if (!this.pdfDocument) {
  1371. return false;
  1372. }
  1373. if (
  1374. !(
  1375. Number.isInteger(pageNumber) &&
  1376. pageNumber > 0 &&
  1377. pageNumber <= this.pagesCount
  1378. )
  1379. ) {
  1380. console.error(`isPageVisible: "${pageNumber}" is not a valid page.`);
  1381. return false;
  1382. }
  1383. return this._getVisiblePages().ids.has(pageNumber);
  1384. }
  1385. /**
  1386. * @param {number} pageNumber
  1387. */
  1388. isPageCached(pageNumber) {
  1389. if (!this.pdfDocument) {
  1390. return false;
  1391. }
  1392. if (
  1393. !(
  1394. Number.isInteger(pageNumber) &&
  1395. pageNumber > 0 &&
  1396. pageNumber <= this.pagesCount
  1397. )
  1398. ) {
  1399. console.error(`isPageCached: "${pageNumber}" is not a valid page.`);
  1400. return false;
  1401. }
  1402. const pageView = this._pages[pageNumber - 1];
  1403. return this.#buffer.has(pageView);
  1404. }
  1405. cleanup() {
  1406. for (const pageView of this._pages) {
  1407. if (pageView.renderingState !== RenderingStates.FINISHED) {
  1408. pageView.reset();
  1409. }
  1410. }
  1411. }
  1412. /**
  1413. * @private
  1414. */
  1415. _cancelRendering() {
  1416. for (const pageView of this._pages) {
  1417. pageView.cancelRendering();
  1418. }
  1419. }
  1420. /**
  1421. * @param {PDFPageView} pageView
  1422. * @returns {Promise<PDFPageProxy | null>}
  1423. */
  1424. async #ensurePdfPageLoaded(pageView) {
  1425. if (pageView.pdfPage) {
  1426. return pageView.pdfPage;
  1427. }
  1428. try {
  1429. const pdfPage = await this.pdfDocument.getPage(pageView.id);
  1430. if (!pageView.pdfPage) {
  1431. pageView.setPdfPage(pdfPage);
  1432. }
  1433. if (!this.linkService._cachedPageNumber?.(pdfPage.ref)) {
  1434. this.linkService.cachePageRef(pageView.id, pdfPage.ref);
  1435. }
  1436. return pdfPage;
  1437. } catch (reason) {
  1438. console.error("Unable to get page for page view", reason);
  1439. return null; // Page error -- there is nothing that can be done.
  1440. }
  1441. }
  1442. #getScrollAhead(visible) {
  1443. if (visible.first?.id === 1) {
  1444. return true;
  1445. } else if (visible.last?.id === this.pagesCount) {
  1446. return false;
  1447. }
  1448. switch (this._scrollMode) {
  1449. case ScrollMode.PAGE:
  1450. return this.#scrollModePageState.scrollDown;
  1451. case ScrollMode.HORIZONTAL:
  1452. return this.scroll.right;
  1453. }
  1454. return this.scroll.down;
  1455. }
  1456. forceRendering(currentlyVisiblePages) {
  1457. const visiblePages = currentlyVisiblePages || this._getVisiblePages();
  1458. const scrollAhead = this.#getScrollAhead(visiblePages);
  1459. const preRenderExtra =
  1460. this._spreadMode !== SpreadMode.NONE &&
  1461. this._scrollMode !== ScrollMode.HORIZONTAL;
  1462. const pageView = this.renderingQueue.getHighestPriority(
  1463. visiblePages,
  1464. this._pages,
  1465. scrollAhead,
  1466. preRenderExtra
  1467. );
  1468. if (pageView) {
  1469. this.#ensurePdfPageLoaded(pageView).then(() => {
  1470. this.renderingQueue.renderView(pageView);
  1471. });
  1472. return true;
  1473. }
  1474. return false;
  1475. }
  1476. /**
  1477. * @type {boolean} Whether all pages of the PDF document have identical
  1478. * widths and heights.
  1479. */
  1480. get hasEqualPageSizes() {
  1481. const firstPageView = this._pages[0];
  1482. for (let i = 1, ii = this._pages.length; i < ii; ++i) {
  1483. const pageView = this._pages[i];
  1484. if (
  1485. pageView.width !== firstPageView.width ||
  1486. pageView.height !== firstPageView.height
  1487. ) {
  1488. return false;
  1489. }
  1490. }
  1491. return true;
  1492. }
  1493. /**
  1494. * Returns sizes of the pages.
  1495. * @returns {Array} Array of objects with width/height/rotation fields.
  1496. */
  1497. getPagesOverview() {
  1498. return this._pages.map(pageView => {
  1499. const viewport = pageView.pdfPage.getViewport({ scale: 1 });
  1500. if (!this.enablePrintAutoRotate || isPortraitOrientation(viewport)) {
  1501. return {
  1502. width: viewport.width,
  1503. height: viewport.height,
  1504. rotation: viewport.rotation,
  1505. };
  1506. }
  1507. // Landscape orientation.
  1508. return {
  1509. width: viewport.height,
  1510. height: viewport.width,
  1511. rotation: (viewport.rotation - 90) % 360,
  1512. };
  1513. });
  1514. }
  1515. /**
  1516. * @type {Promise<OptionalContentConfig | null>}
  1517. */
  1518. get optionalContentConfigPromise() {
  1519. if (!this.pdfDocument) {
  1520. return Promise.resolve(null);
  1521. }
  1522. if (!this._optionalContentConfigPromise) {
  1523. console.error("optionalContentConfigPromise: Not initialized yet.");
  1524. // Prevent issues if the getter is accessed *before* the `onePageRendered`
  1525. // promise has resolved; won't (normally) happen in the default viewer.
  1526. return this.pdfDocument.getOptionalContentConfig();
  1527. }
  1528. return this._optionalContentConfigPromise;
  1529. }
  1530. /**
  1531. * @param {Promise<OptionalContentConfig>} promise - A promise that is
  1532. * resolved with an {@link OptionalContentConfig} instance.
  1533. */
  1534. set optionalContentConfigPromise(promise) {
  1535. if (!(promise instanceof Promise)) {
  1536. throw new Error(`Invalid optionalContentConfigPromise: ${promise}`);
  1537. }
  1538. if (!this.pdfDocument) {
  1539. return;
  1540. }
  1541. if (!this._optionalContentConfigPromise) {
  1542. // Ignore the setter *before* the `onePageRendered` promise has resolved,
  1543. // since it'll be overwritten anyway; won't happen in the default viewer.
  1544. return;
  1545. }
  1546. this._optionalContentConfigPromise = promise;
  1547. this.refresh(false, { optionalContentConfigPromise: promise });
  1548. this.eventBus.dispatch("optionalcontentconfigchanged", {
  1549. source: this,
  1550. promise,
  1551. });
  1552. }
  1553. /**
  1554. * @type {number} One of the values in {ScrollMode}.
  1555. */
  1556. get scrollMode() {
  1557. return this._scrollMode;
  1558. }
  1559. /**
  1560. * @param {number} mode - The direction in which the document pages should be
  1561. * laid out within the scrolling container.
  1562. * The constants from {ScrollMode} should be used.
  1563. */
  1564. set scrollMode(mode) {
  1565. if (this._scrollMode === mode) {
  1566. return; // The Scroll mode didn't change.
  1567. }
  1568. if (!isValidScrollMode(mode)) {
  1569. throw new Error(`Invalid scroll mode: ${mode}`);
  1570. }
  1571. if (this.pagesCount > PagesCountLimit.FORCE_SCROLL_MODE_PAGE) {
  1572. return; // Disabled for performance reasons.
  1573. }
  1574. this._previousScrollMode = this._scrollMode;
  1575. this._scrollMode = mode;
  1576. this.eventBus.dispatch("scrollmodechanged", { source: this, mode });
  1577. this._updateScrollMode(/* pageNumber = */ this._currentPageNumber);
  1578. }
  1579. _updateScrollMode(pageNumber = null) {
  1580. const scrollMode = this._scrollMode,
  1581. viewer = this.viewer;
  1582. viewer.classList.toggle(
  1583. "scrollHorizontal",
  1584. scrollMode === ScrollMode.HORIZONTAL
  1585. );
  1586. viewer.classList.toggle("scrollWrapped", scrollMode === ScrollMode.WRAPPED);
  1587. if (!this.pdfDocument || !pageNumber) {
  1588. return;
  1589. }
  1590. if (scrollMode === ScrollMode.PAGE) {
  1591. this.#ensurePageViewVisible();
  1592. } else if (this._previousScrollMode === ScrollMode.PAGE) {
  1593. // Ensure that the current spreadMode is still applied correctly when
  1594. // the *previous* scrollMode was `ScrollMode.PAGE`.
  1595. this._updateSpreadMode();
  1596. }
  1597. // Non-numeric scale values can be sensitive to the scroll orientation.
  1598. // Call this before re-scrolling to the current page, to ensure that any
  1599. // changes in scale don't move the current page.
  1600. if (this._currentScaleValue && isNaN(this._currentScaleValue)) {
  1601. this._setScale(this._currentScaleValue, { noScroll: true });
  1602. }
  1603. this._setCurrentPageNumber(pageNumber, /* resetCurrentPageView = */ true);
  1604. this.update();
  1605. }
  1606. /**
  1607. * @type {number} One of the values in {SpreadMode}.
  1608. */
  1609. get spreadMode() {
  1610. return this._spreadMode;
  1611. }
  1612. /**
  1613. * @param {number} mode - Group the pages in spreads, starting with odd- or
  1614. * even-number pages (unless `SpreadMode.NONE` is used).
  1615. * The constants from {SpreadMode} should be used.
  1616. */
  1617. set spreadMode(mode) {
  1618. if (this._spreadMode === mode) {
  1619. return; // The Spread mode didn't change.
  1620. }
  1621. if (!isValidSpreadMode(mode)) {
  1622. throw new Error(`Invalid spread mode: ${mode}`);
  1623. }
  1624. this._spreadMode = mode;
  1625. this.eventBus.dispatch("spreadmodechanged", { source: this, mode });
  1626. this._updateSpreadMode(/* pageNumber = */ this._currentPageNumber);
  1627. }
  1628. _updateSpreadMode(pageNumber = null) {
  1629. if (!this.pdfDocument) {
  1630. return;
  1631. }
  1632. const viewer = this.viewer,
  1633. pages = this._pages;
  1634. if (this._scrollMode === ScrollMode.PAGE) {
  1635. this.#ensurePageViewVisible();
  1636. } else {
  1637. // Temporarily remove all the pages from the DOM.
  1638. viewer.textContent = "";
  1639. if (this._spreadMode === SpreadMode.NONE) {
  1640. for (const pageView of this._pages) {
  1641. viewer.append(pageView.div);
  1642. }
  1643. } else {
  1644. const parity = this._spreadMode - 1;
  1645. let spread = null;
  1646. for (let i = 0, ii = pages.length; i < ii; ++i) {
  1647. if (spread === null) {
  1648. spread = document.createElement("div");
  1649. spread.className = "spread";
  1650. viewer.append(spread);
  1651. } else if (i % 2 === parity) {
  1652. spread = spread.cloneNode(false);
  1653. viewer.append(spread);
  1654. }
  1655. spread.append(pages[i].div);
  1656. }
  1657. }
  1658. }
  1659. if (!pageNumber) {
  1660. return;
  1661. }
  1662. // Non-numeric scale values can be sensitive to the scroll orientation.
  1663. // Call this before re-scrolling to the current page, to ensure that any
  1664. // changes in scale don't move the current page.
  1665. if (this._currentScaleValue && isNaN(this._currentScaleValue)) {
  1666. this._setScale(this._currentScaleValue, { noScroll: true });
  1667. }
  1668. this._setCurrentPageNumber(pageNumber, /* resetCurrentPageView = */ true);
  1669. this.update();
  1670. }
  1671. /**
  1672. * @private
  1673. */
  1674. _getPageAdvance(currentPageNumber, previous = false) {
  1675. switch (this._scrollMode) {
  1676. case ScrollMode.WRAPPED: {
  1677. const { views } = this._getVisiblePages(),
  1678. pageLayout = new Map();
  1679. // Determine the current (visible) page layout.
  1680. for (const { id, y, percent, widthPercent } of views) {
  1681. if (percent === 0 || widthPercent < 100) {
  1682. continue;
  1683. }
  1684. let yArray = pageLayout.get(y);
  1685. if (!yArray) {
  1686. pageLayout.set(y, (yArray ||= []));
  1687. }
  1688. yArray.push(id);
  1689. }
  1690. // Find the row of the current page.
  1691. for (const yArray of pageLayout.values()) {
  1692. const currentIndex = yArray.indexOf(currentPageNumber);
  1693. if (currentIndex === -1) {
  1694. continue;
  1695. }
  1696. const numPages = yArray.length;
  1697. if (numPages === 1) {
  1698. break;
  1699. }
  1700. // Handle documents with varying page sizes.
  1701. if (previous) {
  1702. for (let i = currentIndex - 1, ii = 0; i >= ii; i--) {
  1703. const currentId = yArray[i],
  1704. expectedId = yArray[i + 1] - 1;
  1705. if (currentId < expectedId) {
  1706. return currentPageNumber - expectedId;
  1707. }
  1708. }
  1709. } else {
  1710. for (let i = currentIndex + 1, ii = numPages; i < ii; i++) {
  1711. const currentId = yArray[i],
  1712. expectedId = yArray[i - 1] + 1;
  1713. if (currentId > expectedId) {
  1714. return expectedId - currentPageNumber;
  1715. }
  1716. }
  1717. }
  1718. // The current row is "complete", advance to the previous/next one.
  1719. if (previous) {
  1720. const firstId = yArray[0];
  1721. if (firstId < currentPageNumber) {
  1722. return currentPageNumber - firstId + 1;
  1723. }
  1724. } else {
  1725. const lastId = yArray[numPages - 1];
  1726. if (lastId > currentPageNumber) {
  1727. return lastId - currentPageNumber + 1;
  1728. }
  1729. }
  1730. break;
  1731. }
  1732. break;
  1733. }
  1734. case ScrollMode.HORIZONTAL: {
  1735. break;
  1736. }
  1737. case ScrollMode.PAGE:
  1738. case ScrollMode.VERTICAL: {
  1739. if (this._spreadMode === SpreadMode.NONE) {
  1740. break; // Normal vertical scrolling.
  1741. }
  1742. const parity = this._spreadMode - 1;
  1743. if (previous && currentPageNumber % 2 !== parity) {
  1744. break; // Left-hand side page.
  1745. } else if (!previous && currentPageNumber % 2 === parity) {
  1746. break; // Right-hand side page.
  1747. }
  1748. const { views } = this._getVisiblePages(),
  1749. expectedId = previous ? currentPageNumber - 1 : currentPageNumber + 1;
  1750. for (const { id, percent, widthPercent } of views) {
  1751. if (id !== expectedId) {
  1752. continue;
  1753. }
  1754. if (percent > 0 && widthPercent === 100) {
  1755. return 2;
  1756. }
  1757. break;
  1758. }
  1759. break;
  1760. }
  1761. }
  1762. return 1;
  1763. }
  1764. /**
  1765. * Go to the next page, taking scroll/spread-modes into account.
  1766. * @returns {boolean} Whether navigation occured.
  1767. */
  1768. nextPage() {
  1769. const currentPageNumber = this._currentPageNumber,
  1770. pagesCount = this.pagesCount;
  1771. if (currentPageNumber >= pagesCount) {
  1772. return false;
  1773. }
  1774. const advance =
  1775. this._getPageAdvance(currentPageNumber, /* previous = */ false) || 1;
  1776. this.currentPageNumber = Math.min(currentPageNumber + advance, pagesCount);
  1777. return true;
  1778. }
  1779. /**
  1780. * Go to the previous page, taking scroll/spread-modes into account.
  1781. * @returns {boolean} Whether navigation occured.
  1782. */
  1783. previousPage() {
  1784. const currentPageNumber = this._currentPageNumber;
  1785. if (currentPageNumber <= 1) {
  1786. return false;
  1787. }
  1788. const advance =
  1789. this._getPageAdvance(currentPageNumber, /* previous = */ true) || 1;
  1790. this.currentPageNumber = Math.max(currentPageNumber - advance, 1);
  1791. return true;
  1792. }
  1793. /**
  1794. * Increase the current zoom level one, or more, times.
  1795. * @param {number} [steps] - Defaults to zooming once.
  1796. * @param {Object|null} [options]
  1797. */
  1798. increaseScale(steps = 1, options = null) {
  1799. let newScale = this._currentScale;
  1800. do {
  1801. newScale = (newScale * DEFAULT_SCALE_DELTA).toFixed(2);
  1802. newScale = Math.ceil(newScale * 10) / 10;
  1803. newScale = Math.min(MAX_SCALE, newScale);
  1804. } while (--steps > 0 && newScale < MAX_SCALE);
  1805. options ||= Object.create(null);
  1806. options.noScroll = false;
  1807. this._setScale(newScale, options);
  1808. }
  1809. /**
  1810. * Decrease the current zoom level one, or more, times.
  1811. * @param {number} [steps] - Defaults to zooming once.
  1812. * @param {Object|null} [options]
  1813. */
  1814. decreaseScale(steps = 1, options = null) {
  1815. let newScale = this._currentScale;
  1816. do {
  1817. newScale = (newScale / DEFAULT_SCALE_DELTA).toFixed(2);
  1818. newScale = Math.floor(newScale * 10) / 10;
  1819. newScale = Math.max(MIN_SCALE, newScale);
  1820. } while (--steps > 0 && newScale > MIN_SCALE);
  1821. options ||= Object.create(null);
  1822. options.noScroll = false;
  1823. this._setScale(newScale, options);
  1824. }
  1825. #updateContainerHeightCss(height = this.container.clientHeight) {
  1826. if (height !== this.#previousContainerHeight) {
  1827. this.#previousContainerHeight = height;
  1828. docStyle.setProperty("--viewer-container-height", `${height}px`);
  1829. }
  1830. }
  1831. #resizeObserverCallback(entries) {
  1832. for (const entry of entries) {
  1833. if (entry.target === this.container) {
  1834. this.#updateContainerHeightCss(
  1835. Math.floor(entry.borderBoxSize[0].blockSize)
  1836. );
  1837. this.#containerTopLeft = null;
  1838. break;
  1839. }
  1840. }
  1841. }
  1842. get containerTopLeft() {
  1843. return (this.#containerTopLeft ||= [
  1844. this.container.offsetTop,
  1845. this.container.offsetLeft,
  1846. ]);
  1847. }
  1848. /**
  1849. * @type {number}
  1850. */
  1851. get annotationEditorMode() {
  1852. return this.#annotationEditorUIManager
  1853. ? this.#annotationEditorMode
  1854. : AnnotationEditorType.DISABLE;
  1855. }
  1856. /**
  1857. * @param {number} mode - AnnotationEditor mode (None, FreeText, Ink, ...)
  1858. */
  1859. set annotationEditorMode(mode) {
  1860. if (!this.#annotationEditorUIManager) {
  1861. throw new Error(`The AnnotationEditor is not enabled.`);
  1862. }
  1863. if (this.#annotationEditorMode === mode) {
  1864. return; // The AnnotationEditor mode didn't change.
  1865. }
  1866. if (!isValidAnnotationEditorMode(mode)) {
  1867. throw new Error(`Invalid AnnotationEditor mode: ${mode}`);
  1868. }
  1869. if (!this.pdfDocument) {
  1870. return;
  1871. }
  1872. this.#annotationEditorMode = mode;
  1873. this.eventBus.dispatch("annotationeditormodechanged", {
  1874. source: this,
  1875. mode,
  1876. });
  1877. this.#annotationEditorUIManager.updateMode(mode);
  1878. }
  1879. // eslint-disable-next-line accessor-pairs
  1880. set annotationEditorParams({ type, value }) {
  1881. if (!this.#annotationEditorUIManager) {
  1882. throw new Error(`The AnnotationEditor is not enabled.`);
  1883. }
  1884. this.#annotationEditorUIManager.updateParams(type, value);
  1885. }
  1886. refresh(noUpdate = false, updateArgs = Object.create(null)) {
  1887. if (!this.pdfDocument) {
  1888. return;
  1889. }
  1890. for (const pageView of this._pages) {
  1891. pageView.update(updateArgs);
  1892. }
  1893. if (this.#scaleTimeoutId !== null) {
  1894. clearTimeout(this.#scaleTimeoutId);
  1895. this.#scaleTimeoutId = null;
  1896. }
  1897. if (!noUpdate) {
  1898. this.update();
  1899. }
  1900. }
  1901. }
  1902. export { PagesCountLimit, PDFPageViewBuffer, PDFViewer };