]> git.saurik.com Git - wxWidgets.git/blob - src/dfb/fontmgr.cpp
avoid duplicate move events
[wxWidgets.git] / src / dfb / fontmgr.cpp
1 /////////////////////////////////////////////////////////////////////////////
2 // Name: src/dfb/fontmgr.cpp
3 // Purpose: font management for wxDFB
4 // Author: Vaclav Slavik
5 // Created: 2006-11-18
6 // Copyright: (c) 2001-2002 SciTech Software, Inc. (www.scitechsoft.com)
7 // (c) 2006 REA Elektronik GmbH
8 // Licence: wxWindows licence
9 /////////////////////////////////////////////////////////////////////////////
10
11 // For compilers that support precompilation, includes "wx.h".
12 #include "wx/wxprec.h"
13
14 #ifdef __BORLANDC__
15 #pragma hdrstop
16 #endif
17
18 #ifndef WX_PRECOMP
19 #include "wx/gdicmn.h"
20 #include "wx/utils.h"
21 #include "wx/log.h"
22 #endif
23
24 #include "wx/fileconf.h"
25 #include "wx/filename.h"
26 #include "wx/tokenzr.h"
27 #include "wx/dir.h"
28
29 #include "wx/private/fontmgr.h"
30 #include "wx/dfb/wrapdfb.h"
31
32 // ============================================================================
33 // implementation
34 // ============================================================================
35
36 // ----------------------------------------------------------------------------
37 // wxFontInstance
38 // ----------------------------------------------------------------------------
39
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"
44
45 wxFontInstance::wxFontInstance(float ptSize, bool aa,
46 const wxString& filename)
47 : wxFontInstanceBase(ptSize, aa)
48 {
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);
53
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;
59
60 if ( filename == BUILTIN_DFB_FONT_FILENAME )
61 m_font = wxIDirectFB::Get()->CreateFont(NULL, &desc);
62 else
63 m_font = wxIDirectFB::Get()->CreateFont(filename.fn_str(), &desc);
64
65 wxASSERT_MSG( m_font, "cannot create font instance" );
66 }
67
68 // ----------------------------------------------------------------------------
69 // wxFontFace
70 // ----------------------------------------------------------------------------
71
72 wxFontInstance *wxFontFace::CreateFontInstance(float ptSize, bool aa)
73 {
74 return new wxFontInstance(ptSize, aa, m_fileName);
75 }
76
77 // ----------------------------------------------------------------------------
78 // wxFontBundle
79 // ----------------------------------------------------------------------------
80
81 wxFontBundle::wxFontBundle(const wxString& name,
82 const wxString& fileRegular,
83 const wxString& fileBold,
84 const wxString& fileItalic,
85 const wxString& fileBoldItalic,
86 bool isFixed)
87 {
88 m_name = name;
89 m_isFixed = isFixed;
90
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);
99 }
100
101 // ----------------------------------------------------------------------------
102 // wxFontsManager
103 // ----------------------------------------------------------------------------
104
105 /*
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
111 files.
112
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:
116
117 [Font Name]
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):
124 IsFixed=1
125
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:
129
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):
138 Default=Font Name
139 */
140
141 void wxFontsManager::AddAllFonts()
142 {
143 wxString path;
144 if ( !wxGetEnv("WXDFB_FONTPATH", &path) )
145 path = wxT(wxINSTALL_PREFIX "/share/wx/fonts");
146
147 wxStringTokenizer tkn(path, wxPATH_SEP);
148 while ( tkn.HasMoreTokens() )
149 {
150 wxString dir = tkn.GetNextToken();
151
152 if ( !wxDir::Exists(dir) )
153 {
154 wxLogDebug("font directory %s doesn't exist", dir);
155 continue;
156 }
157
158 wxArrayString indexFiles;
159 if ( !wxDir::GetAllFiles(dir, &indexFiles, "FontsIndex") )
160 continue;
161
162 for ( wxArrayString::const_iterator i = indexFiles.begin();
163 i != indexFiles.end(); ++i )
164 {
165 AddFontsFromDir(*i);
166 }
167 }
168
169 if ( GetBundles().empty() )
170 {
171 // We can fall back to the builtin default font if no other fonts are
172 // defined:
173 wxLogTrace("font",
174 _("no fonts found in %s, using builtin font"), path);
175
176 AddBundle
177 (
178 new wxFontBundle
179 (
180 _("Default font"),
181 BUILTIN_DFB_FONT_FILENAME,
182 wxEmptyString,
183 wxEmptyString,
184 wxEmptyString,
185 false // IsFixed
186 )
187 );
188 }
189 }
190
191 void wxFontsManager::AddFontsFromDir(const wxString& indexFile)
192 {
193 wxFileName fn(indexFile);
194 wxString dir = fn.GetPath();
195
196 if ( !fn.FileExists() )
197 {
198 wxLogWarning(_("Fonts index file %s disappeared while loading fonts."),
199 indexFile.c_str());
200 return;
201 }
202
203 wxLogTrace("font", "adding fonts from %s", dir.c_str());
204
205 wxFileConfig cfg(wxEmptyString, wxEmptyString,
206 indexFile, wxEmptyString,
207 wxCONFIG_USE_LOCAL_FILE);
208
209 long i;
210 wxString name;
211 for ( bool cont = cfg.GetFirstGroup(name, i);
212 cont;
213 cont = cfg.GetNextGroup(name, i) )
214 {
215 AddFont(dir, name, cfg);
216 }
217
218 // set default fonts for families:
219 SetDefaultFonts(cfg);
220 }
221
222 static wxString
223 ReadFilePath(const wxString& name, const wxString& dir, wxFileConfig& cfg)
224 {
225 wxString p = cfg.Read(name, wxEmptyString);
226
227 if ( p.empty() || wxFileName(p).IsAbsolute() )
228 return p;
229
230 return dir + "/" + p;
231 }
232
233 void wxFontsManager::AddFont(const wxString& dir,
234 const wxString& name,
235 wxFileConfig& cfg)
236 {
237 wxLogTrace("font", "adding font '%s'", name.c_str());
238
239 wxConfigPathChanger ch(&cfg, wxString::Format("/%s/", name.c_str()));
240
241 AddBundle
242 (
243 new wxFontBundle
244 (
245 name,
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)
251 )
252 );
253 }
254
255 void wxFontsManager::SetDefaultFonts(wxFileConfig& cfg)
256 {
257 wxString name;
258
259 if ( cfg.Read("Default", &name) )
260 {
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;
267 }
268
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;
281 }