CCDevice-mac.mm 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398
  1. /****************************************************************************
  2. Copyright (c) 2010-2012 cocos2d-x.org
  3. Copyright (c) 2013-2016 Chukong Technologies Inc.
  4. Copyright (c) 2017-2018 Xiamen Yaji Software Co., Ltd.
  5. http://www.cocos2d-x.org
  6. Permission is hereby granted, free of charge, to any person obtaining a copy
  7. of this software and associated documentation files (the "Software"), to deal
  8. in the Software without restriction, including without limitation the rights
  9. to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  10. copies of the Software, and to permit persons to whom the Software is
  11. furnished to do so, subject to the following conditions:
  12. The above copyright notice and this permission notice shall be included in
  13. all copies or substantial portions of the Software.
  14. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  15. IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  16. FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  17. AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  18. LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  19. OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  20. THE SOFTWARE.
  21. ****************************************************************************/
  22. #include "platform/CCPlatformConfig.h"
  23. #if CC_TARGET_PLATFORM == CC_PLATFORM_MAC
  24. #include "platform/CCDevice.h"
  25. #include <Foundation/Foundation.h>
  26. #include <Cocoa/Cocoa.h>
  27. #include <string>
  28. #include "base/ccTypes.h"
  29. #include "platform/apple/CCDevice-apple.h"
  30. NS_CC_BEGIN
  31. static NSAttributedString* __attributedStringWithFontSize(NSMutableAttributedString* attributedString, CGFloat fontSize)
  32. {
  33. {
  34. [attributedString beginEditing];
  35. [attributedString enumerateAttribute:NSFontAttributeName inRange:NSMakeRange(0, attributedString.length) options:0 usingBlock:^(id value, NSRange range, BOOL *stop) {
  36. NSFont* font = value;
  37. font = [[NSFontManager sharedFontManager] convertFont:font toSize:fontSize];
  38. [attributedString removeAttribute:NSFontAttributeName range:range];
  39. [attributedString addAttribute:NSFontAttributeName value:font range:range];
  40. }];
  41. [attributedString endEditing];
  42. }
  43. return [[attributedString copy] autorelease];
  44. }
  45. int Device::getDPI()
  46. {
  47. NSScreen *screen = [NSScreen mainScreen];
  48. NSDictionary *description = [screen deviceDescription];
  49. NSSize displayPixelSize = [[description objectForKey:NSDeviceSize] sizeValue];
  50. CGSize displayPhysicalSize = CGDisplayScreenSize([[description objectForKey:@"NSScreenNumber"] unsignedIntValue]);
  51. return ((displayPixelSize.width / displayPhysicalSize.width) * 25.4f);
  52. }
  53. void Device::setAccelerometerEnabled(bool isEnabled)
  54. {
  55. }
  56. void Device::setAccelerometerInterval(float interval)
  57. {
  58. }
  59. typedef struct
  60. {
  61. int height;
  62. int width;
  63. bool hasAlpha;
  64. bool isPremultipliedAlpha;
  65. float strokeSize;
  66. float tintColorR;
  67. float tintColorG;
  68. float tintColorB;
  69. float tintColorA;
  70. unsigned char* data;
  71. } tImageInfo;
  72. static NSSize _calculateStringSize(NSAttributedString *str, id font, CGSize *constrainSize, bool enableWrap, int overflow)
  73. {
  74. NSSize textRect = NSZeroSize;
  75. textRect.width = constrainSize->width > 0 ? constrainSize->width
  76. : CGFLOAT_MAX;
  77. textRect.height = constrainSize->height > 0 ? constrainSize->height
  78. : CGFLOAT_MAX;
  79. if (overflow == 1) {
  80. if (!enableWrap) {
  81. textRect.width = CGFLOAT_MAX;
  82. textRect.height = CGFLOAT_MAX;
  83. } else {
  84. textRect.height = CGFLOAT_MAX;
  85. }
  86. }
  87. NSSize dim;
  88. #ifdef __MAC_10_11
  89. #if __MAC_OS_X_VERSION_MAX_ALLOWED >= __MAC_10_11
  90. dim = [str boundingRectWithSize:textRect options:(NSStringDrawingOptions)(NSStringDrawingUsesLineFragmentOrigin) context:nil].size;
  91. #else
  92. dim = [str boundingRectWithSize:textRect options:(NSStringDrawingOptions)(NSStringDrawingUsesLineFragmentOrigin)].size;
  93. #endif
  94. #else
  95. dim = [str boundingRectWithSize:textRect options:(NSStringDrawingOptions)(NSStringDrawingUsesLineFragmentOrigin)].size;
  96. #endif
  97. dim.width = ceilf(dim.width);
  98. dim.height = ceilf(dim.height);
  99. return dim;
  100. }
  101. static NSSize _calculateRealSizeForString(NSAttributedString **str, id font, NSSize constrainSize, bool enableWrap)
  102. {
  103. CGRect actualSize = CGRectMake(0, 0, constrainSize.width + 1, constrainSize.height + 1);
  104. int fontSize = [font pointSize];
  105. fontSize = fontSize + 1;
  106. if (!enableWrap) {
  107. while (actualSize.size.width > constrainSize.width ||
  108. actualSize.size.height > constrainSize.height) {
  109. fontSize = fontSize - 1;
  110. if (fontSize < 0) {
  111. actualSize = CGRectMake(0, 0, 0, 0);
  112. break;
  113. }
  114. NSMutableAttributedString *mutableString = [[*str mutableCopy] autorelease];
  115. *str = __attributedStringWithFontSize(mutableString, fontSize);
  116. #ifdef __MAC_10_11
  117. #if __MAC_OS_X_VERSION_MAX_ALLOWED >= __MAC_10_11
  118. CGSize fitSize = [*str boundingRectWithSize:CGSizeMake( CGFLOAT_MAX, CGFLOAT_MAX) options:NSStringDrawingUsesLineFragmentOrigin context:nil].size;
  119. #else
  120. CGSize fitSize = [*str boundingRectWithSize:CGSizeMake( CGFLOAT_MAX, CGFLOAT_MAX) options:NSStringDrawingUsesLineFragmentOrigin].size;
  121. #endif
  122. #else
  123. CGSize fitSize = [*str boundingRectWithSize:CGSizeMake( CGFLOAT_MAX, CGFLOAT_MAX) options:NSStringDrawingUsesLineFragmentOrigin].size;
  124. #endif
  125. if(fitSize.width == 0 || fitSize.height == 0) continue;
  126. actualSize.size = fitSize;
  127. if (constrainSize.width <= 0) {
  128. constrainSize.width = fitSize.width;
  129. }
  130. if (constrainSize.height <= 0){
  131. constrainSize.height = fitSize.height;
  132. }
  133. if(fontSize <= 0){
  134. break;
  135. }
  136. }
  137. }
  138. else {
  139. while (actualSize.size.height > constrainSize.height
  140. ||actualSize.size.width > constrainSize.width) {
  141. fontSize = fontSize - 1;
  142. if (fontSize < 0) {
  143. actualSize = CGRectMake(0, 0, 0, 0);
  144. break;
  145. }
  146. NSMutableAttributedString *mutableString = [[*str mutableCopy] autorelease];
  147. *str = __attributedStringWithFontSize(mutableString, fontSize);
  148. #ifdef __MAC_10_11
  149. #if __MAC_OS_X_VERSION_MAX_ALLOWED >= __MAC_10_11
  150. CGSize fitSize = [*str boundingRectWithSize:CGSizeMake( constrainSize.width, CGFLOAT_MAX) options:NSStringDrawingUsesLineFragmentOrigin context:nil].size;
  151. #else
  152. CGSize fitSize = [*str boundingRectWithSize:CGSizeMake( constrainSize.width, CGFLOAT_MAX) options:NSStringDrawingUsesLineFragmentOrigin].size;
  153. #endif
  154. #else
  155. CGSize fitSize = [*str boundingRectWithSize:CGSizeMake( constrainSize.width, CGFLOAT_MAX) options:NSStringDrawingUsesLineFragmentOrigin].size;
  156. #endif
  157. if(fitSize.width == 0 || fitSize.height == 0) continue;
  158. actualSize.size = fitSize;
  159. if (constrainSize.width <= 0) {
  160. constrainSize.width = fitSize.width;
  161. }
  162. if (constrainSize.height <= 0){
  163. constrainSize.height = fitSize.height;
  164. }
  165. if(fontSize <= 0){
  166. break;
  167. }
  168. }
  169. }
  170. return CGSizeMake(actualSize.size.width, actualSize.size.height);
  171. }
  172. static NSFont* _createSystemFont(const char* fontName, int size)
  173. {
  174. NSString * fntName = [NSString stringWithUTF8String:fontName];
  175. fntName = [[fntName lastPathComponent] stringByDeletingPathExtension];
  176. // font
  177. NSFont *font = [NSFont fontWithName:fntName size:size];
  178. if (font == nil) {
  179. font = [NSFont systemFontOfSize:size];
  180. }
  181. return font;
  182. }
  183. static CGFloat _calculateTextDrawStartHeight(cocos2d::Device::TextAlign align, CGSize realDimensions, CGSize dimensions)
  184. {
  185. float startH = 0;
  186. // vertical alignment
  187. unsigned int vAlignment = ((int)align >> 4) & 0x0F;
  188. switch (vAlignment) {
  189. //bottom
  190. case 1:startH = dimensions.height - realDimensions.height;break;
  191. //top
  192. case 2:startH = 0;break;
  193. //center
  194. case 3: startH = (dimensions.height - realDimensions.height) / 2;break;
  195. default:
  196. break;
  197. }
  198. return startH;
  199. }
  200. static bool _initWithString(const char * text, Device::TextAlign align, const char * fontName, int size, tImageInfo* info, const Color3B* fontColor, int fontAlpha, bool enableWrap, int overflow)
  201. {
  202. bool ret = false;
  203. CCASSERT(text, "Invalid text");
  204. CCASSERT(info, "Invalid info");
  205. do {
  206. NSString * string = [NSString stringWithUTF8String:text];
  207. CC_BREAK_IF(!string);
  208. id font = _createSystemFont(fontName, size);
  209. CC_BREAK_IF(!font);
  210. // color
  211. NSColor* foregroundColor;
  212. if (fontColor) {
  213. foregroundColor = [NSColor colorWithDeviceRed:fontColor->r/255.0
  214. green:fontColor->g/255.0
  215. blue:fontColor->b/255.0
  216. alpha:fontAlpha/255.0];
  217. } else {
  218. foregroundColor = [NSColor whiteColor];
  219. }
  220. // alignment
  221. NSTextAlignment textAlign = FontUtils::_calculateTextAlignment(align);
  222. NSMutableParagraphStyle *paragraphStyle = FontUtils::_calculateParagraphStyle(enableWrap, overflow);
  223. [paragraphStyle setAlignment:textAlign];
  224. // attribute
  225. NSDictionary* tokenAttributesDict = [NSDictionary dictionaryWithObjectsAndKeys:
  226. foregroundColor,NSForegroundColorAttributeName,
  227. font, NSFontAttributeName,
  228. paragraphStyle, NSParagraphStyleAttributeName, nil];
  229. NSAttributedString *stringWithAttributes =[[[NSAttributedString alloc] initWithString:string
  230. attributes:tokenAttributesDict] autorelease];
  231. CGSize dimensions = CGSizeMake(info->width, info->height);
  232. NSSize realDimensions;
  233. if (overflow == 2) {
  234. realDimensions = _calculateRealSizeForString(&stringWithAttributes, font, dimensions, enableWrap);
  235. } else {
  236. realDimensions = _calculateStringSize(stringWithAttributes, font, &dimensions, enableWrap, overflow);
  237. }
  238. // Mac crashes if the width or height is 0
  239. CC_BREAK_IF(realDimensions.width <= 0 || realDimensions.height <= 0);
  240. if(dimensions.width <= 0.f) {
  241. dimensions.width = realDimensions.width;
  242. }
  243. if (dimensions.height <= 0.f) {
  244. dimensions.height = realDimensions.height;
  245. }
  246. //Alignment
  247. CGFloat xPadding = FontUtils::_calculateTextDrawStartWidth(align, realDimensions, dimensions);
  248. CGFloat yPadding = _calculateTextDrawStartHeight(align, realDimensions, dimensions);
  249. NSInteger POTWide = dimensions.width;
  250. NSInteger POTHigh = dimensions.height;
  251. NSRect textRect = NSMakeRect(xPadding, POTHigh - dimensions.height + yPadding,
  252. realDimensions.width, realDimensions.height);
  253. NSBitmapImageRep* offscreenRep = [[[NSBitmapImageRep alloc]
  254. initWithBitmapDataPlanes:NULL
  255. pixelsWide:POTWide
  256. pixelsHigh:POTHigh
  257. bitsPerSample:8
  258. samplesPerPixel:4
  259. hasAlpha:YES
  260. isPlanar:NO
  261. colorSpaceName:NSDeviceRGBColorSpace
  262. bitmapFormat: 0
  263. bytesPerRow:4 * POTWide
  264. bitsPerPixel:32] autorelease];
  265. NSGraphicsContext* g = [NSGraphicsContext graphicsContextWithBitmapImageRep:offscreenRep];
  266. [NSGraphicsContext saveGraphicsState];
  267. [NSGraphicsContext setCurrentContext:g];
  268. [stringWithAttributes drawInRect:textRect];
  269. [NSGraphicsContext restoreGraphicsState];
  270. // [[NSGraphicsContext currentContext] setShouldAntialias:NO];
  271. //
  272. // NSImage *image = [[NSImage alloc] initWithSize:NSMakeSize(POTWide, POTHigh)];
  273. // [image lockFocus];
  274. // // patch for mac retina display and lableTTF
  275. // [[NSAffineTransform transform] set];
  276. // [stringWithAttributes drawInRect:textRect];
  277. // NSBitmapImageRep *bitmap = [[NSBitmapImageRep alloc] initWithFocusedViewRect:NSMakeRect (0.0f, 0.0f, POTWide, POTHigh)];
  278. // [image unlockFocus];
  279. auto data = (unsigned char*) [offscreenRep bitmapData]; //Use the same buffer to improve the performance.
  280. NSUInteger textureSize = POTWide * POTHigh * 4;
  281. auto dataNew = (unsigned char*)malloc(sizeof(unsigned char) * textureSize);
  282. if (dataNew) {
  283. memcpy(dataNew, data, textureSize);
  284. // output params
  285. info->width = static_cast<int>(POTWide);
  286. info->height = static_cast<int>(POTHigh);
  287. info->data = dataNew;
  288. info->hasAlpha = true;
  289. info->isPremultipliedAlpha = true;
  290. ret = true;
  291. }
  292. // [bitmap release];
  293. // [image release];
  294. } while (0);
  295. return ret;
  296. }
  297. Data Device::getTextureDataForText(const char * text, const FontDefinition& textDefinition, TextAlign align, int &width, int &height, bool& hasPremultipliedAlpha)
  298. {
  299. Data ret;
  300. do {
  301. tImageInfo info = {0};
  302. info.width = textDefinition._dimensions.width;
  303. info.height = textDefinition._dimensions.height;
  304. info.tintColorR = textDefinition._fontFillColor.r / 255.0f;
  305. info.tintColorG = textDefinition._fontFillColor.g / 255.0f;
  306. info.tintColorB = textDefinition._fontFillColor.b / 255.0f;
  307. info.tintColorA = textDefinition._fontAlpha / 255.0f;
  308. if (! _initWithString(text, align, textDefinition._fontName.c_str(), textDefinition._fontSize, &info, &textDefinition._fontFillColor, textDefinition._fontAlpha, textDefinition._enableWrap, textDefinition._overflow))
  309. {
  310. break;
  311. }
  312. height = (short)info.height;
  313. width = (short)info.width;
  314. ret.fastSet(info.data,width * height * 4);
  315. hasPremultipliedAlpha = true;
  316. } while (0);
  317. return ret;
  318. }
  319. void Device::setKeepScreenOn(bool value)
  320. {
  321. }
  322. void Device::vibrate(float duration)
  323. {
  324. }
  325. NS_CC_END
  326. #endif // CC_TARGET_PLATFORM == CC_PLATFORM_MAC