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
|
#include "Runtime/GUI/Font.h"
#include "Runtime/Graphics/Shader.h"
#include "Runtime/Debug/Log.h"
#include "Runtime/Graphics/GfxDevice.h"
#include "Runtime/Common/DataBuffer.h"
#include "Runtime/GUI/utf8.h"
#include "Runtime/Utilities/StaticInitiator.h"
#include "Runtime/GUI/UITextMesh.h"
#include "Runtime/Math/Math.h"
static std::vector<character::Unicode>* s_Codepoints;
InitializeStaticVariables([](){
s_Codepoints = new std::vector<character::Unicode>();
});
LUA_BIND_REGISTRY(Font)
{
LUA_BIND_REGISTER_METHODS(state,
{ "New", _New },
{ "GetCharacter", _GetCharacter },
{ "GetCharacters", _GetCharacters },
{ "GetGlyphAtlas", _GetGlyphAtlas }
);
}
LUA_BIND_POSTPROCESS(Font)
{
}
// Font.New(ttfPath, atlasSize, margin, padding)
// Font.New(ttfData, atlasSize, margin, padding)
LUA_BIND_IMPL_METHOD(Font, _New)
{
LUA_BIND_STATE(L);
LUA_BIND_CHECK(L, "STNN!|UTNN!");
Font* font = NULL;
if (state.CheckParams(1, "STNN!"))
{
const char* path = state.GetValue<const char*>(1, "");
TextGeneratingSettings setting;
setting.atlasSize = state.GetValue<Vector2>(2, Vector2(128, 128));
setting.margin = state.GetValue<int>(3, 0);
setting.padding = state.GetValue<int>(4, 0);
try {
font = new Font(state.GetVM(), path, setting);
}
catch (FontException& e)
{
log_error(e.what());
state.PushNil();
return 1;
}
}
else if (state.CheckParams(1, "UTNN!"))
{
DataBuffer* buffer = state.GetUserdata<DataBuffer>(1);
if (buffer == NULL)
{
log_error("Create font failed, DataBuffer is invalid.");
state.PushNil();
return 1;
}
TextGeneratingSettings setting;
setting.atlasSize = state.GetValue<Vector2>(2, Vector2(128, 128));
setting.margin = state.GetValue<int>(3, 0);
setting.padding = state.GetValue<int>(4, 0);
try {
font = new Font(state.GetVM(), buffer, setting);
}
catch (FontException& e)
{
log_error(e.what());
state.PushNil();
return 1;
}
}
font->PushUserdata(state);
return 1;
}
// font:GetCharacter(char, size, encoding)
LUA_BIND_IMPL_METHOD(Font, _GetCharacter)
{
LUA_BIND_PREPARE(L, Font);
return 1;
}
// font:GetCharacter(str, size, wordwrap, preferredWidth, encoding)
LUA_BIND_IMPL_METHOD(Font, _GetCharacters)
{
LUA_BIND_PREPARE(L, Font);
//LUA_BIND_CHECK(L, "US");
char* buf = (char*)state.GetValue<const char*>(2, "");
int size = state.GetValue<int>(3, 12);
bool wordwrap = state.GetValue<bool>(4, false);
float preferred = state.GetValue<float>(5, 0);
int encoding = state.GetValue<int>(6, EEncoding::Encoding_UTF8);
s_Codepoints->clear();
if (encoding == EEncoding::Encoding_UTF8)
{
while (*buf != 0) {
int err;
s_Codepoints->push_back(utf8::getu8c(&buf, &err));
if (err != 0)
{
log_warning("Illegal utf8 bytes %d", err);
}
}
}
else if (encoding == EEncoding::Encoding_UTF16)
{
while (*buf != 0) {
unsigned short* s = (unsigned short*)(buf);
s_Codepoints->push_back(*s);
buf += 2;
}
}
else if (encoding == EEncoding::Encoding_ASCII)
{
while (*buf != 0) {
s_Codepoints->push_back(*buf);
buf += 1;
}
}
self->RenderCharacters(*s_Codepoints, size);
UnicodeString str;
str.str = s_Codepoints->data();
str.length = s_Codepoints->size();
WipeGLError();
UITextMesh* tm = new UITextMesh(str, self, size, size + 3, TextAnchor_UpperLeft, TextAlignment_Left, wordwrap, preferred);
tm->Draw();
return 0;
}
// font:GetAtlas(i)
// return lightuserdata
LUA_BIND_IMPL_METHOD(Font, _GetGlyphAtlas)
{
LUA_BIND_PREPARE(L, Font);
//LUA_BIND_CHECK(L, "UN");
//int i = state.GetValue<int>(2, 0);
//GlyphAtals atlas = self->m_Atlases[i];
//state.Push((const void*)atlas.altas);
return 1;
}
|