mirror of
https://github.com/mozilla/pdf.js.git
synced 2025-04-20 15:18:08 +02:00
[api-minor] Combine the textContent
/textContentStream
parameters
Rather than handling these parameters separately, which is a left-over from back when streaming of textContent was originally added, we can simply pass either data directly to the `TextLayer` and let it handle things accordingly. Also, improves a few JSDoc comments and `typedef`-imports.
This commit is contained in:
parent
67e1c37e0f
commit
fe8fded23b
5 changed files with 58 additions and 62 deletions
|
@ -301,7 +301,7 @@ class PDFPageView {
|
|||
const readableStream = pdfPage.streamTextContent({
|
||||
includeMarkedContent: true,
|
||||
});
|
||||
textLayer.setTextContentStream(readableStream);
|
||||
textLayer.setTextContentSource(readableStream);
|
||||
}
|
||||
await textLayer.render(viewport);
|
||||
} catch (ex) {
|
||||
|
|
|
@ -15,6 +15,7 @@
|
|||
|
||||
// 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 */
|
||||
|
@ -36,18 +37,18 @@ import { renderTextLayer, updateTextLayer } from "pdfjs-lib";
|
|||
* contain text that matches the PDF text they are overlaying.
|
||||
*/
|
||||
class TextLayerBuilder {
|
||||
#rotation = 0;
|
||||
|
||||
#scale = 0;
|
||||
|
||||
#rotation = 0;
|
||||
#textContentSource = null;
|
||||
|
||||
constructor({
|
||||
highlighter = null,
|
||||
accessibilityManager = null,
|
||||
isOffscreenCanvasSupported = true,
|
||||
}) {
|
||||
this.textContent = null;
|
||||
this.textContentItemsStr = [];
|
||||
this.textContentStream = null;
|
||||
this.renderingDone = false;
|
||||
this.textDivs = [];
|
||||
this.textDivProperties = new WeakMap();
|
||||
|
@ -76,12 +77,11 @@ class TextLayerBuilder {
|
|||
|
||||
/**
|
||||
* Renders the text layer.
|
||||
* @param {PageViewport} viewport
|
||||
*/
|
||||
async render(viewport) {
|
||||
if (!(this.textContent || this.textContentStream)) {
|
||||
throw new Error(
|
||||
`Neither "textContent" nor "textContentStream" specified.`
|
||||
);
|
||||
if (!this.#textContentSource) {
|
||||
throw new Error('No "textContentSource" parameter specified.');
|
||||
}
|
||||
|
||||
const scale = viewport.scale * (globalThis.devicePixelRatio || 1);
|
||||
|
@ -112,8 +112,7 @@ class TextLayerBuilder {
|
|||
this.accessibilityManager?.setTextMapping(this.textDivs);
|
||||
|
||||
this.textLayerRenderTask = renderTextLayer({
|
||||
textContent: this.textContent,
|
||||
textContentStream: this.textContentStream,
|
||||
textContentSource: this.#textContentSource,
|
||||
container: this.div,
|
||||
viewport,
|
||||
textDivs: this.textDivs,
|
||||
|
@ -156,14 +155,12 @@ class TextLayerBuilder {
|
|||
this.textDivProperties = new WeakMap();
|
||||
}
|
||||
|
||||
setTextContentStream(readableStream) {
|
||||
/**
|
||||
* @param {ReadableStream | TextContent} source
|
||||
*/
|
||||
setTextContentSource(source) {
|
||||
this.cancel();
|
||||
this.textContentStream = readableStream;
|
||||
}
|
||||
|
||||
setTextContent(textContent) {
|
||||
this.cancel();
|
||||
this.textContent = textContent;
|
||||
this.#textContentSource = source;
|
||||
}
|
||||
|
||||
/**
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue