44 auto it = mCache.find(key);
46 if (it != mCache.end()) {
48 it->second.ref_count++;
50 return it->second.ttf_font;
58 TTF_Font* new_font = TTF_OpenFont(full_path.c_str(), key.
size);
61 std::cerr <<
"[FontCache] ERROR: Failed to load font: " << full_path
62 <<
" - " << TTF_GetError() << std::endl;
67 TTF_SetFontStyle(new_font, key.
style);
80 auto it = mCache.find(key);
82 if (it == mCache.end()) {
84 std::cerr <<
"[FontCache] WARNING: Attempted to release non-cached font: "
91 it->second.ref_count--;
93 #ifdef FONT_CACHE_DEBUG
94 std::cout <<
"[FontCache] RELEASE: " << key.
filename
95 <<
" (style=" << key.
style <<
", size=" << key.
size <<
")"
96 <<
" refs=" << it->second.ref_count << std::endl;
100 if (it->second.ref_count <= 0) {
101 if (it->second.ttf_font) {
102 TTF_CloseFont(it->second.ttf_font);
105 #ifdef FONT_CACHE_DEBUG
106 std::cout <<
"[FontCache] DELETED: " << key.
filename
107 <<
" (style=" << key.
style <<
", size=" << key.
size <<
")"
108 <<
" cache_size=" << (mCache.size() - 1) << std::endl;
120 int& cache_hits,
int& cache_misses) {
121 unique_fonts = mCache.size();
125 for (
const auto& pair : mCache) {
126 total_refs += pair.second.ref_count;
129 cache_hits = mCacheHits;
130 cache_misses = mCacheMisses;
138 std::cout <<
"\n=== Font Cache Contents ===" << std::endl;
139 std::cout <<
"Unique fonts in cache: " << mCache.size() << std::endl;
140 std::cout <<
"Total cache hits: " << mCacheHits << std::endl;
141 std::cout <<
"Total cache misses: " << mCacheMisses << std::endl;
143 if (mCache.empty()) {
144 std::cout <<
"(cache is empty)" << std::endl;
146 std::cout <<
"\nCached fonts:" << std::endl;
147 for (
const auto& pair : mCache) {
148 std::cout <<
" " << pair.first.filename
149 <<
" (style=" << pair.first.style
150 <<
", size=" << pair.first.size
151 <<
") refs=" << pair.second.ref_count << std::endl;
154 std::cout <<
"==========================\n" << std::endl;
161FontCacheManager::~FontCacheManager() {
162 #ifdef FONT_CACHE_DEBUG
163 if (!mCache.empty()) {
164 std::cout <<
"[FontCache] Destructor: Cleaning up " << mCache.size()
165 <<
" remaining fonts" << std::endl;
170 for (
auto& pair : mCache) {
171 if (pair.second.ttf_font) {
172 TTF_CloseFont(pair.second.ttf_font);
175 #ifdef FONT_CACHE_DEBUG
176 std::cout <<
"[FontCache] Destructor: Closed " << pair.first.filename
177 <<
" (refs=" << pair.second.ref_count <<
")" << std::endl;
static FontCacheManager & GetInstance()
Get singleton instance.
void ReleaseFont(const FontCacheKey &key)
TTF_Font * GetFont(const FontCacheKey &key, const std::string &full_path)
void PrintCache()
Print cache contents to stdout (for debugging)
void GetStats(int &unique_fonts, int &total_refs, int &cache_hits, int &cache_misses)
bool operator<(const FontCacheKey &other) const
Comparison operator for std::map ordering.
bool operator==(const FontCacheKey &other) const
Equality operator for debugging/testing.