]> git.saurik.com Git - wxWidgets.git/blame - src/dfb/fontmgr.cpp
Avoid calling gtk_window_get_position() from "configure-event" handler, if possible.
[wxWidgets.git] / src / dfb / fontmgr.cpp
CommitLineData
d7ae4a62
VS
1/////////////////////////////////////////////////////////////////////////////
2// Name: src/dfb/fontmgr.cpp
3// Purpose: font management for wxDFB
4// Author: Vaclav Slavik
5// Created: 2006-11-18
d7ae4a62
VS
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
23205be8 18#ifndef WX_PRECOMP
90815a45 19 #include "wx/gdicmn.h"
23205be8
VS
20 #include "wx/utils.h"
21 #include "wx/log.h"
22#endif
23
d7ae4a62
VS
24#include "wx/fileconf.h"
25#include "wx/filename.h"
5ea50c92
VS
26#include "wx/tokenzr.h"
27#include "wx/dir.h"
d7ae4a62
VS
28
29#include "wx/private/fontmgr.h"
30#include "wx/dfb/wrapdfb.h"
31
32// ============================================================================
33// implementation
34// ============================================================================
35
36// ----------------------------------------------------------------------------
37// wxFontInstance
38// ----------------------------------------------------------------------------
39
c4f6f41c
VS
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
d7ae4a62
VS
45wxFontInstance::wxFontInstance(float ptSize, bool aa,
46 const wxString& filename)
47 : wxFontInstanceBase(ptSize, aa)
48{
d7ae4a62
VS
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"
90815a45 52 int pixSize = int(ptSize * wxGetDisplayPPI().y * 64 / 72);
d7ae4a62
VS
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;
c4f6f41c
VS
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);
d7ae4a62 64
a5001e93 65 wxASSERT_MSG( m_font, "cannot create font instance" );
d7ae4a62
VS
66}
67
68// ----------------------------------------------------------------------------
69// wxFontFace
70// ----------------------------------------------------------------------------
71
72wxFontInstance *wxFontFace::CreateFontInstance(float ptSize, bool aa)
73{
74 return new wxFontInstance(ptSize, aa, m_fileName);
75}
76
77// ----------------------------------------------------------------------------
78// wxFontBundle
79// ----------------------------------------------------------------------------
80
81wxFontBundle::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/*
5ea50c92
VS
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.
d7ae4a62 112
5ea50c92
VS
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:
d7ae4a62
VS
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
141void wxFontsManager::AddAllFonts()
142{
5ea50c92 143 wxString path;
a5001e93 144 if ( !wxGetEnv("WXDFB_FONTPATH", &path) )
9a83f860 145 path = wxT(wxINSTALL_PREFIX "/share/wx/fonts");
d7ae4a62 146
5ea50c92
VS
147 wxStringTokenizer tkn(path, wxPATH_SEP);
148 while ( tkn.HasMoreTokens() )
149 {
150 wxString dir = tkn.GetNextToken();
151
cc096b11
VS
152 if ( !wxDir::Exists(dir) )
153 {
a5001e93 154 wxLogDebug("font directory %s doesn't exist", dir);
cc096b11
VS
155 continue;
156 }
157
5ea50c92 158 wxArrayString indexFiles;
a5001e93 159 if ( !wxDir::GetAllFiles(dir, &indexFiles, "FontsIndex") )
5ea50c92
VS
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 {
c4f6f41c
VS
171 // We can fall back to the builtin default font if no other fonts are
172 // defined:
a5001e93 173 wxLogTrace("font",
c4f6f41c
VS
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 );
5ea50c92
VS
188 }
189}
190
191void wxFontsManager::AddFontsFromDir(const wxString& indexFile)
192{
193 wxFileName fn(indexFile);
194 wxString dir = fn.GetPath();
d7ae4a62 195
5ea50c92 196 if ( !fn.FileExists() )
d7ae4a62 197 {
5ea50c92
VS
198 wxLogWarning(_("Fonts index file %s disappeared while loading fonts."),
199 indexFile.c_str());
d7ae4a62
VS
200 return;
201 }
202
a5001e93 203 wxLogTrace("font", "adding fonts from %s", dir.c_str());
d7ae4a62
VS
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
222static wxString
223ReadFilePath(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
a5001e93 230 return dir + "/" + p;
d7ae4a62
VS
231}
232
233void wxFontsManager::AddFont(const wxString& dir,
234 const wxString& name,
235 wxFileConfig& cfg)
236{
a5001e93 237 wxLogTrace("font", "adding font '%s'", name.c_str());
d7ae4a62 238
a5001e93 239 wxConfigPathChanger ch(&cfg, wxString::Format("/%s/", name.c_str()));
d7ae4a62
VS
240
241 AddBundle
242 (
243 new wxFontBundle
244 (
245 name,
a5001e93
VS
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)
d7ae4a62
VS
251 )
252 );
253}
254
255void wxFontsManager::SetDefaultFonts(wxFileConfig& cfg)
256{
257 wxString name;
258
a5001e93 259 if ( cfg.Read("Default", &name) )
d7ae4a62
VS
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
a5001e93 269 if ( cfg.Read("DefaultDecorative", &name) )
d7ae4a62 270 m_defaultFacenames[wxFONTFAMILY_DECORATIVE] = name;
a5001e93 271 if ( cfg.Read("DefaultRoman", &name) )
d7ae4a62 272 m_defaultFacenames[wxFONTFAMILY_ROMAN] = name;
a5001e93 273 if ( cfg.Read("DefaultScript", &name) )
d7ae4a62 274 m_defaultFacenames[wxFONTFAMILY_SCRIPT] = name;
a5001e93 275 if ( cfg.Read("DefaultSwiss", &name) )
d7ae4a62 276 m_defaultFacenames[wxFONTFAMILY_SWISS] = name;
a5001e93 277 if ( cfg.Read("DefaultModern", &name) )
d7ae4a62 278 m_defaultFacenames[wxFONTFAMILY_MODERN] = name;
a5001e93 279 if ( cfg.Read("DefaultTeletype", &name) )
d7ae4a62
VS
280 m_defaultFacenames[wxFONTFAMILY_TELETYPE] = name;
281}