From ee4cf2d7b404e8945b902a385bb0229e65409da0 Mon Sep 17 00:00:00 2001 From: TomTkacz Date: Wed, 12 Mar 2025 04:48:33 -0500 Subject: [PATCH] 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();