raylib-cpp
C++ object-oriented wrapper library for raylib.
Font.hpp
1 #ifndef RAYLIB_CPP_INCLUDE_FONT_HPP_
2 #define RAYLIB_CPP_INCLUDE_FONT_HPP_
3 
4 #include <string>
5 
6 #include "./raylib.hpp"
7 #include "./raylib-cpp-utils.hpp"
8 #include "./RaylibException.hpp"
9 
10 namespace raylib {
14 class Font : public ::Font {
15  public:
16  Font(int baseSize,
17  int glyphCount,
18  int glyphPadding,
19  ::Texture2D texture,
20  ::Rectangle *recs = nullptr,
21  ::GlyphInfo *glyphs = nullptr) : ::Font{baseSize, glyphCount, glyphPadding, texture, recs, glyphs} {
22  // Nothing.
23  }
24 
25  Font() {
26  set(::GetFontDefault());
27  }
28 
29  Font(const ::Font& font) {
30  set(font);
31  }
32 
40  Font(const std::string& fileName) {
41  if (!Load(fileName)) {
42  throw RaylibException("Failed to load Font from file");
43  }
44  }
45 
55  Font(const std::string& fileName, int fontSize, int* fontChars, int charCount) {
56  if (!Load(fileName, fontSize, fontChars, charCount)) {
57  throw RaylibException("Failed to load font from font with extras");
58  }
59  }
60 
70  Font(const ::Image& image, ::Color key, int firstChar) {
71  if (!Load(image, key, firstChar)) {
72  throw RaylibException("Failed to load Texture from Image");
73  }
74  }
75 
83  Font(const std::string& fileType, const unsigned char* fileData, int dataSize, int fontSize,
84  int *fontChars, int charsCount) {
85  if (!Load(fileType, fileData, dataSize, fontSize, fontChars, charsCount)) {
86  throw RaylibException("Failed to load Texture from file data");
87  }
88  }
89 
90  Font(const Font&) = delete;
91 
92  Font(Font&& other) {
93  set(other);
94 
95  other.baseSize = 0;
96  other.glyphCount = 0;
97  other.glyphPadding = 0;
98  other.texture = {};
99  other.recs = nullptr;
100  other.glyphs = nullptr;
101  }
102 
103  ~Font() {
104  Unload();
105  }
106 
107  void Unload() {
108  UnloadFont(*this);
109  }
110 
111  GETTERSETTER(int, BaseSize, baseSize)
112  GETTERSETTER(int, GlyphCount, glyphCount)
113  GETTERSETTER(int, GlyphPadding, glyphPadding)
114  GETTERSETTER(::Texture2D, Texture, texture)
115  GETTERSETTER(::Rectangle*, Recs, recs)
116  GETTERSETTER(::GlyphInfo*, Glyphs, glyphs)
117 
118  Font& operator=(const ::Font& font) {
119  set(font);
120  return *this;
121  }
122 
123  Font& operator=(const Font&) = delete;
124 
125  Font& operator=(Font&& other) {
126  if (this == &other) {
127  return *this;
128  }
129 
130  Unload();
131  set(other);
132 
133  other.baseSize = 0;
134  other.glyphCount = 0;
135  other.glyphPadding = 0;
136  other.texture = {};
137  other.recs = nullptr;
138  other.glyphs = nullptr;
139 
140  return *this;
141  }
142 
152  bool Load(const std::string& fileName) {
153  set(::LoadFont(fileName.c_str()));
154  return baseSize > 0;
155  }
156 
167  bool Load(const std::string& fileName, int fontSize, int* fontChars, int charCount) {
168  set(::LoadFontEx(fileName.c_str(), fontSize, fontChars, charCount));
169  return baseSize > 0;
170  }
171 
172  bool Load(const ::Image& image, ::Color key, int firstChar) {
173  set(::LoadFontFromImage(image, key, firstChar));
174  return baseSize > 0;
175  }
176 
177  bool Load(const std::string& fileType, const unsigned char* fileData, int dataSize, int fontSize,
178  int *fontChars, int charsCount) {
179  set(::LoadFontFromMemory(fileType.c_str(), fileData, dataSize, fontSize, fontChars,
180  charsCount));
181  return baseSize > 0;
182  }
183 
187  inline Font& DrawText(const std::string& text, ::Vector2 position, float fontSize,
188  float spacing, ::Color tint = WHITE) {
189  ::DrawTextEx(*this, text.c_str(), position, fontSize, spacing, tint);
190  return *this;
191  }
192 
193  inline Font& DrawText(
194  const std::string& text,
195  ::Vector2 position,
196  ::Vector2 origin,
197  float rotation,
198  float fontSize,
199  float spacing,
200  ::Color tint = WHITE) {
201  ::DrawTextPro(*this, text.c_str(), position, origin, rotation, fontSize, spacing, tint);
202  return *this;
203  }
204 
208  inline Font& DrawText(int codepoint,
209  ::Vector2 position,
210  float fontSize,
211  ::Color tint = { 255, 255, 255, 255 }) {
212  ::DrawTextCodepoint(*this, codepoint, position, fontSize, tint);
213  return *this;
214  }
215 
219  inline Vector2 MeasureText(const std::string& text, float fontSize, float spacing) const {
220  return ::MeasureTextEx(*this, text.c_str(), fontSize, spacing);
221  }
222 
226  inline int GetGlyphIndex(int character) const {
227  return ::GetGlyphIndex(*this, character);
228  }
229 
233  inline ::Image ImageText(const std::string& text, float fontSize,
234  float spacing, ::Color tint) const {
235  return ::ImageTextEx(*this, text.c_str(), fontSize, spacing, tint);
236  }
237 
238  private:
239  void set(const ::Font& font) {
240  baseSize = font.baseSize;
241  glyphCount = font.glyphCount;
242  glyphPadding = font.glyphPadding;
243  texture = font.texture;
244  recs = font.recs;
245  glyphs = font.glyphs;
246  }
247 };
248 } // namespace raylib
249 
250 #endif // RAYLIB_CPP_INCLUDE_FONT_HPP_
raylib::Font::Font
Font(const std::string &fileName)
Loads a Font from the given file.
Definition: Font.hpp:40
raylib
All raylib-cpp classes and functions appear in the raylib namespace.
Definition: AudioDevice.hpp:8
raylib::Font::DrawText
Font & DrawText(const std::string &text, ::Vector2 position, float fontSize, float spacing, ::Color tint=WHITE)
Draw text using font and additional parameters.
Definition: Font.hpp:187
raylib::Font::Font
Font(const ::Image &image, ::Color key, int firstChar)
Loads a Font from the given image with a color key.
Definition: Font.hpp:70
raylib::Font::DrawText
Font & DrawText(int codepoint, ::Vector2 position, float fontSize, ::Color tint={ 255, 255, 255, 255 })
Draw one character (codepoint)
Definition: Font.hpp:208
raylib::Font::Font
Font(const std::string &fileType, const unsigned char *fileData, int dataSize, int fontSize, int *fontChars, int charsCount)
Loads a font from memory, based on the given file type and file data.
Definition: Font.hpp:83
raylib::DrawTextPro
static void DrawTextPro(const Font &font, const std::string &text, Vector2 position, Vector2 origin, float rotation, float fontSize, float spacing, ::Color tint)
Draw text using Font and pro parameters (rotation)
Definition: Functions.hpp:271
raylib::Rectangle
Rectangle type.
Definition: Rectangle.hpp:12
raylib::Font
Font type, includes texture and charSet array data.
Definition: Font.hpp:14
raylib::Font::GetGlyphIndex
int GetGlyphIndex(int character) const
Get index position for a unicode character on font.
Definition: Font.hpp:226
raylib::Vector2
Vector2 type.
Definition: Vector2.hpp:16
raylib::Font::Font
Font(const std::string &fileName, int fontSize, int *fontChars, int charCount)
Loads a Font from the given file, with generation parameters.
Definition: Font.hpp:55
raylib::Color
Color type, RGBA (32bit)
Definition: Color.hpp:14
raylib::Font::ImageText
inline ::Image ImageText(const std::string &text, float fontSize, float spacing, ::Color tint) const
Create an image from text (custom sprite font)
Definition: Font.hpp:233
raylib::Font::Load
bool Load(const std::string &fileName, int fontSize, int *fontChars, int charCount)
Loads a font from a given file with generation parameters.
Definition: Font.hpp:167
raylib::Texture
Texture type.
Definition: Texture.hpp:16
raylib::Font::MeasureText
Vector2 MeasureText(const std::string &text, float fontSize, float spacing) const
Measure string size for Font.
Definition: Font.hpp:219
raylib::Font::Load
bool Load(const std::string &fileName)
Loads a font from a given file.
Definition: Font.hpp:152
raylib::RaylibException
Exception used for most raylib-related exceptions.
Definition: RaylibException.hpp:13
raylib::DrawTextEx
static void DrawTextEx(const Font &font, const std::string &text, Vector2 position, float fontSize, float spacing, ::Color tint)
Draw text using font and additional parameters.
Definition: Functions.hpp:263