Browse Source

Don't scale single-char text divs.

This change makes scrolling noticeably smoother on files with many
single-char text divs, such as the one in #1045. The trade-off is that
the visual appearance of text selection in such documents is slightly
worse, because more text divs overlap.

This change also uses `scaleX(N)` instead of `scale(N, 1)`. This might
be marginally more efficient in terms of JS string concatenation.
Nicholas Nethercote 11 years ago
parent
commit
cd61aad24f
  1. 14
      web/text_layer_builder.js

14
web/text_layer_builder.js

@ -83,16 +83,23 @@ var TextLayerBuilder = (function TextLayerBuilderClosure() {
var width = ctx.measureText(textDiv.textContent).width; var width = ctx.measureText(textDiv.textContent).width;
if (width > 0) { if (width > 0) {
textLayerFrag.appendChild(textDiv); textLayerFrag.appendChild(textDiv);
var transform;
if (textDiv.dataset.canvasWidth !== undefined) {
// Dataset values come of type string. // Dataset values come of type string.
var textScale = textDiv.dataset.canvasWidth / width; var textScale = textDiv.dataset.canvasWidth / width;
transform = 'scaleX(' + textScale + ')';
} else {
transform = '';
}
var rotation = textDiv.dataset.angle; var rotation = textDiv.dataset.angle;
var transform = 'scale(' + textScale + ', 1)';
if (rotation) { if (rotation) {
transform = 'rotate(' + rotation + 'deg) ' + transform; transform = 'rotate(' + rotation + 'deg) ' + transform;
} }
if (transform) {
CustomStyle.setProp('transform' , textDiv, transform); CustomStyle.setProp('transform' , textDiv, transform);
} }
} }
}
this.textLayerDiv.appendChild(textLayerFrag); this.textLayerDiv.appendChild(textLayerFrag);
this.renderingDone = true; this.renderingDone = true;
@ -165,11 +172,16 @@ var TextLayerBuilder = (function TextLayerBuilderClosure() {
if (angle !== 0) { if (angle !== 0) {
textDiv.dataset.angle = angle * (180 / Math.PI); textDiv.dataset.angle = angle * (180 / Math.PI);
} }
// We don't bother scaling single-char text divs, because it has very
// little effect on text highlighting. This makes scrolling on docs with
// lots of such divs a lot faster.
if (textDiv.textContent.length > 1) {
if (style.vertical) { if (style.vertical) {
textDiv.dataset.canvasWidth = geom.height * this.viewport.scale; textDiv.dataset.canvasWidth = geom.height * this.viewport.scale;
} else { } else {
textDiv.dataset.canvasWidth = geom.width * this.viewport.scale; textDiv.dataset.canvasWidth = geom.width * this.viewport.scale;
} }
}
}, },
setTextContent: function TextLayerBuilder_setTextContent(textContent) { setTextContent: function TextLayerBuilder_setTextContent(textContent) {

Loading…
Cancel
Save