font_manager.cpp 18 KB

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