From ee4cf2d7b404e8945b902a385bb0229e65409da0 Mon Sep 17 00:00:00 2001 From: TomTkacz Date: Wed, 12 Mar 2025 04:48:33 -0500 Subject: [PATCH 1/4] made fonts available without installing system-wide --- CMakeSettings.json | 11 +++++++++-- src/data/font.cpp | 40 +++++++++++++++++++++++++++++++++++++++- src/data/font.hpp | 10 ++++++---- src/main.cpp | 2 ++ 4 files changed, 56 insertions(+), 7 deletions(-) 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/src/data/font.cpp b/src/data/font.cpp index db81e41b..4a7292cd 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,41 @@ Font::Font() , separator_color(Color(0,0,0,128)) , flags(FONT_NORMAL) {} + +bool Font::PreloadResourceFonts() { +#if wxUSE_PRIVATE_FONTS + wxUniChar pathSeparator = wxFileName::GetPathSeparator(); + String appPath( wxFileName(wxStandardPaths::Get().GetExecutablePath()).GetPath() ); + String fontsPath = appPath + pathSeparator + "resource" + pathSeparator + "fonts" + pathSeparator; + + if (!wxDirExists(fontsPath)) + return false; + + wxDir fontsDirectory(fontsPath); + 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); + + bool preloadHadErrors = false; + for (String fn : fontFilePaths) { + if (!wxFont::AddPrivateFont(fn)) { + preloadHadErrors = true; + continue; + } + } + + return preloadHadErrors; + +#endif // wxUSE_PRIVATE_FONTS + return false; +} bool Font::update(Context& ctx) { bool changes = false; diff --git a/src/data/font.hpp b/src/data/font.hpp index cc030f92..fa6c72a2 100644 --- a/src/data/font.hpp +++ b/src/data/font.hpp @@ -45,10 +45,12 @@ 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) from the resource/fonts directory + static bool PreloadResourceFonts(); /// 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 +66,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..3a3bb348 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -15,6 +15,7 @@ #include #include #include +#include #include #include #include @@ -86,6 +87,7 @@ int MSE::OnRun() { // Platform friendly appname SetAppName(_("magicseteditor")); #endif + Font::PreloadResourceFonts(); wxInitAllImageHandlers(); wxFileSystem::AddHandler(new wxInternetFSHandler); // needed for update checker wxSocketBase::Initialize(); From cb18500f4105f2aee7b7e2375d0944fc585e5b47 Mon Sep 17 00:00:00 2001 From: Tom Tkacz Date: Mon, 7 Apr 2025 14:27:27 -0500 Subject: [PATCH 2/4] Updated static windows build instructions --- README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) 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 ```` From dca935a966191949c0e6a85b8f9ebfaf57f3a701 Mon Sep 17 00:00:00 2001 From: GenevensiS <66968533+G-e-n-e-v-e-n-s-i-S@users.noreply.github.com> Date: Mon, 7 Apr 2025 23:12:58 +0200 Subject: [PATCH 3/4] Add support for .otf files, add option to load subdirectories --- src/data/font.cpp | 50 +++++++++++++++++++++++++++++------------------ src/data/font.hpp | 6 ++++-- src/main.cpp | 4 ++-- 3 files changed, 37 insertions(+), 23 deletions(-) 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(); From 28b2862bd888b1983bf7c8a8ea710d39e4f39474 Mon Sep 17 00:00:00 2001 From: GenevensiS <66968533+G-e-n-e-v-e-n-s-i-S@users.noreply.github.com> Date: Tue, 8 Apr 2025 01:47:45 +0200 Subject: [PATCH 4/4] simplify code --- src/data/font.cpp | 12 +++++------- src/data/font.hpp | 4 ++-- 2 files changed, 7 insertions(+), 9 deletions(-) diff --git a/src/data/font.cpp b/src/data/font.cpp index 4a130134..65a9153f 100644 --- a/src/data/font.cpp +++ b/src/data/font.cpp @@ -37,7 +37,8 @@ bool Font::PreloadResourceFonts(String fontsDirectoryPath, bool recursive) { // tally fonts vector fontFilePaths; - if (!TallyResourceFonts(fontsDirectoryPath, fontFilePaths, recursive)) return false; + TallyResourceFonts(fontsDirectoryPath, fontFilePaths, recursive); + if (fontFilePaths.size() == 0) return false; // load fonts bool preloadHadErrors = false; @@ -53,25 +54,22 @@ bool Font::PreloadResourceFonts(String fontsDirectoryPath, bool recursive) { return false; } -bool Font::TallyResourceFonts(String fontsDirectoryPath, vector& fontFilePaths, bool recursive) { +void Font::TallyResourceFonts(String fontsDirectoryPath, vector& fontFilePaths, bool recursive) { wxDir fontsDirectory(fontsDirectoryPath); - String fontFileName = ""; - bool foundFonts = false; + String fontFileName = wxEmptyString; bool hasNext = fontsDirectory.GetFirst(&fontFileName); while (hasNext) { String fontFilePath = fontsDirectoryPath + fontFileName; if (wxDirExists(fontFilePath)) { if (recursive) { - foundFonts = foundFonts || TallyResourceFonts(fontFilePath + wxFileName::GetPathSeparator(), fontFilePaths, true); + 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) { diff --git a/src/data/font.hpp b/src/data/font.hpp index 39a2a976..9a3368b1 100644 --- a/src/data/font.hpp +++ b/src/data/font.hpp @@ -51,8 +51,8 @@ public: /// 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); + /// 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