fontengine.h 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170
  1. /*
  2. Minetest
  3. Copyright (C) 2010-2014 sapier <sapier at gmx dot net>
  4. This program is free software; you can redistribute it and/or modify
  5. it under the terms of the GNU Lesser General Public License as published by
  6. the Free Software Foundation; either version 2.1 of the License, or
  7. (at your option) any later version.
  8. This program is distributed in the hope that it will be useful,
  9. but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. GNU Lesser General Public License for more details.
  12. You should have received a copy of the GNU Lesser General Public License along
  13. with this program; if not, write to the Free Software Foundation, Inc.,
  14. 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
  15. */
  16. #pragma once
  17. #include <map>
  18. #include "util/basic_macros.h"
  19. #include "irrlichttypes.h"
  20. #include <IGUIFont.h>
  21. #include <IGUISkin.h>
  22. #include <IGUIEnvironment.h>
  23. #include "settings.h"
  24. #include "threading/mutex_auto_lock.h"
  25. #define FONT_SIZE_UNSPECIFIED 0xFFFFFFFF
  26. enum FontMode : u8 {
  27. FM_Standard = 0,
  28. FM_Mono,
  29. _FM_Fallback, // do not use directly
  30. FM_MaxMode,
  31. FM_Unspecified
  32. };
  33. struct FontSpec {
  34. FontSpec(unsigned int font_size, FontMode mode, bool bold, bool italic) :
  35. size(font_size),
  36. mode(mode),
  37. bold(bold),
  38. italic(italic) {}
  39. u16 getHash() const
  40. {
  41. return (mode << 2) | (static_cast<u8>(bold) << 1) | static_cast<u8>(italic);
  42. }
  43. unsigned int size;
  44. FontMode mode;
  45. bool bold;
  46. bool italic;
  47. };
  48. class FontEngine
  49. {
  50. public:
  51. FontEngine(gui::IGUIEnvironment* env);
  52. ~FontEngine();
  53. // Get best possible font specified by FontSpec
  54. irr::gui::IGUIFont *getFont(FontSpec spec);
  55. irr::gui::IGUIFont *getFont(unsigned int font_size=FONT_SIZE_UNSPECIFIED,
  56. FontMode mode=FM_Unspecified)
  57. {
  58. FontSpec spec(font_size, mode, m_default_bold, m_default_italic);
  59. return getFont(spec);
  60. }
  61. /** get text height for a specific font */
  62. unsigned int getTextHeight(const FontSpec &spec);
  63. /** get text width if a text for a specific font */
  64. unsigned int getTextHeight(
  65. unsigned int font_size=FONT_SIZE_UNSPECIFIED,
  66. FontMode mode=FM_Unspecified)
  67. {
  68. FontSpec spec(font_size, mode, m_default_bold, m_default_italic);
  69. return getTextHeight(spec);
  70. }
  71. unsigned int getTextWidth(const std::wstring &text, const FontSpec &spec);
  72. /** get text width if a text for a specific font */
  73. unsigned int getTextWidth(const std::wstring& text,
  74. unsigned int font_size=FONT_SIZE_UNSPECIFIED,
  75. FontMode mode=FM_Unspecified)
  76. {
  77. FontSpec spec(font_size, mode, m_default_bold, m_default_italic);
  78. return getTextWidth(text, spec);
  79. }
  80. unsigned int getTextWidth(const std::string &text, const FontSpec &spec)
  81. {
  82. return getTextWidth(utf8_to_wide(text), spec);
  83. }
  84. unsigned int getTextWidth(const std::string& text,
  85. unsigned int font_size=FONT_SIZE_UNSPECIFIED,
  86. FontMode mode=FM_Unspecified)
  87. {
  88. FontSpec spec(font_size, mode, m_default_bold, m_default_italic);
  89. return getTextWidth(utf8_to_wide(text), spec);
  90. }
  91. /** get line height for a specific font (including empty room between lines) */
  92. unsigned int getLineHeight(const FontSpec &spec);
  93. unsigned int getLineHeight(unsigned int font_size=FONT_SIZE_UNSPECIFIED,
  94. FontMode mode=FM_Unspecified)
  95. {
  96. FontSpec spec(font_size, mode, m_default_bold, m_default_italic);
  97. return getLineHeight(spec);
  98. }
  99. /** get default font size */
  100. unsigned int getDefaultFontSize();
  101. /** get font size for a specific mode */
  102. unsigned int getFontSize(FontMode mode);
  103. /** update internal parameters from settings */
  104. void readSettings();
  105. private:
  106. irr::gui::IGUIFont *getFont(FontSpec spec, bool may_fail);
  107. /** update content of font cache in case of a setting change made it invalid */
  108. void updateFontCache();
  109. /** initialize a new TTF font */
  110. gui::IGUIFont *initFont(const FontSpec &spec);
  111. /** update current minetest skin with font changes */
  112. void updateSkin();
  113. /** clean cache */
  114. void cleanCache();
  115. /** pointer to irrlicht gui environment */
  116. gui::IGUIEnvironment* m_env = nullptr;
  117. /** mutex used to protect font init and cache */
  118. std::recursive_mutex m_font_mutex;
  119. /** internal storage for caching fonts of different size */
  120. std::map<unsigned int, irr::gui::IGUIFont*> m_font_cache[FM_MaxMode << 2];
  121. /** default font size to use */
  122. unsigned int m_default_size[FM_MaxMode];
  123. /** default bold and italic */
  124. bool m_default_bold = false;
  125. bool m_default_italic = false;
  126. /** default font engine mode (fixed) */
  127. static const FontMode m_currentMode = FM_Standard;
  128. DISABLE_CLASS_COPY(FontEngine);
  129. };
  130. /** interface to access main font engine*/
  131. extern FontEngine* g_fontengine;