2024-10-28 15:57:39 +01:00
|
|
|
// Luanti
|
|
|
|
// SPDX-License-Identifier: LGPL-2.1-or-later
|
|
|
|
// Copyright (C) 2010-2014 sapier <sapier at gmx dot net>
|
2017-08-17 22:19:39 +02:00
|
|
|
|
|
|
|
#pragma once
|
2014-11-23 13:40:43 +01:00
|
|
|
|
|
|
|
#include <map>
|
2025-01-19 20:42:40 +01:00
|
|
|
#include <unordered_map>
|
|
|
|
#include "irr_ptr.h"
|
|
|
|
#include "irrlicht_changes/CGUITTFont.h"
|
2017-08-18 07:44:42 +02:00
|
|
|
#include "util/basic_macros.h"
|
2021-01-28 23:25:13 +03:00
|
|
|
#include "irrlichttypes.h"
|
2024-10-12 23:04:31 +02:00
|
|
|
#include "irrString.h" // utf8_to_wide
|
2021-12-17 23:49:47 +01:00
|
|
|
#include "threading/mutex_auto_lock.h"
|
2014-11-23 13:40:43 +01:00
|
|
|
|
2024-10-12 23:04:31 +02:00
|
|
|
namespace irr {
|
|
|
|
namespace gui {
|
|
|
|
class IGUIEnvironment;
|
|
|
|
class IGUIFont;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-11-23 13:40:43 +01:00
|
|
|
#define FONT_SIZE_UNSPECIFIED 0xFFFFFFFF
|
|
|
|
|
2019-10-17 20:40:50 +02:00
|
|
|
enum FontMode : u8 {
|
2025-03-15 15:30:35 +01:00
|
|
|
/// Regular font (settings "font_path*", overwritable)
|
2014-11-23 13:40:43 +01:00
|
|
|
FM_Standard = 0,
|
2025-03-15 15:30:35 +01:00
|
|
|
|
|
|
|
/// Monospace font (settings "mono_font*", overwritable)
|
2014-11-23 13:40:43 +01:00
|
|
|
FM_Mono,
|
2025-03-15 15:30:35 +01:00
|
|
|
|
|
|
|
/// Use only in `FontEngine`. Fallback font to render glyphs that are not present
|
|
|
|
/// in the originally requested font (setting "fallback_font_path")
|
|
|
|
_FM_Fallback,
|
|
|
|
|
|
|
|
/// Sum of all font modes
|
2014-11-23 13:40:43 +01:00
|
|
|
FM_MaxMode,
|
2025-03-15 15:30:35 +01:00
|
|
|
|
|
|
|
// ----------------------------
|
|
|
|
|
|
|
|
/// Request the defult font specified by `s_default_font_mode`
|
2014-11-23 13:40:43 +01:00
|
|
|
FM_Unspecified
|
|
|
|
};
|
|
|
|
|
2019-10-17 20:40:50 +02:00
|
|
|
struct FontSpec {
|
|
|
|
FontSpec(unsigned int font_size, FontMode mode, bool bold, bool italic) :
|
|
|
|
size(font_size),
|
|
|
|
mode(mode),
|
|
|
|
bold(bold),
|
|
|
|
italic(italic) {}
|
|
|
|
|
2025-03-15 15:30:35 +01:00
|
|
|
static const unsigned VARIANT_BITS = 3;
|
|
|
|
static const size_t MAX_VARIANTS = FM_MaxMode << VARIANT_BITS;
|
|
|
|
|
2021-03-29 19:55:24 +02:00
|
|
|
u16 getHash() const
|
2019-10-17 20:40:50 +02:00
|
|
|
{
|
2025-03-15 15:30:35 +01:00
|
|
|
return (mode << VARIANT_BITS)
|
|
|
|
| (static_cast<u8>(allow_server_media) << 2)
|
|
|
|
| (static_cast<u8>(bold) << 1)
|
|
|
|
| static_cast<u8>(italic);
|
2019-10-17 20:40:50 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
unsigned int size;
|
|
|
|
FontMode mode;
|
|
|
|
bool bold;
|
|
|
|
bool italic;
|
2025-03-15 15:30:35 +01:00
|
|
|
bool allow_server_media = true;
|
2019-10-17 20:40:50 +02:00
|
|
|
};
|
|
|
|
|
2014-11-23 13:40:43 +01:00
|
|
|
class FontEngine
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
|
2020-11-29 17:56:25 +01:00
|
|
|
FontEngine(gui::IGUIEnvironment* env);
|
2014-11-23 13:40:43 +01:00
|
|
|
|
|
|
|
~FontEngine();
|
|
|
|
|
2019-10-17 20:40:50 +02:00
|
|
|
// Get best possible font specified by FontSpec
|
|
|
|
irr::gui::IGUIFont *getFont(FontSpec spec);
|
2019-09-10 15:11:26 +02:00
|
|
|
|
|
|
|
irr::gui::IGUIFont *getFont(unsigned int font_size=FONT_SIZE_UNSPECIFIED,
|
|
|
|
FontMode mode=FM_Unspecified)
|
|
|
|
{
|
2019-10-17 20:40:50 +02:00
|
|
|
FontSpec spec(font_size, mode, m_default_bold, m_default_italic);
|
|
|
|
return getFont(spec);
|
2019-09-10 15:11:26 +02:00
|
|
|
}
|
2014-11-23 13:40:43 +01:00
|
|
|
|
|
|
|
/** get text height for a specific font */
|
2019-10-17 20:40:50 +02:00
|
|
|
unsigned int getTextHeight(const FontSpec &spec);
|
2014-11-23 13:40:43 +01:00
|
|
|
|
2025-01-19 20:42:40 +01:00
|
|
|
/** get text width of a text for a specific font */
|
2019-09-10 15:11:26 +02:00
|
|
|
unsigned int getTextHeight(
|
2014-11-23 13:40:43 +01:00
|
|
|
unsigned int font_size=FONT_SIZE_UNSPECIFIED,
|
|
|
|
FontMode mode=FM_Unspecified)
|
|
|
|
{
|
2019-10-17 20:40:50 +02:00
|
|
|
FontSpec spec(font_size, mode, m_default_bold, m_default_italic);
|
|
|
|
return getTextHeight(spec);
|
2014-11-23 13:40:43 +01:00
|
|
|
}
|
|
|
|
|
2019-10-17 20:40:50 +02:00
|
|
|
unsigned int getTextWidth(const std::wstring &text, const FontSpec &spec);
|
2019-09-10 15:11:26 +02:00
|
|
|
|
2025-01-19 20:42:40 +01:00
|
|
|
/** get text width of a text for a specific font */
|
2014-11-23 13:40:43 +01:00
|
|
|
unsigned int getTextWidth(const std::wstring& text,
|
|
|
|
unsigned int font_size=FONT_SIZE_UNSPECIFIED,
|
2019-09-10 15:11:26 +02:00
|
|
|
FontMode mode=FM_Unspecified)
|
|
|
|
{
|
2019-10-17 20:40:50 +02:00
|
|
|
FontSpec spec(font_size, mode, m_default_bold, m_default_italic);
|
|
|
|
return getTextWidth(text, spec);
|
2019-09-10 15:11:26 +02:00
|
|
|
}
|
|
|
|
|
2019-10-17 20:40:50 +02:00
|
|
|
unsigned int getTextWidth(const std::string &text, const FontSpec &spec)
|
2019-09-10 15:11:26 +02:00
|
|
|
{
|
2019-10-17 20:40:50 +02:00
|
|
|
return getTextWidth(utf8_to_wide(text), spec);
|
2019-09-10 15:11:26 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
unsigned int getTextWidth(const std::string& text,
|
|
|
|
unsigned int font_size=FONT_SIZE_UNSPECIFIED,
|
|
|
|
FontMode mode=FM_Unspecified)
|
|
|
|
{
|
2019-10-17 20:40:50 +02:00
|
|
|
FontSpec spec(font_size, mode, m_default_bold, m_default_italic);
|
|
|
|
return getTextWidth(utf8_to_wide(text), spec);
|
2019-09-10 15:11:26 +02:00
|
|
|
}
|
2014-11-23 13:40:43 +01:00
|
|
|
|
|
|
|
/** get line height for a specific font (including empty room between lines) */
|
2019-10-17 20:40:50 +02:00
|
|
|
unsigned int getLineHeight(const FontSpec &spec);
|
2019-09-10 15:11:26 +02:00
|
|
|
|
2014-11-23 13:40:43 +01:00
|
|
|
unsigned int getLineHeight(unsigned int font_size=FONT_SIZE_UNSPECIFIED,
|
2019-09-10 15:11:26 +02:00
|
|
|
FontMode mode=FM_Unspecified)
|
|
|
|
{
|
2019-10-17 20:40:50 +02:00
|
|
|
FontSpec spec(font_size, mode, m_default_bold, m_default_italic);
|
|
|
|
return getLineHeight(spec);
|
2019-09-10 15:11:26 +02:00
|
|
|
}
|
2014-11-23 13:40:43 +01:00
|
|
|
|
|
|
|
/** get default font size */
|
|
|
|
unsigned int getDefaultFontSize();
|
|
|
|
|
2020-07-12 00:48:50 -07:00
|
|
|
/** get font size for a specific mode */
|
|
|
|
unsigned int getFontSize(FontMode mode);
|
|
|
|
|
2014-11-23 13:40:43 +01:00
|
|
|
/** update internal parameters from settings */
|
|
|
|
void readSettings();
|
|
|
|
|
2025-03-16 17:55:39 +01:00
|
|
|
/** reload fonts if settings were changed */
|
|
|
|
void handleReload();
|
|
|
|
|
2025-01-19 20:42:40 +01:00
|
|
|
void setMediaFont(const std::string &name, const std::string &data);
|
|
|
|
|
|
|
|
void clearMediaFonts();
|
|
|
|
|
2014-11-23 13:40:43 +01:00
|
|
|
private:
|
2021-03-29 19:55:24 +02:00
|
|
|
irr::gui::IGUIFont *getFont(FontSpec spec, bool may_fail);
|
|
|
|
|
2014-11-23 13:40:43 +01:00
|
|
|
/** update content of font cache in case of a setting change made it invalid */
|
2025-01-19 20:42:40 +01:00
|
|
|
void updateCache();
|
2014-11-23 13:40:43 +01:00
|
|
|
|
2021-03-29 19:55:24 +02:00
|
|
|
/** initialize a new TTF font */
|
2025-03-15 15:30:35 +01:00
|
|
|
gui::IGUIFont *initFont(FontSpec spec);
|
2014-11-23 13:40:43 +01:00
|
|
|
|
|
|
|
/** update current minetest skin with font changes */
|
|
|
|
void updateSkin();
|
|
|
|
|
2025-01-19 20:42:40 +01:00
|
|
|
void clearCache();
|
|
|
|
|
|
|
|
/** refresh after fonts have been changed */
|
|
|
|
void refresh();
|
2014-11-23 13:40:43 +01:00
|
|
|
|
2025-03-16 17:55:39 +01:00
|
|
|
/** callback to be used on change of font size setting */
|
|
|
|
static void fontSettingChanged(const std::string &name, void *userdata);
|
|
|
|
|
2014-11-23 13:40:43 +01:00
|
|
|
/** pointer to irrlicht gui environment */
|
2017-08-18 07:44:42 +02:00
|
|
|
gui::IGUIEnvironment* m_env = nullptr;
|
2014-11-23 13:40:43 +01:00
|
|
|
|
2021-12-17 23:49:47 +01:00
|
|
|
/** mutex used to protect font init and cache */
|
|
|
|
std::recursive_mutex m_font_mutex;
|
|
|
|
|
2014-11-23 13:40:43 +01:00
|
|
|
/** internal storage for caching fonts of different size */
|
2025-03-15 15:30:35 +01:00
|
|
|
std::map<unsigned int, irr::gui::IGUIFont*> m_font_cache[FontSpec::MAX_VARIANTS];
|
2014-11-23 13:40:43 +01:00
|
|
|
|
2025-01-19 20:42:40 +01:00
|
|
|
/** media-provided faces, indexed by filename (without extension) */
|
|
|
|
std::unordered_map<std::string, irr_ptr<gui::SGUITTFace>> m_media_faces;
|
|
|
|
|
2014-11-23 13:40:43 +01:00
|
|
|
/** default font size to use */
|
|
|
|
unsigned int m_default_size[FM_MaxMode];
|
|
|
|
|
2019-09-10 15:11:26 +02:00
|
|
|
/** default bold and italic */
|
2019-10-17 20:40:50 +02:00
|
|
|
bool m_default_bold = false;
|
|
|
|
bool m_default_italic = false;
|
2019-09-10 15:11:26 +02:00
|
|
|
|
2022-01-08 14:53:25 +01:00
|
|
|
/** default font engine mode (fixed) */
|
2025-03-15 15:30:35 +01:00
|
|
|
static const FontMode s_default_font_mode = FM_Standard;
|
2014-11-23 13:40:43 +01:00
|
|
|
|
2025-03-16 17:55:39 +01:00
|
|
|
bool m_needs_reload = false;
|
|
|
|
|
2017-08-18 07:44:42 +02:00
|
|
|
DISABLE_CLASS_COPY(FontEngine);
|
2014-11-23 13:40:43 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
/** interface to access main font engine*/
|
2014-11-28 20:06:34 +01:00
|
|
|
extern FontEngine* g_fontengine;
|