CCFontFreeType.cpp 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668
  1. /****************************************************************************
  2. Copyright (c) 2013 Zynga Inc.
  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 "2d/CCFontFreeType.h"
  23. #include FT_BBOX_H
  24. #include "edtaa3func.h"
  25. #include "2d/CCFontAtlas.h"
  26. #include "base/CCDirector.h"
  27. #include "base/ccUTF8.h"
  28. #include "platform/CCFileUtils.h"
  29. NS_CC_BEGIN
  30. FT_Library FontFreeType::_FTlibrary;
  31. bool FontFreeType::_FTInitialized = false;
  32. const int FontFreeType::DistanceMapSpread = 3;
  33. const char* FontFreeType::_glyphASCII = "\"!#$%&'()*+,-./0123456789:;<=>?@ABCDEFGHIJKLMNOPQRSTUVWXYZ[\\]^_`abcdefghijklmnopqrstuvwxyz{|}~¡¢£¤¥¦§¨©ª«¬­®¯°±²³´µ¶·¸¹º»¼½¾¿ÀÁÂÃÄÅÆÇÈÉÊËÌÍÎÏÐÑÒÓÔÕÖ×ØÙÚÛÜÝÞßàáâãäåæçèéêëìíîïðñòóôõö÷øùúûüýþ ";
  34. const char* FontFreeType::_glyphNEHE = "!\"#$%&'()*+,-./0123456789:;<=>?@ABCDEFGHIJKLMNOPQRSTUVWXYZ[\\]^_`abcdefghijklmnopqrstuvwxyz{|}~ ";
  35. typedef struct _DataRef
  36. {
  37. Data data;
  38. unsigned int referenceCount;
  39. }DataRef;
  40. static std::unordered_map<std::string, DataRef> s_cacheFontData;
  41. FontFreeType * FontFreeType::create(const std::string &fontName, float fontSize, GlyphCollection glyphs, const char *customGlyphs,bool distanceFieldEnabled /* = false */,float outline /* = 0 */)
  42. {
  43. FontFreeType *tempFont = new (std::nothrow) FontFreeType(distanceFieldEnabled,outline);
  44. if (!tempFont)
  45. return nullptr;
  46. tempFont->setGlyphCollection(glyphs, customGlyphs);
  47. if (!tempFont->createFontObject(fontName, fontSize))
  48. {
  49. delete tempFont;
  50. return nullptr;
  51. }
  52. tempFont->autorelease();
  53. return tempFont;
  54. }
  55. bool FontFreeType::initFreeType()
  56. {
  57. if (_FTInitialized == false)
  58. {
  59. // begin freetype
  60. if (FT_Init_FreeType( &_FTlibrary ))
  61. return false;
  62. _FTInitialized = true;
  63. }
  64. return _FTInitialized;
  65. }
  66. void FontFreeType::shutdownFreeType()
  67. {
  68. if (_FTInitialized == true)
  69. {
  70. FT_Done_FreeType(_FTlibrary);
  71. s_cacheFontData.clear();
  72. _FTInitialized = false;
  73. }
  74. }
  75. FT_Library FontFreeType::getFTLibrary()
  76. {
  77. initFreeType();
  78. return _FTlibrary;
  79. }
  80. FontFreeType::FontFreeType(bool distanceFieldEnabled /* = false */, float outline /* = 0 */)
  81. : _fontRef(nullptr)
  82. , _stroker(nullptr)
  83. , _distanceFieldEnabled(distanceFieldEnabled)
  84. , _outlineSize(0.0f)
  85. , _lineHeight(0)
  86. , _fontAtlas(nullptr)
  87. , _encoding(FT_ENCODING_UNICODE)
  88. , _usedGlyphs(GlyphCollection::ASCII)
  89. {
  90. if (outline > 0.0f)
  91. {
  92. _outlineSize = outline * CC_CONTENT_SCALE_FACTOR();
  93. FT_Stroker_New(FontFreeType::getFTLibrary(), &_stroker);
  94. FT_Stroker_Set(_stroker,
  95. (int)(_outlineSize * 64),
  96. FT_STROKER_LINECAP_ROUND,
  97. FT_STROKER_LINEJOIN_ROUND,
  98. 0);
  99. }
  100. }
  101. bool FontFreeType::createFontObject(const std::string &fontName, float fontSize)
  102. {
  103. FT_Face face;
  104. // save font name locally
  105. _fontName = fontName;
  106. auto it = s_cacheFontData.find(fontName);
  107. if (it != s_cacheFontData.end())
  108. {
  109. (*it).second.referenceCount += 1;
  110. }
  111. else
  112. {
  113. s_cacheFontData[fontName].referenceCount = 1;
  114. s_cacheFontData[fontName].data = FileUtils::getInstance()->getDataFromFile(fontName);
  115. if (s_cacheFontData[fontName].data.isNull())
  116. {
  117. return false;
  118. }
  119. }
  120. if (FT_New_Memory_Face(getFTLibrary(), s_cacheFontData[fontName].data.getBytes(), s_cacheFontData[fontName].data.getSize(), 0, &face ))
  121. return false;
  122. if (FT_Select_Charmap(face, FT_ENCODING_UNICODE))
  123. {
  124. int foundIndex = -1;
  125. for (int charmapIndex = 0; charmapIndex < face->num_charmaps; charmapIndex++)
  126. {
  127. if (face->charmaps[charmapIndex]->encoding != FT_ENCODING_NONE)
  128. {
  129. foundIndex = charmapIndex;
  130. break;
  131. }
  132. }
  133. if (foundIndex == -1)
  134. {
  135. return false;
  136. }
  137. _encoding = face->charmaps[foundIndex]->encoding;
  138. if (FT_Select_Charmap(face, _encoding))
  139. {
  140. return false;
  141. }
  142. }
  143. // set the requested font size
  144. int dpi = 72;
  145. int fontSizePoints = (int)(64.f * fontSize * CC_CONTENT_SCALE_FACTOR());
  146. if (FT_Set_Char_Size(face, fontSizePoints, fontSizePoints, dpi, dpi))
  147. return false;
  148. // store the face globally
  149. _fontRef = face;
  150. _lineHeight = static_cast<int>((_fontRef->size->metrics.ascender - _fontRef->size->metrics.descender) >> 6);
  151. // done and good
  152. return true;
  153. }
  154. FontFreeType::~FontFreeType()
  155. {
  156. if (_FTInitialized)
  157. {
  158. if (_stroker)
  159. {
  160. FT_Stroker_Done(_stroker);
  161. }
  162. if (_fontRef)
  163. {
  164. FT_Done_Face(_fontRef);
  165. }
  166. }
  167. auto iter = s_cacheFontData.find(_fontName);
  168. if (iter != s_cacheFontData.end())
  169. {
  170. iter->second.referenceCount -= 1;
  171. if (iter->second.referenceCount == 0)
  172. {
  173. s_cacheFontData.erase(iter);
  174. }
  175. }
  176. }
  177. FontAtlas * FontFreeType::createFontAtlas()
  178. {
  179. if (_fontAtlas == nullptr)
  180. {
  181. _fontAtlas = new (std::nothrow) FontAtlas(*this);
  182. if (_fontAtlas && _usedGlyphs != GlyphCollection::DYNAMIC)
  183. {
  184. std::u32string utf32;
  185. if (StringUtils::UTF8ToUTF32(getGlyphCollection(), utf32))
  186. {
  187. _fontAtlas->prepareLetterDefinitions(utf32);
  188. }
  189. }
  190. // this->autorelease();
  191. }
  192. return _fontAtlas;
  193. }
  194. int * FontFreeType::getHorizontalKerningForTextUTF32(const std::u32string& text, int &outNumLetters) const
  195. {
  196. if (!_fontRef)
  197. return nullptr;
  198. outNumLetters = static_cast<int>(text.length());
  199. if (!outNumLetters)
  200. return nullptr;
  201. int *sizes = new (std::nothrow) int[outNumLetters];
  202. if (!sizes)
  203. return nullptr;
  204. memset(sizes,0,outNumLetters * sizeof(int));
  205. bool hasKerning = FT_HAS_KERNING( _fontRef ) != 0;
  206. if (hasKerning)
  207. {
  208. for (int c = 1; c < outNumLetters; ++c)
  209. {
  210. sizes[c] = getHorizontalKerningForChars(text[c-1], text[c]);
  211. }
  212. }
  213. return sizes;
  214. }
  215. int FontFreeType::getHorizontalKerningForChars(uint64_t firstChar, uint64_t secondChar) const
  216. {
  217. // get the ID to the char we need
  218. int glyphIndex1 = FT_Get_Char_Index(_fontRef, firstChar);
  219. if (!glyphIndex1)
  220. return 0;
  221. // get the ID to the char we need
  222. int glyphIndex2 = FT_Get_Char_Index(_fontRef, secondChar);
  223. if (!glyphIndex2)
  224. return 0;
  225. FT_Vector kerning;
  226. if (FT_Get_Kerning( _fontRef, glyphIndex1, glyphIndex2, FT_KERNING_DEFAULT, &kerning))
  227. return 0;
  228. return (static_cast<int>(kerning.x >> 6));
  229. }
  230. int FontFreeType::getFontAscender() const
  231. {
  232. return (static_cast<int>(_fontRef->size->metrics.ascender >> 6));
  233. }
  234. const char* FontFreeType::getFontFamily() const
  235. {
  236. if (!_fontRef)
  237. return nullptr;
  238. return _fontRef->family_name;
  239. }
  240. unsigned char* FontFreeType::getGlyphBitmap(uint64_t theChar, long &outWidth, long &outHeight, Rect &outRect,int &xAdvance)
  241. {
  242. bool invalidChar = true;
  243. unsigned char* ret = nullptr;
  244. do
  245. {
  246. if (_fontRef == nullptr)
  247. break;
  248. if (_distanceFieldEnabled)
  249. {
  250. if (FT_Load_Char(_fontRef, theChar, FT_LOAD_RENDER | FT_LOAD_NO_HINTING | FT_LOAD_NO_AUTOHINT))
  251. break;
  252. }
  253. else
  254. {
  255. if (FT_Load_Char(_fontRef, theChar, FT_LOAD_RENDER | FT_LOAD_NO_AUTOHINT))
  256. break;
  257. }
  258. auto& metrics = _fontRef->glyph->metrics;
  259. outRect.origin.x = metrics.horiBearingX >> 6;
  260. outRect.origin.y = -(metrics.horiBearingY >> 6);
  261. outRect.size.width = (metrics.width >> 6);
  262. outRect.size.height = (metrics.height >> 6);
  263. xAdvance = (static_cast<int>(_fontRef->glyph->metrics.horiAdvance >> 6));
  264. outWidth = _fontRef->glyph->bitmap.width;
  265. outHeight = _fontRef->glyph->bitmap.rows;
  266. ret = _fontRef->glyph->bitmap.buffer;
  267. if (_outlineSize > 0 && outWidth > 0 && outHeight > 0)
  268. {
  269. auto copyBitmap = new (std::nothrow) unsigned char[outWidth * outHeight];
  270. memcpy(copyBitmap,ret,outWidth * outHeight * sizeof(unsigned char));
  271. FT_BBox bbox;
  272. auto outlineBitmap = getGlyphBitmapWithOutline(theChar,bbox);
  273. if(outlineBitmap == nullptr)
  274. {
  275. ret = nullptr;
  276. delete [] copyBitmap;
  277. break;
  278. }
  279. long glyphMinX = outRect.origin.x;
  280. long glyphMaxX = outRect.origin.x + outWidth;
  281. long glyphMinY = -outHeight - outRect.origin.y;
  282. long glyphMaxY = -outRect.origin.y;
  283. auto outlineMinX = bbox.xMin >> 6;
  284. auto outlineMaxX = bbox.xMax >> 6;
  285. auto outlineMinY = bbox.yMin >> 6;
  286. auto outlineMaxY = bbox.yMax >> 6;
  287. auto outlineWidth = outlineMaxX - outlineMinX;
  288. auto outlineHeight = outlineMaxY - outlineMinY;
  289. auto blendImageMinX = MIN(outlineMinX, glyphMinX);
  290. auto blendImageMaxY = MAX(outlineMaxY, glyphMaxY);
  291. auto blendWidth = MAX(outlineMaxX, glyphMaxX) - blendImageMinX;
  292. auto blendHeight = blendImageMaxY - MIN(outlineMinY, glyphMinY);
  293. outRect.origin.x = blendImageMinX;
  294. outRect.origin.y = -blendImageMaxY + _outlineSize;
  295. unsigned char *blendImage = nullptr;
  296. if (blendWidth > 0 && blendHeight > 0)
  297. {
  298. long index, index2;
  299. blendImage = new (std::nothrow) unsigned char[blendWidth * blendHeight * 2];
  300. memset(blendImage, 0, blendWidth * blendHeight * 2);
  301. auto px = outlineMinX - blendImageMinX;
  302. auto py = blendImageMaxY - outlineMaxY;
  303. for (int x = 0; x < outlineWidth; ++x)
  304. {
  305. for (int y = 0; y < outlineHeight; ++y)
  306. {
  307. index = px + x + ((py + y) * blendWidth);
  308. index2 = x + (y * outlineWidth);
  309. blendImage[2 * index] = outlineBitmap[index2];
  310. }
  311. }
  312. px = glyphMinX - blendImageMinX;
  313. py = blendImageMaxY - glyphMaxY;
  314. for (int x = 0; x < outWidth; ++x)
  315. {
  316. for (int y = 0; y < outHeight; ++y)
  317. {
  318. index = px + x + ((y + py) * blendWidth);
  319. index2 = x + (y * outWidth);
  320. blendImage[2 * index + 1] = copyBitmap[index2];
  321. }
  322. }
  323. }
  324. outRect.size.width = blendWidth;
  325. outRect.size.height = blendHeight;
  326. outWidth = blendWidth;
  327. outHeight = blendHeight;
  328. delete [] outlineBitmap;
  329. delete [] copyBitmap;
  330. ret = blendImage;
  331. }
  332. invalidChar = false;
  333. } while (0);
  334. if (invalidChar)
  335. {
  336. outRect.size.width = 0;
  337. outRect.size.height = 0;
  338. xAdvance = 0;
  339. return nullptr;
  340. }
  341. else
  342. {
  343. return ret;
  344. }
  345. }
  346. unsigned char * FontFreeType::getGlyphBitmapWithOutline(uint64_t theChar, FT_BBox &bbox)
  347. {
  348. unsigned char* ret = nullptr;
  349. if (FT_Load_Char(_fontRef, theChar, FT_LOAD_NO_BITMAP) == 0)
  350. {
  351. if (_fontRef->glyph->format == FT_GLYPH_FORMAT_OUTLINE)
  352. {
  353. FT_Glyph glyph;
  354. if (FT_Get_Glyph(_fontRef->glyph, &glyph) == 0)
  355. {
  356. FT_Glyph_StrokeBorder(&glyph, _stroker, 0, 1);
  357. if (glyph->format == FT_GLYPH_FORMAT_OUTLINE)
  358. {
  359. FT_Outline *outline = &reinterpret_cast<FT_OutlineGlyph>(glyph)->outline;
  360. FT_Glyph_Get_CBox(glyph,FT_GLYPH_BBOX_GRIDFIT,&bbox);
  361. long width = (bbox.xMax - bbox.xMin)>>6;
  362. long rows = (bbox.yMax - bbox.yMin)>>6;
  363. FT_Bitmap bmp;
  364. bmp.buffer = new (std::nothrow) unsigned char[width * rows];
  365. memset(bmp.buffer, 0, width * rows);
  366. bmp.width = (int)width;
  367. bmp.rows = (int)rows;
  368. bmp.pitch = (int)width;
  369. bmp.pixel_mode = FT_PIXEL_MODE_GRAY;
  370. bmp.num_grays = 256;
  371. FT_Raster_Params params;
  372. memset(&params, 0, sizeof (params));
  373. params.source = outline;
  374. params.target = &bmp;
  375. params.flags = FT_RASTER_FLAG_AA;
  376. FT_Outline_Translate(outline,-bbox.xMin,-bbox.yMin);
  377. FT_Outline_Render(_FTlibrary, outline, &params);
  378. ret = bmp.buffer;
  379. }
  380. FT_Done_Glyph(glyph);
  381. }
  382. }
  383. }
  384. return ret;
  385. }
  386. unsigned char * makeDistanceMap( unsigned char *img, long width, long height)
  387. {
  388. long pixelAmount = (width + 2 * FontFreeType::DistanceMapSpread) * (height + 2 * FontFreeType::DistanceMapSpread);
  389. short * xdist = (short *) malloc( pixelAmount * sizeof(short) );
  390. short * ydist = (short *) malloc( pixelAmount * sizeof(short) );
  391. double * gx = (double *) calloc( pixelAmount, sizeof(double) );
  392. double * gy = (double *) calloc( pixelAmount, sizeof(double) );
  393. double * data = (double *) calloc( pixelAmount, sizeof(double) );
  394. double * outside = (double *) calloc( pixelAmount, sizeof(double) );
  395. double * inside = (double *) calloc( pixelAmount, sizeof(double) );
  396. long i,j;
  397. // Convert img into double (data) rescale image levels between 0 and 1
  398. long outWidth = width + 2 * FontFreeType::DistanceMapSpread;
  399. for (i = 0; i < width; ++i)
  400. {
  401. for (j = 0; j < height; ++j)
  402. {
  403. data[j * outWidth + FontFreeType::DistanceMapSpread + i] = img[j * width + i] / 255.0;
  404. }
  405. }
  406. width += 2 * FontFreeType::DistanceMapSpread;
  407. height += 2 * FontFreeType::DistanceMapSpread;
  408. // Transform background (outside contour, in areas of 0's)
  409. computegradient( data, (int)width, (int)height, gx, gy);
  410. edtaa3(data, gx, gy, (int)width, (int)height, xdist, ydist, outside);
  411. for( i=0; i< pixelAmount; i++)
  412. if( outside[i] < 0.0 )
  413. outside[i] = 0.0;
  414. // Transform foreground (inside contour, in areas of 1's)
  415. for( i=0; i< pixelAmount; i++)
  416. data[i] = 1 - data[i];
  417. computegradient( data, (int)width, (int)height, gx, gy);
  418. edtaa3(data, gx, gy, (int)width, (int)height, xdist, ydist, inside);
  419. for( i=0; i< pixelAmount; i++)
  420. if( inside[i] < 0.0 )
  421. inside[i] = 0.0;
  422. // The bipolar distance field is now outside-inside
  423. double dist;
  424. /* Single channel 8-bit output (bad precision and range, but simple) */
  425. unsigned char *out = (unsigned char *) malloc( pixelAmount * sizeof(unsigned char) );
  426. for( i=0; i < pixelAmount; i++)
  427. {
  428. dist = outside[i] - inside[i];
  429. dist = 128.0 - dist*16;
  430. if( dist < 0 ) dist = 0;
  431. if( dist > 255 ) dist = 255;
  432. out[i] = (unsigned char) dist;
  433. }
  434. /* Dual channel 16-bit output (more complicated, but good precision and range) */
  435. /*unsigned char *out = (unsigned char *) malloc( pixelAmount * 3 * sizeof(unsigned char) );
  436. for( i=0; i< pixelAmount; i++)
  437. {
  438. dist = outside[i] - inside[i];
  439. dist = 128.0 - dist*16;
  440. if( dist < 0.0 ) dist = 0.0;
  441. if( dist >= 256.0 ) dist = 255.999;
  442. // R channel is a copy of the original grayscale image
  443. out[3*i] = img[i];
  444. // G channel is fraction
  445. out[3*i + 1] = (unsigned char) ( 256 - (dist - floor(dist)* 256.0 ));
  446. // B channel is truncated integer part
  447. out[3*i + 2] = (unsigned char)dist;
  448. }*/
  449. free( xdist );
  450. free( ydist );
  451. free( gx );
  452. free( gy );
  453. free( data );
  454. free( outside );
  455. free( inside );
  456. return out;
  457. }
  458. void FontFreeType::renderCharAt(unsigned char *dest,int posX, int posY, unsigned char* bitmap,long bitmapWidth,long bitmapHeight)
  459. {
  460. int iX = posX;
  461. int iY = posY;
  462. if (_distanceFieldEnabled)
  463. {
  464. auto distanceMap = makeDistanceMap(bitmap,bitmapWidth,bitmapHeight);
  465. bitmapWidth += 2 * DistanceMapSpread;
  466. bitmapHeight += 2 * DistanceMapSpread;
  467. for (long y = 0; y < bitmapHeight; ++y)
  468. {
  469. long bitmap_y = y * bitmapWidth;
  470. for (long x = 0; x < bitmapWidth; ++x)
  471. {
  472. /* Dual channel 16-bit output (more complicated, but good precision and range) */
  473. /*int index = (iX + ( iY * destSize )) * 3;
  474. int index2 = (bitmap_y + x)*3;
  475. dest[index] = out[index2];
  476. dest[index + 1] = out[index2 + 1];
  477. dest[index + 2] = out[index2 + 2];*/
  478. //Single channel 8-bit output
  479. dest[iX + ( iY * FontAtlas::CacheTextureWidth )] = distanceMap[bitmap_y + x];
  480. iX += 1;
  481. }
  482. iX = posX;
  483. iY += 1;
  484. }
  485. free(distanceMap);
  486. }
  487. else if(_outlineSize > 0)
  488. {
  489. unsigned char tempChar;
  490. for (long y = 0; y < bitmapHeight; ++y)
  491. {
  492. long bitmap_y = y * bitmapWidth;
  493. for (int x = 0; x < bitmapWidth; ++x)
  494. {
  495. tempChar = bitmap[(bitmap_y + x) * 2];
  496. dest[(iX + ( iY * FontAtlas::CacheTextureWidth ) ) * 2] = tempChar;
  497. tempChar = bitmap[(bitmap_y + x) * 2 + 1];
  498. dest[(iX + ( iY * FontAtlas::CacheTextureWidth ) ) * 2 + 1] = tempChar;
  499. iX += 1;
  500. }
  501. iX = posX;
  502. iY += 1;
  503. }
  504. delete [] bitmap;
  505. }
  506. else
  507. {
  508. for (long y = 0; y < bitmapHeight; ++y)
  509. {
  510. long bitmap_y = y * bitmapWidth;
  511. for (int x = 0; x < bitmapWidth; ++x)
  512. {
  513. unsigned char cTemp = bitmap[bitmap_y + x];
  514. // the final pixel
  515. dest[(iX + ( iY * FontAtlas::CacheTextureWidth ) )] = cTemp;
  516. iX += 1;
  517. }
  518. iX = posX;
  519. iY += 1;
  520. }
  521. }
  522. }
  523. void FontFreeType::setGlyphCollection(GlyphCollection glyphs, const char* customGlyphs /* = nullptr */)
  524. {
  525. _usedGlyphs = glyphs;
  526. if (glyphs == GlyphCollection::CUSTOM)
  527. {
  528. _customGlyphs = customGlyphs;
  529. }
  530. }
  531. const char* FontFreeType::getGlyphCollection() const
  532. {
  533. const char* glyphCollection = nullptr;
  534. switch (_usedGlyphs)
  535. {
  536. case cocos2d::GlyphCollection::DYNAMIC:
  537. break;
  538. case cocos2d::GlyphCollection::NEHE:
  539. glyphCollection = _glyphNEHE;
  540. break;
  541. case cocos2d::GlyphCollection::ASCII:
  542. glyphCollection = _glyphASCII;
  543. break;
  544. case cocos2d::GlyphCollection::CUSTOM:
  545. glyphCollection = _customGlyphs.c_str();
  546. break;
  547. default:
  548. break;
  549. }
  550. return glyphCollection;
  551. }
  552. void FontFreeType::releaseFont(const std::string &fontName)
  553. {
  554. auto item = s_cacheFontData.begin();
  555. while (s_cacheFontData.end() != item)
  556. {
  557. if (item->first.find(fontName) != std::string::npos)
  558. item = s_cacheFontData.erase(item);
  559. else
  560. item++;
  561. }
  562. }
  563. NS_CC_END