]> git.saurik.com Git - wxWidgets.git/blob - src/dfb/fontmgr.cpp
Fixed wxAtomicInc/Dec() to not use asm/atomic.h header on Linux - it's kernel interna...
[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 // RCS-ID: $Id$
7 // Copyright: (c) 2001-2002 SciTech Software, Inc. (www.scitechsoft.com)
8 // (c) 2006 REA Elektronik GmbH
9 // Licence: wxWindows licence
10 /////////////////////////////////////////////////////////////////////////////
11
12 // For compilers that support precompilation, includes "wx.h".
13 #include "wx/wxprec.h"
14
15 #ifdef __BORLANDC__
16 #pragma hdrstop
17 #endif
18
19 #ifndef WX_PRECOMP
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 int scrSizePx, scrSizeMM;
50 wxDisplaySize(NULL, &scrSizePx);
51 wxDisplaySizeMM(NULL, &scrSizeMM);
52 double dpi = (scrSizePx / (scrSizeMM * mm2inches));
53 // NB: DFB's fract_height value is 32bit integer with the last 6 bit
54 // representing fractional value, hence the multiplication by 64;
55 // 1pt=1/72inch, hence "/ 72"
56 int pixSize = int(ptSize * dpi * 64 / 72);
57
58 DFBFontDescription desc;
59 desc.flags = (DFBFontDescriptionFlags)(
60 DFDESC_ATTRIBUTES | DFDESC_FRACT_HEIGHT);
61 desc.attributes = aa ? DFFA_NONE : DFFA_MONOCHROME;
62 desc.fract_height = pixSize;
63
64 if ( filename == BUILTIN_DFB_FONT_FILENAME )
65 m_font = wxIDirectFB::Get()->CreateFont(NULL, &desc);
66 else
67 m_font = wxIDirectFB::Get()->CreateFont(filename.fn_str(), &desc);
68
69 wxASSERT_MSG( m_font, "cannot create font instance" );
70 }
71
72 // ----------------------------------------------------------------------------
73 // wxFontFace
74 // ----------------------------------------------------------------------------
75
76 wxFontInstance *wxFontFace::CreateFontInstance(float ptSize, bool aa)
77 {
78 return new wxFontInstance(ptSize, aa, m_fileName);
79 }
80
81 // ----------------------------------------------------------------------------
82 // wxFontBundle
83 // ----------------------------------------------------------------------------
84
85 wxFontBundle::wxFontBundle(const wxString& name,
86 const wxString& fileRegular,
87 const wxString& fileBold,
88 const wxString& fileItalic,
89 const wxString& fileBoldItalic,
90 bool isFixed)
91 {
92 m_name = name;
93 m_isFixed = isFixed;
94
95 if ( !fileRegular.empty() )
96 m_faces[FaceType_Regular] = new wxFontFace(fileRegular);
97 if ( !fileItalic.empty() )
98 m_faces[FaceType_Italic] = new wxFontFace(fileItalic);
99 if ( !fileBold.empty() )
100 m_faces[FaceType_Bold] = new wxFontFace(fileBold);
101 if ( !fileBoldItalic.empty() )
102 m_faces[FaceType_BoldItalic] = new wxFontFace(fileBoldItalic);
103 }
104
105 // ----------------------------------------------------------------------------
106 // wxFontsManager
107 // ----------------------------------------------------------------------------
108
109 /*
110 The code below looks up and parses font configuration files FontsIndex.
111 The files are looked up in directories specified in the WXDFB_FONTPATH
112 environment variable (separated with :, similarly to the PATH variable).
113 If the variable is not set, $prefix/share/wx/fonts directory is used.
114 All subdirectories of directories on the path are scanned for FontsIndex
115 files.
116
117 The FontsIndex file is standard wxFileConfig file text file. Each toplevel
118 group specifies one font bundle, font's name is the name of group. Group's
119 entries look like this:
120
121 [Font Name]
122 # font files (at least one of them must be present):
123 Regular=RegularFaceFile.ttf
124 Italic=ItalicFaceFile.ttf
125 Bold=BoldFaceFile.ttf
126 BoldItalic=BoldItalicFaceFile.ttf
127 # optional tag indicating this font is fixed-with (default is false):
128 IsFixed=1
129
130 Additionally, there may be DefaultXXX entries at the toplevel for every
131 family XXX and a Default entry that is shortcut for setting all families'
132 default, their value is name of the default font:
133
134 # optional tags indicating the default font for given family:
135 DefaultDecorative=Font Name
136 DefaultRoman=Font Name
137 DefaultScript=Font Name
138 DefaultSwiss=Font Name
139 DefaultModern=Font Name
140 DefaultTeletype=Font Name
141 # indicate the font that is default for all families (optional):
142 Default=Font Name
143 */
144
145 void wxFontsManager::AddAllFonts()
146 {
147 wxString path;
148 if ( !wxGetEnv("WXDFB_FONTPATH", &path) )
149 path = _T(wxINSTALL_PREFIX "/share/wx/fonts");
150
151 wxStringTokenizer tkn(path, wxPATH_SEP);
152 while ( tkn.HasMoreTokens() )
153 {
154 wxString dir = tkn.GetNextToken();
155
156 if ( !wxDir::Exists(dir) )
157 {
158 wxLogDebug("font directory %s doesn't exist", dir);
159 continue;
160 }
161
162 wxArrayString indexFiles;
163 if ( !wxDir::GetAllFiles(dir, &indexFiles, "FontsIndex") )
164 continue;
165
166 for ( wxArrayString::const_iterator i = indexFiles.begin();
167 i != indexFiles.end(); ++i )
168 {
169 AddFontsFromDir(*i);
170 }
171 }
172
173 if ( GetBundles().empty() )
174 {
175 // We can fall back to the builtin default font if no other fonts are
176 // defined:
177 wxLogTrace("font",
178 _("no fonts found in %s, using builtin font"), path);
179
180 AddBundle
181 (
182 new wxFontBundle
183 (
184 _("Default font"),
185 BUILTIN_DFB_FONT_FILENAME,
186 wxEmptyString,
187 wxEmptyString,
188 wxEmptyString,
189 false // IsFixed
190 )
191 );
192 }
193 }
194
195 void wxFontsManager::AddFontsFromDir(const wxString& indexFile)
196 {
197 wxFileName fn(indexFile);
198 wxString dir = fn.GetPath();
199
200 if ( !fn.FileExists() )
201 {
202 wxLogWarning(_("Fonts index file %s disappeared while loading fonts."),
203 indexFile.c_str());
204 return;
205 }
206
207 wxLogTrace("font", "adding fonts from %s", dir.c_str());
208
209 wxFileConfig cfg(wxEmptyString, wxEmptyString,
210 indexFile, wxEmptyString,
211 wxCONFIG_USE_LOCAL_FILE);
212
213 long i;
214 wxString name;
215 for ( bool cont = cfg.GetFirstGroup(name, i);
216 cont;
217 cont = cfg.GetNextGroup(name, i) )
218 {
219 AddFont(dir, name, cfg);
220 }
221
222 // set default fonts for families:
223 SetDefaultFonts(cfg);
224 }
225
226 static wxString
227 ReadFilePath(const wxString& name, const wxString& dir, wxFileConfig& cfg)
228 {
229 wxString p = cfg.Read(name, wxEmptyString);
230
231 if ( p.empty() || wxFileName(p).IsAbsolute() )
232 return p;
233
234 return dir + "/" + p;
235 }
236
237 void wxFontsManager::AddFont(const wxString& dir,
238 const wxString& name,
239 wxFileConfig& cfg)
240 {
241 wxLogTrace("font", "adding font '%s'", name.c_str());
242
243 wxConfigPathChanger ch(&cfg, wxString::Format("/%s/", name.c_str()));
244
245 AddBundle
246 (
247 new wxFontBundle
248 (
249 name,
250 ReadFilePath("Regular", dir, cfg),
251 ReadFilePath("Italic", dir, cfg),
252 ReadFilePath("Bold", dir, cfg),
253 ReadFilePath("BoldItalic", dir, cfg),
254 cfg.Read("IsFixed", (long)false)
255 )
256 );
257 }
258
259 void wxFontsManager::SetDefaultFonts(wxFileConfig& cfg)
260 {
261 wxString name;
262
263 if ( cfg.Read("Default", &name) )
264 {
265 m_defaultFacenames[wxFONTFAMILY_DECORATIVE] =
266 m_defaultFacenames[wxFONTFAMILY_ROMAN] =
267 m_defaultFacenames[wxFONTFAMILY_SCRIPT] =
268 m_defaultFacenames[wxFONTFAMILY_SWISS] =
269 m_defaultFacenames[wxFONTFAMILY_MODERN] =
270 m_defaultFacenames[wxFONTFAMILY_TELETYPE] = name;
271 }
272
273 if ( cfg.Read("DefaultDecorative", &name) )
274 m_defaultFacenames[wxFONTFAMILY_DECORATIVE] = name;
275 if ( cfg.Read("DefaultRoman", &name) )
276 m_defaultFacenames[wxFONTFAMILY_ROMAN] = name;
277 if ( cfg.Read("DefaultScript", &name) )
278 m_defaultFacenames[wxFONTFAMILY_SCRIPT] = name;
279 if ( cfg.Read("DefaultSwiss", &name) )
280 m_defaultFacenames[wxFONTFAMILY_SWISS] = name;
281 if ( cfg.Read("DefaultModern", &name) )
282 m_defaultFacenames[wxFONTFAMILY_MODERN] = name;
283 if ( cfg.Read("DefaultTeletype", &name) )
284 m_defaultFacenames[wxFONTFAMILY_TELETYPE] = name;
285 }