2014-11-23 13:40:43 +01:00
|
|
|
/*
|
|
|
|
Minetest
|
|
|
|
Copyright (C) 2010-2014 sapier <sapier at gmx dot net>
|
|
|
|
|
|
|
|
This program is free software; you can redistribute it and/or modify
|
|
|
|
it under the terms of the GNU Lesser General Public License as published by
|
|
|
|
the Free Software Foundation; either version 2.1 of the License, or
|
|
|
|
(at your option) any later version.
|
|
|
|
|
|
|
|
This program is distributed in the hope that it will be useful,
|
|
|
|
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
GNU Lesser General Public License for more details.
|
|
|
|
|
|
|
|
You should have received a copy of the GNU Lesser General Public License along
|
|
|
|
with this program; if not, write to the Free Software Foundation, Inc.,
|
|
|
|
51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
|
|
|
|
*/
|
2017-08-17 22:19:39 +02:00
|
|
|
|
|
|
|
#pragma once
|
2014-11-23 13:40:43 +01:00
|
|
|
|
|
|
|
#include <map>
|
|
|
|
#include <vector>
|
2017-08-18 07:44:42 +02:00
|
|
|
#include "util/basic_macros.h"
|
2021-01-28 21:25:13 +01:00
|
|
|
#include "irrlichttypes.h"
|
2017-08-18 07:44:42 +02:00
|
|
|
#include <IGUIFont.h>
|
|
|
|
#include <IGUISkin.h>
|
|
|
|
#include <IGUIEnvironment.h>
|
2014-11-23 13:40:43 +01:00
|
|
|
#include "settings.h"
|
|
|
|
|
|
|
|
#define FONT_SIZE_UNSPECIFIED 0xFFFFFFFF
|
|
|
|
|
2019-10-17 20:40:50 +02:00
|
|
|
enum FontMode : u8 {
|
2014-11-23 13:40:43 +01:00
|
|
|
FM_Standard = 0,
|
|
|
|
FM_Mono,
|
|
|
|
FM_Fallback,
|
|
|
|
FM_Simple,
|
|
|
|
FM_SimpleMono,
|
|
|
|
FM_MaxMode,
|
|
|
|
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) {}
|
|
|
|
|
|
|
|
u16 getHash()
|
|
|
|
{
|
2020-08-23 15:41:04 +02:00
|
|
|
return (mode << 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;
|
|
|
|
};
|
|
|
|
|
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
|
|
|
|
|
|
|
/** get text width if 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
|
|
|
|
2014-11-23 13:40:43 +01:00
|
|
|
/** get text width if a text for a specific font */
|
|
|
|
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 09:48:50 +02: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();
|
|
|
|
|
|
|
|
private:
|
|
|
|
/** update content of font cache in case of a setting change made it invalid */
|
|
|
|
void updateFontCache();
|
|
|
|
|
|
|
|
/** initialize a new font */
|
2019-10-17 20:40:50 +02:00
|
|
|
gui::IGUIFont *initFont(const FontSpec &spec);
|
2014-11-23 13:40:43 +01:00
|
|
|
|
|
|
|
/** initialize a font without freetype */
|
2019-10-17 20:40:50 +02:00
|
|
|
gui::IGUIFont *initSimpleFont(const FontSpec &spec);
|
2014-11-23 13:40:43 +01:00
|
|
|
|
|
|
|
/** update current minetest skin with font changes */
|
|
|
|
void updateSkin();
|
|
|
|
|
|
|
|
/** clean cache */
|
|
|
|
void cleanCache();
|
|
|
|
|
|
|
|
/** 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
|
|
|
|
|
|
|
/** internal storage for caching fonts of different size */
|
2019-09-10 15:11:26 +02:00
|
|
|
std::map<unsigned int, irr::gui::IGUIFont*> m_font_cache[FM_MaxMode << 2];
|
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
|
|
|
|
2014-11-23 13:40:43 +01:00
|
|
|
/** current font engine mode */
|
2017-06-16 11:25:52 +02:00
|
|
|
FontMode m_currentMode = FM_Standard;
|
2014-11-23 13:40:43 +01:00
|
|
|
|
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;
|