]>
git.saurik.com Git - wxWidgets.git/blob - src/dfb/fontmgr.cpp
1bc84cac588326e4f0108cf9a14d131b3dee0d90
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 wxFontInstance::wxFontInstance(float ptSize
, bool aa
,
41 const wxString
& filename
)
42 : wxFontInstanceBase(ptSize
, aa
)
44 int scrSizePx
, scrSizeMM
;
45 wxDisplaySize(NULL
, &scrSizePx
);
46 wxDisplaySizeMM(NULL
, &scrSizeMM
);
47 double dpi
= (scrSizePx
/ (scrSizeMM
* mm2inches
));
48 // NB: DFB's fract_height value is 32bit integer with the last 6 bit
49 // representing fractional value, hence the multiplication by 64;
50 // 1pt=1/72inch, hence "/ 72"
51 int pixSize
= int(ptSize
* dpi
* 64 / 72);
53 DFBFontDescription desc
;
54 desc
.flags
= (DFBFontDescriptionFlags
)(
55 DFDESC_ATTRIBUTES
| DFDESC_FRACT_HEIGHT
);
56 desc
.attributes
= aa
? DFFA_NONE
: DFFA_MONOCHROME
;
57 desc
.fract_height
= pixSize
;
58 m_font
= wxIDirectFB::Get()->CreateFont(filename
.fn_str(), &desc
);
60 wxASSERT_MSG( m_font
, _T("cannot create font instance") );
63 // ----------------------------------------------------------------------------
65 // ----------------------------------------------------------------------------
67 wxFontInstance
*wxFontFace::CreateFontInstance(float ptSize
, bool aa
)
69 return new wxFontInstance(ptSize
, aa
, m_fileName
);
72 // ----------------------------------------------------------------------------
74 // ----------------------------------------------------------------------------
76 wxFontBundle::wxFontBundle(const wxString
& name
,
77 const wxString
& fileRegular
,
78 const wxString
& fileBold
,
79 const wxString
& fileItalic
,
80 const wxString
& fileBoldItalic
,
86 if ( !fileRegular
.empty() )
87 m_faces
[FaceType_Regular
] = new wxFontFace(fileRegular
);
88 if ( !fileItalic
.empty() )
89 m_faces
[FaceType_Italic
] = new wxFontFace(fileItalic
);
90 if ( !fileBold
.empty() )
91 m_faces
[FaceType_Bold
] = new wxFontFace(fileBold
);
92 if ( !fileBoldItalic
.empty() )
93 m_faces
[FaceType_BoldItalic
] = new wxFontFace(fileBoldItalic
);
96 // ----------------------------------------------------------------------------
98 // ----------------------------------------------------------------------------
101 The code below looks up and parses font configuration files FontsIndex.
102 The files are looked up in directories specified in the WXDFB_FONTPATH
103 environment variable (separated with :, similarly to the PATH variable).
104 If the variable is not set, $prefix/share/wx/fonts directory is used.
105 All subdirectories of directories on the path are scanned for FontsIndex
108 The FontsIndex file is standard wxFileConfig file text file. Each toplevel
109 group specifies one font bundle, font's name is the name of group. Group's
110 entries look like this:
113 # font files (at least one of them must be present):
114 Regular=RegularFaceFile.ttf
115 Italic=ItalicFaceFile.ttf
116 Bold=BoldFaceFile.ttf
117 BoldItalic=BoldItalicFaceFile.ttf
118 # optional tag indicating this font is fixed-with (default is false):
121 Additionally, there may be DefaultXXX entries at the toplevel for every
122 family XXX and a Default entry that is shortcut for setting all families'
123 default, their value is name of the default font:
125 # optional tags indicating the default font for given family:
126 DefaultDecorative=Font Name
127 DefaultRoman=Font Name
128 DefaultScript=Font Name
129 DefaultSwiss=Font Name
130 DefaultModern=Font Name
131 DefaultTeletype=Font Name
132 # indicate the font that is default for all families (optional):
136 void wxFontsManager::AddAllFonts()
139 if ( !wxGetEnv(_T("WXDFB_FONTPATH"), &path
) )
140 path
= _T(wxINSTALL_PREFIX
"/share/wx/fonts");
142 wxStringTokenizer
tkn(path
, wxPATH_SEP
);
143 while ( tkn
.HasMoreTokens() )
145 wxString dir
= tkn
.GetNextToken();
147 if ( !wxDir::Exists(dir
) )
149 wxLogTrace(_T("font"), _T("font directory %s doesn't exist"), dir
);
153 wxArrayString indexFiles
;
154 if ( !wxDir::GetAllFiles(dir
, &indexFiles
, _T("FontsIndex")) )
157 for ( wxArrayString::const_iterator i
= indexFiles
.begin();
158 i
!= indexFiles
.end(); ++i
)
164 if ( GetBundles().empty() )
166 // wxDFB is unusable without fonts, so terminate the app
167 wxLogFatalError(_("No fonts found in %s."), path
.c_str());
171 void wxFontsManager::AddFontsFromDir(const wxString
& indexFile
)
173 wxFileName
fn(indexFile
);
174 wxString dir
= fn
.GetPath();
176 if ( !fn
.FileExists() )
178 wxLogWarning(_("Fonts index file %s disappeared while loading fonts."),
183 wxLogTrace(_T("font"), _T("adding fonts from %s"), dir
.c_str());
185 wxFileConfig
cfg(wxEmptyString
, wxEmptyString
,
186 indexFile
, wxEmptyString
,
187 wxCONFIG_USE_LOCAL_FILE
);
191 for ( bool cont
= cfg
.GetFirstGroup(name
, i
);
193 cont
= cfg
.GetNextGroup(name
, i
) )
195 AddFont(dir
, name
, cfg
);
198 // set default fonts for families:
199 SetDefaultFonts(cfg
);
203 ReadFilePath(const wxString
& name
, const wxString
& dir
, wxFileConfig
& cfg
)
205 wxString p
= cfg
.Read(name
, wxEmptyString
);
207 if ( p
.empty() || wxFileName(p
).IsAbsolute() )
210 return dir
+ _T("/") + p
;
213 void wxFontsManager::AddFont(const wxString
& dir
,
214 const wxString
& name
,
217 wxLogTrace(_T("font"), _T("adding font '%s'"), name
.c_str());
219 wxConfigPathChanger
ch(&cfg
, wxString::Format(_T("/%s/"), name
.c_str()));
226 ReadFilePath(_T("Regular"), dir
, cfg
),
227 ReadFilePath(_T("Italic"), dir
, cfg
),
228 ReadFilePath(_T("Bold"), dir
, cfg
),
229 ReadFilePath(_T("BoldItalic"), dir
, cfg
),
230 cfg
.Read(_T("IsFixed"), (long)false)
235 void wxFontsManager::SetDefaultFonts(wxFileConfig
& cfg
)
239 if ( cfg
.Read(_T("Default"), &name
) )
241 m_defaultFacenames
[wxFONTFAMILY_DECORATIVE
] =
242 m_defaultFacenames
[wxFONTFAMILY_ROMAN
] =
243 m_defaultFacenames
[wxFONTFAMILY_SCRIPT
] =
244 m_defaultFacenames
[wxFONTFAMILY_SWISS
] =
245 m_defaultFacenames
[wxFONTFAMILY_MODERN
] =
246 m_defaultFacenames
[wxFONTFAMILY_TELETYPE
] = name
;
249 if ( cfg
.Read(_T("DefaultDecorative"), &name
) )
250 m_defaultFacenames
[wxFONTFAMILY_DECORATIVE
] = name
;
251 if ( cfg
.Read(_T("DefaultRoman"), &name
) )
252 m_defaultFacenames
[wxFONTFAMILY_ROMAN
] = name
;
253 if ( cfg
.Read(_T("DefaultScript"), &name
) )
254 m_defaultFacenames
[wxFONTFAMILY_SCRIPT
] = name
;
255 if ( cfg
.Read(_T("DefaultSwiss"), &name
) )
256 m_defaultFacenames
[wxFONTFAMILY_SWISS
] = name
;
257 if ( cfg
.Read(_T("DefaultModern"), &name
) )
258 m_defaultFacenames
[wxFONTFAMILY_MODERN
] = name
;
259 if ( cfg
.Read(_T("DefaultTeletype"), &name
) )
260 m_defaultFacenames
[wxFONTFAMILY_TELETYPE
] = name
;