font_manager.cpp 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563
  1. /*
  2. * Copyright 2013 Jeremie Roy. All rights reserved.
  3. * License: https://github.com/bkaradzic/bgfx/blob/master/LICENSE
  4. */
  5. #include <bx/bx.h>
  6. #include <stb/stb_truetype.h>
  7. #include "../common.h"
  8. #include <bgfx/bgfx.h>
  9. BX_PRAGMA_DIAGNOSTIC_PUSH()
  10. BX_PRAGMA_DIAGNOSTIC_IGNORED_MSVC(4244) // warning C4244: '=': conversion from 'double' to 'float', possible loss of data
  11. BX_PRAGMA_DIAGNOSTIC_IGNORED_MSVC(4701) // warning C4701: potentially uninitialized local variable 'pt' used
  12. #define SDF_IMPLEMENTATION
  13. #include <sdf/sdf.h>
  14. BX_PRAGMA_DIAGNOSTIC_POP()
  15. #include <wchar.h> // wcslen
  16. #include <tinystl/allocator.h>
  17. #include <tinystl/unordered_map.h>
  18. namespace stl = tinystl;
  19. #include "font_manager.h"
  20. #include "../cube_atlas.h"
  21. class TrueTypeFont
  22. {
  23. public:
  24. TrueTypeFont();
  25. ~TrueTypeFont();
  26. /// Initialize from an external buffer
  27. /// @remark The ownership of the buffer is external, and you must ensure it stays valid up to this object lifetime
  28. /// @return true if the initialization succeed
  29. bool init(const uint8_t* _buffer, uint32_t _bufferSize, int32_t _fontIndex, uint32_t _pixelHeight, int16_t _widthPadding, int16_t _heightPadding);
  30. /// return the font descriptor of the current font
  31. FontInfo getFontInfo();
  32. /// raster a glyph as 8bit alpha to a memory buffer
  33. /// update the GlyphInfo according to the raster strategy
  34. /// @ remark buffer min size: glyphInfo.m_width * glyphInfo * height * sizeof(char)
  35. bool bakeGlyphAlpha(CodePoint _codePoint, GlyphInfo& _outGlyphInfo, uint8_t* _outBuffer);
  36. /// raster a glyph as 8bit signed distance to a memory buffer
  37. /// update the GlyphInfo according to the raster strategy
  38. /// @ remark buffer min size: glyphInfo.m_width * glyphInfo * height * sizeof(char)
  39. bool bakeGlyphDistance(CodePoint _codePoint, GlyphInfo& _outGlyphInfo, uint8_t* _outBuffer);
  40. private:
  41. friend class FontManager;
  42. stbtt_fontinfo m_font;
  43. float m_scale;
  44. int16_t m_widthPadding;
  45. int16_t m_heightPadding;
  46. };
  47. TrueTypeFont::TrueTypeFont() : m_font()
  48. , m_widthPadding(6)
  49. , m_heightPadding(6)
  50. {
  51. }
  52. TrueTypeFont::~TrueTypeFont()
  53. {
  54. }
  55. bool TrueTypeFont::init(const uint8_t* _buffer, uint32_t _bufferSize, int32_t _fontIndex, uint32_t _pixelHeight, int16_t _widthPadding, int16_t _heightPadding)
  56. {
  57. BX_WARN( (_bufferSize > 256 && _bufferSize < 100000000), "(FontIndex %d) TrueType buffer size is suspicious (%d)", _fontIndex, _bufferSize);
  58. BX_WARN( (_pixelHeight > 4 && _pixelHeight < 128), "(FontIndex %d) TrueType pixel height is suspicious (%d)", _fontIndex, _pixelHeight);
  59. BX_UNUSED(_bufferSize);
  60. int offset = stbtt_GetFontOffsetForIndex(_buffer, _fontIndex);
  61. stbtt_InitFont(&m_font, _buffer, offset);
  62. m_scale = stbtt_ScaleForMappingEmToPixels(&m_font, (float)_pixelHeight);
  63. m_widthPadding = _widthPadding;
  64. m_heightPadding = _heightPadding;
  65. return true;
  66. }
  67. FontInfo TrueTypeFont::getFontInfo()
  68. {
  69. int ascent;
  70. int descent;
  71. int lineGap;
  72. stbtt_GetFontVMetrics(&m_font, &ascent, &descent, &lineGap);
  73. float scale = m_scale;
  74. int x0, y0, x1, y1;
  75. stbtt_GetFontBoundingBox(&m_font, &x0, &y0, &x1, &y1);
  76. FontInfo outFontInfo;
  77. outFontInfo.scale = 1.0f;
  78. outFontInfo.ascender = bx::round(ascent * scale);
  79. outFontInfo.descender = bx::round(descent * scale);
  80. outFontInfo.lineGap = bx::round(lineGap * scale);
  81. outFontInfo.maxAdvanceWidth = bx::round((y1 - y0) * scale);
  82. outFontInfo.underlinePosition = (x1 - x0) * scale - ascent;
  83. outFontInfo.underlineThickness = (x1 - x0) * scale / 24.f;
  84. return outFontInfo;
  85. }
  86. bool TrueTypeFont::bakeGlyphAlpha(CodePoint _codePoint, GlyphInfo& _glyphInfo, uint8_t* _outBuffer)
  87. {
  88. int32_t ascent, descent, lineGap;
  89. stbtt_GetFontVMetrics(&m_font, &ascent, &descent, &lineGap);
  90. int32_t advance, lsb;
  91. stbtt_GetCodepointHMetrics(&m_font, _codePoint, &advance, &lsb);
  92. const float scale = m_scale;
  93. int32_t x0, y0, x1, y1;
  94. stbtt_GetCodepointBitmapBox(&m_font, _codePoint, scale, scale, &x0, &y0, &x1, &y1);
  95. const int32_t ww = x1-x0;
  96. const int32_t hh = y1-y0;
  97. _glyphInfo.offset_x = (float)x0;
  98. _glyphInfo.offset_y = (float)y0;
  99. _glyphInfo.width = (float)ww;
  100. _glyphInfo.height = (float)hh;
  101. _glyphInfo.advance_x = bx::round(((float)advance) * scale);
  102. _glyphInfo.advance_y = bx::round(((float)(ascent + descent + lineGap)) * scale);
  103. uint32_t bpp = 1;
  104. uint32_t dstPitch = ww * bpp;
  105. stbtt_MakeCodepointBitmap(&m_font, _outBuffer, ww, hh, dstPitch, scale, scale, _codePoint);
  106. return true;
  107. }
  108. bool TrueTypeFont::bakeGlyphDistance(CodePoint _codePoint, GlyphInfo& _glyphInfo, uint8_t* _outBuffer)
  109. {
  110. int32_t ascent, descent, lineGap;
  111. stbtt_GetFontVMetrics(&m_font, &ascent, &descent, &lineGap);
  112. int32_t advance, lsb;
  113. stbtt_GetCodepointHMetrics(&m_font, _codePoint, &advance, &lsb);
  114. const float scale = m_scale;
  115. int32_t x0, y0, x1, y1;
  116. stbtt_GetCodepointBitmapBox(&m_font, _codePoint, scale, scale, &x0, &y0, &x1, &y1);
  117. const int32_t ww = x1-x0;
  118. const int32_t hh = y1-y0;
  119. _glyphInfo.offset_x = (float)x0;
  120. _glyphInfo.offset_y = (float)y0;
  121. _glyphInfo.width = (float)ww;
  122. _glyphInfo.height = (float)hh;
  123. _glyphInfo.advance_x = bx::round(((float)advance) * scale);
  124. _glyphInfo.advance_y = bx::round(((float)(ascent + descent + lineGap)) * scale);
  125. uint32_t bpp = 1;
  126. uint32_t dstPitch = ww * bpp;
  127. stbtt_MakeCodepointBitmap(&m_font, _outBuffer, ww, hh, dstPitch, scale, scale, _codePoint);
  128. if (ww * hh > 0)
  129. {
  130. uint32_t dw = m_widthPadding;
  131. uint32_t dh = m_heightPadding;
  132. uint32_t nw = ww + dw * 2;
  133. uint32_t nh = hh + dh * 2;
  134. BX_ASSERT(nw * nh < 128 * 128, "Buffer overflow (size %d)", nw * nh);
  135. uint32_t buffSize = nw * nh * sizeof(uint8_t);
  136. uint8_t* alphaImg = (uint8_t*)malloc(buffSize);
  137. bx::memSet(alphaImg, 0, nw * nh * sizeof(uint8_t) );
  138. //copy the original buffer to the temp one
  139. for (uint32_t ii = dh; ii < nh - dh; ++ii)
  140. {
  141. bx::memCopy(alphaImg + ii * nw + dw, _outBuffer + (ii - dh) * ww, ww);
  142. }
  143. // stb_truetype has some builtin sdf functionality, we can investigate using that too
  144. sdfBuildDistanceField(_outBuffer, nw, 8.0f, alphaImg, nw, nh, nw);
  145. free(alphaImg);
  146. _glyphInfo.offset_x -= (float)dw;
  147. _glyphInfo.offset_y -= (float)dh;
  148. _glyphInfo.width = (float)nw;
  149. _glyphInfo.height = (float)nh;
  150. }
  151. return true;
  152. }
  153. typedef stl::unordered_map<CodePoint, GlyphInfo> GlyphHashMap;
  154. // cache font data
  155. struct FontManager::CachedFont
  156. {
  157. CachedFont()
  158. : trueTypeFont(NULL)
  159. {
  160. masterFontHandle.idx = bx::kInvalidHandle;
  161. }
  162. FontInfo fontInfo;
  163. GlyphHashMap cachedGlyphs;
  164. TrueTypeFont* trueTypeFont;
  165. // an handle to a master font in case of sub distance field font
  166. FontHandle masterFontHandle;
  167. int16_t padding;
  168. };
  169. #define MAX_FONT_BUFFER_SIZE (512 * 512 * 4)
  170. FontManager::FontManager(Atlas* _atlas)
  171. : m_ownAtlas(false)
  172. , m_atlas(_atlas)
  173. {
  174. init();
  175. }
  176. FontManager::FontManager(uint16_t _textureSideWidth)
  177. : m_ownAtlas(true)
  178. , m_atlas(new Atlas(_textureSideWidth) )
  179. {
  180. init();
  181. }
  182. void FontManager::init()
  183. {
  184. m_cachedFiles = new CachedFile[MAX_OPENED_FILES];
  185. m_cachedFonts = new CachedFont[MAX_OPENED_FONT];
  186. m_buffer = new uint8_t[MAX_FONT_BUFFER_SIZE];
  187. const uint32_t W = 3;
  188. // Create filler rectangle
  189. uint8_t buffer[W * W * 4];
  190. bx::memSet(buffer, 255, W * W * 4);
  191. m_blackGlyph.width = W;
  192. m_blackGlyph.height = W;
  193. ///make sure the black glyph doesn't bleed by using a one pixel inner outline
  194. m_blackGlyph.regionIndex = m_atlas->addRegion(W, W, buffer, AtlasRegion::TYPE_GRAY, 1);
  195. }
  196. FontManager::~FontManager()
  197. {
  198. BX_ASSERT(m_fontHandles.getNumHandles() == 0, "All the fonts must be destroyed before destroying the manager");
  199. delete [] m_cachedFonts;
  200. BX_ASSERT(m_filesHandles.getNumHandles() == 0, "All the font files must be destroyed before destroying the manager");
  201. delete [] m_cachedFiles;
  202. delete [] m_buffer;
  203. if (m_ownAtlas)
  204. {
  205. delete m_atlas;
  206. }
  207. }
  208. TrueTypeHandle FontManager::createTtf(const uint8_t* _buffer, uint32_t _size)
  209. {
  210. uint16_t id = m_filesHandles.alloc();
  211. BX_ASSERT(id != bx::kInvalidHandle, "Invalid handle used");
  212. m_cachedFiles[id].buffer = new uint8_t[_size];
  213. m_cachedFiles[id].bufferSize = _size;
  214. bx::memCopy(m_cachedFiles[id].buffer, _buffer, _size);
  215. TrueTypeHandle ret = { id };
  216. return ret;
  217. }
  218. void FontManager::destroyTtf(TrueTypeHandle _handle)
  219. {
  220. BX_ASSERT(isValid(_handle), "Invalid handle used");
  221. delete[] m_cachedFiles[_handle.idx].buffer;
  222. m_cachedFiles[_handle.idx].bufferSize = 0;
  223. m_cachedFiles[_handle.idx].buffer = NULL;
  224. m_filesHandles.free(_handle.idx);
  225. }
  226. FontHandle FontManager::createFontByPixelSize(TrueTypeHandle _ttfHandle, uint32_t _typefaceIndex, uint32_t _pixelSize, uint32_t _fontType,
  227. uint16_t _glyphWidthPadding, uint16_t _glyphHeightPadding)
  228. {
  229. BX_ASSERT(isValid(_ttfHandle), "Invalid handle used");
  230. TrueTypeFont* ttf = new TrueTypeFont();
  231. if (!ttf->init(m_cachedFiles[_ttfHandle.idx].buffer, m_cachedFiles[_ttfHandle.idx].bufferSize, _typefaceIndex, _pixelSize, _glyphWidthPadding, _glyphHeightPadding) )
  232. {
  233. delete ttf;
  234. FontHandle invalid = { bx::kInvalidHandle };
  235. return invalid;
  236. }
  237. uint16_t fontIdx = m_fontHandles.alloc();
  238. BX_ASSERT(fontIdx != bx::kInvalidHandle, "Invalid handle used");
  239. CachedFont& font = m_cachedFonts[fontIdx];
  240. font.trueTypeFont = ttf;
  241. font.fontInfo = ttf->getFontInfo();
  242. font.fontInfo.fontType = int16_t(_fontType);
  243. font.fontInfo.pixelSize = uint16_t(_pixelSize);
  244. font.cachedGlyphs.clear();
  245. font.masterFontHandle.idx = bx::kInvalidHandle;
  246. FontHandle handle = { fontIdx };
  247. return handle;
  248. }
  249. FontHandle FontManager::createScaledFontToPixelSize(FontHandle _baseFontHandle, uint32_t _pixelSize)
  250. {
  251. BX_ASSERT(isValid(_baseFontHandle), "Invalid handle used");
  252. CachedFont& baseFont = m_cachedFonts[_baseFontHandle.idx];
  253. FontInfo& fontInfo = baseFont.fontInfo;
  254. FontInfo newFontInfo = fontInfo;
  255. newFontInfo.pixelSize = uint16_t(_pixelSize);
  256. newFontInfo.scale = (float)_pixelSize / (float) fontInfo.pixelSize;
  257. newFontInfo.ascender = (newFontInfo.ascender * newFontInfo.scale);
  258. newFontInfo.descender = (newFontInfo.descender * newFontInfo.scale);
  259. newFontInfo.lineGap = (newFontInfo.lineGap * newFontInfo.scale);
  260. newFontInfo.maxAdvanceWidth = (newFontInfo.maxAdvanceWidth * newFontInfo.scale);
  261. newFontInfo.underlineThickness = (newFontInfo.underlineThickness * newFontInfo.scale);
  262. newFontInfo.underlinePosition = (newFontInfo.underlinePosition * newFontInfo.scale);
  263. uint16_t fontIdx = m_fontHandles.alloc();
  264. BX_ASSERT(fontIdx != bx::kInvalidHandle, "Invalid handle used");
  265. CachedFont& font = m_cachedFonts[fontIdx];
  266. font.cachedGlyphs.clear();
  267. font.fontInfo = newFontInfo;
  268. font.trueTypeFont = NULL;
  269. font.masterFontHandle = _baseFontHandle;
  270. FontHandle handle = { fontIdx };
  271. return handle;
  272. }
  273. void FontManager::destroyFont(FontHandle _handle)
  274. {
  275. BX_ASSERT(isValid(_handle), "Invalid handle used");
  276. CachedFont& font = m_cachedFonts[_handle.idx];
  277. if (font.trueTypeFont != NULL)
  278. {
  279. delete font.trueTypeFont;
  280. font.trueTypeFont = NULL;
  281. }
  282. font.cachedGlyphs.clear();
  283. m_fontHandles.free(_handle.idx);
  284. }
  285. bool FontManager::preloadGlyph(FontHandle _handle, const wchar_t* _string)
  286. {
  287. BX_ASSERT(isValid(_handle), "Invalid handle used");
  288. CachedFont& font = m_cachedFonts[_handle.idx];
  289. if (NULL == font.trueTypeFont)
  290. {
  291. return false;
  292. }
  293. for (uint32_t ii = 0, end = (uint32_t)wcslen(_string); ii < end; ++ii)
  294. {
  295. CodePoint codePoint = _string[ii];
  296. if (!preloadGlyph(_handle, codePoint) )
  297. {
  298. return false;
  299. }
  300. }
  301. return true;
  302. }
  303. bool FontManager::preloadGlyph(FontHandle _handle, CodePoint _codePoint)
  304. {
  305. BX_ASSERT(isValid(_handle), "Invalid handle used");
  306. CachedFont& font = m_cachedFonts[_handle.idx];
  307. FontInfo& fontInfo = font.fontInfo;
  308. GlyphHashMap::iterator iter = font.cachedGlyphs.find(_codePoint);
  309. if (iter != font.cachedGlyphs.end() )
  310. {
  311. return true;
  312. }
  313. if (NULL != font.trueTypeFont)
  314. {
  315. GlyphInfo glyphInfo;
  316. switch (font.fontInfo.fontType)
  317. {
  318. case FONT_TYPE_ALPHA:
  319. font.trueTypeFont->bakeGlyphAlpha(_codePoint, glyphInfo, m_buffer);
  320. break;
  321. case FONT_TYPE_DISTANCE:
  322. font.trueTypeFont->bakeGlyphDistance(_codePoint, glyphInfo, m_buffer);
  323. break;
  324. case FONT_TYPE_DISTANCE_SUBPIXEL:
  325. font.trueTypeFont->bakeGlyphDistance(_codePoint, glyphInfo, m_buffer);
  326. break;
  327. case FONT_TYPE_DISTANCE_OUTLINE:
  328. case FONT_TYPE_DISTANCE_OUTLINE_IMAGE:
  329. case FONT_TYPE_DISTANCE_DROP_SHADOW:
  330. case FONT_TYPE_DISTANCE_DROP_SHADOW_IMAGE:
  331. case FONT_TYPE_DISTANCE_OUTLINE_DROP_SHADOW_IMAGE:
  332. font.trueTypeFont->bakeGlyphDistance(_codePoint, glyphInfo, m_buffer);
  333. break;
  334. default:
  335. BX_ASSERT(false, "TextureType not supported yet");
  336. }
  337. if (!addBitmap(glyphInfo, m_buffer) )
  338. {
  339. return false;
  340. }
  341. glyphInfo.advance_x = (glyphInfo.advance_x * fontInfo.scale);
  342. glyphInfo.advance_y = (glyphInfo.advance_y * fontInfo.scale);
  343. glyphInfo.offset_x = (glyphInfo.offset_x * fontInfo.scale);
  344. glyphInfo.offset_y = (glyphInfo.offset_y * fontInfo.scale);
  345. glyphInfo.height = (glyphInfo.height * fontInfo.scale);
  346. glyphInfo.width = (glyphInfo.width * fontInfo.scale);
  347. font.cachedGlyphs[_codePoint] = glyphInfo;
  348. return true;
  349. }
  350. if (isValid(font.masterFontHandle)
  351. && preloadGlyph(font.masterFontHandle, _codePoint) )
  352. {
  353. const GlyphInfo* glyph = getGlyphInfo(font.masterFontHandle, _codePoint);
  354. GlyphInfo glyphInfo = *glyph;
  355. glyphInfo.advance_x = (glyphInfo.advance_x * fontInfo.scale);
  356. glyphInfo.advance_y = (glyphInfo.advance_y * fontInfo.scale);
  357. glyphInfo.offset_x = (glyphInfo.offset_x * fontInfo.scale);
  358. glyphInfo.offset_y = (glyphInfo.offset_y * fontInfo.scale);
  359. glyphInfo.height = (glyphInfo.height * fontInfo.scale);
  360. glyphInfo.width = (glyphInfo.width * fontInfo.scale);
  361. font.cachedGlyphs[_codePoint] = glyphInfo;
  362. return true;
  363. }
  364. return false;
  365. }
  366. bool FontManager::addGlyphBitmap(FontHandle _handle, CodePoint _codePoint, uint16_t _width, uint16_t _height, uint16_t _pitch, float extraScale, const uint8_t* _bitmapBuffer, float glyphOffsetX, float glyphOffsetY)
  367. {
  368. BX_ASSERT(isValid(_handle), "Invalid handle used");
  369. CachedFont& font = m_cachedFonts[_handle.idx];
  370. GlyphHashMap::iterator iter = font.cachedGlyphs.find(_codePoint);
  371. if (iter != font.cachedGlyphs.end() )
  372. {
  373. return true;
  374. }
  375. GlyphInfo glyphInfo;
  376. float glyphScale = extraScale;
  377. glyphInfo.offset_x = glyphOffsetX * glyphScale;
  378. glyphInfo.offset_y = glyphOffsetY * glyphScale;
  379. glyphInfo.width = (float)_width;
  380. glyphInfo.height = (float)_height;
  381. glyphInfo.advance_x = (float)_width * glyphScale;
  382. glyphInfo.advance_y = (float)_height * glyphScale;
  383. glyphInfo.bitmapScale = glyphScale;
  384. uint32_t dstPitch = _width * 4;
  385. uint8_t* dst = m_buffer;
  386. const uint8_t* src = _bitmapBuffer;
  387. uint32_t srcPitch = _pitch;
  388. for (int32_t ii = 0; ii < _height; ++ii)
  389. {
  390. bx::memCopy(dst, src, dstPitch);
  391. dst += dstPitch;
  392. src += srcPitch;
  393. }
  394. glyphInfo.regionIndex = m_atlas->addRegion(
  395. (uint16_t)bx::ceil(glyphInfo.width)
  396. , (uint16_t)bx::ceil(glyphInfo.height)
  397. , m_buffer
  398. , AtlasRegion::TYPE_BGRA8
  399. );
  400. font.cachedGlyphs[_codePoint] = glyphInfo;
  401. return true;
  402. }
  403. const FontInfo& FontManager::getFontInfo(FontHandle _handle) const
  404. {
  405. BX_ASSERT(isValid(_handle), "Invalid handle used");
  406. return m_cachedFonts[_handle.idx].fontInfo;
  407. }
  408. float FontManager::getKerning(FontHandle _handle, CodePoint _prevCodePoint, CodePoint _codePoint)
  409. {
  410. const CachedFont& cachedFont = m_cachedFonts[_handle.idx];
  411. if (isValid(cachedFont.masterFontHandle))
  412. {
  413. CachedFont& baseFont = m_cachedFonts[cachedFont.masterFontHandle.idx];
  414. return baseFont.trueTypeFont->m_scale
  415. * stbtt_GetCodepointKernAdvance(&baseFont.trueTypeFont->m_font, _prevCodePoint, _codePoint)
  416. * cachedFont.fontInfo.scale;
  417. }
  418. else
  419. {
  420. return cachedFont.trueTypeFont->m_scale * stbtt_GetCodepointKernAdvance(&cachedFont.trueTypeFont->m_font, _prevCodePoint, _codePoint);
  421. }
  422. }
  423. const GlyphInfo* FontManager::getGlyphInfo(FontHandle _handle, CodePoint _codePoint)
  424. {
  425. const GlyphHashMap& cachedGlyphs = m_cachedFonts[_handle.idx].cachedGlyphs;
  426. GlyphHashMap::const_iterator it = cachedGlyphs.find(_codePoint);
  427. if (it == cachedGlyphs.end() )
  428. {
  429. if (!preloadGlyph(_handle, _codePoint) )
  430. {
  431. return NULL;
  432. }
  433. it = cachedGlyphs.find(_codePoint);
  434. }
  435. BX_ASSERT(it != cachedGlyphs.end(), "Failed to preload glyph.");
  436. return &it->second;
  437. }
  438. bool FontManager::addBitmap(GlyphInfo& _glyphInfo, const uint8_t* _data)
  439. {
  440. _glyphInfo.regionIndex = m_atlas->addRegion(
  441. (uint16_t)bx::ceil(_glyphInfo.width)
  442. , (uint16_t)bx::ceil(_glyphInfo.height)
  443. , _data
  444. , AtlasRegion::TYPE_GRAY
  445. );
  446. return true;
  447. }