Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[api-minor] Remove the setTextContentSource method in TextLayerBuilder (PR 18104 follow-up) #18170

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
49 changes: 16 additions & 33 deletions web/pdf_page_view.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -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);
}
}

Expand All @@ -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);
}
}

Expand Down Expand Up @@ -422,44 +422,26 @@ 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;
}
console.error(`#renderTextLayer: "${ex}".`);
error = ex;
}

this.eventBus.dispatch("textlayerrendered", {
source: this,
pageNumber: this.id,
error,
});
this.#dispatchLayerRendered("textlayerrendered", error);

this.#renderStructTreeLayer();
}
Expand Down Expand Up @@ -917,6 +899,7 @@ class PDFPageView {
this._accessibilityManager ||= new TextAccessibilityManager();

this.textLayer = new TextLayerBuilder({
pdfPage,
highlighter: this._textHighlighter,
accessibilityManager: this._accessibilityManager,
enablePermissions:
Expand Down Expand Up @@ -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(
Expand Down
40 changes: 18 additions & 22 deletions web/text_layer_builder.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 */
Expand All @@ -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]
Expand All @@ -41,7 +42,7 @@ class TextLayerBuilder {

#onAppend = null;

#textContentSource = null;
#renderingDone = false;

#textLayer = null;

Expand All @@ -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;
Expand All @@ -67,7 +69,7 @@ class TextLayerBuilder {
}

#finishRendering() {
this.renderingDone = true;
this.#renderingDone = true;

const endOfContent = document.createElement("div");
endOfContent.className = "endOfContent";
Expand All @@ -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),
Expand All @@ -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,
});
Expand All @@ -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();
Expand All @@ -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();
}
Expand All @@ -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
Expand Down