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 = 0, int charCount = 0) {
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  Unload();
120  set(font);
121  return *this;
122  }
123 
124  Font& operator=(const Font&) = delete;
125 
126  Font& operator=(Font&& other) noexcept {
127  if (this == &other) {
128  return *this;
129  }
130 
131  Unload();
132  set(other);
133 
134  other.baseSize = 0;
135  other.glyphCount = 0;
136  other.glyphPadding = 0;
137  other.texture = {};
138  other.recs = nullptr;
139  other.glyphs = nullptr;
140 
141  return *this;
142  }
143 
153  bool Load(const std::string& fileName) {
154  set(::LoadFont(fileName.c_str()));
155  return baseSize > 0;
156  }
157 
168  bool Load(const std::string& fileName, int fontSize, int* fontChars, int charCount) {
169  set(::LoadFontEx(fileName.c_str(), fontSize, fontChars, charCount));
170  return baseSize > 0;
171  }
172 
173  bool Load(const ::Image& image, ::Color key, int firstChar) {
174  set(::LoadFontFromImage(image, key, firstChar));
175  return baseSize > 0;
176  }
177 
178  bool Load(const std::string& fileType, const unsigned char* fileData, int dataSize, int fontSize,
179  int *fontChars, int charsCount) {
180  set(::LoadFontFromMemory(fileType.c_str(), fileData, dataSize, fontSize, fontChars,
181  charsCount));
182  return baseSize > 0;
183  }
184 
188  inline Font& DrawText(const std::string& text, ::Vector2 position, float fontSize,
189  float spacing, ::Color tint = WHITE) {
190  ::DrawTextEx(*this, text.c_str(), position, fontSize, spacing, tint);
191  return *this;
192  }
193 
194  inline Font& DrawText(
195  const std::string& text,
196  ::Vector2 position,
197  ::Vector2 origin,
198  float rotation,
199  float fontSize,
200  float spacing,
201  ::Color tint = WHITE) {
202  ::DrawTextPro(*this, text.c_str(), position, origin, rotation, fontSize, spacing, tint);
203  return *this;
204  }
205 
209  inline Font& DrawText(int codepoint,
210  ::Vector2 position,
211  float fontSize,
212  ::Color tint = { 255, 255, 255, 255 }) {
213  ::DrawTextCodepoint(*this, codepoint, position, fontSize, tint);
214  return *this;
215  }
216 
220  inline Vector2 MeasureText(const std::string& text, float fontSize, float spacing) const {
221  return ::MeasureTextEx(*this, text.c_str(), fontSize, spacing);
222  }
223 
227  inline int GetGlyphIndex(int character) const {
228  return ::GetGlyphIndex(*this, character);
229  }
230 
234  inline ::Image ImageText(const std::string& text, float fontSize,
235  float spacing, ::Color tint) const {
236  return ::ImageTextEx(*this, text.c_str(), fontSize, spacing, tint);
237  }
238 
239  private:
240  void set(const ::Font& font) {
241  baseSize = font.baseSize;
242  glyphCount = font.glyphCount;
243  glyphPadding = font.glyphPadding;
244  texture = font.texture;
245  recs = font.recs;
246  glyphs = font.glyphs;
247  }
248 };
249 } // namespace raylib
250 
251 #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:188
raylib::LoadFont
static inline ::Font LoadFont(const std::string &fileName)
Load font from file (filename must include file extension)
Definition: Functions.hpp:279
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:209
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::Font::Font
Font(const std::string &fileName, int fontSize, int *fontChars=0, int charCount=0)
Loads a Font from the given file, with generation parameters.
Definition: Font.hpp:55
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:227
raylib::Vector2
Vector2 type.
Definition: Vector2.hpp:16
raylib::Color
Color type, RGBA (32bit)
Definition: Color.hpp:14
raylib::LoadFontEx
static inline ::Font LoadFontEx(const std::string &fileName, int fontSize, int *fontChars, int charsCount)
Load font from file (filename must include file extension)
Definition: Functions.hpp:286
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:234
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:168
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:220
raylib::Font::Load
bool Load(const std::string &fileName)
Loads a font from a given file.
Definition: Font.hpp:153
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