mirror of https://github.com/axmolengine/axmol.git
Merge pull request #14801 from xiaofeng11/v3.10
Merge https://github.com/cocos2d/cocos2d-x/pull/14784 to V3.10
This commit is contained in:
commit
b219828d69
|
@ -63,17 +63,34 @@ typedef struct
|
||||||
unsigned char* data;
|
unsigned char* data;
|
||||||
} tImageInfo;
|
} tImageInfo;
|
||||||
|
|
||||||
|
static CGSize _calculateStringSize(NSString *str, id font, CGSize *constrainSize)
|
||||||
|
{
|
||||||
|
CGSize textRect = CGSizeZero;
|
||||||
|
textRect.width = constrainSize->width > 0 ? constrainSize->width
|
||||||
|
: 0x7fffffff;
|
||||||
|
textRect.height = constrainSize->height > 0 ? constrainSize->height
|
||||||
|
: 0x7fffffff;
|
||||||
|
|
||||||
|
CGSize dim;
|
||||||
|
NSDictionary *attibutes = @{NSFontAttributeName:font};
|
||||||
|
dim = [str boundingRectWithSize:textRect options:(NSStringDrawingOptions)(NSStringDrawingUsesLineFragmentOrigin) attributes:attibutes context:nil].size;
|
||||||
|
|
||||||
|
dim.width = ceilf(dim.width);
|
||||||
|
dim.height = ceilf(dim.height);
|
||||||
|
|
||||||
|
return dim;
|
||||||
|
}
|
||||||
|
|
||||||
static bool _initWithString(const char * text, Device::TextAlign align, const char * fontName, int size, tImageInfo* info, const Color3B* fontColor, int fontAlpha)
|
static bool _initWithString(const char * text, Device::TextAlign align, const char * fontName, int size, tImageInfo* info, const Color3B* fontColor, int fontAlpha)
|
||||||
{
|
{
|
||||||
bool ret = false;
|
bool ret = false;
|
||||||
|
|
||||||
CCASSERT(text, "Invalid pText");
|
CCASSERT(text, "Invalid text");
|
||||||
CCASSERT(info, "Invalid pInfo");
|
CCASSERT(info, "Invalid info");
|
||||||
|
|
||||||
do {
|
do {
|
||||||
NSString * string = [NSString stringWithUTF8String:text];
|
NSString * string = [NSString stringWithUTF8String:text];
|
||||||
NSString * fntName = [NSString stringWithUTF8String:fontName];
|
NSString * fntName = [NSString stringWithUTF8String:fontName];
|
||||||
|
|
||||||
fntName = [[fntName lastPathComponent] stringByDeletingPathExtension];
|
fntName = [[fntName lastPathComponent] stringByDeletingPathExtension];
|
||||||
|
|
||||||
// font
|
// font
|
||||||
|
@ -82,7 +99,6 @@ static bool _initWithString(const char * text, Device::TextAlign align, const ch
|
||||||
traits:NSUnboldFontMask | NSUnitalicFontMask
|
traits:NSUnboldFontMask | NSUnitalicFontMask
|
||||||
weight:0
|
weight:0
|
||||||
size:size];
|
size:size];
|
||||||
|
|
||||||
if (font == nil) {
|
if (font == nil) {
|
||||||
font = [[NSFontManager sharedFontManager]
|
font = [[NSFontManager sharedFontManager]
|
||||||
fontWithFamily:@"Arial"
|
fontWithFamily:@"Arial"
|
||||||
|
@ -100,17 +116,23 @@ static bool _initWithString(const char * text, Device::TextAlign align, const ch
|
||||||
foregroundColor = [NSColor whiteColor];
|
foregroundColor = [NSColor whiteColor];
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// alignment
|
||||||
// alignment, linebreak
|
|
||||||
unsigned horiFlag = (int)align & 0x0f;
|
unsigned horiFlag = (int)align & 0x0f;
|
||||||
unsigned vertFlag = ((int)align >> 4) & 0x0f;
|
NSTextAlignment textAlign = NSLeftTextAlignment;
|
||||||
NSTextAlignment textAlign = (2 == horiFlag) ? NSRightTextAlignment
|
switch (horiFlag) {
|
||||||
: (3 == horiFlag) ? NSCenterTextAlignment
|
case 2:
|
||||||
: NSLeftTextAlignment;
|
textAlign = NSRightTextAlignment;
|
||||||
|
break;
|
||||||
|
case 3:
|
||||||
|
textAlign = NSCenterTextAlignment;
|
||||||
|
break;
|
||||||
|
default:
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
|
||||||
NSMutableParagraphStyle *paragraphStyle = [[[NSMutableParagraphStyle alloc] init] autorelease];
|
NSMutableParagraphStyle *paragraphStyle = [[[NSMutableParagraphStyle alloc] init] autorelease];
|
||||||
[paragraphStyle setParagraphStyle:[NSParagraphStyle defaultParagraphStyle]];
|
[paragraphStyle setParagraphStyle:[NSParagraphStyle defaultParagraphStyle]];
|
||||||
[paragraphStyle setLineBreakMode:NSLineBreakByCharWrapping];
|
[paragraphStyle setLineBreakMode:NSLineBreakByWordWrapping];
|
||||||
[paragraphStyle setAlignment:textAlign];
|
[paragraphStyle setAlignment:textAlign];
|
||||||
|
|
||||||
// attribute
|
// attribute
|
||||||
|
@ -118,53 +140,14 @@ static bool _initWithString(const char * text, Device::TextAlign align, const ch
|
||||||
foregroundColor,NSForegroundColorAttributeName,
|
foregroundColor,NSForegroundColorAttributeName,
|
||||||
font, NSFontAttributeName,
|
font, NSFontAttributeName,
|
||||||
paragraphStyle, NSParagraphStyleAttributeName, nil];
|
paragraphStyle, NSParagraphStyleAttributeName, nil];
|
||||||
|
|
||||||
// linebreak
|
|
||||||
if (info->width > 0) {
|
|
||||||
if ([string sizeWithAttributes:tokenAttributesDict].width > info->width) {
|
|
||||||
int nextLineTop = 0;
|
|
||||||
NSMutableString *lineBreak = [[[NSMutableString alloc] init] autorelease];
|
|
||||||
NSUInteger length = [string length];
|
|
||||||
NSRange range = NSMakeRange(0, 1);
|
|
||||||
NSSize textSize;
|
|
||||||
NSUInteger lastBreakLocation = 0;
|
|
||||||
NSUInteger insertCount = 0;
|
|
||||||
for (NSUInteger i = 0; i < length; i++) {
|
|
||||||
range.location = i;
|
|
||||||
NSString *character = [string substringWithRange:range];
|
|
||||||
[lineBreak appendString:character];
|
|
||||||
if ([@"!?.,-= " rangeOfString:character].location != NSNotFound) {
|
|
||||||
lastBreakLocation = i + insertCount;
|
|
||||||
}
|
|
||||||
textSize = [lineBreak sizeWithAttributes:tokenAttributesDict];
|
|
||||||
if ((int)textSize.width > info->width) {
|
|
||||||
if(lastBreakLocation > 0) {
|
|
||||||
[lineBreak insertString:@"\r" atIndex:lastBreakLocation];
|
|
||||||
lastBreakLocation = 0;
|
|
||||||
}
|
|
||||||
else {
|
|
||||||
[lineBreak insertString:@"\r" atIndex:[lineBreak length] - 1];
|
|
||||||
}
|
|
||||||
insertCount += 1;
|
|
||||||
|
|
||||||
nextLineTop += (int)textSize.height;
|
|
||||||
if(info->height > 0 && nextLineTop > (int)info->height)
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
string = lineBreak;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
NSAttributedString *stringWithAttributes =[[[NSAttributedString alloc] initWithString:string
|
NSAttributedString *stringWithAttributes =[[[NSAttributedString alloc] initWithString:string
|
||||||
attributes:tokenAttributesDict] autorelease];
|
attributes:tokenAttributesDict] autorelease];
|
||||||
|
|
||||||
NSSize realDimensions = [stringWithAttributes size];
|
CGSize dimensions = CGSizeMake(info->width, info->height);
|
||||||
|
NSSize realDimensions = _calculateStringSize(string, font, &dimensions);
|
||||||
// Mac crashes if the width or height is 0
|
// Mac crashes if the width or height is 0
|
||||||
CC_BREAK_IF(realDimensions.width <= 0 || realDimensions.height <= 0);
|
CC_BREAK_IF(realDimensions.width <= 0 || realDimensions.height <= 0);
|
||||||
|
|
||||||
CGSize dimensions = CGSizeMake(info->width, info->height);
|
|
||||||
if(dimensions.width <= 0.f) {
|
if(dimensions.width <= 0.f) {
|
||||||
dimensions.width = realDimensions.width;
|
dimensions.width = realDimensions.width;
|
||||||
}
|
}
|
||||||
|
@ -172,20 +155,17 @@ static bool _initWithString(const char * text, Device::TextAlign align, const ch
|
||||||
dimensions.height = realDimensions.height;
|
dimensions.height = realDimensions.height;
|
||||||
}
|
}
|
||||||
|
|
||||||
NSInteger POTWide = dimensions.width;
|
|
||||||
NSInteger POTHigh = dimensions.height;
|
|
||||||
unsigned char* data = nullptr;
|
|
||||||
|
|
||||||
//Alignment
|
//Alignment
|
||||||
CGFloat xPadding = 0;
|
CGFloat xPadding = 0;
|
||||||
switch (textAlign) {
|
switch (textAlign) {
|
||||||
case NSLeftTextAlignment: xPadding = 0; break;
|
case NSLeftTextAlignment: xPadding = 0; break;
|
||||||
case NSCenterTextAlignment: xPadding = (dimensions.width-realDimensions.width)/2.0f; break;
|
case NSCenterTextAlignment: xPadding = (dimensions.width - realDimensions.width) / 2.0f; break;
|
||||||
case NSRightTextAlignment: xPadding = dimensions.width-realDimensions.width; break;
|
case NSRightTextAlignment: xPadding = dimensions.width - realDimensions.width; break;
|
||||||
default: break;
|
default: break;
|
||||||
}
|
}
|
||||||
|
|
||||||
CGFloat yPadding = 0.f;
|
CGFloat yPadding = 0.f;
|
||||||
|
unsigned vertFlag = ((int)align >> 4) & 0x0f;
|
||||||
switch (vertFlag) {
|
switch (vertFlag) {
|
||||||
// align to top
|
// align to top
|
||||||
case 1: yPadding = dimensions.height - realDimensions.height; break;
|
case 1: yPadding = dimensions.height - realDimensions.height; break;
|
||||||
|
@ -196,29 +176,23 @@ static bool _initWithString(const char * text, Device::TextAlign align, const ch
|
||||||
default: break;
|
default: break;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
NSInteger POTWide = dimensions.width;
|
||||||
|
NSInteger POTHigh = dimensions.height;
|
||||||
NSRect textRect = NSMakeRect(xPadding, POTHigh - dimensions.height + yPadding, realDimensions.width, realDimensions.height);
|
NSRect textRect = NSMakeRect(xPadding, POTHigh - dimensions.height + yPadding, realDimensions.width, realDimensions.height);
|
||||||
//Disable antialias
|
|
||||||
|
|
||||||
[[NSGraphicsContext currentContext] setShouldAntialias:NO];
|
[[NSGraphicsContext currentContext] setShouldAntialias:NO];
|
||||||
|
|
||||||
NSImage *image = [[NSImage alloc] initWithSize:NSMakeSize(POTWide, POTHigh)];
|
NSImage *image = [[NSImage alloc] initWithSize:NSMakeSize(POTWide, POTHigh)];
|
||||||
|
|
||||||
[image lockFocus];
|
[image lockFocus];
|
||||||
|
// patch for mac retina display and lableTTF
|
||||||
// patch for mac retina display and labelTTF
|
|
||||||
[[NSAffineTransform transform] set];
|
[[NSAffineTransform transform] set];
|
||||||
|
|
||||||
//[stringWithAttributes drawAtPoint:NSMakePoint(xPadding, offsetY)]; // draw at offset position
|
|
||||||
[stringWithAttributes drawInRect:textRect];
|
[stringWithAttributes drawInRect:textRect];
|
||||||
//[stringWithAttributes drawInRect:textRect withAttributes:tokenAttributesDict];
|
|
||||||
NSBitmapImageRep *bitmap = [[NSBitmapImageRep alloc] initWithFocusedViewRect:NSMakeRect (0.0f, 0.0f, POTWide, POTHigh)];
|
NSBitmapImageRep *bitmap = [[NSBitmapImageRep alloc] initWithFocusedViewRect:NSMakeRect (0.0f, 0.0f, POTWide, POTHigh)];
|
||||||
[image unlockFocus];
|
[image unlockFocus];
|
||||||
|
|
||||||
data = (unsigned char*) [bitmap bitmapData]; //Use the same buffer to improve the performance.
|
auto data = (unsigned char*) [bitmap bitmapData]; //Use the same buffer to improve the performance.
|
||||||
|
|
||||||
NSUInteger textureSize = POTWide*POTHigh*4;
|
NSUInteger textureSize = POTWide * POTHigh * 4;
|
||||||
|
auto dataNew = (unsigned char*)malloc(sizeof(unsigned char) * textureSize);
|
||||||
unsigned char* dataNew = (unsigned char*)malloc(sizeof(unsigned char) * textureSize);
|
|
||||||
if (dataNew) {
|
if (dataNew) {
|
||||||
memcpy(dataNew, data, textureSize);
|
memcpy(dataNew, data, textureSize);
|
||||||
// output params
|
// output params
|
||||||
|
@ -268,4 +242,4 @@ void Device::vibrate(float duration)
|
||||||
|
|
||||||
NS_CC_END
|
NS_CC_END
|
||||||
|
|
||||||
#endif // CC_TARGET_PLATFORM == CC_PLATFORM_MAC
|
#endif // CC_TARGET_PLATFORM == CC_PLATFORM_MAC
|
Loading…
Reference in New Issue