SuperTuxKart
Loading...
Searching...
No Matches
font_manager.hpp
1//
2// SuperTuxKart - a fun racing game with go-kart
3// Copyright (C) 2016 SuperTuxKart-Team
4//
5// This program is free software; you can redistribute it and/or
6// modify it under the terms of the GNU General Public License
7// as published by the Free Software Foundation; either version 3
8// of the License, or (at your option) any later version.
9//
10// This program is distributed in the hope that it will be useful,
11// but WITHOUT ANY WARRANTY; without even the implied warranty of
12// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13// GNU General Public License for more details.
14//
15// You should have received a copy of the GNU General Public License
16// along with this program; if not, write to the Free Software
17// Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
18
19#ifndef HEADER_FONT_MANAGER_HPP
20#define HEADER_FONT_MANAGER_HPP
21
26#include "utils/leak_check.hpp"
27#include "utils/log.hpp"
28#include "utils/no_copy.hpp"
29
30#include <string>
31#include <map>
32#include <typeindex>
33#include <unordered_map>
34#include <vector>
35
36#ifndef SERVER_ONLY
37#include <ft2build.h>
38#include <harfbuzz/hb.h>
39#include FT_FREETYPE_H
40
41#include "irrString.h"
42#include "GlyphLayout.h"
43#endif
44
45class FontWithFace;
46
50class FontManager : public NoCopy
51{
52private:
54 std::vector<FontWithFace*> m_fonts;
55
56#ifndef SERVER_ONLY
58 FT_Library m_ft_library;
59
61 std::vector<FT_Face> m_faces;
62
64 FT_Face m_digit_face;
65
69 unsigned m_shaping_dpi;
70
72 std::map<FT_Face, uint16_t> m_ft_faces_to_index;
73
75 std::map<irr::core::stringw,
76 std::vector<irr::gui::GlyphLayout> > m_cached_gls;
77
78 bool m_has_color_emoji;
79 // ------------------------------------------------------------------------
80 std::vector<FT_Face> loadTTF(const std::vector<std::string>& ttf_list);
81
82 hb_buffer_t* m_hb_buffer;
83
84 std::vector<hb_font_t*> m_hb_fonts;
85#endif
86
89 std::unordered_map<std::type_index, int> m_font_type_map;
90public:
91 LEAK_CHECK()
92 // ------------------------------------------------------------------------
94 // ------------------------------------------------------------------------
95 ~FontManager();
96 // ------------------------------------------------------------------------
98 template <typename T> T* getFont()
99 {
100 T* out = NULL;
101 const unsigned int n = m_font_type_map[std::type_index(typeid(T))];
102 out = dynamic_cast<T*>(m_fonts[n]);
103 if (out != NULL)
104 {
105 return out;
106 }
107 Log::fatal("FontManager", "Can't get a font!");
108 return out;
109 }
110 // ------------------------------------------------------------------------
111#ifndef SERVER_ONLY
116 void checkFTError(FT_Error err, const std::string& desc) const
117 {
118 if (err > 0)
119 {
120 Log::error("FontManager", "Something wrong when %s! The error "
121 "code was %d.", desc.c_str(), err);
122 }
123 }
124 // ------------------------------------------------------------------------
125 bool hasColorEmoji() const { return m_has_color_emoji; }
126 // ------------------------------------------------------------------------
128 FT_Library getFTLibrary() const { return m_ft_library; }
129 // ------------------------------------------------------------------------
130 FT_Face loadColorEmoji();
131 // ------------------------------------------------------------------------
132 unsigned getShapingDPI() const { return m_shaping_dpi; }
133 // ------------------------------------------------------------------------
134 void shape(const std::u32string& text,
135 std::vector<irr::gui::GlyphLayout>& gls,
136 irr::u32 shape_flag = 0);
137 // ------------------------------------------------------------------------
138 std::vector<irr::gui::GlyphLayout>& getCachedLayouts
139 (const irr::core::stringw& str);
140 // ------------------------------------------------------------------------
141 void clearCachedLayouts() { m_cached_gls.clear(); }
142 // ------------------------------------------------------------------------
143 void initGlyphLayouts(const irr::core::stringw& text,
144 std::vector<irr::gui::GlyphLayout>& gls,
145 irr::u32 shape_flag = 0);
146#endif
147 // ------------------------------------------------------------------------
148 void loadFonts();
149 // ------------------------------------------------------------------------
150 void unitTesting();
151
152}; // FontManager
153
154extern FontManager *font_manager;
155
156#endif
157/* EOF */
This class stores all font files required in STK.
Definition: font_manager.hpp:51
T * getFont()
Return a specfic type of FontWithFace found in m_fonts.
Definition: font_manager.hpp:98
void checkFTError(FT_Error err, const std::string &desc) const
Check for any error discovered in a freetype function that will return a FT_Error value,...
Definition: font_manager.hpp:116
std::vector< FontWithFace * > m_fonts
Stores all FontWithFace used in STK.
Definition: font_manager.hpp:54
std::map< irr::core::stringw, std::vector< irr::gui::GlyphLayout > > m_cached_gls
Text drawn to glyph layouts cache.
Definition: font_manager.hpp:76
void unitTesting()
Unit testing that will try to load all translations in STK, and discover if there is any characters r...
Definition: font_manager.cpp:870
unsigned m_shaping_dpi
DPI used when shaping, each face will apply an inverse of this and the dpi of itself when rendering,...
Definition: font_manager.hpp:69
FT_Library m_ft_library
A FreeType library, it holds the FT_Face internally inside freetype.
Definition: font_manager.hpp:58
FT_Face m_digit_face
TTF file for digit font in STK.
Definition: font_manager.hpp:64
void initGlyphLayouts(const irr::core::stringw &text, std::vector< irr::gui::GlyphLayout > &gls, irr::u32 shape_flag=0)
Convert text to glyph layouts for fast rendering with (optional) caching enabled.
Definition: font_manager.cpp:688
std::map< FT_Face, uint16_t > m_ft_faces_to_index
Map FT_Face to index for quicker layout.
Definition: font_manager.hpp:72
FT_Library getFTLibrary() const
Return the m_ft_library.
Definition: font_manager.hpp:128
void loadFonts()
Initialize all FaceTTF and FontWithFace members.
Definition: font_manager.cpp:780
std::vector< FT_Face > m_faces
List of TTF files for complex text shaping.
Definition: font_manager.hpp:61
std::vector< FT_Face > loadTTF(const std::vector< std::string > &ttf_list)
Load all TTFs from a list to m_faces.
Definition: font_manager.cpp:90
std::unordered_map< std::type_index, int > m_font_type_map
Map type for each FontWithFace with a index, save getting time in getFont.
Definition: font_manager.hpp:89
An abstract class which contains functions which convert vector fonts into bitmap and render them in ...
Definition: font_with_face.hpp:72
Utility class, you can inherit from this class to disallow the assignment operator and copy construct...
Definition: no_copy.hpp:26