Skip to content

Commit 1998046

Browse files
calixtemanpull[bot]
authored andcommitted
Refactor the text layer code in order to avoid to recompute it on each draw
The idea is just to resuse what we got on the first draw. Now, we only update the scaleX of the different spans and the other values are dependant of --scale-factor. Move some properties in the CSS in order to avoid any updates in JS.
1 parent 7886f51 commit 1998046

File tree

13 files changed

+362
-240
lines changed

13 files changed

+362
-240
lines changed

src/display/text_layer.js

Lines changed: 177 additions & 76 deletions
Large diffs are not rendered by default.

src/pdf.js

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
/** @typedef {import("./display/text_layer").TextLayerRenderTask} TextLayerRenderTask */
2424

2525
import {
26+
AbortException,
2627
AnnotationEditorParamsType,
2728
AnnotationEditorType,
2829
AnnotationMode,
@@ -60,12 +61,12 @@ import {
6061
PixelsPerInch,
6162
RenderingCancelledException,
6263
} from "./display/display_utils.js";
64+
import { renderTextLayer, updateTextLayer } from "./display/text_layer.js";
6365
import { AnnotationEditorLayer } from "./display/editor/annotation_editor_layer.js";
6466
import { AnnotationEditorUIManager } from "./display/editor/tools.js";
6567
import { AnnotationLayer } from "./display/annotation_layer.js";
6668
import { GlobalWorkerOptions } from "./display/worker_options.js";
6769
import { isNodeJS } from "./shared/is_node.js";
68-
import { renderTextLayer } from "./display/text_layer.js";
6970
import { SVGGraphics } from "./display/svg.js";
7071
import { XfaLayer } from "./display/xfa_layer.js";
7172

@@ -110,6 +111,7 @@ if (typeof PDFJSDev === "undefined" || !PDFJSDev.test("PRODUCTION")) {
110111
}
111112

112113
export {
114+
AbortException,
113115
AnnotationEditorLayer,
114116
AnnotationEditorParamsType,
115117
AnnotationEditorType,
@@ -143,6 +145,7 @@ export {
143145
SVGGraphics,
144146
UnexpectedResponseException,
145147
UNSUPPORTED_FEATURES,
148+
updateTextLayer,
146149
Util,
147150
VerbosityLevel,
148151
version,

test/driver.js

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -168,7 +168,7 @@ class Rasterize {
168168
}
169169

170170
static get textStylePromise() {
171-
const styles = ["./text_layer_test.css"];
171+
const styles = [VIEWER_CSS, "./text_layer_test.css"];
172172
return shadow(this, "textStylePromise", loadStyles(styles));
173173
}
174174

@@ -256,15 +256,18 @@ class Rasterize {
256256
// Items are transformed to have 1px font size.
257257
svg.setAttribute("font-size", 1);
258258

259-
const [overrides] = await this.textStylePromise;
260-
style.textContent = overrides;
259+
const [common, overrides] = await this.textStylePromise;
260+
style.textContent =
261+
`${common}\n${overrides}\n` +
262+
`:root { --scale-factor: ${viewport.scale} }`;
261263

262264
// Rendering text layer as HTML.
263265
const task = renderTextLayer({
264266
textContent,
265267
container: div,
266268
viewport,
267269
});
270+
268271
await task.promise;
269272
svg.append(foreignObject);
270273

test/text_layer_test.css

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,9 +22,11 @@
2222
right: 0;
2323
bottom: 0;
2424
line-height: 1;
25+
opacity: 1;
2526
}
2627
.textLayer span,
2728
.textLayer br {
29+
color: black;
2830
position: absolute;
2931
white-space: pre;
3032
transform-origin: 0% 0%;

test/unit/text_layer_spec.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ import { isNodeJS } from "../../src/shared/is_node.js";
2424
describe("textLayer", function () {
2525
it("creates textLayer from ReadableStream", async function () {
2626
if (isNodeJS) {
27-
pending("document.createDocumentFragment is not supported in Node.js.");
27+
pending("document.createElement is not supported in Node.js.");
2828
}
2929
const loadingTask = getDocument(buildGetDocumentParams("basicapi.pdf"));
3030
const pdfDocument = await loadingTask.promise;
@@ -34,7 +34,7 @@ describe("textLayer", function () {
3434

3535
const textLayerRenderTask = renderTextLayer({
3636
textContentStream: page.streamTextContent(),
37-
container: document.createDocumentFragment(),
37+
container: document.createElement("div"),
3838
viewport: page.getViewport(),
3939
textContentItemsStr,
4040
});

web/app.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -501,6 +501,7 @@ const PDFViewerApplication = {
501501
imageResourcesPath: AppOptions.get("imageResourcesPath"),
502502
enablePrintAutoRotate: AppOptions.get("enablePrintAutoRotate"),
503503
useOnlyCssZoom: AppOptions.get("useOnlyCssZoom"),
504+
isOffscreenCanvasSupported: AppOptions.get("isOffscreenCanvasSupported"),
504505
maxCanvasPixels: AppOptions.get("maxCanvasPixels"),
505506
enablePermissions: AppOptions.get("enablePermissions"),
506507
pageColors,

web/default_factory.js

Lines changed: 3 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -165,33 +165,24 @@ class DefaultStructTreeLayerFactory {
165165
class DefaultTextLayerFactory {
166166
/**
167167
* @typedef {Object} CreateTextLayerBuilderParameters
168-
* @property {HTMLDivElement} textLayerDiv
169-
* @property {number} pageIndex
170-
* @property {PageViewport} viewport
171-
* @property {EventBus} eventBus
172168
* @property {TextHighlighter} highlighter
173169
* @property {TextAccessibilityManager} [accessibilityManager]
170+
* @property {boolean} [isOffscreenCanvasSupported]
174171
*/
175172

176173
/**
177174
* @param {CreateTextLayerBuilderParameters}
178175
* @returns {TextLayerBuilder}
179176
*/
180177
createTextLayerBuilder({
181-
textLayerDiv,
182-
pageIndex,
183-
viewport,
184-
eventBus,
185178
highlighter,
186179
accessibilityManager = null,
180+
isOffscreenCanvasSupported = true,
187181
}) {
188182
return new TextLayerBuilder({
189-
textLayerDiv,
190-
pageIndex,
191-
viewport,
192-
eventBus,
193183
highlighter,
194184
accessibilityManager,
185+
isOffscreenCanvasSupported,
195186
});
196187
}
197188
}

web/interfaces.js

Lines changed: 2 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,6 @@
2121
/** @typedef {import("./annotation_layer_builder").AnnotationLayerBuilder} AnnotationLayerBuilder */
2222
// eslint-disable-next-line max-len
2323
/** @typedef {import("./annotation_editor_layer_builder").AnnotationEditorLayerBuilder} AnnotationEditorLayerBuilder */
24-
/** @typedef {import("./event_utils").EventBus} EventBus */
2524
// eslint-disable-next-line max-len
2625
/** @typedef {import("./struct_tree_builder").StructTreeLayerBuilder} StructTreeLayerBuilder */
2726
/** @typedef {import("./text_highlighter").TextHighlighter} TextHighlighter */
@@ -168,25 +167,19 @@ class IRenderableView {
168167
class IPDFTextLayerFactory {
169168
/**
170169
* @typedef {Object} CreateTextLayerBuilderParameters
171-
* @property {HTMLDivElement} textLayerDiv
172-
* @property {number} pageIndex
173-
* @property {PageViewport} viewport
174-
* @property {EventBus} eventBus
175170
* @property {TextHighlighter} highlighter
176171
* @property {TextAccessibilityManager} [accessibilityManager]
172+
* @property {boolean} [isOffscreenCanvasSupported]
177173
*/
178174

179175
/**
180176
* @param {CreateTextLayerBuilderParameters}
181177
* @returns {TextLayerBuilder}
182178
*/
183179
createTextLayerBuilder({
184-
textLayerDiv,
185-
pageIndex,
186-
viewport,
187-
eventBus,
188180
highlighter,
189181
accessibilityManager,
182+
isOffscreenCanvasSupported,
190183
}) {}
191184
}
192185

0 commit comments

Comments
 (0)