1
0
Fork 0
mirror of https://github.com/mozilla/pdf.js.git synced 2025-04-19 22:58:07 +02:00

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

The `setTextContentSource` functionality is very old code, and was introduced years ago together with streaming of textContent.
By moving the `streamTextContent`-call into the `TextLayerBuilder` class we collect more functionality in one place and slightly reduce the amount of code needed.
This commit is contained in:
Jonas Jenwald 2024-05-26 12:11:50 +02:00
parent 17e09e5478
commit db86f8132e
2 changed files with 22 additions and 33 deletions

View file

@ -432,21 +432,13 @@ class PDFPageView {
}
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;
@ -917,6 +909,7 @@ class PDFPageView {
this._accessibilityManager ||= new TextAccessibilityManager();
this.textLayer = new TextLayerBuilder({
pdfPage,
highlighter: this._textHighlighter,
accessibilityManager: this._accessibilityManager,
enablePermissions:
@ -1047,7 +1040,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(

View file

@ -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