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
|
#pragma once
#include "drape/pointers.hpp"
#include "drape/texture.hpp"
#include "drape/glyph_manager.hpp"
#include "drape/dynamic_texture.hpp"
#include "std/map.hpp"
#include "std/vector.hpp"
#include "std/string.hpp"
namespace dp
{
class GlyphPacker
{
public:
GlyphPacker(m2::PointU const & size);
bool PackGlyph(uint32_t width, uint32_t height, m2::RectU & rect);
m2::RectF MapTextureCoords(m2::RectU const & pixelRect) const;
bool IsFull() const;
private:
m2::PointU m_size = m2::PointU(0, 0);
m2::PointU m_cursor = m2::PointU(0, 0);
uint32_t m_yStep = 0;
bool m_isFull = false;
};
class GlyphKey : public Texture::Key
{
public:
GlyphKey(strings::UniChar unicodePoint) : m_unicodePoint(unicodePoint) {}
Texture::ResourceType GetType() const { return Texture::Glyph; }
strings::UniChar GetUnicodePoint() const { return m_unicodePoint; }
private:
strings::UniChar m_unicodePoint;
};
class GlyphInfo : public Texture::ResourceInfo
{
typedef Texture::ResourceInfo TBase;
public:
GlyphInfo(m2::RectF const & texRect, GlyphManager::GlyphMetrics const & metrics)
: TBase(texRect)
, m_metrics(metrics)
{
}
virtual Texture::ResourceType GetType() const { return Texture::Glyph; }
GlyphManager::GlyphMetrics const & GetMetrics() const { return m_metrics; }
private:
GlyphManager::GlyphMetrics m_metrics;
};
class GlyphIndex
{
public:
GlyphIndex(m2::PointU size, RefPointer<GlyphManager> mng);
/// can return nullptr
RefPointer<Texture::ResourceInfo> MapResource(GlyphKey const & key);
void UploadResources(RefPointer<Texture> texture);
glConst GetMinFilter() const { return gl_const::GLLinear; }
glConst GetMagFilter() const { return gl_const::GLLinear; }
private:
GlyphPacker m_packer;
RefPointer<GlyphManager> m_mng;
typedef map<strings::UniChar, GlyphInfo> TResourceMapping;
typedef pair<m2::RectU, GlyphManager::Glyph> TPendingNode;
typedef vector<TPendingNode> TPendingNodes;
TResourceMapping m_index;
TPendingNodes m_pendingNodes;
};
class FontTexture : public DynamicTexture<GlyphIndex, GlyphKey, Texture::Glyph>
{
typedef DynamicTexture<GlyphIndex, GlyphKey, Texture::Glyph> TBase;
public:
FontTexture(m2::PointU const & size, RefPointer<GlyphManager> glyphMng)
: m_index(size, glyphMng)
{
TBase::TextureParams params;
params.m_size = size;
params.m_format = TextureFormat::ALPHA;
params.m_minFilter = gl_const::GLLinear;
params.m_magFilter = gl_const::GLLinear;
vector<uint8_t> initData(params.m_size.x * params.m_size.y, 0);
TBase::Init(MakeStackRefPointer(&m_index), params, MakeStackRefPointer<void>(initData.data()));
}
~FontTexture() { TBase::Reset(); }
private:
GlyphIndex m_index;
};
}
|