Merge pull request #15826 from Snuffleupagus/lazy-textHighlighter

Initialize the `TextHighlighter`-instance lazily in `PDFPageView`
This commit is contained in:
Jonas Jenwald 2022-12-14 14:17:34 +01:00 committed by GitHub
commit 68f36d82d5
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -39,6 +39,7 @@ import {
PixelsPerInch, PixelsPerInch,
RenderingCancelledException, RenderingCancelledException,
setLayerDimensions, setLayerDimensions,
shadow,
SVGGraphics, SVGGraphics,
} from "pdfjs-lib"; } from "pdfjs-lib";
import { import {
@ -143,11 +144,7 @@ class PDFPageView {
this.annotationLayerFactory = options.annotationLayerFactory; this.annotationLayerFactory = options.annotationLayerFactory;
this.annotationEditorLayerFactory = options.annotationEditorLayerFactory; this.annotationEditorLayerFactory = options.annotationEditorLayerFactory;
this.xfaLayerFactory = options.xfaLayerFactory; this.xfaLayerFactory = options.xfaLayerFactory;
this.textHighlighter = this._textHighlighterFactory = options.textHighlighterFactory;
options.textHighlighterFactory?.createTextHighlighter({
pageIndex: this.id - 1,
eventBus: this.eventBus,
});
this.structTreeLayerFactory = options.structTreeLayerFactory; this.structTreeLayerFactory = options.structTreeLayerFactory;
if ( if (
typeof PDFJSDev === "undefined" || typeof PDFJSDev === "undefined" ||
@ -247,15 +244,23 @@ class PDFPageView {
this.pdfPage?.cleanup(); this.pdfPage?.cleanup();
} }
/** get _textHighlighter() {
* @private return shadow(
*/ this,
async _renderAnnotationLayer() { "_textHighlighter",
this._textHighlighterFactory?.createTextHighlighter({
pageIndex: this.id - 1,
eventBus: this.eventBus,
})
);
}
async #renderAnnotationLayer() {
let error = null; let error = null;
try { try {
await this.annotationLayer.render(this.viewport, "display"); await this.annotationLayer.render(this.viewport, "display");
} catch (ex) { } catch (ex) {
console.error(`_renderAnnotationLayer: "${ex}".`); console.error(`#renderAnnotationLayer: "${ex}".`);
error = ex; error = ex;
} finally { } finally {
this.eventBus.dispatch("annotationlayerrendered", { this.eventBus.dispatch("annotationlayerrendered", {
@ -266,15 +271,12 @@ class PDFPageView {
} }
} }
/** async #renderAnnotationEditorLayer() {
* @private
*/
async _renderAnnotationEditorLayer() {
let error = null; let error = null;
try { try {
await this.annotationEditorLayer.render(this.viewport, "display"); await this.annotationEditorLayer.render(this.viewport, "display");
} catch (ex) { } catch (ex) {
console.error(`_renderAnnotationEditorLayer: "${ex}".`); console.error(`#renderAnnotationEditorLayer: "${ex}".`);
error = ex; error = ex;
} finally { } finally {
this.eventBus.dispatch("annotationeditorlayerrendered", { this.eventBus.dispatch("annotationeditorlayerrendered", {
@ -285,18 +287,15 @@ class PDFPageView {
} }
} }
/** async #renderXfaLayer() {
* @private
*/
async _renderXfaLayer() {
let error = null; let error = null;
try { try {
const result = await this.xfaLayer.render(this.viewport, "display"); const result = await this.xfaLayer.render(this.viewport, "display");
if (result?.textDivs && this.textHighlighter) { if (result?.textDivs && this._textHighlighter) {
this._buildXfaTextContentItems(result.textDivs); this.#buildXfaTextContentItems(result.textDivs);
} }
} catch (ex) { } catch (ex) {
console.error(`_renderXfaLayer: "${ex}".`); console.error(`#renderXfaLayer: "${ex}".`);
error = ex; error = ex;
} finally { } finally {
this.eventBus.dispatch("xfalayerrendered", { this.eventBus.dispatch("xfalayerrendered", {
@ -365,14 +364,14 @@ class PDFPageView {
} }
} }
async _buildXfaTextContentItems(textDivs) { async #buildXfaTextContentItems(textDivs) {
const text = await this.pdfPage.getTextContent(); const text = await this.pdfPage.getTextContent();
const items = []; const items = [];
for (const item of text.items) { for (const item of text.items) {
items.push(item.str); items.push(item.str);
} }
this.textHighlighter.setTextMapping(textDivs, items); this._textHighlighter.setTextMapping(textDivs, items);
this.textHighlighter.enable(); this._textHighlighter.enable();
} }
/** /**
@ -639,7 +638,7 @@ class PDFPageView {
if (this.xfaLayer && (!keepXfaLayer || !this.xfaLayer.div)) { if (this.xfaLayer && (!keepXfaLayer || !this.xfaLayer.div)) {
this.xfaLayer.cancel(); this.xfaLayer.cancel();
this.xfaLayer = null; this.xfaLayer = null;
this.textHighlighter?.disable(); this._textHighlighter?.disable();
} }
} }
@ -676,13 +675,13 @@ class PDFPageView {
target.style.transform = `rotate(${relativeRotation}deg) scale(${scaleX}, ${scaleY})`; target.style.transform = `rotate(${relativeRotation}deg) scale(${scaleX}, ${scaleY})`;
if (redrawAnnotationLayer && this.annotationLayer) { if (redrawAnnotationLayer && this.annotationLayer) {
this._renderAnnotationLayer(); this.#renderAnnotationLayer();
} }
if (redrawAnnotationEditorLayer && this.annotationEditorLayer) { if (redrawAnnotationEditorLayer && this.annotationEditorLayer) {
this._renderAnnotationEditorLayer(); this.#renderAnnotationEditorLayer();
} }
if (redrawXfaLayer && this.xfaLayer) { if (redrawXfaLayer && this.xfaLayer) {
this._renderXfaLayer(); this.#renderXfaLayer();
} }
if (redrawTextLayer && this.textLayer) { if (redrawTextLayer && this.textLayer) {
this.#renderTextLayer(); this.#renderTextLayer();
@ -753,7 +752,7 @@ class PDFPageView {
this._accessibilityManager ||= new TextAccessibilityManager(); this._accessibilityManager ||= new TextAccessibilityManager();
this.textLayer = this.textLayerFactory.createTextLayerBuilder({ this.textLayer = this.textLayerFactory.createTextLayerBuilder({
highlighter: this.textHighlighter, highlighter: this._textHighlighter,
accessibilityManager: this._accessibilityManager, accessibilityManager: this._accessibilityManager,
isOffscreenCanvasSupported: this.isOffscreenCanvasSupported, isOffscreenCanvasSupported: this.isOffscreenCanvasSupported,
}); });
@ -851,7 +850,7 @@ class PDFPageView {
this.#renderTextLayer(); this.#renderTextLayer();
if (this.annotationLayer) { if (this.annotationLayer) {
this._renderAnnotationLayer().then(() => { this.#renderAnnotationLayer().then(() => {
if (this.annotationEditorLayerFactory) { if (this.annotationEditorLayerFactory) {
this.annotationEditorLayer ||= this.annotationEditorLayer ||=
this.annotationEditorLayerFactory.createAnnotationEditorLayerBuilder( this.annotationEditorLayerFactory.createAnnotationEditorLayerBuilder(
@ -862,7 +861,7 @@ class PDFPageView {
accessibilityManager: this._accessibilityManager, accessibilityManager: this._accessibilityManager,
} }
); );
this._renderAnnotationEditorLayer(); this.#renderAnnotationEditorLayer();
} }
}); });
} }
@ -878,7 +877,7 @@ class PDFPageView {
pageDiv: div, pageDiv: div,
pdfPage, pdfPage,
}); });
this._renderXfaLayer(); this.#renderXfaLayer();
} }
div.setAttribute("data-loaded", true); div.setAttribute("data-loaded", true);