Skip to content

Commit edafea5

Browse files
committed
Add: list_fonts console command to list available fonts.
1 parent 60b201c commit edafea5

3 files changed

Lines changed: 63 additions & 0 deletions

File tree

src/console_cmds.cpp

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,8 @@
2424
#include "fios.h"
2525
#include "fileio_func.h"
2626
#include "fontcache.h"
27+
#include "fontdetection.h"
28+
#include "language.h"
2729
#include "screenshot.h"
2830
#include "genworld.h"
2931
#include "strings_func.h"
@@ -46,6 +48,7 @@
4648
#include "company_cmd.h"
4749
#include "misc_cmd.h"
4850

51+
#include <charconv>
4952
#include <sstream>
5053

5154
#include "safeguards.h"
@@ -2125,6 +2128,54 @@ DEF_CONSOLE_CMD(ConContent)
21252128
}
21262129
#endif /* defined(WITH_ZLIB) */
21272130

2131+
/* List all the fonts available via console */
2132+
DEF_CONSOLE_CMD(ConListFonts)
2133+
{
2134+
if (argc == 0) {
2135+
IConsolePrint(CC_HELP, "List all fonts.");
2136+
return true;
2137+
}
2138+
2139+
FontSearcher *fs = FontSearcher::GetFontSearcher();
2140+
if (fs == nullptr) {
2141+
IConsolePrint(CC_ERROR, "No font searcher exists.");
2142+
return true;
2143+
}
2144+
2145+
if (argc == 1) {
2146+
auto families = fs->ListFamilies(_current_language->isocode, _current_language->winlangid);
2147+
std::sort(std::begin(families), std::end(families));
2148+
2149+
int i = 0;
2150+
for (auto &family : families) {
2151+
IConsolePrint(CC_DEFAULT, "{}) {}", i, family);
2152+
++i;
2153+
}
2154+
} else if (argc == 2) {
2155+
std::string family = argv[1];
2156+
2157+
/* If argv is a number treat it as an index into the list of fonts, which we need to get again... */
2158+
int index;
2159+
auto [_, err] = std::from_chars(family.data(), family.data() + family.size(), index, 10);
2160+
if (err == std::errc()) {
2161+
auto families = fs->ListFamilies(_current_language->isocode, _current_language->winlangid);
2162+
std::sort(std::begin(families), std::end(families));
2163+
if (IsInsideMM(index, 0, families.size())) family = families[index];
2164+
}
2165+
2166+
auto styles = fs->ListStyles(_current_language->isocode, _current_language->winlangid, family);
2167+
std::sort(std::begin(styles), std::end(styles), FontFamilySorter);
2168+
2169+
int i = 0;
2170+
for (auto &font : styles) {
2171+
IConsolePrint(CC_DEFAULT, "{}) {}, {}", i, font.family, font.style);
2172+
i++;
2173+
}
2174+
}
2175+
2176+
return true;
2177+
}
2178+
21282179
DEF_CONSOLE_CMD(ConFont)
21292180
{
21302181
if (argc == 0) {
@@ -2681,6 +2732,7 @@ void IConsoleStdLibRegister()
26812732
IConsole::CmdRegister("saveconfig", ConSaveConfig);
26822733
IConsole::CmdRegister("ls", ConListFiles);
26832734
IConsole::CmdRegister("list_saves", ConListFiles);
2735+
IConsole::CmdRegister("list_fonts", ConListFonts);
26842736
IConsole::CmdRegister("list_scenarios", ConListScenarios);
26852737
IConsole::CmdRegister("list_heightmaps", ConListHeightmaps);
26862738
IConsole::CmdRegister("cd", ConChangeDirectory);

src/fontcache.cpp

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -227,6 +227,15 @@ void UninitFontCache()
227227
#endif /* WITH_FREETYPE */
228228
}
229229

230+
bool FontFamilySorter(const FontFamily &a, const FontFamily &b)
231+
{
232+
int r = StrNaturalCompare(a.family, b.family);
233+
if (r == 0) r = (a.weight - b.weight);
234+
if (r == 0) r = (a.slant - b.slant);
235+
if (r == 0) r = StrNaturalCompare(a.style, b.style);
236+
return r < 0;
237+
}
238+
230239
/**
231240
* Register the FontSearcher instance. There can be only one font searcher, which depends on platform.
232241
*/

src/fontdetection.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,8 @@ struct FontFamily {
4848
FontFamily(std::string_view family, std::string_view style, int32_t slant, int32_t weight) : family(family), style(style), slant(slant), weight(weight) {}
4949
};
5050

51+
bool FontFamilySorter(const FontFamily &a, const FontFamily &b);
52+
5153
class FontSearcher {
5254
public:
5355
FontSearcher();

0 commit comments

Comments
 (0)