-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathFontGeneratorUI.cpp
More file actions
205 lines (184 loc) · 5.31 KB
/
Copy pathFontGeneratorUI.cpp
File metadata and controls
205 lines (184 loc) · 5.31 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
#include "UI/FontGeneratorUI.h"
#include "Meta.h"
#include "Core/FileSystem.h"
#include "Core/Logger.h"
#include "Core/Util.h"
#include "Game/Asset/FontGenerator.h"
#include "External/imgui/misc/cpp/imgui_stdlib.h"
namespace sh::editor
{
FontGeneratorUI::FontGeneratorUI(const render::IRenderContext& ctx) :
renderCtx(ctx)
{
}
SH_EDITOR_API void FontGeneratorUI::SetAssetPath(const std::filesystem::path& assetPath)
{
this->assetPath = assetPath;
explorer.SetCurrentPath(assetPath);
}
SH_EDITOR_API void FontGeneratorUI::Clear()
{
if (!fontPath.empty())
fontPath.clear();
if (!fontData.empty())
fontData.clear();
if (!str.empty())
str.clear();
fontSize = 32.f;
padding = 2;
atlasWidth = 1024;
atlasHeight = 1024;
unicodes.clear();
}
SH_EDITOR_API void FontGeneratorUI::Open()
{
bShow = true;
}
SH_EDITOR_API void FontGeneratorUI::Render()
{
if (!bShow)
{
Clear();
return;
}
ImGui::SetNextWindowSize(ImVec2(800, 400), ImGuiCond_::ImGuiCond_FirstUseEver);
if (ImGui::Begin("FontGeneratorUI", &bShow))
{
if (!fontPath.empty())
{
ImGui::Text(fontPath.u8string().c_str());
ImGui::SameLine();
}
if (ImGui::Button("Open Font File"))
{
explorer.SetExtensionFilter({ ".ttf", ".otf" });
explorer.PushCallbackQueue(
[this](const std::filesystem::path& path)
{
auto opt = core::FileSystem::LoadBinary(path);
if (!opt.has_value())
return;
fontPath = path;
fontData = std::move(opt.value());
}
);
explorer.Open(ExplorerUI::OpenMode::Select);
}
if (!fontData.empty())
{
ImGui::Separator();
ImGui::Text("Font size");
ImGui::InputFloat("##fontSize", &fontSize);
ImGui::Text("Padding");
ImGui::InputInt("##padding", &padding);
if (padding <= 0)
padding = 1;
ImGui::Text("Atlas size");
int size[2] = { atlasWidth, atlasHeight };
ImGui::InputInt2("##padding", size);
if (size[0] <= 0)
size[0] = 1;
if (size[1] <= 0)
size[1] = 1;
atlasWidth = size[0];
atlasHeight = size[1];
ImGui::Text("Characters");
ImGui::InputText("##fontstrs", &str);
if (ImGui::Button("Add from file(UTF-8)"))
{
explorer.ClearExtensionFilter();
explorer.PushCallbackQueue(
[this](const std::filesystem::path& path)
{
auto opt = core::FileSystem::LoadText(path);
if (!opt.has_value())
return;
std::set<uint32_t> uniSet;
uniSet.insert(unicodes.begin(), unicodes.end());
auto tmp = game::FontGenerator::ExtractUnicode(opt.value());
uniSet.insert(tmp.begin(), tmp.end());
unicodes.clear();
unicodes.reserve(uniSet.size());
unicodes.insert(unicodes.begin(), uniSet.begin(), uniSet.end());
}
);
explorer.Open(ExplorerUI::OpenMode::Select);
}
ImGui::SameLine();
if (ImGui::Button("Clear unicodes"))
unicodes.clear();
if (!unicodes.empty())
ImGui::Text("Unicode count: %d", unicodes.size());
ImGui::Separator();
if (ImGui::Button("Generate font data"))
{
explorer.PushCallbackQueue(
[this](const std::filesystem::path& path)
{
ExportFont(path);
}
);
explorer.ResetSelected();
explorer.Open(ExplorerUI::OpenMode::Create);
}
}
}
ImGui::End();
explorer.Render();
}
void FontGeneratorUI::ExportFont(const std::filesystem::path& path)
{
game::FontGenerator::Options option{};
option.fontSize = fontSize;
option.padding = padding;
option.atlasW = atlasWidth;
option.atlasH = atlasHeight;
option.bAllowMultiPage = true;
auto tempUnicodes = game::FontGenerator::ExtractUnicode(str);
unicodes.insert(unicodes.end(), tempUnicodes.begin(), tempUnicodes.end());
render::Font* font = game::FontGenerator::GenerateFont(renderCtx, fontData, unicodes, option);
if (font == nullptr)
{
SH_ERROR_FORMAT("Failed to generate font!: {}", fontPath.u8string());
return;
}
const std::string fileName = path.stem().u8string();
font->SetName(fileName);
const std::filesystem::path exportedFontPath = path.parent_path() / std::filesystem::u8path(fileName + ".font");
std::vector<std::filesystem::path> exportedAtlasPaths;
if (!core::FileSystem::SaveText(font->Serialize(), exportedFontPath))
{
SH_ERROR_FORMAT("Failed to export font: {}", exportedFontPath.u8string());
return;
}
int idx = 0;
for (auto tex : font->GetAtlases())
{
std::filesystem::path exportPath = path.parent_path() / std::filesystem::u8path(fileName + std::to_string(idx++) + ".png");
tex->SetName(exportPath.stem().u8string());
tex->ExportToPNG(exportPath);
exportedAtlasPaths.push_back(std::move(exportPath));
}
// 에셋 폴더내에 생성했다면 메타 파일 생성
const std::filesystem::path relativePath = std::filesystem::relative(path, assetPath);
if (!relativePath.empty())
{
const auto fontMetaDir = Meta::CreateMetaDirectory(exportedFontPath);
if (!std::filesystem::exists(fontMetaDir))
{
Meta meta{};
meta.Save(*font, Meta::CreateMetaDirectory(exportedFontPath));
}
int idx = 0;
for (auto tex : font->GetAtlases())
{
const auto atlasMetaDir = Meta::CreateMetaDirectory(exportedAtlasPaths[idx++]);
if (!std::filesystem::exists(atlasMetaDir))
{
Meta atlasMeta{};
atlasMeta.SaveWithObj(*tex, atlasMetaDir);
}
}
}
}
}//namespace