summaryrefslogtreecommitdiff
path: root/Runtime/GUI/Font.cpp
blob: 67a2e3be6f596a493c3de9da6ee93d8156e69e35 (plain)
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
#include "freetype.h"
#include "Font.h"
#include "../Math/Math.h"
#include "Runtime/Debug/Log.h"
#include "Runtime/Utilities/Assert.h"
#include "Runtime/Graphics/ImageData.h"

using namespace character;

//https://unicode-table.com/en/#cjk-unified-ideographs-extension-a
//https://learnopengl.com/In-Practice/Text-Rendering
//https://www.zhihu.com/question/294660079
//https://baike.baidu.com/item/%E5%9F%BA%E6%9C%AC%E5%A4%9A%E6%96%87%E7%A7%8D%E5%B9%B3%E9%9D%A2/10788078
//https://stackoverflow.com/questions/27331819/whats-the-difference-between-a-character-a-code-point-a-glyph-and-a-grapheme

static std::string s_FontError;

Font::Font(std::string path, TextGeneratingSettings settings)
    : LuaBind::NativeClass<Font>()
{
    m_AtlasMargin = settings.margin;
    m_GlyphPadding = settings.padding;
    m_AtlasSize = settings.atlasSize;

    if (FT_Init_FreeType(&m_FTLibrary))
    {
        s_FontError = "Init freetype error. Font path " + path;
        throw FontException(s_FontError.c_str());
    }

    if (FT_New_Face(m_FTLibrary, path.c_str(), 0, &m_FTFace))
    {
        s_FontError = "Create freetype face error. Font path " + path;
        throw FontException(s_FontError.c_str());
    }
}

Font::Font(LuaBind::VM* vm, std::string path, TextGeneratingSettings settings)
    : LuaBind::NativeClass<Font>(vm)
{
    m_AtlasMargin = settings.margin;
    m_GlyphPadding = settings.padding;
    m_AtlasSize = settings.atlasSize;

    if (FT_Init_FreeType(&m_FTLibrary))
    {
        s_FontError = "Init freetype error. Font path " + path;
        throw FontException(s_FontError.c_str());
    }

    if (FT_New_Face(m_FTLibrary, path.c_str(), 0, &m_FTFace))
    {
        s_FontError = "Create freetype face error. Font path " + path;
        throw FontException(s_FontError.c_str());
    }
}

Font::Font(DataBuffer* db, TextGeneratingSettings settings)
    : LuaBind::NativeClass<Font>()
{
    m_AtlasMargin = settings.margin;
    m_GlyphPadding = settings.padding;
    m_AtlasSize = settings.atlasSize;

    if (FT_Init_FreeType(&m_FTLibrary))
    {
        s_FontError = "Init freetype error.";
        throw FontException(s_FontError.c_str());
    }

    if (FT_New_Memory_Face(m_FTLibrary, db->udata, db->length, 0, &m_FTFace))
    {
        s_FontError = "Create freetype face error.";
        throw FontException(s_FontError.c_str());
    }
}

Font::Font(DataBuffer* db, LuaBind::VM* vm, std::string path, TextGeneratingSettings settings)
    : LuaBind::NativeClass<Font>(vm)
{
    m_AtlasMargin = settings.margin;
    m_GlyphPadding = settings.padding;
    m_AtlasSize = settings.atlasSize;

    if (FT_Init_FreeType(&m_FTLibrary))
    {
        s_FontError = "Init freetype error.";
        throw FontException(s_FontError.c_str());
    }

    if (FT_New_Memory_Face(m_FTLibrary, db->udata, db->length, 0, &m_FTFace))
    {
        s_FontError = "Create freetype face error.";
        throw FontException(s_FontError.c_str());
    }
}

character::Hash Font::GetHash(Codepoint codepoint, int pixelSize)
{
	character::Hash hash;
	hash.codepoint = codepoint;
	hash.size = pixelSize;
	return hash;
}

const Character* Font::GetCharacter(character::Codepoint codepoint, int pixelSize)
{
	character::Hash hash = GetHash(codepoint, pixelSize);
	auto iter = m_Characters.find(hash);
	if (iter == m_Characters.end())
	{
		RenderCharacter(codepoint, pixelSize);
		iter = m_Characters.find(hash);
	}
	Assert(iter != m_Characters.end());
	return &iter->second;
}

void Font::RenderCharacters(character::Codepoint* codepoint, int n, int pixelSize)
{
	for (int i = 0; i < n; ++i)
	{
		RenderCharacter(codepoint[i], pixelSize);
	}
}

