Changeset - r26675:fc2319aeaced
[Not reviewed]
master
0 3 0
glx22 - 18 months ago 2022-12-22 21:54:24
glx@openttd.org
Add: 'font' console command to configure fonts
3 files changed with 127 insertions and 0 deletions:
0 comments (0 inline, 0 general)
src/console_cmds.cpp
Show inline comments
 
@@ -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] [<name>] [<size>] [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 <name> 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 <name> we want a string. */
 
				uint v;
 
				if (!GetArgumentInteger(&v, argv[arg_index])) {
 
					font = argv[arg_index++];
 
				}
 
			}
 
		}
 

	
 
		if (argc > arg_index) {
 
			/* For <size> 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);
src/fontcache.cpp
Show inline comments
 
@@ -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 
 
	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.
src/fontcache.h
Show inline comments
 
@@ -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 */
0 comments (0 inline, 0 general)