Files
client/src/gui/fonts.h
T

94 lines
3.6 KiB
C++

#pragma once
#include <windows.h>
#include <string>
#include "imgui.h"
#include "misc/freetype/imgui_freetype.h"
// optional fallback if the Segoe UI files can't be found
#if __has_include("../font_data.h")
const unsigned char* amnesia_fallback_font();
int amnesia_fallback_font_size();
#define AMNESIA_HAS_FONT 1
#else
#define AMNESIA_HAS_FONT 0
#endif
namespace fonts {
inline constexpr float k_body_size = 13.f; // reference menu Verdana size
inline constexpr float k_label_size = 11.f; // compact secondary labels
inline constexpr float k_heading_size = 13.f;
inline ImFont* body = nullptr; // Verdana, matching the reference menu
inline ImFont* semibold = nullptr;
inline ImFont* label = nullptr;
inline ImFont* heading = nullptr;
inline ImFont* media = nullptr; // original glyphs for song/artist metadata
inline void lowercase_display(ImFont* font) {
// Render capitals with their lowercase glyph, including matching advance
// widths for layout, wrapping and text-input caret placement. Keep source
// strings intact: module/profile identifiers and external data retain case.
// Apply before any glyphs are baked, at every font weight and size.
for (ImWchar upper = 'A'; upper <= 'Z'; ++upper)
font->AddRemapCodepoint(upper, upper + ('a' - 'A'));
}
// PushFont changed signature in ImGui 1.92 (fonts became size-flexible)
inline void push_font(ImFont* font, float size) {
#if IMGUI_VERSION_NUM >= 19200
ImGui::PushFont(font, size);
#else
(void)size;
ImGui::PushFont(font);
#endif
}
inline ImVec2 text_size(ImFont* font, float size, const char* text) {
push_font(font, size);
const ImVec2 ts = ImGui::CalcTextSize(text);
ImGui::PopFont();
return ts;
}
inline bool windows_font_path(const char* file, std::string& out) {
char dir[MAX_PATH] = {};
const UINT n = GetWindowsDirectoryA(dir, MAX_PATH);
if (n == 0 || n >= MAX_PATH) return false;
out = std::string(dir) + "\\Fonts\\" + file;
return GetFileAttributesA(out.c_str()) != INVALID_FILE_ATTRIBUTES;
}
inline ImFont* load_one(ImGuiIO& io, const char* file, float size) {
std::string path;
if (windows_font_path(file, path)) {
ImFontConfig config;config.PixelSnapH=true;
if (ImFont* f = io.Fonts->AddFontFromFileTTF(path.c_str(), size,&config)) return f;
}
#if AMNESIA_HAS_FONT
ImFontConfig cfg;
cfg.FontDataOwnedByAtlas = false;
cfg.PixelSnapH = true;
return io.Fonts->AddFontFromMemoryTTF((void*)amnesia_fallback_font(), amnesia_fallback_font_size(), size, &cfg);
#else
return io.Fonts->AddFontDefault();
#endif
}
// after ImGui::CreateContext(), before the backends are initialized
inline void load() {
ImGuiIO& io = ImGui::GetIO();
io.Fonts->SetFontLoader(ImGuiFreeType::GetFontLoader());
// Strong mono-grid hinting with native Verdana hints; keep grayscale antialiasing.
io.Fonts->FontLoaderFlags = ImGuiFreeTypeLoaderFlags_NoAutoHint | ImGuiFreeTypeLoaderFlags_MonoHinting;
body = load_one(io, "verdana.ttf", k_body_size);
semibold = load_one(io, "verdana.ttf", k_body_size);
label = load_one(io, "verdana.ttf", k_label_size);
heading = load_one(io, "verdana.ttf", k_heading_size);
for (ImFont* font : {body, semibold, label, heading}) lowercase_display(font);
media = load_one(io, "verdana.ttf", k_body_size);
io.FontDefault = body;
}
} // namespace fonts