font_manager.h 6.7 KB

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