X-Git-Url: https://git.saurik.com/wxWidgets.git/blobdiff_plain/d7ae4a62645d369fbd6b8b5f3f0e12ee86536da1..85f622694cf6024a6a2108b134249468770a530d:/src/dfb/fontmgr.cpp diff --git a/src/dfb/fontmgr.cpp b/src/dfb/fontmgr.cpp index 53849e210d..817b4f5572 100644 --- a/src/dfb/fontmgr.cpp +++ b/src/dfb/fontmgr.cpp @@ -16,8 +16,16 @@ #pragma hdrstop #endif +#ifndef WX_PRECOMP + #include "wx/gdicmn.h" + #include "wx/utils.h" + #include "wx/log.h" +#endif + #include "wx/fileconf.h" #include "wx/filename.h" +#include "wx/tokenzr.h" +#include "wx/dir.h" #include "wx/private/fontmgr.h" #include "wx/dfb/wrapdfb.h" @@ -30,27 +38,32 @@ // wxFontInstance // ---------------------------------------------------------------------------- +// This is a fake "filename" for DirectFB's builtin font (which isn't loaded +// from a file); we can't use empty string, because that's already used for +// "this face is not available" by wxFontsManagerBase +#define BUILTIN_DFB_FONT_FILENAME "/dev/null" + wxFontInstance::wxFontInstance(float ptSize, bool aa, const wxString& filename) : wxFontInstanceBase(ptSize, aa) { - int scrSizePx, scrSizeMM; - wxDisplaySize(NULL, &scrSizePx); - wxDisplaySizeMM(NULL, &scrSizeMM); - double dpi = (scrSizePx / (scrSizeMM * mm2inches)); // NB: DFB's fract_height value is 32bit integer with the last 6 bit // representing fractional value, hence the multiplication by 64; // 1pt=1/72inch, hence "/ 72" - int pixSize = int(ptSize * dpi * 64 / 72); + int pixSize = int(ptSize * wxGetDisplayPPI().y * 64 / 72); DFBFontDescription desc; desc.flags = (DFBFontDescriptionFlags)( DFDESC_ATTRIBUTES | DFDESC_FRACT_HEIGHT); desc.attributes = aa ? DFFA_NONE : DFFA_MONOCHROME; desc.fract_height = pixSize; - m_font = wxIDirectFB::Get()->CreateFont(filename.fn_str(), &desc); - wxASSERT_MSG( m_font, _T("cannot create font instance") ); + if ( filename == BUILTIN_DFB_FONT_FILENAME ) + m_font = wxIDirectFB::Get()->CreateFont(NULL, &desc); + else + m_font = wxIDirectFB::Get()->CreateFont(filename.fn_str(), &desc); + + wxASSERT_MSG( m_font, "cannot create font instance" ); } // ---------------------------------------------------------------------------- @@ -91,13 +104,16 @@ wxFontBundle::wxFontBundle(const wxString& name, // ---------------------------------------------------------------------------- /* - The code below parses font configuration file ${WXDFB_FONTDIR}/FontsIndex. - By default, the directory is $prefix/share/wx/fonts, but can be ovewritten - by setting WXDFB_FONTDIR environment variable. + The code below looks up and parses font configuration files FontsIndex. + The files are looked up in directories specified in the WXDFB_FONTPATH + environment variable (separated with :, similarly to the PATH variable). + If the variable is not set, $prefix/share/wx/fonts directory is used. + All subdirectories of directories on the path are scanned for FontsIndex + files. - The file is standard wxFileConfig file text file. Each toplevel group - specifies one font bundle, font's name is the name of group. Group's entries - look like this: + The FontsIndex file is standard wxFileConfig file text file. Each toplevel + group specifies one font bundle, font's name is the name of group. Group's + entries look like this: [Font Name] # font files (at least one of them must be present): @@ -125,18 +141,67 @@ wxFontBundle::wxFontBundle(const wxString& name, void wxFontsManager::AddAllFonts() { - wxString dir = _T(wxINSTALL_PREFIX "/share/wx/fonts"); - wxGetEnv(_T("WXDFB_FONTDIR"), &dir); + wxString path; + if ( !wxGetEnv("WXDFB_FONTPATH", &path) ) + path = wxT(wxINSTALL_PREFIX "/share/wx/fonts"); + + wxStringTokenizer tkn(path, wxPATH_SEP); + while ( tkn.HasMoreTokens() ) + { + wxString dir = tkn.GetNextToken(); + + if ( !wxDir::Exists(dir) ) + { + wxLogDebug("font directory %s doesn't exist", dir); + continue; + } + + wxArrayString indexFiles; + if ( !wxDir::GetAllFiles(dir, &indexFiles, "FontsIndex") ) + continue; + + for ( wxArrayString::const_iterator i = indexFiles.begin(); + i != indexFiles.end(); ++i ) + { + AddFontsFromDir(*i); + } + } + + if ( GetBundles().empty() ) + { + // We can fall back to the builtin default font if no other fonts are + // defined: + wxLogTrace("font", + _("no fonts found in %s, using builtin font"), path); + + AddBundle + ( + new wxFontBundle + ( + _("Default font"), + BUILTIN_DFB_FONT_FILENAME, + wxEmptyString, + wxEmptyString, + wxEmptyString, + false // IsFixed + ) + ); + } +} - wxString indexFile = dir + _T("/FontsIndex"); +void wxFontsManager::AddFontsFromDir(const wxString& indexFile) +{ + wxFileName fn(indexFile); + wxString dir = fn.GetPath(); - if ( !wxFileName::FileExists(indexFile) ) + if ( !fn.FileExists() ) { - wxLogWarning(_("No fonts found in %s"), dir.c_str()); + wxLogWarning(_("Fonts index file %s disappeared while loading fonts."), + indexFile.c_str()); return; } - wxLogTrace(_T("font"), _T("adding fonts from %s"), dir.c_str()); + wxLogTrace("font", "adding fonts from %s", dir.c_str()); wxFileConfig cfg(wxEmptyString, wxEmptyString, indexFile, wxEmptyString, @@ -163,27 +228,27 @@ ReadFilePath(const wxString& name, const wxString& dir, wxFileConfig& cfg) if ( p.empty() || wxFileName(p).IsAbsolute() ) return p; - return dir + _T("/") + p; + return dir + "/" + p; } void wxFontsManager::AddFont(const wxString& dir, const wxString& name, wxFileConfig& cfg) { - wxLogTrace(_T("font"), _T("adding font '%s'"), name.c_str()); + wxLogTrace("font", "adding font '%s'", name.c_str()); - wxConfigPathChanger ch(&cfg, wxString::Format(_T("/%s/"), name.c_str())); + wxConfigPathChanger ch(&cfg, wxString::Format("/%s/", name.c_str())); AddBundle ( new wxFontBundle ( name, - ReadFilePath(_T("Regular"), dir, cfg), - ReadFilePath(_T("Italic"), dir, cfg), - ReadFilePath(_T("Bold"), dir, cfg), - ReadFilePath(_T("BoldItalic"), dir, cfg), - cfg.Read(_T("IsFixed"), (long)false) + ReadFilePath("Regular", dir, cfg), + ReadFilePath("Italic", dir, cfg), + ReadFilePath("Bold", dir, cfg), + ReadFilePath("BoldItalic", dir, cfg), + cfg.Read("IsFixed", (long)false) ) ); } @@ -192,7 +257,7 @@ void wxFontsManager::SetDefaultFonts(wxFileConfig& cfg) { wxString name; - if ( cfg.Read(_T("Default"), &name) ) + if ( cfg.Read("Default", &name) ) { m_defaultFacenames[wxFONTFAMILY_DECORATIVE] = m_defaultFacenames[wxFONTFAMILY_ROMAN] = @@ -202,16 +267,16 @@ void wxFontsManager::SetDefaultFonts(wxFileConfig& cfg) m_defaultFacenames[wxFONTFAMILY_TELETYPE] = name; } - if ( cfg.Read(_T("DefaultDecorative"), &name) ) + if ( cfg.Read("DefaultDecorative", &name) ) m_defaultFacenames[wxFONTFAMILY_DECORATIVE] = name; - if ( cfg.Read(_T("DefaultRoman"), &name) ) + if ( cfg.Read("DefaultRoman", &name) ) m_defaultFacenames[wxFONTFAMILY_ROMAN] = name; - if ( cfg.Read(_T("DefaultScript"), &name) ) + if ( cfg.Read("DefaultScript", &name) ) m_defaultFacenames[wxFONTFAMILY_SCRIPT] = name; - if ( cfg.Read(_T("DefaultSwiss"), &name) ) + if ( cfg.Read("DefaultSwiss", &name) ) m_defaultFacenames[wxFONTFAMILY_SWISS] = name; - if ( cfg.Read(_T("DefaultModern"), &name) ) + if ( cfg.Read("DefaultModern", &name) ) m_defaultFacenames[wxFONTFAMILY_MODERN] = name; - if ( cfg.Read(_T("DefaultTeletype"), &name) ) + if ( cfg.Read("DefaultTeletype", &name) ) m_defaultFacenames[wxFONTFAMILY_TELETYPE] = name; }