1 /////////////////////////////////////////////////////////////////////////////
2 // Name: src/dfb/fontmgr.cpp
3 // Purpose: font management for wxDFB
4 // Author: Vaclav Slavik
6 // Copyright: (c) 2001-2002 SciTech Software, Inc. (www.scitechsoft.com)
7 // (c) 2006 REA Elektronik GmbH
8 // Licence: wxWindows licence
9 /////////////////////////////////////////////////////////////////////////////
11 // For compilers that support precompilation, includes "wx.h".
12 #include "wx/wxprec.h"
19 #include "wx/gdicmn.h"
24 #include "wx/fileconf.h"
25 #include "wx/filename.h"
26 #include "wx/tokenzr.h"
29 #include "wx/private/fontmgr.h"
30 #include "wx/dfb/wrapdfb.h"
32 // ============================================================================
34 // ============================================================================
36 // ----------------------------------------------------------------------------
38 // ----------------------------------------------------------------------------
40 // This is a fake "filename" for DirectFB's builtin font (which isn't loaded
41 // from a file); we can't use empty string, because that's already used for
42 // "this face is not available" by wxFontsManagerBase
43 #define BUILTIN_DFB_FONT_FILENAME "/dev/null"
45 wxFontInstance::wxFontInstance(float ptSize
, bool aa
,
46 const wxString
& filename
)
47 : wxFontInstanceBase(ptSize
, aa
)
49 // NB: DFB's fract_height value is 32bit integer with the last 6 bit
50 // representing fractional value, hence the multiplication by 64;
51 // 1pt=1/72inch, hence "/ 72"
52 int pixSize
= int(ptSize
* wxGetDisplayPPI().y
* 64 / 72);
54 DFBFontDescription desc
;
55 desc
.flags
= (DFBFontDescriptionFlags
)(
56 DFDESC_ATTRIBUTES
| DFDESC_FRACT_HEIGHT
);
57 desc
.attributes
= aa
? DFFA_NONE
: DFFA_MONOCHROME
;
58 desc
.fract_height
= pixSize
;
60 if ( filename
== BUILTIN_DFB_FONT_FILENAME
)
61 m_font
= wxIDirectFB::Get()->CreateFont(NULL
, &desc
);
63 m_font
= wxIDirectFB::Get()->CreateFont(filename
.fn_str(), &desc
);
65 wxASSERT_MSG( m_font
, "cannot create font instance" );
68 // ----------------------------------------------------------------------------
70 // ----------------------------------------------------------------------------
72 wxFontInstance
*wxFontFace::CreateFontInstance(float ptSize
, bool aa
)
74 return new wxFontInstance(ptSize
, aa
, m_fileName
);
77 // ----------------------------------------------------------------------------
79 // ----------------------------------------------------------------------------
81 wxFontBundle::wxFontBundle(const wxString
& name
,
82 const wxString
& fileRegular
,
83 const wxString
& fileBold
,
84 const wxString
& fileItalic
,
85 const wxString
& fileBoldItalic
,
91 if ( !fileRegular
.empty() )
92 m_faces
[FaceType_Regular
] = new wxFontFace(fileRegular
);
93 if ( !fileItalic
.empty() )
94 m_faces
[FaceType_Italic
] = new wxFontFace(fileItalic
);
95 if ( !fileBold
.empty() )
96 m_faces
[FaceType_Bold
] = new wxFontFace(fileBold
);
97 if ( !fileBoldItalic
.empty() )
98 m_faces
[FaceType_BoldItalic
] = new wxFontFace(fileBoldItalic
);
101 // ----------------------------------------------------------------------------
103 // ----------------------------------------------------------------------------
106 The code below looks up and parses font configuration files FontsIndex.
107 The files are looked up in directories specified in the WXDFB_FONTPATH
108 environment variable (separated with :, similarly to the PATH variable).
109 If the variable is not set, $prefix/share/wx/fonts directory is used.
110 All subdirectories of directories on the path are scanned for FontsIndex
113 The FontsIndex file is standard wxFileConfig file text file. Each toplevel
114 group specifies one font bundle, font's name is the name of group. Group's
115 entries look like this:
118 # font files (at least one of them must be present):
119 Regular=RegularFaceFile.ttf
120 Italic=ItalicFaceFile.ttf
121 Bold=BoldFaceFile.ttf
122 BoldItalic=BoldItalicFaceFile.ttf
123 # optional tag indicating this font is fixed-with (default is false):
126 Additionally, there may be DefaultXXX entries at the toplevel for every
127 family XXX and a Default entry that is shortcut for setting all families'
128 default, their value is name of the default font:
130 # optional tags indicating the default font for given family:
131 DefaultDecorative=Font Name
132 DefaultRoman=Font Name
133 DefaultScript=Font Name
134 DefaultSwiss=Font Name
135 DefaultModern=Font Name
136 DefaultTeletype=Font Name
137 # indicate the font that is default for all families (optional):
141 void wxFontsManager::AddAllFonts()
144 if ( !wxGetEnv("WXDFB_FONTPATH", &path
) )
145 path
= wxT(wxINSTALL_PREFIX
"/share/wx/fonts");
147 wxStringTokenizer
tkn(path
, wxPATH_SEP
);
148 while ( tkn
.HasMoreTokens() )
150 wxString dir
= tkn
.GetNextToken();
152 if ( !wxDir::Exists(dir
) )
154 wxLogDebug("font directory %s doesn't exist", dir
);
158 wxArrayString indexFiles
;
159 if ( !wxDir::GetAllFiles(dir
, &indexFiles
, "FontsIndex") )
162 for ( wxArrayString::const_iterator i
= indexFiles
.begin();
163 i
!= indexFiles
.end(); ++i
)
169 if ( GetBundles().empty() )
171 // We can fall back to the builtin default font if no other fonts are
174 _("no fonts found in %s, using builtin font"), path
);
181 BUILTIN_DFB_FONT_FILENAME
,
191 void wxFontsManager::AddFontsFromDir(const wxString
& indexFile
)
193 wxFileName
fn(indexFile
);
194 wxString dir
= fn
.GetPath();
196 if ( !fn
.FileExists() )
198 wxLogWarning(_("Fonts index file %s disappeared while loading fonts."),
203 wxLogTrace("font", "adding fonts from %s", dir
.c_str());
205 wxFileConfig
cfg(wxEmptyString
, wxEmptyString
,
206 indexFile
, wxEmptyString
,
207 wxCONFIG_USE_LOCAL_FILE
);
211 for ( bool cont
= cfg
.GetFirstGroup(name
, i
);
213 cont
= cfg
.GetNextGroup(name
, i
) )
215 AddFont(dir
, name
, cfg
);
218 // set default fonts for families:
219 SetDefaultFonts(cfg
);
223 ReadFilePath(const wxString
& name
, const wxString
& dir
, wxFileConfig
& cfg
)
225 wxString p
= cfg
.Read(name
, wxEmptyString
);
227 if ( p
.empty() || wxFileName(p
).IsAbsolute() )
230 return dir
+ "/" + p
;
233 void wxFontsManager::AddFont(const wxString
& dir
,
234 const wxString
& name
,
237 wxLogTrace("font", "adding font '%s'", name
.c_str());
239 wxConfigPathChanger
ch(&cfg
, wxString::Format("/%s/", name
.c_str()));
246 ReadFilePath("Regular", dir
, cfg
),
247 ReadFilePath("Italic", dir
, cfg
),
248 ReadFilePath("Bold", dir
, cfg
),
249 ReadFilePath("BoldItalic", dir
, cfg
),
250 cfg
.Read("IsFixed", (long)false)
255 void wxFontsManager::SetDefaultFonts(wxFileConfig
& cfg
)
259 if ( cfg
.Read("Default", &name
) )
261 m_defaultFacenames
[wxFONTFAMILY_DECORATIVE
] =
262 m_defaultFacenames
[wxFONTFAMILY_ROMAN
] =
263 m_defaultFacenames
[wxFONTFAMILY_SCRIPT
] =
264 m_defaultFacenames
[wxFONTFAMILY_SWISS
] =
265 m_defaultFacenames
[wxFONTFAMILY_MODERN
] =
266 m_defaultFacenames
[wxFONTFAMILY_TELETYPE
] = name
;
269 if ( cfg
.Read("DefaultDecorative", &name
) )
270 m_defaultFacenames
[wxFONTFAMILY_DECORATIVE
] = name
;
271 if ( cfg
.Read("DefaultRoman", &name
) )
272 m_defaultFacenames
[wxFONTFAMILY_ROMAN
] = name
;
273 if ( cfg
.Read("DefaultScript", &name
) )
274 m_defaultFacenames
[wxFONTFAMILY_SCRIPT
] = name
;
275 if ( cfg
.Read("DefaultSwiss", &name
) )
276 m_defaultFacenames
[wxFONTFAMILY_SWISS
] = name
;
277 if ( cfg
.Read("DefaultModern", &name
) )
278 m_defaultFacenames
[wxFONTFAMILY_MODERN
] = name
;
279 if ( cfg
.Read("DefaultTeletype", &name
) )
280 m_defaultFacenames
[wxFONTFAMILY_TELETYPE
] = name
;