diff --git a/src/data/font.cpp b/src/data/font.cpp index 4a7292cd..4a130134 100644 --- a/src/data/font.cpp +++ b/src/data/font.cpp @@ -27,32 +27,23 @@ Font::Font() , flags(FONT_NORMAL) {} -bool Font::PreloadResourceFonts() { -#if wxUSE_PRIVATE_FONTS - wxUniChar pathSeparator = wxFileName::GetPathSeparator(); +bool Font::PreloadResourceFonts(String fontsDirectoryPath, bool recursive) { +#if wxUSE_PRIVATE_FONTS + String pathSeparator(wxFileName::GetPathSeparator()); String appPath( wxFileName(wxStandardPaths::Get().GetExecutablePath()).GetPath() ); - String fontsPath = appPath + pathSeparator + "resource" + pathSeparator + "fonts" + pathSeparator; + fontsDirectoryPath = appPath + pathSeparator + fontsDirectoryPath + (fontsDirectoryPath.EndsWith(pathSeparator) ? wxEmptyString : pathSeparator); - if (!wxDirExists(fontsPath)) - return false; + if (!wxDirExists(fontsDirectoryPath)) return false; - wxDir fontsDirectory(fontsPath); + // tally fonts vector fontFilePaths; - String fontFileName = ""; - - // are there any font files in the resource/fonts directory? - if (!fontsDirectory.GetFirst(&fontFileName, "*.ttf")) - return false; - - fontFilePaths.push_back(fontsPath + fontFileName); - while (fontsDirectory.GetNext(&fontFileName)) - fontFilePaths.push_back(fontsPath + fontFileName); + if (!TallyResourceFonts(fontsDirectoryPath, fontFilePaths, recursive)) return false; + // load fonts bool preloadHadErrors = false; - for (String fn : fontFilePaths) { - if (!wxFont::AddPrivateFont(fn)) { + for (String fontFilePath : fontFilePaths) { + if (!wxFont::AddPrivateFont(fontFilePath)) { preloadHadErrors = true; - continue; } } @@ -61,6 +52,27 @@ bool Font::PreloadResourceFonts() { #endif // wxUSE_PRIVATE_FONTS return false; } + +bool Font::TallyResourceFonts(String fontsDirectoryPath, vector& fontFilePaths, bool recursive) { + wxDir fontsDirectory(fontsDirectoryPath); + String fontFileName = ""; + bool foundFonts = false; + bool hasNext = fontsDirectory.GetFirst(&fontFileName); + while (hasNext) { + String fontFilePath = fontsDirectoryPath + fontFileName; + if (wxDirExists(fontFilePath)) { + if (recursive) { + foundFonts = foundFonts || TallyResourceFonts(fontFilePath + wxFileName::GetPathSeparator(), fontFilePaths, true); + } + } + else if (fontFilePath.EndsWith(_(".ttf")) || fontFilePath.EndsWith(_(".otf"))) { + foundFonts = true; + fontFilePaths.push_back(fontFilePath); + } + hasNext = fontsDirectory.GetNext(&fontFileName); + } + return foundFonts; +} bool Font::update(Context& ctx) { bool changes = false; diff --git a/src/data/font.hpp b/src/data/font.hpp index fa6c72a2..39a2a976 100644 --- a/src/data/font.hpp +++ b/src/data/font.hpp @@ -49,8 +49,10 @@ public: Font(); - /// Load fonts (.ttf) from the resource/fonts directory - static bool PreloadResourceFonts(); + /// Load fonts (.ttf or .otf) from the given directory and its subdirectories, returns true if there were errors + static bool PreloadResourceFonts(String fontsDirectoryPath, bool recursive); + /// Adds font file paths from the given directory into fontFilePaths, returns true if fonts were found + static bool TallyResourceFonts(String fontsDirectoryPath, vector& fontFilePaths, bool recursive); /// Update the scritables, returns true if there is a change bool update(Context& ctx); /// Add the given dependency to the dependent_scripts list for the variables this font depends on diff --git a/src/main.cpp b/src/main.cpp index 3a3bb348..73979853 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -86,8 +86,8 @@ int MSE::OnRun() { #else // Platform friendly appname SetAppName(_("magicseteditor")); - #endif - Font::PreloadResourceFonts(); + #endif + Font::PreloadResourceFonts(_("Magic - Fonts"), false); wxInitAllImageHandlers(); wxFileSystem::AddHandler(new wxInternetFSHandler); // needed for update checker wxSocketBase::Initialize();