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
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
|
#include "drape/glyph_manager.hpp"
#include "3party/sdf_image/sdf_image.h"
#include "platform/platform.hpp"
#include "coding/reader.hpp"
#include "base/string_utils.hpp"
#include "base/logging.hpp"
#include "base/math.hpp"
#include "base/timer.hpp"
#include <limits>
#include <memory>
#include <set>
#include <sstream>
#include <ft2build.h>
#include FT_TYPES_H
#include FT_SYSTEM_H
#include FT_FREETYPE_H
#include FT_STROKER_H
#include FT_CACHE_H
#ifdef DEBUG
#undef __FTERRORS_H__
#define FT_ERRORDEF(e, v, s) {e, s},
#define FT_ERROR_START_LIST {
#define FT_ERROR_END_LIST {0, 0}};
struct FreetypeError
{
int m_code;
char const * m_message;
};
FreetypeError g_FT_Errors[] =
#include FT_ERRORS_H
#define FREETYPE_CHECK(x) \
do \
{ \
FT_Error const err = (x); \
if (err) \
LOG(LWARNING, ("Freetype:", g_FT_Errors[err].m_code, g_FT_Errors[err].m_message)); \
} while (false)
#define FREETYPE_CHECK_RETURN(x, msg) \
do \
{ \
FT_Error const err = (x); \
if (err) \
{ \
LOG(LWARNING, ("Freetype", g_FT_Errors[err].m_code, g_FT_Errors[err].m_message, msg)); \
return; \
} \
} while (false)
#else
#define FREETYPE_CHECK(x) x
#define FREETYPE_CHECK_RETURN(x, msg) FREETYPE_CHECK(x)
#endif
namespace dp
{
namespace
{
int const kInvalidFont = -1;
template <typename ToDo>
void ParseUniBlocks(string const & uniBlocksFile, ToDo toDo)
{
string uniBlocks;
try
{
ReaderPtr<Reader>(GetPlatform().GetReader(uniBlocksFile)).ReadAsString(uniBlocks);
}
catch (RootException const & e)
{
LOG(LCRITICAL, ("Error reading uniblock description: ", e.what()));
return;
}
std::istringstream fin(uniBlocks);
while (true)
{
string name;
strings::UniChar start;
strings::UniChar end;
fin >> name >> std::hex >> start >> std::hex >> end;
if (!fin)
break;
toDo(name, start, end);
}
}
template <typename ToDo>
void ParseFontList(string const & fontListFile, ToDo toDo)
{
string fontList;
try
{
ReaderPtr<Reader>(GetPlatform().GetReader(fontListFile)).ReadAsString(fontList);
}
catch(RootException const & e)
{
LOG(LWARNING, ("Error reading font list ", fontListFile, " : ", e.what()));
return;
}
std::istringstream fin(fontList);
while (true)
{
string ubName;
string fontName;
fin >> ubName >> fontName;
if (!fin)
break;
toDo(ubName, fontName);
}
}
class Font
{
public:
DECLARE_EXCEPTION(InvalidFontException, RootException);
Font(uint32_t sdfScale, ReaderPtr<Reader> fontReader, FT_Library lib)
: m_fontReader(fontReader)
, m_fontFace(nullptr)
, m_sdfScale(sdfScale)
{
m_stream.base = 0;
m_stream.size = static_cast<unsigned long>(m_fontReader.Size());
m_stream.pos = 0;
m_stream.descriptor.pointer = &m_fontReader;
m_stream.pathname.pointer = 0;
m_stream.read = &Font::Read;
m_stream.close = &Font::Close;
m_stream.memory = 0;
m_stream.cursor = 0;
m_stream.limit = 0;
FT_Open_Args args;
args.flags = FT_OPEN_STREAM;
args.memory_base = 0;
args.memory_size = 0;
args.pathname = 0;
args.stream = &m_stream;
args.driver = 0;
args.num_params = 0;
args.params = 0;
FT_Error const err = FT_Open_Face(lib, &args, 0, &m_fontFace);
#ifdef DEBUG
if (err)
LOG(LWARNING, ("Freetype:", g_FT_Errors[err].m_code, g_FT_Errors[err].m_message));
#endif
if (err || !IsValid())
MYTHROW(InvalidFontException, ());
}
bool IsValid() const
{
return m_fontFace != nullptr && m_fontFace->num_glyphs > 0;
}
void DestroyFont()
{
if (m_fontFace != nullptr)
{
FREETYPE_CHECK(FT_Done_Face(m_fontFace));
m_fontFace = nullptr;
}
}
bool HasGlyph(strings::UniChar unicodePoint) const
{
return FT_Get_Char_Index(m_fontFace, unicodePoint) != 0;
}
GlyphManager::Glyph GetGlyph(strings::UniChar unicodePoint, uint32_t baseHeight, bool isSdf) const
{
uint32_t glyphHeight = isSdf ? baseHeight * m_sdfScale : baseHeight;
FREETYPE_CHECK(FT_Set_Pixel_Sizes(m_fontFace, glyphHeight, glyphHeight));
FREETYPE_CHECK(FT_Load_Glyph(m_fontFace, FT_Get_Char_Index(m_fontFace, unicodePoint), FT_LOAD_RENDER));
FT_Glyph glyph;
FREETYPE_CHECK(FT_Get_Glyph(m_fontFace->glyph, &glyph));
FT_BBox bbox;
FT_Glyph_Get_CBox(glyph, FT_GLYPH_BBOX_PIXELS , &bbox);
FT_Bitmap bitmap = m_fontFace->glyph->bitmap;
float const scale = isSdf ? 1.0f / m_sdfScale : 1.0f;
SharedBufferManager::shared_buffer_ptr_t data;
uint32_t imageWidth = bitmap.width;
uint32_t imageHeight = bitmap.rows;
if (bitmap.buffer != nullptr)
{
if (isSdf)
{
sdf_image::SdfImage img(bitmap.rows, bitmap.pitch, bitmap.buffer, m_sdfScale * kSdfBorder);
imageWidth = img.GetWidth() * scale;
imageHeight = img.GetHeight() * scale;
size_t const bufferSize = bitmap.rows * bitmap.pitch;
data = SharedBufferManager::instance().reserveSharedBuffer(bufferSize);
memcpy(&(*data)[0], bitmap.buffer, bufferSize);
}
else
{
int const border = kSdfBorder;
imageHeight += 2 * border;
imageWidth += 2 * border;
size_t const bufferSize = imageWidth * imageHeight;
data = SharedBufferManager::instance().reserveSharedBuffer(bufferSize);
memset(data->data(), 0, data->size());
for (size_t row = border; row < bitmap.rows + border; ++row)
{
size_t const dstBaseIndex = row * imageWidth + border;
size_t const srcBaseIndex = (row - border) * bitmap.pitch;
for (int column = 0; column < bitmap.pitch; ++column)
data->data()[dstBaseIndex + column] = bitmap.buffer[srcBaseIndex + column];
}
}
}
GlyphManager::Glyph result;
result.m_image = GlyphManager::GlyphImage
{
imageWidth, imageHeight,
bitmap.rows, bitmap.pitch,
data
};
result.m_metrics = GlyphManager::GlyphMetrics
{
static_cast<float>(glyph->advance.x >> 16) * scale,
static_cast<float>(glyph->advance.y >> 16) * scale,
static_cast<float>(bbox.xMin) * scale,
static_cast<float>(bbox.yMin) * scale,
true
};
result.m_code = unicodePoint;
result.m_fixedSize = isSdf ? GlyphManager::kDynamicGlyphSize
: static_cast<int>(baseHeight);
FT_Done_Glyph(glyph);
return result;
}
void GetCharcodes(vector<FT_ULong> & charcodes)
{
FT_UInt gindex;
charcodes.push_back(FT_Get_First_Char(m_fontFace, &gindex));
while (gindex)
charcodes.push_back(FT_Get_Next_Char(m_fontFace, charcodes.back(), &gindex));
sort(charcodes.begin(), charcodes.end());
charcodes.erase(unique(charcodes.begin(), charcodes.end()), charcodes.end());
}
static unsigned long Read(FT_Stream stream, unsigned long offset, unsigned char * buffer, unsigned long count)
{
if (count != 0)
{
ReaderPtr<Reader> * reader = reinterpret_cast<ReaderPtr<Reader> *>(stream->descriptor.pointer);
reader->Read(offset, buffer, count);
}
return count;
}
static void Close(FT_Stream){}
void MarkGlyphReady(strings::UniChar code, int fixedHeight)
{
m_readyGlyphs.insert(make_pair(code, fixedHeight));
}
bool IsGlyphReady(strings::UniChar code, int fixedHeight) const
{
return m_readyGlyphs.find(make_pair(code, fixedHeight)) != m_readyGlyphs.end();
}
private:
ReaderPtr<Reader> m_fontReader;
FT_StreamRec_ m_stream;
FT_Face m_fontFace;
uint32_t m_sdfScale;
std::set<pair<strings::UniChar, int>> m_readyGlyphs;
};
} // namespace
// Information about single unicode block.
struct UnicodeBlock
{
string m_name;
strings::UniChar m_start;
strings::UniChar m_end;
std::vector<int> m_fontsWeight;
UnicodeBlock(string const & name, strings::UniChar start, strings::UniChar end)
: m_name(name)
, m_start(start)
, m_end(end)
{}
int GetFontOffset(int idx) const
{
if (m_fontsWeight.empty())
return kInvalidFont;
int maxWeight = 0;
int upperBoundWeight = std::numeric_limits<int>::max();
if (idx != kInvalidFont)
upperBoundWeight = m_fontsWeight[idx];
int index = kInvalidFont;
ASSERT_LESS(m_fontsWeight.size(), static_cast<size_t>(std::numeric_limits<int>::max()), ());
for (size_t i = 0; i < m_fontsWeight.size(); ++i)
{
int w = m_fontsWeight[i];
if (w < upperBoundWeight && w > maxWeight)
{
maxWeight = w;
index = static_cast<int>(i);
}
}
return index;
}
bool HasSymbol(strings::UniChar sym) const
{
return (m_start <= sym) && (m_end >= sym);
}
};
using TUniBlocks = std::vector<UnicodeBlock>;
using TUniBlockIter = TUniBlocks::const_iterator;
int const GlyphManager::kDynamicGlyphSize = -1;
struct GlyphManager::Impl
{
FT_Library m_library;
TUniBlocks m_blocks;
TUniBlockIter m_lastUsedBlock;
std::vector<std::unique_ptr<Font>> m_fonts;
uint32_t m_baseGlyphHeight;
uint32_t m_sdfScale;
};
GlyphManager::GlyphManager(GlyphManager::Params const & params)
: m_impl(new Impl())
{
m_impl->m_baseGlyphHeight = params.m_baseGlyphHeight;
m_impl->m_sdfScale = params.m_sdfScale;
using TFontAndBlockName = pair<std::string, std::string>;
using TFontLst = buffer_vector<TFontAndBlockName, 64>;
TFontLst whitelst;
TFontLst blacklst;
m_impl->m_blocks.reserve(160);
ParseUniBlocks(params.m_uniBlocks, [this](std::string const & name,
strings::UniChar start, strings::UniChar end)
{
m_impl->m_blocks.push_back(UnicodeBlock(name, start, end));
});
ParseFontList(params.m_whitelist, [&whitelst](std::string const & ubName, std::string const & fontName)
{
whitelst.push_back(TFontAndBlockName(fontName, ubName));
});
ParseFontList(params.m_blacklist, [&blacklst](std::string const & ubName, std::string const & fontName)
{
blacklst.push_back(TFontAndBlockName(fontName, ubName));
});
m_impl->m_fonts.reserve(params.m_fonts.size());
FREETYPE_CHECK(FT_Init_FreeType(&m_impl->m_library));
for (auto const & fontName : params.m_fonts)
{
bool ignoreFont = false;
for_each(blacklst.begin(), blacklst.end(), [&ignoreFont, &fontName](TFontAndBlockName const & p)
{
if (p.first == fontName && p.second == "*")
ignoreFont = true;
});
if (ignoreFont)
continue;
std::vector<FT_ULong> charCodes;
try
{
m_impl->m_fonts.emplace_back(std::make_unique<Font>(params.m_sdfScale, GetPlatform().GetReader(fontName),
m_impl->m_library));
m_impl->m_fonts.back()->GetCharcodes(charCodes);
}
catch(RootException const & e)
{
LOG(LWARNING, ("Error read font file : ", e.what()));
continue;
}
using TBlockIndex = size_t;
using TCharCounter = int;
using TCoverNode = std::pair<TBlockIndex, TCharCounter>;
using TCoverInfo = std::vector<TCoverNode>;
size_t currentUniBlock = 0;
TCoverInfo coverInfo;
for (FT_ULong const & charCode : charCodes)
{
while(currentUniBlock < m_impl->m_blocks.size())
{
if (m_impl->m_blocks[currentUniBlock].HasSymbol(static_cast<strings::UniChar>(charCode)))
break;
++currentUniBlock;
}
if (currentUniBlock >= m_impl->m_blocks.size())
break;
if (coverInfo.empty() || coverInfo.back().first != currentUniBlock)
coverInfo.push_back(make_pair(currentUniBlock, 1));
else
++coverInfo.back().second;
}
using TUpdateCoverInfoFn = std::function<void(UnicodeBlock const & uniBlock, TCoverNode & node)>;
auto enumerateFn = [this, &coverInfo, &fontName] (TFontLst const & lst, TUpdateCoverInfoFn const & fn)
{
for (TFontAndBlockName const & b : lst)
{
if (b.first != fontName)
continue;
for (TCoverNode & node : coverInfo)
{
UnicodeBlock const & uniBlock = m_impl->m_blocks[node.first];
if (uniBlock.m_name == b.second)
{
fn(uniBlock, node);
break;
}
else if (b.second == "*")
fn(uniBlock, node);
}
}
};
enumerateFn(blacklst, [](UnicodeBlock const &, TCoverNode & node)
{
node.second = 0;
});
enumerateFn(whitelst, [this](UnicodeBlock const & uniBlock, TCoverNode & node)
{
node.second = static_cast<int>(uniBlock.m_end + 1 - uniBlock.m_start + m_impl->m_fonts.size());
});
for (TCoverNode & node : coverInfo)
{
UnicodeBlock & uniBlock = m_impl->m_blocks[node.first];
uniBlock.m_fontsWeight.resize(m_impl->m_fonts.size(), 0);
uniBlock.m_fontsWeight.back() = node.second;
}
}
m_impl->m_lastUsedBlock = m_impl->m_blocks.end();
}
GlyphManager::~GlyphManager()
{
for (auto const & f : m_impl->m_fonts)
f->DestroyFont();
FREETYPE_CHECK(FT_Done_FreeType(m_impl->m_library));
delete m_impl;
}
uint32_t GlyphManager::GetBaseGlyphHeight() const
{
return m_impl->m_baseGlyphHeight;
}
uint32_t GlyphManager::GetSdfScale() const
{
return m_impl->m_sdfScale;
}
int GlyphManager::GetFontIndex(strings::UniChar unicodePoint)
{
TUniBlockIter iter = m_impl->m_blocks.end();
if (m_impl->m_lastUsedBlock != m_impl->m_blocks.end() && m_impl->m_lastUsedBlock->HasSymbol(unicodePoint))
{
iter = m_impl->m_lastUsedBlock;
}
else
{
if (iter == m_impl->m_blocks.end() || !iter->HasSymbol(unicodePoint))
{
iter = lower_bound(m_impl->m_blocks.begin(), m_impl->m_blocks.end(), unicodePoint,
[](UnicodeBlock const & block, strings::UniChar const & v)
{
return block.m_end < v;
});
}
}
if (iter == m_impl->m_blocks.end() || !iter->HasSymbol(unicodePoint))
return kInvalidFont;
m_impl->m_lastUsedBlock = iter;
return FindFontIndexInBlock(*m_impl->m_lastUsedBlock, unicodePoint);
}
int GlyphManager::GetFontIndexImmutable(strings::UniChar unicodePoint) const
{
TUniBlockIter iter = lower_bound(m_impl->m_blocks.begin(), m_impl->m_blocks.end(), unicodePoint,
[](UnicodeBlock const & block, strings::UniChar const & v)
{
return block.m_end < v;
});
if (iter == m_impl->m_blocks.end() || !iter->HasSymbol(unicodePoint))
return kInvalidFont;
return FindFontIndexInBlock(*iter, unicodePoint);
}
int GlyphManager::FindFontIndexInBlock(UnicodeBlock const & block, strings::UniChar unicodePoint) const
{
ASSERT(block.HasSymbol(unicodePoint), ());
for (int fontIndex = block.GetFontOffset(kInvalidFont); fontIndex != kInvalidFont;
fontIndex = block.GetFontOffset(fontIndex))
{
ASSERT_LESS(fontIndex, static_cast<int>(m_impl->m_fonts.size()), ());
auto const & f = m_impl->m_fonts[fontIndex];
if (f->HasGlyph(unicodePoint))
return fontIndex;
}
return kInvalidFont;
}
GlyphManager::Glyph GlyphManager::GetGlyph(strings::UniChar unicodePoint, int fixedHeight)
{
int const fontIndex = GetFontIndex(unicodePoint);
if (fontIndex == kInvalidFont)
return GetInvalidGlyph(fixedHeight);
auto const & f = m_impl->m_fonts[fontIndex];
bool const isSdf = fixedHeight < 0;
Glyph glyph = f->GetGlyph(unicodePoint, isSdf ? m_impl->m_baseGlyphHeight : fixedHeight, isSdf);
glyph.m_fontIndex = fontIndex;
return glyph;
}
// static
GlyphManager::Glyph GlyphManager::GenerateGlyph(Glyph const & glyph, uint32_t sdfScale)
{
if (glyph.m_image.m_data != nullptr)
{
GlyphManager::Glyph resultGlyph;
resultGlyph.m_metrics = glyph.m_metrics;
resultGlyph.m_fontIndex = glyph.m_fontIndex;
resultGlyph.m_code = glyph.m_code;
resultGlyph.m_fixedSize = glyph.m_fixedSize;
if (glyph.m_fixedSize < 0)
{
sdf_image::SdfImage img(glyph.m_image.m_bitmapRows, glyph.m_image.m_bitmapPitch,
glyph.m_image.m_data->data(), sdfScale * kSdfBorder);
img.GenerateSDF(1.0f / static_cast<float>(sdfScale));
ASSERT(img.GetWidth() == glyph.m_image.m_width, ());
ASSERT(img.GetHeight() == glyph.m_image.m_height, ());
size_t bufferSize = base::NextPowOf2(glyph.m_image.m_width * glyph.m_image.m_height);
resultGlyph.m_image.m_data = SharedBufferManager::instance().reserveSharedBuffer(bufferSize);
img.GetData(*resultGlyph.m_image.m_data);
}
else
{
size_t bufferSize = base::NextPowOf2(glyph.m_image.m_width * glyph.m_image.m_height);
resultGlyph.m_image.m_data = SharedBufferManager::instance().reserveSharedBuffer(bufferSize);
resultGlyph.m_image.m_data->assign(glyph.m_image.m_data->begin(), glyph.m_image.m_data->end());
}
resultGlyph.m_image.m_width = glyph.m_image.m_width;
resultGlyph.m_image.m_height = glyph.m_image.m_height;
resultGlyph.m_image.m_bitmapRows = 0;
resultGlyph.m_image.m_bitmapPitch = 0;
return resultGlyph;
}
return glyph;
}
void GlyphManager::ForEachUnicodeBlock(GlyphManager::TUniBlockCallback const & fn) const
{
for (UnicodeBlock const & uni : m_impl->m_blocks)
fn(uni.m_start, uni.m_end);
}
void GlyphManager::MarkGlyphReady(Glyph const & glyph)
{
ASSERT_GREATER_OR_EQUAL(glyph.m_fontIndex, 0, ());
ASSERT_LESS(glyph.m_fontIndex, static_cast<int>(m_impl->m_fonts.size()), ());
m_impl->m_fonts[glyph.m_fontIndex]->MarkGlyphReady(glyph.m_code, glyph.m_fixedSize);
}
bool GlyphManager::AreGlyphsReady(strings::UniString const & str, int fixedSize) const
{
for (auto const & code : str)
{
int const fontIndex = GetFontIndexImmutable(code);
if (fontIndex == kInvalidFont)
continue;
if (!m_impl->m_fonts[fontIndex]->IsGlyphReady(code, fixedSize))
return false;
}
return true;
}
GlyphManager::Glyph GlyphManager::GetInvalidGlyph(int fixedSize) const
{
strings::UniChar const kInvalidGlyphCode = 0x9;
int const kFontId = 0;
static bool s_inited = false;
static Glyph s_glyph;
if (!s_inited)
{
ASSERT(!m_impl->m_fonts.empty(), ());
bool const isSdf = fixedSize < 0 ;
s_glyph = m_impl->m_fonts[kFontId]->GetGlyph(kInvalidGlyphCode,
isSdf ? m_impl->m_baseGlyphHeight : fixedSize,
isSdf);
s_glyph.m_metrics.m_isValid = false;
s_glyph.m_fontIndex = kFontId;
s_glyph.m_code = kInvalidGlyphCode;
s_inited = true;
}
return s_glyph;
}
} // namespace dp
|