2014-09-29 11:05:28 -05:00
|
|
|
/* 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.
|
|
|
|
|
*/
|
|
|
|
|
|
2021-12-14 23:59:17 +01:00
|
|
|
/** @typedef {import("../src/display/api").PDFPageProxy} PDFPageProxy */
|
|
|
|
|
// eslint-disable-next-line max-len
|
|
|
|
|
/** @typedef {import("../src/display/display_utils").PageViewport} PageViewport */
|
2023-08-31 12:01:33 +02:00
|
|
|
// eslint-disable-next-line max-len
|
|
|
|
|
/** @typedef {import("../src/display/annotation_storage").AnnotationStorage} AnnotationStorage */
|
2022-07-28 17:59:03 +02:00
|
|
|
// eslint-disable-next-line max-len
|
2025-01-21 23:26:20 +01:00
|
|
|
/** @typedef {import("./struct_tree_layer_builder.js").StructTreeLayerBuilder} StructTreeLayerBuilder */
|
|
|
|
|
// eslint-disable-next-line max-len
|
2023-08-31 12:01:33 +02:00
|
|
|
/** @typedef {import("./text_accessibility.js").TextAccessibilityManager} TextAccessibilityManager */
|
2024-04-09 15:25:49 +02:00
|
|
|
// eslint-disable-next-line max-len
|
|
|
|
|
/** @typedef {import("../src/display/editor/tools.js").AnnotationEditorUIManager} AnnotationEditorUIManager */
|
2025-09-08 18:55:29 +02:00
|
|
|
/** @typedef {import("./comment_manager.js").CommentManager} CommentManager */
|
2026-02-08 22:12:31 +01:00
|
|
|
/** @typedef {import("./pdf_link_service.js").PDFLinkService} PDFLinkService */
|
|
|
|
|
// eslint-disable-next-line max-len
|
|
|
|
|
/** @typedef {import("./base_download_manager.js").BaseDownloadManager} BaseDownloadManager */
|
2021-08-25 18:44:06 -04:00
|
|
|
|
2024-11-12 11:26:32 +05:30
|
|
|
import {
|
|
|
|
|
AnnotationLayer,
|
|
|
|
|
AnnotationType,
|
|
|
|
|
setLayerDimensions,
|
|
|
|
|
Util,
|
|
|
|
|
} from "pdfjs-lib";
|
2022-10-30 17:36:20 +01:00
|
|
|
import { PresentationModeState } from "./ui_utils.js";
|
2016-04-08 12:34:27 -05:00
|
|
|
|
2014-09-29 11:05:28 -05:00
|
|
|
/**
|
2015-12-17 12:54:53 +01:00
|
|
|
* @typedef {Object} AnnotationLayerBuilderOptions
|
2021-12-14 23:59:17 +01:00
|
|
|
* @property {PDFPageProxy} pdfPage
|
2020-07-31 16:13:26 +02:00
|
|
|
* @property {AnnotationStorage} [annotationStorage]
|
2019-10-12 16:30:32 +02:00
|
|
|
* @property {string} [imageResourcesPath] - Path for image resources, mainly
|
|
|
|
|
* for annotation icons. Include trailing slash.
|
2021-08-08 14:36:28 +02:00
|
|
|
* @property {boolean} renderForms
|
2026-01-30 15:50:37 +01:00
|
|
|
* @property {PDFLinkService} linkService
|
2026-01-30 15:51:39 +01:00
|
|
|
* @property {BaseDownloadManager} [downloadManager]
|
2025-08-20 19:04:03 +02:00
|
|
|
* @property {boolean} [enableComment]
|
2020-10-28 19:16:56 +01:00
|
|
|
* @property {boolean} [enableScripting]
|
|
|
|
|
* @property {Promise<boolean>} [hasJSActionsPromise]
|
2021-09-14 12:06:28 +02:00
|
|
|
* @property {Promise<Object<string, Array<Object>> | null>}
|
|
|
|
|
* [fieldObjectsPromise]
|
2021-12-14 23:59:17 +01:00
|
|
|
* @property {Map<string, HTMLCanvasElement>} [annotationCanvasMap]
|
2022-12-07 16:12:48 +01:00
|
|
|
* @property {TextAccessibilityManager} [accessibilityManager]
|
2024-04-09 15:25:49 +02:00
|
|
|
* @property {AnnotationEditorUIManager} [annotationEditorUIManager]
|
2024-03-11 17:03:44 +01:00
|
|
|
* @property {function} [onAppend]
|
2025-09-08 18:55:29 +02:00
|
|
|
* @property {CommentManager} [commentManager]
|
2014-09-29 11:05:28 -05:00
|
|
|
*/
|
|
|
|
|
|
2025-01-21 23:26:20 +01:00
|
|
|
/**
|
|
|
|
|
* @typedef {Object} AnnotationLayerBuilderRenderOptions
|
|
|
|
|
* @property {PageViewport} viewport
|
|
|
|
|
* @property {string} [intent] - The default value is "display".
|
|
|
|
|
* @property {StructTreeLayerBuilder} [structTreeLayer]
|
|
|
|
|
*/
|
|
|
|
|
|
2017-04-23 20:52:58 +02:00
|
|
|
class AnnotationLayerBuilder {
|
2024-11-12 11:26:32 +05:30
|
|
|
#annotations = null;
|
|
|
|
|
|
2025-09-08 18:55:29 +02:00
|
|
|
#commentManager = null;
|
|
|
|
|
|
2024-11-12 11:26:32 +05:30
|
|
|
#externalHide = false;
|
|
|
|
|
|
2024-03-11 17:03:44 +01:00
|
|
|
#onAppend = null;
|
|
|
|
|
|
2024-04-21 09:41:21 +02:00
|
|
|
#eventAbortController = null;
|
2022-10-30 17:36:20 +01:00
|
|
|
|
2025-02-12 01:30:38 +01:00
|
|
|
#linksInjected = false;
|
|
|
|
|
|
2014-09-29 11:05:28 -05:00
|
|
|
/**
|
2015-12-17 12:54:53 +01:00
|
|
|
* @param {AnnotationLayerBuilderOptions} options
|
2014-09-29 11:05:28 -05:00
|
|
|
*/
|
2017-06-30 12:55:22 +02:00
|
|
|
constructor({
|
|
|
|
|
pdfPage,
|
|
|
|
|
linkService,
|
|
|
|
|
downloadManager,
|
2020-07-31 16:13:26 +02:00
|
|
|
annotationStorage = null,
|
2018-02-13 13:17:11 +01:00
|
|
|
imageResourcesPath = "",
|
2021-08-08 14:36:28 +02:00
|
|
|
renderForms = true,
|
2025-08-20 19:04:03 +02:00
|
|
|
enableComment = false,
|
2025-09-08 18:55:29 +02:00
|
|
|
commentManager = null,
|
2020-10-28 19:16:56 +01:00
|
|
|
enableScripting = false,
|
|
|
|
|
hasJSActionsPromise = null,
|
2021-09-14 12:06:28 +02:00
|
|
|
fieldObjectsPromise = null,
|
2021-11-06 18:36:49 +01:00
|
|
|
annotationCanvasMap = null,
|
2022-07-28 17:59:03 +02:00
|
|
|
accessibilityManager = null,
|
2024-04-09 15:25:49 +02:00
|
|
|
annotationEditorUIManager = null,
|
2024-03-11 17:03:44 +01:00
|
|
|
onAppend = null,
|
2018-02-13 13:17:11 +01:00
|
|
|
}) {
|
2017-06-30 12:55:22 +02:00
|
|
|
this.pdfPage = pdfPage;
|
|
|
|
|
this.linkService = linkService;
|
|
|
|
|
this.downloadManager = downloadManager;
|
2018-02-13 13:17:11 +01:00
|
|
|
this.imageResourcesPath = imageResourcesPath;
|
2021-08-08 14:36:28 +02:00
|
|
|
this.renderForms = renderForms;
|
2020-07-22 13:55:52 +02:00
|
|
|
this.annotationStorage = annotationStorage;
|
2025-08-20 19:04:03 +02:00
|
|
|
this.enableComment = enableComment;
|
2025-09-08 18:55:29 +02:00
|
|
|
this.#commentManager = commentManager;
|
2020-10-28 19:16:56 +01:00
|
|
|
this.enableScripting = enableScripting;
|
2022-12-11 14:51:28 +01:00
|
|
|
this._hasJSActionsPromise = hasJSActionsPromise || Promise.resolve(false);
|
|
|
|
|
this._fieldObjectsPromise = fieldObjectsPromise || Promise.resolve(null);
|
2021-11-06 18:36:49 +01:00
|
|
|
this._annotationCanvasMap = annotationCanvasMap;
|
2022-07-28 17:59:03 +02:00
|
|
|
this._accessibilityManager = accessibilityManager;
|
2024-04-09 15:25:49 +02:00
|
|
|
this._annotationEditorUIManager = annotationEditorUIManager;
|
2024-03-11 17:03:44 +01:00
|
|
|
this.#onAppend = onAppend;
|
2014-09-29 11:05:28 -05:00
|
|
|
|
2023-06-01 20:31:05 +02:00
|
|
|
this.annotationLayer = null;
|
2014-09-29 11:05:28 -05:00
|
|
|
this.div = null;
|
2017-10-06 17:26:54 +02:00
|
|
|
this._cancelled = false;
|
2022-10-30 17:36:20 +01:00
|
|
|
this._eventBus = linkService.eventBus;
|
2014-09-29 11:05:28 -05:00
|
|
|
}
|
2015-12-03 00:20:18 +01:00
|
|
|
|
2017-04-23 20:52:58 +02:00
|
|
|
/**
|
2025-01-21 23:26:20 +01:00
|
|
|
* @param {AnnotationLayerBuilderRenderOptions} options
|
2020-08-17 13:19:03 -07:00
|
|
|
* @returns {Promise<void>} A promise that is resolved when rendering of the
|
|
|
|
|
* annotations is complete.
|
2017-04-23 20:52:58 +02:00
|
|
|
*/
|
2025-01-21 23:26:20 +01:00
|
|
|
async render({ viewport, intent = "display", structTreeLayer = null }) {
|
2022-12-11 14:51:28 +01:00
|
|
|
if (this.div) {
|
2023-06-01 20:31:05 +02:00
|
|
|
if (this._cancelled || !this.annotationLayer) {
|
2022-12-11 14:51:28 +01:00
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
// If an annotationLayer already exists, refresh its children's
|
|
|
|
|
// transformation matrices.
|
2023-06-01 20:31:05 +02:00
|
|
|
this.annotationLayer.update({
|
2022-12-11 14:51:28 +01:00
|
|
|
viewport: viewport.clone({ dontFlip: true }),
|
|
|
|
|
});
|
2021-09-14 12:06:28 +02:00
|
|
|
return;
|
|
|
|
|
}
|
2014-09-29 11:05:28 -05:00
|
|
|
|
2022-12-11 14:51:28 +01:00
|
|
|
const [annotations, hasJSActions, fieldObjects] = await Promise.all([
|
|
|
|
|
this.pdfPage.getAnnotations({ intent }),
|
|
|
|
|
this._hasJSActionsPromise,
|
|
|
|
|
this._fieldObjectsPromise,
|
|
|
|
|
]);
|
|
|
|
|
if (this._cancelled) {
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Create an annotation layer div and render the annotations
|
|
|
|
|
// if there is at least one annotation.
|
2023-06-01 20:31:05 +02:00
|
|
|
const div = (this.div = document.createElement("div"));
|
|
|
|
|
div.className = "annotationLayer";
|
2024-03-11 17:03:44 +01:00
|
|
|
this.#onAppend?.(div);
|
2025-09-30 18:41:45 +02:00
|
|
|
this.#initAnnotationLayer(viewport, structTreeLayer);
|
2022-12-11 14:51:28 +01:00
|
|
|
|
2023-06-01 20:31:05 +02:00
|
|
|
if (annotations.length === 0) {
|
2024-11-12 11:26:32 +05:30
|
|
|
this.#annotations = annotations;
|
2025-09-30 18:41:45 +02:00
|
|
|
setLayerDimensions(this.div, viewport);
|
2022-12-11 14:51:28 +01:00
|
|
|
return;
|
|
|
|
|
}
|
2023-06-01 20:31:05 +02:00
|
|
|
|
2023-06-20 16:46:51 +02:00
|
|
|
await this.annotationLayer.render({
|
2021-09-14 12:06:28 +02:00
|
|
|
annotations,
|
|
|
|
|
imageResourcesPath: this.imageResourcesPath,
|
|
|
|
|
renderForms: this.renderForms,
|
|
|
|
|
downloadManager: this.downloadManager,
|
2025-08-20 19:04:03 +02:00
|
|
|
enableComment: this.enableComment,
|
2021-09-14 12:06:28 +02:00
|
|
|
enableScripting: this.enableScripting,
|
|
|
|
|
hasJSActions,
|
|
|
|
|
fieldObjects,
|
2022-12-11 14:51:28 +01:00
|
|
|
});
|
2017-04-23 20:52:58 +02:00
|
|
|
|
2024-11-12 11:26:32 +05:30
|
|
|
this.#annotations = annotations;
|
|
|
|
|
|
2022-12-11 14:51:28 +01:00
|
|
|
// Ensure that interactive form elements in the annotationLayer are
|
|
|
|
|
// disabled while PresentationMode is active (see issue 12232).
|
|
|
|
|
if (this.linkService.isInPresentationMode) {
|
|
|
|
|
this.#updatePresentationModeState(PresentationModeState.FULLSCREEN);
|
|
|
|
|
}
|
2024-04-21 09:41:21 +02:00
|
|
|
if (!this.#eventAbortController) {
|
|
|
|
|
this.#eventAbortController = new AbortController();
|
|
|
|
|
|
2022-12-11 14:51:28 +01:00
|
|
|
this._eventBus?._on(
|
|
|
|
|
"presentationmodechanged",
|
2024-04-21 09:41:21 +02:00
|
|
|
evt => {
|
|
|
|
|
this.#updatePresentationModeState(evt.state);
|
|
|
|
|
},
|
|
|
|
|
{ signal: this.#eventAbortController.signal }
|
2022-12-11 14:51:28 +01:00
|
|
|
);
|
2021-09-14 12:06:28 +02:00
|
|
|
}
|
2017-04-23 20:52:58 +02:00
|
|
|
}
|
2015-12-03 00:20:18 +01:00
|
|
|
|
2024-11-12 11:26:32 +05:30
|
|
|
#initAnnotationLayer(viewport, structTreeLayer) {
|
|
|
|
|
this.annotationLayer = new AnnotationLayer({
|
|
|
|
|
div: this.div,
|
|
|
|
|
accessibilityManager: this._accessibilityManager,
|
|
|
|
|
annotationCanvasMap: this._annotationCanvasMap,
|
|
|
|
|
annotationEditorUIManager: this._annotationEditorUIManager,
|
2025-09-30 18:41:45 +02:00
|
|
|
annotationStorage: this.annotationStorage,
|
2024-11-12 11:26:32 +05:30
|
|
|
page: this.pdfPage,
|
|
|
|
|
viewport: viewport.clone({ dontFlip: true }),
|
|
|
|
|
structTreeLayer,
|
2025-09-08 18:55:29 +02:00
|
|
|
commentManager: this.#commentManager,
|
2025-09-30 18:41:45 +02:00
|
|
|
linkService: this.linkService,
|
2024-11-12 11:26:32 +05:30
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
2017-10-06 17:26:54 +02:00
|
|
|
cancel() {
|
|
|
|
|
this._cancelled = true;
|
2022-10-30 17:36:20 +01:00
|
|
|
|
2024-04-21 09:41:21 +02:00
|
|
|
this.#eventAbortController?.abort();
|
|
|
|
|
this.#eventAbortController = null;
|
2017-10-06 17:26:54 +02:00
|
|
|
}
|
|
|
|
|
|
2024-11-12 11:26:32 +05:30
|
|
|
hide(internal = false) {
|
|
|
|
|
this.#externalHide = !internal;
|
2017-04-23 20:52:58 +02:00
|
|
|
if (!this.div) {
|
|
|
|
|
return;
|
|
|
|
|
}
|
2021-02-08 00:21:49 +01:00
|
|
|
this.div.hidden = true;
|
2017-04-23 20:52:58 +02:00
|
|
|
}
|
2022-10-30 17:36:20 +01:00
|
|
|
|
2024-05-21 14:41:07 +02:00
|
|
|
hasEditableAnnotations() {
|
|
|
|
|
return !!this.annotationLayer?.hasEditableAnnotations();
|
|
|
|
|
}
|
|
|
|
|
|
2024-11-12 11:26:32 +05:30
|
|
|
/**
|
2025-09-30 18:41:45 +02:00
|
|
|
* @param {Array<Object>} inferredLinks
|
2024-11-12 11:26:32 +05:30
|
|
|
* @returns {Promise<void>} A promise that is resolved when the inferred links
|
|
|
|
|
* are added to the annotation layer.
|
|
|
|
|
*/
|
2025-09-30 18:41:45 +02:00
|
|
|
async injectLinkAnnotations(inferredLinks) {
|
2024-11-12 11:26:32 +05:30
|
|
|
if (this.#annotations === null) {
|
|
|
|
|
throw new Error(
|
|
|
|
|
"`render` method must be called before `injectLinkAnnotations`."
|
|
|
|
|
);
|
|
|
|
|
}
|
2025-02-12 01:30:38 +01:00
|
|
|
if (this._cancelled || this.#linksInjected) {
|
2024-11-12 11:26:32 +05:30
|
|
|
return;
|
|
|
|
|
}
|
2025-02-12 01:30:38 +01:00
|
|
|
this.#linksInjected = true;
|
2024-11-12 11:26:32 +05:30
|
|
|
|
|
|
|
|
const newLinks = this.#annotations.length
|
|
|
|
|
? this.#checkInferredLinks(inferredLinks)
|
|
|
|
|
: inferredLinks;
|
|
|
|
|
|
|
|
|
|
if (!newLinks.length) {
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
2025-09-30 18:41:45 +02:00
|
|
|
await this.annotationLayer.addLinkAnnotations(newLinks);
|
2024-11-12 11:26:32 +05:30
|
|
|
// Don't show the annotation layer if it was explicitly hidden previously.
|
|
|
|
|
if (!this.#externalHide) {
|
|
|
|
|
this.div.hidden = false;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2022-10-30 17:36:20 +01:00
|
|
|
#updatePresentationModeState(state) {
|
|
|
|
|
if (!this.div) {
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
let disableFormElements = false;
|
|
|
|
|
|
|
|
|
|
switch (state) {
|
|
|
|
|
case PresentationModeState.FULLSCREEN:
|
|
|
|
|
disableFormElements = true;
|
|
|
|
|
break;
|
|
|
|
|
case PresentationModeState.NORMAL:
|
|
|
|
|
break;
|
|
|
|
|
default:
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
for (const section of this.div.childNodes) {
|
|
|
|
|
if (section.hasAttribute("data-internal-link")) {
|
|
|
|
|
continue;
|
|
|
|
|
}
|
|
|
|
|
section.inert = disableFormElements;
|
|
|
|
|
}
|
|
|
|
|
}
|
2024-11-12 11:26:32 +05:30
|
|
|
|
|
|
|
|
#checkInferredLinks(inferredLinks) {
|
|
|
|
|
function annotationRects(annot) {
|
|
|
|
|
if (!annot.quadPoints) {
|
|
|
|
|
return [annot.rect];
|
|
|
|
|
}
|
|
|
|
|
const rects = [];
|
|
|
|
|
for (let i = 2, ii = annot.quadPoints.length; i < ii; i += 8) {
|
|
|
|
|
const trX = annot.quadPoints[i];
|
|
|
|
|
const trY = annot.quadPoints[i + 1];
|
|
|
|
|
const blX = annot.quadPoints[i + 2];
|
|
|
|
|
const blY = annot.quadPoints[i + 3];
|
|
|
|
|
rects.push([blX, blY, trX, trY]);
|
|
|
|
|
}
|
|
|
|
|
return rects;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
function intersectAnnotations(annot1, annot2) {
|
|
|
|
|
const intersections = [];
|
|
|
|
|
const annot1Rects = annotationRects(annot1);
|
|
|
|
|
const annot2Rects = annotationRects(annot2);
|
|
|
|
|
for (const rect1 of annot1Rects) {
|
|
|
|
|
for (const rect2 of annot2Rects) {
|
|
|
|
|
const intersection = Util.intersect(rect1, rect2);
|
|
|
|
|
if (intersection) {
|
|
|
|
|
intersections.push(intersection);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
return intersections;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
function areaRects(rects) {
|
|
|
|
|
let totalArea = 0;
|
|
|
|
|
for (const rect of rects) {
|
|
|
|
|
totalArea += Math.abs((rect[2] - rect[0]) * (rect[3] - rect[1]));
|
|
|
|
|
}
|
|
|
|
|
return totalArea;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return inferredLinks.filter(link => {
|
|
|
|
|
let linkAreaRects;
|
|
|
|
|
|
|
|
|
|
for (const annotation of this.#annotations) {
|
|
|
|
|
if (
|
|
|
|
|
annotation.annotationType !== AnnotationType.LINK ||
|
2025-02-08 17:07:03 +01:00
|
|
|
!annotation.url
|
2024-11-12 11:26:32 +05:30
|
|
|
) {
|
|
|
|
|
continue;
|
|
|
|
|
}
|
|
|
|
|
// TODO: Add a test case to verify that we can find the intersection
|
|
|
|
|
// between two annotations with quadPoints properly.
|
|
|
|
|
const intersections = intersectAnnotations(annotation, link);
|
|
|
|
|
|
|
|
|
|
if (intersections.length === 0) {
|
|
|
|
|
continue;
|
|
|
|
|
}
|
|
|
|
|
linkAreaRects ??= areaRects(annotationRects(link));
|
|
|
|
|
|
|
|
|
|
if (
|
|
|
|
|
areaRects(intersections) / linkAreaRects >
|
|
|
|
|
0.5 /* If the overlap is more than 50%. */
|
|
|
|
|
) {
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
return true;
|
|
|
|
|
});
|
|
|
|
|
}
|
2017-04-23 20:52:58 +02:00
|
|
|
}
|
2014-12-17 14:47:14 -06:00
|
|
|
|
2021-12-15 13:54:29 +01:00
|
|
|
export { AnnotationLayerBuilder };
|