1#ifndef RAYLIB_CPP_INCLUDE_FONT_HPP_
2#define RAYLIB_CPP_INCLUDE_FONT_HPP_
10#include "./raylib-cpp-utils.hpp"
11#include "./RaylibException.hpp"
12#include "./TextureUnmanaged.hpp"
18class Font :
public ::Font {
24 ::Rectangle *recs =
nullptr,
25 ::GlyphInfo *glyphs =
nullptr) :
::Font{baseSize, glyphCount, glyphPadding, texture, recs, glyphs} {
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()} {
41 set(::GetFontDefault());
44 Font(const ::Font& font) {
55 Font(
const std::string_view fileName) {
68 Font(
const std::string_view fileName,
int fontSize,
int* fontChars = 0,
int charCount = 0) {
69 Load(fileName, fontSize, fontChars, charCount);
81 Font(
const std::string_view fileName,
int fontSize, std::span<int> fontChars) {
82 Load(fileName, fontSize, fontChars.data(),
static_cast<int>(fontChars.size()));
94 Font(const ::Image& image, ::Color key,
int firstChar) {
95 Load(image, key, firstChar);
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);
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()));
121 other.glyphCount = 0;
122 other.glyphPadding = 0;
124 other.recs =
nullptr;
125 other.glyphs =
nullptr;
140 GETTER(
int, BaseSize, baseSize)
141 GETTER(
int, GlyphCount, glyphCount)
142 GETTER(
int, GlyphPadding, glyphPadding)
144 GETTER(::GlyphInfo*, Glyphs, glyphs)
157 texture = newTexture;
160 Font& operator=(const ::Font& font) {
166 Font& operator=(
const Font&) =
delete;
168 Font& operator=(
Font&& other)
noexcept {
169 if (
this == &other) {
177 other.glyphCount = 0;
178 other.glyphPadding = 0;
180 other.recs =
nullptr;
181 other.glyphs =
nullptr;
195 void Load(
const std::string_view fileName) {
196 set(::LoadFont(fileName.data()));
198 throw RaylibException(
"Failed to load Font with from file: " + std::string(fileName));
212 void Load(
const std::string_view fileName,
int fontSize,
int* fontChars,
int charCount) {
213 set(::LoadFontEx(fileName.data(), fontSize, fontChars, charCount));
215 throw RaylibException(
"Failed to load Font with from file with font size: " + std::string(fileName));
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())));
222 throw new RaylibException(
"Failed to load Font with from file with font size: " + std::string(fileName));
226 void Load(const ::Image& image, ::Color key,
int firstChar) {
227 set(::LoadFontFromImage(image, key, firstChar));
229 throw RaylibException(
"Failed to load Font with from image");
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,
238 throw RaylibException(
"Failed to load Font " + std::string(fileType) +
" with from file data");
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())));
247 throw new RaylibException(
"Failed to load Font " + std::string(fileType) +
" with from file data");
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,
256 throw RaylibException(
"Failed to load Font " + std::string(fileType) +
" with from file data");
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())));
265 throw new RaylibException(
"Failed to load Font " + std::string(fileType) +
" with from file data");
273 return ::IsFontReady(*
this);
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);
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);
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);
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);
319 ::Color tint = WHITE)
const {
320 ::DrawTextPro(*
this, text,
327 const std::string_view text,
333 ::Color tint = WHITE)
const {
334 ::DrawTextPro(*
this, text.data(),
346 ::Color tint = { 255, 255, 255, 255 })
const {
347 ::DrawTextCodepoint(*
this, codepoint, position, fontSize, tint);
354 int count, ::Vector2 position,
355 float fontSize,
float spacing,
356 ::Color tint = { 255, 255, 255, 255 })
const {
357 ::DrawTextCodepoints(*
this,
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()),
379 return ::MeasureTextEx(*
this, text, fontSize, spacing);
386 return ::MeasureTextEx(*
this, text.data(), fontSize, spacing);
393 return ::GetGlyphIndex(*
this, character);
400 float spacing, ::Color tint)
const {
401 return ::ImageTextEx(*
this, text, fontSize, spacing, tint);
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);
413 void set(const ::Font& font) {
414 baseSize = font.baseSize;
415 glyphCount = font.glyphCount;
416 glyphPadding = font.glyphPadding;
417 texture = font.texture;
419 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.
void DrawText(int codepoint, ::Vector2 position, float fontSize, ::Color tint={ 255, 255, 255, 255 }) const
Draw one character (codepoint)
Vector2 MeasureText(const char *text, float fontSize, float spacing) const
Measure string size for Font.
bool IsReady() const
Returns if the font is ready to be used.
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(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.
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.