@@ -279,47 +279,49 @@ static bool _initWithString(const char * text, Device::TextAlign align, const ch
279
279
280
280
NSSize realDimensions;
281
281
282
- if (overflow == 2 ) {
282
+ if (overflow == 2 )
283
283
realDimensions = _calculateRealSizeForString (&stringWithAttributes, font, dimensions, enableWrap);
284
- } else {
284
+ else
285
285
realDimensions = _calculateStringSize (stringWithAttributes, font, &dimensions, enableWrap, overflow);
286
- }
287
-
288
286
289
287
// Mac crashes if the width or height is 0
290
288
CC_BREAK_IF (realDimensions.width <= 0 || realDimensions.height <= 0 );
291
289
292
290
293
- if (dimensions.width <= 0 .f ) {
291
+ if (dimensions.width <= 0 .f )
294
292
dimensions.width = realDimensions.width ;
295
- }
296
- if (dimensions.height <= 0 .f ) {
293
+ if (dimensions.height <= 0 .f )
297
294
dimensions.height = realDimensions.height ;
298
- }
299
-
300
-
295
+
301
296
// Alignment
302
297
CGFloat xPadding = FontUtils::_calculateTextDrawStartWidth (align, realDimensions, dimensions);
303
-
304
298
CGFloat yPadding = _calculateTextDrawStartHeight (align, realDimensions, dimensions);
305
299
306
300
NSInteger POTWide = dimensions.width ;
307
301
NSInteger POTHigh = dimensions.height ;
308
302
NSRect textRect = NSMakeRect (xPadding, POTHigh - dimensions.height + yPadding,
309
303
realDimensions.width , realDimensions.height );
310
304
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];
318
321
[stringWithAttributes drawInRect: textRect];
319
- NSBitmapImageRep *bitmap = [[NSBitmapImageRep alloc ] initWithFocusedViewRect: NSMakeRect (0 .0f , 0 .0f , POTWide, POTHigh)];
320
- [image unlockFocus ];
322
+ [NSGraphicsContext restoreGraphicsState ];
321
323
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.
323
325
324
326
NSUInteger textureSize = POTWide * POTHigh * 4 ;
325
327
auto dataNew = (unsigned char *)malloc (sizeof (unsigned char ) * textureSize);
@@ -333,8 +335,6 @@ static bool _initWithString(const char * text, Device::TextAlign align, const ch
333
335
info->isPremultipliedAlpha = true ;
334
336
ret = true ;
335
337
}
336
- [bitmap release ];
337
- [image release ];
338
338
} while (0 );
339
339
return ret;
340
340
}
0 commit comments