-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathXFreeTypeConsole.cpp
517 lines (439 loc) · 13.5 KB
/
XFreeTypeConsole.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
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
///////////////////////////////////////
// Author: phantom1020, ForserX
// Desc : Font converter
///////////////////////////////////////
// Oxygen Engine 2.0
///////////////////////////////////////
#pragma unmanaged // No CLR, Please!
#include <fstream>
#include "Kernel.h"
#include "stb_image_write.h"
bool XRay::CFontGen::bSucFile;
bool XRay::CFontGen::bSucDir;
HANDLE XRay::hConsole = GetStdHandle(STD_OUTPUT_HANDLE);
XRay::CFontGen::CFontGen()
{
bSucFile = false;
bSucDir = false;
}
// @ If method ManageCreationFile is successful processing
void XRay::CFontGen::InitFreeType()
{
FT_Error err = FT_Init_FreeType(&ConverterInfo.lib);
if (err)
{
MessageBox(NULL, L"Error!", L"Failed init FreeType!", MB_OK);
}
}
void XRay::CFontGen::ReleaseFreeType()
{
FT_Done_Face(ConverterInfo.face);
FT_Done_FreeType(ConverterInfo.lib);
}
int g_count = 0;
int max_height_font = 0; // @ äëÿ âûâîäà ôàéëîâ, ïî ñóòè îáìàíêà,
// èáî â äåéñòâèòåëüíîñòè ìû óìåíüøàåì ðàçìåð øðèôòà è âûâîäèòñÿ
//äîëæíî ïîä êàæäûé ðàçìåð, íî ìû äåëàåì êàê ÏÛÑ, òî åñòü _size_800, _size_1600, _size, ãäå size îäíî è òî æå ÷èñëî
void XRay::CFontGen::ParseFont(int index, int max_value)
{
g_count++;
ConverterInfo.FontHeig = index;
FT_Error err = FT_Set_Pixel_Sizes(ConverterInfo.face, 0, index);
if (err)
{
std::cout << "FAILED" << std::endl;
}
FT_Load_Char(ConverterInfo.face, 87, FT_LOAD_RENDER);
info_copy.y_off = ConverterInfo.face->glyph->bitmap_top;
float max_dm = ((1 + (ConverterInfo.face->size->metrics.height >> 6)) * ceilf(sqrtf(TOTAL_ANSCII)));
int current_state = (1 + (ConverterInfo.face->size->metrics.height >> 6));
// @ Ïðîñ÷èòûâàåì ðàçìåð òåêñòóðû óìíîæàÿ íà 2
while (ConverterInfo.TexWid < max_dm)
ConverterInfo.TexWid <<= 1;
ConverterInfo.TexHeig = ConverterInfo.TexWid;
if (!ConverterInfo.Pixels)
{
ConverterInfo.Pixels = (u8*)calloc(ConverterInfo.TexWid * ConverterInfo.TexHeig, 1);
}
if (index <= 0)
{
MessageBoxA(0, "Can't create file, because your size is less or equal is nullptr", "Error!", MB_OK);
return;
}
int pen_x = 0;
int pen_y = 0;
u32 local_tex_width = ConverterInfo.TexWid;
int arr_iter = 0;
int index_to_W = 0;
int current_size = 0;
for (u32 i = 32; i < 128; ++i)
{
err = FT_Load_Char(ConverterInfo.face, i, FT_LOAD_RENDER | FT_LOAD_FORCE_AUTOHINT | FT_LOAD_TARGET_LIGHT);
FT_Bitmap& refBMP = ConverterInfo.face->glyph->bitmap;
// Âåëèêèé è óæàñíûé õàê ñ ïðîáåëàìè
if (i == 32 && !refBMP.rows && !refBMP.width)
{
// 0 is true space
err = FT_Load_Char(ConverterInfo.face, 0, FT_LOAD_RENDER | FT_LOAD_FORCE_AUTOHINT | FT_LOAD_TARGET_LIGHT);
FT_Bitmap* pBMP = &ConverterInfo.face->glyph->bitmap;
// Åñëè îïÿòü ïóñòî, òî íàïèøåì ñâîè äàííûå...
if (!pBMP->rows && !pBMP->width)
{
pBMP->width = 9;
pBMP->rows = 7;
}
}
if (err && i != 32)
{
MessageBox(0, L"Your font doesn't have a [unknown] symbol", L"Error!", MB_OK);
}
else
{
if (pen_x + refBMP.width >= local_tex_width)
{
pen_x = 0;
pen_y += ((ConverterInfo.face->size->metrics.height >> 6)) + 1;
}
if (i == 88)
{
index_to_W = arr_iter;
}
// @ +-.,
if (i >= 42 && i <= 46)
{
current_size = int(info_copy.y_off - ConverterInfo.face->glyph->bitmap_top);
pen_y += current_size;
}
// @ äâîåòî÷èå è òî÷êà ñ çàïÿòîé
if (i == 58 || i == 59)
{
current_size = int(info_copy.y_off - ConverterInfo.face->glyph->bitmap_top);
pen_y += current_size;
}
if (i >= 97 && i < 123)
{
current_size = int(info[index_to_W].y_off - ConverterInfo.face->glyph->bitmap_top);
pen_y += current_size;
}
if ((i >= 42 && i <= 46) || i == 58 || i == 59)
{
if ((info_copy.y_off - ConverterInfo.face->glyph->bitmap_top) > 0)
pen_y -= current_size;
else
pen_y += abs(current_size);
}
if (i >= 97 && i < 123)
{
if ((info[index_to_W].y_off - ConverterInfo.face->glyph->bitmap_top) > 0)
pen_y -= current_size;
else
pen_y += abs(current_size);
}
for (u32 row = 0; row < refBMP.rows; ++row)
{
for (u32 col = 0; col < refBMP.width; ++col)
{
int x = pen_x + col;
int y = pen_y + row;
ConverterInfo.Pixels[y * local_tex_width + x] = (i == 32) ? 0 : refBMP.buffer[row * refBMP.pitch + col];
}
}
info[arr_iter].x[0] = pen_x;
info[arr_iter].y[0] = pen_y;
info[arr_iter].x[1] = pen_x + refBMP.width;
info[arr_iter].y[1] = pen_y + refBMP.rows;
info[arr_iter].x_off = ConverterInfo.face->glyph->bitmap_left;
info[arr_iter].y_off = ConverterInfo.face->glyph->bitmap_top;
info[arr_iter].advance = ConverterInfo.face->glyph->advance.x >> 6;
pen_x += refBMP.width + 1;
arr_iter++;
}
}
int index_to_cyrillic_A = arr_iter;
for (u32 i = 1040; i < 1106; ++i)
{
FT_Error err;
// @ '¨' è '¸'
if (i != 1104)
err = FT_Load_Char(ConverterInfo.face, i, FT_LOAD_RENDER | FT_LOAD_FORCE_AUTOHINT | FT_LOAD_TARGET_LIGHT);
else
err = FT_Load_Char(ConverterInfo.face, 1025, FT_LOAD_RENDER | FT_LOAD_FORCE_AUTOHINT | FT_LOAD_TARGET_LIGHT);
FT_Bitmap* bmp = &ConverterInfo.face->glyph->bitmap;
if (err)
{
std::cout << "ERROR" << std::endl;
system("pause");
}
else
{
if (pen_x + bmp->width >= local_tex_width)
{
pen_x = 0;
pen_y += ((ConverterInfo.face->size->metrics.height >> 6) + 1);
}
if (i >= 1072 || i == 1049)
{
current_size = int(info[index_to_cyrillic_A].y_off - ConverterInfo.face->glyph->bitmap_top);
pen_y += current_size;
}
if (i >= 1072 || i == 1049)
{
if ((info[index_to_cyrillic_A].y_off - ConverterInfo.face->glyph->bitmap_top) > 0)
pen_y -= current_size;
else
pen_y += abs(current_size);
}
for (u32 row = 0; row < bmp->rows; ++row)
{
for (u32 col = 0; col < bmp->width; ++col)
{
u32 x = pen_x + col;
u32 y = pen_y + row;
ConverterInfo.Pixels[y * local_tex_width + x] = bmp->buffer[row * bmp->pitch + col];
}
}
info[arr_iter].x[0] = pen_x;
info[arr_iter].y[0] = pen_y;
info[arr_iter].x[1] = pen_x + bmp->width;
info[arr_iter].y[1] = pen_y + bmp->rows;
info[arr_iter].x_off = ConverterInfo.face->glyph->bitmap_left;
info[arr_iter].y_off = ConverterInfo.face->glyph->bitmap_top;
info[arr_iter].advance = ConverterInfo.face->glyph->advance.x >> 6;
pen_x += bmp->width + 1;
arr_iter++;
}
}
u8* dds_data = (u8*)calloc(ConverterInfo.TexWid * ConverterInfo.TexHeig * 4, 1);
for (u32 i = 0; i < (ConverterInfo.TexWid * ConverterInfo.TexHeig); ++i)
{
dds_data[i * 4] = 255;
dds_data[i * 4 + 1] = 255;
dds_data[i * 4 + 2] = 255;
dds_data[i * 4 + 3] = ConverterInfo.Pixels[i];
}
if (g_count)
{
PathSystem.FileOutName += PathSystem.PathOutName;
PathSystem.FileOutName += PathSystem.FileName + "_";
switch (g_count)
{
case 1:
{
PathSystem.FileOutName += std::to_string(ConverterInfo.FontHeig);
PathSystem.FileOutName += "_" + std::to_string(1600);
max_height_font = ConverterInfo.FontHeig;
} break;
case 2:
{
PathSystem.FileOutName += std::to_string(max_height_font);
PathSystem.FileOutName += "_" + std::to_string(1024);
} break;
case 3:
{
PathSystem.FileOutName += std::to_string(max_height_font);
PathSystem.FileOutName += "_" + std::to_string(800);
g_count = 0;
} break;
}
PathSystem.FileOutName += ".tga";
}
stbi_write_tga(PathSystem.FileOutName.c_str(), ConverterInfo.TexWid, ConverterInfo.TexHeig, 4, dds_data);
free(dds_data);
free(ConverterInfo.Pixels);
ZeroMemory(&ConverterInfo.Pixels, sizeof(ConverterInfo.Pixels));
// @ Ñîçäàíèå ini ôàéëà
std::ofstream INIFile;
xstring output_copy = PathSystem.FileOutName;
INIFile.open((PathSystem.FileOutName.erase(PathSystem.FileOutName.rfind(".")) + ".ini").c_str());
if (INIFile.is_open())
{
auto MakeNulls = [](std::string &x)
{
for (u32 iter = 0; iter < 3; iter++)
{
x += "0";
x += ", ";
} x += "0";
};
INIFile << "[symbol_coords]" << std::endl;
INIFile << "height = " << std::to_string(ConverterInfo.FontHeig) << std::endl;
std::string fac = "00";
std::string x = " = ";
for (u32 i = 0; i < 32; ++i)
{
MakeNulls(x);
fac = (i < 10) ? "00" : "0";
fac += std::to_string(i);
INIFile << fac;
INIFile << x;
INIFile << std::endl;
x = " = ";
}
// @ Èä¸ò Ëàòèíèöà è ñëóæåáíûå ñèìâîëû (ñíà÷à ñëóæåáíûå ñèìâîëû, çàòåì ëàòûíü)
size_t local_it = 0; // @ Íóæíî ó÷èòûâàòü ÷òî ïåðåññûëêà ðàçíàÿ, îñîáåííî ñ äàííûì ìàññèâîì
for (u32 i = 32; i < 128; ++i)
{
for (u32 CordIter = 0; CordIter < 2; CordIter++)
{
x += std::to_string(info[local_it].x[CordIter]) + ", ";
x += std::to_string(info[local_it].y[CordIter]) + ((CordIter == 0) ? ", " : "");
}
fac = (i < 100 && i > 10) ? "0" : "";
fac += std::to_string(i);
INIFile << fac;
INIFile << x;
INIFile << std::endl;
x = " = ";
local_it++;
}
auto WriteToINI = [&INIFile, &fac, &x](size_t &Iterator)
{
fac = std::to_string(Iterator);
INIFile << fac;
INIFile << x;
INIFile << std::endl;
x = " = ";
};
// @ ÏÛÑîâñêèå ìóñîðíûå ñèìâîëû, â òåêñòóðàõ îíè íå ñîäåðæàòüñÿ (íå òî ÷òî â îðèãèíàëå!)
for (size_t i = 128; i < 192; ++i)
{
// @ Çàïèñûâàåì ¨
if (i == 168)
{
for (u32 CordIter = 0; CordIter < 2; CordIter++)
{
x += std::to_string(info[TOTAL_ANSCII-2].x[CordIter]) + ", ";
x += std::to_string(info[TOTAL_ANSCII-2].y[CordIter]) + ((CordIter == 0) ? ", " : "");
}
fac = "";
fac += std::to_string(i);
INIFile << fac;
INIFile << x;
INIFile << std::endl;
x = " = ";
continue;
}
// @ Çàïèñûâàåì ¸
if (i == 184)
{
for (u32 CordIter = 0; CordIter < 2; CordIter++)
{
x += std::to_string(info[TOTAL_ANSCII-1].x[CordIter]) + ", ";
x += std::to_string(info[TOTAL_ANSCII-1].y[CordIter]) + ((CordIter == 0) ? ", " : "");
}
fac = "";
fac += std::to_string(i);
INIFile << fac;
INIFile << x;
INIFile << std::endl;
x = " = ";
continue;
}
MakeNulls(x);
WriteToINI(i);
}
// @ Èä¸ò êèðèëëèöà
for (size_t i = 192; i < 256; ++i)
{
for (u32 CordIter = 0; CordIter < 2; CordIter++)
{
x += std::to_string(info[local_it].x[CordIter]) + ", ";
x += std::to_string(info[local_it].y[CordIter]) + ((CordIter == 0) ? ", " : "");
}
WriteToINI(i);
local_it++;
}
}
PathSystem.FileOutName = "";
INIFile.close();
if (ConverterInfo.bHaveTexconv)
{
// @ Çàïèñàëè óæå .tga -> êîíâåðòèðóåì â ñîîòâåòñòâóþùèé ðàçìåð ïîä dds
system(("texconv.exe -f DXT5 " + output_copy + " -o " + PathSystem.PathOutName +" -y").c_str());
system(("texconv.exe " + output_copy + " -ft png -f R8G8B8A8_UNORM" + " -o " + PathSystem.PathOutName).c_str());
}
else
MessageBoxA(0, "Sorry, but you don't have in your folder texconv.exe!", "Error!", MB_OK);
}
void XRay::CFontGen::CheckTexConv()
{
ConverterInfo.bHaveTexconv = std::filesystem::exists("texconv.exe");
}
void XRay::CFontGen::CreateGSCFonts()
{
if (PathSystem.FontSize)
{
for(u32 Iter = 0; Iter < 3; Iter++)
// Êàæäûé ïîñëåäóþùèé ðàçìåð óìåíüøàåòñÿ ñ øàãîì *2 (òî åñòü -2, -4)
ParseFont(PathSystem.FontSize - (Iter * 2), PathSystem.FontSize);
}
}
void XRay::CFontGen::InitFont()
{
#ifdef DEBUG
SetConsoleTextAttribute(hConsole, DEFAULT_COLOR);
ConverterInfo.Pixels = nullptr;
CreateGSCFonts();
if (!g_count)
{
SetConsoleTextAttribute(hConsole, OK_COLOR);
std::cout << "Successful!" << std::endl;
SetConsoleTextAttribute(hConsole, DEFAULT_COLOR);
}
else
{
SetConsoleTextAttribute(hConsole, ERROR_COLOR);
std::cout << "Failed!" << std::endl;
SetConsoleTextAttribute(hConsole, DEFAULT_COLOR);
}
#endif
}
void XRay::CFontGen::CreateFolder()
{
std::filesystem::path PathOut(PathSystem.PathOutName.c_str());
if (std::filesystem::create_directory(PathOut))
{
bSucDir = true;
}
else
{
// @ But we must be sure that if the folder exists
if (std::filesystem::exists(PathSystem.PathOutName.c_str()))
{
bSucDir = true;
}
else
{
const char* ErrMessage = "Abort! Can't create a folder, maybe your OS is wrong or something happend.";
#ifdef DEBUG
SetConsoleTextAttribute(hConsole, ERROR_COLOR);
std::cout << ErrMessage << std::endl;
SetConsoleTextAttribute(hConsole, DEFAULT_COLOR);
#else
MessageBoxA(nullptr, ErrMessage, "Ohhhh...", NULL);
#endif
}
}
}
void XRay::CFontGen::ManageCreationFile()
{
// Init PathList
xstring FileFullPath = PathSystem.PathName;
xstring mask = FileFullPath.substr(FileFullPath.rfind(".") + 1);
PathSystem.FileName = FileFullPath.substr(FileFullPath.rfind("\\")+1);
PathSystem.FileName = PathSystem.FileName.erase(PathSystem.FileName.rfind("."));
if (mask == "ttf" || mask == "ttc" || mask == "otf")
{
// @ Consider it's valid file for a while. . .
FT_Error err = FT_New_Face(ConverterInfo.lib, PathSystem.PathName.c_str(), 0, &ConverterInfo.face);
if (err)
MessageBoxA(0, "Failed! Your file is invalid.", "Error!", MB_OK);
else
bSucFile = true;
}
else
{
// @ Invalid path
MessageBoxA(0, "Failed! Your path is invalid.", "Error!", MB_OK);
}
}