1#ifndef RAYLIB_CPP_INCLUDE_FONT_HPP_
2#define RAYLIB_CPP_INCLUDE_FONT_HPP_
9#include "./RaylibException.hpp"
10#include "./TextureUnmanaged.hpp"
11#include "./raylib-cpp-utils.hpp"
12#include "./raylib.hpp"
18class Font :
public ::Font {
25 ::Rectangle* recs =
nullptr,
26 ::GlyphInfo* glyphs =
nullptr)
27 :
::Font{baseSize, glyphCount, glyphPadding, texture, recs, glyphs} {
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()} {
44 Font(const ::Font& font) : ::
Font(font) { }
53 Font(
const std::string_view fileName) {
66 Font(
const std::string_view fileName,
int fontSize,
int* fontChars = 0,
int charCount = 0) {
67 Load(fileName, fontSize, fontChars, charCount);
79 Font(
const std::string_view fileName,
int fontSize, std::span<int> fontChars) {
80 Load(fileName, fontSize, fontChars.data(),
static_cast<int>(fontChars.size()));
92 Font(const ::Image& image, ::Color key,
int firstChar) {
Load(image, key, firstChar); }
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);
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()));
117 other.glyphCount = 0;
118 other.glyphPadding = 0;
120 other.recs =
nullptr;
121 other.glyphs =
nullptr;
124 ~Font() { Unload(); }
134 GETTER(
int, BaseSize, baseSize)
135 GETTER(
int, GlyphCount, glyphCount)
136 GETTER(
int, GlyphPadding, glyphPadding)
138 GETTER(::GlyphInfo*, Glyphs, glyphs)
148 void SetTexture(const ::Texture& newTexture) { texture = newTexture; }
150 Font& operator=(const ::Font& font) {
156 Font& operator=(
const Font&) =
delete;
158 Font& operator=(
Font&& other)
noexcept {
159 if (
this == &other) {
167 other.glyphCount = 0;
168 other.glyphPadding = 0;
170 other.recs =
nullptr;
171 other.glyphs =
nullptr;
185 void Load(
const std::string_view fileName) {
188 throw RaylibException(
"Failed to load Font with from file: " + std::string(fileName));
202 void Load(
const std::string_view fileName,
int fontSize,
int* fontChars,
int charCount) {
203 set(
::LoadFontEx(fileName.data(), fontSize, fontChars, charCount));
205 throw RaylibException(
"Failed to load Font with from file with font size: " + std::string(fileName));
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())));
212 throw new RaylibException(
"Failed to load Font with from file with font size: " + std::string(fileName));
216 void Load(const ::Image& image, ::Color key,
int firstChar) {
217 set(::LoadFontFromImage(image, key, firstChar));
219 throw RaylibException(
"Failed to load Font with from image");
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,
228 throw RaylibException(
"Failed to load Font " + std::string(fileType) +
" with from file data");
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())));
237 throw new RaylibException(
"Failed to load Font " + std::string(fileType) +
" with from file data");
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,
246 throw RaylibException(
"Failed to load Font " + std::string(fileType) +
" with from file data");
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())));
255 throw new RaylibException(
"Failed to load Font " + std::string(fileType) +
" with from file data");
262 [[nodiscard]]
bool IsValid()
const { return ::IsFontValid(*
this); }
267 void DrawText(
const char* text, ::Vector2 position,
float fontSize,
float spacing, ::Color tint = WHITE)
const {
268 ::DrawTextEx(*
this, text, position, fontSize, spacing, tint);
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);
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);
289 void DrawText(
const std::string_view text,
int posX,
int posY,
float fontSize,
290 float spacing, ::Color tint = WHITE)
const {
292 { static_cast<float>(posX), static_cast<float>(posY) },
293 fontSize, spacing, tint);
303 ::Color tint = WHITE)
const {
311 const std::string_view text,
317 ::Color tint = WHITE)
const {
327 void DrawText(
int codepoint, ::Vector2 position,
float fontSize, ::Color tint = {255, 255, 255, 255})
const {
328 ::DrawTextCodepoint(*
this, codepoint, position, fontSize, tint);
335 const int* codepoints,
340 ::Color tint = {255, 255, 255, 255})
const {
341 ::DrawTextCodepoints(*
this, codepoints, count, position, fontSize, spacing, tint);
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()),
360 return ::MeasureTextEx(*
this, text, fontSize, spacing);
367 return ::MeasureTextEx(*
this, text.data(), fontSize, spacing);
373 [[nodiscard]]
int GetGlyphIndex(
int character)
const { return ::GetGlyphIndex(*
this, character); }
378 [[nodiscard]] ::Image
ImageText(
const char* text,
float fontSize,
float spacing, ::Color tint)
const {
379 return ::ImageTextEx(*
this, text, fontSize, spacing, tint);
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);
390 void set(const ::Font& font) {
391 baseSize = font.baseSize;
392 glyphCount = font.glyphCount;
393 glyphPadding = font.glyphPadding;
394 texture = font.texture;
396 glyphs = font.glyphs;
Degree type (allows automatic worry free conversion between radians and degrees)
Font type, includes texture and charSet array data.
void DrawText(const int *codepoints, int count, ::Vector2 position, float fontSize, float spacing, ::Color tint={255, 255, 255, 255}) const
Draw multiple character (codepoint)
void SetTexture(const ::Texture &newTexture)
Set the texture atlas containing the glyphs.
Font(const std::string_view fileName, int fontSize, std::span< int > fontChars)
Loads a Font from the given file, with generation parameters.
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.
Font(const std::string_view fileName)
Loads a Font from the given file.
void Load(const std::string_view fileName, int fontSize, int *fontChars, int charCount)
Loads a font from a given file with generation parameters.
int GetGlyphIndex(int character) const
Get index position for a unicode character on font.
Font()
Retrieves the default Font.
Vector2 MeasureText(const char *text, float fontSize, float spacing) const
Measure string size for Font.
Font(const std::string_view fileName, int fontSize, int *fontChars=0, int charCount=0)
Loads a Font from the given file, with generation parameters.
::Image ImageText(const std::string_view text, float fontSize, float spacing, ::Color tint) const
Create an image from text (custom sprite font)
void DrawText(int codepoint, ::Vector2 position, float fontSize, ::Color tint={255, 255, 255, 255}) const
Draw one character (codepoint)
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.
void DrawText(const char *text, int posX, int posY, float fontSize, float spacing, ::Color tint=WHITE) const
Draw text using font and additional parameters.
bool IsValid() const
Returns if the font is ready to be used.
void DrawText(const char *text, ::Vector2 position, float fontSize, float spacing, ::Color tint=WHITE) const
Draw text using font and additional parameters.
void DrawText(std::span< const int > codepoints, ::Vector2 position, float fontSize, float spacing, ::Color tint={ 255, 255, 255, 255 }) const
Draw multiple character (codepoint)
TextureUnmanaged GetTexture()
Get the texture atlas containing the glyphs.
void Load(const std::string_view fileName)
Loads a font from a given file.
void DrawText(const std::string_view text, ::Vector2 position, float fontSize, float spacing, ::Color tint=WHITE) const
Draw text using font and additional parameters.
Font(const ::Image &image, ::Color key, int firstChar)
Loads a Font from the given image with a color key.
Vector2 MeasureText(const std::string_view text, float fontSize, float spacing) const
Measure string size for Font.
::Image ImageText(const char *text, float fontSize, float spacing, ::Color tint) const
Create an image from text (custom sprite font)
Exception used for most raylib-related exceptions.
A Texture that is not managed by C++ RAII.
All raylib-cpp classes and functions appear in the raylib namespace.
static inline ::Font LoadFont(const std::string_view fileName)
Load font from file (filename must include file extension)
static inline ::Font LoadFontEx(const std::string_view fileName, int fontSize, int *fontChars, int charsCount)
Load font from file (filename must include file extension)
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)
static void DrawTextEx(const Font &font, char *text, Vector2 position, float fontSize, float spacing, ::Color tint)
Draw text using font and additional parameters.