Skip to content

Commit

Permalink
Add font reload utility
Browse files Browse the repository at this point in the history
  • Loading branch information
albin-johansson committed Dec 9, 2023
1 parent d75887c commit 7628138
Show file tree
Hide file tree
Showing 3 changed files with 84 additions and 0 deletions.
25 changes: 25 additions & 0 deletions modules/core/inc/tactile/core/ui/fonts.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
// Copyright (C) 2023 Albin Johansson (GNU General Public License v3.0)

#pragma once

#include "tactile/core/api.hpp"
#include "tactile/foundation/prelude.hpp"

namespace tactile {

class IRenderer;

/**
* \brief Attempts to reload the Dear ImGui font texture atlas.
*
* \note This function does nothing if the renderer doesn't support font reloading.
*
* \param renderer the active renderer.
* \param font_size the current font size.
* \param framebuffer_scale the current display framebuffer scale.
*/
TACTILE_CORE_API void try_reload_imgui_fonts(IRenderer& renderer,
float font_size,
float framebuffer_scale);

} // namespace tactile
2 changes: 2 additions & 0 deletions modules/core/src/tactile/core/editor_app.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
#include <imgui.h>
#include <imgui_internal.h>

#include "tactile/core/ui/fonts.hpp"
#include "tactile/foundation/debug/validation.hpp"

namespace tactile {
Expand Down Expand Up @@ -48,6 +49,7 @@ void EditorApp::on_update()

void EditorApp::on_framebuffer_scale_changed(const float framebuffer_scale)
{
try_reload_imgui_fonts(*mRenderer, 13.0f, framebuffer_scale);
}

} // namespace tactile
57 changes: 57 additions & 0 deletions modules/core/src/tactile/core/ui/fonts.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
// Copyright (C) 2023 Albin Johansson (GNU General Public License v3.0)

#include "tactile/core/ui/fonts.hpp"

#include <IconsFontAwesome6.h>
#include <imgui.h>

#include "tactile/foundation/container/array.hpp"
#include "tactile/foundation/log/logger.hpp"
#include "tactile/foundation/render/renderer.hpp"

namespace tactile {
namespace {

inline constexpr Array<ImWchar, 3> kFontIconRange {ICON_MIN_FA, ICON_MAX_FA, 0};

} // namespace

void try_reload_imgui_fonts(IRenderer& renderer,
const float font_size,
const float framebuffer_scale)
{
if (renderer.can_reload_fonts_texture()) {
TACTILE_LOG_TRACE("Reloading fonts...");

auto& io = ImGui::GetIO();
io.Fonts->Clear();

ImFontConfig roboto_config {};
roboto_config.SizePixels = font_size * framebuffer_scale;
io.Fonts->AddFontFromFileTTF("assets/fonts/roboto/Roboto-Regular.ttf",
roboto_config.SizePixels,
&roboto_config);

// The global scale is 1 on most platforms, and 0.5 on macOS.
io.FontGlobalScale = 1.0f / framebuffer_scale;

ImFontConfig icon_config {};
icon_config.MergeMode = true;
icon_config.SizePixels = font_size * framebuffer_scale;
icon_config.GlyphMinAdvanceX = icon_config.SizePixels;
icon_config.GlyphMaxAdvanceX = icon_config.GlyphMinAdvanceX;
icon_config.GlyphOffset = {0, 2};
io.Fonts->AddFontFromFileTTF("assets/fonts/fa/" FONT_ICON_FILE_NAME_FAS,
icon_config.GlyphMinAdvanceX,
&icon_config,
kFontIconRange.data());

io.Fonts->Build();

renderer.reload_fonts_texture();

ImGui::GetStyle().ScaleAllSizes(1.0f);
}
}

} // namespace tactile

0 comments on commit 7628138

Please sign in to comment.