diff --git a/CMakeSettings.json b/CMakeSettings.json index 1252c441..6dd8ced6 100644 --- a/CMakeSettings.json +++ b/CMakeSettings.json @@ -64,7 +64,14 @@ "cmakeCommandArgs": "-DVCPKG_TARGET_TRIPLET=x64-windows-static", "buildCommandArgs": "-v", "ctestCommandArgs": "", - "inheritEnvironments": [ "msvc_x64_x64" ] + "inheritEnvironments": [ "msvc_x64_x64" ], + "variables": [ + { + "name": "VCPKG_MANIFEST_FEATURES", + "value": "fonts", + "type": "STRING" + } + ] } ] -} \ No newline at end of file +} diff --git a/README.md b/README.md index 1a670e02..e514934f 100644 --- a/README.md +++ b/README.md @@ -21,7 +21,7 @@ On windows, the program can be compiled with Visual Studio (recommended) or with ======= ```` -.\vcpkg install pkgconf wxwidgets boost-smart-ptr boost-regex boost-logic boost-pool boost-iterator hunspell --triplet=x64-windows-static +.\vcpkg install pkgconf wxwidgets[fonts] boost-smart-ptr boost-regex boost-logic boost-pool boost-iterator hunspell --triplet=x64-windows-static ```` and/or ```` diff --git a/src/data/font.cpp b/src/data/font.cpp index db81e41b..65a9153f 100644 --- a/src/data/font.cpp +++ b/src/data/font.cpp @@ -8,7 +8,10 @@ #include #include - +#include +#include +#include + // ----------------------------------------------------------------------------- : Font Font::Font() @@ -23,6 +26,51 @@ Font::Font() , separator_color(Color(0,0,0,128)) , flags(FONT_NORMAL) {} + +bool Font::PreloadResourceFonts(String fontsDirectoryPath, bool recursive) { +#if wxUSE_PRIVATE_FONTS + String pathSeparator(wxFileName::GetPathSeparator()); + String appPath( wxFileName(wxStandardPaths::Get().GetExecutablePath()).GetPath() ); + fontsDirectoryPath = appPath + pathSeparator + fontsDirectoryPath + (fontsDirectoryPath.EndsWith(pathSeparator) ? wxEmptyString : pathSeparator); + + if (!wxDirExists(fontsDirectoryPath)) return false; + + // tally fonts + vector fontFilePaths; + TallyResourceFonts(fontsDirectoryPath, fontFilePaths, recursive); + if (fontFilePaths.size() == 0) return false; + + // load fonts + bool preloadHadErrors = false; + for (String fontFilePath : fontFilePaths) { + if (!wxFont::AddPrivateFont(fontFilePath)) { + preloadHadErrors = true; + } + } + + return preloadHadErrors; + +#endif // wxUSE_PRIVATE_FONTS + return false; +} + +void Font::TallyResourceFonts(String fontsDirectoryPath, vector& fontFilePaths, bool recursive) { + wxDir fontsDirectory(fontsDirectoryPath); + String fontFileName = wxEmptyString; + bool hasNext = fontsDirectory.GetFirst(&fontFileName); + while (hasNext) { + String fontFilePath = fontsDirectoryPath + fontFileName; + if (wxDirExists(fontFilePath)) { + if (recursive) { + TallyResourceFonts(fontFilePath + wxFileName::GetPathSeparator(), fontFilePaths, true); + } + } + else if (fontFilePath.EndsWith(_(".ttf")) || fontFilePath.EndsWith(_(".otf"))) { + fontFilePaths.push_back(fontFilePath); + } + hasNext = fontsDirectory.GetNext(&fontFileName); + } +} bool Font::update(Context& ctx) { bool changes = false; diff --git a/src/data/font.hpp b/src/data/font.hpp index cc030f92..9a3368b1 100644 --- a/src/data/font.hpp +++ b/src/data/font.hpp @@ -45,10 +45,14 @@ public: RealSize shadow_displacement; ///< Position of the shadow double shadow_blur; ///< Blur radius of the shadow Color separator_color; ///< Color for text - int flags; ///< FontFlags for this font - + int flags; ///< FontFlags for this font + Font(); - + + /// 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 + static void 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 @@ -64,7 +68,7 @@ public: /// Convert this font to a wxFont wxFont toWxFont(double scale) const; - + private: DECLARE_REFLECTION(); }; diff --git a/src/main.cpp b/src/main.cpp index ca0ab3f1..73979853 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -15,6 +15,7 @@ #include #include #include +#include #include #include #include @@ -85,7 +86,8 @@ int MSE::OnRun() { #else // Platform friendly appname SetAppName(_("magicseteditor")); - #endif + #endif + Font::PreloadResourceFonts(_("Magic - Fonts"), false); wxInitAllImageHandlers(); wxFileSystem::AddHandler(new wxInternetFSHandler); // needed for update checker wxSocketBase::Initialize();