Skip to content

Commit

Permalink
load fonts from conf and merge
Browse files Browse the repository at this point in the history
  • Loading branch information
Green-Sky committed Jun 15, 2024
1 parent 2189c4c commit 5356e59
Show file tree
Hide file tree
Showing 3 changed files with 82 additions and 56 deletions.
1 change: 1 addition & 0 deletions external/imgui/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ add_library(imgui
imgui/misc/cpp/imgui_stdlib.cpp
)

target_compile_definitions(imgui PUBLIC IMGUI_USE_WCHAR32)
target_compile_features(imgui PUBLIC cxx_std_11)

target_include_directories(imgui PUBLIC "${CMAKE_CURRENT_SOURCE_DIR}")
Expand Down
56 changes: 0 additions & 56 deletions src/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -93,62 +93,6 @@ int main(int argc, char** argv) {
theme = getDefaultThemeDark();
}

{
auto* font_atlas = ImGui::GetIO().Fonts;
font_atlas->ClearFonts();
{
ImFontConfig fontcfg;

// upsampling to int looks almost ok
const float font_size_scale = 1.3f * display_scale;
const float font_oversample = 4.f;

// default font is pixel perfect at 13
fontcfg.SizePixels = 13.f * font_size_scale;
fontcfg.RasterizerDensity = font_oversample/font_size_scale;
// normally density would be set to dpi scale of the display

font_atlas->AddFontDefault(&fontcfg);
}
if constexpr (false) {
ImFontConfig fontcfg;
//fontcfg.SizePixels = 16.f;
fontcfg.RasterizerDensity = 1.f;
fontcfg.OversampleH = 1;
fontcfg.OversampleV = 1;
fontcfg.PixelSnapH = true;

//ImFontGlyphRangesBuilder glyphbld;
//glyphbld.AddRanges(font_atlas->GetGlyphRangesDefault());
//glyphbld.AddRanges(font_atlas->GetGlyphRangesGreek());
//glyphbld.AddRanges(font_atlas->GetGlyphRangesCyrillic());
//glyphbld.AddRanges(font_atlas->GetGlyphRangesChineseSimplifiedCommon());

font_atlas->AddFontFromFileTTF(
"/nix/store/7fjwhgbz16i08xm171arr081bqpivv7k-hack-font-3.003/share/fonts/truetype/Hack-Regular.ttf",
20.f*display_scale,
&fontcfg,
font_atlas->GetGlyphRangesGreek()
);

fontcfg.MergeMode = true;

font_atlas->AddFontFromFileTTF(
"/nix/store/7fjwhgbz16i08xm171arr081bqpivv7k-hack-font-3.003/share/fonts/truetype/Hack-Regular.ttf",
20.f*display_scale,
&fontcfg,
font_atlas->GetGlyphRangesCyrillic()
);

font_atlas->AddFontFromFileTTF(
"/nix/store/7fjwhgbz16i08xm171arr081bqpivv7k-hack-font-3.003/share/fonts/truetype/Hack-Regular.ttf",
20.f*display_scale,
&fontcfg,
font_atlas->GetGlyphRangesChineseSimplifiedCommon()
);
}
font_atlas->Build();
}

ImGui_ImplSDL3_InitForSDLRenderer(window.get(), renderer.get());
ImGui_ImplSDLRenderer3_Init(renderer.get());
Expand Down
81 changes: 81 additions & 0 deletions src/start_screen.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,87 @@ StartScreen::StartScreen(const std::vector<std::string_view>& args, SDL_Renderer
}
}

float display_scale = SDL_GetWindowDisplayScale(SDL_GetRenderWindow(renderer));
if (display_scale < 0.001f) {
// error?
display_scale = 1.f;
}

{
auto* font_atlas = ImGui::GetIO().Fonts;
font_atlas->ClearFonts();
// for now we also always merge
//bool has_font {false};

ImFontGlyphRangesBuilder glyphbld;
ImVector<ImWchar> glyph_ranges;
{ // build ranges
glyphbld.AddRanges(font_atlas->GetGlyphRangesDefault());
glyphbld.AddRanges(font_atlas->GetGlyphRangesGreek());
glyphbld.AddRanges(font_atlas->GetGlyphRangesCyrillic());
glyphbld.AddRanges(font_atlas->GetGlyphRangesChineseSimplifiedCommon()); // contains CJK
glyphbld.AddText(""); // somehow missing
// popular emojies
glyphbld.AddText(u8"😂❤️🤣👍😭🙏😘🥰😍😊🎉😁💕🥺😅🔥☺️🤦♥️🤷🙄😆🤗😉🎂🤔👏🙂😳🥳😎👌💜😔💪✨💖👀😋😏😢👉💗😩💯🌹💞🎈💙😃😡💐😜🙈🤞😄🤤🙌🤪❣️😀💋💀👇💔😌💓🤩🙃😬😱😴🤭😐🌞😒😇🌸😈🎶✌️🎊🥵😞💚☀️🖤💰😚👑🎁💥🙋☹️😑🥴👈💩✅👋🤮😤🤢🌟❗😥🌈💛😝😫😲🖕‼️🔴🌻🤯💃👊🤬🏃😕👁️⚡☕🍀💦⭐🦋🤨🌺😹🤘🌷💝💤🤝🐰😓💘🍻😟😣🧐😠🤠😻🌙😛🤙🙊");

if (const auto sv_opt = _conf.get_string("ImGuiFonts", "atlas_extra_text"); sv_opt.has_value) {
glyphbld.AddText(sv_opt.s.start, sv_opt.s.start+sv_opt.s.extend);
}
glyphbld.BuildRanges(&glyph_ranges);
}

ImFontConfig fontcfg;
//fontcfg.SizePixels = 16.f*display_scale;
fontcfg.SizePixels = _conf.get_int("ImGuiFonts", "size").value_or(13) * display_scale;
fontcfg.RasterizerDensity = 1.f;
fontcfg.OversampleH = 2;
fontcfg.OversampleV = 1;
fontcfg.MergeMode = false;

for (const auto [font_path, should_load] : _conf.entries_bool("ImGuiFonts", "fonts")) {
if (!should_load) {
continue;
}

std::cout << "Font: loading '" << font_path << "'\n";
const auto* resulting_font = font_atlas->AddFontFromFileTTF(
font_path.c_str(),
_conf.get_int("ImGuiFonts", "size", font_path).value_or(0) * display_scale,
&fontcfg,
&(glyph_ranges[0])
);

if (resulting_font != nullptr) {
//has_font = true;
fontcfg.MergeMode = true;
} else {
std::cerr << "Font: failed to load '" << "path" << "' !\n";
}
}

// always append the default as a fallback (merge in)
{
#if 0
ImFontConfig fontcfg;

// upsampling to int looks almost ok
//const float font_size_scale = 1.3f * display_scale;
const float font_size_scale = 1.0f * display_scale;
const float font_oversample = 4.f;

// default font is pixel perfect at 13
fontcfg.SizePixels = 13.f * font_size_scale;
fontcfg.RasterizerDensity = font_oversample/font_size_scale;
// normally density would be set to dpi scale of the display

fontcfg.MergeMode = has_font;
#endif

font_atlas->AddFontDefault(&fontcfg);
}

font_atlas->Build();
}
}

Screen* StartScreen::render(float, bool&) {
Expand Down

0 comments on commit 5356e59

Please sign in to comment.