Тьомчик
-
Автор темы
- #1
class fonts_t:
class fonts_t {
private:
ImFont* font[3];
std::string get_path_with_fonts() const {
char windowsDirectory[MAX_PATH];
GetWindowsDirectoryA(windowsDirectory, MAX_PATH);
return std::string(windowsDirectory) + "\\Fonts\\";
}
std::string get_font_path(e_fonts_type type) const {
switch (type) {
case e_fonts_type::light:
return get_path_with_fonts() + "segoeuisl.ttf";
break;
case e_fonts_type::regular:
return get_path_with_fonts() + "segoeui.ttf";
break;
case e_fonts_type::bold:
return get_path_with_fonts() + "seguisb.ttf";
break;
case e_fonts_type::extrabold:
return get_path_with_fonts() + "segoeuib.ttf";
break;
default:
throw std::out_of_range("get_font_path() is out of range.");
break;
}
}
void create_font_from_file(ImFont* name, const char* font_path, float size, const ImWchar* range) const {
auto config = ImFontConfig();
config.FontBuilderFlags = ImGuiFreeTypeBuilderFlags_NoHinting | ImGuiFreeTypeBuilderFlags_NoAutoHint | ImGuiFreeTypeBuilderFlags_Bitmap;
config.SizePixels = size;
config.RasterizerMultiply = 1.0f;
config.RasterizerDensity = 1.0f;
name = ImGui::GetIO().Fonts->AddFontFromFileTTF(font_path, size, &config, range);
if (!name)
throw std::runtime_error("failed to load font from file: " + std::string(font_path));
}
void create_font_from_memory(ImFont* name, const void* font_data, int font_data_size, float size, const ImWchar* range) const {
auto config = ImFontConfig();
config.FontBuilderFlags = ImGuiFreeTypeBuilderFlags_NoHinting | ImGuiFreeTypeBuilderFlags_NoAutoHint | ImGuiFreeTypeBuilderFlags_Bitmap;
config.SizePixels = size;
config.RasterizerMultiply = 1.0f;
config.RasterizerDensity = 1.0f;
name = ImGui::GetIO().Fonts->AddFontFromMemoryCompressedTTF(font_data, font_data_size, size, &config, range);
if (!name)
throw std::runtime_error("failed to load font from memory.");
}
public:
void instalise() {
constexpr ImWchar icon_ranges[] = { 0xE000, 0xF8FF, 0 };
create_font_from_file(font[e_fonts::basic], get_font_path(e_fonts_type::regular).c_str(), 14.0f, ImGui::GetIO().Fonts->GetGlyphRangesCyrillic());
create_font_from_file(font[e_fonts::bolder], get_font_path(e_fonts_type::bold).c_str(), 14.0f, ImGui::GetIO().Fonts->GetGlyphRangesCyrillic());
create_font_from_memory(font[e_fonts::icon], font_awesome_data, font_awesome_size, 14.0f, icon_ranges);
ImGuiFreeType::BuildFontAtlas(ImGui::GetIO().Fonts);
}
ImFont* get_font(e_fonts type) const {
switch (type) {
case e_fonts::basic:
return font[e_fonts::basic];
break;
case e_fonts::bolder:
return font[e_fonts::bolder];
break;
case e_fonts::icon:
return font[e_fonts::icon];
break;
default:
throw std::out_of_range("get_font() is out of range.");
break;
}
}
};
enumerations:
enum e_fonts_type {
light,
regular,
bold,
extrabold
};
enum e_fonts {
basic,
bolder,
icon
};
call fonts:
ImGui::GetWindowDrawList()->AddText(g::fonts.get_font(e_fonts::bolder), g::fonts.get_font(e_fonts::bolder)->FontSize, ImGui::GetWindowPos() + ImVec2(5, 5), ImColor(1.0f, 1.0f, 1.0f, 1.0f), "Song, song of the south, Sweet potato pie and I shut my mouth, Gone, gone with the wind There ain't nobody lookin' back again");
g::fonts.instalise();