132 lines
4 KiB
C++
132 lines
4 KiB
C++
#include "render/ui_font.hpp"
|
|
|
|
#include <raylib.h>
|
|
|
|
#include "raygui.h"
|
|
|
|
#include <cstdio>
|
|
|
|
namespace {
|
|
namespace ui_font_impl {
|
|
|
|
Font font_ui{};
|
|
Font font_title{};
|
|
bool loaded = false;
|
|
|
|
const char* kCandidates[] = {
|
|
"/usr/share/fonts/adwaita-sans-fonts/AdwaitaSans-Regular.ttf",
|
|
"/usr/share/fonts/liberation-sans-fonts/LiberationSans-Regular.ttf",
|
|
"/usr/share/fonts/open-sans/OpenSans-Regular.ttf",
|
|
"/usr/share/fonts/google-carlito-fonts/Carlito-Regular.ttf",
|
|
"/usr/share/fonts/google-droid-sans-fonts/DroidSans.ttf",
|
|
"/usr/share/fonts/google-noto-vf/NotoSans-VF.ttf",
|
|
};
|
|
|
|
int BuildCodepoints(int* out, int cap) {
|
|
int n = 0;
|
|
for (int cp = 32; cp <= 126 && n < cap; ++cp) out[n++] = cp;
|
|
static const int kExtra[] = {0x00B7, 0x00B0, 0x00D7, 0x2022, 0x2013, 0x2014,
|
|
0x2026, 0x2190, 0x2191, 0x2192, 0x2193};
|
|
for (int cp : kExtra)
|
|
if (n < cap) out[n++] = cp;
|
|
return n;
|
|
}
|
|
|
|
} // namespace ui_font_impl
|
|
} // namespace
|
|
|
|
namespace ui_font {
|
|
|
|
void init() {
|
|
using namespace ui_font_impl;
|
|
int cps[128];
|
|
const int n = BuildCodepoints(cps, 128);
|
|
for (const char* path : kCandidates) {
|
|
if (!FileExists(path)) continue;
|
|
font_ui = LoadFontEx(path, 32, cps, n);
|
|
font_title = LoadFontEx(path, 64, cps, n);
|
|
if (font_ui.texture.id == 0 || font_title.texture.id == 0) {
|
|
UnloadFont(font_ui);
|
|
UnloadFont(font_title);
|
|
continue;
|
|
}
|
|
loaded = true;
|
|
TraceLog(LOG_INFO, "UI font: %s (atlases 32 px + 64 px)", path);
|
|
return;
|
|
}
|
|
TraceLog(LOG_WARNING,
|
|
"UI font: no system TTF found — falling back to raylib default bitmap font");
|
|
}
|
|
|
|
void shutdown() {
|
|
using namespace ui_font_impl;
|
|
if (!loaded) return;
|
|
UnloadFont(font_ui);
|
|
UnloadFont(font_title);
|
|
loaded = false;
|
|
}
|
|
|
|
void draw_text(const char* text, int x, int y, int size, Color color, float spacing) {
|
|
using namespace ui_font_impl;
|
|
if (!loaded) {
|
|
DrawText(text, x, y, size, color);
|
|
return;
|
|
}
|
|
const Font& f = size > 40 ? font_title : font_ui;
|
|
DrawTextEx(f, text, Vector2{static_cast<float>(x), static_cast<float>(y)},
|
|
static_cast<float>(size), spacing, color);
|
|
}
|
|
|
|
int measure_text(const char* text, int size, float spacing) {
|
|
using namespace ui_font_impl;
|
|
if (!loaded) return MeasureText(text, size);
|
|
const Font& f = size > 40 ? font_title : font_ui;
|
|
const Vector2 m = MeasureTextEx(f, text, static_cast<float>(size), spacing);
|
|
return static_cast<int>(m.x);
|
|
}
|
|
|
|
Font font() {
|
|
using namespace ui_font_impl;
|
|
if (loaded) return font_ui;
|
|
return GetFontDefault();
|
|
}
|
|
|
|
Font title_font() {
|
|
using namespace ui_font_impl;
|
|
if (loaded) return font_title;
|
|
return GetFontDefault();
|
|
}
|
|
|
|
void init_gui_style() {
|
|
using namespace ui_font_impl;
|
|
GuiSetStyle(DEFAULT, TEXT_SIZE, 18);
|
|
GuiSetStyle(DEFAULT, TEXT_LINE_SPACING, 8);
|
|
|
|
struct C { int control, property; unsigned value; };
|
|
static const C kColors[] = {
|
|
{DEFAULT, BORDER_COLOR_NORMAL, 0x404C66FF},
|
|
{DEFAULT, BASE_COLOR_NORMAL, 0x0D0F17FF},
|
|
{DEFAULT, TEXT_COLOR_NORMAL, 0xE8ECF5FF},
|
|
{DEFAULT, BORDER_COLOR_FOCUSED, 0xFF5C52FF},
|
|
{DEFAULT, BASE_COLOR_FOCUSED, 0x381B1EFF},
|
|
{DEFAULT, TEXT_COLOR_FOCUSED, 0xFFF0EEFF},
|
|
{DEFAULT, BORDER_COLOR_PRESSED, 0x6FDDA5FF},
|
|
{DEFAULT, BASE_COLOR_PRESSED, 0x123026FF},
|
|
{DEFAULT, TEXT_COLOR_PRESSED, 0xD8F6E8FF},
|
|
{DEFAULT, BORDER_COLOR_DISABLED, 0x232B3CFF},
|
|
{DEFAULT, BASE_COLOR_DISABLED, 0x10131CFF},
|
|
{DEFAULT, TEXT_COLOR_DISABLED, 0x5C6780FF},
|
|
{DEFAULT, LINE_COLOR, 0x404C66FF},
|
|
{DEFAULT, BACKGROUND_COLOR, 0x161924FF},
|
|
};
|
|
for (const C& c : kColors) GuiSetStyle(c.control, c.property,
|
|
static_cast<int>(c.value));
|
|
|
|
GuiSetStyle(CHECKBOX, BORDER_WIDTH, 2);
|
|
GuiSetStyle(TOGGLE, GROUP_WIDTH_FULL, 1);
|
|
|
|
GuiSetFont(font());
|
|
TraceLog(LOG_INFO, "raygui style: readable font installed");
|
|
}
|
|
|
|
} // namespace ui_font
|