From 76b6485a33094b40e8f833c32dc84457055062ec Mon Sep 17 00:00:00 2001 From: salmonik Date: Mon, 26 Oct 2026 20:13:07 +0100 Subject: Fonts: bundle Liberation Sans and register it with fontconfig - ship assets/LiberationSans-Regular.ttf (+ OFL license/README) - at startup, resolve the .so path via dladdr, add assets/...ttf to the current fontconfig config with FcConfigAppFontAddFile, and fall back to the system Liberation path if the asset is missing - font creation tries Liberation Sans, then DejaVu Sans/Noto/Tahoma/Verdana/ Arial, so the overlay still renders on installs without the bundled font --- assets/LiberationSans-Regular.ttf | Bin 0 -> 410820 bytes assets/OFL.txt | 94 ++++++++++++++++++++++++++++++++++++++ assets/README.md | 9 ++++ src/menu.cpp | 87 +++++++++++++++++++++++++++++++---- 4 files changed, 180 insertions(+), 10 deletions(-) create mode 100644 assets/LiberationSans-Regular.ttf create mode 100644 assets/OFL.txt create mode 100644 assets/README.md diff --git a/assets/LiberationSans-Regular.ttf b/assets/LiberationSans-Regular.ttf new file mode 100644 index 0000000..adb8947 Binary files /dev/null and b/assets/LiberationSans-Regular.ttf differ diff --git a/assets/OFL.txt b/assets/OFL.txt new file mode 100644 index 0000000..445c414 --- /dev/null +++ b/assets/OFL.txt @@ -0,0 +1,94 @@ +Digitized data copyright (c) 2010 Google Corporation + with Reserved Font Arimo, Tinos and Cousine. +Copyright (c) 2012 Red Hat, Inc. + with Reserved Font Name Liberation. + +This Font Software is licensed under the SIL Open Font License, +Version 1.1. + +This license is copied below, and is also available with a FAQ at: +http://scripts.sil.org/OFL + +SIL OPEN FONT LICENSE Version 1.1 - 26 February 2007 + +PREAMBLE The goals of the Open Font License (OFL) are to stimulate +worldwide development of collaborative font projects, to support the font +creation efforts of academic and linguistic communities, and to provide +a free and open framework in which fonts may be shared and improved in +partnership with others. + +The OFL allows the licensed fonts to be used, studied, modified and +redistributed freely as long as they are not sold by themselves. +The fonts, including any derivative works, can be bundled, embedded, +redistributed and/or sold with any software provided that any reserved +names are not used by derivative works. The fonts and derivatives, +however, cannot be released under any other type of license. The +requirement for fonts to remain under this license does not apply to +any document created using the fonts or their derivatives. + +DEFINITIONS +"Font Software" refers to the set of files released by the Copyright +Holder(s) under this license and clearly marked as such. +This may include source files, build scripts and documentation. + +"Reserved Font Name" refers to any names specified as such after the +copyright statement(s). + +"Original Version" refers to the collection of Font Software components +as distributed by the Copyright Holder(s). + +"Modified Version" refers to any derivative made by adding to, deleting, +or substituting ? in part or in whole ? +any of the components of the Original Version, by changing formats or +by porting the Font Software to a new environment. + +"Author" refers to any designer, engineer, programmer, technical writer +or other person who contributed to the Font Software. + +PERMISSION & CONDITIONS + +Permission is hereby granted, free of charge, to any person obtaining a +copy of the Font Software, to use, study, copy, merge, embed, modify, +redistribute, and sell modified and unmodified copies of the Font +Software, subject to the following conditions: + +1) Neither the Font Software nor any of its individual components,in + Original or Modified Versions, may be sold by itself. + +2) Original or Modified Versions of the Font Software may be bundled, + redistributed and/or sold with any software, provided that each copy + contains the above copyright notice and this license. These can be + included either as stand-alone text files, human-readable headers or + in the appropriate machine-readable metadata fields within text or + binary files as long as those fields can be easily viewed by the user. + +3) No Modified Version of the Font Software may use the Reserved Font + Name(s) unless explicit written permission is granted by the + corresponding Copyright Holder. This restriction only applies to the + primary font name as presented to the users. + +4) The name(s) of the Copyright Holder(s) or the Author(s) of the Font + Software shall not be used to promote, endorse or advertise any + Modified Version, except to acknowledge the contribution(s) of the + Copyright Holder(s) and the Author(s) or with their explicit written + permission. + +5) The Font Software, modified or unmodified, in part or in whole, must + be distributed entirely under this license, and must not be distributed + under any other license. The requirement for fonts to remain under + this license does not apply to any document created using the Font + Software. + +TERMINATION +This license becomes null and void if any of the above conditions are not met. + +DISCLAIMER +THE FONT SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, +EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO ANY WARRANTIES OF +MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT +OF COPYRIGHT, PATENT, TRADEMARK, OR OTHER RIGHT. IN NO EVENT SHALL THE +COPYRIGHT HOLDER BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, +INCLUDING ANY GENERAL, SPECIAL, INDIRECT, INCIDENTAL, OR CONSEQUENTIAL +DAMAGES, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING +FROM, OUT OF THE USE OR INABILITY TO USE THE FONT SOFTWARE OR FROM OTHER +DEALINGS IN THE FONT SOFTWARE. diff --git a/assets/README.md b/assets/README.md new file mode 100644 index 0000000..a4afd45 --- /dev/null +++ b/assets/README.md @@ -0,0 +1,9 @@ +# Bundled font + +`LiberationSans-Regular.ttf` is the Liberation Sans regular face from the +Liberation Fonts project (https://github.com/liberationfonts/liberation-fonts). + +Licensed under the SIL Open Font License 1.1; see `OFL.txt`. + +`hl2hake` registers this file with fontconfig at startup so the VGUI overlay +does not depend on the host/game install having Liberation Sans installed. diff --git a/src/menu.cpp b/src/menu.cpp index 9537e05..1ef8638 100644 --- a/src/menu.cpp +++ b/src/menu.cpp @@ -4,7 +4,9 @@ #include #include #include +#include #include +#include // --------------------------------------------------------------------------- // ConMsg (tier0 console printer — prints to in-game console) @@ -217,6 +219,63 @@ static std::string config_path() { return dir + "/config"; } +// Register the bundled Liberation Sans with fontconfig so the VGUI overlay +// does not depend on the host/game install having it. The font file lives in +// assets/ next to the .so (or the system path as a fallback). +void register_bundled_font() { + static bool done = false; + if (done) + return; + done = true; + + std::string path; + Dl_info info; + if (dladdr((void *)®ister_bundled_font, &info) && info.dli_fname) { + char resolved[4096]; + const char *lib = realpath(info.dli_fname, resolved); + std::string p = lib ? resolved : info.dli_fname; + size_t slash = p.find_last_of('/'); + if (slash != std::string::npos) + p.resize(slash + 1); + else + p = "./"; + p += "assets/LiberationSans-Regular.ttf"; + if (access(p.c_str(), R_OK) == 0) + path = p; + } + if (path.empty()) { + const char *sys = "/usr/share/fonts/liberation/LiberationSans-Regular.ttf"; + if (access(sys, R_OK) == 0) + path = sys; + } + if (path.empty()) { + logf("font: bundled LiberationSans-Regular.ttf not found"); + return; + } + + void *fc = dlopen("libfontconfig.so.1", RTLD_LAZY | RTLD_GLOBAL); + if (!fc) { + logf("font: libfontconfig.so.1 not available"); + return; + } + using InitFn = int (*)(); + using GetCurrentFn = void *(*)(); + using AddFileFn = int (*)(void *, const char *); + auto init = (InitFn)dlsym(fc, "FcInit"); + auto get = (GetCurrentFn)dlsym(fc, "FcConfigGetCurrent"); + auto add = (AddFileFn)dlsym(fc, "FcConfigAppFontAddFile"); + if (!init || !get || !add) { + logf("font: fontconfig symbols missing"); + return; + } + init(); + void *cfg = get(); + if (cfg && add(cfg, path.c_str())) + logf("font: registered %s", path.c_str()); + else + logf("font: failed to register %s", path.c_str()); +} + static void config_save() { FILE *f = fopen(config_path().c_str(), "w"); if (!f) @@ -671,17 +730,24 @@ void outer_draw() { if (!g_surface || (!g_menu_open && !draw_test && !esp_on)) return; if (!g_font) { - g_font = surf_create_font(); - if (!g_font) { - logf("outer_draw: CreateFont failed"); - return; + static const char *families[] = { + "Liberation Sans", "DejaVu Sans", "Noto Sans", "Tahoma", + "Verdana", "Arial", nullptr}; + bool font_ok = false; + for (int fi = 0; families[fi] && !font_ok; fi++) { + g_font = surf_create_font(); + if (!g_font) + continue; + if (surf_set_font_glyph_set(g_font, families[fi], 16, 400, 0, 0, 0x400, + 0x20, 0xFF)) { + logf("outer_draw: font family %s", families[fi]); + font_ok = true; + } else { + g_font = 0; + } } - // FONTFLAG_CUSTOM only; Liberation Sans is an installed real TTF (Verdana - // substitutes to Noto). Raw glyphs, ASCII range pinned. - if (!surf_set_font_glyph_set(g_font, "Liberation Sans", 16, 400, 0, 0, - 0x400, 0x20, 0xFF)) { - logf("outer_draw: SetFontGlyphSet failed"); - g_font = 0; + if (!font_ok) { + logf("outer_draw: no usable font"); return; } } @@ -789,6 +855,7 @@ static ISurface *find_surface_interface() { void install_menu() { build_tree(); + register_bundled_font(); logf("config: %s", config_path().c_str()); config_load(); // resolve ISurface once (used by the DrawPrintText hook in hl2h.cpp and -- cgit v1.2.3