mirror of
https://github.com/amyinspace/MagicSetEditor2.git
synced 2026-06-10 04:57:00 -04:00
Add support for .otf files, add option to load subdirectories
This commit is contained in:
+30
-18
@@ -27,32 +27,23 @@ Font::Font()
|
||||
, flags(FONT_NORMAL)
|
||||
{}
|
||||
|
||||
bool Font::PreloadResourceFonts() {
|
||||
bool Font::PreloadResourceFonts(String fontsDirectoryPath, bool recursive) {
|
||||
#if wxUSE_PRIVATE_FONTS
|
||||
wxUniChar pathSeparator = wxFileName::GetPathSeparator();
|
||||
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<String> 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;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -62,6 +53,27 @@ bool Font::PreloadResourceFonts() {
|
||||
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 changes = false;
|
||||
changes |= name .update(ctx);
|
||||
|
||||
+4
-2
@@ -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<String>& 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
|
||||
|
||||
+1
-1
@@ -87,7 +87,7 @@ int MSE::OnRun() {
|
||||
// Platform friendly appname
|
||||
SetAppName(_("magicseteditor"));
|
||||
#endif
|
||||
Font::PreloadResourceFonts();
|
||||
Font::PreloadResourceFonts(_("Magic - Fonts"), false);
|
||||
wxInitAllImageHandlers();
|
||||
wxFileSystem::AddHandler(new wxInternetFSHandler); // needed for update checker
|
||||
wxSocketBase::Initialize();
|
||||
|
||||
Reference in New Issue
Block a user