/* Copyright 2014 Mozilla Foundation * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ /** @typedef {import("../src/display/api").PDFDocumentProxy} PDFDocumentProxy */ /** @typedef {import("../src/display/api").PDFPageProxy} PDFPageProxy */ // eslint-disable-next-line max-len /** @typedef {import("../src/display/display_utils").PageViewport} PageViewport */ // eslint-disable-next-line max-len /** @typedef {import("../src/display/optional_content_config").OptionalContentConfig} OptionalContentConfig */ /** @typedef {import("./event_utils").EventBus} EventBus */ /** @typedef {import("./interfaces").IDownloadManager} IDownloadManager */ /** @typedef {import("./interfaces").IL10n} IL10n */ /** @typedef {import("./interfaces").IPDFLinkService} IPDFLinkService */ // eslint-disable-next-line max-len /** @typedef {import("./pdf_find_controller").PDFFindController} PDFFindController */ // eslint-disable-next-line max-len /** @typedef {import("./pdf_scripting_manager").PDFScriptingManager} PDFScriptingManager */ import { AnnotationEditorType, AnnotationEditorUIManager, AnnotationMode, PermissionFlag, PixelsPerInch, shadow, version, } from "pdfjs-lib"; import { DEFAULT_SCALE, DEFAULT_SCALE_DELTA, DEFAULT_SCALE_VALUE, docStyle, getVisibleElements, isPortraitOrientation, isValidRotation, isValidScrollMode, isValidSpreadMode, MAX_AUTO_SCALE, MAX_SCALE, MIN_SCALE, PresentationModeState, removeNullCharacters, RenderingStates, SCROLLBAR_PADDING, scrollIntoView, ScrollMode, SpreadMode, TextLayerMode, UNKNOWN_SCALE, VERTICAL_PADDING, watchScroll, } from "./ui_utils.js"; import { GenericL10n } from "web-null_l10n"; import { PDFPageView } from "./pdf_page_view.js"; import { PDFRenderingQueue } from "./pdf_rendering_queue.js"; import { SimpleLinkService } from "./pdf_link_service.js"; const DEFAULT_CACHE_SIZE = 10; const PagesCountLimit = { FORCE_SCROLL_MODE_PAGE: 15000, FORCE_LAZY_PAGE_INIT: 7500, PAUSE_EAGER_PAGE_INIT: 250, }; function isValidAnnotationEditorMode(mode) { return ( Object.values(AnnotationEditorType).includes(mode) && mode !== AnnotationEditorType.DISABLE ); } /** * @typedef {Object} PDFViewerOptions * @property {HTMLDivElement} container - The container for the viewer element. * @property {HTMLDivElement} [viewer] - The viewer element. * @property {EventBus} eventBus - The application event bus. * @property {IPDFLinkService} [linkService] - The navigation/linking service. * @property {IDownloadManager} [downloadManager] - The download manager * component. * @property {PDFFindController} [findController] - The find controller * component. * @property {PDFScriptingManager} [scriptingManager] - The scripting manager * component. * @property {PDFRenderingQueue} [renderingQueue] - The rendering queue object. * @property {boolean} [removePageBorders] - Removes the border shadow around * the pages. The default value is `false`. * @property {number} [textLayerMode] - Controls if the text layer used for * selection and searching is created. The constants from {TextLayerMode} * should be used. The default value is `TextLayerMode.ENABLE`. * @property {number} [annotationMode] - Controls if the annotation layer is * created, and if interactive form elements or `AnnotationStorage`-data are * being rendered. The constants from {@link AnnotationMode} should be used; * see also {@link RenderParameters} and {@link GetOperatorListParameters}. * The default value is `AnnotationMode.ENABLE_FORMS`. * @property {number} [annotationEditorMode] - Enables the creation and editing * of new Annotations. The constants from {@link AnnotationEditorType} should * be used. The default value is `AnnotationEditorType.NONE`. * @property {string} [annotationEditorHighlightColors] - A comma separated list * of colors to propose to highlight some text in the pdf. * @property {string} [imageResourcesPath] - Path for image resources, mainly * mainly for annotation icons. Include trailing slash. * @property {boolean} [enablePrintAutoRotate] - Enables automatic rotation of * landscape pages upon printing. The default is `false`. * @property {number} [maxCanvasPixels] - The maximum supported canvas size in * total pixels, i.e. width * height. Use `-1` for no limit, or `0` for * CSS-only zooming. The default value is 4096 * 8192 (32 mega-pixels). * @property {IL10n} [l10n] - Localization service. * @property {boolean} [enablePermissions] - Enables PDF document permissions, * when they exist. The default value is `false`. * @property {Object} [pageColors] - Overwrites background and foreground colors * with user defined ones in order to improve readability in high contrast * mode. */ class PDFPageViewBuffer { // Here we rely on the fact that `Set`s preserve the insertion order. #buf = new Set(); #size = 0; constructor(size) { this.#size = size; } push(view) { const buf = this.#buf; if (buf.has(view)) { buf.delete(view); // Move the view to the "end" of the buffer. } buf.add(view); if (buf.size > this.#size) { this.#destroyFirstView(); } } /** * After calling resize, the size of the buffer will be `newSize`. * The optional parameter `idsToKeep` is, if present, a Set of page-ids to * push to the back of the buffer, delaying their destruction. The size of * `idsToKeep` has no impact on the final size of the buffer; if `idsToKeep` * is larger than `newSize`, some of those pages will be destroyed anyway. */ resize(newSize, idsToKeep = null) { this.#size = newSize; const buf = this.#buf; if (idsToKeep) { const ii = buf.size; let i = 1; for (const view of buf) { if (idsToKeep.has(view.id)) { buf.delete(view); // Move the view to the "end" of the buffer. buf.add(view); } if (++i > ii) { break; } } } while (buf.size > this.#size) { this.#destroyFirstView(); } } has(view) { return this.#buf.has(view); } [Symbol.iterator]() { return this.#buf.keys(); } #destroyFirstView() { const firstView = this.#buf.keys().next().value; firstView?.destroy(); this.#buf.delete(firstView); } } /** * Simple viewer control to display PDF content/pages. */ class PDFViewer { #buffer = null; #altTextManager = null; #annotationEditorHighlightColors = null; #annotationEditorMode = AnnotationEditorType.NONE; #annotationEditorUIManager = null; #annotationMode = AnnotationMode.ENABLE_FORMS; #containerTopLeft = null; #enableHighlightFloatingButton = false; #enablePermissions = false; #eventAbortController = null; #mlManager = null; #getAllTextInProgress = false; #hiddenCopyElement = null; #interruptCopyCondition = false; #previousContainerHeight = 0; #resizeObserver = new ResizeObserver(this.#resizeObserverCallback.bind(this)); #scrollModePageState = null; #scaleTimeoutId = null; #textLayerMode = TextLayerMode.ENABLE; /** * @param {PDFViewerOptions} options */ constructor(options) { const viewerVersion = typeof PDFJSDev !== "undefined" ? PDFJSDev.eval("BUNDLE_VERSION") : null; if (version !== viewerVersion) { throw new Error( `The API version "${version}" does not match the Viewer version "${viewerVersion}".` ); } this.container = options.container; this.viewer = options.viewer || options.container.firstElementChild; if (typeof PDFJSDev === "undefined" || PDFJSDev.test("GENERIC")) { if (this.container?.tagName !== "DIV" || this.viewer?.tagName !== "DIV") { throw new Error("Invalid `container` and/or `viewer` option."); } if ( this.container.offsetParent && getComputedStyle(this.container).position !== "absolute" ) { throw new Error("The `container` must be absolutely positioned."); } } this.#resizeObserver.observe(this.container); this.eventBus = options.eventBus; this.linkService = options.linkService || new SimpleLinkService(); this.downloadManager = options.downloadManager || null; this.findController = options.findController || null; this.#altTextManager = options.altTextManager || null; if (this.findController) { this.findController.onIsPageVisible = pageNumber => this._getVisiblePages().ids.has(pageNumber); } this._scriptingManager = options.scriptingManager || null; this.#textLayerMode = options.textLayerMode ?? TextLayerMode.ENABLE; this.#annotationMode = options.annotationMode ?? AnnotationMode.ENABLE_FORMS; this.#annotationEditorMode = options.annotationEditorMode ?? AnnotationEditorType.NONE; this.#annotationEditorHighlightColors = options.annotationEditorHighlightColors || null; this.#enableHighlightFloatingButton = options.enableHighlightFloatingButton === true; this.imageResourcesPath = options.imageResourcesPath || ""; this.enablePrintAutoRotate = options.enablePrintAutoRotate || false; if (typeof PDFJSDev === "undefined" || PDFJSDev.test("GENERIC")) { this.removePageBorders = options.removePageBorders || false; } this.maxCanvasPixels = options.maxCanvasPixels; this.l10n = options.l10n; if (typeof PDFJSDev === "undefined" || PDFJSDev.test("GENERIC")) { this.l10n ||= new GenericL10n(); } this.#enablePermissions = options.enablePermissions || false; this.pageColors = options.pageColors || null; this.#mlManager = options.mlManager || null; this.defaultRenderingQueue = !options.renderingQueue; if ( (typeof PDFJSDev === "undefined" || PDFJSDev.test("GENERIC")) && this.defaultRenderingQueue ) { // Custom rendering queue is not specified, using default one this.renderingQueue = new PDFRenderingQueue(); this.renderingQueue.setViewer(this); } else { this.renderingQueue = options.renderingQueue; } this.scroll = watchScroll(this.container, this._scrollUpdate.bind(this)); this.presentationModeState = PresentationModeState.UNKNOWN; this._resetView(); if ( (typeof PDFJSDev === "undefined" || PDFJSDev.test("GENERIC")) && this.removePageBorders ) { this.viewer.classList.add("removePageBorders"); } this.#updateContainerHeightCss(); // Trigger API-cleanup, once thumbnail rendering has finished, // if the relevant pageView is *not* cached in the buffer. this.eventBus._on("thumbnailrendered", ({ pageNumber, pdfPage }) => { const pageView = this._pages[pageNumber - 1]; if (!this.#buffer.has(pageView)) { pdfPage?.cleanup(); } }); if ( (typeof PDFJSDev === "undefined" || PDFJSDev.test("GENERIC")) && !options.l10n ) { // Ensure that Fluent is connected in e.g. the COMPONENTS build. this.l10n.translate(this.container); } } get pagesCount() { return this._pages.length; } getPageView(index) { return this._pages[index]; } getCachedPageViews() { return new Set(this.#buffer); } /** * @type {boolean} - True if all {PDFPageView} objects are initialized. */ get pageViewsReady() { // Prevent printing errors when 'disableAutoFetch' is set, by ensuring // that *all* pages have in fact been completely loaded. return this._pages.every(pageView => pageView?.pdfPage); } /** * @type {boolean} */ get renderForms() { return this.#annotationMode === AnnotationMode.ENABLE_FORMS; } /** * @type {boolean} */ get enableScripting() { return !!this._scriptingManager; } /** * @type {number} */ get currentPageNumber() { return this._currentPageNumber; } /** * @param {number} val - The page number. */ set currentPageNumber(val) { if (!Number.isInteger(val)) { throw new Error("Invalid page number."); } if (!this.pdfDocument) { return; } // The intent can be to just reset a scroll position and/or scale. if (!this._setCurrentPageNumber(val, /* resetCurrentPageView = */ true)) { console.error(`currentPageNumber: "${val}" is not a valid page.`); } } /** * @returns {boolean} Whether the pageNumber is valid (within bounds). * @private */ _setCurrentPageNumber(val, resetCurrentPageView = false) { if (this._currentPageNumber === val) { if (resetCurrentPageView) { this.#resetCurrentPageView(); } return true; } if (!(0 < val && val <= this.pagesCount)) { return false; } const previous = this._currentPageNumber; this._currentPageNumber = val; this.eventBus.dispatch("pagechanging", { source: this, pageNumber: val, pageLabel: this._pageLabels?.[val - 1] ?? null, previous, }); if (resetCurrentPageView) { this.#resetCurrentPageView(); } return true; } /** * @type {string|null} Returns the current page label, or `null` if no page * labels exist. */ get currentPageLabel() { return this._pageLabels?.[this._currentPageNumber - 1] ?? null; } /** * @param {string} val - The page label. */ set currentPageLabel(val) { if (!this.pdfDocument) { return; } let page = val | 0; // Fallback page number. if (this._pageLabels) { const i = this._pageLabels.indexOf(val); if (i >= 0) { page = i + 1; } } // The intent can be to just reset a scroll position and/or scale. if (!this._setCurrentPageNumber(page, /* resetCurrentPageView = */ true)) { console.error(`currentPageLabel: "${val}" is not a valid page.`); } } /** * @type {number} */ get currentScale() { return this._currentScale !== UNKNOWN_SCALE ? this._currentScale : DEFAULT_SCALE; } /** * @param {number} val - Scale of the pages in percents. */ set currentScale(val) { if (isNaN(val)) { throw new Error("Invalid numeric scale."); } if (!this.pdfDocument) { return; } this.#setScale(val, { noScroll: false }); } /** * @type {string} */ get currentScaleValue() { return this._currentScaleValue; } /** * @param val - The scale of the pages (in percent or predefined value). */ set currentScaleValue(val) { if (!this.pdfDocument) { return; } this.#setScale(val, { noScroll: false }); } /** * @type {number} */ get pagesRotation() { return this._pagesRotation; } /** * @param {number} rotation - The rotation of the pages (0, 90, 180, 270). */ set pagesRotation(rotation) { if (!isValidRotation(rotation)) { throw new Error("Invalid pages rotation angle."); } if (!this.pdfDocument) { return; } // Normalize the rotation, by clamping it to the [0, 360) range. rotation %= 360; if (rotation < 0) { rotation += 360; } if (this._pagesRotation === rotation) { return; // The rotation didn't change. } this._pagesRotation = rotation; const pageNumber = this._currentPageNumber; this.refresh(true, { rotation }); // Prevent errors in case the rotation changes *before* the scale has been // set to a non-default value. if (this._currentScaleValue) { this.#setScale(this._currentScaleValue, { noScroll: true }); } this.eventBus.dispatch("rotationchanging", { source: this, pagesRotation: rotation, pageNumber, }); if (this.defaultRenderingQueue) { this.update(); } } get firstPagePromise() { return this.pdfDocument ? this._firstPageCapability.promise : null; } get onePageRendered() { return this.pdfDocument ? this._onePageRenderedCapability.promise : null; } get pagesPromise() { return this.pdfDocument ? this._pagesCapability.promise : null; } get _layerProperties() { const self = this; return shadow(this, "_layerProperties", { get annotationEditorUIManager() { return self.#annotationEditorUIManager; }, get annotationStorage() { return self.pdfDocument?.annotationStorage; }, get downloadManager() { return self.downloadManager; }, get enableScripting() { return !!self._scriptingManager; }, get fieldObjectsPromise() { return self.pdfDocument?.getFieldObjects(); }, get findController() { return self.findController; }, get hasJSActionsPromise() { return self.pdfDocument?.hasJSActions(); }, get linkService() { return self.linkService; }, }); } /** * Currently only *some* permissions are supported. * @returns {Object} */ #initializePermissions(permissions) { const params = { annotationEditorMode: this.#annotationEditorMode, annotationMode: this.#annotationMode, textLayerMode: this.#textLayerMode, }; if (!permissions) { return params; } if ( !permissions.includes(PermissionFlag.COPY) && this.#textLayerMode === TextLayerMode.ENABLE ) { params.textLayerMode = TextLayerMode.ENABLE_PERMISSIONS; } if (!permissions.includes(PermissionFlag.MODIFY_CONTENTS)) { params.annotationEditorMode = AnnotationEditorType.DISABLE; } if ( !permissions.includes(PermissionFlag.MODIFY_ANNOTATIONS) && !permissions.includes(PermissionFlag.FILL_INTERACTIVE_FORMS) && this.#annotationMode === AnnotationMode.ENABLE_FORMS ) { params.annotationMode = AnnotationMode.ENABLE; } return params; } async #onePageRenderedOrForceFetch(signal) { // Unless the viewer *and* its pages are visible, rendering won't start and // `this._onePageRenderedCapability` thus won't be resolved. // To ensure that automatic printing, on document load, still works even in // those cases we force-allow fetching of all pages when: // - The current window/tab is inactive, which will prevent rendering since // `requestAnimationFrame` is being used; fixes bug 1746213. // - The viewer is hidden in the DOM, e.g. in a `display: none`