Skip to content

Commit 59d4645

Browse files
author
pandamicro
committed
Improve label adaptation performance
1 parent 926c9e4 commit 59d4645

File tree

3 files changed

+57
-27
lines changed

3 files changed

+57
-27
lines changed

Diff for: cocos2d/core/labelttf/CCLabelTTF.js

+3-1
Original file line numberDiff line numberDiff line change
@@ -133,7 +133,9 @@ cc.LabelTTF = cc.Sprite.extend(/** @lends cc.LabelTTF# */{
133133
this._renderCmd._setFontStyle(this._fontName, fontSize, this._fontStyle, this._fontWeight);
134134
this.string = strInfo;
135135
this._renderCmd._setColorsString();
136-
this._renderCmd._updateTexture();
136+
if (this._string) {
137+
this._renderCmd._updateTexture();
138+
}
137139
this._setUpdateTextureDirty();
138140

139141
// Needed for high dpi text.

Diff for: cocos2d/core/labelttf/CCLabelTTFCanvasRenderCmd.js

+34-15
Original file line numberDiff line numberDiff line change
@@ -111,6 +111,7 @@ cc.LabelTTF._firsrEnglish = /^[a-zA-Z0-9ÄÖÜäöüßéèçàùêâîôû]/;
111111

112112
this._isMultiLine = false;
113113
this._measureConfig();
114+
var textWidthCache = {};
114115
if (locDimensionsWidth !== 0) {
115116
// Content processing
116117
this._strings = node._string.split('\n');
@@ -121,7 +122,13 @@ cc.LabelTTF._firsrEnglish = /^[a-zA-Z0-9ÄÖÜäöüßéèçàùêâîôû]/;
121122
} else {
122123
this._strings = node._string.split('\n');
123124
for (i = 0, strLength = this._strings.length; i < strLength; i++) {
124-
locLineWidth.push(this._measure(this._strings[i]));
125+
if(this._strings[i]) {
126+
var measuredWidth = this._measure(this._strings[i]);
127+
locLineWidth.push(measuredWidth);
128+
textWidthCache[this._strings[i]] = measuredWidth;
129+
} else {
130+
locLineWidth.push(0);
131+
}
125132
}
126133
}
127134

@@ -139,14 +146,18 @@ cc.LabelTTF._firsrEnglish = /^[a-zA-Z0-9ÄÖÜäöüßéèçàùêâîôû]/;
139146

140147
//get offset for stroke and shadow
141148
if (locDimensionsWidth === 0) {
142-
if (this._isMultiLine)
143-
locSize = cc.size(
144-
Math.ceil(Math.max.apply(Math, locLineWidth) + locStrokeShadowOffsetX),
145-
Math.ceil((this._fontClientHeight * pixelRatio * this._strings.length) + locStrokeShadowOffsetY));
146-
else
147-
locSize = cc.size(
148-
Math.ceil(this._measure(node._string) + locStrokeShadowOffsetX),
149-
Math.ceil(this._fontClientHeight * pixelRatio + locStrokeShadowOffsetY));
149+
if (this._isMultiLine) {
150+
locSize = cc.size(Math.ceil(Math.max.apply(Math, locLineWidth) + locStrokeShadowOffsetX),
151+
Math.ceil((this._fontClientHeight * pixelRatio * this._strings.length) + locStrokeShadowOffsetY));
152+
}
153+
else {
154+
var measuredWidth = textWidthCache[node._string];
155+
if(!measuredWidth && node._string) {
156+
measuredWidth = this._measure(node._string);
157+
}
158+
locSize = cc.size(Math.ceil((measuredWidth ? measuredWidth : 0) + locStrokeShadowOffsetX),
159+
Math.ceil(this._fontClientHeight * pixelRatio + locStrokeShadowOffsetY));
160+
}
150161
} else {
151162
if (node._dimensions.height === 0) {
152163
if (this._isMultiLine)
@@ -381,7 +392,7 @@ cc.LabelTTF._firsrEnglish = /^[a-zA-Z0-9ÄÖÜäöüßéèçàùêâîôû]/;
381392
this._labelContext = locCanvas.getContext("2d");
382393
};
383394

384-
cc.LabelTTF.CacheRenderCmd.prototype = Object.create( cc.LabelTTF.RenderCmd.prototype);
395+
cc.LabelTTF.CacheRenderCmd.prototype = Object.create(cc.LabelTTF.RenderCmd.prototype);
385396
cc.inject(cc.LabelTTF.RenderCmd.prototype, cc.LabelTTF.CacheRenderCmd.prototype);
386397

387398
var proto = cc.LabelTTF.CacheRenderCmd.prototype;
@@ -397,7 +408,7 @@ cc.LabelTTF._firsrEnglish = /^[a-zA-Z0-9ÄÖÜäöüßéèçàùêâîôû]/;
397408

398409
var locContext = this._labelContext, locLabelCanvas = this._labelCanvas;
399410

400-
if(!node._texture){
411+
if (!node._texture) {
401412
var labelTexture = new cc.Texture2D();
402413
labelTexture.initWithElement(this._labelCanvas);
403414
node.setTexture(labelTexture);
@@ -430,7 +441,11 @@ cc.LabelTTF._firsrEnglish = /^[a-zA-Z0-9ÄÖÜäöüßéèçàùêâîôû]/;
430441
};
431442

432443
proto._measure = function (text) {
433-
return this._labelContext.measureText(text).width;
444+
if (text) {
445+
return this._labelContext.measureText(text).width;
446+
} else {
447+
return 0;
448+
}
434449
};
435450

436451
})();
@@ -462,9 +477,13 @@ cc.LabelTTF._firsrEnglish = /^[a-zA-Z0-9ÄÖÜäöüßéèçàùêâîôû]/;
462477
};
463478

464479
proto._measure = function (text) {
465-
var context = cc._renderContext.getContext();
466-
context.font = this._fontStyleStr;
467-
return context.measureText(text).width;
480+
if(text) {
481+
var context = cc._renderContext.getContext();
482+
context.font = this._fontStyleStr;
483+
return context.measureText(text).width;
484+
} else {
485+
return 0;
486+
}
468487
};
469488

470489
proto._updateTexture = function () {

Diff for: cocos2d/labels/CCLabelBMFont.js

+20-11
Original file line numberDiff line numberDiff line change
@@ -128,7 +128,9 @@ cc.LabelBMFont = cc.SpriteBatchNode.extend(/** @lends cc.LabelBMFont# */{
128128
}
129129
}
130130
if (this._textureLoaded) {
131-
this.createFontChars();
131+
if(this._string && this._string.length > 0) {
132+
this.createFontChars();
133+
}
132134
if (needUpdateLabel)
133135
this.updateLabel();
134136
}
@@ -273,6 +275,9 @@ cc.LabelBMFont = cc.SpriteBatchNode.extend(/** @lends cc.LabelBMFont# */{
273275
* updates the font chars based on the string to render
274276
*/
275277
createFontChars: function () {
278+
var locStr = this._string;
279+
var stringLen = locStr ? locStr.length : 0;
280+
276281
var self = this;
277282
var cmd = this._renderCmd;
278283
var locTexture = cmd._texture || this._texture;
@@ -285,11 +290,6 @@ cc.LabelBMFont = cc.SpriteBatchNode.extend(/** @lends cc.LabelBMFont# */{
285290

286291
var quantityOfLines = 1;
287292

288-
var locStr = self._string;
289-
var stringLen = locStr ? locStr.length : 0;
290-
291-
if (stringLen === 0)
292-
return;
293293

294294
var i, locCfg = self._config, locKerningDict = locCfg.kerningDict,
295295
locCommonH = locCfg.commonHeight, locFontDict = locCfg.fontDefDictionary;
@@ -383,13 +383,17 @@ cc.LabelBMFont = cc.SpriteBatchNode.extend(/** @lends cc.LabelBMFont# */{
383383
var self = this;
384384
var locChildren = self._children;
385385
if (locChildren) {
386-
for (var i = 0, li = locChildren.length; i < li; i++) {
386+
var length = locChildren.length;
387+
for (var i = 0, li = length; i < li; i++) {
387388
var node = locChildren[i];
388389
if (node) node.visible = false;
389390
}
390391
}
391-
if (self._config)
392-
self.createFontChars();
392+
if (self._config) {
393+
if(self._string && self._string.length > 0) {
394+
self.createFontChars();
395+
}
396+
}
393397

394398
if (!fromUpdate)
395399
self.updateLabel();
@@ -715,15 +719,20 @@ cc.LabelBMFont = cc.SpriteBatchNode.extend(/** @lends cc.LabelBMFont# */{
715719
var self1 = this;
716720
self1._textureLoaded = true;
717721
self1.setTexture(sender);
718-
self1.createFontChars();
722+
if(self1._string && self1._string.length > 0) {
723+
self1.createFontChars();
724+
}
725+
719726
self1._changeTextureColor();
720727
self1.updateLabel();
721728

722729
self1.dispatchEvent("load");
723730
}, self);
724731
} else {
725732
self.setTexture(texture);
726-
self.createFontChars();
733+
if(self._string && self._string.length > 0) {
734+
self.createFontChars();
735+
}
727736
}
728737
}
729738
},

0 commit comments

Comments
 (0)