forked from vdr-projects/vdr-plugin-skinflatplus
-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathfontcache.c
More file actions
280 lines (248 loc) · 10.5 KB
/
Copy pathfontcache.c
File metadata and controls
280 lines (248 loc) · 10.5 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
/*
* Skin flatPlus: A plugin for the Video Disk Recorder
*
* See the README file for copyright information and how to reach the author.
*
* $Id$
*/
#include <vdr/tools.h>
#include <map>
#include <string>
#include "./fontcache.h"
static GlyphMetricsCache &glyphMetricsCache() {
static GlyphMetricsCache s_cache;
return s_cache;
}
cFontCache FontCache; // Global font cache object
cFontCache::~cFontCache() { Clear(); }
void cFontCache::Create() {
#ifdef DEBUGFUNCSCALL
dsyslog("flatPlus: cFontCache::Create()");
#endif
Clear(); // Clear also creates the cache with zero-initialized FontData
}
void cFontCache::Clear() {
#ifdef DEBUGFUNCSCALL
dsyslog("flatPlus: cFontCache::Clear()");
#endif
for (auto &data : FontCache) {
if (data.font) {
delete data.font;
data.font = nullptr;
}
data.name = "";
data.FileName = "";
data.size = 0;
data.height = 0;
data.ascender = 0;
data.StringWidthCache.clear();
data.GlyphSizeCache.clear();
}
m_InsertIndex = 0;
}
cFont* cFontCache::GetFont(const cString &Name, int Size) {
if (isempty(*Name) || Size <= 0) { // Invalid parameters
esyslog("flatPlus: cFontCache::GetFont() Invalid parameters: Name=%s, Size=%d", *Name, Size);
return cFont::CreateFont("DummyFont", 16); // Return dummy font
}
for (const auto &data : FontCache) {
if (isempty(*data.name)) break; // End of cache, insert new font
if ((strcmp(*data.name, *Name) == 0) && data.size == Size && data.font) {
#ifdef DEBUGFONTCACHE
dsyslog("flatPlus: Found in FontCache: Name=%s, Size=%d", *Name, Size);
#endif
return data.font;
}
}
// Font not found in cache, insert it
InsertFont(Name, Size);
auto &lastFont {FontCache[m_InsertIndex > 0 ? m_InsertIndex - 1 : 0]};
return lastFont.font;
}
/**
* @brief Get the font name for a given font file name from the cache
* @param FileName The file name of the font
* @return The font name if found in the cache, otherwise an empty string
*/
cString cFontCache::GetFontName(const char *FileName) const {
#ifdef DEBUGFONTCACHE
dsyslog("flatPlus: cFontCache::GetFontName() '%s'", FileName);
#endif
for (const auto &data : FontCache) {
if (strcmp(*data.FileName, FileName) == 0) {
return data.name; // Return the font name
}
}
esyslog("flatPlus: cFontCache::GetFontName() Font name not found for file: %s", FileName);
// If the font name is not found in the cache, return an empty string
return ""; // Font name not found in cache
}
int cFontCache::GetFontHeight(const cString &Name, int Size) const {
#ifdef DEBUGFONTCACHE
dsyslog("flatPlus: cFontCache::GetFontHeight() Name=%s, Size=%d", *Name, Size);
#endif
for (const auto &data : FontCache) {
if ((strcmp(*data.name, *Name) == 0) && data.size == Size) {
return data.height;
}
}
esyslog("flatPlus: cFontCache::GetFontHeight() Font not found in cache: Name=%s, Size=%d", *Name, Size);
return 0; // Font not found in cache
}
void cFontCache::InsertFont(const cString& Name, int Size) {
#ifdef DEBUGFONTCACHE
dsyslog("flatPlus: cFontCache::InsertFont() Name=%s, Size=%d", *Name, Size);
#endif
if (isempty(*Name) || Size <= 0) return; // Invalid parameters
if (FontCache[m_InsertIndex].font) { // If the slot is already used, delete it
dsyslog("flatPlus: cFontCache::InsertFont() Replacing existing font at index %zu", m_InsertIndex);
delete FontCache[m_InsertIndex].font;
FontCache[m_InsertIndex].font = nullptr;
FontCache[m_InsertIndex].name = "";
FontCache[m_InsertIndex].FileName = "";
FontCache[m_InsertIndex].size = 0;
FontCache[m_InsertIndex].height = 0;
FontCache[m_InsertIndex].StringWidthCache.clear();
FontCache[m_InsertIndex].GlyphSizeCache.clear();
}
// CreateFont() retuns a dummy font on failure
FontCache[m_InsertIndex].font = cFont::CreateFont(*Name, Size);
FontCache[m_InsertIndex].name = Name;
FontCache[m_InsertIndex].FileName = FontCache[m_InsertIndex].font->FontName();
FontCache[m_InsertIndex].size = Size;
FontCache[m_InsertIndex].height = FontCache[m_InsertIndex].font->Height();
#ifdef DEBUGFONTCACHE
dsyslog(" Font '%s' inserted at index %zu", *FontCache[m_InsertIndex].name, m_InsertIndex);
dsyslog(" Font file name: '%s'", *FontCache[m_InsertIndex].FileName);
dsyslog(" Font size: %d, height: %d", FontCache[m_InsertIndex].size, FontCache[m_InsertIndex].height);
#endif
if (++m_InsertIndex >= kMaxFontCache) {
isyslog("flatPlus: cFontCache::InsertFont() Cache overflow, increase kMaxFontCache (%zu)", kMaxFontCache);
m_InsertIndex = 0;
}
}
int cFontCache::GetStringWidth(const cString &Name, int Height, const cString &Text) const {
for (auto &data : FontCache) {
if ((strcmp(*data.name, *Name) == 0) && data.height == Height) {
const std::string TextStr {*Text};
const auto it {data.StringWidthCache.find(TextStr)};
if (it != data.StringWidthCache.end())
return it->second; // Return cached width
if (data.font) {
const int width {data.font->Width(*Text)};
#ifdef DEBUGFONTCACHE
dsyslog("flatPlus: cFontCache::GetStringWidth() Storing in cache: width=%d for font '%s', height=%d, "
"text='%s'",
width, *Name, Height, *Text);
#endif
data.StringWidthCache[TextStr] = width; // Store in cache for future use
return width;
}
}
}
esyslog("flatPlus: cFontCache::GetStringWidth() Font '%s' with height '%d' not found or invalid", *Name, Height);
return 0; // Font not found or invalid
}
int cFontCache::GetCacheCount() const {
int count {0};
for (const auto &data : FontCache) {
if (isempty(*data.name))
break;
++count;
}
return count;
}
int cFontCache::GetSize() const { return FontCache.size(); }
int cFontCache::GetFontAscender(const cString &FontName, int FontSize) {
if (isempty(*FontName) || FontSize <= 0) {
esyslog("flatPlus: cFontCache::GetFontAscender() Invalid parameters: FontName=%s, FontSize=%d", *FontName,
FontSize);
return FontSize;
}
// Check if the ascender value is already cached
for (auto& data : FontCache) {
if ((strcmp(*data.name, *FontName) == 0) && data.size == FontSize) {
if (data.ascender != 0) {
// Return cached ascender value
return data.ascender;
} else {
// Calculate and cache ascender value
GlyphMetricsCache &cache {glyphMetricsCache()};
const auto face {cache.GetFace(*cFont::GetFontFileName(FontName))};
if (!face) {
esyslog("flatPlus: cFontCache::GetFontAscender() FreeType error: Can't find face (Font = %s)",
*cFont::GetFontFileName(FontName));
return FontSize;
}
int ascender {FontSize}; // Default fallback value
if (face->num_fixed_sizes && face->available_sizes) { // Fixed size
ascender = face->available_sizes->height;
} else if (FT_Set_Char_Size(face, FontSize * 64, FontSize * 64, 0, 0) == 0) {
ascender = face->size->metrics.ascender / 64;
} else {
esyslog(
"flatPlus: cFontCache::GetFontAscender() FreeType error during FT_Set_Char_Size (Font = %s)",
*cFont::GetFontFileName(FontName));
}
data.ascender = ascender;
return ascender;
}
}
}
dsyslog("flatPlus: cFontCache::GetFontAscender() Font not found in cache: FontName=%s, FontSize=%d", *FontName,
FontSize);
return FontSize; // Default fallback if font not found
}
/**
* @brief Get the size of a glyph in a given font and font height.
* @param[in] Name The name of the font to use.
* @param[in] CharCode The character code of the glyph to query.
* @param[in] FontHeight The height of the font in pixels.
* @return The size of the glyph in pixels, rounded up to the nearest integer.
* @note This function returns 0 if any error occurs during the execution of the function.
*/
int cFontCache::GetGlyphSize(const cString &Name, const FT_ULong CharCode, const int FontHeight) {
#ifdef DEBUGFONTCACHE
dsyslog("flatPlus: GetGlyphSize() Name=%s, CharCode=%lu, FontHeight=%d", *Name, CharCode, FontHeight);
#endif
for (auto &data : FontCache) {
if ((strcmp(*data.name, *Name) == 0) && data.size == FontHeight) {
const auto it {data.GlyphSizeCache.find({*Name, CharCode, FontHeight})};
if (it != data.GlyphSizeCache.end()) {
#ifdef DEBUGFONTCACHE
dsyslog(" Cache hit: GlyphSize=%d, Name=%s, CharCode=%lu, FontHeight=%d", it->second, *Name, CharCode,
FontHeight);
#endif
// Return cached size
return it->second;
}
GlyphMetricsCache &cache {glyphMetricsCache()};
const cString FontFileName {cFont::GetFontFileName(*Name)};
if (isempty(*FontFileName)) {
esyslog("flatPlus: GetGlyphSize() Error: Font file name is empty for font %s", *Name);
return 0;
}
const auto face {cache.GetFace(*FontFileName)};
if (!face) {
esyslog("flatPlus: GetGlyphSize() Error: Can't find face (Font = %s)", *FontFileName);
return 0;
}
if (FT_Set_Char_Size(face, FontHeight * 64, FontHeight * 64, 0, 0) != 0) {
esyslog("flatPlus: GetGlyphSize() Error: Can't set char size (Font = %s)", *FontFileName);
return 0;
}
const FT_GlyphSlot slot {face->glyph};
if (FT_Load_Glyph(face, FT_Get_Char_Index(face, CharCode), FT_LOAD_DEFAULT) != 0) {
esyslog("flatPlus: GetGlyphSize() Error: Can't load glyph (Font = %s)", *FontFileName);
return 0;
}
const int GlyphSize {static_cast<int>(slot->metrics.height + 63) / 64}; // Round up to nearest integer
#ifdef DEBUGFONTCACHE
dsyslog(" Calculated GlyphSize: %d", GlyphSize);
#endif
data.GlyphSizeCache[{*Name, CharCode, FontHeight}] = GlyphSize;
return GlyphSize;
}
}
return 0;
}