[feat] load different size fonts in initialization

This commit is contained in:
dijunkun
2024-11-15 18:19:54 +08:00
parent 84194188f8
commit 59b1208321
2 changed files with 36 additions and 19 deletions

View File

@@ -504,11 +504,9 @@ int Render::DestroyStreamWindow() {
return 0; return 0;
} }
int Render::SetupFontAndStyle() { int Render::SetupFontAndStyle(bool is_main_window) {
// Setup Dear ImGui style // Setup Dear ImGui style
ImGuiIO& io = ImGui::GetIO(); ImGuiIO& io = ImGui::GetIO();
// Master keyboard navigation enable flag. Enable full Tabbing + directional
// arrows + space/enter to activate.
io.ConfigFlags |= io.ConfigFlags |=
ImGuiConfigFlags_NavEnableKeyboard; // Enable Keyboard Controls ImGuiConfigFlags_NavEnableKeyboard; // Enable Keyboard Controls
io.ConfigFlags |= io.ConfigFlags |=
@@ -518,23 +516,29 @@ int Render::SetupFontAndStyle() {
// Load Fonts // Load Fonts
ImFontConfig config; ImFontConfig config;
config.FontDataOwnedByAtlas = false; config.FontDataOwnedByAtlas = false;
io.Fonts->AddFontFromMemoryTTF(OPPOSans_Regular_ttf, (is_main_window ? font_size_32_main_ : font_size_32_sub_) =
sizeof(OPPOSans_Regular_ttf), 32.0f, &config, io.Fonts->AddFontFromMemoryTTF(
io.Fonts->GetGlyphRangesChineseFull()); OPPOSans_Regular_ttf, sizeof(OPPOSans_Regular_ttf), 32.0f, &config,
io.Fonts->GetGlyphRangesChineseFull());
config.MergeMode = true; config.MergeMode = true;
config.GlyphMinAdvanceX = (is_main_window ? font_size_25_main_ : font_size_25_sub_) =
13.0f; // Use if you want to make the icon monospaced 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}; static const ImWchar icon_ranges[] = {ICON_MIN_FA, ICON_MAX_FA, 0};
// io.Fonts->AddFontFromMemoryTTF(fa_regular_400_ttf, (is_main_window ? icon_size_30_main_ : icon_size_30_sub_) =
// sizeof(fa_regular_400_ttf), io.Fonts->AddFontFromMemoryTTF(fa_solid_900_ttf, fa_solid_900_ttf_len,
// 30.0f, &config, icon_ranges); 30.0f, &config, icon_ranges);
io.Fonts->AddFontFromMemoryTTF(fa_solid_900_ttf, sizeof(fa_solid_900_ttf), (is_main_window ? icon_size_15_main_ : icon_size_15_sub_) =
30.0f, &config, icon_ranges); io.Fonts->AddFontFromMemoryTTF(fa_solid_900_ttf, fa_solid_900_ttf_len,
15.0f, &config, icon_ranges);
io.Fonts->Build(); io.Fonts->Build();
// Setup Dear ImGui style
// ImGui::StyleColorsDark();
ImGui::StyleColorsLight(); ImGui::StyleColorsLight();
return 0; return 0;
@@ -548,7 +552,7 @@ int Render::SetupMainWindow() {
ImGui::SetCurrentContext(main_ctx_); ImGui::SetCurrentContext(main_ctx_);
SetupFontAndStyle(); SetupFontAndStyle(true);
SDL_GL_GetDrawableSize(main_window_, &main_window_width_real_, SDL_GL_GetDrawableSize(main_window_, &main_window_width_real_,
&main_window_height_real_); &main_window_height_real_);
@@ -588,7 +592,7 @@ int Render::SetupStreamWindow() {
ImGui::SetCurrentContext(stream_ctx_); ImGui::SetCurrentContext(stream_ctx_);
SetupFontAndStyle(); SetupFontAndStyle(false);
SDL_GL_GetDrawableSize(stream_window_, &stream_window_width_real_, SDL_GL_GetDrawableSize(stream_window_, &stream_window_width_real_,
&stream_window_height_real_); &stream_window_height_real_);

View File

@@ -55,7 +55,7 @@ class Render {
int DestroyMainWindow(); int DestroyMainWindow();
int CreateStreamWindow(); int CreateStreamWindow();
int DestroyStreamWindow(); int DestroyStreamWindow();
int SetupFontAndStyle(); int SetupFontAndStyle(bool is_main_window);
int SetupMainWindow(); int SetupMainWindow();
int DestroyMainWindowContext(); int DestroyMainWindowContext();
int SetupStreamWindow(); int SetupStreamWindow();
@@ -353,6 +353,19 @@ class Render {
private: private:
bool settings_window_pos_reset_ = true; 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 #endif