From 508e4c635c6ae2ebf44f8a47b8764297c0d2f4c4 Mon Sep 17 00:00:00 2001 From: Trihedraf Date: Wed, 24 Mar 2021 10:00:43 -0700 Subject: [PATCH] Add --ttf-dir to specify location of path to ttf (#1253) --- Source/diablo.cpp | 6 ++++++ SourceS/paths.cpp | 39 ++++++++++++++++++++++++++++++++++++++ SourceS/paths.h | 4 ++++ SourceX/DiabloUI/fonts.cpp | 13 +++++-------- SourceX/DiabloUI/fonts.h | 8 -------- 5 files changed, 54 insertions(+), 16 deletions(-) diff --git a/Source/diablo.cpp b/Source/diablo.cpp index 65521136a..e9c2ec23b 100644 --- a/Source/diablo.cpp +++ b/Source/diablo.cpp @@ -101,6 +101,8 @@ static void print_help_and_exit() printInConsole(" %-20s %-30s\n", "--data-dir", "Specify the folder of diabdat.mpq"); printInConsole(" %-20s %-30s\n", "--save-dir", "Specify the folder of save files"); printInConsole(" %-20s %-30s\n", "--config-dir", "Specify the location of diablo.ini"); + printInConsole(" %-20s %-30s\n", "--ttf-dir", "Specify the location of the .ttf font"); + printInConsole(" %-20s %-30s\n", "--ttf-name", "Specify the name of a custom .ttf font"); printInConsole(" %-20s %-30s\n", "-n", "Skip startup videos"); printInConsole(" %-20s %-30s\n", "-f", "Display frames per second"); printInConsole(" %-20s %-30s\n", "-x", "Run in windowed mode"); @@ -141,6 +143,10 @@ static void diablo_parse_flags(int argc, char **argv) SetPrefPath(argv[++i]); } else if (strcasecmp("--config-dir", argv[i]) == 0) { SetConfigPath(argv[++i]); + } else if (strcasecmp("--ttf-dir", argv[i]) == 0) { + SetTtfPath(argv[++i]); + } else if (strcasecmp("--ttf-name", argv[i]) == 0) { + SetTtfName(argv[++i]); } else if (strcasecmp("-n", argv[i]) == 0) { gbShowIntro = false; } else if (strcasecmp("-f", argv[i]) == 0) { diff --git a/SourceS/paths.cpp b/SourceS/paths.cpp index bcf1472a2..1d7f6c9c4 100644 --- a/SourceS/paths.cpp +++ b/SourceS/paths.cpp @@ -6,6 +6,14 @@ #include "sdl2_to_1_2_backports.h" #endif +#ifndef TTF_FONT_DIR +#define TTF_FONT_DIR "" +#endif + +#ifndef TTF_FONT_NAME +#define TTF_FONT_NAME "CharisSILB.ttf" +#endif + namespace dvl { namespace { @@ -13,6 +21,8 @@ namespace { std::string *basePath = NULL; std::string *prefPath = NULL; std::string *configPath = NULL; +std::string *ttfPath = NULL; +std::string *ttfName = NULL; void AddTrailingSlash(std::string *path) { #ifdef _WIN32 @@ -60,6 +70,20 @@ const std::string &GetConfigPath() return *configPath; } +const std::string &GetTtfPath() +{ + if (ttfPath == NULL) + ttfPath = new std::string(TTF_FONT_DIR); + return *ttfPath; +} + +const std::string &GetTtfName() +{ + if (ttfName == NULL) + ttfName = new std::string(TTF_FONT_NAME); + return *ttfName; +} + void SetBasePath(const char *path) { if (basePath == NULL) basePath = new std::string; @@ -82,4 +106,19 @@ void SetConfigPath(const char *path) AddTrailingSlash(configPath); } +void SetTtfPath(const char *path) +{ + if (ttfPath == NULL) + ttfPath = new std::string; + *ttfPath = path; + AddTrailingSlash(ttfPath); +} + +void SetTtfName(const char *path) +{ + if (ttfName == NULL) + ttfName = new std::string; + *ttfName = path; +} + } // namespace dvl diff --git a/SourceS/paths.h b/SourceS/paths.h index 6ef064a97..ce7e9d43a 100644 --- a/SourceS/paths.h +++ b/SourceS/paths.h @@ -7,9 +7,13 @@ namespace dvl { const std::string &GetBasePath(); const std::string &GetPrefPath(); const std::string &GetConfigPath(); +const std::string &GetTtfPath(); +const std::string &GetTtfName(); void SetBasePath(const char *path); void SetPrefPath(const char *path); void SetConfigPath(const char *path); +void SetTtfPath(const char *path); +void SetTtfName(const char *path); } // namespace dvl diff --git a/SourceX/DiabloUI/fonts.cpp b/SourceX/DiabloUI/fonts.cpp index 720166ce5..546a1d0e3 100644 --- a/SourceX/DiabloUI/fonts.cpp +++ b/SourceX/DiabloUI/fonts.cpp @@ -1,5 +1,6 @@ #include "DiabloUI/fonts.h" #include "file_util.h" +#include "../SourceS/paths.h" namespace dvl { @@ -61,18 +62,14 @@ void LoadTtfFont() { was_fonts_init = true; } - const char* ttf_font_path = TTF_FONT_NAME; - if (!FileExists(ttf_font_path)) - { - ttf_font_path = TTF_FONT_DIR TTF_FONT_NAME; - } + std::string ttf_font_path = GetTtfPath() + GetTtfName(); #ifdef __linux__ - if (!FileExists(ttf_font_path)) + if (!FileExists(ttf_font_path.c_str())) { - ttf_font_path = "/usr/share/fonts/truetype/" TTF_FONT_NAME; + ttf_font_path = "/usr/share/fonts/truetype/" + GetTtfName(); } #endif - font = TTF_OpenFont(ttf_font_path, 17); + font = TTF_OpenFont(ttf_font_path.c_str(), 17); if (font == NULL) { SDL_Log("TTF_OpenFont: %s", TTF_GetError()); return; diff --git a/SourceX/DiabloUI/fonts.h b/SourceX/DiabloUI/fonts.h index 433053966..c2adaf4a9 100644 --- a/SourceX/DiabloUI/fonts.h +++ b/SourceX/DiabloUI/fonts.h @@ -6,14 +6,6 @@ #include "DiabloUI/art.h" -#ifndef TTF_FONT_DIR -#define TTF_FONT_DIR "" -#endif - -#ifndef TTF_FONT_NAME -#define TTF_FONT_NAME "CharisSILB.ttf" -#endif - namespace dvl { enum _artFontTables {