Repository navigation
Expand file tree
/
Copy pathtexture.cpp
More file actions
343 lines (298 loc) · 7.65 KB
/
Copy pathtexture.cpp
File metadata and controls
343 lines (298 loc) · 7.65 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
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
341
342
343
#include "texture.h"
#include <algorithm>
#include <vector>
namespace ck {
namespace font {
// 比目标整数大的最小2次幂数
inline uint32_t number_pow2_greater(uint32_t v)
{
uint32_t rt = 1;
while(rt < v) {
rt *= 2;
}
return rt;
}
// 比目标整数小的最大2次幂数
inline uint32_t number_pow2_lesser(uint32_t v)
{
uint32_t rt = number_pow2_greater(v);
auto lv = rt / 2;
if(lv > 1)
return lv;
return rt;
}
// 取离目标整数最近的2次幂数
inline uint32_t number_pow2(uint32_t v)
{
uint32_t rt = number_pow2_greater(v);
auto lv = rt / 2;
if(lv > 1 && v-lv < rt-v)
return lv;
return rt;
}
//////////////////////////////////////////////////////////////////////////////
/// Texture
///
static constexpr Texture::Char LT { '\t' };
static constexpr Texture::Char L0 { '\0' };
static constexpr Texture::Char LN { '\n' };
template<typename C>
inline Texture::CharPtrArray __cs(const Texture &f,const C* str)
{
if (!str) return {};
const auto len = std::char_traits<C>::length(str);
Texture::CharPtrArray ret;
ret.resize(len);
for(int i=0;i<(int)len;++i)
{
ret[i] = &f.c(str[i]);
}
return ret;
}
const Texture::Char &Texture::c(char32_t c) const
{
if (_chrs.empty() || c == '\r')
return L0;
if (c == '\n')
return LN;
if (c == '\t')
return LT;
auto iter = _map.find(c);
if(iter == _map.end())
{
return _chrs.front();
}
return *iter->second;
}
Texture::CharPtrArray Texture::cs(const char *str) const
{
return __cs<char>(*this,str);
}
Texture::CharPtrArray Texture::cs(const wchar_t *str) const
{
return __cs<wchar_t>(*this,str);
}
Texture::CharPtrArray Texture::cs(const char32_t *str) const
{
return __cs<char32_t>(*this,str);
}
Texture::CharPtrArray Texture::css(const std::string &str) const
{
return __cs<char>(*this,str.c_str());
}
Texture::CharPtrArray Texture::css(const std::wstring &str) const
{
return __cs<wchar_t>(*this,str.c_str());
}
Texture::CharPtrArray Texture::css(const std::u32string &str) const
{
return __cs<char32_t>(*this,str.c_str());
}
const Texture::CharArray &Texture::chrs() const
{
return _chrs;
}
const std::vector<void *> &Texture::pages() const
{
return _pages;
}
void Texture::clear()
{
_map.clear();
_chrs.clear();
_pages.clear();
}
////////////////////////////////////////
/// TextureCreator
class __EstimateCreator : public TextureCreator
{
public:
struct info
{
int width = 0;
int count = 0; // 页数
int remain_area = 0; // 最后一页剩下的面积
float remain_ratio = 0; // 最后一页剩下面积的比例
};
public:
using TextureCreator::TextureCreator;
void *newTexture() override {
inf.count++;
inf.remain_area = 0;
inf.remain_ratio = 0;
return (void*)0xffffffff;
}
void perchar(const File &, const Char & c, const DataPtr &, void *) override {
inf.remain_area += (c.width + _spacing) * (c.height + _spacing);
}
info inf;
};
struct yoffset{
uint32_t start = 0, end = 0; // x坐标范围
uint32_t y; // y偏移
};
using yoffset_line = std::vector<yoffset>; // 必须xpos从小到大
inline int find_yoffset(
const yoffset_line& line,
uint32_t start,uint32_t end
)
{
if(end < start)
std::swap(start, end);
uint32_t oy = 0;
bool in_range = false;
for(auto& it : line)
{
if(start >= it.start && start <= it.end)
in_range = true;
if(in_range)
oy = std::max(oy,it.y);
if(end >= it.start && end <= it.end)
break;
}
return oy;
}
TextureCreator::TextureCreator(
uint32_t width,
uint32_t height,
uint8_t spacing
)
: _width(width),_height(height),_spacing(spacing)
{
if(_spacing < 1)
_spacing = 1;
}
int TextureCreator::estimate(
const File &fnt,
uint8_t spacing
)
{
auto increment = fnt.header().maxWidth;
int width = 0; // 起始宽度
{
uint64_t area = 0;
for(auto& it : fnt.chrs())
{
area += (it.width + spacing) * (it.height + spacing);
}
width = int(sqrt(area));
}
if(width < 1)
return 0;
int last_n = 0;
Texture ft;
while (true){
__EstimateCreator est(width,width,spacing);
est.create(fnt,ft);
const auto n = ft._pages.size();
if(n < 1)
return 0;
if(last_n > 1 && n == 1) // 这次能一页放下
{
if(increment < 2)
return width;
else
increment /= 2;
}
else if(last_n == 1 && n != 1 && increment > 2) // 上次能一页放下
increment /= 2;
if(n > 1)
width += increment;
else
width -= increment;
last_n = n;
}
return width;
}
int TextureCreator::estimate(
const File &fnt,
uint8_t spacing,
uint32_t min_width,
uint32_t max_width
)
{
using info = __EstimateCreator::info;
if(max_width < min_width)
std::swap(min_width, max_width);
min_width = number_pow2(min_width);
max_width = number_pow2(max_width);
Texture ft;
std::vector<info> infos;
for(auto w = min_width; w <= max_width; w *= 2)
{
__EstimateCreator eftc(w,w,spacing);
eftc.create(fnt,ft);
eftc.inf.width = w;
eftc.inf.remain_area = w * w - eftc.inf.remain_area;
eftc.inf.remain_ratio = eftc.inf.remain_area / double(w * w);
infos.push_back(eftc.inf);
}
std::sort(infos.begin(),infos.end(),[](auto& a,auto& b){
return a.remain_ratio < b.remain_ratio;
});
if(!infos.empty())
return infos.front().width;
return min_width;
}
bool TextureCreator::create(
const File & fnt,
Texture& out
)
{
out.clear();
const auto& chrs = fnt.chrs();
out._chrs.reserve(chrs.size());
int page = 0;
uint32_t left = _spacing,top = _spacing;
Char chr;
yoffset_line yo_last{{ 0,_width,_spacing }}; // 上一行的y偏移
yoffset_line yo_cur; // 当前行的y偏移
auto& it = chrs.back();
void* texture = newTexture();
for(auto i=chrs.rbegin(); i!=chrs.rend() && texture; ++i)
{
const auto& c = *i;
if(c.width + _spacing * 2 > _width) // 跳过宽度+间隔直接大于纹理宽度的
continue;
if(c.height + _spacing * 2 > _height) // 跳过高度+间隔直接大于纹理高度的
continue;
auto right = left + c.width + _spacing;
if(right > _width)
{
left = _spacing;
right = left + c.width + _spacing;
yo_last = std::move(yo_cur);
}
top = find_yoffset(yo_last,left,right);
auto bottom = top + c.height + _spacing;
if(bottom > _height)
{
out._pages.push_back(texture);
texture = newTexture(); // 新页
++page;
yo_last = {{ 0,_width,_spacing }};
yo_cur.clear();
top = _spacing;
bottom = top + c.height + _spacing;
}
(font::Char&)chr = c;
chr.page = page;
chr.x = left;
chr.y = top;
perchar(fnt,chr,fnt.getData(c),texture);
out._chrs.emplace_back(chr);
yo_cur.emplace_back(yoffset{left,right,bottom});
left = right;
}
out._chrs.shrink_to_fit();
if(!out._chrs.empty() && texture) // 最后一页
out._pages.push_back(texture);
out._map.clear();
for(auto& c : out._chrs)
{
out._map[c.code] = &c;
}
return !out._chrs.empty();
}
}
}