Skip to content

fix bug that system font is not rendered correctly on macOS Catalina #20124

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Sep 17, 2019
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
46 changes: 23 additions & 23 deletions cocos/platform/mac/CCDevice-mac.mm
Original file line number Diff line number Diff line change
Expand Up @@ -279,47 +279,49 @@ 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 {
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) {
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];
[NSGraphicsContext restoreGraphicsState];

auto data = (unsigned char*) [bitmap bitmapData]; //Use the same buffer to improve the performance.
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);
Expand All @@ -333,8 +335,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;
}
Expand Down