Skip to content

Commit

Permalink
Add setting for using built-in font
Browse files Browse the repository at this point in the history
  • Loading branch information
albin-johansson committed Jun 15, 2024
1 parent 60f4f9b commit a8c94c2
Show file tree
Hide file tree
Showing 5 changed files with 35 additions and 14 deletions.
3 changes: 3 additions & 0 deletions source/core/inc/tactile/core/model/settings.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,9 @@ struct Settings final
/** The UI font size. */
float font_size;

/** Whether the built-in Dear ImGui font should be used. */
bool use_built_in_font : 1;

/** Whether verbose events (e.g., some mouse events) should be logged. */
bool log_verbose_events : 1;
};
Expand Down
8 changes: 6 additions & 2 deletions source/core/inc/tactile/core/ui/fonts.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,9 @@
namespace tactile {

class IRenderer;
struct Settings;

namespace ui {

/**
* Attempts to reload the font texture atlas.
Expand All @@ -15,11 +18,12 @@ class IRenderer;
* This function does nothing if the renderer doesn't support font reloading.
*
* \param renderer The active renderer.
* \param font_size The font size to use.
* \param settings The current settings.
* \param framebuffer_scale The display framebuffer scale.
*/
void reload_fonts(IRenderer& renderer,
float font_size,
const Settings& settings,
float framebuffer_scale);

} // namespace ui
} // namespace tactile
2 changes: 2 additions & 0 deletions source/core/src/tactile/core/model/settings.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ inline namespace settings {
inline constexpr LanguageID kLanguageDefault = LanguageID::kAmericanEnglish;
inline constexpr usize kCommandCapacityDefault = 100;
inline constexpr float kFontSizeDefault = 13.0f;
inline constexpr bool kUseBuiltInFontDefault = true;
inline constexpr bool kLogVerboseEventsDefault = false;

} // namespace settings
Expand All @@ -18,6 +19,7 @@ auto get_default_settings() -> Settings
.language = kLanguageDefault,
.command_capacity = kCommandCapacityDefault,
.font_size = kFontSizeDefault,
.use_built_in_font = kUseBuiltInFontDefault,
.log_verbose_events = kLogVerboseEventsDefault,
};
}
Expand Down
5 changes: 4 additions & 1 deletion source/core/src/tactile/core/tactile_editor.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
#include "tactile/core/debug/validation.hpp"
#include "tactile/core/document/map_document.hpp"
#include "tactile/core/log/logger.hpp"
#include "tactile/core/ui/fonts.hpp"
#include "tactile/core/ui/i18n/language_parser.hpp"
#include "tactile/render/renderer.hpp"
#include "tactile/render/window.hpp"
Expand Down Expand Up @@ -69,6 +70,8 @@ void TactileEditor::on_update()
}

void TactileEditor::on_framebuffer_scale_changed(const float framebuffer_scale)
{}
{
ui::reload_fonts(*mRenderer, mModel->get_settings(), framebuffer_scale);
}

} // namespace tactile
31 changes: 20 additions & 11 deletions source/core/src/tactile/core/ui/fonts.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,32 +7,40 @@

#include "tactile/base/container/array.hpp"
#include "tactile/core/log/logger.hpp"
#include "tactile/core/model/settings.hpp"
#include "tactile/render/renderer.hpp"

namespace tactile {
namespace tactile::ui {
inline namespace fonts {

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

} // namespace fonts

void reload_fonts(IRenderer& renderer,
const float font_size,
const Settings& settings,
const float framebuffer_scale)
{
if (renderer.can_reload_fonts()) {
TACTILE_LOG_DEBUG("Reloading fonts (size: {})", font_size);
TACTILE_LOG_DEBUG("Reloading fonts (size: {})", settings.font_size);

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

const auto scaled_font_size = font_size * framebuffer_scale;
const auto scaled_font_size = settings.font_size * framebuffer_scale;

ImFontConfig roboto_config {};
roboto_config.SizePixels = scaled_font_size;
io.Fonts->AddFontFromFileTTF("assets/fonts/roboto/Roboto-Regular.ttf",
scaled_font_size,
&roboto_config);
if (settings.use_built_in_font) {
ImFontConfig default_config {};
default_config.SizePixels = scaled_font_size;
io.Fonts->AddFontDefault(&default_config);
}
else {
ImFontConfig roboto_config {};
roboto_config.SizePixels = scaled_font_size;
io.Fonts->AddFontFromFileTTF("assets/fonts/roboto/Roboto-Regular.ttf",
scaled_font_size,
&roboto_config);
}

ImFontConfig fa_config {};
fa_config.MergeMode = true;
Expand All @@ -54,8 +62,9 @@ void reload_fonts(IRenderer& renderer,
ImGui::GetStyle().ScaleAllSizes(1.0f);
}
else {
TACTILE_LOG_WARN("Tried to reload fonts, but the renderer didn't support it");
TACTILE_LOG_WARN(
"Tried to reload fonts, but the renderer didn't support it");
}
}

} // namespace tactile
} // namespace tactile::ui

0 comments on commit a8c94c2

Please sign in to comment.