Skip to content

Commit

Permalink
make processLinks sync by using textContent from textLayer
Browse files Browse the repository at this point in the history
  • Loading branch information
ryzokuken committed Dec 16, 2024
1 parent f44093f commit ebeee90
Show file tree
Hide file tree
Showing 4 changed files with 39 additions and 48 deletions.
41 changes: 18 additions & 23 deletions web/autolinker.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,5 @@
import { createValidAbsoluteUrl, Util } from "../src/shared/util.js";
import {
getOriginalIndex,
normalizedTextContent,
} from "./pdf_find_controller.js";
import { getOriginalIndex, normalize } from "./pdf_find_controller.js";

class Autolinker {
static #urlRegex =
Expand Down Expand Up @@ -71,25 +68,23 @@ class Autolinker {
return linkAnnotations;
}

static processLinks(pdfPageView) {
return pdfPageView.pdfPage.getTextContent().then(content => {
const [text, diffs] = normalizedTextContent(content);
const matches = text.matchAll(Autolinker.#urlRegex);
return Array.from(matches, match => {
const url = createValidAbsoluteUrl(match[0]);
if (url) {
const [index, length] = getOriginalIndex(
diffs,
match.index,
match[0].length
);
return this.#addLinkAnnotations(url.href, index, length, pdfPageView);
}
return url;
})
.filter(annotation => annotation !== null)
.flat();
});
static processLinks(pdfPageView, textContent) {
const [text, diffs] = normalize(textContent.join(""));
const matches = text.matchAll(Autolinker.#urlRegex);
return Array.from(matches, match => {
const url = createValidAbsoluteUrl(match[0]);
if (url) {
const [index, length] = getOriginalIndex(
diffs,
match.index,
match[0].length
);
return this.#addLinkAnnotations(url.href, index, length, pdfPageView);
}
return url;
})
.filter(annotation => annotation !== null)
.flat();
}
}

Expand Down
31 changes: 11 additions & 20 deletions web/pdf_find_controller.js
Original file line number Diff line number Diff line change
Expand Up @@ -384,19 +384,6 @@ function getOriginalIndex(diffs, pos, len) {
return [oldStart, oldLen];
}

function normalizedTextContent(textContent) {
const strBuf = [];

for (const textItem of textContent.items) {
strBuf.push(textItem.str);
if (textItem.hasEOL) {
strBuf.push("\n");
}
}

return normalize(strBuf.join(""));
}

/**
* @typedef {Object} PDFFindControllerOptions
* @property {IPDFLinkService} linkService - The navigation/linking service.
Expand Down Expand Up @@ -892,12 +879,21 @@ class PDFFindController {
.then(pdfPage => pdfPage.getTextContent(textOptions))
.then(
textContent => {
const strBuf = [];

for (const textItem of textContent.items) {
strBuf.push(textItem.str);
if (textItem.hasEOL) {
strBuf.push("\n");
}
}

// Store the normalized page content (text items) as one string.
[
this._pageContents[i],
this._pageDiffs[i],
this._hasDiacritics[i],
] = normalizedTextContent(textContent);
] = normalize(strBuf.join(""));
resolve();
},
reason => {
Expand Down Expand Up @@ -1175,9 +1171,4 @@ class PDFFindController {
}
}

export {
FindState,
getOriginalIndex,
normalizedTextContent,
PDFFindController,
};
export { FindState, getOriginalIndex, normalize, PDFFindController };
11 changes: 7 additions & 4 deletions web/pdf_page_view.js
Original file line number Diff line number Diff line change
Expand Up @@ -463,22 +463,24 @@ class PDFPageView {

async #renderTextLayer() {
if (!this.textLayer) {
return;
return [];
}

let error = null;
let textContent;
try {
await this.textLayer.render(this.viewport);
textContent = await this.textLayer.render(this.viewport);
} catch (ex) {
if (ex instanceof AbortException) {
return;
return [];
}
console.error("#renderTextLayer:", ex);
error = ex;
}
this.#dispatchLayerRendered("textlayerrendered", error);

this.#renderStructTreeLayer();
return textContent;
}

/**
Expand Down Expand Up @@ -1098,7 +1100,8 @@ class PDFPageView {
if (this.annotationLayer) {
await textLayerP;
if (this.#enableAutolinking) {
this.#linkAnnotations = await Autolinker.processLinks(this);
const textContent = await textLayerP;
this.#linkAnnotations = Autolinker.processLinks(this, textContent);
}
await this.#renderAnnotationLayer();
}
Expand Down
4 changes: 3 additions & 1 deletion web/text_layer_builder.js
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,7 @@ class TextLayerBuilder {
* Renders the text layer.
* @param {PageViewport} viewport
* @param {Object} [textContentParams]
* @returns {Array<string>}
*/
async render(viewport, textContentParams = null) {
if (this.#renderingDone && this.#textLayer) {
Expand All @@ -80,7 +81,7 @@ class TextLayerBuilder {
onBefore: this.hide.bind(this),
});
this.show();
return;
return [];
}

this.cancel();
Expand Down Expand Up @@ -112,6 +113,7 @@ class TextLayerBuilder {
this.#onAppend?.(this.div);
this.highlighter?.enable();
this.accessibilityManager?.enable();
return textContentItemsStr;
}

hide() {
Expand Down

0 comments on commit ebeee90

Please sign in to comment.