raylib-cpp
C++ object-oriented wrapper library for raylib.
Loading...
Searching...
No Matches
Font.hpp
1#ifndef RAYLIB_CPP_INCLUDE_FONT_HPP_
2#define RAYLIB_CPP_INCLUDE_FONT_HPP_
3
4#include <cassert>
5#include <string>
6#include <string_view>
7#include <span>
8
9#include "./raylib.hpp"
10#include "./raylib-cpp-utils.hpp"
11#include "./RaylibException.hpp"
12#include "./TextureUnmanaged.hpp"
13
14namespace raylib {
18class Font : public ::Font {
19 public:
20 Font(int baseSize,
21 int glyphCount,
22 int glyphPadding,
23 ::Texture2D texture,
24 ::Rectangle *recs = nullptr,
25 ::GlyphInfo *glyphs = nullptr) : ::Font{baseSize, glyphCount, glyphPadding, texture, recs, glyphs} {
26 // Nothing.
27 }
28
29 Font(int baseSize,
30 int glyphPadding,
31 ::Texture2D texture,
32 std::span<::Rectangle> recs = {},
33 std::span<::GlyphInfo> glyphs = {}) : ::Font{baseSize, static_cast<int>((assert(glyphs.size() == recs.size()), glyphs.size())), glyphPadding, texture, recs.data(), glyphs.data()} {
34 // Nothing.
35 }
36
40 Font() {
41 set(::GetFontDefault());
42 }
43
44 Font(const ::Font& font) {
45 set(font);
46 }
47
55 Font(const std::string_view fileName) {
56 Load(fileName);
57 }
58
68 Font(const std::string_view fileName, int fontSize, int* fontChars = 0, int charCount = 0) {
69 Load(fileName, fontSize, fontChars, charCount);
70 }
71
81 Font(const std::string_view fileName, int fontSize, std::span<int> fontChars) {
82 Load(fileName, fontSize, fontChars.data(), static_cast<int>(fontChars.size()));
83 }
84
94 Font(const ::Image& image, ::Color key, int firstChar) {
95 Load(image, key, firstChar);
96 }
97
105 Font(const std::string_view fileType, const unsigned char* fileData, int dataSize, int fontSize,
106 int *fontChars, int charsCount) {
107 Load(fileType, fileData, dataSize, fontSize, fontChars, charsCount);
108 }
109
110 Font(const std::string_view fileType, const unsigned char* fileData, int dataSize, int fontSize,
111 std::span<int> fontChars) {
112 Load(fileType, fileData, dataSize, fontSize, fontChars.data(), static_cast<int>(fontChars.size()));
113 }
114
115 Font(const Font&) = delete;
116
117 Font(Font&& other) {
118 set(other);
119
120 other.baseSize = 0;
121 other.glyphCount = 0;
122 other.glyphPadding = 0;
123 other.texture = {};
124 other.recs = nullptr;
125 other.glyphs = nullptr;
126 }
127
128 ~Font() {
129 Unload();
130 }
131
132 void Unload() {
133 // Protect against calling UnloadFont() twice.
134 if (baseSize != 0) {
135 UnloadFont(*this);
136 baseSize = 0;
137 }
138 }
139
140 GETTER(int, BaseSize, baseSize)
141 GETTER(int, GlyphCount, glyphCount)
142 GETTER(int, GlyphPadding, glyphPadding)
143 GETTER(::Rectangle*, Recs, recs)
144 GETTER(::GlyphInfo*, Glyphs, glyphs)
145
150 return texture;
151 }
152
156 void SetTexture(const ::Texture& newTexture) {
157 texture = newTexture;
158 }
159
160 Font& operator=(const ::Font& font) {
161 Unload();
162 set(font);
163 return *this;
164 }
165
166 Font& operator=(const Font&) = delete;
167
168 Font& operator=(Font&& other) noexcept {
169 if (this == &other) {
170 return *this;
171 }
172
173 Unload();
174 set(other);
175
176 other.baseSize = 0;
177 other.glyphCount = 0;
178 other.glyphPadding = 0;
179 other.texture = {};
180 other.recs = nullptr;
181 other.glyphs = nullptr;
182
183 return *this;
184 }
185
195 void Load(const std::string_view fileName) {
196 set(::LoadFont(fileName.data()));
197 if (!IsReady()) {
198 throw RaylibException("Failed to load Font with from file: " + std::string(fileName));
199 }
200 }
201
212 void Load(const std::string_view fileName, int fontSize, int* fontChars, int charCount) {
213 set(::LoadFontEx(fileName.data(), fontSize, fontChars, charCount));
214 if (!IsReady()) {
215 throw RaylibException("Failed to load Font with from file with font size: " + std::string(fileName));
216 }
217 }
218
219 void Load(const std::string_view fileName, int fontSize, std::span<int> fontChars) {
220 set(::LoadFontEx(fileName.data(), fontSize, fontChars.data(), static_cast<int>(fontChars.size())));
221 if (!IsReady()) {
222 throw new RaylibException("Failed to load Font with from file with font size: " + std::string(fileName));
223 }
224 }
225
226 void Load(const ::Image& image, ::Color key, int firstChar) {
227 set(::LoadFontFromImage(image, key, firstChar));
228 if (!IsReady()) {
229 throw RaylibException("Failed to load Font with from image");
230 }
231 }
232
233 void Load(const std::string_view fileType, const unsigned char* fileData, int dataSize, int fontSize,
234 int *fontChars, int charsCount) {
235 set(::LoadFontFromMemory(fileType.data(), fileData, dataSize, fontSize, fontChars,
236 charsCount));
237 if (!IsReady()) {
238 throw RaylibException("Failed to load Font " + std::string(fileType) + " with from file data");
239 }
240 }
241
242 void Load(const std::string_view fileType, const unsigned char* fileData, int dataSize, int fontSize,
243 std::span<int> fontChars) {
244 set(::LoadFontFromMemory(fileType.data(), fileData, dataSize, fontSize, fontChars.data(),
245 static_cast<int>(fontChars.size())));
246 if (!IsReady()) {
247 throw new RaylibException("Failed to load Font " + std::string(fileType) + " with from file data");
248 }
249 }
250
251 void Load(const std::string_view fileType, std::span<unsigned char> fileData, int fontSize,
252 int *fontChars, int charsCount) {
253 set(::LoadFontFromMemory(fileType.data(), fileData.data(), static_cast<int>(fileData.size()), fontSize, fontChars,
254 charsCount));
255 if (!IsReady()) {
256 throw RaylibException("Failed to load Font " + std::string(fileType) + " with from file data");
257 }
258 }
259
260 void Load(const std::string_view fileType, std::span<unsigned char> fileData, int fontSize,
261 std::span<int> fontChars) {
262 set(::LoadFontFromMemory(fileType.data(), fileData.data(), static_cast<int>(fileData.size()), fontSize, fontChars.data(),
263 static_cast<int>(fontChars.size())));
264 if (!IsReady()) {
265 throw new RaylibException("Failed to load Font " + std::string(fileType) + " with from file data");
266 }
267 }
268
272 bool IsReady() const {
273 return ::IsFontReady(*this);
274 }
275
279 void DrawText(const char* text, ::Vector2 position, float fontSize,
280 float spacing, ::Color tint = WHITE) const {
281 ::DrawTextEx(*this, text, position, fontSize, spacing, tint);
282 }
283
287 void DrawText(const std::string_view text, ::Vector2 position, float fontSize,
288 float spacing, ::Color tint = WHITE) const {
289 ::DrawTextEx(*this, text.data(), position, fontSize, spacing, tint);
290 }
291
295 void DrawText(const char* text, int posX, int posY, float fontSize,
296 float spacing, ::Color tint = WHITE) const {
297 ::DrawTextEx(*this, text,
298 { static_cast<float>(posX), static_cast<float>(posY) },
299 fontSize, spacing, tint);
300 }
301
305 void DrawText(const std::string_view text, int posX, int posY, float fontSize,
306 float spacing, ::Color tint = WHITE) const {
307 ::DrawTextEx(*this, text.data(),
308 { static_cast<float>(posX), static_cast<float>(posY) },
309 fontSize, spacing, tint);
310 }
311
312 void DrawText(
313 const char* text,
314 ::Vector2 position,
315 ::Vector2 origin,
316 Degree rotation,
317 float fontSize,
318 float spacing,
319 ::Color tint = WHITE) const {
320 ::DrawTextPro(*this, text,
321 position, origin,
322 rotation, fontSize,
323 spacing, tint);
324 }
325
326 void DrawText(
327 const std::string_view text,
328 ::Vector2 position,
329 ::Vector2 origin,
330 Degree rotation,
331 float fontSize,
332 float spacing,
333 ::Color tint = WHITE) const {
334 ::DrawTextPro(*this, text.data(),
335 position, origin,
336 rotation, fontSize,
337 spacing, tint);
338 }
339
343 void DrawText(int codepoint,
344 ::Vector2 position,
345 float fontSize,
346 ::Color tint = { 255, 255, 255, 255 }) const {
347 ::DrawTextCodepoint(*this, codepoint, position, fontSize, tint);
348 }
349
353 void DrawText(const int *codepoints,
354 int count, ::Vector2 position,
355 float fontSize, float spacing,
356 ::Color tint = { 255, 255, 255, 255 }) const {
357 ::DrawTextCodepoints(*this,
358 codepoints, count,
359 position, fontSize,
360 spacing, tint);
361 }
362
366 inline void DrawText(std::span<const int> codepoints, ::Vector2 position,
367 float fontSize, float spacing,
368 ::Color tint = { 255, 255, 255, 255 }) const {
369 ::DrawTextCodepoints(*this,
370 codepoints.data(), static_cast<int>(codepoints.size()),
371 position, fontSize,
372 spacing, tint);
373 }
374
378 Vector2 MeasureText(const char* text, float fontSize, float spacing) const {
379 return ::MeasureTextEx(*this, text, fontSize, spacing);
380 }
381
385 Vector2 MeasureText(const std::string_view text, float fontSize, float spacing) const {
386 return ::MeasureTextEx(*this, text.data(), fontSize, spacing);
387 }
388
392 int GetGlyphIndex(int character) const {
393 return ::GetGlyphIndex(*this, character);
394 }
395
399 ::Image ImageText(const char* text, float fontSize,
400 float spacing, ::Color tint) const {
401 return ::ImageTextEx(*this, text, fontSize, spacing, tint);
402 }
403
407 ::Image ImageText(const std::string_view text, float fontSize,
408 float spacing, ::Color tint) const {
409 return ::ImageTextEx(*this, text.data(), fontSize, spacing, tint);
410 }
411
412 protected:
413 void set(const ::Font& font) {
414 baseSize = font.baseSize;
415 glyphCount = font.glyphCount;
416 glyphPadding = font.glyphPadding;
417 texture = font.texture;
418 recs = font.recs;
419 glyphs = font.glyphs;
420 }
421};
422} // namespace raylib
423
424using RFont = raylib::Font;
425
426#endif // RAYLIB_CPP_INCLUDE_FONT_HPP_
Degree type (allows automatic worry free conversion between radians and degrees)
Font type, includes texture and charSet array data.
Definition: Font.hpp:18
void DrawText(const int *codepoints, int count, ::Vector2 position, float fontSize, float spacing, ::Color tint={ 255, 255, 255, 255 }) const
Draw multiple character (codepoint)
Definition: Font.hpp:353
void SetTexture(const ::Texture &newTexture)
Set the texture atlas containing the glyphs.
Definition: Font.hpp:156
Font(const std::string_view fileName, int fontSize, std::span< int > fontChars)
Loads a Font from the given file, with generation parameters.
Definition: Font.hpp:81
Font(const std::string_view 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:105
Font(const std::string_view fileName)
Loads a Font from the given file.
Definition: Font.hpp:55
void Load(const std::string_view fileName, int fontSize, int *fontChars, int charCount)
Loads a font from a given file with generation parameters.
Definition: Font.hpp:212
int GetGlyphIndex(int character) const
Get index position for a unicode character on font.
Definition: Font.hpp:392
Font()
Retrieves the default Font.
Definition: Font.hpp:40
void DrawText(int codepoint, ::Vector2 position, float fontSize, ::Color tint={ 255, 255, 255, 255 }) const
Draw one character (codepoint)
Definition: Font.hpp:343
Vector2 MeasureText(const char *text, float fontSize, float spacing) const
Measure string size for Font.
Definition: Font.hpp:378
bool IsReady() const
Returns if the font is ready to be used.
Definition: Font.hpp:272
Font(const std::string_view fileName, int fontSize, int *fontChars=0, int charCount=0)
Loads a Font from the given file, with generation parameters.
Definition: Font.hpp:68
::Image ImageText(const std::string_view text, float fontSize, float spacing, ::Color tint) const
Create an image from text (custom sprite font)
Definition: Font.hpp:407
void DrawText(const std::string_view text, int posX, int posY, float fontSize, float spacing, ::Color tint=WHITE) const
Draw text using font and additional parameters.
Definition: Font.hpp:305
void DrawText(const char *text, int posX, int posY, float fontSize, float spacing, ::Color tint=WHITE) const
Draw text using font and additional parameters.
Definition: Font.hpp:295
void DrawText(const char *text, ::Vector2 position, float fontSize, float spacing, ::Color tint=WHITE) const
Draw text using font and additional parameters.
Definition: Font.hpp:279
void DrawText(std::span< const int > codepoints, ::Vector2 position, float fontSize, float spacing, ::Color tint={ 255, 255, 255, 255 }) const
Draw multiple character (codepoint)
Definition: Font.hpp:366
TextureUnmanaged GetTexture()
Get the texture atlas containing the glyphs.
Definition: Font.hpp:149
void Load(const std::string_view fileName)
Loads a font from a given file.
Definition: Font.hpp:195
void DrawText(const std::string_view text, ::Vector2 position, float fontSize, float spacing, ::Color tint=WHITE) const
Draw text using font and additional parameters.
Definition: Font.hpp:287
Font(const ::Image &image, ::Color key, int firstChar)
Loads a Font from the given image with a color key.
Definition: Font.hpp:94
Vector2 MeasureText(const std::string_view text, float fontSize, float spacing) const
Measure string size for Font.
Definition: Font.hpp:385
::Image ImageText(const char *text, float fontSize, float spacing, ::Color tint) const
Create an image from text (custom sprite font)
Definition: Font.hpp:399
Exception used for most raylib-related exceptions.
Rectangle type.
Definition: Rectangle.hpp:14
A Texture that is not managed by C++ RAII.
Vector2 type.
Definition: Vector2.hpp:19