font_manager.cpp 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708
  1. /*
  2. * Copyright 2013 Jeremie Roy. All rights reserved.
  3. * License: http://www.opensource.org/licenses/BSD-2-Clause
  4. */
  5. #define USE_EDTAA3 0
  6. #include <freetype/freetype.h>
  7. #include "../common.h"
  8. #include <bgfx.h>
  9. #include <math.h>
  10. #if USE_EDTAA3
  11. # include <edtaa3/edtaa3func.cpp>
  12. #else
  13. # define SDF_IMPLEMENTATION
  14. # include <sdf/sdf.h>
  15. #endif // USE_EDTAA3
  16. #include <wchar.h> // wcslen
  17. #include <tinystl/allocator.h>
  18. #include <tinystl/unordered_map.h>
  19. namespace stl = tinystl;
  20. #include "font_manager.h"
  21. #include "../cube_atlas.h"
  22. struct FTHolder
  23. {
  24. FT_Library library;
  25. FT_Face face;
  26. };
  27. class TrueTypeFont
  28. {
  29. public:
  30. TrueTypeFont();
  31. ~TrueTypeFont();
  32. /// Initialize from an external buffer
  33. /// @remark The ownership of the buffer is external, and you must ensure it stays valid up to this object lifetime
  34. /// @return true if the initialization succeed
  35. bool init(const uint8_t* _buffer, uint32_t _bufferSize, int32_t _fontIndex, uint32_t _pixelHeight);
  36. /// return the font descriptor of the current font
  37. FontInfo getFontInfo();
  38. /// raster a glyph as 8bit alpha to a memory buffer
  39. /// update the GlyphInfo according to the raster strategy
  40. /// @ remark buffer min size: glyphInfo.m_width * glyphInfo * height * sizeof(char)
  41. bool bakeGlyphAlpha(CodePoint _codePoint, GlyphInfo& _outGlyphInfo, uint8_t* _outBuffer);
  42. /// raster a glyph as 32bit subpixel rgba to a memory buffer
  43. /// update the GlyphInfo according to the raster strategy
  44. /// @ remark buffer min size: glyphInfo.m_width * glyphInfo * height * sizeof(uint32_t)
  45. bool bakeGlyphSubpixel(CodePoint _codePoint, GlyphInfo& _outGlyphInfo, uint8_t* _outBuffer);
  46. /// raster a glyph as 8bit signed distance to a memory buffer
  47. /// update the GlyphInfo according to the raster strategy
  48. /// @ remark buffer min size: glyphInfo.m_width * glyphInfo * height * sizeof(char)
  49. bool bakeGlyphDistance(CodePoint _codePoint, GlyphInfo& _outGlyphInfo, uint8_t* _outBuffer);
  50. private:
  51. FTHolder* m_font;
  52. };
  53. TrueTypeFont::TrueTypeFont() : m_font(NULL)
  54. {
  55. }
  56. TrueTypeFont::~TrueTypeFont()
  57. {
  58. if (NULL != m_font)
  59. {
  60. FT_Done_Face(m_font->face);
  61. FT_Done_FreeType(m_font->library);
  62. delete m_font;
  63. m_font = NULL;
  64. }
  65. }
  66. bool TrueTypeFont::init(const uint8_t* _buffer, uint32_t _bufferSize, int32_t _fontIndex, uint32_t _pixelHeight)
  67. {
  68. BX_CHECK(m_font == NULL, "TrueTypeFont already initialized");
  69. BX_CHECK( (_bufferSize > 256 && _bufferSize < 100000000), "TrueType buffer size is suspicious");
  70. BX_CHECK( (_pixelHeight > 4 && _pixelHeight < 128), "TrueType buffer size is suspicious");
  71. FTHolder* holder = new FTHolder;
  72. FT_Error error = FT_Init_FreeType(&holder->library);
  73. BX_WARN(!error, "FT_Init_FreeType failed.");
  74. if (error)
  75. {
  76. goto err0;
  77. }
  78. error = FT_New_Memory_Face(holder->library, _buffer, _bufferSize, _fontIndex, &holder->face);
  79. BX_WARN(!error, "FT_Init_FreeType failed.");
  80. if (error)
  81. {
  82. if (FT_Err_Unknown_File_Format == error)
  83. {
  84. goto err0;
  85. }
  86. goto err1;
  87. }
  88. error = FT_Select_Charmap(holder->face, FT_ENCODING_UNICODE);
  89. BX_WARN(!error, "FT_Init_FreeType failed.");
  90. if (error)
  91. {
  92. goto err2;
  93. }
  94. error = FT_Set_Pixel_Sizes(holder->face, 0, _pixelHeight);
  95. BX_WARN(!error, "FT_Init_FreeType failed.");
  96. if (error)
  97. {
  98. goto err2;
  99. }
  100. m_font = holder;
  101. return true;
  102. err2:
  103. FT_Done_Face(holder->face);
  104. err1:
  105. FT_Done_FreeType(holder->library);
  106. err0:
  107. delete holder;
  108. return false;
  109. }
  110. FontInfo TrueTypeFont::getFontInfo()
  111. {
  112. BX_CHECK(m_font != NULL, "TrueTypeFont not initialized");
  113. BX_CHECK(FT_IS_SCALABLE(m_font->face), "Font is unscalable");
  114. FT_Size_Metrics metrics = m_font->face->size->metrics;
  115. FontInfo outFontInfo;
  116. outFontInfo.scale = 1.0f;
  117. outFontInfo.ascender = metrics.ascender / 64.0f;
  118. outFontInfo.descender = metrics.descender / 64.0f;
  119. outFontInfo.lineGap = (metrics.height - metrics.ascender + metrics.descender) / 64.0f;
  120. outFontInfo.maxAdvanceWidth = metrics.max_advance/ 64.0f;
  121. outFontInfo.underlinePosition = FT_MulFix(m_font->face->underline_position, metrics.y_scale) / 64.0f;
  122. outFontInfo.underlineThickness = FT_MulFix(m_font->face->underline_thickness, metrics.y_scale) / 64.0f;
  123. return outFontInfo;
  124. }
  125. static void glyphInfoInit(GlyphInfo& _glyphInfo, FT_BitmapGlyph _bitmap, FT_GlyphSlot _slot, uint8_t* _dst, uint32_t _bpp)
  126. {
  127. int32_t xx = _bitmap->left;
  128. int32_t yy = -_bitmap->top;
  129. int32_t ww = _bitmap->bitmap.width;
  130. int32_t hh = _bitmap->bitmap.rows;
  131. _glyphInfo.offset_x = (float)xx;
  132. _glyphInfo.offset_y = (float)yy;
  133. _glyphInfo.width = (float)ww;
  134. _glyphInfo.height = (float)hh;
  135. _glyphInfo.advance_x = (float)_slot->advance.x / 64.0f;
  136. _glyphInfo.advance_y = (float)_slot->advance.y / 64.0f;
  137. uint32_t dstPitch = ww * _bpp;
  138. uint8_t* src = _bitmap->bitmap.buffer;
  139. uint32_t srcPitch = _bitmap->bitmap.pitch;
  140. for (int32_t ii = 0; ii < hh; ++ii)
  141. {
  142. memcpy(_dst, src, dstPitch);
  143. _dst += dstPitch;
  144. src += srcPitch;
  145. }
  146. }
  147. bool TrueTypeFont::bakeGlyphAlpha(CodePoint _codePoint, GlyphInfo& _glyphInfo, uint8_t* _outBuffer)
  148. {
  149. BX_CHECK(m_font != NULL, "TrueTypeFont not initialized");
  150. _glyphInfo.glyphIndex = FT_Get_Char_Index(m_font->face, _codePoint);
  151. FT_GlyphSlot slot = m_font->face->glyph;
  152. FT_Error error = FT_Load_Glyph(m_font->face, _glyphInfo.glyphIndex, FT_LOAD_DEFAULT);
  153. if (error)
  154. {
  155. return false;
  156. }
  157. FT_Glyph glyph;
  158. error = FT_Get_Glyph(slot, &glyph);
  159. if (error)
  160. {
  161. return false;
  162. }
  163. error = FT_Glyph_To_Bitmap(&glyph, FT_RENDER_MODE_NORMAL, 0, 1);
  164. if (error)
  165. {
  166. return false;
  167. }
  168. FT_BitmapGlyph bitmap = (FT_BitmapGlyph)glyph;
  169. glyphInfoInit(_glyphInfo, bitmap, slot, _outBuffer, 1);
  170. FT_Done_Glyph(glyph);
  171. return true;
  172. }
  173. bool TrueTypeFont::bakeGlyphSubpixel(CodePoint _codePoint, GlyphInfo& _glyphInfo, uint8_t* _outBuffer)
  174. {
  175. BX_CHECK(m_font != NULL, "TrueTypeFont not initialized");
  176. _glyphInfo.glyphIndex = FT_Get_Char_Index(m_font->face, _codePoint);
  177. FT_GlyphSlot slot = m_font->face->glyph;
  178. FT_Error error = FT_Load_Glyph(m_font->face, _glyphInfo.glyphIndex, FT_LOAD_DEFAULT);
  179. if (error)
  180. {
  181. return false;
  182. }
  183. FT_Glyph glyph;
  184. error = FT_Get_Glyph(slot, &glyph);
  185. if (error)
  186. {
  187. return false;
  188. }
  189. error = FT_Glyph_To_Bitmap(&glyph, FT_RENDER_MODE_LCD, 0, 1);
  190. if (error)
  191. {
  192. return false;
  193. }
  194. FT_BitmapGlyph bitmap = (FT_BitmapGlyph)glyph;
  195. glyphInfoInit(_glyphInfo, bitmap, slot, _outBuffer, 3);
  196. FT_Done_Glyph(glyph);
  197. return true;
  198. }
  199. static void makeDistanceMap(const uint8_t* _img, uint8_t* _outImg, uint32_t _width, uint32_t _height)
  200. {
  201. #if USE_EDTAA3
  202. int16_t* xdist = (int16_t*)malloc(_width * _height * sizeof(int16_t) );
  203. int16_t* ydist = (int16_t*)malloc(_width * _height * sizeof(int16_t) );
  204. double* gx = (double*)calloc(_width * _height, sizeof(double) );
  205. double* gy = (double*)calloc(_width * _height, sizeof(double) );
  206. double* data = (double*)calloc(_width * _height, sizeof(double) );
  207. double* outside = (double*)calloc(_width * _height, sizeof(double) );
  208. double* inside = (double*)calloc(_width * _height, sizeof(double) );
  209. uint32_t ii;
  210. // Convert img into double (data)
  211. double img_min = 255, img_max = -255;
  212. for (ii = 0; ii < _width * _height; ++ii)
  213. {
  214. double v = _img[ii];
  215. data[ii] = v;
  216. if (v > img_max)
  217. {
  218. img_max = v;
  219. }
  220. if (v < img_min)
  221. {
  222. img_min = v;
  223. }
  224. }
  225. // Rescale image levels between 0 and 1
  226. for (ii = 0; ii < _width * _height; ++ii)
  227. {
  228. data[ii] = (_img[ii] - img_min) / (img_max - img_min);
  229. }
  230. // Compute outside = edtaa3(bitmap); % Transform background (0's)
  231. computegradient(data, _width, _height, gx, gy);
  232. edtaa3(data, gx, gy, _width, _height, xdist, ydist, outside);
  233. for (ii = 0; ii < _width * _height; ++ii)
  234. {
  235. if (outside[ii] < 0)
  236. {
  237. outside[ii] = 0.0;
  238. }
  239. }
  240. // Compute inside = edtaa3(1-bitmap); % Transform foreground (1's)
  241. memset(gx, 0, sizeof(double) * _width * _height);
  242. memset(gy, 0, sizeof(double) * _width * _height);
  243. for (ii = 0; ii < _width * _height; ++ii)
  244. {
  245. data[ii] = 1.0 - data[ii];
  246. }
  247. computegradient(data, _width, _height, gx, gy);
  248. edtaa3(data, gx, gy, _width, _height, xdist, ydist, inside);
  249. for (ii = 0; ii < _width * _height; ++ii)
  250. {
  251. if (inside[ii] < 0)
  252. {
  253. inside[ii] = 0.0;
  254. }
  255. }
  256. // distmap = outside - inside; % Bipolar distance field
  257. uint8_t* out = _outImg;
  258. for (ii = 0; ii < _width * _height; ++ii)
  259. {
  260. outside[ii] -= inside[ii];
  261. outside[ii] = 128 + outside[ii] * 16;
  262. if (outside[ii] < 0)
  263. {
  264. outside[ii] = 0;
  265. }
  266. if (outside[ii] > 255)
  267. {
  268. outside[ii] = 255;
  269. }
  270. out[ii] = 255 - (uint8_t) outside[ii];
  271. }
  272. free(xdist);
  273. free(ydist);
  274. free(gx);
  275. free(gy);
  276. free(data);
  277. free(outside);
  278. free(inside);
  279. #else
  280. sdfBuild(_outImg, _width, 8.0f, _img, _width, _height, _width);
  281. #endif // USE_EDTAA3
  282. }
  283. bool TrueTypeFont::bakeGlyphDistance(CodePoint _codePoint, GlyphInfo& _glyphInfo, uint8_t* _outBuffer)
  284. {
  285. BX_CHECK(m_font != NULL, "TrueTypeFont not initialized");
  286. _glyphInfo.glyphIndex = FT_Get_Char_Index(m_font->face, _codePoint);
  287. FT_Int32 loadMode = FT_LOAD_DEFAULT | FT_LOAD_NO_HINTING;
  288. FT_Render_Mode renderMode = FT_RENDER_MODE_NORMAL;
  289. FT_GlyphSlot slot = m_font->face->glyph;
  290. FT_Error error = FT_Load_Glyph(m_font->face, _glyphInfo.glyphIndex, loadMode);
  291. if (error)
  292. {
  293. return false;
  294. }
  295. FT_Glyph glyph;
  296. error = FT_Get_Glyph(slot, &glyph);
  297. if (error)
  298. {
  299. return false;
  300. }
  301. error = FT_Glyph_To_Bitmap(&glyph, renderMode, 0, 1);
  302. if (error)
  303. {
  304. return false;
  305. }
  306. FT_BitmapGlyph bitmap = (FT_BitmapGlyph)glyph;
  307. int32_t ww = bitmap->bitmap.width;
  308. int32_t hh = bitmap->bitmap.rows;
  309. glyphInfoInit(_glyphInfo, bitmap, slot, _outBuffer, 1);
  310. FT_Done_Glyph(glyph);
  311. if (ww * hh > 0)
  312. {
  313. uint32_t dw = 6;
  314. uint32_t dh = 6;
  315. uint32_t nw = ww + dw * 2;
  316. uint32_t nh = hh + dh * 2;
  317. BX_CHECK(nw * nh < 128 * 128, "Buffer overflow (size %d)", nw * nh);
  318. uint32_t buffSize = nw * nh * sizeof(uint8_t);
  319. uint8_t* alphaImg = (uint8_t*)malloc(buffSize);
  320. memset(alphaImg, 0, nw * nh * sizeof(uint8_t) );
  321. //copy the original buffer to the temp one
  322. for (uint32_t ii = dh; ii < nh - dh; ++ii)
  323. {
  324. memcpy(alphaImg + ii * nw + dw, _outBuffer + (ii - dh) * ww, ww);
  325. }
  326. makeDistanceMap(alphaImg, _outBuffer, nw, nh);
  327. free(alphaImg);
  328. _glyphInfo.offset_x -= (float)dw;
  329. _glyphInfo.offset_y -= (float)dh;
  330. _glyphInfo.width = (float)nw;
  331. _glyphInfo.height = (float)nh;
  332. }
  333. return true;
  334. }
  335. typedef stl::unordered_map<CodePoint, GlyphInfo> GlyphHashMap;
  336. // cache font data
  337. struct FontManager::CachedFont
  338. {
  339. CachedFont()
  340. : trueTypeFont(NULL)
  341. {
  342. masterFontHandle.idx = bx::HandleAlloc::invalid;
  343. }
  344. FontInfo fontInfo;
  345. GlyphHashMap cachedGlyphs;
  346. TrueTypeFont* trueTypeFont;
  347. // an handle to a master font in case of sub distance field font
  348. FontHandle masterFontHandle;
  349. int16_t padding;
  350. };
  351. #define MAX_FONT_BUFFER_SIZE (512 * 512 * 4)
  352. FontManager::FontManager(Atlas* _atlas)
  353. : m_ownAtlas(false)
  354. , m_atlas(_atlas)
  355. {
  356. init();
  357. }
  358. FontManager::FontManager(uint32_t _textureSideWidth)
  359. : m_ownAtlas(true)
  360. , m_atlas(new Atlas(_textureSideWidth) )
  361. {
  362. init();
  363. }
  364. void FontManager::init()
  365. {
  366. m_cachedFiles = new CachedFile[MAX_OPENED_FILES];
  367. m_cachedFonts = new CachedFont[MAX_OPENED_FONT];
  368. m_buffer = new uint8_t[MAX_FONT_BUFFER_SIZE];
  369. const uint32_t W = 3;
  370. // Create filler rectangle
  371. uint8_t buffer[W * W * 4];
  372. memset(buffer, 255, W * W * 4);
  373. m_blackGlyph.width = W;
  374. m_blackGlyph.height = W;
  375. ///make sure the black glyph doesn't bleed by using a one pixel inner outline
  376. m_blackGlyph.regionIndex = m_atlas->addRegion(W, W, buffer, AtlasRegion::TYPE_GRAY, 1);
  377. }
  378. FontManager::~FontManager()
  379. {
  380. BX_CHECK(m_fontHandles.getNumHandles() == 0, "All the fonts must be destroyed before destroying the manager");
  381. delete [] m_cachedFonts;
  382. BX_CHECK(m_filesHandles.getNumHandles() == 0, "All the font files must be destroyed before destroying the manager");
  383. delete [] m_cachedFiles;
  384. delete [] m_buffer;
  385. if (m_ownAtlas)
  386. {
  387. delete m_atlas;
  388. }
  389. }
  390. TrueTypeHandle FontManager::createTtf(const uint8_t* _buffer, uint32_t _size)
  391. {
  392. uint16_t id = m_filesHandles.alloc();
  393. BX_CHECK(id != bx::HandleAlloc::invalid, "Invalid handle used");
  394. m_cachedFiles[id].buffer = new uint8_t[_size];
  395. m_cachedFiles[id].bufferSize = _size;
  396. memcpy(m_cachedFiles[id].buffer, _buffer, _size);
  397. TrueTypeHandle ret = { id };
  398. return ret;
  399. }
  400. void FontManager::destroyTtf(TrueTypeHandle _handle)
  401. {
  402. BX_CHECK(bgfx::isValid(_handle), "Invalid handle used");
  403. delete m_cachedFiles[_handle.idx].buffer;
  404. m_cachedFiles[_handle.idx].bufferSize = 0;
  405. m_cachedFiles[_handle.idx].buffer = NULL;
  406. m_filesHandles.free(_handle.idx);
  407. }
  408. FontHandle FontManager::createFontByPixelSize(TrueTypeHandle _ttfHandle, uint32_t _typefaceIndex, uint32_t _pixelSize, uint32_t _fontType)
  409. {
  410. BX_CHECK(bgfx::isValid(_ttfHandle), "Invalid handle used");
  411. TrueTypeFont* ttf = new TrueTypeFont();
  412. if (!ttf->init(m_cachedFiles[_ttfHandle.idx].buffer, m_cachedFiles[_ttfHandle.idx].bufferSize, _typefaceIndex, _pixelSize) )
  413. {
  414. delete ttf;
  415. FontHandle invalid = { bx::HandleAlloc::invalid };
  416. return invalid;
  417. }
  418. uint16_t fontIdx = m_fontHandles.alloc();
  419. BX_CHECK(fontIdx != bx::HandleAlloc::invalid, "Invalid handle used");
  420. CachedFont& font = m_cachedFonts[fontIdx];
  421. font.trueTypeFont = ttf;
  422. font.fontInfo = ttf->getFontInfo();
  423. font.fontInfo.fontType = _fontType;
  424. font.fontInfo.pixelSize = _pixelSize;
  425. font.cachedGlyphs.clear();
  426. font.masterFontHandle.idx = bx::HandleAlloc::invalid;
  427. FontHandle handle = { fontIdx };
  428. return handle;
  429. }
  430. FontHandle FontManager::createScaledFontToPixelSize(FontHandle _baseFontHandle, uint32_t _pixelSize)
  431. {
  432. BX_CHECK(bgfx::isValid(_baseFontHandle), "Invalid handle used");
  433. CachedFont& baseFont = m_cachedFonts[_baseFontHandle.idx];
  434. FontInfo& fontInfo = baseFont.fontInfo;
  435. FontInfo newFontInfo = fontInfo;
  436. newFontInfo.pixelSize = _pixelSize;
  437. newFontInfo.scale = (float)_pixelSize / (float) fontInfo.pixelSize;
  438. newFontInfo.ascender = (newFontInfo.ascender * newFontInfo.scale);
  439. newFontInfo.descender = (newFontInfo.descender * newFontInfo.scale);
  440. newFontInfo.lineGap = (newFontInfo.lineGap * newFontInfo.scale);
  441. newFontInfo.maxAdvanceWidth = (newFontInfo.maxAdvanceWidth * newFontInfo.scale);
  442. newFontInfo.underlineThickness = (newFontInfo.underlineThickness * newFontInfo.scale);
  443. newFontInfo.underlinePosition = (newFontInfo.underlinePosition * newFontInfo.scale);
  444. uint16_t fontIdx = m_fontHandles.alloc();
  445. BX_CHECK(fontIdx != bx::HandleAlloc::invalid, "Invalid handle used");
  446. CachedFont& font = m_cachedFonts[fontIdx];
  447. font.cachedGlyphs.clear();
  448. font.fontInfo = newFontInfo;
  449. font.trueTypeFont = NULL;
  450. font.masterFontHandle = _baseFontHandle;
  451. FontHandle handle = { fontIdx };
  452. return handle;
  453. }
  454. void FontManager::destroyFont(FontHandle _handle)
  455. {
  456. BX_CHECK(bgfx::isValid(_handle), "Invalid handle used");
  457. CachedFont& font = m_cachedFonts[_handle.idx];
  458. if (font.trueTypeFont != NULL)
  459. {
  460. delete font.trueTypeFont;
  461. font.trueTypeFont = NULL;
  462. }
  463. font.cachedGlyphs.clear();
  464. m_fontHandles.free(_handle.idx);
  465. }
  466. bool FontManager::preloadGlyph(FontHandle _handle, const wchar_t* _string)
  467. {
  468. BX_CHECK(bgfx::isValid(_handle), "Invalid handle used");
  469. CachedFont& font = m_cachedFonts[_handle.idx];
  470. if (NULL == font.trueTypeFont)
  471. {
  472. return false;
  473. }
  474. for (uint32_t ii = 0, end = (uint32_t)wcslen(_string); ii < end; ++ii)
  475. {
  476. CodePoint codePoint = _string[ii];
  477. if (!preloadGlyph(_handle, codePoint) )
  478. {
  479. return false;
  480. }
  481. }
  482. return true;
  483. }
  484. bool FontManager::preloadGlyph(FontHandle _handle, CodePoint _codePoint)
  485. {
  486. BX_CHECK(bgfx::isValid(_handle), "Invalid handle used");
  487. CachedFont& font = m_cachedFonts[_handle.idx];
  488. FontInfo& fontInfo = font.fontInfo;
  489. GlyphHashMap::iterator iter = font.cachedGlyphs.find(_codePoint);
  490. if (iter != font.cachedGlyphs.end() )
  491. {
  492. return true;
  493. }
  494. if (NULL != font.trueTypeFont)
  495. {
  496. GlyphInfo glyphInfo;
  497. switch (font.fontInfo.fontType)
  498. {
  499. case FONT_TYPE_ALPHA:
  500. font.trueTypeFont->bakeGlyphAlpha(_codePoint, glyphInfo, m_buffer);
  501. break;
  502. case FONT_TYPE_DISTANCE:
  503. font.trueTypeFont->bakeGlyphDistance(_codePoint, glyphInfo, m_buffer);
  504. break;
  505. case FONT_TYPE_DISTANCE_SUBPIXEL:
  506. font.trueTypeFont->bakeGlyphDistance(_codePoint, glyphInfo, m_buffer);
  507. break;
  508. default:
  509. BX_CHECK(false, "TextureType not supported yet");
  510. }
  511. if (!addBitmap(glyphInfo, m_buffer) )
  512. {
  513. return false;
  514. }
  515. glyphInfo.advance_x = (glyphInfo.advance_x * fontInfo.scale);
  516. glyphInfo.advance_y = (glyphInfo.advance_y * fontInfo.scale);
  517. glyphInfo.offset_x = (glyphInfo.offset_x * fontInfo.scale);
  518. glyphInfo.offset_y = (glyphInfo.offset_y * fontInfo.scale);
  519. glyphInfo.height = (glyphInfo.height * fontInfo.scale);
  520. glyphInfo.width = (glyphInfo.width * fontInfo.scale);
  521. font.cachedGlyphs[_codePoint] = glyphInfo;
  522. return true;
  523. }
  524. if (isValid(font.masterFontHandle)
  525. && preloadGlyph(font.masterFontHandle, _codePoint) )
  526. {
  527. const GlyphInfo* glyph = getGlyphInfo(font.masterFontHandle, _codePoint);
  528. GlyphInfo glyphInfo = *glyph;
  529. glyphInfo.advance_x = (glyphInfo.advance_x * fontInfo.scale);
  530. glyphInfo.advance_y = (glyphInfo.advance_y * fontInfo.scale);
  531. glyphInfo.offset_x = (glyphInfo.offset_x * fontInfo.scale);
  532. glyphInfo.offset_y = (glyphInfo.offset_y * fontInfo.scale);
  533. glyphInfo.height = (glyphInfo.height * fontInfo.scale);
  534. glyphInfo.width = (glyphInfo.width * fontInfo.scale);
  535. font.cachedGlyphs[_codePoint] = glyphInfo;
  536. return true;
  537. }
  538. return false;
  539. }
  540. const FontInfo& FontManager::getFontInfo(FontHandle _handle) const
  541. {
  542. BX_CHECK(bgfx::isValid(_handle), "Invalid handle used");
  543. return m_cachedFonts[_handle.idx].fontInfo;
  544. }
  545. const GlyphInfo* FontManager::getGlyphInfo(FontHandle _handle, CodePoint _codePoint)
  546. {
  547. const GlyphHashMap& cachedGlyphs = m_cachedFonts[_handle.idx].cachedGlyphs;
  548. GlyphHashMap::const_iterator it = cachedGlyphs.find(_codePoint);
  549. if (it == cachedGlyphs.end() )
  550. {
  551. if (!preloadGlyph(_handle, _codePoint) )
  552. {
  553. return NULL;
  554. }
  555. it = cachedGlyphs.find(_codePoint);
  556. }
  557. BX_CHECK(it != cachedGlyphs.end(), "Failed to preload glyph.");
  558. return &it->second;
  559. }
  560. bool FontManager::addBitmap(GlyphInfo& _glyphInfo, const uint8_t* _data)
  561. {
  562. _glyphInfo.regionIndex = m_atlas->addRegion( (uint16_t) ceil(_glyphInfo.width), (uint16_t) ceil(_glyphInfo.height), _data, AtlasRegion::TYPE_GRAY);
  563. return true;
  564. }