@@ -329,74 +329,10 @@ - (void) setTextureDirty
329
329
_isTextureDirty = YES ;
330
330
}
331
331
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
-
346
332
347
333
#pragma mark -
348
334
#pragma mark Render Font Mac & iOS 6
349
335
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
-
400
336
- (BOOL ) updateTexture
401
337
{
402
338
if (!_attributedString) return NO ;
@@ -422,43 +358,31 @@ - (BOOL) updateTexture
422
358
if (_shadowColor.alpha > 0 ) useFullColor = YES ;
423
359
if (_outlineColor.alpha > 0 && _outlineWidth > 0 ) useFullColor = YES ;
424
360
361
+ #ifdef __CC_PLATFORM_IOS
425
362
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)
440
363
// Font color
441
- if (![formattedAttributedString hasAttribute: ( id ) kCTForegroundColorFromContextAttributeName ])
364
+ if (![formattedAttributedString hasAttribute: NSForegroundColorAttributeName ])
442
365
{
443
366
if (![_fontColor isEqualToColor: [CCColor whiteColor ]])
444
367
{
445
368
useFullColor = YES ;
446
369
}
447
370
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];
449
374
}
450
375
else
451
376
{
452
377
useFullColor = YES ;
453
378
}
454
379
455
380
// Font
456
- if (![formattedAttributedString hasAttribute: ( NSString *) kCTFontAttributeName ])
381
+ if (![formattedAttributedString hasAttribute: NSFontAttributeName ])
457
382
{
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];
462
386
}
463
387
464
388
// Shadow
@@ -588,7 +512,11 @@ - (CCTexture*) createTextureWithAttributedString:(NSAttributedString*)attributed
588
512
if (dimensions.height == 0 )
589
513
{
590
514
// 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
592
520
593
521
dimensions.width = ceil (dimensions.width );
594
522
dimensions.height = ceil (dimensions.height );
@@ -611,7 +539,11 @@ - (CCTexture*) createTextureWithAttributedString:(NSAttributedString*)attributed
611
539
if (fontSize)
612
540
{
613
541
// 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
615
547
616
548
CGFloat wScaleFactor = 1 ;
617
549
CGFloat hScaleFactor = 1 ;
@@ -638,7 +570,11 @@ - (CCTexture*) createTextureWithAttributedString:(NSAttributedString*)attributed
638
570
}
639
571
640
572
// 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
642
578
if (_verticalAlignment == CCVerticalTextAlignmentBottom)
643
579
{
644
580
yOffset = hDrawArea - actualSize.height ;
@@ -689,33 +625,37 @@ - (CCTexture*) createTextureWithAttributedString:(NSAttributedString*)attributed
689
625
// Handle shadow
690
626
if (hasShadow)
691
627
{
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 ]);
693
631
}
694
632
695
633
// Handle outline
696
634
if (hasOutline)
697
635
{
636
+ UIColor* color = _outlineColor.UIColor ;
637
+
698
638
CGContextSetTextDrawingMode (context, kCGTextFillStroke );
699
639
CGContextSetLineWidth (context, outlineWidth * 2 );
700
640
CGContextSetLineJoin (context, kCGLineJoinRound );
701
641
702
642
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)];
704
644
705
- [self drawAttributedString: outlineString inContext: context inRect : drawArea];
645
+ [outlineString drawInRect : drawArea];
706
646
707
647
// Don't draw shadow for main font
708
648
CGContextSetShadowWithColor (context, CGSizeZero, 0 , NULL );
709
649
710
650
if (hasShadow)
711
651
{
712
652
// Draw outline again because shadow overlap
713
- [self drawAttributedString: outlineString inContext: context inRect : drawArea];
653
+ [outlineString drawInRect : drawArea];
714
654
}
715
655
CGContextSetTextDrawingMode (context, kCGTextFill );
716
656
}
717
657
718
- [self drawAttributedString: attributedString inContext: context inRect : drawArea];
658
+ [attributedString drawInRect : drawArea];
719
659
720
660
UIGraphicsPopContext ();
721
661
CGContextRelease (context);
@@ -769,7 +709,7 @@ - (CCTexture*) createTextureWithAttributedString:(NSAttributedString*)attributed
769
709
if (hasShadow)
770
710
{
771
711
CGContextSetShadowWithColor (context, CGSizeZero, 0 , NULL );
772
- [self drawAttributedString: effectsString inContext: context inRect : drawArea];
712
+ [effectsString drawInRect : drawArea];
773
713
}
774
714
CGContextSetTextDrawingMode (context, kCGTextFill );
775
715
}
@@ -847,7 +787,7 @@ - (CCTexture*) createTextureWithString:(NSString*) string useFullColor:(BOOL)use
847
787
{
848
788
// Scale everything up by content scale
849
789
CGFloat scale = [CCDirector sharedDirector ].contentScaleFactor ;
850
- CTFontRef font = CTFontCreateWithName ((__bridge CFStringRef)string, _fontSize * scale, NULL ) ;
790
+ UIFont* font = [UIFont fontWithName: _fontName size: _fontSize * scale] ;
851
791
CGFloat shadowBlurRadius = _shadowBlurRadius * scale;
852
792
CGPoint shadowOffset = ccpMult (self.shadowOffsetInPoints , scale);
853
793
CGFloat outlineWidth = _outlineWidth * scale;
@@ -888,12 +828,12 @@ - (CCTexture*) createTextureWithString:(NSString*) string useFullColor:(BOOL)use
888
828
// Get dimensions for string without dimensions of string with variable height
889
829
if (dimensions.width > 0 )
890
830
{
891
- dimensions = [self sizeForString: string withFont : font constrainedToWidth : dimensions.width];
831
+ dimensions = [string sizeWithFont : font forWidth : dimensions.width lineBreakMode: 0 ];
892
832
}
893
833
else
894
834
{
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 ];
897
837
}
898
838
899
839
wDrawArea = dimensions.width ;
@@ -910,9 +850,9 @@ - (CCTexture*) createTextureWithString:(NSString*) string useFullColor:(BOOL)use
910
850
// Handle strings with fixed dimensions
911
851
if (_adjustsFontSizeToFit)
912
852
{
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 ];
916
856
917
857
CGFloat wScaleFactor = 1 ;
918
858
CGFloat hScaleFactor = 1 ;
@@ -933,14 +873,12 @@ - (CCTexture*) createTextureWithString:(NSString*) string useFullColor:(BOOL)use
933
873
CGFloat newFontSize = fontSize * scaleFactor;
934
874
CGFloat minFontSize = _minimumFontSize * scale;
935
875
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];
939
877
}
940
878
}
941
879
942
880
// 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 ];
944
882
945
883
if (_verticalAlignment == CCVerticalTextAlignmentBottom)
946
884
{
@@ -989,7 +927,9 @@ - (CCTexture*) createTextureWithString:(NSString*) string useFullColor:(BOOL)use
989
927
// Handle shadow
990
928
if (hasShadow)
991
929
{
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 ]);
993
933
}
994
934
995
935
// Handle outline
@@ -1003,32 +943,25 @@ - (CCTexture*) createTextureWithString:(NSString*) string useFullColor:(BOOL)use
1003
943
CGContextSetLineWidth (context, outlineWidth * 2 );
1004
944
CGContextSetLineJoin (context, kCGLineJoinRound );
1005
945
1006
- [self drawString: string withFont: font inContext: context inRect: drawArea ];
946
+ [string drawInRect: drawArea withFont: font lineBreakMode: 0 alignment: ( int )_horizontalAlignment ];
1007
947
1008
948
// Don't draw shadow for main font
1009
949
CGContextSetShadowWithColor (context, CGSizeZero, 0 , NULL );
1010
950
1011
951
if (hasShadow)
1012
952
{
1013
953
// 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 ];
1015
955
}
1016
956
1017
957
CGContextSetTextDrawingMode (context, kCGTextFill );
1018
958
}
1019
959
1020
960
// Handle font color
961
+ UIColor* color = [UIColor colorWithRed: _fontColor.red green: _fontColor.green blue: _fontColor.blue alpha: _fontColor.alpha];
962
+ [color set ];
1021
963
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];
1032
965
1033
966
UIGraphicsPopContext ();
1034
967
CGContextRelease (context);
@@ -1056,7 +989,6 @@ - (CCTexture*) createTextureWithString:(NSString*) string useFullColor:(BOOL)use
1056
989
}
1057
990
1058
991
free (data);
1059
- CFRelease (font);
1060
992
1061
993
return texture;
1062
994
}
0 commit comments