Plutonium framework API  0.3
Simple UI framework for libnx and SDL2
ttf_Font.hpp
Go to the documentation of this file.
1 
2 #pragma once
3 #include <pu/sdl2/sdl2_Types.hpp>
4 #include <pu/pu_String.hpp>
5 #include <pu/ui/ui_Types.hpp>
6 #include <functional>
7 #include <map>
8 
9 namespace pu::ttf
10 {
11  using FontFaceDisposingFunction = std::function<void(void*)>;
12 
13  inline void EmptyFontFaceDisposingFunction(void*)
14  {
15  // Just do nothing :P
16  }
17 
18  class Font
19  {
20 
21  private:
22 
23  struct FontFace
24  {
25  sdl2::Font font;
26  void *ptr;
27  size_t ptr_sz;
28  FontFaceDisposingFunction dispose_fn;
29 
30  FontFace(void *buf, size_t buf_size, FontFaceDisposingFunction disp_fn, u32 font_sz, void *font_class_ptr) : font(nullptr), ptr(buf), ptr_sz(buf_size), dispose_fn(disp_fn)
31  {
32  this->font = TTF_OpenFontRW(SDL_RWFromMem(this->ptr, this->ptr_sz), 1, font_sz);
33  if(this->font != nullptr) TTF_CppWrap_SetCppPtrRef(this->font, font_class_ptr);
34  }
35 
36  FontFace() : font(nullptr), ptr(nullptr), ptr_sz(0), dispose_fn(&EmptyFontFaceDisposingFunction) {}
37 
38  inline bool IsSourceValid()
39  {
40  // AKA - is the base ptr and size valid?
41  if(this->ptr != nullptr)
42  {
43  if(this->ptr_sz > 0) return true;
44  }
45  return false;
46  }
47 
48  void DisposeFont()
49  {
50  if(this->font != nullptr)
51  {
52  TTF_CloseFont(this->font);
53  this->font = nullptr;
54  }
55  }
56 
57  void Dispose()
58  {
59  this->DisposeFont();
60  if(this->IsSourceValid())
61  {
62  (this->dispose_fn)(this->ptr);
63  this->ptr = nullptr;
64  this->ptr_sz = 0;
65  }
66  }
67 
68  };
69 
70  std::vector<std::pair<i32, std::unique_ptr<FontFace>>> font_faces;
71  u32 font_size;
72 
73  public:
74 
75  static constexpr i32 InvalidFontFaceIndex = -1;
76  static constexpr u32 DefaultFontSize = 25;
77 
78  NX_CONSTEXPR bool IsValidFontFaceIndex(i32 index)
79  {
80  return index != InvalidFontFaceIndex;
81  }
82 
83  Font(u32 font_sz);
84  ~Font();
85 
86  i32 LoadFromMemory(void *ptr, size_t size, FontFaceDisposingFunction disp_fn);
87  i32 LoadFromFile(String path);
88  void Unload(i32 font_idx);
89 
91  {
92  return this->font_size;
93  }
94 
95  sdl2::Font FindValidFontFor(char16_t ch);
96  std::pair<u32, u32> GetTextDimensions(String str);
97  SDL_Texture *RenderText(String str, ui::Color color);
98 
99  };
100 }
Font(u32 font_sz)
Definition: ttf_Font.hpp:9
Definition: pu_String.hpp:21
Definition: ui_Types.hpp:24
DECLSPEC void SDLCALL TTF_CloseFont(TTF_Font *font)
i32 LoadFromMemory(void *ptr, size_t size, FontFaceDisposingFunction disp_fn)
SDL_Texture * RenderText(String str, ui::Color color)
Definition: ttf_Font.hpp:18
s32 i32
Definition: pu_Macros.hpp:17
void EmptyFontFaceDisposingFunction(void *)
Definition: ttf_Font.hpp:13
sdl2::Font FindValidFontFor(char16_t ch)
void Unload(i32 font_idx)
i32 LoadFromFile(String path)
std::function< void(void *)> FontFaceDisposingFunction
Definition: ttf_Font.hpp:11
static constexpr i32 InvalidFontFaceIndex
Definition: ttf_Font.hpp:75
u32 GetFontSize()
Definition: ttf_Font.hpp:90
NX_CONSTEXPR bool IsValidFontFaceIndex(i32 index)
Definition: ttf_Font.hpp:78
void TTF_CppWrap_SetCppPtrRef(TTF_Font *font, void *cpp_ptr_ref)
DECLSPEC TTF_Font *SDLCALL TTF_OpenFontRW(SDL_RWops *src, int freesrc, int ptsize)
std::pair< u32, u32 > GetTextDimensions(String str)
static constexpr u32 DefaultFontSize
Definition: ttf_Font.hpp:76
TTF_Font * Font
Definition: sdl2_Types.hpp:14