font_manager.cpp 18 KB

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