From 04ee86d3ace52b0758d76b9ddadff75d7a12251a Mon Sep 17 00:00:00 2001 From: glx22 Date: Thu, 22 Dec 2022 22:54:24 +0100 Subject: [PATCH] Add: 'font' console command to configure fonts --- src/console_cmds.cpp | 78 ++++++++++++++++++++++++++++++++++++++++++++ src/fontcache.cpp | 48 +++++++++++++++++++++++++++ src/fontcache.h | 1 + 3 files changed, 127 insertions(+) diff --git a/src/console_cmds.cpp b/src/console_cmds.cpp index 1718ecce26..33b47d9600 100644 --- a/src/console_cmds.cpp +++ b/src/console_cmds.cpp @@ -23,6 +23,7 @@ #include "settings_func.h" #include "fios.h" #include "fileio_func.h" +#include "fontcache.h" #include "screenshot.h" #include "genworld.h" #include "strings_func.h" @@ -1982,6 +1983,82 @@ DEF_CONSOLE_CMD(ConContent) } #endif /* defined(WITH_ZLIB) */ +DEF_CONSOLE_CMD(ConFont) +{ + if (argc == 0) { + IConsolePrint(CC_HELP, "Manage the fonts configuration."); + IConsolePrint(CC_HELP, "Usage 'font'."); + IConsolePrint(CC_HELP, " Print out the fonts configuration."); + IConsolePrint(CC_HELP, "Usage 'font [medium|small|large|mono] [] [] [aa|noaa]'."); + IConsolePrint(CC_HELP, " Change the configuration for a font."); + IConsolePrint(CC_HELP, " Omitting an argument will keep the current value."); + IConsolePrint(CC_HELP, " Set to \"\" for the sprite font (size and aa have no effect on sprite font)."); + return true; + } + + FontSize argfs; + for (argfs = FS_BEGIN; argfs < FS_END; argfs++) { + if (argc > 1 && strcasecmp(argv[1], FontSizeToName(argfs)) == 0) break; + } + + /* First argument must be a FontSize. */ + if (argc > 1 && argfs == FS_END) return false; + + if (argc > 2) { + FontCacheSubSetting *setting = GetFontCacheSubSetting(argfs); + std::string font = setting->font; + uint size = setting->size; + bool aa = setting->aa; + + byte arg_index = 2; + + if (argc > arg_index) { + /* We may encounter "aa" or "noaa" but it must be the last argument. */ + if (strcasecmp(argv[arg_index], "aa") == 0 || strcasecmp(argv[arg_index], "noaa") == 0) { + aa = strncasecmp(argv[arg_index++], "no", 2) != 0; + if (argc > arg_index) return false; + } else { + /* For we want a string. */ + uint v; + if (!GetArgumentInteger(&v, argv[arg_index])) { + font = argv[arg_index++]; + } + } + } + + if (argc > arg_index) { + /* For we want a number. */ + uint v; + if (GetArgumentInteger(&v, argv[arg_index])) { + size = v; + arg_index++; + } + } + + if (argc > arg_index) { + /* Last argument must be "aa" or "noaa". */ + if (strcasecmp(argv[arg_index], "aa") != 0 && strcasecmp(argv[arg_index], "noaa") != 0) return false; + aa = strncasecmp(argv[arg_index++], "no", 2) != 0; + if (argc > arg_index) return false; + } + + SetFont(argfs, font, size, aa); + } + + for (FontSize fs = FS_BEGIN; fs < FS_END; fs++) { + FontCache *fc = FontCache::Get(fs); + FontCacheSubSetting *setting = GetFontCacheSubSetting(fs); + /* Make sure all non sprite fonts are loaded. */ + if (!setting->font.empty() && !fc->HasParent()) { + InitFontCache(fs == FS_MONO); + fc = FontCache::Get(fs); + } + IConsolePrint(CC_DEFAULT, "{}: \"{}\" {} {} [\"{}\" {} {}]", FontSizeToName(fs), fc->GetFontName(), fc->GetFontSize(), GetFontAAState(fs), setting->font, setting->size, setting->aa); + } + + return true; +} + DEF_CONSOLE_CMD(ConSetting) { if (argc == 0) { @@ -2480,6 +2557,7 @@ void IConsoleStdLibRegister() IConsole::CmdRegister("cd", ConChangeDirectory); IConsole::CmdRegister("pwd", ConPrintWorkingDirectory); IConsole::CmdRegister("clear", ConClearBuffer); + IConsole::CmdRegister("font", ConFont); IConsole::CmdRegister("setting", ConSetting); IConsole::CmdRegister("setting_newgame", ConSettingNewgame); IConsole::CmdRegister("list_settings", ConListSettings); diff --git a/src/fontcache.cpp b/src/fontcache.cpp index 5eec40a215..546199bc1f 100644 --- a/src/fontcache.cpp +++ b/src/fontcache.cpp @@ -13,6 +13,11 @@ #include "blitter/factory.hpp" #include "gfx_layout.h" #include "fontcache/spritefontcache.h" +#include "openttd.h" +#include "settings_func.h" +#include "strings_func.h" +#include "viewport_func.h" +#include "window_func.h" #include "safeguards.h" @@ -73,6 +78,49 @@ bool GetFontAAState(FontSize size, bool check_blitter) return GetFontCacheSubSetting(size)->aa; } +void SetFont(FontSize fontsize, const std::string& font, uint size, bool aa) +{ + FontCacheSubSetting *setting = GetFontCacheSubSetting(fontsize); + bool changed = false; + + if (setting->font != font) { + setting->font = font; + changed = true; + } + + if (setting->size != size) { + setting->size = size; + changed = true; + } + + if (setting->aa != aa) { + setting->aa = aa; + changed = true; + } + + if (!changed) return; + + if (fontsize != FS_MONO) { + /* Try to reload only the modified font. */ + FontCacheSettings backup = _fcsettings; + for (FontSize fs = FS_BEGIN; fs < FS_END; fs++) { + if (fs == fontsize) continue; + FontCache *fc = FontCache::Get(fs); + GetFontCacheSubSetting(fs)->font = fc->HasParent() ? fc->GetFontName() : ""; + } + CheckForMissingGlyphs(); + _fcsettings = backup; + } else { + InitFontCache(true); + } + + LoadStringWidthTable(); + UpdateAllVirtCoords(); + ReInitAllWindows(true); + + if (_save_config) SaveToConfig(); +} + /** * (Re)initialize the font cache related things, i.e. load the non-sprite fonts. * @param monospace Whether to initialise the monospace or regular fonts. diff --git a/src/fontcache.h b/src/fontcache.h index 7a86c137f8..efc1496304 100644 --- a/src/fontcache.h +++ b/src/fontcache.h @@ -239,5 +239,6 @@ void UninitFontCache(); bool HasAntialiasedFonts(); bool GetFontAAState(FontSize size, bool check_blitter = true); +void SetFont(FontSize fontsize, const std::string &font, uint size, bool aa); #endif /* FONTCACHE_H */