void Font::RenderCharacter(character::Codepoint codepoint, int pixelSize)
{
	character::Hash hash = GetHash(codepoint, pixelSize);
	if (m_Characters.count(hash) != 0)
		return; 
	FT_Set_Pixel_Sizes(m_FTFace, 0, pixelSize);
	if (FT_Load_Char(m_FTFace, codepoint, FT_LOAD_RENDER))
	{
		return;
	}

	int w = m_FTFace->glyph->bitmap.width;
	int h = m_FTFace->glyph->bitmap.rows;

	//TextHelper::print_glyph(m_FTFace->glyph->bitmap.buffer, w, h);

	GlyphAtals* atlas = RequestAtlas(pixelSize, Internal::Vector2(w, h));
	Assert(atlas);

	Internal::Rect rect = GetRenderChartAndMove(atlas, Internal::Vector2(w, h));

	try
	{
		atlas->altas->UpdateSubImage(rect, EPixelFormat::PixelFormat_R, EPixelElementType::PixelType_UNSIGNED_BYTE, m_FTFace->glyph->bitmap.buffer);
	}
	catch (TextureException& e)
	{
		std::string error = e.what();
		error = "Render Character Error: " + error;
		log_error(e.what());
		return;
	}

	Character character;
	character.atlas = atlas->index;
	character.position = rect;
	character.bearing = Internal::Vector2(m_FTFace->glyph->bitmap_left, m_FTFace->glyph->bitmap_top);
	character.advance = m_FTFace->glyph->advance.x * 1/64.f;

	m_Characters.insert(std::pair<character::Hash, Character>(hash, character));
}

const GlyphAtals* Font::GetGlyphAtlas(int index)
{
	if (index >= m_Atlases.size())
		return NULL;
	return &m_Atlases[index];
}

Internal::Rect Font::GetRenderChartAndMove(GlyphAtals* atlas, Internal::Vector2 preferSize)
{
	Internal::Rect rect;
	Internal::Vector2 space;
	space.x = atlas->width - atlas->cursor.x - m_AtlasMargin;
	space.y = atlas->height - atlas->cursor.y - m_AtlasMargin;
	if (space.x > preferSize.x && space.y > preferSize.y)
	{
		rect.x = atlas->cursor.x;
		rect.y = atlas->cursor.y;
		rect.width = preferSize.x;
		rect.height = preferSize.y;
		atlas->cursor.x += rect.width + m_GlyphPadding;
		atlas->rowHeight = max(atlas->rowHeight, preferSize.y);
	}
	else if (space.y - atlas->rowHeight - m_GlyphPadding - m_AtlasMargin > preferSize.y)
	{
		rect.x = m_AtlasMargin;
		rect.y = atlas->cursor.y + m_GlyphPadding + atlas->rowHeight;
		rect.width = preferSize.x;
		rect.height = preferSize.y;
		atlas->cursor.x = m_AtlasMargin;
		atlas->cursor.y += atlas->rowHeight + m_GlyphPadding;
		atlas->rowHeight = rect.height;
	}
	else
	{
		Assert(false);
	}
	return rect;
}

GlyphAtals* Font::RequestAtlas(int pixelSize, Internal::Vector2 preferSize)
{
	GlyphAtals* atlas = NULL;
	auto iter = m_AtlasCache.find(pixelSize);
	if (iter == m_AtlasCache.end() || !HasEnoughSpace(iter->second, preferSize))
	{
		Texture* tex = CreateAtlas();
		GlyphAtals newAtlas = GlyphAtals();
		newAtlas.altas = tex;
		newAtlas.width = m_AtlasSize.x;
		newAtlas.height = m_AtlasSize.y;
		newAtlas.index = m_Atlases.size();
		newAtlas.cursor = Internal::Vector2(m_AtlasMargin, m_AtlasMargin);

		m_Atlases.push_back(newAtlas);
		atlas = &m_Atlases[m_Atlases.size() - 1];

		if (iter != m_AtlasCache.end())
			m_AtlasCache.erase(pixelSize);
		m_AtlasCache.insert(std::pair<int, GlyphAtals*>(pixelSize, atlas));
	}
	else 
	{
		atlas = iter->second;
	}
	Assert(atlas);
	return atlas;
}

Texture* Font::CreateAtlas()
{
	TextureSetting setting; 
	setting.filterMode = ETextureFilterMode::Linear;
	setting.wrapMode = ETextureWrapMode::Clamp;
	setting.format = ETextureFormat::R8;
	setting.type = ETextureType::TEX_2D;
	setting.keepImageData = false;
	Texture* tex = new Texture(setting, m_AtlasSize.x, m_AtlasSize.y);
	return tex;
}

bool Font::HasEnoughSpace(GlyphAtals* atlas, Internal::Vector2 preferSize)
{
	if (atlas == NULL)
		return false;
	Internal::Vector2 space;
	space.x = atlas->width - atlas->cursor.x - m_AtlasMargin;
	space.y = atlas->height - atlas->cursor.y - m_AtlasMargin;
	if (space.x > preferSize.x && space.y > preferSize.y)
		return true;
	if (space.y - atlas->rowHeight - m_GlyphPadding - m_AtlasMargin > preferSize.y)
		return true;
	return false;
}

Character GetCharacter(character::Codepoint Codepoint, int pixelSize)
{
	return Character();
}

void TextHelper::print_glyph(unsigned char* glyph, int width, int height)
{
	for (int r = 0; r < height; ++r)
	{
		for (int c = 0; c < width; ++c)
		{
			unsigned char ch = glyph[c + r * width];
			printf("%c", ch == 0 ? ' ' : '+');
		}
		printf("\n");
	}
}