-
Notifications
You must be signed in to change notification settings - Fork 85
/
Copy pathtb_font_renderer.cpp
508 lines (450 loc) · 14.6 KB
/
tb_font_renderer.cpp
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
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
// ================================================================================
// == This file is a part of Turbo Badger. (C) 2011-2014, Emil Segerås ==
// == See tb_core.h for more information. ==
// ================================================================================
#include "tb_font_renderer.h"
#include "tb_renderer.h"
#include "tb_system.h"
#include "tb_skin.h"
#include <math.h>
namespace tb {
// ================================================================================================
static void blurGlyph(const unsigned char* src, int srcw, int srch, int srcStride, unsigned char* dst, int dstw, int dsth, int dstStride, float* temp, const float* kernel, int kernelRadius)
{
for (int y = 0; y < srch; y++)
{
for (int x = 0; x < dstw; x++)
{
float val = 0;
for (int k_ofs = -kernelRadius; k_ofs <= kernelRadius; k_ofs++)
{
if (x - kernelRadius + k_ofs >= 0 && x - kernelRadius + k_ofs < srcw)
val += src[y * srcStride + x - kernelRadius + k_ofs] * kernel[k_ofs + kernelRadius];
}
temp[y * dstw + x] = val;
}
}
for (int y = 0; y < dsth; y++)
{
for (int x = 0; x < dstw; x++)
{
float val = 0;
for (int k_ofs = -kernelRadius; k_ofs <= kernelRadius; k_ofs++)
{
if (y - kernelRadius + k_ofs >= 0 && y - kernelRadius + k_ofs < srch)
val += temp[(y - kernelRadius + k_ofs) * dstw + x] * kernel[k_ofs + kernelRadius];
}
dst[y * dstStride + x] = (unsigned char)(val + 0.5f);
}
}
}
// ================================================================================================
void TBFontEffect::SetBlurRadius(int blur_radius)
{
assert(blur_radius >= 0);
if (m_blur_radius == blur_radius)
return;
m_blur_radius = blur_radius;
if (m_blur_radius > 0)
{
if (!m_kernel.Reserve(sizeof(float) * (m_blur_radius * 2 + 1)))
{
m_blur_radius = 0;
return;
}
float *kernel = (float *) m_kernel.GetData();
float stdDevSq2 = (float) m_blur_radius / 2.f;
stdDevSq2 = 2.f * stdDevSq2 * stdDevSq2;
float scale = 1.f / sqrtf(3.1415f * stdDevSq2);
float sum = 0;
for (int k = 0; k < 2 * m_blur_radius + 1; k++)
{
float x = (float) (k - m_blur_radius);
float kval = scale * expf(-(x * x / stdDevSq2));
kernel[k] = kval;
sum += kval;
}
for (int k = 0; k < 2 * m_blur_radius + 1; k++)
kernel[k] /= sum;
}
}
TBFontGlyphData *TBFontEffect::Render(TBGlyphMetrics *metrics, const TBFontGlyphData *src)
{
TBFontGlyphData *effect_glyph_data = nullptr;
if (m_blur_radius > 0 && src->data8)
{
// Create a new TBFontGlyphData for the blurred glyph
effect_glyph_data = new TBFontGlyphData;
if (!effect_glyph_data)
return nullptr;
effect_glyph_data->w = src->w + m_blur_radius * 2;
effect_glyph_data->h = src->h + m_blur_radius * 2;
effect_glyph_data->stride = effect_glyph_data->w;
// Reserve memory needed for blurring.
if (!m_data_dst.Reserve(effect_glyph_data->w * effect_glyph_data->h) ||
!m_blur_temp.Reserve(effect_glyph_data->w * effect_glyph_data->h * sizeof(float)))
{
delete effect_glyph_data;
return nullptr;
}
effect_glyph_data->data8 = (uint8*) m_data_dst.GetData();
// Blur!
blurGlyph(src->data8, src->w, src->h, src->stride,
effect_glyph_data->data8, effect_glyph_data->w, effect_glyph_data->h, effect_glyph_data->w,
(float*) m_blur_temp.GetData(), (float*) m_kernel.GetData(), m_blur_radius);
// Adjust glyph position to compensate for larger size.
metrics->x -= m_blur_radius;
metrics->y -= m_blur_radius;
}
return effect_glyph_data;
}
// == TBFontGlyph =================================================================================
TBFontGlyph::TBFontGlyph(const TBID &hash_id, UCS4 cp)
: hash_id(hash_id)
, cp(cp)
, frag(nullptr)
, has_rgb(false)
{
}
// == TBFontGlyphCache ============================================================================
TBFontGlyphCache::TBFontGlyphCache()
{
// Only use one map for the font face. The glyph cache will start forgetting
// glyphs that haven't been used for a while if the map gets full.
m_frag_manager.SetNumMapsLimit(1);
m_frag_manager.SetDefaultMapSize(TB_GLYPH_CACHE_WIDTH, TB_GLYPH_CACHE_HEIGHT);
g_renderer->AddListener(this);
}
TBFontGlyphCache::~TBFontGlyphCache()
{
g_renderer->RemoveListener(this);
}
TBFontGlyph *TBFontGlyphCache::GetGlyph(const TBID &hash_id, UCS4 cp)
{
if (TBFontGlyph *glyph = m_glyphs.Get(hash_id))
{
// Move the glyph to the end of m_all_rendered_glyphs so we maintain LRU (oldest first)
if (m_all_rendered_glyphs.ContainsLink(glyph))
{
m_all_rendered_glyphs.Remove(glyph);
m_all_rendered_glyphs.AddLast(glyph);
}
return glyph;
}
return nullptr;
}
TBFontGlyph *TBFontGlyphCache::CreateAndCacheGlyph(const TBID &hash_id, UCS4 cp)
{
assert(!GetGlyph(hash_id, cp));
TBFontGlyph *glyph = new TBFontGlyph(hash_id, cp);
if (glyph && m_glyphs.Add(glyph->hash_id, glyph))
return glyph;
delete glyph;
return nullptr;
}
TBBitmapFragment *TBFontGlyphCache::CreateFragment(TBFontGlyph *glyph, int w, int h, int stride, uint32 *data)
{
assert(GetGlyph(glyph->hash_id, glyph->cp));
// Don't bother if the requested glyph is too large.
if (w > TB_GLYPH_CACHE_WIDTH || h > TB_GLYPH_CACHE_HEIGHT)
return nullptr;
bool try_drop_largest = true;
bool dropped_large_enough_glyph = false;
do
{
// Attempt creating a fragment for the rendered glyph data
if (TBBitmapFragment *frag = m_frag_manager.CreateNewFragment(glyph->hash_id, false, w, h, stride, data))
{
glyph->frag = frag;
m_all_rendered_glyphs.AddLast(glyph);
return frag;
}
// Drop the oldest glyph that's large enough to free up the space we need.
if (try_drop_largest)
{
const int check_limit = 20;
int check_count = 0;
for (TBFontGlyph *oldest = m_all_rendered_glyphs.GetFirst(); oldest && check_count < check_limit; oldest = oldest->GetNext())
{
if (oldest->frag->Width() >= w && oldest->frag->GetAllocatedHeight() >= h)
{
DropGlyphFragment(oldest);
dropped_large_enough_glyph = true;
break;
}
check_count++;
}
try_drop_largest = false;
}
// We had no large enough glyph so just drop the oldest one. We will likely
// spin around the loop, fail and drop again a few times before we succeed.
if (!dropped_large_enough_glyph)
{
if (TBFontGlyph *oldest = m_all_rendered_glyphs.GetFirst())
DropGlyphFragment(oldest);
else
break;
}
} while (true);
return nullptr;
}
void TBFontGlyphCache::DropGlyphFragment(TBFontGlyph *glyph)
{
assert(glyph->frag);
m_frag_manager.FreeFragment(glyph->frag);
glyph->frag = nullptr;
m_all_rendered_glyphs.Remove(glyph);
}
#ifdef TB_RUNTIME_DEBUG_INFO
void TBFontGlyphCache::Debug()
{
m_frag_manager.Debug();
}
#endif // TB_RUNTIME_DEBUG_INFO
void TBFontGlyphCache::OnContextLost()
{
m_frag_manager.DeleteBitmaps();
}
void TBFontGlyphCache::OnContextRestored()
{
// No need to do anything. The bitmaps will be created when drawing.
}
// ================================================================================================
TBFontFace::TBFontFace(TBFontGlyphCache *glyph_cache, TBFontRenderer *renderer, const TBFontDescription &font_desc)
: m_glyph_cache(glyph_cache), m_font_renderer(renderer), m_font_desc(font_desc), m_bgFont(nullptr), m_bgX(0), m_bgY(0)
{
if (m_font_renderer)
m_metrics = m_font_renderer->GetMetrics();
else
{
// Invent some metrics for the test font
int size = m_font_desc.GetSize();
m_metrics.ascent = size - size / 4;
m_metrics.descent = size / 4;
m_metrics.height = size;
}
}
TBFontFace::~TBFontFace()
{
// It would be nice to drop all glyphs we have live for this font face.
// Now they only die when they get old and kicked out of the cache.
// We currently don't drop any font faces either though (except on shutdown)
delete m_font_renderer;
}
void TBFontFace::SetBackgroundFont(TBFontFace *font, const TBColor &col, int xofs, int yofs)
{
m_bgFont = font;
m_bgX = xofs;
m_bgY = yofs;
m_bgColor = col;
}
bool TBFontFace::RenderGlyphs(const char *glyph_str, int glyph_str_len)
{
if (!m_font_renderer)
return true; // This is the test font
if (glyph_str_len == TB_ALL_TO_TERMINATION)
glyph_str_len = strlen(glyph_str);
bool has_all_glyphs = true;
int i = 0;
while (glyph_str[i] && i < glyph_str_len)
{
UCS4 cp = utf8::decode_next(glyph_str, &i, glyph_str_len);
if (!GetGlyph(cp, true))
has_all_glyphs = false;
}
return has_all_glyphs;
}
TBFontGlyph *TBFontFace::CreateAndCacheGlyph(const TBID &hash_id, UCS4 cp)
{
if (!m_font_renderer)
return nullptr; // This is the test font
// Create the new glyph
TBFontGlyph *glyph = m_glyph_cache->CreateAndCacheGlyph(hash_id, cp);
if (glyph)
m_font_renderer->GetGlyphMetrics(&glyph->metrics, cp);
return glyph;
}
void TBFontFace::RenderGlyph(TBFontGlyph *glyph)
{
assert(!glyph->frag);
TBFontGlyphData glyph_data;
if (m_font_renderer->RenderGlyph(&glyph_data, glyph->cp))
{
TBFontGlyphData *effect_glyph_data = m_effect.Render(&glyph->metrics, &glyph_data);
TBFontGlyphData *result_glyph_data = effect_glyph_data ? effect_glyph_data : &glyph_data;
// The glyph data may be in uint8 format, which we have to convert since we always
// create fragments (and TBBitmap) in 32bit format.
uint32 *glyph_dsta_src = result_glyph_data->data32;
if (!glyph_dsta_src && result_glyph_data->data8)
{
if (m_temp_buffer.Reserve(result_glyph_data->w * result_glyph_data->h * sizeof(uint32)))
{
glyph_dsta_src = (uint32 *) m_temp_buffer.GetData();
for (int y = 0; y < result_glyph_data->h; y++)
for (int x = 0; x < result_glyph_data->w; x++)
{
#ifdef TB_PREMULTIPLIED_ALPHA
uint8 opacity = result_glyph_data->data8[x + y * result_glyph_data->stride];
glyph_dsta_src[x + y * result_glyph_data->w] = TBColor(opacity, opacity, opacity, opacity);
#else
glyph_dsta_src[x + y * result_glyph_data->w] = TBColor(255, 255, 255, result_glyph_data->data8[x + y * result_glyph_data->stride]);
#endif
}
}
}
// Finally, the glyph data is ready and we can create a bitmap fragment.
if (glyph_dsta_src)
{
glyph->has_rgb = result_glyph_data->rgb;
m_glyph_cache->CreateFragment(glyph, result_glyph_data->w, result_glyph_data->h,
result_glyph_data->stride, glyph_dsta_src);
}
delete effect_glyph_data;
}
#ifdef TB_RUNTIME_DEBUG_INFO
//char glyph_str[9];
//int len = utf8::encode(cp, glyph_str);
//glyph_str[len] = 0;
//TBStr info;
//info.SetFormatted("Created glyph %d (\"%s\"). Cache contains %d glyphs (%d%% full) using %d bitmaps.\n", cp, glyph_str, m_all_glyphs.CountLinks(), m_frag_manager.GetUseRatio(), m_frag_manager.GetNumMaps());
//TBDebugOut(info);
#endif
}
TBID TBFontFace::GetHashId(UCS4 cp) const
{
return cp * 31 + m_font_desc.GetFontFaceID();
}
TBFontGlyph *TBFontFace::GetGlyph(UCS4 cp, bool render_if_needed)
{
const TBID &hash_id = GetHashId(cp);
TBFontGlyph *glyph = m_glyph_cache->GetGlyph(hash_id, cp);
if (!glyph)
glyph = CreateAndCacheGlyph(hash_id, cp);
if (glyph && !glyph->frag && render_if_needed)
RenderGlyph(glyph);
return glyph;
}
void TBFontFace::DrawString(int x, int y, const TBColor &color, const char *str, int len)
{
if (m_bgFont)
m_bgFont->DrawString(x+m_bgX, y+m_bgY, m_bgColor, str, len);
if (m_font_renderer)
g_renderer->BeginBatchHint(TBRenderer::BATCH_HINT_DRAW_BITMAP_FRAGMENT);
int i = 0;
while (str[i] && i < len)
{
UCS4 cp = utf8::decode_next(str, &i, len);
if (cp == 0xFFFF)
continue;
if (TBFontGlyph *glyph = GetGlyph(cp, true))
{
if (glyph->frag)
{
TBRect dst_rect(x + glyph->metrics.x, y + glyph->metrics.y + GetAscent(), glyph->frag->Width(), glyph->frag->Height());
TBRect src_rect(0, 0, glyph->frag->Width(), glyph->frag->Height());
if (glyph->has_rgb)
g_renderer->DrawBitmap(dst_rect, src_rect, glyph->frag);
else
g_renderer->DrawBitmapColored(dst_rect, src_rect, color, glyph->frag);
}
x += glyph->metrics.advance;
}
else if (!m_font_renderer) // This is the test font. Use same glyph width as height and draw square.
{
g_tb_skin->PaintRect(TBRect(x, y, m_metrics.height / 3, m_metrics.height), color, 1);
x += m_metrics.height / 3 + 1;
}
}
if (m_font_renderer)
g_renderer->EndBatchHint();
}
int TBFontFace::GetStringWidth(const char *str, int len)
{
int width = 0;
int i = 0;
while (str[i] && i < len)
{
UCS4 cp = utf8::decode_next(str, &i, len);
if (cp == 0xFFFF)
continue;
if (!m_font_renderer) // This is the test font. Use same glyph width as height.
width += m_metrics.height / 3 + 1;
else if (TBFontGlyph *glyph = GetGlyph(cp, false))
width += glyph->metrics.advance;
}
return width;
}
#ifdef TB_RUNTIME_DEBUG_INFO
void TBFontFace::Debug()
{
m_glyph_cache->Debug();
}
#endif // TB_RUNTIME_DEBUG_INFO
// == TBFontManager ===============================================================================
TBFontManager::TBFontManager()
{
// Add the test dummy font with empty name (Equals to ID 0)
AddFontInfo("-test-font-dummy-", "");
m_test_font_desc.SetSize(16);
CreateFontFace(m_test_font_desc);
// Use the test dummy font as default by default
m_default_font_desc = m_test_font_desc;
}
TBFontManager::~TBFontManager()
{
}
TBFontInfo *TBFontManager::AddFontInfo(const char *filename, const char *name)
{
if (TBFontInfo *fi = new TBFontInfo(filename, name))
{
if (m_font_info.Add(fi->GetID(), fi))
return fi;
delete fi;
}
return nullptr;
}
TBFontInfo *TBFontManager::GetFontInfo(const TBID &id) const
{
return m_font_info.Get(id);
}
bool TBFontManager::HasFontFace(const TBFontDescription &font_desc) const
{
return m_fonts.Get(font_desc.GetFontFaceID()) ? true : false;
}
TBFontFace *TBFontManager::GetFontFace(const TBFontDescription &font_desc)
{
if (TBFontFace *font = m_fonts.Get(font_desc.GetFontFaceID()))
return font;
if (TBFontFace *font = m_fonts.Get(GetDefaultFontDescription().GetFontFaceID()))
return font;
return m_fonts.Get(m_test_font_desc.GetFontFaceID());
}
TBFontFace *TBFontManager::CreateFontFace(const TBFontDescription &font_desc)
{
assert(!HasFontFace(font_desc)); // There is already a font added with this description!
TBFontInfo *fi = GetFontInfo(font_desc.GetID());
if (!fi)
return nullptr;
if (fi->GetID() == 0) // Is this the test dummy font
{
if (TBFontFace *font = new TBFontFace(&m_glyph_cache, nullptr, font_desc))
{
if (m_fonts.Add(font_desc.GetFontFaceID(), font))
return font;
delete font;
}
return nullptr;
}
// Iterate through font renderers until we find one capable of creating a font for this file.
for (TBFontRenderer *fr = m_font_renderers.GetFirst(); fr; fr = fr->GetNext())
{
if (TBFontFace *font = fr->Create(this, fi->GetFilename(), font_desc))
{
if (m_fonts.Add(font_desc.GetFontFaceID(), font))
return font;
delete font;
}
}
return nullptr;
}
} // namespace tb