Skip to content

Reduce the size of TextLayerRenderTask._textDivProperties in "regular" text-selection mode #13976

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
43 changes: 29 additions & 14 deletions src/display/text_layer.js
Original file line number Diff line number Diff line change
Expand Up @@ -118,18 +118,25 @@ function getAscent(fontFamily, ctx) {
function appendText(task, geom, styles, ctx) {
// Initialize all used properties to keep the caches monomorphic.
const textDiv = document.createElement("span");
const textDivProperties = {
angle: 0,
canvasWidth: 0,
hasText: geom.str !== "",
hasEOL: geom.hasEOL,
originalTransform: null,
paddingBottom: 0,
paddingLeft: 0,
paddingRight: 0,
paddingTop: 0,
scale: 1,
};
const textDivProperties = task._enhanceTextSelection
? {
angle: 0,
canvasWidth: 0,
hasText: geom.str !== "",
hasEOL: geom.hasEOL,
originalTransform: null,
paddingBottom: 0,
paddingLeft: 0,
paddingRight: 0,
paddingTop: 0,
scale: 1,
}
: {
angle: 0,
canvasWidth: 0,
hasText: geom.str !== "",
hasEOL: geom.hasEOL,
};

task._textDivs.push(textDiv);

Expand Down Expand Up @@ -588,6 +595,11 @@ class TextLayerRenderTask {
// Always clean-up the temporary canvas once rendering is no longer pending.
this._capability.promise
.finally(() => {
if (!this._enhanceTextSelection) {
// The `textDiv` properties are no longer needed.
this._textDivProperties = null;
}

if (this._layoutTextCtx) {
// Zeroing the width and height cause Firefox to release graphics
// resources immediately, which can greatly reduce memory consumption.
Expand Down Expand Up @@ -679,8 +691,11 @@ class TextLayerRenderTask {
const { width } = this._layoutTextCtx.measureText(textDiv.textContent);

if (width > 0) {
textDivProperties.scale = textDivProperties.canvasWidth / width;
transform = `scaleX(${textDivProperties.scale})`;
const scale = textDivProperties.canvasWidth / width;
if (this._enhanceTextSelection) {
textDivProperties.scale = scale;
}
transform = `scaleX(${scale})`;
}
}
if (textDivProperties.angle !== 0) {
Expand Down