Skip to content

Commit efb2d7c

Browse files
authored
fix bug that system font is not rendered correctly on macOS Catalina (#20124)
1 parent 10a0bb1 commit efb2d7c

File tree

1 file changed

+23
-23
lines changed

1 file changed

+23
-23
lines changed

cocos/platform/mac/CCDevice-mac.mm

+23-23
Original file line numberDiff line numberDiff line change
@@ -279,47 +279,49 @@ static bool _initWithString(const char * text, Device::TextAlign align, const ch
279279

280280
NSSize realDimensions;
281281

282-
if (overflow == 2) {
282+
if (overflow == 2)
283283
realDimensions = _calculateRealSizeForString(&stringWithAttributes, font, dimensions, enableWrap);
284-
} else {
284+
else
285285
realDimensions = _calculateStringSize(stringWithAttributes, font, &dimensions, enableWrap, overflow);
286-
}
287-
288286

289287
// Mac crashes if the width or height is 0
290288
CC_BREAK_IF(realDimensions.width <= 0 || realDimensions.height <= 0);
291289

292290

293-
if(dimensions.width <= 0.f) {
291+
if(dimensions.width <= 0.f)
294292
dimensions.width = realDimensions.width;
295-
}
296-
if (dimensions.height <= 0.f) {
293+
if (dimensions.height <= 0.f)
297294
dimensions.height = realDimensions.height;
298-
}
299-
300-
295+
301296
//Alignment
302297
CGFloat xPadding = FontUtils::_calculateTextDrawStartWidth(align, realDimensions, dimensions);
303-
304298
CGFloat yPadding = _calculateTextDrawStartHeight(align, realDimensions, dimensions);
305299

306300
NSInteger POTWide = dimensions.width;
307301
NSInteger POTHigh = dimensions.height;
308302
NSRect textRect = NSMakeRect(xPadding, POTHigh - dimensions.height + yPadding,
309303
realDimensions.width, realDimensions.height);
310304

311-
312-
[[NSGraphicsContext currentContext] setShouldAntialias:NO];
313-
314-
NSImage *image = [[NSImage alloc] initWithSize:NSMakeSize(POTWide, POTHigh)];
315-
[image lockFocus];
316-
// patch for mac retina display and lableTTF
317-
[[NSAffineTransform transform] set];
305+
NSBitmapImageRep* offscreenRep = [[[NSBitmapImageRep alloc]
306+
initWithBitmapDataPlanes:NULL
307+
pixelsWide:POTWide
308+
pixelsHigh:POTHigh
309+
bitsPerSample:8
310+
samplesPerPixel:4
311+
hasAlpha:YES
312+
isPlanar:NO
313+
colorSpaceName:NSDeviceRGBColorSpace
314+
bitmapFormat: 0
315+
bytesPerRow:4 * POTWide
316+
bitsPerPixel:32] autorelease];
317+
318+
NSGraphicsContext* g = [NSGraphicsContext graphicsContextWithBitmapImageRep:offscreenRep];
319+
[NSGraphicsContext saveGraphicsState];
320+
[NSGraphicsContext setCurrentContext:g];
318321
[stringWithAttributes drawInRect:textRect];
319-
NSBitmapImageRep *bitmap = [[NSBitmapImageRep alloc] initWithFocusedViewRect:NSMakeRect (0.0f, 0.0f, POTWide, POTHigh)];
320-
[image unlockFocus];
322+
[NSGraphicsContext restoreGraphicsState];
321323

322-
auto data = (unsigned char*) [bitmap bitmapData]; //Use the same buffer to improve the performance.
324+
auto data = (unsigned char*) [offscreenRep bitmapData]; //Use the same buffer to improve the performance.
323325

324326
NSUInteger textureSize = POTWide * POTHigh * 4;
325327
auto dataNew = (unsigned char*)malloc(sizeof(unsigned char) * textureSize);
@@ -333,8 +335,6 @@ static bool _initWithString(const char * text, Device::TextAlign align, const ch
333335
info->isPremultipliedAlpha = true;
334336
ret = true;
335337
}
336-
[bitmap release];
337-
[image release];
338338
} while (0);
339339
return ret;
340340
}

0 commit comments

Comments
 (0)