[TextLayer] Remove setAttribute usage in appendText (issue 8066)

One of the motivations for using `setAttribute` in the first place was to support more efficient DOM updates in the `expandTextDivs` method, since performance of the `enhanceTextSelection` mode can be somewhat bad when there's a lot of `textDivs` on the page.

With recent `TextLayer` changes/optimizations it's no longer necessary to store a complete `style`-string for every `textDiv`, and we can thus re-visit the `setAttribute` usage.
Note that with the current code, in `appendText`, there's only *one* string per `textDiv` which avoids a bunch of temporary strings. While the changes in this patch means that there's now *three* strings per `textDiv` instead, the total length of these strings are now quite a bit shorter (42 characters to be exact).
This commit is contained in:
Jonas Jenwald 2019-08-28 16:17:30 +02:00
parent 106b239c5d
commit 667e548e5f

View File

@ -48,11 +48,6 @@ var renderTextLayer = (function renderTextLayerClosure() {
return !NonWhitespaceRegexp.test(str); return !NonWhitespaceRegexp.test(str);
} }
// Text layers may contain many thousands of divs, and using `styleBuf` avoids
// creating many intermediate strings when building their 'style' properties.
var styleBuf = ['left: ', 0, 'px; top: ', 0, 'px; font-size: ', 0,
'px; font-family: ', '', ';'];
function appendText(task, geom, styles) { function appendText(task, geom, styles) {
// Initialize all used properties to keep the caches monomorphic. // Initialize all used properties to keep the caches monomorphic.
var textDiv = document.createElement('span'); var textDiv = document.createElement('span');
@ -97,11 +92,12 @@ var renderTextLayer = (function renderTextLayerClosure() {
left = tx[4] + (fontAscent * Math.sin(angle)); left = tx[4] + (fontAscent * Math.sin(angle));
top = tx[5] - (fontAscent * Math.cos(angle)); top = tx[5] - (fontAscent * Math.cos(angle));
} }
styleBuf[1] = left; // Setting the style properties individually, rather than all at once,
styleBuf[3] = top; // should be OK since the `textDiv` isn't appended to the document yet.
styleBuf[5] = fontHeight; textDiv.style.left = `${left}px`;
styleBuf[7] = style.fontFamily; textDiv.style.top = `${top}px`;
textDiv.setAttribute('style', styleBuf.join('')); textDiv.style.fontSize = `${fontHeight}px`;
textDiv.style.fontFamily = style.fontFamily;
textDiv.textContent = geom.str; textDiv.textContent = geom.str;
// `fontName` is only used by the FontInspector, and we only use `dataset` // `fontName` is only used by the FontInspector, and we only use `dataset`