Plutonium framework API 0.3.0
UI framework libraries for libnx
Loading...
Searching...
No Matches
ttf_Font.hpp
Go to the documentation of this file.
1
2#pragma once
3#include <pu/sdl2/sdl2_Types.hpp>
4#include <pu/ui/ui_Types.hpp>
5#include <vector>
6
7namespace pu::ttf {
8
9 class Font {
10 private:
11 using FontFaceDisposingFunction = void(*)(void*);
12
13 struct FontFace {
14 sdl2::Font font;
15 void *ptr;
16 size_t ptr_sz;
17 FontFaceDisposingFunction dispose_fn;
18
19 FontFace(void *buf, const size_t buf_size, FontFaceDisposingFunction disp_fn, const u32 font_sz, void *font_class_ptr) : font(nullptr), ptr(buf), ptr_sz(buf_size), dispose_fn(disp_fn) {
20 this->font = TTF_OpenFontRW(SDL_RWFromMem(this->ptr, this->ptr_sz), 1, font_sz);
21 if(this->font != nullptr) {
22 TTF_CppWrap_SetCppPtrRef(this->font, font_class_ptr);
23 }
24 }
25
26 FontFace() : font(nullptr), ptr(nullptr), ptr_sz(0), dispose_fn(EmptyFontFaceDisposingFunction) {}
27
28 inline bool IsSourceValid() {
29 // AKA - is the base ptr and size valid?
30 return (this->ptr != nullptr) && (this->ptr_sz > 0);
31 }
32
33 void DisposeFont() {
34 if(this->font != nullptr) {
35 TTF_CloseFont(this->font);
36 this->font = nullptr;
37 }
38 }
39
40 void Dispose() {
41 this->DisposeFont();
42 if(this->IsSourceValid()) {
43 (this->dispose_fn)(this->ptr);
44 this->ptr = nullptr;
45 this->ptr_sz = 0;
46 }
47 }
48
49 };
50
52 u32 font_size;
53
54 inline sdl2::Font TryGetFirstFont() {
55 if(!this->font_faces.empty()) {
56 return this->font_faces.begin()->second->font;
57 }
58 return nullptr;
59 }
60
61 public:
62 static constexpr i32 InvalidFontFaceIndex = -1;
63 static constexpr u32 DefaultFontSize = 25;
64
65 static void EmptyFontFaceDisposingFunction(void*) {}
66
67 static inline constexpr bool IsValidFontFaceIndex(const i32 index) {
68 return index != InvalidFontFaceIndex;
69 }
70
71 Font(const u32 font_sz) : font_size(font_sz) {}
72 ~Font();
73
74 i32 LoadFromMemory(void *ptr, const size_t size, FontFaceDisposingFunction disp_fn);
75 i32 LoadFromFile(const std::string &path);
76 void Unload(const i32 font_idx);
77
78 inline u32 GetFontSize() {
79 return this->font_size;
80 }
81
82 sdl2::Font FindValidFontFor(const Uint16 ch);
83 std::pair<u32, u32> GetTextDimensions(const std::string &str);
84 sdl2::Texture RenderText(const std::string &str, const ui::Color clr);
85 };
86
87}
Definition ttf_Font.hpp:9
void Unload(const i32 font_idx)
static void EmptyFontFaceDisposingFunction(void *)
Definition ttf_Font.hpp:65
i32 LoadFromMemory(void *ptr, const size_t size, FontFaceDisposingFunction disp_fn)
sdl2::Texture RenderText(const std::string &str, const ui::Color clr)
u32 GetFontSize()
Definition ttf_Font.hpp:78
static constexpr i32 InvalidFontFaceIndex
Definition ttf_Font.hpp:62
static constexpr bool IsValidFontFaceIndex(const i32 index)
Definition ttf_Font.hpp:67
Font(const u32 font_sz)
Definition ttf_Font.hpp:71
static constexpr u32 DefaultFontSize
Definition ttf_Font.hpp:63
i32 LoadFromFile(const std::string &path)
sdl2::Font FindValidFontFor(const Uint16 ch)
std::pair< u32, u32 > GetTextDimensions(const std::string &str)
Definition sdl2_Types.hpp:10
Definition ttf_Font.hpp:7
DECLSPEC void SDLCALL TTF_CloseFont(TTF_Font *font)
void TTF_CppWrap_SetCppPtrRef(TTF_Font *font, void *cpp_ptr_ref)
Definition ui_Types.hpp:44