-
Notifications
You must be signed in to change notification settings - Fork 38
/
App.cpp
367 lines (310 loc) · 13.3 KB
/
App.cpp
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
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
#include "App.h"
#include "ProgramOptions.h"
#include <string>
#include <iomanip>
#include "FontInfo.h"
#include "utils/extractFileName.h"
#include "external/lodepng/lodepng.h"
#include "utils/getNumberLen.h"
//TODO: read .bmfc files (BMFont configuration file)
std::vector<rbp::RectSize> App::getGlyphRectangles(const Glyphs &glyphs, const std::uint32_t additionalWidth, const std::uint32_t additionalHeight, const Config& config)
{
std::vector<rbp::RectSize> result;
for (const auto& kv : glyphs)
{
const auto& glyphInfo = kv.second;
if (!glyphInfo.isEmpty()) {
auto width = glyphInfo.width + additionalWidth;
auto height = glyphInfo.height + additionalHeight;
width = ((width + config.alignment.hor - 1) / config.alignment.hor) * config.alignment.hor;
height = ((height + config.alignment.ver - 1) / config.alignment.ver) * config.alignment.ver;
result.emplace_back(width, height, kv.first);
}
}
return result;
}
App::Glyphs App::collectGlyphInfo(const ft::Font& font, const std::set<std::uint32_t>& codes)
{
Glyphs result;
for (const auto& id : codes)
{
GlyphInfo glyphInfo;
if (font.isGlyphProvided(id))
{
ft::Font::GlyphMetrics glyphMetrics = font.renderGlyph(nullptr, 0, 0, 0, 0, id, 0);
glyphInfo.width = glyphMetrics.width;
glyphInfo.height = glyphMetrics.height;
glyphInfo.xAdvance = glyphMetrics.horiAdvance;
glyphInfo.xOffset = glyphMetrics.horiBearingX;
glyphInfo.yOffset = font.ascent - glyphMetrics.horiBearingY;
result[id] = glyphInfo;
}
else
{
std::cout << "warning: glyph " << id << " not found";
if (id == 65279)
std::cout << " (it looks like Unicode byte order mark (BOM))";
std::cout << "." << std::endl;
}
}
return result;
}
std::vector<Config::Size> App::arrangeGlyphs(Glyphs& glyphs, const Config& config)
{
const auto additionalWidth = config.spacing.hor + config.padding.left + config.padding.right;
const auto additionalHeight = config.spacing.ver + config.padding.up + config.padding.down;
std::vector<Config::Size> result;
auto glyphRectangles = getGlyphRectangles(glyphs, additionalWidth, additionalHeight, config);
rbp::MaxRectsBinPack mrbp;
for (;;)
{
std::vector<rbp::Rect> arrangedRectangles;
auto glyphRectanglesCopy = glyphRectangles;
Config::Size lastSize;
uint64_t allGlyphSquare = 0;
for (const auto& i : glyphRectangles)
allGlyphSquare += static_cast<uint64_t>(i.width) * i.height;
for (size_t i = 0; i < config.textureSizeList.size(); ++i)
{
const auto& ss = config.textureSizeList[i];
//TODO: check workAreaW,H
const auto workAreaW = ss.w - config.spacing.hor;
const auto workAreaH = ss.h - config.spacing.ver;
uint64_t textureSquare = static_cast<uint64_t>(workAreaW) * workAreaH;
if (textureSquare < allGlyphSquare && i + 1 < config.textureSizeList.size())
continue;
lastSize = ss;
glyphRectangles = glyphRectanglesCopy;
mrbp.Init(workAreaW, workAreaH);
mrbp.Insert(glyphRectangles, arrangedRectangles, rbp::MaxRectsBinPack::RectBestAreaFit);
if (glyphRectangles.empty())
break;
}
if (arrangedRectangles.empty())
{
if (!glyphRectangles.empty())
throw std::runtime_error("can not fit glyphs into texture");
break;
}
std::uint32_t maxX = 0;
std::uint32_t maxY = 0;
for (const auto& r: arrangedRectangles)
{
std::uint32_t x = r.x + config.spacing.hor;
std::uint32_t y = r.y + config.spacing.ver;
glyphs[r.tag].x = x;
glyphs[r.tag].y = y;
glyphs[r.tag].page = static_cast<std::uint32_t>(result.size());
if (maxX < x + r.width)
maxX = x + r.width;
if (maxY < y + r.height)
maxY = y + r.height;
}
if (config.cropTexturesWidth)
lastSize.w = maxX;
if (config.cropTexturesHeight)
lastSize.h = maxY;
result.push_back(lastSize);
}
return result;
}
void App::savePng(const std::string& fileName, const std::uint32_t* buffer, const std::uint32_t w, const std::uint32_t h, const bool withAlpha)
{
std::vector<std::uint8_t> png;
lodepng::State state;
state.encoder.add_id = 0; // Don't add LodePNG version chunk to save more bytes
state.encoder.auto_convert = 0;
state.info_png.color.colortype = withAlpha ? LCT_RGBA : LCT_RGB;
state.encoder.zlibsettings.windowsize = 32768; // Use maximum possible window size for best compression
auto error = lodepng::encode(png, reinterpret_cast<const unsigned char*>(buffer), w, h, state);
if (error)
throw std::runtime_error("png encoder error " + std::to_string(error) + ": " + lodepng_error_text(error));
error = lodepng::save_file(png, fileName);
if (error)
throw std::runtime_error("png save to file error " + std::to_string(error) + ": " + lodepng_error_text(error));
}
std::vector<std::string> App::renderTextures(const Glyphs& glyphs, const Config& config, const ft::Font& font, const std::vector<Config::Size>& pages)
{
std::vector<std::string> fileNames;
if (pages.empty())
return {};
const auto pageNameDigits = getNumberLen(pages.size() - 1);
for (std::uint32_t page = 0; page < pages.size(); ++page)
{
const Config::Size& s = pages[page];
std::vector<std::uint32_t> surface(s.w * s.h, config.color.getBGR());
// Render every glyph
//TODO: do not repeat same glyphs (with same index)
for (const auto& kv: glyphs)
{
const auto& glyph = kv.second;
if (glyph.page != page)
continue;
if (!glyph.isEmpty())
{
const auto x = glyph.x + config.padding.left;
const auto y = glyph.y + config.padding.up;
font.renderGlyph(&surface[0], s.w, s.h, x, y,
kv.first, config.color.getBGR());
}
}
if (!config.backgroundTransparent)
{
auto cur = surface.data();
const auto end = &surface.back();
const auto fgColor = config.color.getBGR();
const auto bgColor = config.backgroundColor.getBGR();
while (cur <= end)
{
const std::uint32_t a0 = (*cur) >> 24u;
const std::uint32_t a1 = 256 - a0;
const std::uint32_t rb1 = (a1 * (bgColor & 0xFF00FFu)) >> 8u;
const std::uint32_t rb2 = (a0 * (fgColor & 0xFF00FFu)) >> 8u;
const std::uint32_t g1 = (a1 * (bgColor & 0x00FF00u)) >> 8u;
const std::uint32_t g2 = (a0 * (fgColor & 0x00FF00u)) >> 8u;
*cur = ((rb1 | rb2) & 0xFF00FFu) + ((g1 | g2) & 0x00FF00u);
++cur;
}
}
std::stringstream ss;
ss << config.output;
if (config.textureNameSuffix != Config::TextureNameSuffix::None)
{
ss << "_";
if (config.textureNameSuffix == Config::TextureNameSuffix::IndexAligned)
ss << std::setfill ('0') << std::setw(pageNameDigits);
ss << page;
}
ss << ".png";
const auto fileName = ss.str();
fileNames.push_back(extractFileName(fileName));
savePng(fileName, &surface[0], s.w, s.h, config.backgroundTransparent);
}
return fileNames;
}
void App::writeFontInfoFile(const Glyphs& glyphs, const Config& config, const ft::Font& font,
const std::vector<std::string>& fileNames, const std::vector<Config::Size>& pages)
{
if (!fileNames.empty())
for (size_t i = 0; i < fileNames.size() - 1; ++i)
for (size_t k = i + 1; k < fileNames.size(); ++k)
if (fileNames[i] == fileNames[k])
throw std::runtime_error("textures have the same names");
bool pagesHaveDifferentSize = false;
if (pages.size() > 1)
{
for (size_t i = 1; i < pages.size(); ++i)
{
if (pages[0].w != pages[i].w || pages[0].h != pages[i].h)
{
pagesHaveDifferentSize = true;
break;
}
}
}
FontInfo f;
f.info.face = font.getFamilyNameOr("unknown");
f.info.size = -static_cast<std::int16_t>(config.fontSize);
f.info.smooth = config.monochrome;
f.info.unicode = true;
f.info.bold = font.isBold();
f.info.italic = font.isItalic();
f.info.stretchH = 100;
f.info.aa = 1;
f.info.padding.up = static_cast<std::uint8_t>(config.padding.up);
f.info.padding.right = static_cast<std::uint8_t>(config.padding.right);
f.info.padding.down = static_cast<std::uint8_t>(config.padding.down);
f.info.padding.left = static_cast<std::uint8_t>(config.padding.left);
f.info.spacing.horizontal = static_cast<std::uint8_t>(config.spacing.hor);
f.info.spacing.vertical = static_cast<std::uint8_t>(config.spacing.ver);
f.common.lineHeight = static_cast<std::uint16_t>(font.height);
f.common.base = static_cast<std::uint16_t>(font.ascent);
if (!pagesHaveDifferentSize && !pages.empty())
{
f.common.scaleW = static_cast<std::uint16_t>(pages.front().w);
f.common.scaleH = static_cast<std::uint16_t>(pages.front().h);
}
f.common.alphaChnl = 0;
f.common.redChnl = 4;
f.common.greenChnl = 4;
f.common.blueChnl = 4;
f.common.totalHeight = static_cast<std::uint16_t>(font.totalHeight);
f.pages = fileNames;
for (const auto& kv: glyphs)
{
//TODO: page = 0 for empty glyphs.
const auto &glyph = kv.second;
FontInfo::Char c;
c.id = kv.first;
if (!glyph.isEmpty())
{
c.x = static_cast<std::uint16_t>(glyph.x);
c.y = static_cast<std::uint16_t>(glyph.y);
c.width = static_cast<std::uint16_t>(glyph.width + config.padding.left + config.padding.right);
c.height = static_cast<std::uint16_t>(glyph.height + config.padding.up + config.padding.down);
c.page = static_cast<std::uint8_t>(glyph.page);
c.xoffset = static_cast<std::int16_t>(glyph.xOffset - config.padding.left);
c.yoffset = static_cast<std::int16_t>(glyph.yOffset - config.padding.up);
}
c.xadvance = static_cast<std::int16_t>(glyph.xAdvance);
c.chnl = 15;
f.chars.push_back(c);
}
if (config.kerningPairs != Config::KerningPairs::Disabled)
{
auto chars(config.chars);
ft::Font::KerningMode kerningMode = ft::Font::KerningMode::Basic;
if (config.kerningPairs == Config::KerningPairs::Regular)
kerningMode = ft::Font::KerningMode::Regular;
if (config.kerningPairs == Config::KerningPairs::Extended)
kerningMode = ft::Font::KerningMode::Extended;
for (const auto& ch0 : config.chars)
{
for (const auto& ch1 : chars)
{
const auto k = static_cast<std::int16_t>(font.getKerning(ch0, ch1, kerningMode));
if (k)
{
FontInfo::Kerning kerning;
kerning.first = ch0;
kerning.second = ch1;
kerning.amount = k;
f.kernings.push_back(kerning);
}
}
}
}
f.extraInfo = config.extraInfo;
const auto dataFileName = config.output + ".fnt";
switch (config.dataFormat) {
case Config::DataFormat::Xml:
f.writeToXmlFile(dataFileName);
break;
case Config::DataFormat::Text:
f.writeToTextFile(dataFileName);
break;
case Config::DataFormat::Bin:
f.writeToBinFile(dataFileName);
break;
case Config::DataFormat::Json:
f.writeToJsonFile(dataFileName);
break;
case Config::DataFormat::Cbor:
f.writeToCborFile(dataFileName);
break;
}
}
void App::execute(const int argc, char* argv[])
{
const auto config = ProgramOptions::parseCommandLine(argc, argv);
ft::Library library;
if (config.verbose)
std::cout << "freetype " << library.getVersionString() << "\n";
ft::Font font(library, config.fontFile, config.fontSize, 0, config.monochrome);
auto glyphs = collectGlyphInfo(font, config.chars);
const auto pages = arrangeGlyphs(glyphs, config);
if (config.useMaxTextureCount && pages.size() > config.maxTextureCount)
throw std::runtime_error("too many generated textures (more than --max-texture-count)");
const auto fileNames = renderTextures(glyphs, config, font, pages);
writeFontInfoFile(glyphs, config, font, fileNames, pages);
}