ougge/src/resources/fonts.cpp

34 lines
991 B
C++

#include <imgui.h>
#include "resources.hpp"
#include <cstdio>
#include <cstring>
namespace ougge::resources {
// select all glyphs from font
static const ImWchar glyph_ranges[] = {
0x0020, 0xFFFF, 0
};
ImFont* ImFont_LoadFromFile(const std::string& file_path, f32 font_size, f32 dpi){
ImGuiIO& io = ImGui::GetIO();
font_size *= dpi;
return io.Fonts->AddFontFromFileTTF(file_path.c_str(), font_size, nullptr, glyph_ranges);
}
ImFont* ImFont_LoadFromResource(const std::string& font_name, f32 font_size, f32 dpi){
ImGuiIO& io = ImGui::GetIO();
font_size *= dpi;
ImFontConfig font_cfg = ImFontConfig();
std::sprintf(font_cfg.Name, "%s %ipx", font_name.c_str(), (i32)font_size);
auto& res = getResource("fonts/" + font_name + ".ttf");
char* font_data = new char[res.size];
res.openStream()->read(font_data, res.size);
return io.Fonts->AddFontFromMemoryTTF((void*)(font_data), res.size,
font_size, &font_cfg, glyph_ranges);
}
}