]>
Commit | Line | Data |
---|---|---|
d7ae4a62 VS |
1 | ///////////////////////////////////////////////////////////////////////////// |
2 | // Name: src/common/fontmgrcmn.cpp | |
3 | // Purpose: font management for ports that don't have their own | |
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/private/fontmgr.h" | |
20 | ||
21 | #include "wx/listimpl.cpp" | |
22 | #include "wx/hashmap.h" | |
23 | ||
24 | WX_DECLARE_LIST(wxFontInstance, wxFontInstanceList); | |
25 | WX_DEFINE_LIST(wxFontInstanceList) | |
26 | WX_DEFINE_LIST(wxFontBundleList) | |
9580fdc3 | 27 | |
d7ae4a62 VS |
28 | WX_DECLARE_HASH_MAP(wxString, wxFontBundle*, |
29 | wxStringHash, wxStringEqual, | |
9580fdc3 VS |
30 | wxFontBundleHashBase); |
31 | // in STL build, hash class is typedef to a template, so it can't be forward | |
32 | // declared, as we do; solve it by having a dummy class: | |
33 | class wxFontBundleHash : public wxFontBundleHashBase | |
34 | { | |
35 | }; | |
d7ae4a62 VS |
36 | |
37 | // ============================================================================ | |
38 | // implementation | |
39 | // ============================================================================ | |
40 | ||
41 | // ---------------------------------------------------------------------------- | |
42 | // wxFontFaceBase | |
43 | // ---------------------------------------------------------------------------- | |
44 | ||
45 | wxFontFaceBase::wxFontFaceBase() | |
46 | : m_refCnt(0) | |
47 | { | |
48 | m_instances = new wxFontInstanceList; | |
49 | m_instances->DeleteContents(true); | |
50 | } | |
51 | ||
52 | wxFontFaceBase::~wxFontFaceBase() | |
53 | { | |
54 | delete m_instances; | |
55 | } | |
56 | ||
57 | void wxFontFaceBase::Acquire() | |
58 | { | |
59 | m_refCnt++; | |
60 | } | |
61 | ||
62 | void wxFontFaceBase::Release() | |
63 | { | |
64 | if ( --m_refCnt == 0 ) | |
65 | { | |
66 | m_instances->Clear(); | |
67 | } | |
68 | } | |
69 | ||
70 | wxFontInstance *wxFontFaceBase::GetFontInstance(float ptSize, bool aa) | |
71 | { | |
9a83f860 | 72 | wxASSERT_MSG( m_refCnt > 0, wxT("font library not loaded!") ); |
d7ae4a62 | 73 | |
9580fdc3 VS |
74 | for ( wxFontInstanceList::const_iterator i = m_instances->begin(); |
75 | i != m_instances->end(); ++i ) | |
d7ae4a62 | 76 | { |
9580fdc3 VS |
77 | if ( (*i)->GetPointSize() == ptSize && (*i)->IsAntiAliased() == aa ) |
78 | return *i; | |
d7ae4a62 VS |
79 | } |
80 | ||
9580fdc3 | 81 | wxFontInstance *i = CreateFontInstance(ptSize, aa); |
d7ae4a62 VS |
82 | m_instances->Append(i); |
83 | return i; | |
84 | } | |
85 | ||
86 | // ---------------------------------------------------------------------------- | |
87 | // wxFontBundleBase | |
88 | // ---------------------------------------------------------------------------- | |
89 | ||
90 | wxFontBundleBase::wxFontBundleBase() | |
91 | { | |
92 | for (int i = 0; i < FaceType_Max; i++) | |
93 | m_faces[i] = NULL; | |
94 | } | |
95 | ||
96 | wxFontBundleBase::~wxFontBundleBase() | |
97 | { | |
98 | for (int i = 0; i < FaceType_Max; i++) | |
99 | delete m_faces[i]; | |
100 | } | |
101 | ||
102 | wxFontFace *wxFontBundleBase::GetFace(FaceType type) const | |
103 | { | |
104 | wxFontFace *f = m_faces[type]; | |
105 | ||
9a83f860 | 106 | wxCHECK_MSG( f, NULL, wxT("no such face in font bundle") ); |
d7ae4a62 VS |
107 | |
108 | f->Acquire(); | |
109 | ||
110 | return f; | |
111 | } | |
112 | ||
113 | wxFontFace * | |
114 | wxFontBundleBase::GetFaceForFont(const wxFontMgrFontRefData& font) const | |
115 | { | |
dff1625b VS |
116 | wxASSERT_MSG( font.GetFaceName().empty() || |
117 | GetName().CmpNoCase(font.GetFaceName()) == 0, | |
9a83f860 | 118 | wxT("calling GetFaceForFont for incompatible font") ); |
d7ae4a62 VS |
119 | |
120 | int type = FaceType_Regular; | |
121 | ||
122 | if ( font.GetWeight() == wxBOLD ) | |
123 | type |= FaceType_Bold; | |
124 | ||
125 | // FIXME -- this should read "if ( font->GetStyle() == wxITALIC )", | |
0e1f8ea4 VZ |
126 | // but since DFB doesn't support slant, we try to display it with italic |
127 | // face (better than nothing...) | |
d7ae4a62 VS |
128 | if ( font.GetStyle() == wxITALIC || font.GetStyle() == wxSLANT ) |
129 | { | |
130 | if ( HasFace((FaceType)(type | FaceType_Italic)) ) | |
131 | type |= FaceType_Italic; | |
132 | } | |
133 | ||
134 | if ( !HasFace((FaceType)type) ) | |
135 | { | |
209ce629 VS |
136 | // if we can't get the exact font requested, substitute it with |
137 | // some other variant: | |
d7ae4a62 VS |
138 | for (int i = 0; i < FaceType_Max; i++) |
139 | { | |
140 | if ( HasFace((FaceType)i) ) | |
141 | return GetFace((FaceType)i); | |
142 | } | |
143 | ||
9a83f860 | 144 | wxFAIL_MSG( wxT("no face") ); |
d7ae4a62 VS |
145 | return NULL; |
146 | } | |
147 | ||
148 | return GetFace((FaceType)type); | |
149 | } | |
150 | ||
151 | // ---------------------------------------------------------------------------- | |
152 | // wxFontsManagerBase | |
153 | // ---------------------------------------------------------------------------- | |
154 | ||
155 | wxFontsManager *wxFontsManagerBase::ms_instance = NULL; | |
156 | ||
157 | wxFontsManagerBase::wxFontsManagerBase() | |
158 | { | |
159 | m_hash = new wxFontBundleHash(); | |
160 | m_list = new wxFontBundleList; | |
161 | m_list->DeleteContents(true); | |
162 | } | |
163 | ||
164 | wxFontsManagerBase::~wxFontsManagerBase() | |
165 | { | |
166 | delete m_hash; | |
167 | delete m_list; | |
168 | } | |
169 | ||
170 | /* static */ | |
171 | wxFontsManager *wxFontsManagerBase::Get() | |
172 | { | |
173 | if ( !ms_instance ) | |
174 | ms_instance = new wxFontsManager(); | |
175 | return ms_instance; | |
176 | } | |
177 | ||
178 | /* static */ | |
179 | void wxFontsManagerBase::CleanUp() | |
180 | { | |
181 | wxDELETE(ms_instance); | |
182 | } | |
183 | ||
184 | wxFontBundle *wxFontsManagerBase::GetBundle(const wxString& name) const | |
185 | { | |
186 | return (*m_hash)[name.Lower()]; | |
187 | } | |
188 | ||
189 | wxFontBundle * | |
190 | wxFontsManagerBase::GetBundleForFont(const wxFontMgrFontRefData& font) const | |
191 | { | |
192 | wxFontBundle *bundle = NULL; | |
193 | ||
194 | wxString facename = font.GetFaceName(); | |
195 | if ( !facename.empty() ) | |
196 | bundle = GetBundle(facename); | |
197 | ||
198 | if ( !bundle ) | |
199 | { | |
200 | facename = GetDefaultFacename((wxFontFamily)font.GetFamily()); | |
201 | if ( !facename.empty() ) | |
202 | bundle = GetBundle(facename); | |
203 | } | |
204 | ||
205 | if ( !bundle ) | |
206 | { | |
207 | if ( m_list->GetFirst() ) | |
208 | bundle = m_list->GetFirst()->GetData(); | |
209 | else | |
210 | wxFAIL_MSG(wxT("Fatal error, no fonts available!")); | |
211 | } | |
212 | ||
213 | return bundle; | |
214 | } | |
215 | ||
216 | void wxFontsManagerBase::AddBundle(wxFontBundle *bundle) | |
217 | { | |
218 | (*m_hash)[bundle->GetName().Lower()] = bundle; | |
219 | m_list->Append(bundle); | |
220 | } | |
221 | ||
222 | ||
223 | // ---------------------------------------------------------------------------- | |
224 | // wxFontMgrFontRefData | |
225 | // ---------------------------------------------------------------------------- | |
226 | ||
227 | wxFontMgrFontRefData::wxFontMgrFontRefData(int size, | |
5a2c086a VZ |
228 | wxFontFamily family, |
229 | wxFontStyle style, | |
230 | wxFontWeight weight, | |
d7ae4a62 VS |
231 | bool underlined, |
232 | const wxString& faceName, | |
233 | wxFontEncoding encoding) | |
234 | { | |
5a2c086a VZ |
235 | if ( family == wxFONTFAMILY_DEFAULT ) |
236 | family = wxFONTFAMILY_SWISS; | |
d7ae4a62 VS |
237 | if ( size == wxDEFAULT ) |
238 | size = 12; | |
239 | ||
240 | m_info.family = (wxFontFamily)family; | |
241 | m_info.faceName = faceName; | |
242 | m_info.style = (wxFontStyle)style; | |
243 | m_info.weight = (wxFontWeight)weight; | |
244 | m_info.pointSize = size; | |
245 | m_info.underlined = underlined; | |
246 | m_info.encoding = encoding; | |
247 | ||
d7ae4a62 VS |
248 | m_fontFace = NULL; |
249 | m_fontBundle = NULL; | |
250 | m_fontValid = false; | |
251 | } | |
252 | ||
253 | wxFontMgrFontRefData::wxFontMgrFontRefData(const wxFontMgrFontRefData& data) | |
254 | { | |
255 | m_info = data.m_info; | |
d7ae4a62 VS |
256 | |
257 | m_fontFace = data.m_fontFace; | |
258 | m_fontBundle = data.m_fontBundle; | |
259 | m_fontValid = data.m_fontValid; | |
260 | if ( m_fontFace ) | |
261 | m_fontFace->Acquire(); | |
262 | } | |
263 | ||
264 | wxFontMgrFontRefData::~wxFontMgrFontRefData() | |
265 | { | |
266 | if ( m_fontFace ) | |
267 | m_fontFace->Release(); | |
268 | } | |
269 | ||
270 | wxFontBundle *wxFontMgrFontRefData::GetFontBundle() const | |
271 | { | |
272 | wxConstCast(this, wxFontMgrFontRefData)->EnsureValidFont(); | |
273 | return m_fontBundle; | |
274 | } | |
275 | ||
276 | wxFontInstance * | |
277 | wxFontMgrFontRefData::GetFontInstance(float scale, bool antialiased) const | |
278 | { | |
279 | wxConstCast(this, wxFontMgrFontRefData)->EnsureValidFont(); | |
280 | return m_fontFace->GetFontInstance(m_info.pointSize * scale, | |
8462a84b | 281 | antialiased); |
d7ae4a62 VS |
282 | } |
283 | ||
284 | void wxFontMgrFontRefData::SetPointSize(int pointSize) | |
285 | { | |
286 | m_info.pointSize = pointSize; | |
287 | m_fontValid = false; | |
288 | } | |
289 | ||
5a2c086a | 290 | void wxFontMgrFontRefData::SetFamily(wxFontFamily family) |
d7ae4a62 | 291 | { |
5a2c086a | 292 | m_info.family = family; |
d7ae4a62 VS |
293 | m_fontValid = false; |
294 | } | |
295 | ||
5a2c086a | 296 | void wxFontMgrFontRefData::SetStyle(wxFontStyle style) |
d7ae4a62 | 297 | { |
5a2c086a | 298 | m_info.style = style; |
d7ae4a62 VS |
299 | m_fontValid = false; |
300 | } | |
301 | ||
5a2c086a | 302 | void wxFontMgrFontRefData::SetWeight(wxFontWeight weight) |
d7ae4a62 | 303 | { |
5a2c086a | 304 | m_info.weight = weight; |
d7ae4a62 VS |
305 | m_fontValid = false; |
306 | } | |
307 | ||
308 | void wxFontMgrFontRefData::SetFaceName(const wxString& faceName) | |
309 | { | |
310 | m_info.faceName = faceName; | |
311 | m_fontValid = false; | |
312 | } | |
313 | ||
314 | void wxFontMgrFontRefData::SetUnderlined(bool underlined) | |
315 | { | |
316 | m_info.underlined = underlined; | |
317 | m_fontValid = false; | |
318 | } | |
319 | ||
320 | void wxFontMgrFontRefData::SetEncoding(wxFontEncoding encoding) | |
321 | { | |
322 | m_info.encoding = encoding; | |
323 | m_fontValid = false; | |
324 | } | |
325 | ||
d7ae4a62 VS |
326 | void wxFontMgrFontRefData::EnsureValidFont() |
327 | { | |
328 | if ( !m_fontValid ) | |
329 | { | |
330 | wxFontFace *old = m_fontFace; | |
331 | ||
332 | m_fontBundle = wxFontsManager::Get()->GetBundleForFont(*this); | |
333 | m_fontFace = m_fontBundle->GetFaceForFont(*this); | |
334 | ||
335 | if ( old ) | |
336 | old->Release(); | |
337 | } | |
338 | } |