diff --git a/web/pdf_page_view.js b/web/pdf_page_view.js index 915f566f97813..b40f2dcc507c5 100644 --- a/web/pdf_page_view.js +++ b/web/pdf_page_view.js @@ -361,6 +361,14 @@ class PDFPageView { ); } + #dispatchLayerRendered(name, error) { + this.eventBus.dispatch(name, { + source: this, + pageNumber: this.id, + error, + }); + } + async #renderAnnotationLayer() { let error = null; try { @@ -369,11 +377,7 @@ class PDFPageView { console.error(`#renderAnnotationLayer: "${ex}".`); error = ex; } finally { - this.eventBus.dispatch("annotationlayerrendered", { - source: this, - pageNumber: this.id, - error, - }); + this.#dispatchLayerRendered("annotationlayerrendered", error); } } @@ -385,11 +389,7 @@ class PDFPageView { console.error(`#renderAnnotationEditorLayer: "${ex}".`); error = ex; } finally { - this.eventBus.dispatch("annotationeditorlayerrendered", { - source: this, - pageNumber: this.id, - error, - }); + this.#dispatchLayerRendered("annotationeditorlayerrendered", error); } } @@ -422,31 +422,18 @@ class PDFPageView { this.#addLayer(this.xfaLayer.div, "xfaLayer"); this.l10n.resume(); } - - this.eventBus.dispatch("xfalayerrendered", { - source: this, - pageNumber: this.id, - error, - }); + this.#dispatchLayerRendered("xfalayerrendered", error); } } async #renderTextLayer() { - const { pdfPage, textLayer, viewport } = this; - if (!textLayer) { + if (!this.textLayer) { return; } let error = null; try { - if (!textLayer.renderingDone) { - const readableStream = pdfPage.streamTextContent({ - includeMarkedContent: true, - disableNormalization: true, - }); - textLayer.setTextContentSource(readableStream); - } - await textLayer.render(viewport); + await this.textLayer.render(this.viewport); } catch (ex) { if (ex instanceof AbortException) { return; @@ -454,12 +441,7 @@ class PDFPageView { console.error(`#renderTextLayer: "${ex}".`); error = ex; } - - this.eventBus.dispatch("textlayerrendered", { - source: this, - pageNumber: this.id, - error, - }); + this.#dispatchLayerRendered("textlayerrendered", error); this.#renderStructTreeLayer(); } @@ -917,6 +899,7 @@ class PDFPageView { this._accessibilityManager ||= new TextAccessibilityManager(); this.textLayer = new TextLayerBuilder({ + pdfPage, highlighter: this._textHighlighter, accessibilityManager: this._accessibilityManager, enablePermissions: @@ -1047,7 +1030,7 @@ class PDFPageView { annotationCanvasMap: this._annotationCanvasMap, pageColors, }; - const renderTask = (this.renderTask = this.pdfPage.render(renderContext)); + const renderTask = (this.renderTask = pdfPage.render(renderContext)); renderTask.onContinue = renderContinueCallback; const resultPromise = renderTask.promise.then( diff --git a/web/text_layer_builder.js b/web/text_layer_builder.js index e0fd2b3b05e64..531534ddd4f56 100644 --- a/web/text_layer_builder.js +++ b/web/text_layer_builder.js @@ -13,9 +13,9 @@ * limitations under the License. */ +/** @typedef {import("../src/display/api").PDFPageProxy} PDFPageProxy */ // eslint-disable-next-line max-len /** @typedef {import("../src/display/display_utils").PageViewport} PageViewport */ -/** @typedef {import("../src/display/api").TextContent} TextContent */ /** @typedef {import("./text_highlighter").TextHighlighter} TextHighlighter */ // eslint-disable-next-line max-len /** @typedef {import("./text_accessibility.js").TextAccessibilityManager} TextAccessibilityManager */ @@ -25,7 +25,8 @@ import { removeNullCharacters } from "./ui_utils.js"; /** * @typedef {Object} TextLayerBuilderOptions - * @property {TextHighlighter} highlighter - Optional object that will handle + * @property {PDFPageProxy} pdfPage + * @property {TextHighlighter} [highlighter] - Optional object that will handle * highlighting text from the find controller. * @property {TextAccessibilityManager} [accessibilityManager] * @property {function} [onAppend] @@ -41,7 +42,7 @@ class TextLayerBuilder { #onAppend = null; - #textContentSource = null; + #renderingDone = false; #textLayer = null; @@ -50,12 +51,13 @@ class TextLayerBuilder { static #selectionChangeAbortController = null; constructor({ + pdfPage, highlighter = null, accessibilityManager = null, enablePermissions = false, onAppend = null, }) { - this.renderingDone = false; + this.pdfPage = pdfPage; this.highlighter = highlighter; this.accessibilityManager = accessibilityManager; this.#enablePermissions = enablePermissions === true; @@ -67,7 +69,7 @@ class TextLayerBuilder { } #finishRendering() { - this.renderingDone = true; + this.#renderingDone = true; const endOfContent = document.createElement("div"); endOfContent.className = "endOfContent"; @@ -79,13 +81,10 @@ class TextLayerBuilder { /** * Renders the text layer. * @param {PageViewport} viewport + * @param {Object} [textContentParams] */ - async render(viewport) { - if (!this.#textContentSource) { - throw new Error('No "textContentSource" parameter specified.'); - } - - if (this.renderingDone && this.#textLayer) { + async render(viewport, textContentParams = null) { + if (this.#renderingDone && this.#textLayer) { this.#textLayer.update({ viewport, onBefore: this.hide.bind(this), @@ -96,7 +95,12 @@ class TextLayerBuilder { this.cancel(); this.#textLayer = new TextLayer({ - textContentSource: this.#textContentSource, + textContentSource: this.pdfPage.streamTextContent( + textContentParams || { + includeMarkedContent: true, + disableNormalization: true, + } + ), container: this.div, viewport, }); @@ -115,7 +119,7 @@ class TextLayerBuilder { } hide() { - if (!this.div.hidden && this.renderingDone) { + if (!this.div.hidden && this.#renderingDone) { // We turn off the highlighter in order to avoid to scroll into view an // element of the text layer which could be hidden. this.highlighter?.disable(); @@ -124,7 +128,7 @@ class TextLayerBuilder { } show() { - if (this.div.hidden && this.renderingDone) { + if (this.div.hidden && this.#renderingDone) { this.div.hidden = false; this.highlighter?.enable(); } @@ -142,14 +146,6 @@ class TextLayerBuilder { TextLayerBuilder.#removeGlobalSelectionListener(this.div); } - /** - * @param {ReadableStream | TextContent} source - */ - setTextContentSource(source) { - this.cancel(); - this.#textContentSource = source; - } - /** * Improves text selection by adding an additional div where the mouse was * clicked. This reduces flickering of the content if the mouse is slowly