Skip to content

Commit

Permalink
Merge pull request #13976 from Snuffleupagus/_textDivProperties-cleanup
Browse files Browse the repository at this point in the history
Reduce the size of `TextLayerRenderTask._textDivProperties` in "regular" text-selection mode
  • Loading branch information
timvandermeij authored Sep 5, 2021
2 parents e965aa3 + 4c1b586 commit d95f680
Showing 1 changed file with 29 additions and 14 deletions.
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

0 comments on commit d95f680

Please # to comment.