]> git.lizzy.rs Git - dragonfireclient.git/blobdiff - src/client/fontengine.h
Improved X-Ray, added AutoEject
[dragonfireclient.git] / src / client / fontengine.h
index a75618f8600f296807813b69611fce752b2cc2c8..865b2d3ffcb1346a37e4be3d5bb7d7f6eeb29d79 100644 (file)
@@ -29,7 +29,7 @@ with this program; if not, write to the Free Software Foundation, Inc.,
 
 #define FONT_SIZE_UNSPECIFIED 0xFFFFFFFF
 
-enum FontMode {
+enum FontMode : u8 {
        FM_Standard = 0,
        FM_Mono,
        FM_Fallback,
@@ -39,6 +39,24 @@ enum FontMode {
        FM_Unspecified
 };
 
+struct FontSpec {
+       FontSpec(unsigned int font_size, FontMode mode, bool bold, bool italic) :
+               size(font_size),
+               mode(mode),
+               bold(bold),
+               italic(italic) {}
+
+       u16 getHash()
+       {
+               return (mode << 2) | (bold << 1) | italic;
+       }
+
+       unsigned int size;
+       FontMode mode;
+       bool bold;
+       bool italic;
+};
+
 class FontEngine
 {
 public:
@@ -47,34 +65,68 @@ class FontEngine
 
        ~FontEngine();
 
-       /** get Font */
-       irr::gui::IGUIFont* getFont(unsigned int font_size=FONT_SIZE_UNSPECIFIED,
-                       FontMode mode=FM_Unspecified);
+       // Get best possible font specified by FontSpec
+       irr::gui::IGUIFont *getFont(FontSpec spec);
+
+       irr::gui::IGUIFont *getFont(unsigned int font_size=FONT_SIZE_UNSPECIFIED,
+                       FontMode mode=FM_Unspecified)
+       {
+               FontSpec spec(font_size, mode, m_default_bold, m_default_italic);
+               return getFont(spec);
+       }
 
        /** get text height for a specific font */
-       unsigned int getTextHeight(unsigned int font_size=FONT_SIZE_UNSPECIFIED,
-                       FontMode mode=FM_Unspecified);
+       unsigned int getTextHeight(const FontSpec &spec);
 
        /** get text width if a text for a specific font */
-       unsigned int getTextWidth(const std::string& text,
+       unsigned int getTextHeight(
                        unsigned int font_size=FONT_SIZE_UNSPECIFIED,
                        FontMode mode=FM_Unspecified)
        {
-               return getTextWidth(utf8_to_wide(text));
+               FontSpec spec(font_size, mode, m_default_bold, m_default_italic);
+               return getTextHeight(spec);
        }
 
+       unsigned int getTextWidth(const std::wstring &text, const FontSpec &spec);
+
        /** get text width if a text for a specific font */
        unsigned int getTextWidth(const std::wstring& text,
                        unsigned int font_size=FONT_SIZE_UNSPECIFIED,
-                       FontMode mode=FM_Unspecified);
+                       FontMode mode=FM_Unspecified)
+       {
+               FontSpec spec(font_size, mode, m_default_bold, m_default_italic);
+               return getTextWidth(text, spec);
+       }
+
+       unsigned int getTextWidth(const std::string &text, const FontSpec &spec)
+       {
+               return getTextWidth(utf8_to_wide(text), spec);
+       }
+
+       unsigned int getTextWidth(const std::string& text,
+                       unsigned int font_size=FONT_SIZE_UNSPECIFIED,
+                       FontMode mode=FM_Unspecified)
+       {
+               FontSpec spec(font_size, mode, m_default_bold, m_default_italic);
+               return getTextWidth(utf8_to_wide(text), spec);
+       }
 
        /** get line height for a specific font (including empty room between lines) */
+       unsigned int getLineHeight(const FontSpec &spec);
+
        unsigned int getLineHeight(unsigned int font_size=FONT_SIZE_UNSPECIFIED,
-                       FontMode mode=FM_Unspecified);
+                       FontMode mode=FM_Unspecified)
+       {
+               FontSpec spec(font_size, mode, m_default_bold, m_default_italic);
+               return getLineHeight(spec);
+       }
 
        /** get default font size */
        unsigned int getDefaultFontSize();
 
+       /** get font size for a specific mode */
+       unsigned int getFontSize(FontMode mode);
+
        /** initialize font engine */
        void initialize(Settings* main_settings, gui::IGUIEnvironment* env);
 
@@ -86,10 +138,10 @@ class FontEngine
        void updateFontCache();
 
        /** initialize a new font */
-       void initFont(unsigned int basesize, FontMode mode=FM_Unspecified);
+       gui::IGUIFont *initFont(const FontSpec &spec);
 
        /** initialize a font without freetype */
-       void initSimpleFont(unsigned int basesize, FontMode mode);
+       gui::IGUIFont *initSimpleFont(const FontSpec &spec);
 
        /** update current minetest skin with font changes */
        void updateSkin();
@@ -104,23 +156,18 @@ class FontEngine
        gui::IGUIEnvironment* m_env = nullptr;
 
        /** internal storage for caching fonts of different size */
-       std::map<unsigned int, irr::gui::IGUIFont*> m_font_cache[FM_MaxMode];
+       std::map<unsigned int, irr::gui::IGUIFont*> m_font_cache[FM_MaxMode << 2];
 
        /** default font size to use */
        unsigned int m_default_size[FM_MaxMode];
 
+       /** default bold and italic */
+       bool m_default_bold = false;
+       bool m_default_italic = false;
+
        /** current font engine mode */
        FontMode m_currentMode = FM_Standard;
 
-       /** font mode of last request */
-       FontMode m_lastMode;
-
-       /** size of last request */
-       unsigned int m_lastSize = 0;
-
-       /** last font returned */
-       irr::gui::IGUIFont* m_lastFont = nullptr;
-
        DISABLE_CLASS_COPY(FontEngine);
 };