moved wxMGL's font management code to common so that it can be reused by wxDFB; imple...
[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 #include "wx/fileconf.h"
20 #include "wx/filename.h"
21
22 #include "wx/private/fontmgr.h"
23 #include "wx/dfb/wrapdfb.h"
24
25 // ============================================================================
26 // implementation
27 // ============================================================================
28
29 // ----------------------------------------------------------------------------
30 // wxFontInstance
31 // ----------------------------------------------------------------------------
32
33 wxFontInstance::wxFontInstance(float ptSize, bool aa,
34 const wxString& filename)
35 : wxFontInstanceBase(ptSize, aa)
36 {
37 int scrSizePx, scrSizeMM;
38 wxDisplaySize(NULL, &scrSizePx);
39 wxDisplaySizeMM(NULL, &scrSizeMM);
40 double dpi = (scrSizePx / (scrSizeMM * mm2inches));
41 // NB: DFB's fract_height value is 32bit integer with the last 6 bit
42 // representing fractional value, hence the multiplication by 64;
43 // 1pt=1/72inch, hence "/ 72"
44 int pixSize = int(ptSize * dpi * 64 / 72);
45
46 DFBFontDescription desc;
47 desc.flags = (DFBFontDescriptionFlags)(
48 DFDESC_ATTRIBUTES | DFDESC_FRACT_HEIGHT);
49 desc.attributes = aa ? DFFA_NONE : DFFA_MONOCHROME;
50 desc.fract_height = pixSize;
51 m_font = wxIDirectFB::Get()->CreateFont(filename.fn_str(), &desc);
52
53 wxASSERT_MSG( m_font, _T("cannot create font instance") );
54 }
55
56 // ----------------------------------------------------------------------------
57 // wxFontFace
58 // ----------------------------------------------------------------------------
59
60 wxFontInstance *wxFontFace::CreateFontInstance(float ptSize, bool aa)
61 {
62 return new wxFontInstance(ptSize, aa, m_fileName);
63 }
64
65 // ----------------------------------------------------------------------------
66 // wxFontBundle
67 // ----------------------------------------------------------------------------
68
69 wxFontBundle::wxFontBundle(const wxString& name,
70 const wxString& fileRegular,
71 const wxString& fileBold,
72 const wxString& fileItalic,
73 const wxString& fileBoldItalic,
74 bool isFixed)
75 {
76 m_name = name;
77 m_isFixed = isFixed;
78
79 if ( !fileRegular.empty() )
80 m_faces[FaceType_Regular] = new wxFontFace(fileRegular);
81 if ( !fileItalic.empty() )
82 m_faces[FaceType_Italic] = new wxFontFace(fileItalic);
83 if ( !fileBold.empty() )
84 m_faces[FaceType_Bold] = new wxFontFace(fileBold);
85 if ( !fileBoldItalic.empty() )
86 m_faces[FaceType_BoldItalic] = new wxFontFace(fileBoldItalic);
87 }
88
89 // ----------------------------------------------------------------------------
90 // wxFontsManager
91 // ----------------------------------------------------------------------------
92
93 /*
94 The code below parses font configuration file ${WXDFB_FONTDIR}/FontsIndex.
95 By default, the directory is $prefix/share/wx/fonts, but can be ovewritten
96 by setting WXDFB_FONTDIR environment variable.
97
98 The file is standard wxFileConfig file text file. Each toplevel group
99 specifies one font bundle, font's name is the name of group. Group's entries
100 look like this:
101
102 [Font Name]
103 # font files (at least one of them must be present):
104 Regular=RegularFaceFile.ttf
105 Italic=ItalicFaceFile.ttf
106 Bold=BoldFaceFile.ttf
107 BoldItalic=BoldItalicFaceFile.ttf
108 # optional tag indicating this font is fixed-with (default is false):
109 IsFixed=1
110
111 Additionally, there may be DefaultXXX entries at the toplevel for every
112 family XXX and a Default entry that is shortcut for setting all families'
113 default, their value is name of the default font:
114
115 # optional tags indicating the default font for given family:
116 DefaultDecorative=Font Name
117 DefaultRoman=Font Name
118 DefaultScript=Font Name
119 DefaultSwiss=Font Name
120 DefaultModern=Font Name
121 DefaultTeletype=Font Name
122 # indicate the font that is default for all families (optional):
123 Default=Font Name
124 */
125
126 void wxFontsManager::AddAllFonts()
127 {
128 wxString dir = _T(wxINSTALL_PREFIX "/share/wx/fonts");
129 wxGetEnv(_T("WXDFB_FONTDIR"), &dir);
130
131 wxString indexFile = dir + _T("/FontsIndex");
132
133 if ( !wxFileName::FileExists(indexFile) )
134 {
135 wxLogWarning(_("No fonts found in %s"), dir.c_str());
136 return;
137 }
138
139 wxLogTrace(_T("font"), _T("adding fonts from %s"), dir.c_str());
140
141 wxFileConfig cfg(wxEmptyString, wxEmptyString,
142 indexFile, wxEmptyString,
143 wxCONFIG_USE_LOCAL_FILE);
144
145 long i;
146 wxString name;
147 for ( bool cont = cfg.GetFirstGroup(name, i);
148 cont;
149 cont = cfg.GetNextGroup(name, i) )
150 {
151 AddFont(dir, name, cfg);
152 }
153
154 // set default fonts for families:
155 SetDefaultFonts(cfg);
156 }
157
158 static wxString
159 ReadFilePath(const wxString& name, const wxString& dir, wxFileConfig& cfg)
160 {
161 wxString p = cfg.Read(name, wxEmptyString);
162
163 if ( p.empty() || wxFileName(p).IsAbsolute() )
164 return p;
165
166 return dir + _T("/") + p;
167 }
168
169 void wxFontsManager::AddFont(const wxString& dir,
170 const wxString& name,
171 wxFileConfig& cfg)
172 {
173 wxLogTrace(_T("font"), _T("adding font '%s'"), name.c_str());
174
175 wxConfigPathChanger ch(&cfg, wxString::Format(_T("/%s/"), name.c_str()));
176
177 AddBundle
178 (
179 new wxFontBundle
180 (
181 name,
182 ReadFilePath(_T("Regular"), dir, cfg),
183 ReadFilePath(_T("Italic"), dir, cfg),
184 ReadFilePath(_T("Bold"), dir, cfg),
185 ReadFilePath(_T("BoldItalic"), dir, cfg),
186 cfg.Read(_T("IsFixed"), (long)false)
187 )
188 );
189 }
190
191 void wxFontsManager::SetDefaultFonts(wxFileConfig& cfg)
192 {
193 wxString name;
194
195 if ( cfg.Read(_T("Default"), &name) )
196 {
197 m_defaultFacenames[wxFONTFAMILY_DECORATIVE] =
198 m_defaultFacenames[wxFONTFAMILY_ROMAN] =
199 m_defaultFacenames[wxFONTFAMILY_SCRIPT] =
200 m_defaultFacenames[wxFONTFAMILY_SWISS] =
201 m_defaultFacenames[wxFONTFAMILY_MODERN] =
202 m_defaultFacenames[wxFONTFAMILY_TELETYPE] = name;
203 }
204
205 if ( cfg.Read(_T("DefaultDecorative"), &name) )
206 m_defaultFacenames[wxFONTFAMILY_DECORATIVE] = name;
207 if ( cfg.Read(_T("DefaultRoman"), &name) )
208 m_defaultFacenames[wxFONTFAMILY_ROMAN] = name;
209 if ( cfg.Read(_T("DefaultScript"), &name) )
210 m_defaultFacenames[wxFONTFAMILY_SCRIPT] = name;
211 if ( cfg.Read(_T("DefaultSwiss"), &name) )
212 m_defaultFacenames[wxFONTFAMILY_SWISS] = name;
213 if ( cfg.Read(_T("DefaultModern"), &name) )
214 m_defaultFacenames[wxFONTFAMILY_MODERN] = name;
215 if ( cfg.Read(_T("DefaultTeletype"), &name) )
216 m_defaultFacenames[wxFONTFAMILY_TELETYPE] = name;
217 }