CommonUnityLib.mm 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493
  1. //
  2. // CommonUnityLib.m
  3. // CommonUnityLib
  4. //
  5. // Created by zyf on 14-3-27.
  6. // Copyright (c) 2014年 zyf. All rights reserved.
  7. //
  8. //#import "CommonUnityLib.h"
  9. #import <Foundation/Foundation.h>
  10. #import <UIKit/UIKit.h>
  11. #import <QuartzCore/QuartzCore.h>
  12. #import <OpenGLES/ES2/gl.h>
  13. #include "CommonMPQ.h"
  14. #include "MFMD5.h"
  15. extern "C"
  16. {
  17. bool _SysFontTest(const char * pText,
  18. const char * fontName,
  19. int fontStyle,
  20. int fontSize,
  21. int bgCount,
  22. int expectSizeW,
  23. int expectSizeH,
  24. int& outW,
  25. int& outH);
  26. bool _SysFontTexture2(const char * pText,
  27. const char * fontName,
  28. int fontStyle,
  29. int fontSize,
  30. int fontColorRGBA,
  31. int bgCount,
  32. int bgColorRGBA,
  33. int expectSizeW,
  34. int expectSizeH,
  35. int glTextureID);
  36. bool _SysFontTexture2_Ptr(const char * pText,
  37. const char * fontName,
  38. int fontStyle,
  39. int fontSize,
  40. int fontColorRGBA,
  41. int bgCount,
  42. int bgColorRGBA,
  43. int expectSizeW,
  44. int expectSizeH,
  45. void* glTextureID_Ptr);
  46. bool _SysFontGetPixels(const char * pText,
  47. const char * fontName,
  48. int fontStyle,
  49. int fontSize,
  50. int fontColorRGBA,
  51. int bgCount,
  52. int bgColorRGBA,
  53. int pixelW,
  54. int pixelH,
  55. unsigned char * pixels);
  56. bool _SysFontGetPixels_Color32(const char * pText,
  57. const char * fontName,
  58. int fontStyle,
  59. int fontSize,
  60. int fontColorRGBA,
  61. int bgCount,
  62. int bgColorRGBA,
  63. int pixelW,
  64. int pixelH,
  65. void * pixels);
  66. }
  67. typedef enum FontStyle
  68. {
  69. STYLE_PLAIN = 0,
  70. STYLE_BOLD = 1,
  71. STYLE_ITALIC = 2,
  72. STYLE_UNDERLINED = 4,
  73. }
  74. kFontStyle;
  75. int _nextPoT(int size)
  76. {
  77. size = size - 1;
  78. size = size | (size >> 1);
  79. size = size | (size >> 2);
  80. size = size | (size >> 4);
  81. size = size | (size >> 8);
  82. size = size | (size >>16);
  83. return size + 1;
  84. }
  85. CGSize _calculateStringSizeWithFontOrZFont(NSString *str, id font)
  86. {
  87. CGSize dim = CGSizeZero;
  88. dim = [str sizeWithFont:font
  89. constrainedToSize:CGSizeMake(65535, 65535)
  90. lineBreakMode:NSLineBreakByWordWrapping];
  91. dim.width = ceilf(dim.width);
  92. dim.height = ceilf(dim.height);
  93. return dim;
  94. }
  95. id _SysGetFont(const char * fontName, int fontSize, int fontStyle)
  96. {
  97. NSString * fntName = [NSString stringWithUTF8String:fontName];
  98. // create the font
  99. id font = [UIFont fontWithName:fntName size:fontSize];
  100. if (!font)
  101. {
  102. font = [UIFont fontWithName:@"Helvetica-Bold" size:fontSize];
  103. if (! font)
  104. {
  105. font = [UIFont systemFontOfSize:fontSize];
  106. }
  107. }
  108. return font;
  109. }
  110. bool _SysFontTest(const char * pText,
  111. const char * fontName,
  112. int fontStyle,
  113. int fontSize,
  114. int bgCount,
  115. int expectSizeW,
  116. int expectSizeH,
  117. int& outW,
  118. int& outH)
  119. {
  120. outW = 0;
  121. outH = 0;
  122. NSString * str = [NSString stringWithUTF8String:pText];
  123. CGSize dim;
  124. // create the font
  125. id font = _SysGetFont(fontName, fontSize, fontStyle);
  126. if (font)
  127. {
  128. dim = _calculateStringSizeWithFontOrZFont(str, font);
  129. }
  130. else
  131. {
  132. return false;
  133. }
  134. // adjust text rect
  135. if (expectSizeW > 0 && expectSizeW > dim.width)
  136. {
  137. dim.width = expectSizeW;
  138. }
  139. if (expectSizeH > 0 && expectSizeH > dim.height)
  140. {
  141. dim.height = expectSizeH;
  142. }
  143. dim.width += 3;
  144. dim.height += 3;
  145. outW = (int)ceilf(dim.width);
  146. outH = (int)ceilf(dim.height);
  147. return true;
  148. }
  149. #define BORDER_Null 0
  150. #define BORDER_Shadow 1
  151. #define BORDER_Border_4 4
  152. #define BORDER_Border 8
  153. #define BORDER_Shadow_L_T 10
  154. #define BORDER_Shadow_C_T 11
  155. #define BORDER_Shadow_R_T 12
  156. #define BORDER_Shadow_L_C 13
  157. #define BORDER_Shadow_C_C 14
  158. #define BORDER_Shadow_R_C 15
  159. #define BORDER_Shadow_L_B 16
  160. #define BORDER_Shadow_C_B 17
  161. #define BORDER_Shadow_R_B 18
  162. void _SysDrawInContext(CGContextRef context,
  163. id font,
  164. NSString* str,
  165. CGSize dim,
  166. int fontStyle,
  167. int fontColorRGBA,
  168. int bgCount,
  169. int bgColorRGBA)
  170. {
  171. //CGContextSetRGBFillColor(context, 0.5f, 0.5f, 0.5f, 1.0f);
  172. //CGContextFillRect(context, CGRectMake(0, 0, dim.width, dim.height));
  173. CGContextSetShouldSmoothFonts(context, true);
  174. float fr=(float)((fontColorRGBA>>24)&0xff)/255.0f;
  175. float fg=(float)((fontColorRGBA>>16)&0xff)/255.0f;
  176. float fb=(float)((fontColorRGBA>>8 )&0xff)/255.0f;
  177. float fa=(float)((fontColorRGBA>>0 )&0xff)/255.0f;
  178. if (STYLE_UNDERLINED == fontStyle)
  179. {
  180. //draw underline
  181. CGContextSetRGBStrokeColor(context, fr, fg, fb, fa); // set as the text's color
  182. CGContextSetLineWidth(context, 2.0f);
  183. CGPoint leftPoint = CGPointMake(1,dim.height - 2);
  184. CGPoint rightPoint = CGPointMake(dim.width - 1,dim.height - 2);
  185. CGContextMoveToPoint(context, leftPoint.x, leftPoint.y);
  186. CGContextAddLineToPoint(context, rightPoint.x, rightPoint.y);
  187. CGContextStrokePath(context);
  188. }
  189. // measure text size with specified font and determine the rectangle to draw text in
  190. NSTextAlignment align = NSTextAlignmentLeft;
  191. if (bgCount != 0)
  192. {
  193. const char offset_8[8][2]={
  194. { 0, 0},{ 1, 0},{ 2, 0},
  195. { 0, 1},/*1, 1*/{ 2, 1},
  196. { 0, 2},{ 1, 2},{ 2, 2}};
  197. const char offset_4[8][2]={
  198. /*0, 0*/{ 1, 0},/*2, 0*/
  199. { 0, 1},/*1, 1*/{ 2, 1},
  200. /*0, 2*/{ 1, 2},/*2, 2*/};
  201. float br=(float)((bgColorRGBA>>24)&0xff)/255.0f;
  202. float bg=(float)((bgColorRGBA>>16)&0xff)/255.0f;
  203. float bb=(float)((bgColorRGBA>>8 )&0xff)/255.0f;
  204. float ba=(float)((bgColorRGBA>>0 )&0xff)/255.0f;
  205. CGContextSetRGBFillColor(context, br, bg, bb, ba);
  206. switch(bgCount)
  207. {
  208. case BORDER_Border:
  209. for (int i=0; i < 8; i++)
  210. {
  211. [str drawInRect:CGRectMake(offset_8[i][0], offset_8[i][1], dim.width, dim.height)
  212. withFont:font
  213. lineBreakMode:NSLineBreakByWordWrapping
  214. alignment:align];
  215. }
  216. break;
  217. case BORDER_Border_4:
  218. for (int i=0; i < 4; i++)
  219. {
  220. [str drawInRect:CGRectMake(offset_4[i][0], offset_4[i][1], dim.width, dim.height)
  221. withFont:font
  222. lineBreakMode:NSLineBreakByWordWrapping
  223. alignment:align];
  224. }
  225. break;
  226. case BORDER_Shadow:
  227. [str drawInRect:CGRectMake(1, 2, dim.width, dim.height)
  228. withFont:font
  229. lineBreakMode:NSLineBreakByWordWrapping
  230. alignment:align];
  231. break;
  232. case BORDER_Shadow_L_T:
  233. [str drawInRect:CGRectMake(0, 0, dim.width, dim.height)
  234. withFont:font
  235. lineBreakMode:NSLineBreakByWordWrapping
  236. alignment:align];
  237. break;
  238. case BORDER_Shadow_C_T:
  239. [str drawInRect:CGRectMake(1, 0, dim.width, dim.height)
  240. withFont:font
  241. lineBreakMode:NSLineBreakByWordWrapping
  242. alignment:align];
  243. break;
  244. case BORDER_Shadow_R_T:
  245. [str drawInRect:CGRectMake(2, 0, dim.width, dim.height)
  246. withFont:font
  247. lineBreakMode:NSLineBreakByWordWrapping
  248. alignment:align];
  249. break;
  250. case BORDER_Shadow_L_C:
  251. [str drawInRect:CGRectMake(0, 1, dim.width, dim.height)
  252. withFont:font
  253. lineBreakMode:NSLineBreakByWordWrapping
  254. alignment:align];
  255. break;
  256. case BORDER_Shadow_R_C:
  257. [str drawInRect:CGRectMake(2, 1, dim.width, dim.height)
  258. withFont:font
  259. lineBreakMode:NSLineBreakByWordWrapping
  260. alignment:align];
  261. break;
  262. case BORDER_Shadow_L_B:
  263. [str drawInRect:CGRectMake(0, 2, dim.width, dim.height)
  264. withFont:font
  265. lineBreakMode:NSLineBreakByWordWrapping
  266. alignment:align];
  267. break;
  268. case BORDER_Shadow_C_B:
  269. [str drawInRect:CGRectMake(1, 2, dim.width, dim.height)
  270. withFont:font
  271. lineBreakMode:NSLineBreakByWordWrapping
  272. alignment:align];
  273. break;
  274. case BORDER_Shadow_R_B:
  275. [str drawInRect:CGRectMake(2, 2, dim.width, dim.height)
  276. withFont:font
  277. lineBreakMode:NSLineBreakByWordWrapping
  278. alignment:align];
  279. break;
  280. }
  281. }
  282. CGContextSetRGBFillColor(context, fr, fg, fb, fa);
  283. [str drawInRect:CGRectMake(1, 1, dim.width, dim.height)
  284. withFont:font
  285. lineBreakMode:NSLineBreakByWordWrapping
  286. alignment:align];
  287. [str drawInRect:CGRectMake(1, 1, dim.width, dim.height)
  288. withFont:font
  289. lineBreakMode:NSLineBreakByWordWrapping
  290. alignment:align];
  291. }
  292. bool _SysFontTexture2 (const char * pText,
  293. const char * fontName,
  294. int fontStyle,
  295. int fontSize,
  296. int fontColorRGBA,
  297. int bgCount,
  298. int bgColorRGBA,
  299. int expectSizeW,
  300. int expectSizeH,
  301. int glTextureID)
  302. {
  303. bool bRet = false;
  304. do
  305. {
  306. NSString * str = [NSString stringWithUTF8String:pText];
  307. CGSize dim = CGSizeMake(expectSizeW, expectSizeH);
  308. id font = _SysGetFont(fontName, fontSize, fontStyle);
  309. if (!font)
  310. {
  311. break;
  312. }
  313. int _potw = (int)ceilf(dim.width); //_nextPoT(dim.width);//
  314. int _poth = (int)ceilf(dim.height); //_nextPoT(dim.height);//
  315. unsigned char* data = new unsigned char[(_potw * _poth * 4)];
  316. memset(data, 0, ( _potw * _poth * 4));
  317. // draw text
  318. CGColorSpaceRef colorSpace = CGColorSpaceCreateDeviceRGB();
  319. //浮点数*4再强转成int 不一定为4的倍数,此时调试ios7会产生崩溃//
  320. CGContextRef context = CGBitmapContextCreate(data,
  321. _potw, _poth,
  322. 8, (_potw * 4),
  323. colorSpace,
  324. kCGImageAlphaPremultipliedLast | kCGBitmapByteOrder32Big);
  325. CGColorSpaceRelease(colorSpace);
  326. if (!context)
  327. {
  328. delete[] data;
  329. break;
  330. }
  331. UIGraphicsPushContext(context);
  332. {
  333. //CGContextTranslateCTM(context, 0.0f, startH + 1.5f);
  334. //CGContextScaleCTM(context, 1.0f, 1.0f);
  335. _SysDrawInContext(context, font, str, dim, fontStyle,fontColorRGBA, bgCount, bgColorRGBA);
  336. }
  337. UIGraphicsPopContext();
  338. CGContextRelease(context);
  339. {
  340. GLenum err = GL_NO_ERROR;
  341. //Update GL Texture
  342. glBindTexture(GL_TEXTURE_2D, glTextureID);
  343. glPixelStorei(GL_UNPACK_ALIGNMENT, 1);
  344. glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
  345. glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
  346. glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
  347. glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
  348. glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, _potw, _poth, 0, GL_RGBA, GL_UNSIGNED_BYTE, data);
  349. while((err = glGetError()) != GL_NO_ERROR)
  350. {
  351. printf("GLError: 0x%x\n", err);
  352. }
  353. }
  354. delete[] data;
  355. bRet = true;
  356. }
  357. while (0);
  358. return bRet;
  359. }
  360. bool _SysFontTexture2_Ptr (const char * pText,
  361. const char * fontName,
  362. int fontStyle,
  363. int fontSize,
  364. int fontColorRGBA,
  365. int bgCount,
  366. int bgColorRGBA,
  367. int expectSizeW,
  368. int expectSizeH,
  369. void* glTextureID_Ptr)
  370. {
  371. int glTextureID = (int)((long)glTextureID_Ptr);
  372. return _SysFontTexture2(pText,fontName,fontStyle,fontSize,fontColorRGBA,bgCount,bgColorRGBA,expectSizeW,expectSizeH,glTextureID);
  373. }
  374. bool _SysFontGetPixels(const char * pText,
  375. const char * fontName,
  376. int fontStyle,
  377. int fontSize,
  378. int fontColorRGBA,
  379. int bgCount,
  380. int bgColorRGBA,
  381. int pixelW,
  382. int pixelH,
  383. unsigned char * pixels)
  384. {
  385. bool bRet = false;
  386. do
  387. {
  388. NSString * str = [NSString stringWithUTF8String:pText];
  389. CGSize dim = CGSizeMake(pixelW, pixelH);
  390. id font = _SysGetFont(fontName, fontSize, fontStyle);
  391. if (!font)
  392. {
  393. break;
  394. }
  395. int _potw = pixelW;
  396. int _poth = pixelH;
  397. memset(pixels, 0, ( _potw * _poth * 4));
  398. // draw text
  399. CGColorSpaceRef colorSpace = CGColorSpaceCreateDeviceRGB();
  400. //浮点数*4再强转成int 不一定为4的倍数,此时调试ios7会产生崩溃//
  401. CGContextRef context = CGBitmapContextCreate(pixels,
  402. _potw, _poth,
  403. 8, (_potw * 4),
  404. colorSpace,
  405. kCGImageAlphaPremultipliedLast | kCGBitmapByteOrder32Big);
  406. CGColorSpaceRelease(colorSpace);
  407. if (! context)
  408. {
  409. break;
  410. }
  411. UIGraphicsPushContext(context);
  412. {
  413. _SysDrawInContext(context, font, str, dim, fontStyle,fontColorRGBA, bgCount, bgColorRGBA);
  414. }
  415. UIGraphicsPopContext();
  416. CGContextRelease(context);
  417. bRet = true;
  418. }
  419. while (0);
  420. return bRet;
  421. }
  422. bool _SysFontGetPixels_Color32(const char * pText,
  423. const char * fontName,
  424. int fontStyle,
  425. int fontSize,
  426. int fontColorRGBA,
  427. int bgCount,
  428. int bgColorRGBA,
  429. int pixelW,
  430. int pixelH,
  431. void * pixels32)
  432. {
  433. unsigned char * pixels = (unsigned char *)pixels32;
  434. return _SysFontGetPixels(pText,fontName,fontStyle,fontSize,fontColorRGBA,bgCount,bgColorRGBA,pixelW,pixelH,pixels);
  435. }