Skip to content

Commit ac8fcd1

Browse files
committed
Rolls back changes made to CCLabelTTF
1 parent 2267752 commit ac8fcd1

File tree

1 file changed

+51
-119
lines changed

1 file changed

+51
-119
lines changed

cocos2d/CCLabelTTF.m

Lines changed: 51 additions & 119 deletions
Original file line numberDiff line numberDiff line change
@@ -329,74 +329,10 @@ - (void) setTextureDirty
329329
_isTextureDirty = YES;
330330
}
331331

332-
-(CCRenderState *)renderState
333-
{
334-
if(_renderState == nil){
335-
// Allowing the uniforms to be copied speeds up the rendering by making the render state immutable.
336-
// Copy the uniforms if custom uniforms are not being used.
337-
BOOL copyUniforms = self.hasDefaultShaderUniforms;
338-
339-
// Create an uncached renderstate so the texture can be released before the renderstate cache is flushed.
340-
_renderState = [[CCRenderState alloc] initWithBlendMode:_blendMode shader:_shader shaderUniforms:self.shaderUniforms copyUniforms:copyUniforms];
341-
}
342-
343-
return _renderState;
344-
}
345-
346332

347333
#pragma mark -
348334
#pragma mark Render Font Mac & iOS 6
349335

350-
351-
- (void) drawAttributedString:(NSAttributedString *)attrString inContext:(CGContextRef) context inRect:(CGRect)rect {
352-
CGFloat contextHeight = CGBitmapContextGetHeight(context);
353-
CTFramesetterRef framesetter = CTFramesetterCreateWithAttributedString((__bridge CFAttributedStringRef)attrString);
354-
CGPathRef path = CGPathCreateWithRect(CGRectMake(rect.origin.x, contextHeight-rect.origin.y-rect.size.height, rect.size.width, rect.size.height), NULL);
355-
CTFrameRef frame = CTFramesetterCreateFrame(framesetter, CFRangeMake(0, 0), path, NULL);
356-
CFRelease(framesetter);
357-
CGPathRelease(path);
358-
CGContextSaveGState(context);
359-
CGContextSetTextMatrix (context, CGAffineTransformIdentity);
360-
CGContextTranslateCTM(context, 0.0f, contextHeight);
361-
CGContextScaleCTM(context, 1.0f, -1.0f);
362-
CTFrameDraw(frame, context);
363-
CGContextRestoreGState(context);
364-
CFRelease(frame);
365-
366-
}
367-
368-
- (void) drawString:(NSString *)string withFont:(CTFontRef)font inContext:(CGContextRef) context inRect:(CGRect)rect {
369-
NSAttributedString *attrString = [[NSAttributedString alloc] initWithString:string attributes:@{(NSString *)kCTFontAttributeName: (__bridge id)font}];
370-
[self drawAttributedString:attrString inContext:context inRect:rect];
371-
}
372-
373-
- (CGSize) sizeForString:(NSString *)string withFont:(CTFontRef)font constrainedToSize:(CGSize) size {
374-
NSAttributedString *attrString = [[NSAttributedString alloc] initWithString:string attributes:@{(NSString *)kCTFontAttributeName: (__bridge id)font}];
375-
376-
return [self sizeForAttributedString:attrString constrainedToSize:size];
377-
378-
}
379-
380-
- (CGSize) sizeForString:(NSString *)string withFont:(CTFontRef)font constrainedToWidth:(CGFloat) width {
381-
return [self sizeForString:string withFont:font constrainedToSize:CGSizeMake(width, 0)];
382-
}
383-
384-
- (CGSize) sizeForString:(NSString *)string withFont:(CTFontRef)font {
385-
return [self sizeForString:string withFont:font constrainedToSize:CGSizeZero];
386-
}
387-
388-
- (CGSize) sizeForAttributedString:(NSAttributedString *)attrString constrainedToSize:(CGSize) size {
389-
CTFramesetterRef framesetter = CTFramesetterCreateWithAttributedString((__bridge CFAttributedStringRef)attrString);
390-
391-
CFRange suggestedRange;
392-
CGSize suggestedSize = CTFramesetterSuggestFrameSizeWithConstraints(framesetter, CFRangeMake(0, 0), NULL, size, &suggestedRange);
393-
CFRelease(framesetter);
394-
395-
return suggestedSize;
396-
397-
}
398-
399-
400336
- (BOOL) updateTexture
401337
{
402338
if (!_attributedString) return NO;
@@ -422,43 +358,31 @@ - (BOOL) updateTexture
422358
if (_shadowColor.alpha > 0) useFullColor = YES;
423359
if (_outlineColor.alpha > 0 && _outlineWidth > 0) useFullColor = YES;
424360

361+
#ifdef __CC_PLATFORM_IOS
425362

426-
#if defined(__CC_PLATFORM_IOS)
427-
if ([formattedAttributedString hasAttribute:NSForegroundColorAttributeName]) {
428-
UIColor * color = [formattedAttributedString attribute:NSForegroundColorAttributeName atIndex:0 effectiveRange:NULL];
429-
[formattedAttributedString addAttribute:(id)kCTForegroundColorFromContextAttributeName value:(__bridge id)color.CGColor range:fullRange];
430-
}
431-
if ([formattedAttributedString hasAttribute:NSFontAttributeName]) {
432-
UIFont * font = [formattedAttributedString attribute:NSFontAttributeName atIndex:0 effectiveRange:NULL];
433-
CTFontRef ctFont = CTFontCreateWithName((__bridge CFStringRef)font.fontName, font.pointSize, NULL);
434-
[formattedAttributedString addAttribute:(id)kCTFontAttributeName value:(__bridge id)ctFont range:fullRange];
435-
CFRelease(ctFont);
436-
}
437-
#endif
438-
439-
#if defined(__CC_PLATFORM_IOS) || defined(__CC_PLATFORM_ANDROID)
440363
// Font color
441-
if (![formattedAttributedString hasAttribute:(id)kCTForegroundColorFromContextAttributeName])
364+
if (![formattedAttributedString hasAttribute:NSForegroundColorAttributeName])
442365
{
443366
if (![_fontColor isEqualToColor:[CCColor whiteColor]])
444367
{
445368
useFullColor = YES;
446369
}
447370

448-
[formattedAttributedString addAttribute:(__bridge id)kCTForegroundColorAttributeName value:(__bridge id)_fontColor.CGColor range:fullRange];
371+
UIColor* color = _fontColor.UIColor;
372+
373+
[formattedAttributedString addAttribute:NSForegroundColorAttributeName value:color range:fullRange];
449374
}
450375
else
451376
{
452377
useFullColor = YES;
453378
}
454379

455380
// Font
456-
if (![formattedAttributedString hasAttribute:(NSString *)kCTFontAttributeName])
381+
if (![formattedAttributedString hasAttribute:NSFontAttributeName])
457382
{
458-
CTFontRef font = CTFontCreateWithName((__bridge CFStringRef)_fontName, _fontSize, NULL);
459-
if (font == NULL) font = CTFontCreateWithName(CFSTR("Helvetica"), _fontSize, NULL);
460-
[formattedAttributedString addAttribute:(NSString *)kCTFontAttributeName value:(__bridge id)font range:fullRange];
461-
CFRelease(font);
383+
UIFont* font = [UIFont fontWithName:_fontName size:_fontSize];
384+
if (!font) font = [UIFont fontWithName:@"Helvetica" size:_fontSize];
385+
[formattedAttributedString addAttribute:NSFontAttributeName value:font range:fullRange];
462386
}
463387

464388
// Shadow
@@ -588,7 +512,11 @@ - (CCTexture*) createTextureWithAttributedString:(NSAttributedString*)attributed
588512
if (dimensions.height == 0)
589513
{
590514
// Get dimensions for string without dimensions of string with variable height
591-
dimensions = [self sizeForAttributedString:attributedString constrainedToSize:dimensions];
515+
#ifdef __CC_PLATFORM_IOS
516+
dimensions = [attributedString boundingRectWithSize:dimensions options:NSStringDrawingUsesLineFragmentOrigin context:NULL].size;
517+
#elif defined(__CC_PLATFORM_MAC)
518+
dimensions = [attributedString boundingRectWithSize:NSSizeFromCGSize(dimensions) options:NSStringDrawingUsesLineFragmentOrigin].size;
519+
#endif
592520

593521
dimensions.width = ceil(dimensions.width);
594522
dimensions.height = ceil(dimensions.height);
@@ -611,7 +539,11 @@ - (CCTexture*) createTextureWithAttributedString:(NSAttributedString*)attributed
611539
if (fontSize)
612540
{
613541
// This is a string that can be resized (it only uses one font and size)
614-
CGSize wantedSize = [self sizeForAttributedString:attributedString constrainedToSize:CGSizeZero];
542+
#ifdef __CC_PLATFORM_IOS
543+
CGSize wantedSize = [attributedString boundingRectWithSize:CGSizeZero options:NSStringDrawingUsesLineFragmentOrigin context:NULL].size;
544+
#elif defined(__CC_PLATFORM_MAC)
545+
CGSize wantedSize = [attributedString boundingRectWithSize:CGSizeZero options:NSStringDrawingUsesLineFragmentOrigin].size;
546+
#endif
615547

616548
CGFloat wScaleFactor = 1;
617549
CGFloat hScaleFactor = 1;
@@ -638,7 +570,11 @@ - (CCTexture*) createTextureWithAttributedString:(NSAttributedString*)attributed
638570
}
639571

640572
// Handle vertical alignment
641-
CGSize actualSize = [self sizeForAttributedString:attributedString constrainedToSize:CGSizeMake(wDrawArea, 0)];
573+
#ifdef __CC_PLATFORM_IOS
574+
CGSize actualSize = [attributedString boundingRectWithSize:CGSizeMake(wDrawArea, 0) options:NSStringDrawingUsesLineFragmentOrigin context:NULL].size;
575+
#elif defined(__CC_PLATFORM_MAC)
576+
CGSize actualSize = NSSizeToCGSize([attributedString boundingRectWithSize:NSMakeSize(wDrawArea, 0) options:NSStringDrawingUsesLineFragmentOrigin].size);
577+
#endif
642578
if (_verticalAlignment == CCVerticalTextAlignmentBottom)
643579
{
644580
yOffset = hDrawArea - actualSize.height;
@@ -689,33 +625,37 @@ - (CCTexture*) createTextureWithAttributedString:(NSAttributedString*)attributed
689625
// Handle shadow
690626
if (hasShadow)
691627
{
692-
CGContextSetShadowWithColor(context, CGSizeMake(shadowOffset.x, -shadowOffset.y), shadowBlurRadius, _shadowColor.CGColor);
628+
UIColor* color = _shadowColor.UIColor;
629+
630+
CGContextSetShadowWithColor(context, CGSizeMake(shadowOffset.x, -shadowOffset.y), shadowBlurRadius, [color CGColor]);
693631
}
694632

695633
// Handle outline
696634
if (hasOutline)
697635
{
636+
UIColor* color = _outlineColor.UIColor;
637+
698638
CGContextSetTextDrawingMode(context, kCGTextFillStroke);
699639
CGContextSetLineWidth(context, outlineWidth * 2);
700640
CGContextSetLineJoin(context, kCGLineJoinRound);
701641

702642
NSMutableAttributedString* outlineString = [attributedString mutableCopy];
703-
[outlineString addAttribute:NSForegroundColorAttributeName value:(__bridge id)_outlineColor.CGColor range:NSMakeRange(0, outlineString.length)];
643+
[outlineString addAttribute:NSForegroundColorAttributeName value:color range:NSMakeRange(0, outlineString.length)];
704644

705-
[self drawAttributedString:outlineString inContext:context inRect:drawArea];
645+
[outlineString drawInRect:drawArea];
706646

707647
// Don't draw shadow for main font
708648
CGContextSetShadowWithColor(context, CGSizeZero, 0, NULL);
709649

710650
if (hasShadow)
711651
{
712652
// Draw outline again because shadow overlap
713-
[self drawAttributedString:outlineString inContext:context inRect:drawArea];
653+
[outlineString drawInRect:drawArea];
714654
}
715655
CGContextSetTextDrawingMode(context, kCGTextFill);
716656
}
717657

718-
[self drawAttributedString:attributedString inContext:context inRect:drawArea];
658+
[attributedString drawInRect:drawArea];
719659

720660
UIGraphicsPopContext();
721661
CGContextRelease(context);
@@ -769,7 +709,7 @@ - (CCTexture*) createTextureWithAttributedString:(NSAttributedString*)attributed
769709
if (hasShadow)
770710
{
771711
CGContextSetShadowWithColor(context, CGSizeZero, 0, NULL);
772-
[self drawAttributedString:effectsString inContext:context inRect:drawArea];
712+
[effectsString drawInRect:drawArea];
773713
}
774714
CGContextSetTextDrawingMode(context, kCGTextFill);
775715
}
@@ -847,7 +787,7 @@ - (CCTexture*) createTextureWithString:(NSString*) string useFullColor:(BOOL)use
847787
{
848788
// Scale everything up by content scale
849789
CGFloat scale = [CCDirector sharedDirector].contentScaleFactor;
850-
CTFontRef font = CTFontCreateWithName((__bridge CFStringRef)string, _fontSize * scale, NULL);
790+
UIFont* font = [UIFont fontWithName:_fontName size:_fontSize * scale];
851791
CGFloat shadowBlurRadius = _shadowBlurRadius * scale;
852792
CGPoint shadowOffset = ccpMult(self.shadowOffsetInPoints, scale);
853793
CGFloat outlineWidth = _outlineWidth * scale;
@@ -888,12 +828,12 @@ - (CCTexture*) createTextureWithString:(NSString*) string useFullColor:(BOOL)use
888828
// Get dimensions for string without dimensions of string with variable height
889829
if (dimensions.width > 0)
890830
{
891-
dimensions = [self sizeForString:string withFont:font constrainedToWidth:dimensions.width];
831+
dimensions = [string sizeWithFont:font forWidth:dimensions.width lineBreakMode:0];
892832
}
893833
else
894834
{
895-
CGSize firstLineSize = [self sizeForString:string withFont:font];
896-
dimensions = [self sizeForString:string withFont:font constrainedToSize:CGSizeMake(firstLineSize.width,1024)];
835+
CGSize firstLineSize = [string sizeWithFont:font];
836+
dimensions = [string sizeWithFont:font constrainedToSize:CGSizeMake(firstLineSize.width,1024) lineBreakMode:0];
897837
}
898838

899839
wDrawArea = dimensions.width;
@@ -910,9 +850,9 @@ - (CCTexture*) createTextureWithString:(NSString*) string useFullColor:(BOOL)use
910850
// Handle strings with fixed dimensions
911851
if (_adjustsFontSizeToFit)
912852
{
913-
CGFloat fontSize = CTFontGetSize(font);
914-
CGSize wantedSizeFirstLine = [self sizeForString:string withFont:font];
915-
CGSize wantedSize = [self sizeForString:string withFont:font constrainedToSize:CGSizeMake(wantedSizeFirstLine.width,1024)];
853+
CGFloat fontSize = font.pointSize;
854+
CGSize wantedSizeFirstLine = [string sizeWithFont:font];
855+
CGSize wantedSize = [string sizeWithFont:font constrainedToSize:CGSizeMake(wantedSizeFirstLine.width, 1024) lineBreakMode:0];
916856

917857
CGFloat wScaleFactor = 1;
918858
CGFloat hScaleFactor = 1;
@@ -933,14 +873,12 @@ - (CCTexture*) createTextureWithString:(NSString*) string useFullColor:(BOOL)use
933873
CGFloat newFontSize = fontSize * scaleFactor;
934874
CGFloat minFontSize = _minimumFontSize * scale;
935875
if (minFontSize && newFontSize < minFontSize) newFontSize = minFontSize;
936-
CTFontRef newFont = CTFontCreateCopyWithAttributes(font, newFontSize, NULL, NULL);
937-
CFRelease(font);
938-
font = newFont;
876+
font = [UIFont fontWithName:font.fontName size:newFontSize];
939877
}
940878
}
941879

942880
// Handle vertical alignment
943-
CGSize actualSize = [self sizeForString:string withFont:font constrainedToSize:CGSizeMake(wDrawArea, 1024)];
881+
CGSize actualSize = [string sizeWithFont:font constrainedToSize:CGSizeMake(wDrawArea, 1024) lineBreakMode:0];
944882

945883
if (_verticalAlignment == CCVerticalTextAlignmentBottom)
946884
{
@@ -989,7 +927,9 @@ - (CCTexture*) createTextureWithString:(NSString*) string useFullColor:(BOOL)use
989927
// Handle shadow
990928
if (hasShadow)
991929
{
992-
CGContextSetShadowWithColor(context, CGSizeMake(shadowOffset.x, -shadowOffset.y), shadowBlurRadius, _shadowColor.CGColor);
930+
UIColor* color = _shadowColor.UIColor;
931+
932+
CGContextSetShadowWithColor(context, CGSizeMake(shadowOffset.x, -shadowOffset.y), shadowBlurRadius, [color CGColor]);
993933
}
994934

995935
// Handle outline
@@ -1003,32 +943,25 @@ - (CCTexture*) createTextureWithString:(NSString*) string useFullColor:(BOOL)use
1003943
CGContextSetLineWidth(context, outlineWidth * 2);
1004944
CGContextSetLineJoin(context, kCGLineJoinRound);
1005945

1006-
[self drawString:string withFont:font inContext:context inRect:drawArea];
946+
[string drawInRect:drawArea withFont:font lineBreakMode:0 alignment:(int)_horizontalAlignment];
1007947

1008948
// Don't draw shadow for main font
1009949
CGContextSetShadowWithColor(context, CGSizeZero, 0, NULL);
1010950

1011951
if (hasShadow)
1012952
{
1013953
// Draw again, because shadows overlap
1014-
[self drawString:string withFont:font inContext:context inRect:drawArea];
954+
[string drawInRect:drawArea withFont:font lineBreakMode:0 alignment:(int)_horizontalAlignment];
1015955
}
1016956

1017957
CGContextSetTextDrawingMode(context, kCGTextFill);
1018958
}
1019959

1020960
// Handle font color
961+
UIColor* color = [UIColor colorWithRed:_fontColor.red green:_fontColor.green blue:_fontColor.blue alpha:_fontColor.alpha];
962+
[color set];
1021963

1022-
CGColorSpaceRef colorspace = CGColorSpaceCreateDeviceRGB();
1023-
const CGFloat components[] = {_fontColor.red, _fontColor.green, _fontColor.blue, _fontColor.alpha};
1024-
CGColorRef color = CGColorCreate(colorspace, components);
1025-
CGColorSpaceRelease(colorspace);
1026-
1027-
CGContextSetFillColorWithColor(context, color);
1028-
CGContextSetStrokeColorWithColor(context, color);
1029-
CGColorRelease(color);
1030-
1031-
[self drawString:string withFont:font inContext:context inRect:drawArea];
964+
[string drawInRect:drawArea withFont:font lineBreakMode:0 alignment:(int)_horizontalAlignment];
1032965

1033966
UIGraphicsPopContext();
1034967
CGContextRelease(context);
@@ -1056,7 +989,6 @@ - (CCTexture*) createTextureWithString:(NSString*) string useFullColor:(BOOL)use
1056989
}
1057990

1058991
free(data);
1059-
CFRelease(font);
1060992

1061993
return texture;
1062994
}

0 commit comments

Comments
 (0)