fontengine.h 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160
  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 <vector>
  19. #include "util/basic_macros.h"
  20. #include <IGUIFont.h>
  21. #include <IGUISkin.h>
  22. #include <IGUIEnvironment.h>
  23. #include "settings.h"
  24. #define FONT_SIZE_UNSPECIFIED 0xFFFFFFFF
  25. enum FontMode {
  26. FM_Standard = 0,
  27. FM_Mono,
  28. FM_Fallback,
  29. FM_Simple,
  30. FM_SimpleMono,
  31. FM_MaxMode,
  32. FM_Unspecified
  33. };
  34. class FontEngine
  35. {
  36. public:
  37. FontEngine(Settings* main_settings, gui::IGUIEnvironment* env);
  38. ~FontEngine();
  39. /** get Font */
  40. irr::gui::IGUIFont *getFont(unsigned int font_size, FontMode mode,
  41. bool bold, bool italic);
  42. irr::gui::IGUIFont *getFont(unsigned int font_size=FONT_SIZE_UNSPECIFIED,
  43. FontMode mode=FM_Unspecified)
  44. {
  45. return getFont(font_size, mode, m_default_bold, m_default_italic);
  46. }
  47. /** get text height for a specific font */
  48. unsigned int getTextHeight(unsigned int font_size, FontMode mode,
  49. bool bold, bool italic);
  50. /** get text width if a text for a specific font */
  51. unsigned int getTextHeight(
  52. unsigned int font_size=FONT_SIZE_UNSPECIFIED,
  53. FontMode mode=FM_Unspecified)
  54. {
  55. return getTextHeight(font_size, mode, m_default_bold, m_default_italic);
  56. }
  57. unsigned int getTextWidth(const std::wstring& text,
  58. unsigned int font_size, FontMode mode, bool bold, bool italic);
  59. /** get text width if a text for a specific font */
  60. unsigned int getTextWidth(const std::wstring& text,
  61. unsigned int font_size=FONT_SIZE_UNSPECIFIED,
  62. FontMode mode=FM_Unspecified)
  63. {
  64. return getTextWidth(text, font_size, mode, m_default_bold,
  65. m_default_italic);
  66. }
  67. unsigned int getTextWidth(const std::string& text,
  68. unsigned int font_size, FontMode mode, bool bold, bool italic)
  69. {
  70. return getTextWidth(utf8_to_wide(text), font_size, mode, bold, italic);
  71. }
  72. unsigned int getTextWidth(const std::string& text,
  73. unsigned int font_size=FONT_SIZE_UNSPECIFIED,
  74. FontMode mode=FM_Unspecified)
  75. {
  76. return getTextWidth(utf8_to_wide(text), font_size, mode, m_default_bold,
  77. m_default_italic);
  78. }
  79. /** get line height for a specific font (including empty room between lines) */
  80. unsigned int getLineHeight(unsigned int font_size, FontMode mode, bool bold,
  81. bool italic);
  82. unsigned int getLineHeight(unsigned int font_size=FONT_SIZE_UNSPECIFIED,
  83. FontMode mode=FM_Unspecified)
  84. {
  85. return getLineHeight(font_size, mode, m_default_bold, m_default_italic);
  86. }
  87. /** get default font size */
  88. unsigned int getDefaultFontSize();
  89. /** initialize font engine */
  90. void initialize(Settings* main_settings, gui::IGUIEnvironment* env);
  91. /** update internal parameters from settings */
  92. void readSettings();
  93. private:
  94. /** update content of font cache in case of a setting change made it invalid */
  95. void updateFontCache();
  96. /** initialize a new font */
  97. void initFont(
  98. unsigned int basesize,
  99. FontMode mode,
  100. bool bold,
  101. bool italic);
  102. /** initialize a font without freetype */
  103. void initSimpleFont(unsigned int basesize, FontMode mode);
  104. /** update current minetest skin with font changes */
  105. void updateSkin();
  106. /** clean cache */
  107. void cleanCache();
  108. /** pointer to settings for registering callbacks or reading config */
  109. Settings* m_settings = nullptr;
  110. /** pointer to irrlicht gui environment */
  111. gui::IGUIEnvironment* m_env = nullptr;
  112. /** internal storage for caching fonts of different size */
  113. std::map<unsigned int, irr::gui::IGUIFont*> m_font_cache[FM_MaxMode << 2];
  114. /** default font size to use */
  115. unsigned int m_default_size[FM_MaxMode];
  116. /** default bold and italic */
  117. bool m_default_bold;
  118. bool m_default_italic;
  119. /** current font engine mode */
  120. FontMode m_currentMode = FM_Standard;
  121. DISABLE_CLASS_COPY(FontEngine);
  122. };
  123. /** interface to access main font engine*/
  124. extern FontEngine* g_fontengine;