From 2fe424644a03dc2cc830874c2efb4f73daaada51 Mon Sep 17 00:00:00 2001 From: Rodrigo Braz Monteiro Date: Wed, 20 Jun 2007 21:08:31 +0000 Subject: [PATCH] Standard paths semi-operational, and font caching (for ft2 retrieval engine) working. Originally committed to SVN as r1274. --- aegisub/dialog_fonts_collector.cpp | 2 +- aegisub/font_file_lister.cpp | 58 ++++++++++++++++++++++++++++-- aegisub/standard_paths.cpp | 43 ++++++++++++++++++---- 3 files changed, 94 insertions(+), 9 deletions(-) diff --git a/aegisub/dialog_fonts_collector.cpp b/aegisub/dialog_fonts_collector.cpp index 88194fd8b..7d511faf5 100644 --- a/aegisub/dialog_fonts_collector.cpp +++ b/aegisub/dialog_fonts_collector.cpp @@ -370,7 +370,7 @@ void FontsCollectorThread::Collect() { } // Collect font data - AppendText(_("Collecting font data from system... ")); + AppendText(_("Collecting font data from system. This might take a while, depending on the number of fonts installed. Results are cached and subsequent executions will be faster... ")); CollectFontData(); AppendText(_("done.\n\nScanning file for fonts...")); diff --git a/aegisub/font_file_lister.cpp b/aegisub/font_file_lister.cpp index 646c50812..a9364441b 100644 --- a/aegisub/font_file_lister.cpp +++ b/aegisub/font_file_lister.cpp @@ -40,6 +40,7 @@ //////////// // Includes #include +#include #ifdef WIN32 #include #include FT_FREETYPE_H @@ -50,6 +51,7 @@ #include "font_file_lister.h" #include "text_file_writer.h" #include "text_file_reader.h" +#include "standard_paths.h" //////////////////// @@ -111,6 +113,9 @@ void FontFileLister::DoClearData() { void FontFileLister::DoGatherData() { #ifdef WIN32 + // Load cache + LoadCache(); + // Get fonts folder wxString source; TCHAR szPath[MAX_PATH]; @@ -143,6 +148,9 @@ void FontFileLister::DoGatherData() { } } + // Save cache + SaveCache(); + #else // TODO: implement fconfig @@ -176,12 +184,58 @@ bool FontFileLister::IsFilenameCached(wxString filename) { ////////////// // Save cache void FontFileLister::SaveCache() { - // TODO + try { + // Open file + TextFileWriter file(StandardPaths::DecodePath(_T("?user/fontcache.dat"))); + + // For each face... + for (FontMap::iterator iter = fontTable.begin();iter!=fontTable.end();iter++) { + // Write face name + wxString line = iter->first + _T("?"); + size_t len = iter->second.Count(); + + // Write file names + for (size_t i=0;isecond[i]; + if (i != len-1) line += _T("|"); + } + + // Write line + file.WriteLineToFile(line); + } + } + catch (...) { + } } ////////////// // Load cache void FontFileLister::LoadCache() { - // TODO + try { + // Load cache + TextFileReader file(StandardPaths::DecodePath(_T("?user/fontcache.dat"))); + + // Read each line + while (file.HasMoreLines()) { + // Read line + wxString line = file.ReadLineFromFile(); + int pos = line.Find(_T('?')); + + // Get face name + wxString face = line.Left(pos); + if (face.IsEmpty()) continue; + + // Get files + wxStringTokenizer tkn(line.Mid(pos+1),_T("|")); + while (tkn.HasMoreTokens()) { + wxString file = tkn.GetNextToken(); + if (!file.IsEmpty()) { + AddFont(file,face); + } + } + } + } + catch (...) { + } } diff --git a/aegisub/standard_paths.cpp b/aegisub/standard_paths.cpp index ee08f15b4..cbc4f33f4 100644 --- a/aegisub/standard_paths.cpp +++ b/aegisub/standard_paths.cpp @@ -37,6 +37,7 @@ /////////// // Headers #include +#include #include "standard_paths.h" @@ -51,10 +52,23 @@ StandardPaths *StandardPaths::GetInstance() { /////////////// // Constructor StandardPaths::StandardPaths() { - wxFileName aegiPath(wxStandardPaths::Get().GetExecutablePath()); - SetPathValue(_T("?install"),aegiPath.GetPath()); - SetPathValue(_T("?user"),wxStandardPaths::Get().GetUserDataDir()); - SetPathValue(_T("?temp"),wxStandardPaths::Get().GetTempDir()); + // Get paths + wxString dataDir = wxStandardPaths::Get().GetDataDir(); + wxString userDir = wxStandardPaths::Get().GetUserDataDir(); + wxString tempDir = wxStandardPaths::Get().GetTempDir(); + + // Set paths + DoSetPathValue(_T("?data"),dataDir); + DoSetPathValue(_T("?user"),userDir); + DoSetPathValue(_T("?temp"),tempDir); + + // Create paths if they don't exist + wxFileName folder(dataDir + _T("/")); + if (!folder.DirExists()) folder.Mkdir(0777,wxPATH_MKDIR_FULL); + folder.Assign(userDir + _T("/")); + if (!folder.DirExists()) folder.Mkdir(0777,wxPATH_MKDIR_FULL); + folder.Assign(tempDir + _T("/")); + if (!folder.DirExists()) folder.Mkdir(0777,wxPATH_MKDIR_FULL); } @@ -63,8 +77,25 @@ StandardPaths::StandardPaths() { wxString StandardPaths::DoDecodePath(wxString path) { // Decode if (path[0] == _T('?')) { - // TODO - return path; + // Split ?part from rest + path.Replace(_T("\\"),_T("/")); + int pos = path.Find(_T("/")); + wxString path1,path2; + if (pos == wxNOT_FOUND) path1 = path; + else { + path1 = path.Left(pos); + path2 = path.Mid(pos+1); + } + + // Replace ?part if valid + std::map::iterator iter = paths.find(path1); + if (iter == paths.end()) return path; + wxString final = iter->second + _T("/") + path2; + final.Replace(_T("//"),_T("/")); +#ifdef WIN32 + final.Replace(_T("/"),_T("\\")); +#endif + return final; } // Nothing to decode