1
0
Fork 0
mirror of https://github.com/mozilla/pdf.js.git synced 2025-04-20 15:18:08 +02:00

Merge pull request #18052 from Snuffleupagus/textLayer-only-ReadableStream

Restore broken functionality and simplify the implementation in `src/display/text_layer.js`
This commit is contained in:
Jonas Jenwald 2024-05-14 12:30:27 +02:00 committed by GitHub
commit c0b5d93ef4
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
3 changed files with 109 additions and 82 deletions

View file

@ -335,15 +335,11 @@ class Rasterize {
await task.promise;
const { _pageWidth, _pageHeight, _textContentSource, _textDivs } = task;
const { _pageWidth, _pageHeight, _textDivs } = task;
const boxes = [];
let posRegex;
for (
let i = 0, j = 0, ii = _textContentSource.items.length;
i < ii;
i++
) {
const { width, height, type } = _textContentSource.items[i];
let j = 0,
posRegex;
for (const { width, height, type } of textContent.items) {
if (type) {
continue;
}
@ -396,7 +392,7 @@ class Rasterize {
drawLayer.destroy();
} catch (reason) {
throw new Error(`Rasterize.textLayer: "${reason?.message}".`);
throw new Error(`Rasterize.highlightLayer: "${reason?.message}".`);
}
}

View file

@ -58,5 +58,47 @@ describe("textLayer", function () {
"",
"page 1 / 3",
]);
await loadingTask.destroy();
});
it("creates textLayer from TextContent", async function () {
if (isNodeJS) {
pending("document.createElement is not supported in Node.js.");
}
const loadingTask = getDocument(buildGetDocumentParams("basicapi.pdf"));
const pdfDocument = await loadingTask.promise;
const page = await pdfDocument.getPage(1);
const textContentItemsStr = [];
const textLayerRenderTask = renderTextLayer({
textContentSource: await page.getTextContent(),
container: document.createElement("div"),
viewport: page.getViewport({ scale: 1 }),
textContentItemsStr,
});
expect(textLayerRenderTask instanceof TextLayerRenderTask).toEqual(true);
await textLayerRenderTask.promise;
expect(textContentItemsStr).toEqual([
"Table Of Content",
"",
"Chapter 1",
" ",
"..........................................................",
" ",
"2",
"",
"Paragraph 1.1",
" ",
"......................................................",
" ",
"3",
"",
"page 1 / 3",
]);
await loadingTask.destroy();
});
});