From 5df341ed7ef0488cbc1f5fab4dc7027de6e6e618 Mon Sep 17 00:00:00 2001 From: Jonas Jenwald Date: Wed, 14 Dec 2022 13:12:49 +0100 Subject: [PATCH 1/2] Make the various layer-render methods, in `PDFPageView`, properly private --- web/pdf_page_view.js | 37 ++++++++++++++----------------------- 1 file changed, 14 insertions(+), 23 deletions(-) diff --git a/web/pdf_page_view.js b/web/pdf_page_view.js index 0bdea2c74..f9c8a5e12 100644 --- a/web/pdf_page_view.js +++ b/web/pdf_page_view.js @@ -247,15 +247,12 @@ class PDFPageView { this.pdfPage?.cleanup(); } - /** - * @private - */ - async _renderAnnotationLayer() { + async #renderAnnotationLayer() { let error = null; try { await this.annotationLayer.render(this.viewport, "display"); } catch (ex) { - console.error(`_renderAnnotationLayer: "${ex}".`); + console.error(`#renderAnnotationLayer: "${ex}".`); error = ex; } finally { this.eventBus.dispatch("annotationlayerrendered", { @@ -266,15 +263,12 @@ class PDFPageView { } } - /** - * @private - */ - async _renderAnnotationEditorLayer() { + async #renderAnnotationEditorLayer() { let error = null; try { await this.annotationEditorLayer.render(this.viewport, "display"); } catch (ex) { - console.error(`_renderAnnotationEditorLayer: "${ex}".`); + console.error(`#renderAnnotationEditorLayer: "${ex}".`); error = ex; } finally { this.eventBus.dispatch("annotationeditorlayerrendered", { @@ -285,18 +279,15 @@ class PDFPageView { } } - /** - * @private - */ - async _renderXfaLayer() { + async #renderXfaLayer() { let error = null; try { const result = await this.xfaLayer.render(this.viewport, "display"); if (result?.textDivs && this.textHighlighter) { - this._buildXfaTextContentItems(result.textDivs); + this.#buildXfaTextContentItems(result.textDivs); } } catch (ex) { - console.error(`_renderXfaLayer: "${ex}".`); + console.error(`#renderXfaLayer: "${ex}".`); error = ex; } finally { this.eventBus.dispatch("xfalayerrendered", { @@ -365,7 +356,7 @@ class PDFPageView { } } - async _buildXfaTextContentItems(textDivs) { + async #buildXfaTextContentItems(textDivs) { const text = await this.pdfPage.getTextContent(); const items = []; for (const item of text.items) { @@ -676,13 +667,13 @@ class PDFPageView { target.style.transform = `rotate(${relativeRotation}deg) scale(${scaleX}, ${scaleY})`; if (redrawAnnotationLayer && this.annotationLayer) { - this._renderAnnotationLayer(); + this.#renderAnnotationLayer(); } if (redrawAnnotationEditorLayer && this.annotationEditorLayer) { - this._renderAnnotationEditorLayer(); + this.#renderAnnotationEditorLayer(); } if (redrawXfaLayer && this.xfaLayer) { - this._renderXfaLayer(); + this.#renderXfaLayer(); } if (redrawTextLayer && this.textLayer) { this.#renderTextLayer(); @@ -851,7 +842,7 @@ class PDFPageView { this.#renderTextLayer(); if (this.annotationLayer) { - this._renderAnnotationLayer().then(() => { + this.#renderAnnotationLayer().then(() => { if (this.annotationEditorLayerFactory) { this.annotationEditorLayer ||= this.annotationEditorLayerFactory.createAnnotationEditorLayerBuilder( @@ -862,7 +853,7 @@ class PDFPageView { accessibilityManager: this._accessibilityManager, } ); - this._renderAnnotationEditorLayer(); + this.#renderAnnotationEditorLayer(); } }); } @@ -878,7 +869,7 @@ class PDFPageView { pageDiv: div, pdfPage, }); - this._renderXfaLayer(); + this.#renderXfaLayer(); } div.setAttribute("data-loaded", true); From 8ac94d65199d4e3fd6050b0470ad292cd4b4fa3e Mon Sep 17 00:00:00 2001 From: Jonas Jenwald Date: Wed, 14 Dec 2022 13:19:41 +0100 Subject: [PATCH 2/2] Initialize the `TextHighlighter`-instance lazily in `PDFPageView` Depending on e.g. the `textLayerMode` option it might not actually be necessary to always initialize this eagerly. *Please note:* Unfortunately we cannot `shadow` a private field, hence why this is only made semi-"private". --- web/pdf_page_view.js | 28 ++++++++++++++++++---------- 1 file changed, 18 insertions(+), 10 deletions(-) diff --git a/web/pdf_page_view.js b/web/pdf_page_view.js index f9c8a5e12..ab68c7ea2 100644 --- a/web/pdf_page_view.js +++ b/web/pdf_page_view.js @@ -39,6 +39,7 @@ import { PixelsPerInch, RenderingCancelledException, setLayerDimensions, + shadow, SVGGraphics, } from "pdfjs-lib"; import { @@ -143,11 +144,7 @@ class PDFPageView { this.annotationLayerFactory = options.annotationLayerFactory; this.annotationEditorLayerFactory = options.annotationEditorLayerFactory; this.xfaLayerFactory = options.xfaLayerFactory; - this.textHighlighter = - options.textHighlighterFactory?.createTextHighlighter({ - pageIndex: this.id - 1, - eventBus: this.eventBus, - }); + this._textHighlighterFactory = options.textHighlighterFactory; this.structTreeLayerFactory = options.structTreeLayerFactory; if ( typeof PDFJSDev === "undefined" || @@ -247,6 +244,17 @@ class PDFPageView { this.pdfPage?.cleanup(); } + get _textHighlighter() { + return shadow( + this, + "_textHighlighter", + this._textHighlighterFactory?.createTextHighlighter({ + pageIndex: this.id - 1, + eventBus: this.eventBus, + }) + ); + } + async #renderAnnotationLayer() { let error = null; try { @@ -283,7 +291,7 @@ class PDFPageView { let error = null; try { const result = await this.xfaLayer.render(this.viewport, "display"); - if (result?.textDivs && this.textHighlighter) { + if (result?.textDivs && this._textHighlighter) { this.#buildXfaTextContentItems(result.textDivs); } } catch (ex) { @@ -362,8 +370,8 @@ class PDFPageView { for (const item of text.items) { items.push(item.str); } - this.textHighlighter.setTextMapping(textDivs, items); - this.textHighlighter.enable(); + this._textHighlighter.setTextMapping(textDivs, items); + this._textHighlighter.enable(); } /** @@ -630,7 +638,7 @@ class PDFPageView { if (this.xfaLayer && (!keepXfaLayer || !this.xfaLayer.div)) { this.xfaLayer.cancel(); this.xfaLayer = null; - this.textHighlighter?.disable(); + this._textHighlighter?.disable(); } } @@ -744,7 +752,7 @@ class PDFPageView { this._accessibilityManager ||= new TextAccessibilityManager(); this.textLayer = this.textLayerFactory.createTextLayerBuilder({ - highlighter: this.textHighlighter, + highlighter: this._textHighlighter, accessibilityManager: this._accessibilityManager, isOffscreenCanvasSupported: this.isOffscreenCanvasSupported, });