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

[api-minor] Re-factor the basic textLayer-functionality

This is very old code, and predates e.g. the introduction of JavaScript classes, which creates unnecessarily unwieldy code in the viewer.
By introducing a new `TextLayer` class in the API, similar to how e.g. the `AnnotationLayer` looks, we're able to keep most parameters on the class-instance itself. This removes the need to manually track them in the viewer, and simplifies the call-sites.

This also removes the `numTextDivs` parameter from the "textlayerrendered" event, since that's only added to support default-viewer functionality that no longer exists.

Finally we try, as far as possible, to polyfill the old `renderTextLayer` and `updateTextLayer` functions since they are exposed in the library API.
For *simple* invocations of `renderTextLayer` the behaviour should thus be the same, with only a warning printed in the console.
This commit is contained in:
Jonas Jenwald 2024-05-16 11:28:06 +02:00
parent 63b66b412c
commit 15b5808eee
9 changed files with 383 additions and 377 deletions

View file

@ -22,8 +22,8 @@ const {
GlobalWorkerOptions,
Outliner,
PixelsPerInch,
renderTextLayer,
shadow,
TextLayer,
XfaLayer,
} = pdfjsLib;
const { GenericL10n, parseQueryString, SimpleLinkService } = pdfjsViewer;
@ -297,13 +297,12 @@ class Rasterize {
`:root { --scale-factor: ${viewport.scale} }`;
// Rendering text layer as HTML.
const task = renderTextLayer({
const textLayer = new TextLayer({
textContentSource: textContent,
container: div,
viewport,
});
await task.promise;
await textLayer.render();
svg.append(foreignObject);
@ -327,15 +326,14 @@ class Rasterize {
`:root { --scale-factor: ${viewport.scale} }`;
// Rendering text layer as HTML.
const task = renderTextLayer({
const textLayer = new TextLayer({
textContentSource: textContent,
container: dummyParent,
viewport,
});
await textLayer.render();
await task.promise;
const { _pageWidth, _pageHeight, _textDivs } = task;
const { pageWidth, pageHeight, textDivs } = textLayer;
const boxes = [];
let j = 0,
posRegex;
@ -343,7 +341,7 @@ class Rasterize {
if (type) {
continue;
}
const { top, left } = _textDivs[j++].style;
const { top, left } = textDivs[j++].style;
let x = parseFloat(left) / 100;
let y = parseFloat(top) / 100;
if (isNaN(x)) {
@ -352,12 +350,12 @@ class Rasterize {
// string, e.g. `calc(var(--scale-factor)*66.32px)`.
let match = left.match(posRegex);
if (match) {
x = parseFloat(match[1]) / _pageWidth;
x = parseFloat(match[1]) / pageWidth;
}
match = top.match(posRegex);
if (match) {
y = parseFloat(match[1]) / _pageHeight;
y = parseFloat(match[1]) / pageHeight;
}
}
if (width === 0 || height === 0) {
@ -366,8 +364,8 @@ class Rasterize {
boxes.push({
x,
y,
width: width / _pageWidth,
height: height / _pageHeight,
width: width / pageWidth,
height: height / pageHeight,
});
}
// We set the borderWidth to 0.001 to slighly increase the size of the

View file

@ -57,6 +57,7 @@ import {
} from "../../src/display/display_utils.js";
import {
renderTextLayer,
TextLayer,
updateTextLayer,
} from "../../src/display/text_layer.js";
import { AnnotationEditorLayer } from "../../src/display/editor/annotation_editor_layer.js";
@ -108,6 +109,7 @@ const expectedAPI = Object.freeze({
renderTextLayer,
setLayerDimensions,
shadow,
TextLayer,
UnexpectedResponseException,
updateTextLayer,
Util,

View file

@ -13,13 +13,10 @@
* limitations under the License.
*/
import {
renderTextLayer,
TextLayerRenderTask,
} from "../../src/display/text_layer.js";
import { buildGetDocumentParams } from "./test_utils.js";
import { getDocument } from "../../src/display/api.js";
import { isNodeJS } from "../../src/shared/util.js";
import { TextLayer } from "../../src/display/text_layer.js";
describe("textLayer", function () {
it("creates textLayer from ReadableStream", async function () {
@ -30,18 +27,14 @@ describe("textLayer", function () {
const pdfDocument = await loadingTask.promise;
const page = await pdfDocument.getPage(1);
const textContentItemsStr = [];
const textLayerRenderTask = renderTextLayer({
const textLayer = new TextLayer({
textContentSource: page.streamTextContent(),
container: document.createElement("div"),
viewport: page.getViewport({ scale: 1 }),
textContentItemsStr,
});
expect(textLayerRenderTask instanceof TextLayerRenderTask).toEqual(true);
await textLayer.render();
await textLayerRenderTask.promise;
expect(textContentItemsStr).toEqual([
expect(textLayer.textContentItemsStr).toEqual([
"Table Of Content",
"",
"Chapter 1",
@ -70,18 +63,14 @@ describe("textLayer", function () {
const pdfDocument = await loadingTask.promise;
const page = await pdfDocument.getPage(1);
const textContentItemsStr = [];
const textLayerRenderTask = renderTextLayer({
const textLayer = new TextLayer({
textContentSource: await page.getTextContent(),
container: document.createElement("div"),
viewport: page.getViewport({ scale: 1 }),
textContentItemsStr,
});
expect(textLayerRenderTask instanceof TextLayerRenderTask).toEqual(true);
await textLayer.render();
await textLayerRenderTask.promise;
expect(textContentItemsStr).toEqual([
expect(textLayer.textContentItemsStr).toEqual([
"Table Of Content",
"",
"Chapter 1",