@@ -111,6 +111,7 @@ cc.LabelTTF._firsrEnglish = /^[a-zA-Z0-9ÄÖÜäöüßéèçàùêâîôû]/;
111
111
112
112
this . _isMultiLine = false ;
113
113
this . _measureConfig ( ) ;
114
+ var textWidthCache = { } ;
114
115
if ( locDimensionsWidth !== 0 ) {
115
116
// Content processing
116
117
this . _strings = node . _string . split ( '\n' ) ;
@@ -121,7 +122,13 @@ cc.LabelTTF._firsrEnglish = /^[a-zA-Z0-9ÄÖÜäöüßéèçàùêâîôû]/;
121
122
} else {
122
123
this . _strings = node . _string . split ( '\n' ) ;
123
124
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
+ }
125
132
}
126
133
}
127
134
@@ -139,14 +146,18 @@ cc.LabelTTF._firsrEnglish = /^[a-zA-Z0-9ÄÖÜäöüßéèçàùêâîôû]/;
139
146
140
147
//get offset for stroke and shadow
141
148
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
+ }
150
161
} else {
151
162
if ( node . _dimensions . height === 0 ) {
152
163
if ( this . _isMultiLine )
@@ -381,7 +392,7 @@ cc.LabelTTF._firsrEnglish = /^[a-zA-Z0-9ÄÖÜäöüßéèçàùêâîôû]/;
381
392
this . _labelContext = locCanvas . getContext ( "2d" ) ;
382
393
} ;
383
394
384
- cc . LabelTTF . CacheRenderCmd . prototype = Object . create ( cc . LabelTTF . RenderCmd . prototype ) ;
395
+ cc . LabelTTF . CacheRenderCmd . prototype = Object . create ( cc . LabelTTF . RenderCmd . prototype ) ;
385
396
cc . inject ( cc . LabelTTF . RenderCmd . prototype , cc . LabelTTF . CacheRenderCmd . prototype ) ;
386
397
387
398
var proto = cc . LabelTTF . CacheRenderCmd . prototype ;
@@ -397,7 +408,7 @@ cc.LabelTTF._firsrEnglish = /^[a-zA-Z0-9ÄÖÜäöüßéèçàùêâîôû]/;
397
408
398
409
var locContext = this . _labelContext , locLabelCanvas = this . _labelCanvas ;
399
410
400
- if ( ! node . _texture ) {
411
+ if ( ! node . _texture ) {
401
412
var labelTexture = new cc . Texture2D ( ) ;
402
413
labelTexture . initWithElement ( this . _labelCanvas ) ;
403
414
node . setTexture ( labelTexture ) ;
@@ -430,7 +441,11 @@ cc.LabelTTF._firsrEnglish = /^[a-zA-Z0-9ÄÖÜäöüßéèçàùêâîôû]/;
430
441
} ;
431
442
432
443
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
+ }
434
449
} ;
435
450
436
451
} ) ( ) ;
@@ -462,9 +477,13 @@ cc.LabelTTF._firsrEnglish = /^[a-zA-Z0-9ÄÖÜäöüßéèçàùêâîôû]/;
462
477
} ;
463
478
464
479
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
+ }
468
487
} ;
469
488
470
489
proto . _updateTexture = function ( ) {
0 commit comments