Aether
SDL2 based UI Framework for NX
FontCache.hpp
1 #ifndef AETHER_FONTCACHE_HPP
2 #define AETHER_FONTCACHE_HPP
3 
4 #include "Aether/types/GlyphMetrics.hpp"
5 #include "Aether/types/LRUCache.hpp"
6 #include <cstddef>
7 #include <mutex>
8 #include <string>
9 #include <vector>
10 
11 // Forward declare all pointers used
12 namespace Aether {
13  class Renderer;
14 }
15 #ifdef __SWITCH__
16 struct PlFontData_;
17 #endif
18 struct SDL_Surface;
19 struct _TTF_Font;
20 typedef struct _TTF_Font TTF_Font;
21 
22 namespace {
23  // Typedef cause this is really long
24  typedef std::tuple<size_t, unsigned int, uint16_t> SurfaceKey;
25 };
26 
27 // Custom hash function for above tuple, required for LRUCache
28 namespace std {
29  template <>
30  struct hash<SurfaceKey> {
31  size_t operator() (const SurfaceKey & k) const {
32  // Format: ....0000XXXXXXXXYYYYYYYYZZZZZZZZZZZZZZZZ
33  // X: index
34  // Y: font size
35  // Z: UTF8 code
36  size_t hash = 0;
37  hash |= std::get<0>(k);
38  hash <<= 8;
39  hash |= std::get<1>(k);
40  hash <<= 16;
41  hash |= std::get<2>(k);
42  return hash;
43  }
44  };
45 };
46 
47 namespace Aether {
55  class FontCache {
56  private:
57  std::string customFontPath;
58  #ifdef __SWITCH__
59  PlFontData_ * ninFontData;
60  #endif
61 
62  std::vector< LRUCache<unsigned int, TTF_Font *> * > fontCache;
65  Renderer * renderer;
67  public:
73  FontCache(Renderer * renderer);
74 
78  void empty();
79 
87  void setCustomFont(const std::string & path);
88 
101  SDL_Surface * getGlyph(const uint16_t ch, const unsigned int fontSize);
102 
110  GlyphMetrics getMetrics(const uint16_t ch, const unsigned int fontSize);
111 
115  ~FontCache();
116  };
117 };
118 
119 #endif
Caches SDL_ttf font objects + surfaces to reduce rendering. This class is not thread-safe, all operations should be protected from the outside by a mutex.
Definition: FontCache.hpp:55
Definition: FontCache.hpp:28
Aether&#39;s main renderer instance. It provides all methods relating to drawing directly to the screen...
Definition: Renderer.hpp:30
A basic LRU (least recently used) cache implementation. Also supports a custom callback for when an i...
Definition: LRUCache.hpp:14
Base namespace for all Aether related classes and functions.
Class storing metrics for a font&#39;s character. Designed to be read-only as it is only able to return v...
Definition: GlyphMetrics.hpp:11