Skip to content

Commit

Permalink
[UI] Allow loading custom font
Browse files Browse the repository at this point in the history
- Unified font size to 12. This causes default UI to look a bit bigger
- Set oversample to 2 to make font more readable (especially custom fonts)
  • Loading branch information
Gliniak committed Feb 25, 2023
1 parent f357f26 commit cd3180b
Show file tree
Hide file tree
Showing 2 changed files with 73 additions and 32 deletions.
104 changes: 72 additions & 32 deletions src/xenia/ui/imgui_drawer.cc
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,15 @@
#include "xenia/ui/ui_event.h"
#include "xenia/ui/window.h"

#if XE_PLATFORM_WIN32
#include <ShlObj_core.h>
#endif

DEFINE_path(
custom_font_path, "",
"Allows user to load custom font and use it instead of default one.",
"General");

namespace xe {
namespace ui {

Expand Down Expand Up @@ -98,38 +107,7 @@ void ImGuiDrawer::Initialize() {
internal_state_ = ImGui::CreateContext();
ImGui::SetCurrentContext(internal_state_);

auto& io = ImGui::GetIO();

// TODO(gibbed): disable imgui.ini saving for now,
// imgui assumes paths are char* so we can't throw a good path at it on
// Windows.
io.IniFilename = nullptr;

// Setup the font glyphs.
ImFontConfig font_config;
font_config.OversampleH = font_config.OversampleV = 1;
font_config.PixelSnapH = true;
static const ImWchar font_glyph_ranges[] = {
0x0020,
0x00FF, // Basic Latin + Latin Supplement
0,
};
io.Fonts->AddFontFromMemoryCompressedBase85TTF(
kProggyTinyCompressedDataBase85, 10.0f, &font_config, font_glyph_ranges);
// TODO(benvanik): jp font on other platforms?
// https://github.com/Koruri/kibitaki looks really good, but is 1.5MiB.
const char* jp_font_path = "C:\\Windows\\Fonts\\msgothic.ttc";
if (std::filesystem::exists(jp_font_path)) {
ImFontConfig jp_font_config;
jp_font_config.MergeMode = true;
jp_font_config.OversampleH = jp_font_config.OversampleV = 1;
jp_font_config.PixelSnapH = true;
jp_font_config.FontNo = 0;
io.Fonts->AddFontFromFileTTF(jp_font_path, 12.0f, &jp_font_config,
io.Fonts->GetGlyphRangesJapanese());
} else {
XELOGW("Unable to load Japanese font; JP characters will be boxes");
}
InitializeFonts();

auto& style = ImGui::GetStyle();
style.ScrollbarRounding = 0;
Expand Down Expand Up @@ -218,6 +196,68 @@ std::optional<ImGuiKey> ImGuiDrawer::VirtualKeyToImGuiKey(VirtualKey vkey) {
}
}

void ImGuiDrawer::InitializeFonts() {
auto& io = ImGui::GetIO();

const float default_font_size = 12.0f;
// TODO(gibbed): disable imgui.ini saving for now,
// imgui assumes paths are char* so we can't throw a good path at it on
// Windows.
io.IniFilename = nullptr;

// Setup the font glyphs.
ImFontConfig font_config;
font_config.OversampleH = font_config.OversampleV = 2;
font_config.PixelSnapH = true;

if (!cvars::custom_font_path.empty() &&
std::filesystem::exists(cvars::custom_font_path)) {
const std::string font_path = xe::path_to_utf8(cvars::custom_font_path);
ImFont* font = io.Fonts->AddFontFromFileTTF(
font_path.c_str(), default_font_size, &font_config,
io.Fonts->GetGlyphRangesDefault());

io.Fonts->Build();
// Something went wrong while loading custom font. Probably corrupted.
if (!font->IsLoaded()) {
XELOGE("Failed to load custom font: {}", font_path);
io.Fonts->Clear();
}
}

if (io.Fonts->Fonts.empty()) {
io.Fonts->AddFontFromMemoryCompressedBase85TTF(
kProggyTinyCompressedDataBase85, default_font_size, &font_config,
io.Fonts->GetGlyphRangesDefault());
}

// TODO(benvanik): jp font on other platforms?
#if XE_PLATFORM_WIN32
PWSTR fonts_dir;
HRESULT result = SHGetKnownFolderPath(FOLDERID_Fonts, 0, NULL, &fonts_dir);
if (FAILED(result)) {
XELOGW("Unable to find Windows fonts directory");
return;
}

std::filesystem::path jp_font_path = std::wstring(fonts_dir);
jp_font_path.append("msgothic.ttc");
if (std::filesystem::exists(jp_font_path)) {
ImFontConfig jp_font_config;
jp_font_config.MergeMode = true;
jp_font_config.OversampleH = jp_font_config.OversampleV = 2;
jp_font_config.PixelSnapH = true;
jp_font_config.FontNo = 0;
io.Fonts->AddFontFromFileTTF(xe::path_to_utf8(jp_font_path).c_str(),
default_font_size, &jp_font_config,
io.Fonts->GetGlyphRangesJapanese());
} else {
XELOGW("Unable to load Japanese font; JP characters will be boxes");
}
CoTaskMemFree(static_cast<void*>(fonts_dir));
#endif
}

void ImGuiDrawer::SetupFontTexture() {
if (font_texture_ || !immediate_drawer_) {
return;
Expand Down
1 change: 1 addition & 0 deletions src/xenia/ui/imgui_drawer.h
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,7 @@ class ImGuiDrawer : public WindowInputListener, public UIDrawer {

private:
void Initialize();
void InitializeFonts();

void SetupFontTexture();

Expand Down

0 comments on commit cd3180b

Please sign in to comment.