From 59b1208321fdf06a6c0cb13cd7953724150267f3 Mon Sep 17 00:00:00 2001 From: dijunkun Date: Fri, 15 Nov 2024 18:19:54 +0800 Subject: [PATCH] [feat] load different size fonts in initialization --- src/single_window/render.cpp | 40 ++++++++++++++++++++---------------- src/single_window/render.h | 15 +++++++++++++- 2 files changed, 36 insertions(+), 19 deletions(-) diff --git a/src/single_window/render.cpp b/src/single_window/render.cpp index 0173dd1..a9fdb76 100644 --- a/src/single_window/render.cpp +++ b/src/single_window/render.cpp @@ -504,11 +504,9 @@ int Render::DestroyStreamWindow() { return 0; } -int Render::SetupFontAndStyle() { +int Render::SetupFontAndStyle(bool is_main_window) { // Setup Dear ImGui style ImGuiIO& io = ImGui::GetIO(); - // Master keyboard navigation enable flag. Enable full Tabbing + directional - // arrows + space/enter to activate. io.ConfigFlags |= ImGuiConfigFlags_NavEnableKeyboard; // Enable Keyboard Controls io.ConfigFlags |= @@ -518,23 +516,29 @@ int Render::SetupFontAndStyle() { // Load Fonts ImFontConfig config; config.FontDataOwnedByAtlas = false; - io.Fonts->AddFontFromMemoryTTF(OPPOSans_Regular_ttf, - sizeof(OPPOSans_Regular_ttf), 32.0f, &config, - io.Fonts->GetGlyphRangesChineseFull()); - + (is_main_window ? font_size_32_main_ : font_size_32_sub_) = + io.Fonts->AddFontFromMemoryTTF( + OPPOSans_Regular_ttf, sizeof(OPPOSans_Regular_ttf), 32.0f, &config, + io.Fonts->GetGlyphRangesChineseFull()); config.MergeMode = true; - config.GlyphMinAdvanceX = - 13.0f; // Use if you want to make the icon monospaced + (is_main_window ? font_size_25_main_ : font_size_25_sub_) = + io.Fonts->AddFontFromMemoryTTF( + OPPOSans_Regular_ttf, sizeof(OPPOSans_Regular_ttf), 25.0f, &config, + io.Fonts->GetGlyphRangesChineseFull()); + (is_main_window ? font_size_16_main_ : font_size_16_sub_) = + io.Fonts->AddFontFromMemoryTTF( + OPPOSans_Regular_ttf, sizeof(OPPOSans_Regular_ttf), 16.0f, &config, + io.Fonts->GetGlyphRangesChineseFull()); + static const ImWchar icon_ranges[] = {ICON_MIN_FA, ICON_MAX_FA, 0}; - // io.Fonts->AddFontFromMemoryTTF(fa_regular_400_ttf, - // sizeof(fa_regular_400_ttf), - // 30.0f, &config, icon_ranges); - io.Fonts->AddFontFromMemoryTTF(fa_solid_900_ttf, sizeof(fa_solid_900_ttf), - 30.0f, &config, icon_ranges); + (is_main_window ? icon_size_30_main_ : icon_size_30_sub_) = + io.Fonts->AddFontFromMemoryTTF(fa_solid_900_ttf, fa_solid_900_ttf_len, + 30.0f, &config, icon_ranges); + (is_main_window ? icon_size_15_main_ : icon_size_15_sub_) = + io.Fonts->AddFontFromMemoryTTF(fa_solid_900_ttf, fa_solid_900_ttf_len, + 15.0f, &config, icon_ranges); io.Fonts->Build(); - // Setup Dear ImGui style - // ImGui::StyleColorsDark(); ImGui::StyleColorsLight(); return 0; @@ -548,7 +552,7 @@ int Render::SetupMainWindow() { ImGui::SetCurrentContext(main_ctx_); - SetupFontAndStyle(); + SetupFontAndStyle(true); SDL_GL_GetDrawableSize(main_window_, &main_window_width_real_, &main_window_height_real_); @@ -588,7 +592,7 @@ int Render::SetupStreamWindow() { ImGui::SetCurrentContext(stream_ctx_); - SetupFontAndStyle(); + SetupFontAndStyle(false); SDL_GL_GetDrawableSize(stream_window_, &stream_window_width_real_, &stream_window_height_real_); diff --git a/src/single_window/render.h b/src/single_window/render.h index 36c470f..4215df3 100644 --- a/src/single_window/render.h +++ b/src/single_window/render.h @@ -55,7 +55,7 @@ class Render { int DestroyMainWindow(); int CreateStreamWindow(); int DestroyStreamWindow(); - int SetupFontAndStyle(); + int SetupFontAndStyle(bool is_main_window); int SetupMainWindow(); int DestroyMainWindowContext(); int SetupStreamWindow(); @@ -353,6 +353,19 @@ class Render { private: bool settings_window_pos_reset_ = true; + + private: + ImFont *font_size_32_main_ = nullptr; + ImFont *font_size_25_main_ = nullptr; + ImFont *font_size_16_main_ = nullptr; + ImFont *font_size_32_sub_ = nullptr; + ImFont *font_size_25_sub_ = nullptr; + ImFont *font_size_16_sub_ = nullptr; + + ImFont *icon_size_30_main_ = nullptr; + ImFont *icon_size_15_main_ = nullptr; + ImFont *icon_size_30_sub_ = nullptr; + ImFont *icon_size_15_sub_ = nullptr; }; #endif \ No newline at end of file