Something went wrong. Try again.
🎴 Comfortable GUI port of ddhx to SDL3 & ddui
Something went wrong. Try again.
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825/// ddui draw commands to SDL3's renderer.////// Text and UI icons go through SDL3_ttf's renderer text engine; its atlas/// spans all open faces, so one string can mix base, CJK and symbol glyphs./// Authors: dd86k <dd@dax.moe>module render;
import core.stdc.string : strlen;import std.file : exists, isFile;import std.format : format;import std.math : ceil, lround;import std.path : buildPath, expandTilde;import std.string : fromStringz, toStringz;
import bindbc.sdl; // re-exports TTF_* in the static config
import ddlogger;import ddui;import elite : MU_COMMAND_SHIP, elite_draw;import preferences : FONT_SIZE, FONT_SIZE_MAX, FONT_SIZE_MIN;
/// Text size in points, before density. Sets the scale only,/// render_text_height reports actual line height.private __gshared float fontSize = FONT_SIZE;
// Output pixels per point. ddui works in points; geometry is mapped here and// faces are sized to match, so glyphs rasterize at output resolution.private __gshared float density = 1;
private __gshared TTF_TextEngine* engine;
// Both share the fallback chain.private __gshared TTF_Font* fontUI;private __gshared TTF_Font* fontMono;
// Font setting each face was opened from, empty for the default.private __gshared string uiSpec, monoSpec;
// Built-in face only: no system faces, fallbacks, or font settings.private __gshared bool builtinOnly;
// Kept so render_quit can close them after the primaries.private __gshared TTF_Font*[8] fallbacks;private __gshared size_t fallbackCount;
private struct Icon{ dchar code; immutable(char)* glyph; immutable(char)* plain;}
// Drawn with the UI face and fallbacks. Index 0 unused. `plain` is used when// no face has the codepoint.private immutable Icon[MU_ICON_MAX] icons = [ MU_ICON_CLOSE: Icon('✕', "✕", "x"), MU_ICON_CHECK: Icon('✓', "✓", "*"), MU_ICON_COLLAPSED: Icon('▶', "▶", ">"), MU_ICON_EXPANDED: Icon('▼', "▼", "v"), MU_ICON_DROPDOWN: Icon('▾', "▾", "v"),];
// Resolved after the faces are open.private __gshared immutable(char)*[MU_ICON_MAX] iconText;
// Shaped text cache, keyed by face and content. The hex view draws two strings// per visible byte, and reshaping dominated frame time. Entries unused for// TEXT_TTL frames are evicted.private struct TextKey{ const(TTF_Font)* font; const(char)[] str;
size_t toHash() const nothrow @trusted => hashOf(str, cast(size_t) font); bool opEquals(ref const(TextKey) o) const nothrow @safe => font is o.font && str == o.str;}private struct TextEntry{ TTF_Text* text; uint frame;}private enum uint TEXT_TTL = 64;private __gshared TextEntry[TextKey] textCache;private __gshared TextKey[] textStale; // reused by sweep_textprivate __gshared uint textFrame;
// Font files tried in order in each font_dirs directory. May include a subdir.version (Windows){ private immutable string[] uiNames = [ "NotoSans-Regular.ttf", "segoeui.ttf", "arial.ttf", ]; private immutable string[] monoNames = [ "NotoSansMono-Regular.ttf", "consola.ttf", ]; private immutable string[] cjkNames = [ "NotoSansCJKsc-Regular.otf", "msgothic.ttc", ]; private immutable string[] symNames = [ "NotoSansSymbols-Regular.ttf", "seguisym.ttf", ];}else version (OSX){ private immutable string[] uiNames = [ "NotoSans-Regular.ttf", "SFNS.ttf", "Helvetica.ttc", ]; private immutable string[] monoNames = [ "NotoSansMono-Regular.ttf", "SFNSMono.ttf", "Menlo.ttc", "Monaco.ttf", ]; private immutable string[] cjkNames = [ "NotoSansCJK-Regular.ttc", "Hiragino Sans GB.ttc", "Supplemental/Arial Unicode.ttf", ]; private immutable string[] symNames = [ "NotoSansSymbols2-Regular.ttf", "Apple Symbols.ttf", "Supplemental/Arial Unicode.ttf", ];}else{ // Without fontconfig: common distro paths. private immutable string[] uiNames = [ "truetype/noto/NotoSans-Regular.ttf", "noto/NotoSans-Regular.ttf", "TTF/NotoSans-Regular.ttf", "truetype/dejavu/DejaVuSans.ttf", "dejavu/DejaVuSans.ttf", "TTF/DejaVuSans.ttf", "truetype/liberation/LiberationSans-Regular.ttf", "liberation/LiberationSans-Regular.ttf", ]; private immutable string[] monoNames = [ "truetype/noto/NotoSansMono-Regular.ttf", "noto/NotoSansMono-Regular.ttf", "TTF/NotoSansMono-Regular.ttf", "truetype/dejavu/DejaVuSansMono.ttf", "dejavu/DejaVuSansMono.ttf", "TTF/DejaVuSansMono.ttf", "truetype/liberation/LiberationMono-Regular.ttf", "liberation/LiberationMono-Regular.ttf", ]; private immutable string[] cjkNames = [ "opentype/noto/NotoSansCJK-Regular.ttc", "truetype/noto/NotoSansCJK-Regular.ttc", "noto-cjk/NotoSansCJK-Regular.ttc", ]; private immutable string[] symNames = [ "truetype/noto/NotoSansSymbols2-Regular.ttf", "truetype/noto/NotoSansSymbols-Regular.ttf", "noto/NotoSansSymbols2-Regular.ttf", ];}
// Set by render_init; per-user on Windows and macOS.private __gshared string[] fontDirs;
/// DejaVu Sans Mono, built in. UI and mono face when nothing else opens.private immutable ubyte[] embeddedFont = cast(immutable(ubyte)[]) import("DejaVuSansMono.ttf");
version (OSX) {}else version (Posix) version = Fontconfig;
/// Create the window's renderer. SDL picks the driver, falling back to/// software. An unusable `SDL_RENDER_DRIVER` is warned about and ignored./// Returns: null on failure (see SDL_GetError).SDL_Renderer* render_create(SDL_Window* window){ SDL_Renderer* renderer = SDL_CreateRenderer(window, null);
const(char)* wanted = SDL_GetHint(SDL_HINT_RENDER_DRIVER); if (renderer is null && wanted && *wanted) { logWarn(`renderer "%s" unavailable: %s (have: %-(%s, %)); letting SDL choose`, fromStringz(wanted), SDL_GetError().fromStringz, driverNames()); // Empty, not reset: reset falls back to the env var. SDL_SetHintWithPriority(SDL_HINT_RENDER_DRIVER, "", SDL_HINT_OVERRIDE); renderer = SDL_CreateRenderer(window, null); }
if (renderer) logInfo("renderer: %s", SDL_GetRendererName(renderer).fromStringz); return renderer;}
/// Use only the built-in face, see `--builtin-font`. Call before render_init.void render_set_builtin(bool on) { builtinOnly = on; }
/// Whether only the built-in face is used.bool render_builtin() => builtinOnly;
/// Init SDL3_ttf, the text engine and font faces. Call once after/// render_create./// Returns: null on success, else a user-facing reason.string render_init(SDL_Renderer* renderer){ if (TTF_Init() == false) return format("TTF_Init: %s", SDL_GetError().fromStringz);
engine = TTF_CreateRendererTextEngine(renderer); if (engine is null) return format("TTF_CreateRendererTextEngine: %s", SDL_GetError().fromStringz);
// Otherwise translucent fills (minimap viewport) are opaque and the // transparent MU_COLOR_PANELBG paints black. SDL_SetRenderDrawBlendMode(renderer, SDL_BLENDMODE_BLEND);
fontDirs = font_dirs();
// Missing faces are reported without SDL_GetError: SDL wasn't involved. fontUI = openDefault(false); if (fontUI is null) { logCritical("no UI font; looked for %-(%s, %) in %-(%s, %)", uiNames, fontDirs); return "No font found.\n\nInstall a TrueType font: fonts-noto or " ~ "fonts-dejavu will do."; }
// No mono face: not fatal, hex panel is just unaligned. fontMono = openDefault(true); if (fontMono is null) { logWarn("no monospace font, falling back to the UI face (the hex grid " ~ "will not align); install one of: %s", monoNames); fontMono = fontUI; }
// Probe glyphs are verified: fontconfig returns a best effort even when // nothing matches. if (builtinOnly == false) { addFallback("CJK", ":lang=ja", '一', cjkNames); addFallback("symbol", format(":charset=%04X", cast(uint) icons[MU_ICON_CLOSE].code), icons[MU_ICON_CLOSE].code, symNames); }
resolveIcons(); return null;}
/// Close every face and tear down the text engine and SDL3_ttf.void render_quit(){ foreach (ref TextEntry e; textCache) TTF_DestroyText(e.text); textCache = null;
foreach (ref f; fallbacks[0 .. fallbackCount]) { TTF_CloseFont(f); f = null; } fallbackCount = 0;
if (fontMono && fontMono !is fontUI) TTF_CloseFont(fontMono); fontMono = null;
if (fontUI) TTF_CloseFont(fontUI); fontUI = null; uiSpec = monoSpec = null;
if (engine) { TTF_DestroyRendererTextEngine(engine); engine = null; } TTF_Quit();}
/// Proportional UI face; set as ctx.style.font after mu_init.TTF_Font* render_font_ui() => fontUI;
/// Monospace face for the hex panel. Swap into ctx.style.font around it.TTF_Font* render_font_mono() => fontMono;
/// Font setting the UI or hex panel (`mono`) face was opened from, empty for/// the default.string render_font_spec(bool mono) => mono ? monoSpec : uiSpec;
/// Replace the UI or hex panel face with `spec`: a font file, or a font name/// (fontconfig, else a file in the system font folders). Empty restores the/// default face, as does any `spec` under `--builtin-font`. False if nothing/// opened, the current face stays.bool render_set_font(bool mono, string spec){ if (builtinOnly) spec = null; string* current = mono ? &monoSpec : &uiSpec; if (spec == *current) return true;
string role = mono ? "mono" : "UI"; TTF_Font* f = spec.length ? openCustom(role, spec) : openDefault(mono); if (f is null) { logWarn("%s font not found: %s", role, spec); return false; } if (mono && TTF_FontIsFixedWidth(f) == false) logInfo("%s is not monospace, the hex grid will not align", spec);
// Shaped text holds the old face. flushText(); foreach (TTF_Font* fb; fallbacks[0 .. fallbackCount]) TTF_AddFallbackFont(f, fb);
TTF_Font** slot = mono ? &fontMono : &fontUI; TTF_Font* old = *slot; *slot = f; // Still in use when the mono face fell back to the UI face. if (old !is fontUI && old !is fontMono) TTF_CloseFont(old); *current = spec;
if (mono == false) resolveIcons(); return true;}
/// Set the window pixel density; 0 (SDL_GetWindowPixelDensity failure) is/// ignored.void render_set_density(float d){ if (d <= 0 || d == density) return; logInfo("pixel density: %g", d); density = d; resize();}
/// Set the text size in points, clamped to FONT_SIZE_MIN..FONT_SIZE_MAX.void render_set_size(int pt){ if (pt < FONT_SIZE_MIN) pt = FONT_SIZE_MIN; if (pt > FONT_SIZE_MAX) pt = FONT_SIZE_MAX; if (pt == fontSize) return; fontSize = pt; resize();}
/// Text size in points.int render_size() => cast(int) fontSize;
/// mu_Context text measuring callbacks. `font` is a TTF_Font*.extern (C) int render_text_width(mu_Font font, const(char)* str, int len){ if (len < 0) len = cast(int) strlen(str); // TTF_CreateText treats length 0 as NUL-terminated, running off the slice. if (len == 0) return 0; TTF_Text* text = cached_text(cast(TTF_Font*) font, str[0 .. len]); if (text is null) return 0; int w; TTF_GetTextSize(text, &w, null); return toPoints(w);}
/// dittoextern (C) int render_text_height(mu_Font font){ TTF_Font* f = cast(TTF_Font*) font; if (f is null) f = fontUI; return toPoints(TTF_GetFontHeight(f));}
/// Replay ddui draw commands onto the renderer.////// Must use mu_get_next_command, not mu_command_range: only it follows the/// JUMP commands that order root containers by z-index (popups on top).void render_commands(SDL_Renderer* renderer, mu_Context* ctx){ mu_Command* cmd; while (mu_get_next_command(ctx, &cmd)) { switch (cmd.type) { case MU_COMMAND_RECT: draw_rect(renderer, toPixels(cmd.rect.rect), cmd.rect.color); break; case MU_COMMAND_TEXT: draw_text(cmd.text.font, mu_command_text(ctx, cmd), cmd.text.pos, cmd.text.color); break; case MU_COMMAND_ICON: draw_icon(cmd.icon.id, toPixels(cmd.icon.rect), cmd.icon.color); break; case MU_COMMAND_SHIP: // elite.d elite_draw(renderer, toPixels(cmd.rect.rect)); break; case MU_COMMAND_CLIP: mu_Rect r = toPixels(cmd.clip.rect); SDL_Rect clip = SDL_Rect(r.x, r.y, r.w, r.h); SDL_SetRenderClipRect(renderer, &clip); break; default: } } // Leave clipping disabled for whatever is drawn after us. SDL_SetRenderClipRect(renderer, null); sweep_text();}
private:
// Map edges, not sizes, so adjacent rects meet at fractional densities.mu_Rect toPixels(mu_Rect r){ int x = toPixels(r.x), y = toPixels(r.y); return mu_Rect(x, y, toPixels(r.x + r.w) - x, toPixels(r.y + r.h) - y);}int toPixels(int v) => cast(int) lround(v * density);
/// Apply fontSize and density to every face and drop shaped text.void resize(){ float px = fontSize * density; TTF_SetFontSize(fontUI, px); if (fontMono !is fontUI) TTF_SetFontSize(fontMono, px); foreach (TTF_Font* f; fallbacks[0 .. fallbackCount]) TTF_SetFontSize(f, px);
flushText();}
/// Destroy all shaped text.void flushText(){ foreach (ref TextEntry e; textCache) TTF_DestroyText(e.text); textCache = null;}
/// Pick each icon's glyph, or its plain text if no face has it. Run after the/// fallback chain is built: TTF_FontHasGlyph checks fallbacks too.void resolveIcons(){ foreach (size_t i, ref immutable(Icon) icon; icons) { if (icon.glyph is null) continue; bool has = TTF_FontHasGlyph(fontUI, icon.code); iconText[i] = has ? icon.glyph : icon.plain; if (has == false) logInfo("no glyph for U+%04X, drawing icon %u as '%s'", cast(uint) icon.code, cast(uint) i, icon.plain.fromStringz); }}
/// Default UI or mono face: system match, then the embedded one. Null if none.TTF_Font* openDefault(bool mono){ if (builtinOnly) return openEmbedded(mono ? "mono" : "UI"); TTF_Font* f = mono ? openFace("mono", "monospace", 0, monoNames) : openFace("UI", "sans-serif", 0, uiNames); return f ? f : openEmbedded(mono ? "mono" : "UI");}
/// Face from a font setting: file path, then font name. Null if none.TTF_Font* openCustom(string role, string spec){ string path = expandTilde(spec); if (TTF_Font* f = openPath(role, path, 0)) return f;
string matched = fc_match(spec, true); if (matched.length) if (TTF_Font* f = openPath(role, matched, 0)) return f;
static immutable string[4] exts = [ "", ".ttf", ".otf", ".ttc" ]; foreach (string dir; fontDirs) foreach (string ext; exts) if (TTF_Font* f = openPath(role, buildPath(dir, spec ~ ext), 0)) return f; return null;}
// Round up so text-sized boxes don't clip.int toPoints(int v) => cast(int) ceil(v / density);
string[] driverNames(){ int count = SDL_GetNumRenderDrivers(); string[] names = new string[count]; foreach (int i; 0 .. count) names[i] = SDL_GetRenderDriver(i).fromStringz.idup; return names;}
// Open a face: fontconfig match first, then known files.// `probe`: required codepoint, or 0.TTF_Font* openFace(string role, string pattern, dchar probe, const(string)[] names){ string matched = fc_match(pattern); if (matched.length) if (TTF_Font* f = openPath(role, matched, probe)) return f;
foreach (string name; names) foreach (string dir; fontDirs) if (TTF_Font* f = openPath(role, buildPath(dir, name), probe)) return f;
return null;}
TTF_Font* openPath(string role, string path, dchar probe){ if (exists(path) == false || isFile(path) == false) return null; TTF_Font* f = TTF_OpenFont(path.toStringz, fontSize * density); if (f is null) return null; if (probe == 0 || TTF_FontHasGlyph(f, probe)) { logInfo("%s face: %s", role, path); return f; } TTF_CloseFont(f); return null;}
/// Open the built-in face for `role`, or null.TTF_Font* openEmbedded(string role){ SDL_IOStream* io = SDL_IOFromConstMem(embeddedFont.ptr, embeddedFont.length); if (io is null) return null; TTF_Font* f = TTF_OpenFontIO(io, true, fontSize * density); if (f is null) { logWarn("embedded font: %s", SDL_GetError().fromStringz); return null; } logInfo("%s face: embedded DejaVu Sans Mono", role); return f;}
// Open a fallback face and add it to both primaries.void addFallback(string role, string pattern, dchar probe, const(string)[] names){ if (fallbackCount >= fallbacks.length) return; TTF_Font* f = openFace(role, pattern, probe, names); if (f is null) { logInfo("no %s face; those glyphs will draw blank", role); return; } fallbacks[fallbackCount++] = f; TTF_AddFallbackFont(fontUI, f); if (fontMono !is fontUI) TTF_AddFallbackFont(fontMono, f);}
// fontconfig loaded at runtime: optional, SDL3_ttf doesn't need it and// containers may lack it.version (Fontconfig){ import core.sys.posix.dlfcn : RTLD_LAZY, dlopen, dlsym;
extern (C) nothrow @nogc { alias fcInit_t = int function(); alias fcNameParse_t = void* function(const(char)*); alias fcConfigSubstitute_t = int function(void*, void*, int); alias fcDefaultSubstitute_t = void function(void*); alias fcFontMatch_t = void* function(void*, void*, int*); alias fcPatternGetString_t = int function(void*, const(char)*, int, const(char)**); alias fcPatternDestroy_t = void function(void*); }
private __gshared fcNameParse_t FcNameParse; private __gshared fcConfigSubstitute_t FcConfigSubstitute; private __gshared fcDefaultSubstitute_t FcDefaultSubstitute; private __gshared fcFontMatch_t FcFontMatch; private __gshared fcPatternGetString_t FcPatternGetString; private __gshared fcPatternDestroy_t FcPatternDestroy; private __gshared bool fcTried;
// Returns: matched file, or null (no library or no fonts). `exact`: also // null unless the match has the pattern's family, since fontconfig always // returns its best effort. string fc_match(string pattern, bool exact = false) { if (fcOpen() == false) return null;
void* pat = FcNameParse(pattern.toStringz); if (pat is null) return null; scope(exit) FcPatternDestroy(pat);
// Read before substitution adds aliases. string family; const(char)* fam; if (exact && FcPatternGetString(pat, "family", 0, &fam) == FcResultMatch && fam) family = fam.fromStringz.idup;
// Standard pre-match setup: apply config rules, then defaults. FcConfigSubstitute(null, pat, FcMatchPattern); FcDefaultSubstitute(pat);
int result; void* match = FcFontMatch(null, pat, &result); if (match is null) return null; scope(exit) FcPatternDestroy(match);
const(char)* file; if (FcPatternGetString(match, "file", 0, &file) != FcResultMatch || file is null) return null; if (family.length && fc_has_family(match, family) == false) return null; return file.fromStringz.idup; }
// Whether any of the match's family names is `family`, case-insensitive. private bool fc_has_family(void* match, string family) { import std.uni : icmp;
const(char)* name; for (int i; FcPatternGetString(match, "family", i, &name) == FcResultMatch; ++i) if (name && icmp(name.fromStringz, family) == 0) return true; return false; }
private enum FcMatchPattern = 0; private enum FcResultMatch = 0;
private bool fcOpen() { if (fcTried) return FcFontMatch !is null; fcTried = true;
void* lib = dlopen("libfontconfig.so.1", RTLD_LAZY); if (lib is null) lib = dlopen("libfontconfig.so", RTLD_LAZY); if (lib is null) { logInfo("no fontconfig, falling back to known font paths"); return false; }
fcInit_t FcInit; bool bind(T)(ref T fn, string name) { fn = cast(T) dlsym(lib, name.ptr); // literals are NUL-terminated return fn !is null; } if (bind(FcInit, "FcInit") == false || bind(FcNameParse, "FcNameParse") == false || bind(FcConfigSubstitute, "FcConfigSubstitute") == false || bind(FcDefaultSubstitute, "FcDefaultSubstitute") == false || bind(FcFontMatch, "FcFontMatch") == false || bind(FcPatternGetString, "FcPatternGetString") == false || bind(FcPatternDestroy, "FcPatternDestroy") == false) { logWarn("fontconfig is missing symbols, ignoring it"); FcFontMatch = null; return false; }
if (FcInit() == 0) { logWarn("FcInit failed, ignoring fontconfig"); FcFontMatch = null; return false; } return true; }}else{ // No fontconfig on Windows and macOS. string fc_match(string pattern, bool exact = false) => null;}
version (Windows){ import core.sys.windows.basetyps : GUID; import core.sys.windows.objbase : CoTaskMemFree; import core.sys.windows.windef : DWORD, HANDLE, HRESULT; import std.conv : to;
pragma(lib, "shell32"); pragma(lib, "ole32");
// Not in druntime. extern (Windows) nothrow @nogc HRESULT SHGetKnownFolderPath(const(GUID)* rfid, DWORD flags, HANDLE token, wchar** path);
private immutable GUID FOLDERID_Fonts = { 0xFD228CB7, 0xAE11, 0x4AE3, [ 0x86, 0x4C, 0x16, 0xF3, 0x91, 0x0A, 0xB8, 0xFE ] }; private immutable GUID FOLDERID_LocalAppData = { 0xF1B32785, 0x6FBA, 0x4FCF, [ 0x9D, 0x55, 0x7B, 0x8E, 0x7F, 0x15, 0x70, 0x91 ] };
private string knownFolder(ref immutable(GUID) id) { wchar* path; HRESULT hr = SHGetKnownFolderPath(&id, 0, null, &path); scope(exit) CoTaskMemFree(path); // required even on failure return hr == 0 ? path.fromStringz.to!string : null; }
// Per-user font installs (Windows 10 1809+) go to the user's folder. string[] font_dirs() { string[] dirs; if (string sys = knownFolder(FOLDERID_Fonts)) dirs ~= sys; if (string local = knownFolder(FOLDERID_LocalAppData)) dirs ~= buildPath(local, `Microsoft\Windows\Fonts`); return dirs; }}else version (OSX){ import std.process : environment;
string[] font_dirs() { string[] dirs = [ "/System/Library/Fonts", "/Library/Fonts" ]; if (string home = environment.get("HOME")) dirs ~= buildPath(home, "Library/Fonts"); return dirs; }}else{ string[] font_dirs() => [ "/usr/share/fonts", "/usr/local/share/fonts" ];}
void draw_rect(SDL_Renderer* renderer, mu_Rect rect, mu_Color color){ if (color.a == 0) // transparent: nothing to draw return; SDL_SetRenderDrawColor(renderer, color.r, color.g, color.b, color.a); SDL_FRect dst = SDL_FRect(rect.x, rect.y, rect.w, rect.h); SDL_RenderFillRect(renderer, &dst);}
void draw_text(mu_Font font, const(char)* str, mu_Vec2 pos, mu_Color color){ TTF_Text* text = cached_text(cast(TTF_Font*) font, str[0 .. strlen(str)]); if (text is null) return;
TTF_SetTextColor(text, color.r, color.g, color.b, color.a); TTF_DrawRendererText(text, toPixels(pos.x), toPixels(pos.y));}
void draw_icon(int id, mu_Rect rect, mu_Color color){ if (id <= 0 || id >= MU_ICON_MAX) return; immutable(char)* glyph = iconText[id]; if (glyph is null) return;
TTF_Text* text = cached_text(fontUI, glyph[0 .. strlen(glyph)]); if (text is null) return;
TTF_SetTextColor(text, color.r, color.g, color.b, color.a);
int w, h; TTF_GetTextSize(text, &w, &h); int x = rect.x + (rect.w - w) / 2; int y = rect.y + (rect.h - h) / 2; TTF_DrawRendererText(text, x, y);}
TTF_Text* cached_text(TTF_Font* font, const(char)[] str){ if (font is null) font = fontUI; if (TextEntry* e = TextKey(font, str) in textCache) { e.frame = textFrame; return e.text; } TTF_Text* text = TTF_CreateText(engine, font, str.ptr, str.length); if (text is null) return null; textCache[TextKey(font, str.idup)] = TextEntry(text, textFrame); return text;}
void sweep_text(){ if (++textFrame % TEXT_TTL) return; textStale.length = 0; textStale.assumeSafeAppend(); foreach (TextKey key, ref TextEntry e; textCache) if (textFrame - e.frame >= TEXT_TTL) textStale ~= key; foreach (ref TextKey key; textStale) { TTF_DestroyText(textCache[key].text); textCache.remove(key); }}