From fba5f3c2378f099d1649aaf12f66a7d2b6050447 Mon Sep 17 00:00:00 2001 From: Jonas Jenwald Date: Mon, 10 Feb 2025 10:03:28 +0100 Subject: [PATCH] Create the `borderStyle` of inferred links lazily (PR 19110 follow-up) Given that most inferred links will overlap existing LinkAnnotations, creating a lot of unused `borderStyle` objects seem unnecessary. Hence we can move that into the `AnnotationLayer.prototype.addLinkAnnotations` method instead, which also allows us to slightly reduce the API-surface. --- src/display/annotation_layer.js | 20 ++++++++++++++++++++ src/pdf.js | 2 -- test/unit/pdf_spec.js | 2 -- web/autolinker.js | 19 ++++--------------- web/pdfjs.js | 2 -- 5 files changed, 24 insertions(+), 21 deletions(-) diff --git a/src/display/annotation_layer.js b/src/display/annotation_layer.js index 0937dc566..8db5d994f 100644 --- a/src/display/annotation_layer.js +++ b/src/display/annotation_layer.js @@ -3254,6 +3254,8 @@ class AnnotationLayer { parent: this, }; for (const data of annotations) { + data.borderStyle ||= AnnotationLayer._defaultBorderStyle; + elementParams.data = data; const element = AnnotationElementFactory.create(elementParams); @@ -3329,6 +3331,24 @@ class AnnotationLayer { getEditableAnnotation(id) { return this.#editableAnnotations.get(id); } + + /** + * @private + */ + static get _defaultBorderStyle() { + return shadow( + this, + "_defaultBorderStyle", + Object.freeze({ + width: 1, + rawWidth: 1, + style: AnnotationBorderStyleType.SOLID, + dashArray: [3], + horizontalCornerRadius: 0, + verticalCornerRadius: 0, + }) + ); + } } export { diff --git a/src/pdf.js b/src/pdf.js index 869f41db5..36b0ccc97 100644 --- a/src/pdf.js +++ b/src/pdf.js @@ -24,7 +24,6 @@ import { AbortException, - AnnotationBorderStyleType, AnnotationEditorParamsType, AnnotationEditorType, AnnotationMode, @@ -92,7 +91,6 @@ if (typeof PDFJSDev !== "undefined" && PDFJSDev.test("TESTING || GENERIC")) { export { AbortException, - AnnotationBorderStyleType, AnnotationEditorLayer, AnnotationEditorParamsType, AnnotationEditorType, diff --git a/test/unit/pdf_spec.js b/test/unit/pdf_spec.js index 73bab4a98..1f4de0f21 100644 --- a/test/unit/pdf_spec.js +++ b/test/unit/pdf_spec.js @@ -15,7 +15,6 @@ import { AbortException, - AnnotationBorderStyleType, AnnotationEditorParamsType, AnnotationEditorType, AnnotationMode, @@ -69,7 +68,6 @@ import { XfaLayer } from "../../src/display/xfa_layer.js"; const expectedAPI = Object.freeze({ AbortException, - AnnotationBorderStyleType, AnnotationEditorLayer, AnnotationEditorParamsType, AnnotationEditorType, diff --git a/web/autolinker.js b/web/autolinker.js index b2c7007ef..098acb273 100644 --- a/web/autolinker.js +++ b/web/autolinker.js @@ -13,12 +13,7 @@ * limitations under the License. */ -import { - AnnotationBorderStyleType, - AnnotationType, - createValidAbsoluteUrl, - Util, -} from "pdfjs-lib"; +import { AnnotationType, createValidAbsoluteUrl, Util } from "pdfjs-lib"; import { getOriginalIndex, normalize } from "./pdf_find_controller.js"; function DOMRectToPDF({ width, height, left, top }, pdfPageView) { @@ -89,15 +84,9 @@ function createLinkAnnotation({ url, index, length }, pdfPageView, id) { annotationType: AnnotationType.LINK, rotation: 0, ...calculateLinkPosition(range, pdfPageView), - // This is just the default for AnnotationBorderStyle. - borderStyle: { - width: 1, - rawWidth: 1, - style: AnnotationBorderStyleType.SOLID, - dashArray: [3], - horizontalCornerRadius: 0, - verticalCornerRadius: 0, - }, + // Populated in the annotationLayer to avoid unnecessary object creation, + // since most inferred links overlap existing LinkAnnotations: + borderStyle: null, }; } diff --git a/web/pdfjs.js b/web/pdfjs.js index 763e194bc..3a2daeb74 100644 --- a/web/pdfjs.js +++ b/web/pdfjs.js @@ -15,7 +15,6 @@ const { AbortException, - AnnotationBorderStyleType, AnnotationEditorLayer, AnnotationEditorParamsType, AnnotationEditorType, @@ -65,7 +64,6 @@ const { export { AbortException, - AnnotationBorderStyleType, AnnotationEditorLayer, AnnotationEditorParamsType, AnnotationEditorType,