Mac label v4 (#20125)

* fix system font issue on macOS Catalina
This commit is contained in:
minggo 2019-09-17 13:35:19 +08:00 committed by GitHub
parent f3caa09fd6
commit f3b09563f8
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
1 changed files with 29 additions and 30 deletions

View File

@ -263,7 +263,6 @@ static bool _initWithString(const char * text, Device::TextAlign align, const ch
// alignment
NSTextAlignment textAlign = FontUtils::_calculateTextAlignment(align);
NSMutableParagraphStyle *paragraphStyle = FontUtils::_calculateParagraphStyle(enableWrap, overflow);
[paragraphStyle setAlignment:textAlign];
@ -279,48 +278,50 @@ static bool _initWithString(const char * text, Device::TextAlign align, const ch
NSSize realDimensions;
if (overflow == 2) {
if (overflow == 2)
realDimensions = _calculateRealSizeForString(&stringWithAttributes, font, dimensions, enableWrap);
} else {
realDimensions = _calculateStringSize(stringWithAttributes, font, &dimensions, enableWrap, overflow);
}
else
realDimensions = _calculateStringSize(stringWithAttributes, font, &dimensions, enableWrap, overflow);
// Mac crashes if the width or height is 0
CC_BREAK_IF(realDimensions.width <= 0 || realDimensions.height <= 0);
if(dimensions.width <= 0.f) {
if(dimensions.width <= 0.f)
dimensions.width = realDimensions.width;
}
if (dimensions.height <= 0.f) {
dimensions.height = realDimensions.height;
}
if (dimensions.height <= 0.f)
dimensions.height = realDimensions.height;
//Alignment
CGFloat xPadding = FontUtils::_calculateTextDrawStartWidth(align, realDimensions, dimensions);
CGFloat yPadding = _calculateTextDrawStartHeight(align, realDimensions, dimensions);
NSInteger POTWide = dimensions.width;
NSInteger POTHigh = dimensions.height;
NSRect textRect = NSMakeRect(xPadding, POTHigh - dimensions.height + yPadding,
realDimensions.width, realDimensions.height);
[[NSGraphicsContext currentContext] setShouldAntialias:NO];
NSImage *image = [[NSImage alloc] initWithSize:NSMakeSize(POTWide, POTHigh)];
[image lockFocus];
// patch for mac retina display and lableTTF
[[NSAffineTransform transform] set];
NSBitmapImageRep* offscreenRep = [[[NSBitmapImageRep alloc]
initWithBitmapDataPlanes:NULL
pixelsWide:POTWide
pixelsHigh:POTHigh
bitsPerSample:8
samplesPerPixel:4
hasAlpha:YES
isPlanar:NO
colorSpaceName:NSDeviceRGBColorSpace
bitmapFormat: 0
bytesPerRow:4 * POTWide
bitsPerPixel:32] autorelease];
NSGraphicsContext* g = [NSGraphicsContext graphicsContextWithBitmapImageRep:offscreenRep];
[NSGraphicsContext saveGraphicsState];
[NSGraphicsContext setCurrentContext:g];
[stringWithAttributes drawInRect:textRect];
NSBitmapImageRep *bitmap = [[NSBitmapImageRep alloc] initWithFocusedViewRect:NSMakeRect (0.0f, 0.0f, POTWide, POTHigh)];
[image unlockFocus];
auto data = (unsigned char*) [bitmap bitmapData]; //Use the same buffer to improve the performance.
[NSGraphicsContext restoreGraphicsState];
auto data = (unsigned char*) [offscreenRep bitmapData]; //Use the same buffer to improve the performance.
NSUInteger textureSize = POTWide * POTHigh * 4;
auto dataNew = (unsigned char*)malloc(sizeof(unsigned char) * textureSize);
if (dataNew) {
@ -333,8 +334,6 @@ static bool _initWithString(const char * text, Device::TextAlign align, const ch
info->isPremultipliedAlpha = true;
ret = true;
}
[bitmap release];
[image release];
} while (0);
return ret;
}