Add support for .otf files, add option to load subdirectories

This commit is contained in:
GenevensiS
2025-04-07 23:12:58 +02:00
parent cb18500f41
commit dca935a966
3 changed files with 37 additions and 23 deletions
+31 -19
View File
@@ -27,32 +27,23 @@ Font::Font()
, flags(FONT_NORMAL) , flags(FONT_NORMAL)
{} {}
bool Font::PreloadResourceFonts() { bool Font::PreloadResourceFonts(String fontsDirectoryPath, bool recursive) {
#if wxUSE_PRIVATE_FONTS #if wxUSE_PRIVATE_FONTS
wxUniChar pathSeparator = wxFileName::GetPathSeparator(); String pathSeparator(wxFileName::GetPathSeparator());
String appPath( wxFileName(wxStandardPaths::Get().GetExecutablePath()).GetPath() ); 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)) if (!wxDirExists(fontsDirectoryPath)) return false;
return false;
wxDir fontsDirectory(fontsPath); // tally fonts
vector<String> fontFilePaths; vector<String> fontFilePaths;
String fontFileName = ""; if (!TallyResourceFonts(fontsDirectoryPath, fontFilePaths, recursive)) return false;
// 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);
// load fonts
bool preloadHadErrors = false; bool preloadHadErrors = false;
for (String fn : fontFilePaths) { for (String fontFilePath : fontFilePaths) {
if (!wxFont::AddPrivateFont(fn)) { if (!wxFont::AddPrivateFont(fontFilePath)) {
preloadHadErrors = true; preloadHadErrors = true;
continue;
} }
} }
@@ -61,6 +52,27 @@ bool Font::PreloadResourceFonts() {
#endif // wxUSE_PRIVATE_FONTS #endif // wxUSE_PRIVATE_FONTS
return false; return false;
} }
bool Font::TallyResourceFonts(String fontsDirectoryPath, vector<String>& 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 Font::update(Context& ctx) {
bool changes = false; bool changes = false;
+4 -2
View File
@@ -49,8 +49,10 @@ public:
Font(); Font();
/// Load fonts (.ttf) from the resource/fonts directory /// Load fonts (.ttf or .otf) from the given directory and its subdirectories, returns true if there were errors
static bool PreloadResourceFonts(); 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<String>& fontFilePaths, bool recursive);
/// Update the scritables, returns true if there is a change /// Update the scritables, returns true if there is a change
bool update(Context& ctx); bool update(Context& ctx);
/// Add the given dependency to the dependent_scripts list for the variables this font depends on /// Add the given dependency to the dependent_scripts list for the variables this font depends on
+2 -2
View File
@@ -86,8 +86,8 @@ int MSE::OnRun() {
#else #else
// Platform friendly appname // Platform friendly appname
SetAppName(_("magicseteditor")); SetAppName(_("magicseteditor"));
#endif #endif
Font::PreloadResourceFonts(); Font::PreloadResourceFonts(_("Magic - Fonts"), false);
wxInitAllImageHandlers(); wxInitAllImageHandlers();
wxFileSystem::AddHandler(new wxInternetFSHandler); // needed for update checker wxFileSystem::AddHandler(new wxInternetFSHandler); // needed for update checker
wxSocketBase::Initialize(); wxSocketBase::Initialize();