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
|
#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;
/*
* http://madig.github.io/freetype-web/documentation/tutorial/
* 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;
static std::vector<unsigned char> s_PixelBuffer;
static const int s_SizePerPixel = sizeof(unsigned char);
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(LuaBind::VM* vm, DataBuffer* db, 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(Unicode codepoint, int pixelSize)
{
character::Hash hash;
hash.codepoint = codepoint;
hash.size = pixelSize;
return hash;
}
const Character* Font::GetCharacter(character::Unicode codepoint, int pixelSize)
{
character::Hash hash = GetHash(codepoint, pixelSize);
auto iter = m_Characters.find(hash);
if (iter == m_Characters.end())
{
if (RenderCharacter(codepoint, pixelSize))
{
iter = m_Characters.find(hash);
}
else
{
return NULL;
}
}
Assert(iter != m_Characters.end());
return &iter->second;
}
void Font::RenderCharacters(character::Unicode* codepoint, int n, int pixelSize)
{
for (int i = 0; i < n; ++i)
{
RenderCharacter(codepoint[i], pixelSize);
}
}
void Font::RenderCharacters(std::vector<character::Unicode>& codepoint, int pixelSize)
{
int n = codepoint.size();
for (int i = 0; i < n; ++i)
{
RenderCharacter(codepoint[i], pixelSize);
}
}
bool Font::RenderCharacter(character::Unicode codepoint, int pixelSize)
{
character::Hash hash = GetHash(codepoint, pixelSize);
if (m_Characters.count(hash) != 0)
return true;
FT_Set_Pixel_Sizes(m_FTFace, 0, pixelSize);
// bug: 发现有时候渲染的结果是FT_PIXEL_MODE_MONO(1-bits),试过不同的flags组合还是不对,最后手动转换为8-bits
FT_Int32 flags = FT_LOAD_RENDER;
if (FT_Load_Char(m_FTFace, codepoint, flags))
return false;
Character character;
int w = m_FTFace->glyph->bitmap.width;
int h = m_FTFace->glyph->bitmap.rows;
const unsigned char* pixels = m_FTFace->glyph->bitmap.buffer;
if (pixels != NULL) // 有些字体中space、tab没有字形的
{
s_PixelBuffer.resize(w * h);
if (m_FTFace->glyph->bitmap.pixel_mode == FT_PIXEL_MODE_MONO)
{
// 1-bit monochrome
int pitch = m_FTFace->glyph->bitmap.pitch;
for (int y = 0; y < h; ++y)
{
for (int x = 0; x < w; ++x)
{
int index = x + y * w;
s_PixelBuffer[index] = ((pixels[pitch * y + x / 8]) & (1 << (7 - x % 8))) ? 255 : 0;
}
}
}
else if (m_FTFace->glyph->bitmap.pixel_mode)
{
// 8-bit grayscale
memcpy(&s_PixelBuffer[0], pixels, s_SizePerPixel * w * h);
}
TextHelper::print_glyph(&s_PixelBuffer[0], w, h);
GlyphAtals* atlas = RequestAtlas(pixelSize, Vector2(w, h));
Assert(atlas);
Rect rect = GetRenderChartAndMove(atlas, Vector2(w, h));
try
{
atlas->altas->UpdateSubImage(rect, EPixelFormat::PixelFormat_R, EPixelElementType::PixelType_UNSIGNED_BYTE, &s_PixelBuffer[0]);
}
catch (TextureException& e)
{
s_PixelBuffer.clear();
std::string error = e.what();
error = "Render Character Error: " + error;
log_error(e.what());
return false;
}
s_PixelBuffer.clear();
character.atlas = atlas->index;
character.position = rect;
character.bearing = Vector2(m_FTFace->glyph->bitmap_left, m_FTFace->glyph->bitmap_top);
}
else // space、tab
{
character.atlas = FONT_NOT_IN_ATLAS_PLACEHOLDER;
character.position = Rect(0,0,0,0);
character.bearing = Vector2(0, 0);
}
character.advance = m_FTFace->glyph->advance.x * 1/64.f;
m_Characters.insert(std::pair<character::Hash, Character>(hash, character));
return true;
}
const GlyphAtals* Font::GetGlyphAtlas(int index)
{
if (index >= m_Atlases.size())
return NULL;
return &m_Atlases[index];
}
Rect Font::GetRenderChartAndMove(GlyphAtals* atlas, Vector2 preferSize)
{
Rect rect;
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, Vector2 preferSize)
{
GlyphAtals* atlas = NULL;
auto iter = m_AtlasCache.find(pixelSize);
if (iter == m_AtlasCache.end() || !HasEnoughSpace(iter->second, preferSize))
{
Assert(m_Atlases.size() < FONT_NOT_IN_ATLAS_PLACEHOLDER);
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 = 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::Nearest;
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, Vector2 preferSize)
{
if (atlas == NULL)
return false;
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::Unicode Unicode, 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");
}
}
|