Skip to content
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

Use a WeakMap in src/display/text_layer.js #7588

Merged
merged 3 commits into from
Sep 4, 2016
Merged
Changes from 1 commit
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
40 changes: 18 additions & 22 deletions src/display/text_layer.js
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,8 @@ var getDefaultSetting = displayDOMUtils.getDefaultSetting;
* initially be set to empty array.
* @property {number} timeout - (optional) Delay in milliseconds before
* rendering of the text runs occurs.
* @property {boolean} enhanceTextSelection - (optional) Whether to turn on the
* text selection enhancement.
*/
var renderTextLayer = (function renderTextLayerClosure() {
var MAX_TEXT_DIVS_TO_RENDER = 100000;
Expand All @@ -56,16 +58,15 @@ var renderTextLayer = (function renderTextLayerClosure() {
return !NonWhitespaceRegexp.test(str);
}

function appendText(textDivs, viewport, geom, styles, bounds,
enhanceTextSelection) {
var style = styles[geom.fontName];
function appendText(task, geom) {
var style = task._textContent.styles[geom.fontName];
var textDiv = document.createElement('div');
textDivs.push(textDiv);
task._textDivs.push(textDiv);
if (isAllWhitespace(geom.str)) {
textDiv.dataset.isWhitespace = true;
return;
}
var tx = Util.transform(viewport.transform, geom.transform);
var tx = Util.transform(task._viewport.transform, geom.transform);
var angle = Math.atan2(tx[1], tx[0]);
if (style.vertical) {
angle += Math.PI / 2;
Expand Down Expand Up @@ -108,19 +109,19 @@ var renderTextLayer = (function renderTextLayerClosure() {
// lots of such divs a lot faster.
if (geom.str.length > 1) {
if (style.vertical) {
textDiv.dataset.canvasWidth = geom.height * viewport.scale;
textDiv.dataset.canvasWidth = geom.height * task._viewport.scale;
} else {
textDiv.dataset.canvasWidth = geom.width * viewport.scale;
textDiv.dataset.canvasWidth = geom.width * task._viewport.scale;
}
}
if (enhanceTextSelection) {
if (task._enhanceTextSelection) {
var angleCos = 1, angleSin = 0;
if (angle !== 0) {
angleCos = Math.cos(angle);
angleSin = Math.sin(angle);
}
var divWidth = (style.vertical ? geom.height : geom.width) *
viewport.scale;
task._viewport.scale;
var divHeight = fontHeight;

var m, b;
Expand All @@ -131,7 +132,7 @@ var renderTextLayer = (function renderTextLayerClosure() {
b = [left, top, left + divWidth, top + divHeight];
}

bounds.push({
task._bounds.push({
left: b[0],
top: b[1],
right: b[2],
Expand Down Expand Up @@ -209,7 +210,10 @@ var renderTextLayer = (function renderTextLayerClosure() {
capability.resolve();
}

function expand(bounds, viewport) {
function expand(task) {
var bounds = task._bounds;
var viewport = task._viewport;

var expanded = expandBounds(viewport.width, viewport.height, bounds);
for (var i = 0; i < expanded.length; i++) {
var div = bounds[i].div;
Expand Down Expand Up @@ -487,8 +491,7 @@ var renderTextLayer = (function renderTextLayerClosure() {
this._textContent = textContent;
this._container = container;
this._viewport = viewport;
textDivs = textDivs || [];
this._textDivs = textDivs;
this._textDivs = textDivs || [];
this._renderingDone = false;
this._canceled = false;
this._capability = createPromiseCapability();
Expand All @@ -513,15 +516,8 @@ var renderTextLayer = (function renderTextLayerClosure() {

_render: function TextLayer_render(timeout) {
var textItems = this._textContent.items;
var styles = this._textContent.styles;
var textDivs = this._textDivs;
var viewport = this._viewport;
var bounds = this._bounds;
var enhanceTextSelection = this._enhanceTextSelection;

for (var i = 0, len = textItems.length; i < len; i++) {
appendText(textDivs, viewport, textItems[i], styles, bounds,
enhanceTextSelection);
appendText(this, textItems[i]);
Copy link
Collaborator

@Snuffleupagus Snuffleupagus Sep 3, 2016

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Since you're passing in textItems separately here, I'm wondering if it wouldn't make sense to still pass in styles in a similar way too?
I.e. re-introduce var styles = this._textContent.styles; above and make this function call appendText(this, textItems[i], styles);?

The reason that I'm asking is that this would get rid of the, comparatively complex given the others, assignment var style = task._textContent.styles[geom.fontName]; in appendText and use the previous var style = styles[geom.fontName]; instead.

}

if (!timeout) { // Render right away
Expand All @@ -540,7 +536,7 @@ var renderTextLayer = (function renderTextLayerClosure() {
return;
}
if (!this._expanded) {
expand(this._bounds, this._viewport);
expand(this);
this._expanded = true;
this._bounds.length = 0;
}
Expand Down