font_manager.h 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222
  1. /*
  2. * Copyright 2013 Jeremie Roy. All rights reserved.
  3. * License: https://github.com/bkaradzic/bgfx#license-bsd-2-clause
  4. */
  5. #ifndef FONT_MANAGER_H_HEADER_GUARD
  6. #define FONT_MANAGER_H_HEADER_GUARD
  7. #include <bx/handlealloc.h>
  8. #include <bx/string.h>
  9. #include <bgfx/bgfx.h>
  10. class Atlas;
  11. #define MAX_OPENED_FILES 64
  12. #define MAX_OPENED_FONT 64
  13. #define FONT_TYPE_ALPHA UINT32_C(0x00000100) // L8
  14. // #define FONT_TYPE_LCD UINT32_C(0x00000200) // BGRA8
  15. // #define FONT_TYPE_RGBA UINT32_C(0x00000300) // BGRA8
  16. #define FONT_TYPE_DISTANCE UINT32_C(0x00000400) // L8
  17. #define FONT_TYPE_DISTANCE_SUBPIXEL UINT32_C(0x00000500) // L8
  18. #define FONT_TYPE_DISTANCE_OUTLINE UINT32_C(0x00000600) // L8
  19. #define FONT_TYPE_DISTANCE_OUTLINE_IMAGE UINT32_C(0x00001600) // L8 + BGRA8
  20. #define FONT_TYPE_DISTANCE_DROP_SHADOW UINT32_C(0x00002700) // L8
  21. #define FONT_TYPE_DISTANCE_DROP_SHADOW_IMAGE UINT32_C(0x00003800) // L8 + BGRA8
  22. #define FONT_TYPE_DISTANCE_OUTLINE_DROP_SHADOW_IMAGE UINT32_C(0x00003900) // L8 + BGRA8
  23. #define FONT_TYPE_MASK_DISTANCE_IMAGE UINT32_C(0x00001000)
  24. #define FONT_TYPE_MASK_DISTANCE_DROP_SHADOW UINT32_C(0x00002000)
  25. struct FontInfo
  26. {
  27. /// The font height in pixel.
  28. uint16_t pixelSize;
  29. /// Rendering type used for the font.
  30. int16_t fontType;
  31. /// The pixel extents above the baseline in pixels (typically positive).
  32. float ascender;
  33. /// The extents below the baseline in pixels (typically negative).
  34. float descender;
  35. /// The spacing in pixels between one row's descent and the next row's ascent.
  36. float lineGap;
  37. /// This field gives the maximum horizontal cursor advance for all glyphs in the font.
  38. float maxAdvanceWidth;
  39. /// The thickness of the under/hover/strike-trough line in pixels.
  40. float underlineThickness;
  41. /// The position of the underline relatively to the baseline.
  42. float underlinePosition;
  43. /// Scale to apply to glyph data.
  44. float scale;
  45. };
  46. // Glyph metrics:
  47. // --------------
  48. //
  49. // xmin xmax
  50. // | |
  51. // |<-------- width -------->|
  52. // | |
  53. // | +-------------------------+----------------- ymax
  54. // | | ggggggggg ggggg | ^ ^
  55. // | | g:::::::::ggg::::g | | |
  56. // | | g:::::::::::::::::g | | |
  57. // | | g::::::ggggg::::::gg | | |
  58. // | | g:::::g g:::::g | | |
  59. // offset_x -|-------->| g:::::g g:::::g | offset_y |
  60. // | | g:::::g g:::::g | | |
  61. // | | g::::::g g:::::g | | |
  62. // | | g:::::::ggggg:::::g | | |
  63. // | | g::::::::::::::::g | | height
  64. // | | gg::::::::::::::g | | |
  65. // baseline ---*---------|---- gggggggg::::::g-----*-------- |
  66. // / | | g:::::g | |
  67. // origin | | gggggg g:::::g | |
  68. // | | g:::::gg gg:::::g | |
  69. // | | g::::::ggg:::::::g | |
  70. // | | gg:::::::::::::g | |
  71. // | | ggg::::::ggg | |
  72. // | | gggggg | v
  73. // | +-------------------------+----------------- ymin
  74. // | |
  75. // |------------- advance_x ---------->|
  76. /// Unicode value of a character
  77. typedef int32_t CodePoint;
  78. /// A structure that describe a glyph.
  79. struct GlyphInfo
  80. {
  81. /// Index for faster retrieval.
  82. int32_t glyphIndex;
  83. /// Glyph's width in pixels.
  84. float width;
  85. /// Glyph's height in pixels.
  86. float height;
  87. /// Glyph's left offset in pixels
  88. float offset_x;
  89. /// Glyph's top offset in pixels.
  90. ///
  91. /// @remark This is the distance from the baseline to the top-most glyph
  92. /// scan line, upwards y coordinates being positive.
  93. float offset_y;
  94. /// For horizontal text layouts, this is the unscaled horizontal
  95. /// distance in pixels used to increment the pen position when the
  96. /// glyph is drawn as part of a string of text.
  97. float advance_x;
  98. /// For vertical text layouts, this is the unscaled vertical distance
  99. /// in pixels used to increment the pen position when the glyph is
  100. /// drawn as part of a string of text.
  101. float advance_y;
  102. /// Amount to scale a bitmap image glyph.
  103. float bitmapScale;
  104. /// Region index in the atlas storing textures.
  105. uint16_t regionIndex;
  106. };
  107. BGFX_HANDLE(TrueTypeHandle)
  108. BGFX_HANDLE(FontHandle)
  109. class FontManager
  110. {
  111. public:
  112. /// Create the font manager using an external cube atlas (doesn't take
  113. /// ownership of the atlas).
  114. FontManager(Atlas* _atlas);
  115. /// Create the font manager and create the texture cube as BGRA8 with
  116. /// linear filtering.
  117. FontManager(uint16_t _textureSideWidth = 512);
  118. ~FontManager();
  119. /// Retrieve the atlas used by the font manager (e.g. to add stuff to it)
  120. const Atlas* getAtlas() const
  121. {
  122. return m_atlas;
  123. }
  124. /// Load a TrueType font from a given buffer. The buffer is copied and
  125. /// thus can be freed or reused after this call.
  126. ///
  127. /// @return invalid handle if the loading fail
  128. TrueTypeHandle createTtf(const uint8_t* _buffer, uint32_t _size);
  129. /// Unload a TrueType font (free font memory) but keep loaded glyphs.
  130. void destroyTtf(TrueTypeHandle _handle);
  131. /// Return a font whose height is a fixed pixel size.
  132. FontHandle createFontByPixelSize(TrueTypeHandle _handle, uint32_t _typefaceIndex, uint32_t _pixelSize, uint32_t _fontType = FONT_TYPE_ALPHA,
  133. uint16_t _glyphWidthPadding = 6, uint16_t glyphHeightPadding = 6);
  134. /// Return a scaled child font whose height is a fixed pixel size.
  135. FontHandle createScaledFontToPixelSize(FontHandle _baseFontHandle, uint32_t _pixelSize);
  136. /// destroy a font (truetype or baked)
  137. void destroyFont(FontHandle _handle);
  138. /// Preload a set of glyphs from a TrueType file.
  139. ///
  140. /// @return True if every glyph could be preloaded, false otherwise if
  141. /// the Font is a baked font, this only do validation on the characters.
  142. bool preloadGlyph(FontHandle _handle, const wchar_t* _string);
  143. /// Preload a single glyph, return true on success.
  144. bool preloadGlyph(FontHandle _handle, CodePoint _character);
  145. bool addGlyphBitmap(FontHandle _handle, CodePoint _character, uint16_t _width, uint16_t height, uint16_t _pitch, float extraScale, const uint8_t* _bitmapBuffer, float glyphOffsetX, float glyphOffsetY);
  146. /// Return the font descriptor of a font.
  147. ///
  148. /// @remark the handle is required to be valid
  149. const FontInfo& getFontInfo(FontHandle _handle) const;
  150. /// Return the rendering informations about the glyph region. Load the
  151. /// glyph from a TrueType font if possible
  152. ///
  153. const GlyphInfo* getGlyphInfo(FontHandle _handle, CodePoint _codePoint);
  154. float getKerning(FontHandle _handle, CodePoint _prevCodePoint, CodePoint _codePoint);
  155. const GlyphInfo& getBlackGlyph() const
  156. {
  157. return m_blackGlyph;
  158. }
  159. private:
  160. struct CachedFont;
  161. struct CachedFile
  162. {
  163. uint8_t* buffer;
  164. uint32_t bufferSize;
  165. };
  166. void init();
  167. bool addBitmap(GlyphInfo& _glyphInfo, const uint8_t* _data);
  168. bool m_ownAtlas;
  169. Atlas* m_atlas;
  170. bx::HandleAllocT<MAX_OPENED_FONT> m_fontHandles;
  171. CachedFont* m_cachedFonts;
  172. bx::HandleAllocT<MAX_OPENED_FILES> m_filesHandles;
  173. CachedFile* m_cachedFiles;
  174. GlyphInfo m_blackGlyph;
  175. //temporary buffer to raster glyph
  176. uint8_t* m_buffer;
  177. };
  178. #endif // FONT_MANAGER_H_HEADER_GUARD