]> git.saurik.com Git - wxWidgets.git/blobdiff - src/dfb/fontmgr.cpp
don't crash in SetBackgroundStyle() if the window is not realized
[wxWidgets.git] / src / dfb / fontmgr.cpp
index 53849e210de473fb40c6d52764e6e001969b22e0..7abfabaacfd1e97d01c15b9c3ba2ca3dce1fb6ff 100644 (file)
     #pragma hdrstop
 #endif
 
+#ifndef WX_PRECOMP
+    #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"
 // 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)
@@ -48,7 +60,11 @@ wxFontInstance::wxFontInstance(float ptSize, bool aa,
                     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);
+
+    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, _T("cannot create font instance") );
 }
@@ -91,13 +107,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,14 +144,63 @@ 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(_T("WXDFB_FONTPATH"), &path) )
+        path = _T(wxINSTALL_PREFIX "/share/wx/fonts");
+
+    wxStringTokenizer tkn(path, wxPATH_SEP);
+    while ( tkn.HasMoreTokens() )
+    {
+        wxString dir = tkn.GetNextToken();
+
+        if ( !wxDir::Exists(dir) )
+        {
+            wxLogDebug(_T("font directory %s doesn't exist"), dir);
+            continue;
+        }
+
+        wxArrayString indexFiles;
+        if ( !wxDir::GetAllFiles(dir, &indexFiles, _T("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(_T("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;
     }