forked from scarsty/kys-cpp
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathFont.cpp
More file actions
176 lines (167 loc) · 4.45 KB
/
Font.cpp
File metadata and controls
176 lines (167 loc) · 4.45 KB
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
#include "Font.h"
#include "OpenCCConverter.h"
#include "PotConv.h"
#include "TextureManager.h"
Font::Font()
{
fontnamec_ = GameUtil::PATH() + "font/chinese.ttf";
fontnamee_ = GameUtil::PATH() + "font/english.ttf";
}
BP_Rect Font::getBoxSize(int textLen, int size, int x, int y)
{
BP_Rect r;
r.x = x - 10;
r.y = y - 3;
r.w = size * textLen / 2 + 20;
r.h = size + 6;
return r;
}
void Font::draw(const std::string& text, int size, int x, int y, BP_Color color, uint8_t alpha)
{
int p = 0;
int char_count = 0;
int s1;
color.a = alpha;
std::string text1;
const std::string* textp = &text;
if (simplified_)
{
text1 = t2s_buffer_[text];
if (text1.empty())
{
text1 = OpenCCConverter::getInstance()->UTF8t2s(text);
t2s_buffer_[text] = text1;
}
textp = &text1;
}
if (stat_message_)
{
s1 = getBufferSize();
}
//auto ss = PotConv::utf8tocp936(text);
while (p < textp->size())
{
int w = size, h = size;
uint32_t c = (uint8_t)textp->data()[p];
p++;
if (c >= 128)
{
c += (uint8_t)textp->data()[p] * 256 + (uint8_t)textp->data()[p + 1] * 256 * 256;
p += 2;
}
if (buffer_[c].count(size) == 0)
{
auto s = std::string((char*)(&c));
buffer_[c][size] = Engine::getInstance()->createTextTexture(fontnamec_, s, size, { 255, 255, 255, 255 });
}
auto tex = buffer_[c][size];
char_count++;
int w1 = w;
int x1 = x;
//Engine::getInstance()->queryTexture(tex, &w, &h);
if (c <= 128)
{
w = size / 2;
Engine::getInstance()->queryTexture(tex, &w1, nullptr);
w1 = (std::min)(w1, w);
x1 += (w - w1) / 2;
}
if (c != 32)
{
Engine::getInstance()->setColor(tex, { uint8_t(color.r / 2), uint8_t(color.g / 2), uint8_t(color.b / 2), color.a });
Engine::getInstance()->renderCopy(tex, x1 + 1, y, w1, h);
Engine::getInstance()->setColor(tex, color);
Engine::getInstance()->renderCopy(tex, x1, y, w1, h);
}
x += w;
}
if (stat_message_)
{
int s = getBufferSize() - s1;
if (s > 0)
{
fmt1::print(" {}/{}, {}, total = {}, t2s buffer = {}\n", s, char_count, size, getBufferSize(), t2s_buffer_.size());
}
}
}
void Font::drawWithBox(const std::string& text, int size, int x, int y, BP_Color color, uint8_t alpha, uint8_t alpha_box)
{
//TextureManager::getInstance()->renderTexture("title", 19, x - 19, y - 3);
//for (int i = 0; i < text.size(); i++)
//{
//TextureManager::getInstance()->renderTexture("title", 20, x + 10 * i, y - 3);
//}
auto r = getBoxSize(getTextDrawSize(text), size, x, y);
TextureManager::getInstance()->renderTexture("title", 126, r, { 255, 255, 255, 255 }, alpha_box);
draw(text, size, x, y, color, alpha);
}
//此处仅接受utf8
void Font::drawText(const std::string& fontname, std::string& text, int size, int x, int y, uint8_t alpha, int align, BP_Color c)
{
if (alpha == 0)
{
return;
}
auto text_t = Engine::getInstance()->createTextTexture(fontname, text, size, c);
if (!text_t)
{
return;
}
Engine::getInstance()->setTextureAlphaMod(text_t, alpha);
BP_Rect rect;
Engine::getInstance()->queryTexture(text_t, &rect.w, &rect.h);
rect.y = y;
switch (align)
{
case BP_ALIGN_LEFT:
rect.x = x;
break;
case BP_ALIGN_RIGHT:
rect.x = x - rect.w;
break;
case BP_ALIGN_MIDDLE:
rect.x = x - rect.w / 2;
break;
}
Engine::getInstance()->renderCopy(text_t, nullptr, &rect);
Engine::getInstance()->destroyTexture(text_t);
}
void Font::clearBuffer()
{
for (auto& f : buffer_)
{
for (auto& s : f.second)
{
Engine::getInstance()->destroyTexture(s.second);
}
}
buffer_.clear();
}
int Font::getBufferSize()
{
int sum = 0;
for (auto& f : buffer_)
{
sum += f.second.size();
}
return sum;
}
int Font::getTextDrawSize(const std::string& text)
{
int len = 0;
for (int i = 0; i < text.size();)
{
uint8_t v = text[i];
if (v < 128)
{
len++;
i++;
}
else
{
len += 2;
i += 3;
}
}
return len;
}