1 /////////////////////////////////////////////////////////////////////////////
2 // Name: src/dfb/fontmgr.cpp
3 // Purpose: font management for wxDFB
4 // Author: Vaclav Slavik
7 // Copyright: (c) 2001-2002 SciTech Software, Inc. (www.scitechsoft.com)
8 // (c) 2006 REA Elektronik GmbH
9 // Licence: wxWindows licence
10 /////////////////////////////////////////////////////////////////////////////
12 // For compilers that support precompilation, includes "wx.h".
13 #include "wx/wxprec.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 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);
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
;
64 if ( filename
== BUILTIN_DFB_FONT_FILENAME
)
65 m_font
= wxIDirectFB::Get()->CreateFont(NULL
, &desc
);
67 m_font
= wxIDirectFB::Get()->CreateFont(filename
.fn_str(), &desc
);
69 wxASSERT_MSG( m_font
, "cannot create font instance" );
72 // ----------------------------------------------------------------------------
74 // ----------------------------------------------------------------------------
76 wxFontInstance
*wxFontFace::CreateFontInstance(float ptSize
, bool aa
)
78 return new wxFontInstance(ptSize
, aa
, m_fileName
);
81 // ----------------------------------------------------------------------------
83 // ----------------------------------------------------------------------------
85 wxFontBundle::wxFontBundle(const wxString
& name
,
86 const wxString
& fileRegular
,
87 const wxString
& fileBold
,
88 const wxString
& fileItalic
,
89 const wxString
& fileBoldItalic
,
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
);
105 // ----------------------------------------------------------------------------
107 // ----------------------------------------------------------------------------
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
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:
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):
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:
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):
145 void wxFontsManager::AddAllFonts()
148 if ( !wxGetEnv("WXDFB_FONTPATH", &path
) )
149 path
= _T(wxINSTALL_PREFIX
"/share/wx/fonts");
151 wxStringTokenizer
tkn(path
, wxPATH_SEP
);
152 while ( tkn
.HasMoreTokens() )
154 wxString dir
= tkn
.GetNextToken();
156 if ( !wxDir::Exists(dir
) )
158 wxLogDebug("font directory %s doesn't exist", dir
);
162 wxArrayString indexFiles
;
163 if ( !wxDir::GetAllFiles(dir
, &indexFiles
, "FontsIndex") )
166 for ( wxArrayString::const_iterator i
= indexFiles
.begin();
167 i
!= indexFiles
.end(); ++i
)
173 if ( GetBundles().empty() )
175 // We can fall back to the builtin default font if no other fonts are
178 _("no fonts found in %s, using builtin font"), path
);
185 BUILTIN_DFB_FONT_FILENAME
,
195 void wxFontsManager::AddFontsFromDir(const wxString
& indexFile
)
197 wxFileName
fn(indexFile
);
198 wxString dir
= fn
.GetPath();
200 if ( !fn
.FileExists() )
202 wxLogWarning(_("Fonts index file %s disappeared while loading fonts."),
207 wxLogTrace("font", "adding fonts from %s", dir
.c_str());
209 wxFileConfig
cfg(wxEmptyString
, wxEmptyString
,
210 indexFile
, wxEmptyString
,
211 wxCONFIG_USE_LOCAL_FILE
);
215 for ( bool cont
= cfg
.GetFirstGroup(name
, i
);
217 cont
= cfg
.GetNextGroup(name
, i
) )
219 AddFont(dir
, name
, cfg
);
222 // set default fonts for families:
223 SetDefaultFonts(cfg
);
227 ReadFilePath(const wxString
& name
, const wxString
& dir
, wxFileConfig
& cfg
)
229 wxString p
= cfg
.Read(name
, wxEmptyString
);
231 if ( p
.empty() || wxFileName(p
).IsAbsolute() )
234 return dir
+ "/" + p
;
237 void wxFontsManager::AddFont(const wxString
& dir
,
238 const wxString
& name
,
241 wxLogTrace("font", "adding font '%s'", name
.c_str());
243 wxConfigPathChanger
ch(&cfg
, wxString::Format("/%s/", name
.c_str()));
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)
259 void wxFontsManager::SetDefaultFonts(wxFileConfig
& cfg
)
263 if ( cfg
.Read("Default", &name
) )
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
;
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
;