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 "./RaylibException.hpp"
10#include "./TextureUnmanaged.hpp"
11#include "./raylib-cpp-utils.hpp"
12#include "./raylib.hpp"
13
14namespace raylib {
18class Font : public ::Font {
19public:
20 Font(
21 int baseSize,
22 int glyphCount,
23 int glyphPadding,
24 ::Texture2D texture,
25 ::Rectangle* recs = nullptr,
26 ::GlyphInfo* glyphs = nullptr)
27 : ::Font{baseSize, glyphCount, glyphPadding, texture, recs, glyphs} {
28 // Nothing.
29 }
30
31 Font(int baseSize,
32 int glyphPadding,
33 ::Texture2D texture,
34 std::span<::Rectangle> recs = {},
35 std::span<::GlyphInfo> glyphs = {}) : ::Font{baseSize, static_cast<int>((assert(glyphs.size() == recs.size()), glyphs.size())), glyphPadding, texture, recs.data(), glyphs.data()} {
36 // Nothing.
37 }
38
42 Font() : ::Font(::GetFontDefault()) { }
43
44 Font(const ::Font& font) : ::Font(font) { }
45
53 Font(const std::string_view fileName) {
54 Load(fileName);
55 }
56
66 Font(const std::string_view fileName, int fontSize, int* fontChars = 0, int charCount = 0) {
67 Load(fileName, fontSize, fontChars, charCount);
68 }
69
79 Font(const std::string_view fileName, int fontSize, std::span<int> fontChars) {
80 Load(fileName, fontSize, fontChars.data(), static_cast<int>(fontChars.size()));
81 }
82
92 Font(const ::Image& image, ::Color key, int firstChar) { Load(image, key, firstChar); }
93
101 Font(const std::string_view fileType, const unsigned char* fileData, int dataSize, int fontSize,
102 int *fontChars, int charsCount) {
103 Load(fileType, fileData, dataSize, fontSize, fontChars, charsCount);
104 }
105
106 Font(const std::string_view fileType, const unsigned char* fileData, int dataSize, int fontSize,
107 std::span<int> fontChars) {
108 Load(fileType, fileData, dataSize, fontSize, fontChars.data(), static_cast<int>(fontChars.size()));
109 }
110
111 Font(const Font&) = delete;
112
113 Font(Font&& other) noexcept {
114 set(other);
115
116 other.baseSize = 0;
117 other.glyphCount = 0;
118 other.glyphPadding = 0;
119 other.texture = {};
120 other.recs = nullptr;
121 other.glyphs = nullptr;
122 }
123
124 ~Font() { Unload(); }
125
126 void Unload() {
127 // Protect against calling UnloadFont() twice.
128 if (baseSize != 0) {
129 UnloadFont(*this);
130 baseSize = 0;
131 }
132 }
133
134 GETTER(int, BaseSize, baseSize)
135 GETTER(int, GlyphCount, glyphCount)
136 GETTER(int, GlyphPadding, glyphPadding)
137 GETTER(::Rectangle*, Recs, recs)
138 GETTER(::GlyphInfo*, Glyphs, glyphs)
139
143 TextureUnmanaged GetTexture() { return texture; }
144
148 void SetTexture(const ::Texture& newTexture) { texture = newTexture; }
149
150 Font& operator=(const ::Font& font) {
151 Unload();
152 set(font);
153 return *this;
154 }
155
156 Font& operator=(const Font&) = delete;
157
158 Font& operator=(Font&& other) noexcept {
159 if (this == &other) {
160 return *this;
161 }
162
163 Unload();
164 set(other);
165
166 other.baseSize = 0;
167 other.glyphCount = 0;
168 other.glyphPadding = 0;
169 other.texture = {};
170 other.recs = nullptr;
171 other.glyphs = nullptr;
172
173 return *this;
174 }
175
185 void Load(const std::string_view fileName) {
186 set(::LoadFont(fileName.data()));
187 if (!IsValid()) {
188 throw RaylibException("Failed to load Font with from file: " + std::string(fileName));
189 }
190 }
191
202 void Load(const std::string_view fileName, int fontSize, int* fontChars, int charCount) {
203 set(::LoadFontEx(fileName.data(), fontSize, fontChars, charCount));
204 if (!IsValid()) {
205 throw RaylibException("Failed to load Font with from file with font size: " + std::string(fileName));
206 }
207 }
208
209 void Load(const std::string_view fileName, int fontSize, std::span<int> fontChars) {
210 set(::LoadFontEx(fileName.data(), fontSize, fontChars.data(), static_cast<int>(fontChars.size())));
211 if (!IsValid()) {
212 throw new RaylibException("Failed to load Font with from file with font size: " + std::string(fileName));
213 }
214 }
215
216 void Load(const ::Image& image, ::Color key, int firstChar) {
217 set(::LoadFontFromImage(image, key, firstChar));
218 if (!IsValid()) {
219 throw RaylibException("Failed to load Font with from image");
220 }
221 }
222
223 void Load(const std::string_view fileType, const unsigned char* fileData, int dataSize, int fontSize,
224 int *fontChars, int charsCount) {
225 set(::LoadFontFromMemory(fileType.data(), fileData, dataSize, fontSize, fontChars,
226 charsCount));
227 if (!IsValid()) {
228 throw RaylibException("Failed to load Font " + std::string(fileType) + " with from file data");
229 }
230 }
231
232 void Load(const std::string_view fileType, const unsigned char* fileData, int dataSize, int fontSize,
233 std::span<int> fontChars) {
234 set(::LoadFontFromMemory(fileType.data(), fileData, dataSize, fontSize, fontChars.data(),
235 static_cast<int>(fontChars.size())));
236 if (!IsValid()) {
237 throw new RaylibException("Failed to load Font " + std::string(fileType) + " with from file data");
238 }
239 }
240
241 void Load(const std::string_view fileType, std::span<unsigned char> fileData, int fontSize,
242 int *fontChars, int charsCount) {
243 set(::LoadFontFromMemory(fileType.data(), fileData.data(), static_cast<int>(fileData.size()), fontSize, fontChars,
244 charsCount));
245 if (!IsValid()) {
246 throw RaylibException("Failed to load Font " + std::string(fileType) + " with from file data");
247 }
248 }
249
250 void Load(const std::string_view fileType, std::span<unsigned char> fileData, int fontSize,
251 std::span<int> fontChars) {
252 set(::LoadFontFromMemory(fileType.data(), fileData.data(), static_cast<int>(fileData.size()), fontSize, fontChars.data(),
253 static_cast<int>(fontChars.size())));
254 if (!IsValid()) {
255 throw new RaylibException("Failed to load Font " + std::string(fileType) + " with from file data");
256 }
257 }
258
262 [[nodiscard]] bool IsValid() const { return ::IsFontValid(*this); }
263
267 void DrawText(const char* text, ::Vector2 position, float fontSize, float spacing, ::Color tint = WHITE) const {
268 ::DrawTextEx(*this, text, position, fontSize, spacing, tint);
269 }
270
274 void DrawText(const std::string_view text, ::Vector2 position, float fontSize,
275 float spacing, ::Color tint = WHITE) const {
276 ::DrawTextEx(*this, text.data(), position, fontSize, spacing, tint);
277 }
278
282 void DrawText(const char* text, int posX, int posY, float fontSize, float spacing, ::Color tint = WHITE) const {
283 ::DrawTextEx(*this, text, {static_cast<float>(posX), static_cast<float>(posY)}, fontSize, spacing, tint);
284 }
285
289 void DrawText(const std::string_view text, int posX, int posY, float fontSize,
290 float spacing, ::Color tint = WHITE) const {
291 ::DrawTextEx(*this, text.data(),
292 { static_cast<float>(posX), static_cast<float>(posY) },
293 fontSize, spacing, tint);
294 }
295
296 void DrawText(
297 const char* text,
298 ::Vector2 position,
299 ::Vector2 origin,
300 Degree rotation,
301 float fontSize,
302 float spacing,
303 ::Color tint = WHITE) const {
304 ::DrawTextPro(*this, text,
305 position, origin,
306 rotation, fontSize,
307 spacing, tint);
308 }
309
310 void DrawText(
311 const std::string_view text,
312 ::Vector2 position,
313 ::Vector2 origin,
314 Degree rotation,
315 float fontSize,
316 float spacing,
317 ::Color tint = WHITE) const {
318 ::DrawTextPro(*this, text.data(),
319 position, origin,
320 rotation, fontSize,
321 spacing, tint);
322 }
323
327 void DrawText(int codepoint, ::Vector2 position, float fontSize, ::Color tint = {255, 255, 255, 255}) const {
328 ::DrawTextCodepoint(*this, codepoint, position, fontSize, tint);
329 }
330
335 const int* codepoints,
336 int count,
337 ::Vector2 position,
338 float fontSize,
339 float spacing,
340 ::Color tint = {255, 255, 255, 255}) const {
341 ::DrawTextCodepoints(*this, codepoints, count, position, fontSize, spacing, tint);
342 }
343
347 inline void DrawText(std::span<const int> codepoints, ::Vector2 position,
348 float fontSize, float spacing,
349 ::Color tint = { 255, 255, 255, 255 }) const {
350 ::DrawTextCodepoints(*this,
351 codepoints.data(), static_cast<int>(codepoints.size()),
352 position, fontSize,
353 spacing, tint);
354 }
355
359 [[nodiscard]] Vector2 MeasureText(const char* text, float fontSize, float spacing) const {
360 return ::MeasureTextEx(*this, text, fontSize, spacing);
361 }
362
366 Vector2 MeasureText(const std::string_view text, float fontSize, float spacing) const {
367 return ::MeasureTextEx(*this, text.data(), fontSize, spacing);
368 }
369
373 [[nodiscard]] int GetGlyphIndex(int character) const { return ::GetGlyphIndex(*this, character); }
374
378 [[nodiscard]] ::Image ImageText(const char* text, float fontSize, float spacing, ::Color tint) const {
379 return ::ImageTextEx(*this, text, fontSize, spacing, tint);
380 }
381
385 ::Image ImageText(const std::string_view text, float fontSize,
386 float spacing, ::Color tint) const {
387 return ::ImageTextEx(*this, text.data(), fontSize, spacing, tint);
388 }
389protected:
390 void set(const ::Font& font) {
391 baseSize = font.baseSize;
392 glyphCount = font.glyphCount;
393 glyphPadding = font.glyphPadding;
394 texture = font.texture;
395 recs = font.recs;
396 glyphs = font.glyphs;
397 }
398};
399} // namespace raylib
400
401using RFont = raylib::Font;
402
403#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:334
void SetTexture(const ::Texture &newTexture)
Set the texture atlas containing the glyphs.
Definition: Font.hpp:148
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:79
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:101
Font(const std::string_view fileName)
Loads a Font from the given file.
Definition: Font.hpp:53
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:202
int GetGlyphIndex(int character) const
Get index position for a unicode character on font.
Definition: Font.hpp:373
Font()
Retrieves the default Font.
Definition: Font.hpp:42
Vector2 MeasureText(const char *text, float fontSize, float spacing) const
Measure string size for Font.
Definition: Font.hpp:359
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:66
::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:385
void DrawText(int codepoint, ::Vector2 position, float fontSize, ::Color tint={255, 255, 255, 255}) const
Draw one character (codepoint)
Definition: Font.hpp:327
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:289
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:282
bool IsValid() const
Returns if the font is ready to be used.
Definition: Font.hpp:262
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:267
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:347
TextureUnmanaged GetTexture()
Get the texture atlas containing the glyphs.
Definition: Font.hpp:143
void Load(const std::string_view fileName)
Loads a font from a given file.
Definition: Font.hpp:185
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:274
Font(const ::Image &image, ::Color key, int firstChar)
Loads a Font from the given image with a color key.
Definition: Font.hpp:92
Vector2 MeasureText(const std::string_view text, float fontSize, float spacing) const
Measure string size for Font.
Definition: Font.hpp:366
::Image ImageText(const char *text, float fontSize, float spacing, ::Color tint) const
Create an image from text (custom sprite font)
Definition: Font.hpp:378
Exception used for most raylib-related exceptions.
Rectangle type.
Definition: Rectangle.hpp:12
A Texture that is not managed by C++ RAII.
Vector2 type.
Definition: Vector2.hpp:20
All raylib-cpp classes and functions appear in the raylib namespace.
Definition: AudioDevice.hpp:8
static inline ::Font LoadFont(const std::string_view fileName)
Load font from file (filename must include file extension)
Definition: Functions.hpp:299
static inline ::Font LoadFontEx(const std::string_view fileName, int fontSize, int *fontChars, int charsCount)
Load font from file (filename must include file extension)
Definition: Functions.hpp:306
static void DrawTextPro(const Font &font, const char *text, Vector2 position, Vector2 origin, Degree rotation, float fontSize, float spacing, ::Color tint)
Draw text using Font and pro parameters (rotation)
Definition: Functions.hpp:283
static void DrawTextEx(const Font &font, char *text, Vector2 position, float fontSize, float spacing, ::Color tint)
Draw text using font and additional parameters.
Definition: Functions.hpp:268