GameKit  0.0.1a
C++ gamedev tools
Font.hpp
Go to the documentation of this file.
1 /*
2  * =====================================================================================
3  *
4  * Filename: Font.hpp
5  *
6  * Description:
7  *
8  * Created: 30/12/2018 18:54:10
9  *
10  * Author: Quentin Bazin, <quent42340@gmail.com>
11  *
12  * =====================================================================================
13  */
14 #ifndef GK_FONT_HPP_
15 #define GK_FONT_HPP_
16 
17 #include <memory>
18 #include <string>
19 #include <unordered_map>
20 
21 #include "gk/core/SDLHeaders.hpp"
22 #include "gk/utils/NonCopyable.hpp"
23 
24 namespace gk {
25 
30 class Font : public NonCopyable {
31  public:
38  Font() = default;
39 
46  Font(const std::string &filename) : m_filename(filename) {}
47 
54  void loadFromFile(const std::string &filename) { m_filename = filename; }
55 
62  void setFontKerning(int kerning) { m_kerning = kerning; update(); }
63 
70  void setFontHinting(int hinting) { m_hinting = hinting; update(); }
71 
78  void setFontOutline(int outline) { m_outline = outline; update(); }
79 
86  void loadFont(int ptsize) const;
87 
94  TTF_Font *getFont(int ptsize) const;
95 
96  private:
101  void update();
102 
103  using TTF_FontPtr = std::unique_ptr<TTF_Font, decltype(&TTF_CloseFont)>;
104 
106  // Member data
108  std::string m_filename;
109 
110  int m_kerning = 1;
111  int m_hinting = TTF_HINTING_NORMAL;
112  int m_outline = 0;
113 
114  mutable std::unordered_map<int, TTF_FontPtr> m_fonts;
115 };
116 
117 } // namespace gk
118 
119 #endif // GK_FONT_HPP_
120 
void update()
Update kerning, hinting and outline of all font sizes.
Definition: Font.cpp:42
void loadFromFile(const std::string &filename)
Load the font from a file.
Definition: Font.hpp:54
void loadFont(int ptsize) const
Load a new font size into this font.
Definition: Font.cpp:19
void setFontHinting(int hinting)
Set font hinting.
Definition: Font.hpp:70
std::unordered_map< int, TTF_FontPtr > m_fonts
SDL font container.
Definition: Font.hpp:114
Font(const std::string &filename)
Construct the font from a file.
Definition: Font.hpp:46
std::string m_filename
Font filename.
Definition: Font.hpp:108
int m_hinting
Hinting value.
Definition: Font.hpp:111
Font()=default
Default constructor.
int m_kerning
Kerning value.
Definition: Font.hpp:110
TTF_Font * getFont(int ptsize) const
Get the SDL font for a specific font size.
Definition: Font.cpp:34
void setFontOutline(int outline)
Set font outline.
Definition: Font.hpp:78
std::unique_ptr< TTF_Font, decltype(&TTF_CloseFont)> TTF_FontPtr
Definition: Font.hpp:103
void setFontKerning(int kerning)
Set font kerning.
Definition: Font.hpp:62
Class for loading and manipulating character fonts.
Definition: Font.hpp:30
int m_outline
Outline value.
Definition: Font.hpp:112